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