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 | |
Radek Krejci | 7b4ddd0 | 2012-07-30 08:09:58 +0200 | [diff] [blame] | 46 | #include <unistd.h> |
| 47 | #include <poll.h> |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 48 | #include <sys/types.h> |
| 49 | #include <sys/socket.h> |
| 50 | #include <sys/un.h> |
Radek Krejci | 7b4ddd0 | 2012-07-30 08:09:58 +0200 | [diff] [blame] | 51 | |
| 52 | #include <unixd.h> |
| 53 | #include <httpd.h> |
| 54 | #include <http_log.h> |
| 55 | #include <http_config.h> |
| 56 | |
| 57 | #include <apr_sha1.h> |
| 58 | #include <apr_hash.h> |
| 59 | #include <apr_signal.h> |
| 60 | #include <apr_strings.h> |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 61 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 62 | #include <json/json.h> |
| 63 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 64 | #include <libnetconf.h> |
Radek Krejci | 7b4ddd0 | 2012-07-30 08:09:58 +0200 | [diff] [blame] | 65 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 66 | |
| 67 | #define MAX_PROCS 5 |
Radek Krejci | 6cb0898 | 2012-07-25 18:01:06 +0200 | [diff] [blame] | 68 | #define SOCKET_FILENAME "/tmp/mod_netconf.sock" |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 69 | #define MAX_SOCKET_CL 10 |
| 70 | #define BUFFER_SIZE 4096 |
| 71 | |
| 72 | /* sleep in master process for non-blocking socket reading */ |
| 73 | #define SLEEP_TIME 200 |
| 74 | |
| 75 | #ifndef offsetof |
| 76 | #define offsetof(type, member) ((size_t) ((type *) 0)->member) |
| 77 | #endif |
| 78 | |
| 79 | struct timeval timeout = { 1, 0 }; |
| 80 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 81 | typedef enum MSG_TYPE { |
| 82 | REPLY_OK, |
| 83 | REPLY_DATA, |
| 84 | REPLY_ERROR, |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 85 | REPLY_INFO, |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 86 | MSG_CONNECT, |
| 87 | MSG_DISCONNECT, |
| 88 | MSG_GET, |
| 89 | MSG_GETCONFIG, |
| 90 | MSG_EDITCONFIG, |
| 91 | MSG_COPYCONFIG, |
| 92 | MSG_DELETECONFIG, |
| 93 | MSG_LOCK, |
| 94 | MSG_UNLOCK, |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 95 | MSG_KILL, |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 96 | MSG_INFO, |
| 97 | MSG_GENERIC |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 98 | } MSG_TYPE; |
| 99 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 100 | #define MSG_OK 0 |
| 101 | #define MSG_OPEN 1 |
| 102 | #define MSG_DATA 2 |
| 103 | #define MSG_CLOSE 3 |
| 104 | #define MSG_ERROR 4 |
| 105 | #define MSG_UNKNOWN 5 |
| 106 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 107 | module AP_MODULE_DECLARE_DATA netconf_module; |
| 108 | |
| 109 | typedef struct { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 110 | apr_pool_t *pool; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 111 | apr_proc_t *forkproc; |
| 112 | char* sockname; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 113 | } mod_netconf_cfg; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 114 | |
| 115 | volatile int isterminated = 0; |
| 116 | |
| 117 | static char* password; |
| 118 | |
| 119 | |
| 120 | static void signal_handler(int sign) |
| 121 | { |
| 122 | switch (sign) { |
| 123 | case SIGTERM: |
| 124 | isterminated = 1; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 129 | 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] | 130 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 131 | unsigned char hash_raw[APR_SHA1_DIGESTSIZE]; |
| 132 | int i; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 133 | char* hash; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 134 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 135 | apr_sha1_ctx_t sha1_ctx; |
| 136 | apr_sha1_init(&sha1_ctx); |
| 137 | apr_sha1_update(&sha1_ctx, hostname, strlen(hostname)); |
| 138 | apr_sha1_update(&sha1_ctx, port, strlen(port)); |
| 139 | apr_sha1_update(&sha1_ctx, sid, strlen(sid)); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 140 | apr_sha1_final(hash_raw, &sha1_ctx); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 141 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 142 | /* convert binary hash into hex string, which is printable */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 143 | hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1)); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 144 | for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 145 | snprintf(hash + (2*i), 3, "%02x", hash_raw[i]); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 146 | } |
| 147 | //hash[2*APR_SHA1_DIGESTSIZE] = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 148 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 149 | return (hash); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint) |
| 153 | { |
| 154 | /* always approve */ |
| 155 | return (EXIT_SUCCESS); |
| 156 | } |
| 157 | |
| 158 | char* netconf_callback_sshauth_password (const char* username, const char* hostname) |
| 159 | { |
| 160 | char* buf; |
| 161 | |
| 162 | buf = malloc ((strlen(password) + 1) * sizeof(char)); |
| 163 | apr_cpystrn(buf, password, strlen(password) + 1); |
| 164 | |
| 165 | return (buf); |
| 166 | } |
| 167 | |
| 168 | void netconf_callback_sshauth_interactive (const char* name, |
| 169 | int name_len, |
| 170 | const char* instruction, |
| 171 | int instruction_len, |
| 172 | int num_prompts, |
| 173 | const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts, |
| 174 | LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses, |
| 175 | void** abstract) |
| 176 | { |
| 177 | int i; |
| 178 | |
| 179 | for (i=0; i<num_prompts; i++) { |
| 180 | responses[i].text = malloc ((strlen(password) + 1) * sizeof(char)); |
| 181 | apr_cpystrn(responses[i].text, password, strlen(password) + 1); |
| 182 | responses[i].length = strlen(responses[i].text) + 1; |
| 183 | } |
| 184 | |
| 185 | return; |
| 186 | } |
| 187 | |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 188 | static json_object *err_reply = NULL; |
| 189 | void netconf_callback_error_process(const char* tag, |
| 190 | const char* type, |
| 191 | const char* severity, |
| 192 | const char* apptag, |
| 193 | const char* path, |
| 194 | const char* message, |
| 195 | const char* attribute, |
| 196 | const char* element, |
| 197 | const char* ns, |
| 198 | const char* sid) |
| 199 | { |
| 200 | err_reply = json_object_new_object(); |
| 201 | json_object_object_add(err_reply, "type", json_object_new_int(REPLY_ERROR)); |
| 202 | if (tag) json_object_object_add(err_reply, "error-tag", json_object_new_string(tag)); |
| 203 | if (type) json_object_object_add(err_reply, "error-type", json_object_new_string(type)); |
| 204 | if (severity) json_object_object_add(err_reply, "error-severity", json_object_new_string(severity)); |
| 205 | if (apptag) json_object_object_add(err_reply, "error-app-tag", json_object_new_string(apptag)); |
| 206 | if (path) json_object_object_add(err_reply, "error-path", json_object_new_string(path)); |
| 207 | if (message) json_object_object_add(err_reply, "error-message", json_object_new_string(message)); |
| 208 | if (attribute) json_object_object_add(err_reply, "bad-attribute", json_object_new_string(attribute)); |
| 209 | if (element) json_object_object_add(err_reply, "bad-element", json_object_new_string(element)); |
| 210 | if (ns) json_object_object_add(err_reply, "bad-namespace", json_object_new_string(ns)); |
| 211 | if (sid) json_object_object_add(err_reply, "session-id", json_object_new_string(sid)); |
| 212 | } |
| 213 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 214 | 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] | 215 | { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 216 | struct nc_session* session; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 217 | char *session_key; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 218 | |
| 219 | /* connect to the requested NETCONF server */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 220 | password = (char*)pass; |
| 221 | session = nc_session_connect(host, (unsigned short) atoi (port), user, NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 222 | |
| 223 | /* if connected successful, add session to the list */ |
| 224 | if (session != NULL) { |
| 225 | /* generate hash for the session */ |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 226 | session_key = gen_ncsession_hash( |
| 227 | (host==NULL) ? "localhost" : host, |
| 228 | (port==NULL) ? "830" : port, |
Radek Krejci | a282bed | 2012-07-27 14:43:45 +0200 | [diff] [blame] | 229 | nc_session_get_id(session)); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 230 | 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] | 231 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 232 | return (session_key); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 233 | } else { |
| 234 | 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] | 235 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 240 | static int netconf_close(server_rec* server, apr_hash_t* conns, const char* session_key) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 241 | { |
| 242 | struct nc_session *ns = NULL; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 243 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 244 | 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] | 245 | ns = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 246 | if (ns != NULL) { |
| 247 | nc_session_close (ns, "NETCONF session closed by client"); |
| 248 | nc_session_free (ns); |
| 249 | ns = NULL; |
| 250 | |
| 251 | /* remove session from the active sessions list */ |
| 252 | apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL); |
| 253 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed"); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 254 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 255 | return (EXIT_SUCCESS); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 256 | } else { |
| 257 | 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] | 258 | return (EXIT_FAILURE); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 262 | static int netconf_op(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 263 | { |
| 264 | struct nc_session *session = NULL; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 265 | nc_reply* reply; |
| 266 | int retval = EXIT_SUCCESS; |
| 267 | |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 268 | /* check requests */ |
| 269 | if (rpc == NULL) { |
| 270 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created"); |
| 271 | return (EXIT_FAILURE); |
| 272 | } |
| 273 | |
| 274 | /* get session where send the RPC */ |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 275 | session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 276 | if (session != NULL) { |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 277 | /* send the request and get the reply */ |
| 278 | nc_session_send_rpc (session, rpc); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 279 | if (nc_session_recv_reply (session, &reply) == 0) { |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 280 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 281 | 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] | 282 | netconf_close(server, conns, session_key); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 283 | return (EXIT_FAILURE); |
| 284 | } |
| 285 | |
| 286 | /* there is error handled by callback */ |
| 287 | return (EXIT_FAILURE); |
| 288 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 289 | |
| 290 | switch (nc_reply_get_type (reply)) { |
| 291 | case NC_REPLY_OK: |
| 292 | retval = EXIT_SUCCESS; |
| 293 | break; |
| 294 | default: |
| 295 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply"); |
| 296 | retval = EXIT_FAILURE; |
| 297 | break; |
| 298 | } |
| 299 | nc_reply_free(reply); |
| 300 | return (retval); |
| 301 | } else { |
| 302 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 303 | return (EXIT_FAILURE); |
| 304 | } |
| 305 | } |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 306 | |
| 307 | static char* netconf_opdata(server_rec* server, apr_hash_t* conns, const char* session_key, nc_rpc* rpc) |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 308 | { |
| 309 | struct nc_session *session = NULL; |
| 310 | nc_reply* reply; |
| 311 | char* data; |
| 312 | |
| 313 | /* check requests */ |
| 314 | if (rpc == NULL) { |
| 315 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: rpc is not created"); |
| 316 | return (NULL); |
| 317 | } |
| 318 | |
| 319 | /* get session where send the RPC */ |
| 320 | session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 321 | if (session != NULL) { |
| 322 | /* send the request and get the reply */ |
| 323 | nc_session_send_rpc (session, rpc); |
| 324 | if (nc_session_recv_reply (session, &reply) == 0) { |
| 325 | nc_rpc_free (rpc); |
| 326 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 327 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
| 328 | netconf_close(server, conns, session_key); |
| 329 | return (NULL); |
| 330 | } |
| 331 | |
| 332 | /* there is error handled by callback */ |
| 333 | return (NULL); |
| 334 | } |
| 335 | nc_rpc_free (rpc); |
| 336 | |
| 337 | switch (nc_reply_get_type (reply)) { |
| 338 | case NC_REPLY_DATA: |
| 339 | if ((data = nc_reply_get_data (reply)) == NULL) { |
| 340 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply"); |
| 341 | return (NULL); |
| 342 | } |
| 343 | break; |
| 344 | default: |
| 345 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply"); |
| 346 | return (NULL); |
| 347 | } |
| 348 | nc_reply_free(reply); |
| 349 | |
| 350 | return (data); |
| 351 | } else { |
| 352 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 353 | return (NULL); |
| 354 | } |
| 355 | } |
| 356 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 357 | static char* netconf_getconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE source, const char* filter) |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 358 | { |
| 359 | nc_rpc* rpc; |
| 360 | struct nc_filter *f = NULL; |
| 361 | char* data = NULL; |
| 362 | |
| 363 | /* create filter if set */ |
| 364 | if (filter != NULL) { |
| 365 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 366 | } |
| 367 | |
| 368 | /* create requests */ |
| 369 | rpc = nc_rpc_getconfig (source, f); |
| 370 | nc_filter_free(f); |
| 371 | if (rpc == NULL) { |
| 372 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 373 | return (NULL); |
| 374 | } |
| 375 | |
| 376 | data = netconf_opdata(server, conns, session_key, rpc); |
| 377 | nc_rpc_free (rpc); |
| 378 | return (data); |
| 379 | } |
| 380 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 381 | static char* netconf_get(server_rec* server, apr_hash_t* conns, const char* session_key, const char* filter) |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 382 | { |
| 383 | nc_rpc* rpc; |
| 384 | struct nc_filter *f = NULL; |
| 385 | char* data = NULL; |
| 386 | |
| 387 | /* create filter if set */ |
| 388 | if (filter != NULL) { |
| 389 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 390 | } |
| 391 | |
| 392 | /* create requests */ |
| 393 | rpc = nc_rpc_get (f); |
| 394 | nc_filter_free(f); |
| 395 | if (rpc == NULL) { |
| 396 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 397 | return (NULL); |
| 398 | } |
| 399 | |
| 400 | data = netconf_opdata(server, conns, session_key, rpc); |
| 401 | nc_rpc_free (rpc); |
| 402 | return (data); |
| 403 | } |
| 404 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 405 | static int netconf_copyconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE source, NC_DATASTORE target, const char* config) |
Radek Krejci | 8e4632a | 2012-07-26 13:40:34 +0200 | [diff] [blame] | 406 | { |
| 407 | nc_rpc* rpc; |
| 408 | int retval = EXIT_SUCCESS; |
| 409 | |
| 410 | /* create requests */ |
| 411 | rpc = nc_rpc_copyconfig(source, target, config); |
| 412 | if (rpc == NULL) { |
| 413 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 414 | return (EXIT_FAILURE); |
| 415 | } |
| 416 | |
| 417 | retval = netconf_op(server, conns, session_key, rpc); |
| 418 | nc_rpc_free (rpc); |
| 419 | return (retval); |
| 420 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 421 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 422 | static int netconf_editconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target, NC_EDIT_DEFOP_TYPE defop, NC_EDIT_ERROPT_TYPE erropt, const char* config) |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 423 | { |
| 424 | nc_rpc* rpc; |
| 425 | int retval = EXIT_SUCCESS; |
| 426 | |
| 427 | /* create requests */ |
| 428 | rpc = nc_rpc_editconfig(target, defop, erropt, config); |
| 429 | if (rpc == NULL) { |
| 430 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 431 | return (EXIT_FAILURE); |
| 432 | } |
| 433 | |
| 434 | retval = netconf_op(server, conns, session_key, rpc); |
| 435 | nc_rpc_free (rpc); |
| 436 | return (retval); |
| 437 | } |
| 438 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 439 | static int netconf_killsession(server_rec* server, apr_hash_t* conns, const char* session_key, const char* sid) |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 440 | { |
| 441 | nc_rpc* rpc; |
| 442 | int retval = EXIT_SUCCESS; |
| 443 | |
| 444 | /* create requests */ |
| 445 | rpc = nc_rpc_killsession(sid); |
| 446 | if (rpc == NULL) { |
| 447 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 448 | return (EXIT_FAILURE); |
| 449 | } |
| 450 | |
| 451 | retval = netconf_op(server, conns, session_key, rpc); |
| 452 | nc_rpc_free (rpc); |
| 453 | return (retval); |
| 454 | } |
| 455 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 456 | static int netconf_onlytargetop(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target, nc_rpc* (*op_func)(NC_DATASTORE)) |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 457 | { |
| 458 | nc_rpc* rpc; |
| 459 | int retval = EXIT_SUCCESS; |
| 460 | |
| 461 | /* create requests */ |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 462 | rpc = op_func(target); |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 463 | if (rpc == NULL) { |
| 464 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 465 | return (EXIT_FAILURE); |
| 466 | } |
| 467 | |
| 468 | retval = netconf_op(server, conns, session_key, rpc); |
| 469 | nc_rpc_free (rpc); |
| 470 | return (retval); |
| 471 | } |
| 472 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 473 | static int netconf_deleteconfig(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 474 | { |
| 475 | return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_deleteconfig)); |
| 476 | } |
| 477 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 478 | static int netconf_lock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 479 | { |
| 480 | return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_lock)); |
| 481 | } |
| 482 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 483 | static int netconf_unlock(server_rec* server, apr_hash_t* conns, const char* session_key, NC_DATASTORE target) |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 484 | { |
| 485 | return (netconf_onlytargetop(server, conns, session_key, target, nc_rpc_unlock)); |
| 486 | } |
| 487 | |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 488 | /** |
| 489 | * @return REPLY_OK: 0, *data == NULL |
| 490 | * REPLY_DATA: 0, *data != NULL |
| 491 | * REPLY_ERROR: 1, *data == NULL |
| 492 | */ |
| 493 | static int netconf_generic(server_rec* server, apr_hash_t* conns, const char* session_key, const char* content, char** data) |
| 494 | { |
| 495 | struct nc_session *session = NULL; |
| 496 | nc_reply* reply; |
| 497 | nc_rpc* rpc; |
| 498 | int retval = EXIT_SUCCESS; |
| 499 | |
| 500 | /* create requests */ |
| 501 | rpc = nc_rpc_generic(content); |
| 502 | if (rpc == NULL) { |
| 503 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 504 | return (EXIT_FAILURE); |
| 505 | } |
| 506 | |
| 507 | *data = NULL; |
| 508 | |
| 509 | /* get session where send the RPC */ |
| 510 | session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 511 | if (session != NULL) { |
| 512 | /* send the request and get the reply */ |
| 513 | nc_session_send_rpc (session, rpc); |
| 514 | if (nc_session_recv_reply (session, &reply) == 0) { |
| 515 | nc_rpc_free (rpc); |
| 516 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 517 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
| 518 | netconf_close(server, conns, session_key); |
| 519 | return (EXIT_FAILURE); |
| 520 | } |
| 521 | |
| 522 | /* there is error handled by callback */ |
| 523 | return (EXIT_FAILURE); |
| 524 | } |
| 525 | nc_rpc_free (rpc); |
| 526 | |
| 527 | switch (nc_reply_get_type (reply)) { |
| 528 | case NC_REPLY_DATA: |
| 529 | if ((*data = nc_reply_get_data (reply)) == NULL) { |
| 530 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply"); |
| 531 | return (EXIT_FAILURE); |
| 532 | } |
| 533 | retval = EXIT_SUCCESS; |
| 534 | break; |
| 535 | case NC_REPLY_OK: |
| 536 | retval = EXIT_SUCCESS; |
| 537 | break; |
| 538 | default: |
| 539 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply"); |
| 540 | retval = EXIT_FAILURE; |
| 541 | break; |
| 542 | } |
| 543 | nc_reply_free(reply); |
| 544 | |
| 545 | return (retval); |
| 546 | } else { |
| 547 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 548 | return (EXIT_FAILURE); |
| 549 | } |
| 550 | } |
| 551 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 552 | server_rec* clb_print_server; |
| 553 | int clb_print(const char* msg) |
| 554 | { |
| 555 | ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg); |
| 556 | return (0); |
| 557 | } |
| 558 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 559 | /* |
| 560 | * This is actually implementation of NETCONF client |
| 561 | * - requests are received from UNIX socket in the predefined format |
| 562 | * - results are replied through the same way |
| 563 | * - the daemon run as a separate process, but it is started and stopped |
| 564 | * automatically by Apache. |
| 565 | * |
| 566 | */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 567 | static void forked_proc(apr_pool_t * pool, server_rec * server) |
| 568 | { |
| 569 | struct sockaddr_un local, remote; |
| 570 | int lsock, client; |
| 571 | socklen_t len, len2; |
| 572 | struct pollfd fds; |
| 573 | int status; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 574 | mod_netconf_cfg *cfg; |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 575 | json_object *request, *reply, *json_obj; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 576 | int operation; |
| 577 | char* session_key, *data; |
Radek Krejci | a282bed | 2012-07-27 14:43:45 +0200 | [diff] [blame] | 578 | const char *msgtext, *cpbltstr; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 579 | const char *host, *port, *user, *pass; |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 580 | const char *target, *source, *filter, *config, *defop, *erropt, *sid; |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 581 | struct nc_session *session = NULL; |
| 582 | struct nc_cpblts* cpblts; |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame] | 583 | NC_DATASTORE ds_type_s, ds_type_t; |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 584 | NC_EDIT_DEFOP_TYPE defop_type = 0; |
| 585 | NC_EDIT_ERROPT_TYPE erropt_type = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 586 | |
| 587 | apr_hash_t *netconf_sessions_list; |
| 588 | char buffer[BUFFER_SIZE]; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 589 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 590 | /* change uid and gid of process for security reasons */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 591 | unixd_setup_child(); |
| 592 | |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 593 | cfg = ap_get_module_config(server->module_config, &netconf_module); |
| 594 | if (cfg == NULL) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 595 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed"); |
| 596 | return; |
| 597 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 598 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 599 | /* create listening UNIX socket to accept incoming connections */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 600 | if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 601 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno)); |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | local.sun_family = AF_UNIX; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 606 | strncpy(local.sun_path, cfg->sockname, sizeof(local.sun_path)); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 607 | unlink(local.sun_path); |
| 608 | len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path); |
| 609 | |
| 610 | if (bind(lsock, (struct sockaddr *) &local, len) == -1) { |
| 611 | if (errno == EADDRINUSE) { |
| 612 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use"); |
| 613 | close(lsock); |
| 614 | exit(0); |
| 615 | } |
| 616 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno)); |
| 617 | close(lsock); |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | if (listen(lsock, MAX_SOCKET_CL) == -1) { |
| 622 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno)); |
| 623 | close(lsock); |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | /* prepare internal lists */ |
| 628 | netconf_sessions_list = apr_hash_make(pool); |
| 629 | |
| 630 | /* setup libnetconf's callbacks */ |
| 631 | nc_verbosity(NC_VERB_DEBUG); |
| 632 | clb_print_server = server; |
| 633 | nc_callback_print(clb_print); |
| 634 | nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check); |
| 635 | nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive); |
| 636 | nc_callback_sshauth_password(netconf_callback_sshauth_password); |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 637 | nc_callback_error_reply(netconf_callback_error_process); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 638 | |
| 639 | /* disable publickey authentication */ |
| 640 | nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1); |
| 641 | |
| 642 | while (isterminated == 0) { |
| 643 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request"); |
| 644 | |
| 645 | /* open incoming connection if any */ |
| 646 | len2 = sizeof(remote); |
| 647 | client = accept(lsock, (struct sockaddr *) &remote, &len2); |
| 648 | if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 649 | apr_sleep(SLEEP_TIME); |
| 650 | continue; |
| 651 | } else if (client == -1 && (errno == EINTR)) { |
| 652 | continue; |
| 653 | } else if (client == -1) { |
| 654 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno)); |
| 655 | continue; |
| 656 | } |
| 657 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "client's socket accepted."); |
| 658 | |
| 659 | /* set client's socket as non-blocking */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 660 | //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 661 | |
| 662 | while (1) { |
| 663 | fds.fd = client; |
| 664 | fds.events = POLLIN; |
| 665 | fds.revents = 0; |
| 666 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 667 | status = poll(&fds, 1, 1000); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 668 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 669 | if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 670 | /* 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] | 671 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted"); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 672 | continue; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 673 | } else if (status < 0) { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 674 | /* 0: poll time outed |
| 675 | * close socket and ignore this request from the client, it can try it again |
| 676 | * -1: poll failed |
| 677 | * something wrong happend, close this socket and wait for another request |
| 678 | */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 679 | //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] | 680 | close(client); |
| 681 | break; |
| 682 | } |
| 683 | /* status > 0 */ |
| 684 | |
| 685 | /* check the status of the socket */ |
| 686 | |
| 687 | /* if nothing to read and POLLHUP (EOF) or POLLERR set */ |
| 688 | if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) { |
| 689 | /* close client's socket (it's probably already closed by client */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 690 | //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] | 691 | close(client); |
| 692 | break; |
| 693 | } |
| 694 | |
| 695 | if ((len2 = recv(client, buffer, BUFFER_SIZE, 0)) <= 0) { |
| 696 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "receiving failed %d (%s)", errno, strerror(errno)); |
| 697 | continue; |
| 698 | } else { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 699 | request = json_tokener_parse(buffer); |
| 700 | operation = json_object_get_int(json_object_object_get(request, "type")); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 701 | |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 702 | session_key = (char*) json_object_get_string(json_object_object_get(request, "session")); |
| 703 | /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */ |
| 704 | if (operation != MSG_CONNECT && session_key == NULL) { |
| 705 | reply = json_object_new_object(); |
| 706 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 707 | json_object_object_add(reply, "error-message", json_object_new_string("Missing session specification.")); |
| 708 | msgtext = json_object_to_json_string(reply); |
| 709 | send(client, msgtext, strlen(msgtext) + 1, 0); |
| 710 | json_object_put(reply); |
| 711 | /* there is some stupid client, so close the connection to give a chance to some other client */ |
| 712 | close(client); |
| 713 | break; |
| 714 | } |
| 715 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame] | 716 | /* get parameters */ |
| 717 | ds_type_t = -1; |
| 718 | if ((target = json_object_get_string(json_object_object_get(request, "target"))) != NULL) { |
| 719 | if (strcmp(target, "running") == 0) { |
| 720 | ds_type_t = NC_DATASTORE_RUNNING; |
| 721 | } else if (strcmp(target, "startup") == 0) { |
| 722 | ds_type_t = NC_DATASTORE_STARTUP; |
| 723 | } else if (strcmp(target, "candidate") == 0) { |
| 724 | ds_type_t = NC_DATASTORE_CANDIDATE; |
| 725 | } |
| 726 | } |
| 727 | ds_type_s = -1; |
| 728 | if ((source = json_object_get_string(json_object_object_get(request, "source"))) != NULL) { |
| 729 | if (strcmp(source, "running") == 0) { |
| 730 | ds_type_s = NC_DATASTORE_RUNNING; |
| 731 | } else if (strcmp(source, "startup") == 0) { |
| 732 | ds_type_s = NC_DATASTORE_STARTUP; |
| 733 | } else if (strcmp(source, "candidate") == 0) { |
| 734 | ds_type_s = NC_DATASTORE_CANDIDATE; |
| 735 | } |
| 736 | } |
| 737 | |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 738 | /* null global JSON error-reply */ |
| 739 | err_reply = NULL; |
| 740 | |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 741 | /* prepare reply envelope */ |
| 742 | reply = json_object_new_object(); |
| 743 | |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 744 | /* process required operation */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 745 | switch (operation) { |
| 746 | case MSG_CONNECT: |
| 747 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect"); |
| 748 | |
| 749 | host = json_object_get_string(json_object_object_get(request, "host")); |
| 750 | port = json_object_get_string(json_object_object_get(request, "port")); |
| 751 | user = json_object_get_string(json_object_object_get(request, "user")); |
| 752 | pass = json_object_get_string(json_object_object_get(request, "pass")); |
| 753 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user); |
| 754 | session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass); |
| 755 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key); |
| 756 | |
| 757 | reply = json_object_new_object(); |
| 758 | if (session_key == NULL) { |
| 759 | /* negative reply */ |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 760 | if (err_reply == NULL) { |
| 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("Connecting NETCONF server failed.")); |
| 763 | } else { |
| 764 | /* use filled err_reply from libnetconf's callback */ |
| 765 | json_object_put(reply); |
| 766 | reply = err_reply; |
| 767 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 768 | } else { |
| 769 | /* positive reply */ |
| 770 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 771 | json_object_object_add(reply, "session", json_object_new_string(session_key)); |
Radek Krejci | e31ad21 | 2012-07-26 12:51:15 +0200 | [diff] [blame] | 772 | |
| 773 | free(session_key); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 774 | } |
| 775 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 776 | break; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 777 | case MSG_GET: |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 778 | 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] | 779 | |
| 780 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 781 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 782 | if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) { |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 783 | if (err_reply == NULL) { |
| 784 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 785 | json_object_object_add(reply, "error-message", json_object_new_string("get failed.")); |
| 786 | } else { |
| 787 | /* use filled err_reply from libnetconf's callback */ |
| 788 | json_object_put(reply); |
| 789 | reply = err_reply; |
| 790 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 791 | } else { |
| 792 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 793 | json_object_object_add(reply, "data", json_object_new_string(data)); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 794 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 795 | break; |
| 796 | case MSG_GETCONFIG: |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 797 | 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] | 798 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 799 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 800 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame] | 801 | if (ds_type_s == -1) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 802 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 803 | 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] | 804 | break; |
| 805 | } |
| 806 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame] | 807 | 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] | 808 | if (err_reply == NULL) { |
| 809 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 810 | json_object_object_add(reply, "error-message", json_object_new_string("get-config failed.")); |
| 811 | } else { |
| 812 | /* use filled err_reply from libnetconf's callback */ |
| 813 | json_object_put(reply); |
| 814 | reply = err_reply; |
| 815 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 816 | } else { |
| 817 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 818 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 819 | } |
| 820 | break; |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 821 | case MSG_EDITCONFIG: |
| 822 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: edit-config (session %s)", session_key); |
| 823 | |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 824 | defop = json_object_get_string(json_object_object_get(request, "default-operation")); |
| 825 | if (defop != NULL) { |
| 826 | if (strcmp(defop, "merge") == 0) { |
| 827 | defop_type = NC_EDIT_DEFOP_MERGE; |
| 828 | } else if (strcmp(defop, "replace") == 0) { |
| 829 | defop_type = NC_EDIT_DEFOP_REPLACE; |
| 830 | } else if (strcmp(defop, "none") == 0) { |
| 831 | defop_type = NC_EDIT_DEFOP_NONE; |
| 832 | } else { |
| 833 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 834 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid default-operation parameter.")); |
| 835 | break; |
| 836 | } |
| 837 | } else { |
| 838 | defop_type = 0; |
| 839 | } |
| 840 | |
| 841 | erropt = json_object_get_string(json_object_object_get(request, "error-option")); |
| 842 | if (erropt != NULL) { |
| 843 | if (strcmp(erropt, "continue-on-error") == 0) { |
| 844 | erropt_type = NC_EDIT_ERROPT_CONT; |
| 845 | } else if (strcmp(erropt, "stop-on-error") == 0) { |
| 846 | erropt_type = NC_EDIT_ERROPT_STOP; |
| 847 | } else if (strcmp(erropt, "rollback-on-error") == 0) { |
| 848 | erropt_type = NC_EDIT_ERROPT_ROLLBACK; |
| 849 | } else { |
| 850 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 851 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid error-option parameter.")); |
| 852 | break; |
| 853 | } |
| 854 | } else { |
| 855 | erropt_type = 0; |
| 856 | } |
| 857 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame] | 858 | if (ds_type_t == -1) { |
Radek Krejci | 62ab34b | 2012-07-26 13:42:05 +0200 | [diff] [blame] | 859 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 860 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested.")); |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | config = json_object_get_string(json_object_object_get(request, "config")); |
| 865 | if (config == NULL) { |
| 866 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 867 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid config data parameter.")); |
| 868 | break; |
| 869 | } |
| 870 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame] | 871 | 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] | 872 | if (err_reply == NULL) { |
| 873 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 874 | json_object_object_add(reply, "error-message", json_object_new_string("edit-config failed.")); |
| 875 | } else { |
| 876 | /* use filled err_reply from libnetconf's callback */ |
| 877 | json_object_put(reply); |
| 878 | reply = err_reply; |
| 879 | } |
| 880 | } else { |
| 881 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 882 | } |
| 883 | break; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 884 | case MSG_COPYCONFIG: |
| 885 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: copy-config (session %s)", session_key); |
Radek Krejci | cebb7af | 2012-07-26 14:58:12 +0200 | [diff] [blame] | 886 | config = NULL; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 887 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame] | 888 | if (source == NULL) { |
| 889 | /* no explicit source specified -> use config data */ |
| 890 | ds_type_s = NC_DATASTORE_NONE; |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 891 | config = json_object_get_string(json_object_object_get(request, "config")); |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame] | 892 | } else if (ds_type_s == -1) { |
| 893 | /* source datastore specified, but it is invalid */ |
| 894 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 895 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid source repository type requested.")); |
| 896 | break; |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 897 | } |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 898 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame] | 899 | if (ds_type_t == -1) { |
| 900 | /* invalid target datastore specified */ |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 901 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 902 | 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] | 903 | break; |
| 904 | } |
| 905 | |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame] | 906 | if (source == NULL && config == NULL) { |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 907 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame] | 908 | 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] | 909 | } else { |
Radek Krejci | fa891e7 | 2012-07-26 14:04:27 +0200 | [diff] [blame] | 910 | 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] | 911 | if (err_reply == NULL) { |
| 912 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 913 | json_object_object_add(reply, "error-message", json_object_new_string("copy-config failed.")); |
| 914 | } else { |
| 915 | /* use filled err_reply from libnetconf's callback */ |
| 916 | json_object_put(reply); |
| 917 | reply = err_reply; |
| 918 | } |
Radek Krejci | ae021c1 | 2012-07-25 18:03:52 +0200 | [diff] [blame] | 919 | } else { |
| 920 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 921 | } |
Radek Krejci | 035bf4e | 2012-07-25 10:59:09 +0200 | [diff] [blame] | 922 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 923 | break; |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 924 | case MSG_DELETECONFIG: |
| 925 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: delete-config (session %s)", session_key); |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 926 | /* no break - unifying code */ |
| 927 | case MSG_LOCK: |
| 928 | if (operation == MSG_LOCK) { |
| 929 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: lock (session %s)", session_key); |
| 930 | } |
| 931 | /* no break - unifying code */ |
| 932 | case MSG_UNLOCK: |
| 933 | if (operation == MSG_UNLOCK) { |
| 934 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: unlock (session %s)", session_key); |
| 935 | } |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 936 | |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 937 | if (ds_type_t == -1) { |
| 938 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 939 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested.")); |
| 940 | break; |
| 941 | } |
| 942 | |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 943 | switch(operation) { |
| 944 | case MSG_DELETECONFIG: |
| 945 | status = netconf_deleteconfig(server, netconf_sessions_list, session_key, ds_type_t); |
| 946 | break; |
| 947 | case MSG_LOCK: |
| 948 | status = netconf_lock(server, netconf_sessions_list, session_key, ds_type_t); |
| 949 | break; |
| 950 | case MSG_UNLOCK: |
| 951 | status = netconf_unlock(server, netconf_sessions_list, session_key, ds_type_t); |
| 952 | break; |
| 953 | default: |
| 954 | status = -1; |
| 955 | break; |
| 956 | } |
| 957 | |
| 958 | if (status != EXIT_SUCCESS) { |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 959 | if (err_reply == NULL) { |
| 960 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
Radek Krejci | 5cd7d42 | 2012-07-26 14:50:29 +0200 | [diff] [blame] | 961 | json_object_object_add(reply, "error-message", json_object_new_string("operation failed.")); |
Radek Krejci | 2f31837 | 2012-07-26 14:22:35 +0200 | [diff] [blame] | 962 | } else { |
| 963 | /* use filled err_reply from libnetconf's callback */ |
| 964 | json_object_put(reply); |
| 965 | reply = err_reply; |
| 966 | } |
| 967 | } else { |
| 968 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 969 | } |
| 970 | break; |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 971 | case MSG_KILL: |
| 972 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: kill-session, session %s", session_key); |
| 973 | |
| 974 | sid = json_object_get_string(json_object_object_get(request, "session-id")); |
| 975 | |
Radek Krejci | e34d3eb | 2012-07-26 15:05:53 +0200 | [diff] [blame] | 976 | if (sid == NULL) { |
| 977 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 978 | json_object_object_add(reply, "error-message", json_object_new_string("Missing session-id parameter.")); |
| 979 | break; |
| 980 | } |
| 981 | |
| 982 | if (netconf_killsession(server, netconf_sessions_list, session_key, sid) != EXIT_SUCCESS) { |
| 983 | if (err_reply == NULL) { |
| 984 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 985 | json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed.")); |
| 986 | } else { |
| 987 | /* use filled err_reply from libnetconf's callback */ |
| 988 | json_object_put(reply); |
| 989 | reply = err_reply; |
| 990 | } |
| 991 | } else { |
| 992 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 993 | } |
| 994 | break; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 995 | case MSG_DISCONNECT: |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 996 | 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] | 997 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 998 | if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) { |
Radek Krejci | c11fd86 | 2012-07-26 12:41:21 +0200 | [diff] [blame] | 999 | if (err_reply == NULL) { |
| 1000 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1001 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier.")); |
| 1002 | } else { |
| 1003 | /* use filled err_reply from libnetconf's callback */ |
| 1004 | json_object_put(reply); |
| 1005 | reply = err_reply; |
| 1006 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1007 | } else { |
| 1008 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1009 | } |
| 1010 | break; |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 1011 | case MSG_INFO: |
| 1012 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get info about session %s", session_key); |
| 1013 | |
| 1014 | session = (struct nc_session *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING); |
| 1015 | if (session != NULL) { |
Radek Krejci | a282bed | 2012-07-27 14:43:45 +0200 | [diff] [blame] | 1016 | json_object_object_add(reply, "sid", json_object_new_string(nc_session_get_id(session))); |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 1017 | json_object_object_add(reply, "version", json_object_new_string((nc_session_get_version(session) == 0)?"1.0":"1.1")); |
Radek Krejci | a282bed | 2012-07-27 14:43:45 +0200 | [diff] [blame] | 1018 | json_object_object_add(reply, "host", json_object_new_string(nc_session_get_host(session))); |
| 1019 | json_object_object_add(reply, "port", json_object_new_string(nc_session_get_port(session))); |
| 1020 | json_object_object_add(reply, "user", json_object_new_string(nc_session_get_user(session))); |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 1021 | cpblts = nc_session_get_cpblts (session); |
| 1022 | if (cpblts != NULL) { |
| 1023 | json_obj = json_object_new_array(); |
| 1024 | nc_cpblts_iter_start (cpblts); |
Radek Krejci | a282bed | 2012-07-27 14:43:45 +0200 | [diff] [blame] | 1025 | while ((cpbltstr = nc_cpblts_iter_next (cpblts)) != NULL) { |
| 1026 | json_object_array_add(json_obj, json_object_new_string(cpbltstr)); |
Radek Krejci | 9e04c7b | 2012-07-26 15:54:25 +0200 | [diff] [blame] | 1027 | } |
| 1028 | json_object_object_add(reply, "capabilities", json_obj); |
| 1029 | } |
| 1030 | } else { |
| 1031 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1032 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier.")); |
| 1033 | } |
| 1034 | |
| 1035 | break; |
Radek Krejci | 80c10d9 | 2012-07-30 08:38:50 +0200 | [diff] [blame^] | 1036 | case MSG_GENERIC: |
| 1037 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: generic request for session %s", session_key); |
| 1038 | |
| 1039 | config = json_object_get_string(json_object_object_get(request, "content")); |
| 1040 | |
| 1041 | if (config == NULL) { |
| 1042 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1043 | json_object_object_add(reply, "error-message", json_object_new_string("Missing content parameter.")); |
| 1044 | break; |
| 1045 | } |
| 1046 | |
| 1047 | if (netconf_generic(server, netconf_sessions_list, session_key, config, &data) != EXIT_SUCCESS) { |
| 1048 | if (err_reply == NULL) { |
| 1049 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1050 | json_object_object_add(reply, "error-message", json_object_new_string("kill-session failed.")); |
| 1051 | } else { |
| 1052 | /* use filled err_reply from libnetconf's callback */ |
| 1053 | json_object_put(reply); |
| 1054 | reply = err_reply; |
| 1055 | } |
| 1056 | } else { |
| 1057 | if (data == NULL) { |
| 1058 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 1059 | } else { |
| 1060 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 1061 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 1062 | } |
| 1063 | } |
| 1064 | break; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1065 | default: |
| 1066 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation); |
| 1067 | reply = json_object_new_object(); |
| 1068 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 1069 | json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported.")); |
| 1070 | break; |
| 1071 | } |
| 1072 | json_object_put(request); |
| 1073 | |
| 1074 | /* send reply to caller */ |
| 1075 | if (reply != NULL) { |
| 1076 | msgtext = json_object_to_json_string(reply); |
| 1077 | send(client, msgtext, strlen(msgtext) + 1, 0); |
| 1078 | json_object_put(reply); |
| 1079 | } else { |
| 1080 | break; |
| 1081 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | close(lsock); |
| 1087 | |
| 1088 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon"); |
| 1089 | |
| 1090 | exit(APR_SUCCESS); |
| 1091 | } |
| 1092 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1093 | 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] | 1094 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1095 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config"); |
| 1096 | |
| 1097 | mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg)); |
| 1098 | apr_pool_create(&config->pool, pool); |
| 1099 | config->forkproc = NULL; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1100 | config->sockname = SOCKET_FILENAME; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1101 | |
| 1102 | return (void *)config; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp, |
| 1106 | apr_pool_t * plog, server_rec * s) |
| 1107 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1108 | mod_netconf_cfg *config; |
| 1109 | apr_status_t res; |
| 1110 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1111 | /* These two help ensure that we only init once. */ |
| 1112 | void *data; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1113 | const char *userdata_key = "netconf_ipc_init"; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1114 | |
| 1115 | /* |
| 1116 | * The following checks if this routine has been called before. |
| 1117 | * This is necessary because the parent process gets initialized |
| 1118 | * a couple of times as the server starts up. |
| 1119 | */ |
| 1120 | apr_pool_userdata_get(&data, userdata_key, s->process->pool); |
| 1121 | if (!data) { |
| 1122 | apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool); |
| 1123 | return (OK); |
| 1124 | } |
| 1125 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1126 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon"); |
| 1127 | config = ap_get_module_config(s->module_config, &netconf_module); |
Radek Krejci | dfaa6ea | 2012-07-23 09:04:43 +0200 | [diff] [blame] | 1128 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1129 | if (config && config->forkproc == NULL) { |
| 1130 | config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t)); |
| 1131 | res = apr_proc_fork(config->forkproc, config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1132 | switch (res) { |
| 1133 | case APR_INCHILD: |
| 1134 | /* set signal handler */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1135 | apr_signal_init(config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1136 | apr_signal(SIGTERM, signal_handler); |
| 1137 | |
| 1138 | /* log start of the separated NETCONF communication process */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1139 | 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] | 1140 | |
| 1141 | /* start main loop providing NETCONF communication */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1142 | forked_proc(config->pool, s); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1143 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1144 | /* I never should be here, wtf?!? */ |
| 1145 | 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] | 1146 | exit(APR_EGENERAL); |
| 1147 | break; |
| 1148 | case APR_INPARENT: |
| 1149 | /* 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] | 1150 | apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1151 | break; |
| 1152 | default: |
| 1153 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed"); |
| 1154 | break; |
| 1155 | } |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1156 | } else { |
| 1157 | 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] | 1158 | } |
| 1159 | |
| 1160 | return OK; |
| 1161 | } |
| 1162 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1163 | /** |
| 1164 | * Register module hooks |
| 1165 | */ |
| 1166 | static void mod_netconf_register_hooks(apr_pool_t * p) |
| 1167 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1168 | 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] | 1169 | } |
| 1170 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1171 | static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg) |
| 1172 | { |
| 1173 | ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg); |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | |
| 1177 | static const command_rec netconf_cmds[] = { |
| 1178 | AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."), |
| 1179 | {NULL} |
| 1180 | }; |
| 1181 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1182 | /* Dispatch list for API hooks */ |
| 1183 | module AP_MODULE_DECLARE_DATA netconf_module = { |
| 1184 | STANDARD20_MODULE_STUFF, |
| 1185 | NULL, /* create per-dir config structures */ |
| 1186 | NULL, /* merge per-dir config structures */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 1187 | mod_netconf_create_conf, /* create per-server config structures */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1188 | NULL, /* merge per-server config structures */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame] | 1189 | netconf_cmds, /* table of config file commands */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1190 | mod_netconf_register_hooks /* register hooks */ |
| 1191 | }; |