Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 1 | /*! |
| 2 | * \file mod_netconf.c |
| 3 | * \brief NETCONF Apache modul for Netopeer |
| 4 | * \author Tomas Cejka <cejkat@cesnet.cz> |
| 5 | * \author Radek Krejci <rkrejci@cesnet.cz> |
| 6 | * \date 2011 |
| 7 | * \date 2012 |
| 8 | */ |
| 9 | /* |
| 10 | * Copyright (C) 2011-2012 CESNET |
| 11 | * |
| 12 | * LICENSE TERMS |
| 13 | * |
| 14 | * Redistribution and use in source and binary forms, with or without |
| 15 | * modification, are permitted provided that the following conditions |
| 16 | * are met: |
| 17 | * 1. Redistributions of source code must retain the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer. |
| 19 | * 2. Redistributions in binary form must reproduce the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer in |
| 21 | * the documentation and/or other materials provided with the |
| 22 | * distribution. |
| 23 | * 3. Neither the name of the Company nor the names of its contributors |
| 24 | * may be used to endorse or promote products derived from this |
| 25 | * software without specific prior written permission. |
| 26 | * |
| 27 | * ALTERNATIVELY, provided that this notice is retained in full, this |
| 28 | * product may be distributed under the terms of the GNU General Public |
| 29 | * License (GPL) version 2 or later, in which case the provisions |
| 30 | * of the GPL apply INSTEAD OF those given above. |
| 31 | * |
| 32 | * This software is provided ``as is'', and any express or implied |
| 33 | * warranties, including, but not limited to, the implied warranties of |
| 34 | * merchantability and fitness for a particular purpose are disclaimed. |
| 35 | * In no event shall the company or contributors be liable for any |
| 36 | * direct, indirect, incidental, special, exemplary, or consequential |
| 37 | * damages (including, but not limited to, procurement of substitute |
| 38 | * goods or services; loss of use, data, or profits; or business |
| 39 | * interruption) however caused and on any theory of liability, whether |
| 40 | * in contract, strict liability, or tort (including negligence or |
| 41 | * otherwise) arising in any way out of the use of this software, even |
| 42 | * if advised of the possibility of such damage. |
| 43 | * |
| 44 | */ |
| 45 | |
| 46 | #include "httpd.h" |
| 47 | #include "http_config.h" |
| 48 | #include "http_protocol.h" |
| 49 | #include "http_request.h" |
| 50 | #include "ap_config.h" |
| 51 | #include "http_log.h" |
| 52 | #include "apu.h" |
| 53 | #include "apr_general.h" |
| 54 | #include "apr_sha1.h" |
| 55 | #include "apr_file_io.h" |
| 56 | |
| 57 | #include <unixd.h> |
| 58 | #include <apr_base64.h> |
| 59 | #include <apr_pools.h> |
| 60 | #include <apr_general.h> |
| 61 | #include <apr_hash.h> |
| 62 | #include <apr_strings.h> |
| 63 | #include <apr_thread_proc.h> |
| 64 | #include <apr_signal.h> |
| 65 | |
| 66 | #include <sys/types.h> |
| 67 | #include <sys/socket.h> |
| 68 | #include <sys/un.h> |
| 69 | #include <stdlib.h> |
| 70 | #include <stdio.h> |
| 71 | #include <poll.h> |
| 72 | #include <unistd.h> |
| 73 | #include <string.h> |
| 74 | #include <errno.h> |
| 75 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 76 | #include <json/json.h> |
| 77 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 78 | #include <libnetconf.h> |
| 79 | #include <libxml/tree.h> |
| 80 | #include <libxml/parser.h> |
| 81 | |
| 82 | #define MAX_PROCS 5 |
| 83 | #define SOCKET_FILENAME "/tmp/pcon.sock" |
| 84 | #define MAX_SOCKET_CL 10 |
| 85 | #define BUFFER_SIZE 4096 |
| 86 | |
| 87 | /* sleep in master process for non-blocking socket reading */ |
| 88 | #define SLEEP_TIME 200 |
| 89 | |
| 90 | #ifndef offsetof |
| 91 | #define offsetof(type, member) ((size_t) ((type *) 0)->member) |
| 92 | #endif |
| 93 | |
| 94 | struct timeval timeout = { 1, 0 }; |
| 95 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 96 | typedef enum MSG_TYPE { |
| 97 | REPLY_OK, |
| 98 | REPLY_DATA, |
| 99 | REPLY_ERROR, |
| 100 | MSG_CONNECT, |
| 101 | MSG_DISCONNECT, |
| 102 | MSG_GET, |
| 103 | MSG_GETCONFIG, |
| 104 | MSG_EDITCONFIG, |
| 105 | MSG_COPYCONFIG, |
| 106 | MSG_DELETECONFIG, |
| 107 | MSG_LOCK, |
| 108 | MSG_UNLOCK, |
| 109 | MSG_KILL |
| 110 | } MSG_TYPE; |
| 111 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 112 | #define MSG_OK 0 |
| 113 | #define MSG_OPEN 1 |
| 114 | #define MSG_DATA 2 |
| 115 | #define MSG_CLOSE 3 |
| 116 | #define MSG_ERROR 4 |
| 117 | #define MSG_UNKNOWN 5 |
| 118 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 119 | module AP_MODULE_DECLARE_DATA netconf_module; |
| 120 | |
| 121 | typedef struct { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 122 | apr_pool_t *pool; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 123 | apr_proc_t *forkproc; |
| 124 | char* sockname; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 125 | } mod_netconf_cfg; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 126 | |
| 127 | volatile int isterminated = 0; |
| 128 | |
| 129 | static char* password; |
| 130 | |
| 131 | |
| 132 | static void signal_handler(int sign) |
| 133 | { |
| 134 | switch (sign) { |
| 135 | case SIGTERM: |
| 136 | isterminated = 1; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 141 | static char* gen_ncsession_hash( const char* hostname, const char* port, const char* sid) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 142 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 143 | unsigned char hash_raw[APR_SHA1_DIGESTSIZE]; |
| 144 | int i; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 145 | char* hash; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 146 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 147 | apr_sha1_ctx_t sha1_ctx; |
| 148 | apr_sha1_init(&sha1_ctx); |
| 149 | apr_sha1_update(&sha1_ctx, hostname, strlen(hostname)); |
| 150 | apr_sha1_update(&sha1_ctx, port, strlen(port)); |
| 151 | apr_sha1_update(&sha1_ctx, sid, strlen(sid)); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 152 | apr_sha1_final(hash_raw, &sha1_ctx); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 153 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 154 | /* convert binary hash into hex string, which is printable */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 155 | hash = malloc(sizeof(char) * ((2*APR_SHA1_DIGESTSIZE)+1)); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 156 | for (i = 0; i < APR_SHA1_DIGESTSIZE; i++) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 157 | snprintf(hash + (2*i), 3, "%02x", hash_raw[i]); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 158 | } |
| 159 | //hash[2*APR_SHA1_DIGESTSIZE] = 0; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 160 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 161 | return (hash); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | int netconf_callback_ssh_hostkey_check (const char* hostname, int keytype, const char* fingerprint) |
| 165 | { |
| 166 | /* always approve */ |
| 167 | return (EXIT_SUCCESS); |
| 168 | } |
| 169 | |
| 170 | char* netconf_callback_sshauth_password (const char* username, const char* hostname) |
| 171 | { |
| 172 | char* buf; |
| 173 | |
| 174 | buf = malloc ((strlen(password) + 1) * sizeof(char)); |
| 175 | apr_cpystrn(buf, password, strlen(password) + 1); |
| 176 | |
| 177 | return (buf); |
| 178 | } |
| 179 | |
| 180 | void netconf_callback_sshauth_interactive (const char* name, |
| 181 | int name_len, |
| 182 | const char* instruction, |
| 183 | int instruction_len, |
| 184 | int num_prompts, |
| 185 | const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts, |
| 186 | LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses, |
| 187 | void** abstract) |
| 188 | { |
| 189 | int i; |
| 190 | |
| 191 | for (i=0; i<num_prompts; i++) { |
| 192 | responses[i].text = malloc ((strlen(password) + 1) * sizeof(char)); |
| 193 | apr_cpystrn(responses[i].text, password, strlen(password) + 1); |
| 194 | responses[i].length = strlen(responses[i].text) + 1; |
| 195 | } |
| 196 | |
| 197 | return; |
| 198 | } |
| 199 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 200 | 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] | 201 | { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 202 | struct nc_session* session; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 203 | char *sid; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 204 | char *session_key; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 205 | |
| 206 | /* connect to the requested NETCONF server */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 207 | password = (char*)pass; |
| 208 | session = nc_session_connect(host, (unsigned short) atoi (port), user, NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 209 | |
| 210 | /* if connected successful, add session to the list */ |
| 211 | if (session != NULL) { |
| 212 | /* generate hash for the session */ |
| 213 | sid = nc_session_get_id(session); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 214 | session_key = gen_ncsession_hash(host, port, sid); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 215 | free(sid); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 216 | 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] | 217 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 218 | return (session_key); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 219 | } else { |
| 220 | 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^] | 221 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 226 | 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] | 227 | { |
| 228 | struct nc_session *ns = NULL; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 229 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 230 | 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] | 231 | ns = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 232 | if (ns != NULL) { |
| 233 | nc_session_close (ns, "NETCONF session closed by client"); |
| 234 | nc_session_free (ns); |
| 235 | ns = NULL; |
| 236 | |
| 237 | /* remove session from the active sessions list */ |
| 238 | apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL); |
| 239 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed"); |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 240 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 241 | return (EXIT_SUCCESS); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 242 | } else { |
| 243 | 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^] | 244 | return (EXIT_FAILURE); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 248 | static char* netconf_getconfig(server_rec* server, apr_hash_t* conns, char* session_key, NC_DATASTORE source, const char* filter) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 249 | { |
| 250 | struct nc_session *session = NULL; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 251 | nc_rpc* rpc; |
| 252 | nc_reply* reply; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 253 | char* data; |
| 254 | struct nc_filter *f = NULL; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 255 | |
| 256 | session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 257 | if (session != NULL) { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 258 | /* create filter if set */ |
| 259 | if (filter != NULL) { |
| 260 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 261 | } |
| 262 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 263 | /* create requests */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 264 | rpc = nc_rpc_getconfig (source, f); |
| 265 | nc_filter_free(f); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 266 | if (rpc == NULL) { |
| 267 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 268 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 269 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 270 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 271 | /* send the request and get the reply */ |
| 272 | nc_session_send_rpc (session, rpc); |
| 273 | if (nc_session_recv_reply (session, &reply) == 0) { |
| 274 | nc_rpc_free (rpc); |
| 275 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 276 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 277 | netconf_close(server, conns, session_key); |
| 278 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 279 | } |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 280 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 281 | /* there is error handled by callback */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 282 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 283 | } |
| 284 | nc_rpc_free (rpc); |
| 285 | |
| 286 | switch (nc_reply_get_type (reply)) { |
| 287 | case NC_REPLY_DATA: |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 288 | if ((data = nc_reply_get_data (reply)) == NULL) { |
| 289 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply"); |
| 290 | return (NULL); |
| 291 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 292 | break; |
| 293 | default: |
| 294 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply"); |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 295 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 296 | } |
| 297 | nc_reply_free(reply); |
| 298 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 299 | return (data); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 300 | } else { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 301 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 302 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 303 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 306 | static char* netconf_get(server_rec* server, apr_hash_t* conns, char* session_key, const char* filter) |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 307 | { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 308 | struct nc_session *session = NULL; |
| 309 | nc_rpc* rpc; |
| 310 | nc_reply* reply; |
| 311 | char* data; |
| 312 | struct nc_filter *f = NULL; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 313 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 314 | session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 315 | if (session != NULL) { |
| 316 | /* create filter if set */ |
| 317 | if (filter != NULL) { |
| 318 | f = nc_filter_new(NC_FILTER_SUBTREE, filter); |
| 319 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 320 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 321 | /* create requests */ |
| 322 | rpc = nc_rpc_get (f); |
| 323 | nc_filter_free(f); |
| 324 | if (rpc == NULL) { |
| 325 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 326 | return (NULL); |
| 327 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 328 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 329 | /* send the request and get the reply */ |
| 330 | nc_session_send_rpc (session, rpc); |
| 331 | if (nc_session_recv_reply (session, &reply) == 0) { |
| 332 | nc_rpc_free (rpc); |
| 333 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 334 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
| 335 | netconf_close(server, conns, session_key); |
| 336 | return (NULL); |
| 337 | } |
| 338 | |
| 339 | /* there is error handled by callback */ |
| 340 | return (NULL); |
| 341 | } |
| 342 | nc_rpc_free (rpc); |
| 343 | |
| 344 | switch (nc_reply_get_type (reply)) { |
| 345 | case NC_REPLY_DATA: |
| 346 | if ((data = nc_reply_get_data (reply)) == NULL) { |
| 347 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply"); |
| 348 | return (NULL); |
| 349 | } |
| 350 | break; |
| 351 | default: |
| 352 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply"); |
| 353 | return (NULL); |
| 354 | } |
| 355 | nc_reply_free(reply); |
| 356 | |
| 357 | return (data); |
| 358 | } else { |
| 359 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to process."); |
| 360 | return (NULL); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | server_rec* clb_print_server; |
| 365 | int clb_print(const char* msg) |
| 366 | { |
| 367 | ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg); |
| 368 | return (0); |
| 369 | } |
| 370 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 371 | /* |
| 372 | * This is actually implementation of NETCONF client |
| 373 | * - requests are received from UNIX socket in the predefined format |
| 374 | * - results are replied through the same way |
| 375 | * - the daemon run as a separate process, but it is started and stopped |
| 376 | * automatically by Apache. |
| 377 | * |
| 378 | */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 379 | static void forked_proc(apr_pool_t * pool, server_rec * server) |
| 380 | { |
| 381 | struct sockaddr_un local, remote; |
| 382 | int lsock, client; |
| 383 | socklen_t len, len2; |
| 384 | struct pollfd fds; |
| 385 | int status; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 386 | mod_netconf_cfg *config; |
| 387 | json_object *request, *reply; |
| 388 | int operation; |
| 389 | char* session_key, *data; |
| 390 | const char *msgtext; |
| 391 | const char *host, *port, *user, *pass; |
| 392 | const char *target, *source, *filter; |
| 393 | NC_DATASTORE ds_type1, ds_type2; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 394 | |
| 395 | apr_hash_t *netconf_sessions_list; |
| 396 | char buffer[BUFFER_SIZE]; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 397 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 398 | /* change uid and gid of process for security reasons */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 399 | unixd_setup_child(); |
| 400 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 401 | config = ap_get_module_config(server->module_config, &netconf_module); |
| 402 | if (config == NULL) { |
| 403 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Getting mod_netconf configuration failed"); |
| 404 | return; |
| 405 | } |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 406 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 407 | /* create listening UNIX socket to accept incoming connections */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 408 | if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 409 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno)); |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | local.sun_family = AF_UNIX; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 414 | strncpy(local.sun_path, config->sockname, sizeof(local.sun_path)); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 415 | unlink(local.sun_path); |
| 416 | len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path); |
| 417 | |
| 418 | if (bind(lsock, (struct sockaddr *) &local, len) == -1) { |
| 419 | if (errno == EADDRINUSE) { |
| 420 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use"); |
| 421 | close(lsock); |
| 422 | exit(0); |
| 423 | } |
| 424 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno)); |
| 425 | close(lsock); |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | if (listen(lsock, MAX_SOCKET_CL) == -1) { |
| 430 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno)); |
| 431 | close(lsock); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | /* prepare internal lists */ |
| 436 | netconf_sessions_list = apr_hash_make(pool); |
| 437 | |
| 438 | /* setup libnetconf's callbacks */ |
| 439 | nc_verbosity(NC_VERB_DEBUG); |
| 440 | clb_print_server = server; |
| 441 | nc_callback_print(clb_print); |
| 442 | nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check); |
| 443 | nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive); |
| 444 | nc_callback_sshauth_password(netconf_callback_sshauth_password); |
| 445 | |
| 446 | /* disable publickey authentication */ |
| 447 | nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1); |
| 448 | |
| 449 | while (isterminated == 0) { |
| 450 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request"); |
| 451 | |
| 452 | /* open incoming connection if any */ |
| 453 | len2 = sizeof(remote); |
| 454 | client = accept(lsock, (struct sockaddr *) &remote, &len2); |
| 455 | if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 456 | apr_sleep(SLEEP_TIME); |
| 457 | continue; |
| 458 | } else if (client == -1 && (errno == EINTR)) { |
| 459 | continue; |
| 460 | } else if (client == -1) { |
| 461 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno)); |
| 462 | continue; |
| 463 | } |
| 464 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "client's socket accepted."); |
| 465 | |
| 466 | /* set client's socket as non-blocking */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 467 | //fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 468 | |
| 469 | while (1) { |
| 470 | fds.fd = client; |
| 471 | fds.events = POLLIN; |
| 472 | fds.revents = 0; |
| 473 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 474 | status = poll(&fds, 1, 1000); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 475 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 476 | if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 477 | /* 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^] | 478 | //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "poll interrupted"); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 479 | continue; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 480 | } else if (status < 0) { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 481 | /* 0: poll time outed |
| 482 | * close socket and ignore this request from the client, it can try it again |
| 483 | * -1: poll failed |
| 484 | * something wrong happend, close this socket and wait for another request |
| 485 | */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 486 | //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] | 487 | close(client); |
| 488 | break; |
| 489 | } |
| 490 | /* status > 0 */ |
| 491 | |
| 492 | /* check the status of the socket */ |
| 493 | |
| 494 | /* if nothing to read and POLLHUP (EOF) or POLLERR set */ |
| 495 | if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) { |
| 496 | /* close client's socket (it's probably already closed by client */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 497 | //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] | 498 | close(client); |
| 499 | break; |
| 500 | } |
| 501 | |
| 502 | if ((len2 = recv(client, buffer, BUFFER_SIZE, 0)) <= 0) { |
| 503 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "receiving failed %d (%s)", errno, strerror(errno)); |
| 504 | continue; |
| 505 | } else { |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 506 | request = json_tokener_parse(buffer); |
| 507 | operation = json_object_get_int(json_object_object_get(request, "type")); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 508 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 509 | switch (operation) { |
| 510 | case MSG_CONNECT: |
| 511 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Connect"); |
| 512 | |
| 513 | host = json_object_get_string(json_object_object_get(request, "host")); |
| 514 | port = json_object_get_string(json_object_object_get(request, "port")); |
| 515 | user = json_object_get_string(json_object_object_get(request, "user")); |
| 516 | pass = json_object_get_string(json_object_object_get(request, "pass")); |
| 517 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", host, port, user); |
| 518 | session_key = netconf_connect(server, pool, netconf_sessions_list, host, port, user, pass); |
| 519 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "hash: %s", session_key); |
| 520 | |
| 521 | reply = json_object_new_object(); |
| 522 | if (session_key == NULL) { |
| 523 | /* negative reply */ |
| 524 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 525 | json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed.")); |
| 526 | } else { |
| 527 | /* positive reply */ |
| 528 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 529 | json_object_object_add(reply, "session", json_object_new_string(session_key)); |
| 530 | } |
| 531 | |
| 532 | free(session_key); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 533 | break; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 534 | case MSG_GET: |
| 535 | session_key = (char*)json_object_get_string(json_object_object_get(request, "session")); |
| 536 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key); |
| 537 | /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */ |
| 538 | |
| 539 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 540 | |
| 541 | reply = json_object_new_object(); |
| 542 | |
| 543 | if ((data = netconf_get(server, netconf_sessions_list, session_key, filter)) == NULL) { |
| 544 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 545 | json_object_object_add(reply, "error-message", json_object_new_string("get-config failed.")); |
| 546 | break; |
| 547 | } |
| 548 | |
| 549 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 550 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 551 | break; |
| 552 | case MSG_GETCONFIG: |
| 553 | session_key = (char*)json_object_get_string(json_object_object_get(request, "session")); |
| 554 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: get-config (session %s)", session_key); |
| 555 | /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */ |
| 556 | |
| 557 | source = json_object_get_string(json_object_object_get(request, "source")); |
| 558 | filter = json_object_get_string(json_object_object_get(request, "filter")); |
| 559 | |
| 560 | reply = json_object_new_object(); |
| 561 | |
| 562 | if (strcmp(source, "running") == 0) { |
| 563 | ds_type1 = NC_DATASTORE_RUNNING; |
| 564 | } else if (strcmp(source, "startup") == 0) { |
| 565 | ds_type1 = NC_DATASTORE_STARTUP; |
| 566 | } else if (strcmp(source, "candidate") == 0) { |
| 567 | ds_type1 = NC_DATASTORE_CANDIDATE; |
| 568 | } else { |
| 569 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 570 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid target repository type requested.")); |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | if ((data = netconf_getconfig(server, netconf_sessions_list, session_key, ds_type1, filter)) == NULL) { |
| 575 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 576 | json_object_object_add(reply, "error-message", json_object_new_string("get-config failed.")); |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | json_object_object_add(reply, "type", json_object_new_int(REPLY_DATA)); |
| 581 | json_object_object_add(reply, "data", json_object_new_string(data)); |
| 582 | break; |
| 583 | case MSG_DISCONNECT: |
| 584 | session_key = (char*)json_object_get_string(json_object_object_get(request, "session")); |
| 585 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "Request: Disconnect session %s", session_key); |
| 586 | /* DO NOT FREE session_key HERE, IT IS PART OF REQUEST */ |
| 587 | |
| 588 | reply = json_object_new_object(); |
| 589 | if (netconf_close(server, netconf_sessions_list, session_key) != EXIT_SUCCESS) { |
| 590 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 591 | json_object_object_add(reply, "error-message", json_object_new_string("Invalid session identifier.")); |
| 592 | } else { |
| 593 | json_object_object_add(reply, "type", json_object_new_int(REPLY_OK)); |
| 594 | } |
| 595 | break; |
| 596 | default: |
| 597 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf operation requested (%d)", operation); |
| 598 | reply = json_object_new_object(); |
| 599 | json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR)); |
| 600 | json_object_object_add(reply, "error-message", json_object_new_string("Operation not supported.")); |
| 601 | break; |
| 602 | } |
| 603 | json_object_put(request); |
| 604 | |
| 605 | /* send reply to caller */ |
| 606 | if (reply != NULL) { |
| 607 | msgtext = json_object_to_json_string(reply); |
| 608 | send(client, msgtext, strlen(msgtext) + 1, 0); |
| 609 | json_object_put(reply); |
| 610 | } else { |
| 611 | break; |
| 612 | } |
| 613 | |
| 614 | #if 0 |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 615 | case MSG_DATA: |
| 616 | /* netconf data */ |
| 617 | handle_netconf_request(server, client, netconf_sessions_list, &buffer[1]); |
| 618 | break; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 619 | #endif |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | close(lsock); |
| 625 | |
| 626 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon"); |
| 627 | |
| 628 | exit(APR_SUCCESS); |
| 629 | } |
| 630 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 631 | 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] | 632 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 633 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "Init netconf module config"); |
| 634 | |
| 635 | mod_netconf_cfg *config = apr_pcalloc(pool, sizeof(mod_netconf_cfg)); |
| 636 | apr_pool_create(&config->pool, pool); |
| 637 | config->forkproc = NULL; |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 638 | config->sockname = SOCKET_FILENAME; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 639 | |
| 640 | return (void *)config; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp, |
| 644 | apr_pool_t * plog, server_rec * s) |
| 645 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 646 | mod_netconf_cfg *config; |
| 647 | apr_status_t res; |
| 648 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 649 | /* These two help ensure that we only init once. */ |
| 650 | void *data; |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 651 | const char *userdata_key = "netconf_ipc_init"; |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 652 | |
| 653 | /* |
| 654 | * The following checks if this routine has been called before. |
| 655 | * This is necessary because the parent process gets initialized |
| 656 | * a couple of times as the server starts up. |
| 657 | */ |
| 658 | apr_pool_userdata_get(&data, userdata_key, s->process->pool); |
| 659 | if (!data) { |
| 660 | apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool); |
| 661 | return (OK); |
| 662 | } |
| 663 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 664 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf daemon"); |
| 665 | config = ap_get_module_config(s->module_config, &netconf_module); |
Radek Krejci | dfaa6ea | 2012-07-23 09:04:43 +0200 | [diff] [blame] | 666 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 667 | if (config && config->forkproc == NULL) { |
| 668 | config->forkproc = apr_pcalloc(config->pool, sizeof(apr_proc_t)); |
| 669 | res = apr_proc_fork(config->forkproc, config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 670 | switch (res) { |
| 671 | case APR_INCHILD: |
| 672 | /* set signal handler */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 673 | apr_signal_init(config->pool); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 674 | apr_signal(SIGTERM, signal_handler); |
| 675 | |
| 676 | /* log start of the separated NETCONF communication process */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 677 | 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] | 678 | |
| 679 | /* start main loop providing NETCONF communication */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 680 | forked_proc(config->pool, s); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 681 | |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 682 | /* I never should be here, wtf?!? */ |
| 683 | 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] | 684 | exit(APR_EGENERAL); |
| 685 | break; |
| 686 | case APR_INPARENT: |
| 687 | /* 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] | 688 | apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT); |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 689 | break; |
| 690 | default: |
| 691 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed"); |
| 692 | break; |
| 693 | } |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 694 | } else { |
| 695 | 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] | 696 | } |
| 697 | |
| 698 | return OK; |
| 699 | } |
| 700 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 701 | /** |
| 702 | * Register module hooks |
| 703 | */ |
| 704 | static void mod_netconf_register_hooks(apr_pool_t * p) |
| 705 | { |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 706 | 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] | 707 | } |
| 708 | |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 709 | static const char* cfg_set_socket_path(cmd_parms* cmd, void* cfg, const char* arg) |
| 710 | { |
| 711 | ((mod_netconf_cfg*)cfg)->sockname = apr_pstrdup(cmd->pool, arg); |
| 712 | return NULL; |
| 713 | } |
| 714 | |
| 715 | static const command_rec netconf_cmds[] = { |
| 716 | AP_INIT_TAKE1("NetconfSocket", cfg_set_socket_path, NULL, OR_ALL, "UNIX socket path for mod_netconf communication."), |
| 717 | {NULL} |
| 718 | }; |
| 719 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 720 | /* Dispatch list for API hooks */ |
| 721 | module AP_MODULE_DECLARE_DATA netconf_module = { |
| 722 | STANDARD20_MODULE_STUFF, |
| 723 | NULL, /* create per-dir config structures */ |
| 724 | NULL, /* merge per-dir config structures */ |
Radek Krejci | f23850c | 2012-07-23 16:14:17 +0200 | [diff] [blame] | 725 | mod_netconf_create_conf, /* create per-server config structures */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 726 | NULL, /* merge per-server config structures */ |
Radek Krejci | 8fd1f5e | 2012-07-24 17:33:36 +0200 | [diff] [blame^] | 727 | netconf_cmds, /* table of config file commands */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 728 | mod_netconf_register_hooks /* register hooks */ |
| 729 | }; |