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 | |
| 76 | #include <libnetconf.h> |
| 77 | #include <libxml/tree.h> |
| 78 | #include <libxml/parser.h> |
| 79 | |
| 80 | #define MAX_PROCS 5 |
| 81 | #define SOCKET_FILENAME "/tmp/pcon.sock" |
| 82 | #define MAX_SOCKET_CL 10 |
| 83 | #define BUFFER_SIZE 4096 |
| 84 | |
| 85 | /* sleep in master process for non-blocking socket reading */ |
| 86 | #define SLEEP_TIME 200 |
| 87 | |
| 88 | #ifndef offsetof |
| 89 | #define offsetof(type, member) ((size_t) ((type *) 0)->member) |
| 90 | #endif |
| 91 | |
| 92 | struct timeval timeout = { 1, 0 }; |
| 93 | |
| 94 | #define MSG_OK 0 |
| 95 | #define MSG_OPEN 1 |
| 96 | #define MSG_DATA 2 |
| 97 | #define MSG_CLOSE 3 |
| 98 | #define MSG_ERROR 4 |
| 99 | #define MSG_UNKNOWN 5 |
| 100 | |
| 101 | typedef struct sck_message { |
| 102 | uint8_t type; |
| 103 | char session_key[APR_SHA1_DIGESTSIZE + 1]; |
| 104 | } __attribute__ ((packed)) sck_message_t; |
| 105 | |
| 106 | module AP_MODULE_DECLARE_DATA netconf_module; |
| 107 | |
| 108 | typedef struct { |
| 109 | apr_proc_t *forkproc; |
| 110 | apr_pool_t *pool; |
| 111 | uint32_t count; |
| 112 | } mod_netconf_srv_cfg; |
| 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; |
| 124 | fprintf(stderr, "got TERM signal... %s\n", |
| 125 | apr_signal_description_get(sign)); |
| 126 | break; |
| 127 | default: |
| 128 | printf("%s\n", apr_signal_description_get(sign)); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static int gen_ncsession_hash(char *s, const int len, const char* hostname, const char* port, const char* sid) |
| 134 | { |
| 135 | if (s == NULL || len != (APR_SHA1_DIGESTSIZE + 1)) { |
| 136 | return (APR_EINVAL); |
| 137 | } |
| 138 | apr_sha1_ctx_t sha1_ctx; |
| 139 | apr_sha1_init(&sha1_ctx); |
| 140 | apr_sha1_update(&sha1_ctx, hostname, strlen(hostname)); |
| 141 | apr_sha1_update(&sha1_ctx, port, strlen(port)); |
| 142 | apr_sha1_update(&sha1_ctx, sid, strlen(sid)); |
| 143 | apr_sha1_final((unsigned char*)s, &sha1_ctx); |
| 144 | |
| 145 | /* add missing terminating null byte */ |
| 146 | s[APR_SHA1_DIGESTSIZE] = 0; |
| 147 | |
| 148 | return (APR_SUCCESS); |
| 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 | |
| 187 | static void handle_msg_open(server_rec* server, int client, apr_hash_t* conns, char** address) |
| 188 | { |
| 189 | sck_message_t message; |
| 190 | struct nc_session* session; |
| 191 | char *hostname, *port, *username, *sid; |
| 192 | |
| 193 | hostname = address[0]; |
| 194 | port = address[1]; |
| 195 | username = address[2]; |
| 196 | |
| 197 | /* connect to the requested NETCONF server */ |
| 198 | password = address[3]; |
| 199 | session = nc_session_connect(hostname, (unsigned short) atoi (port), username, NULL); |
| 200 | password = NULL; |
| 201 | |
| 202 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "host: %s, port: %s, user: %s", hostname, port, username); |
| 203 | |
| 204 | /* clear plaintext password */ |
| 205 | size_t size = strlen(address[3]); |
| 206 | memset(address[3], 0, size + 1); |
| 207 | |
| 208 | /* if connected successful, add session to the list */ |
| 209 | if (session != NULL) { |
| 210 | /* generate hash for the session */ |
| 211 | sid = nc_session_get_id(session); |
| 212 | gen_ncsession_hash(message.session_key, APR_SHA1_DIGESTSIZE + 1, hostname, port, sid); |
| 213 | free(sid); |
| 214 | |
| 215 | apr_hash_set(conns, message.session_key, APR_HASH_KEY_STRING, (void *) session); |
| 216 | send(client, &message, sizeof(message), 0); |
| 217 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session established"); |
| 218 | return; |
| 219 | } else { |
| 220 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Connection could not be established"); |
| 221 | } |
| 222 | |
| 223 | message.type = MSG_ERROR; |
| 224 | memset(&message.session_key, 0, sizeof(message.session_key)); |
| 225 | send(client, &message, sizeof(message), 0); |
| 226 | } |
| 227 | |
| 228 | static void handle_msg_close(server_rec* server, int client, apr_hash_t* conns, char* session_key) |
| 229 | { |
| 230 | struct nc_session *ns = NULL; |
| 231 | sck_message_t reply; |
| 232 | |
| 233 | ns = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 234 | if (ns != NULL) { |
| 235 | nc_session_close (ns, "NETCONF session closed by client"); |
| 236 | nc_session_free (ns); |
| 237 | ns = NULL; |
| 238 | |
| 239 | /* remove session from the active sessions list */ |
| 240 | apr_hash_set(conns, session_key, APR_HASH_KEY_STRING, NULL); |
| 241 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF session closed"); |
| 242 | reply.type = MSG_OK; |
| 243 | memset(&reply.session_key, 0, sizeof(reply.session_key)); |
| 244 | send(client, &reply, sizeof(reply), 0); |
| 245 | } else { |
| 246 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close"); |
| 247 | reply.type = MSG_ERROR; |
| 248 | memset(&reply.session_key, 0, sizeof(reply.session_key)); |
| 249 | send(client, &reply, sizeof(reply), 0); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | static void handle_netconf_request(server_rec* server, int client, apr_hash_t* conns, char* session_key) |
| 254 | { |
| 255 | struct nc_session *session = NULL; |
| 256 | sck_message_t client_reply; |
| 257 | NC_DATASTORE target = NC_DATASTORE_RUNNING; |
| 258 | nc_rpc* rpc; |
| 259 | nc_reply* reply; |
| 260 | char* data, *client_reply_data; |
| 261 | |
| 262 | session = (struct nc_session *)apr_hash_get(conns, session_key, APR_HASH_KEY_STRING); |
| 263 | if (session != NULL) { |
| 264 | /* create requests */ |
| 265 | rpc = nc_rpc_getconfig (target, NULL); |
| 266 | if (rpc == NULL) { |
| 267 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: creating rpc request failed"); |
| 268 | goto error_reply; |
| 269 | } |
| 270 | /* send the request and get the reply */ |
| 271 | nc_session_send_rpc (session, rpc); |
| 272 | if (nc_session_recv_reply (session, &reply) == 0) { |
| 273 | nc_rpc_free (rpc); |
| 274 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
| 275 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: receiving rpc-reply failed"); |
| 276 | handle_msg_close(server, client, conns, session_key); |
| 277 | goto error_reply; |
| 278 | } |
| 279 | /* there is error handled by callback */ |
| 280 | return; |
| 281 | } |
| 282 | nc_rpc_free (rpc); |
| 283 | |
| 284 | switch (nc_reply_get_type (reply)) { |
| 285 | case NC_REPLY_DATA: |
| 286 | case NC_REPLY_ERROR: |
| 287 | data = nc_reply_dump(reply); |
| 288 | break; |
| 289 | default: |
| 290 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: unexpected rpc-reply"); |
| 291 | goto error_reply; |
| 292 | } |
| 293 | nc_reply_free(reply); |
| 294 | |
| 295 | if (!data) { |
| 296 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: no data from reply"); |
| 297 | goto error_reply; |
| 298 | } |
| 299 | |
| 300 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, server, "NETCONF get-config is done"); |
| 301 | client_reply_data = malloc(sizeof(char) * (1 + strlen(data) + 1)); /* 1B message type, message length, 1B terminating null */ |
| 302 | if (client_reply_data == NULL) { |
| 303 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "mod_netconf: allocating memory for client reply failed"); |
| 304 | goto error_reply; |
| 305 | } |
| 306 | client_reply_data[0] = MSG_DATA; |
| 307 | apr_cpystrn(&client_reply_data[1], data, strlen(data) + 1); |
| 308 | send(client, client_reply_data, strlen(client_reply_data) + 1, 0); |
| 309 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "sending message from daemon: %x", client_reply_data[0]); |
| 310 | } else { |
| 311 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown session to close"); |
| 312 | goto error_reply; |
| 313 | } |
| 314 | |
| 315 | return; |
| 316 | |
| 317 | error_reply: |
| 318 | client_reply.type = MSG_ERROR; |
| 319 | memset(&client_reply.session_key, 0, sizeof(client_reply.session_key)); |
| 320 | send(client, &client_reply, sizeof(client_reply), 0); |
| 321 | } |
| 322 | |
| 323 | static void get_target_address(char *address[4], char *cred) |
| 324 | { |
| 325 | int i; |
| 326 | |
| 327 | /* <MSG_TYPE> <host> \0 <port> \0 <user> \0 <pass> \0 */ |
| 328 | |
| 329 | /* hostname */ |
| 330 | address[0] = &cred[0]; |
| 331 | |
| 332 | /* process the rest in loop */ |
| 333 | for (i = 1; i < 4; i++) { |
| 334 | /* port, user, password */ |
| 335 | address[i] = address[i-1] + strlen(address[i-1]) + 1; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | server_rec* clb_print_server; |
| 340 | int clb_print(const char* msg) |
| 341 | { |
| 342 | ap_log_error(APLOG_MARK, APLOG_INFO, 0, clb_print_server, msg); |
| 343 | return (0); |
| 344 | } |
| 345 | |
| 346 | static void forked_proc(apr_pool_t * pool, server_rec * server) |
| 347 | { |
| 348 | struct sockaddr_un local, remote; |
| 349 | int lsock, client; |
| 350 | socklen_t len, len2; |
| 351 | struct pollfd fds; |
| 352 | int status; |
| 353 | |
| 354 | apr_hash_t *netconf_sessions_list; |
| 355 | char buffer[BUFFER_SIZE]; |
| 356 | char *address[4]; |
| 357 | |
| 358 | /* change uid and gid of proccess for security reasons */ |
| 359 | unixd_setup_child(); |
| 360 | |
| 361 | /* create listening UNIX socket to accept incomming connections */ |
| 362 | |
| 363 | if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 364 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Creating socket failed (%s)", strerror(errno)); |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | local.sun_family = AF_UNIX; |
| 369 | strncpy(local.sun_path, SOCKET_FILENAME, sizeof(local.sun_path)); |
| 370 | unlink(local.sun_path); |
| 371 | len = offsetof(struct sockaddr_un, sun_path) + strlen(local.sun_path); |
| 372 | |
| 373 | if (bind(lsock, (struct sockaddr *) &local, len) == -1) { |
| 374 | if (errno == EADDRINUSE) { |
| 375 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "mod_netconf socket address already in use"); |
| 376 | close(lsock); |
| 377 | exit(0); |
| 378 | } |
| 379 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Binding socket failed (%s)", strerror(errno)); |
| 380 | close(lsock); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | if (listen(lsock, MAX_SOCKET_CL) == -1) { |
| 385 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Setting up listen socket failed (%s)", strerror(errno)); |
| 386 | close(lsock); |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | /* prepare internal lists */ |
| 391 | netconf_sessions_list = apr_hash_make(pool); |
| 392 | |
| 393 | /* setup libnetconf's callbacks */ |
| 394 | nc_verbosity(NC_VERB_DEBUG); |
| 395 | clb_print_server = server; |
| 396 | nc_callback_print(clb_print); |
| 397 | nc_callback_ssh_host_authenticity_check(netconf_callback_ssh_hostkey_check); |
| 398 | nc_callback_sshauth_interactive(netconf_callback_sshauth_interactive); |
| 399 | nc_callback_sshauth_password(netconf_callback_sshauth_password); |
| 400 | |
| 401 | /* disable publickey authentication */ |
| 402 | nc_ssh_pref(NC_SSH_AUTH_PUBLIC_KEYS, -1); |
| 403 | |
| 404 | while (isterminated == 0) { |
| 405 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "waiting for another client's request"); |
| 406 | |
| 407 | /* open incoming connection if any */ |
| 408 | len2 = sizeof(remote); |
| 409 | client = accept(lsock, (struct sockaddr *) &remote, &len2); |
| 410 | if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 411 | apr_sleep(SLEEP_TIME); |
| 412 | continue; |
| 413 | } else if (client == -1 && (errno == EINTR)) { |
| 414 | continue; |
| 415 | } else if (client == -1) { |
| 416 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Accepting mod_netconf client connection failed (%s)", strerror(errno)); |
| 417 | continue; |
| 418 | } |
| 419 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "client's socket accepted."); |
| 420 | |
| 421 | /* set client's socket as non-blocking */ |
| 422 | fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_NONBLOCK); |
| 423 | |
| 424 | while (1) { |
| 425 | fds.fd = client; |
| 426 | fds.events = POLLIN; |
| 427 | fds.revents = 0; |
| 428 | |
| 429 | status = poll(&fds, 1, 100); |
| 430 | |
| 431 | if (status == -1 && errno == EINTR && isterminated == 0) { |
| 432 | /* poll was interrupted - check if the isterminated is set and if not, try poll again */ |
| 433 | continue; |
| 434 | } else if (status <= 0) { |
| 435 | /* 0: poll time outed |
| 436 | * close socket and ignore this request from the client, it can try it again |
| 437 | * -1: poll failed |
| 438 | * something wrong happend, close this socket and wait for another request |
| 439 | */ |
| 440 | close(client); |
| 441 | break; |
| 442 | } |
| 443 | /* status > 0 */ |
| 444 | |
| 445 | /* check the status of the socket */ |
| 446 | |
| 447 | /* if nothing to read and POLLHUP (EOF) or POLLERR set */ |
| 448 | if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) { |
| 449 | /* close client's socket (it's probably already closed by client */ |
| 450 | close(client); |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | if ((len2 = recv(client, buffer, BUFFER_SIZE, 0)) <= 0) { |
| 455 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "receiving failed %d (%s)", errno, strerror(errno)); |
| 456 | continue; |
| 457 | } else { |
| 458 | /* got message from client */ |
| 459 | buffer[len2] = 0; |
| 460 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, server, "received from client: %s", buffer); |
| 461 | |
| 462 | /* message type */ |
| 463 | switch (buffer[0]) { |
| 464 | case MSG_OPEN: |
| 465 | get_target_address(address, &buffer[1]); |
| 466 | handle_msg_open(server, client, netconf_sessions_list, address); |
| 467 | break; |
| 468 | case MSG_DATA: |
| 469 | /* netconf data */ |
| 470 | handle_netconf_request(server, client, netconf_sessions_list, &buffer[1]); |
| 471 | break; |
| 472 | case MSG_CLOSE: |
| 473 | handle_msg_close(server, client, netconf_sessions_list, &buffer[1]); |
| 474 | break; |
| 475 | default: |
| 476 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Unknown mod_netconf message type"); |
| 477 | sck_message_t message; |
| 478 | message.type = MSG_UNKNOWN; |
| 479 | memset(&message.session_key, 0, sizeof(message.session_key)); |
| 480 | send(client, (void *) &message, sizeof(message), 0); |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | close(lsock); |
| 488 | |
| 489 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, server, "Exiting from the mod_netconf daemon"); |
| 490 | |
| 491 | exit(APR_SUCCESS); |
| 492 | } |
| 493 | |
| 494 | static void *mod_netconf_create_srv_conf(apr_pool_t * pool, server_rec * s) |
| 495 | { |
| 496 | mod_netconf_srv_cfg *srv = apr_pcalloc(pool, sizeof(mod_netconf_srv_cfg)); |
| 497 | apr_pool_create(&srv->pool, pool); |
| 498 | srv->forkproc = NULL; |
| 499 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "init netconf module config"); |
| 500 | if (srv == NULL) { |
| 501 | srv = apr_pcalloc(pool, sizeof(mod_netconf_srv_cfg)); |
| 502 | apr_pool_create(&srv->pool, pool); |
| 503 | } |
| 504 | return (void *)srv; |
| 505 | } |
| 506 | |
| 507 | static int mod_netconf_master_init(apr_pool_t * pconf, apr_pool_t * ptemp, |
| 508 | apr_pool_t * plog, server_rec * s) |
| 509 | { |
| 510 | /* These two help ensure that we only init once. */ |
| 511 | void *data; |
| 512 | const char *userdata_key = "netconf_ipc_init_module"; |
| 513 | |
| 514 | /* |
| 515 | * The following checks if this routine has been called before. |
| 516 | * This is necessary because the parent process gets initialized |
| 517 | * a couple of times as the server starts up. |
| 518 | */ |
| 519 | apr_pool_userdata_get(&data, userdata_key, s->process->pool); |
| 520 | if (!data) { |
| 521 | apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool); |
| 522 | return (OK); |
| 523 | } |
| 524 | |
| 525 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "creating mod_netconf master process"); |
| 526 | mod_netconf_srv_cfg *srv = ap_get_module_config(s->module_config, &netconf_module); |
Radek Krejci | dfaa6ea | 2012-07-23 09:04:43 +0200 | [diff] [blame^] | 527 | |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 528 | if (srv->forkproc == NULL) { |
| 529 | srv->forkproc = apr_pcalloc(srv->pool, sizeof(apr_proc_t)); |
| 530 | apr_status_t res = apr_proc_fork(srv->forkproc, srv->pool); |
| 531 | switch (res) { |
| 532 | case APR_INCHILD: |
| 533 | /* set signal handler */ |
| 534 | apr_signal_init(srv->pool); |
| 535 | apr_signal(SIGTERM, signal_handler); |
| 536 | |
| 537 | /* log start of the separated NETCONF communication process */ |
| 538 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf master process created (PID %d)", getpid()); |
| 539 | |
| 540 | /* start main loop providing NETCONF communication */ |
| 541 | forked_proc(srv->pool, s); |
| 542 | |
| 543 | /* I never should be here */ |
| 544 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf master process broke the main loop"); |
| 545 | exit(APR_EGENERAL); |
| 546 | break; |
| 547 | case APR_INPARENT: |
| 548 | /* register child to be killed (SIGTERM) when the module config's pool dies */ |
| 549 | apr_pool_note_subprocess(srv->pool, srv->forkproc, APR_KILL_AFTER_TIMEOUT); |
| 550 | break; |
| 551 | default: |
| 552 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed"); |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | return OK; |
| 558 | } |
| 559 | |
| 560 | static int mod_netconf_fixups(request_rec *r) |
| 561 | { |
| 562 | apr_pool_t *temp_pool = NULL; |
| 563 | char* operation; |
| 564 | char *cred; |
| 565 | int len; |
| 566 | int sock = -1; |
| 567 | char buffer[BUFFER_SIZE]; |
| 568 | struct sockaddr_un addr; |
| 569 | |
| 570 | /* process only requests for the mod_netconf handler */ |
| 571 | if (!r->handler || (strcmp(r->handler, "netconf") != 0)) { |
| 572 | return DECLINED; |
| 573 | } |
| 574 | |
| 575 | /* allow running module only as subrequest */ |
| 576 | if (r->main == NULL) { |
| 577 | return DECLINED; |
| 578 | } |
| 579 | |
| 580 | /* create temporary pool */ |
| 581 | apr_pool_create(&temp_pool, NULL); |
| 582 | |
| 583 | operation = apr_pstrdup(temp_pool, apr_table_get(r->subprocess_env, "NETCONF_OP")); |
| 584 | if (operation == NULL) { |
| 585 | apr_pool_destroy(temp_pool); |
| 586 | return HTTP_INTERNAL_SERVER_ERROR; |
| 587 | } |
| 588 | |
| 589 | /* connect to the daemon */ |
| 590 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 591 | if (sock == -1) { |
| 592 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot create client's mod_netconf socket"); |
| 593 | apr_pool_destroy(temp_pool); |
| 594 | return HTTP_INTERNAL_SERVER_ERROR; |
| 595 | } |
| 596 | addr.sun_family = AF_UNIX; |
| 597 | strncpy(addr.sun_path, SOCKET_FILENAME, sizeof(addr.sun_path)); |
| 598 | len = strlen(addr.sun_path) + sizeof(addr.sun_family); |
| 599 | if (connect(sock, (struct sockaddr *) &addr, len) == -1) { |
| 600 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot connect to the mod_netconf daemon"); |
| 601 | apr_pool_destroy(temp_pool); |
| 602 | return HTTP_INTERNAL_SERVER_ERROR; |
| 603 | } |
| 604 | |
| 605 | /* process the request */ |
| 606 | if (strcmp(operation, "connect") == 0) { |
| 607 | char *host = apr_pstrdup(r->pool, apr_table_get(r->subprocess_env, "NETCONF_HOST")); |
| 608 | char *port = apr_pstrdup(r->pool, apr_table_get(r->subprocess_env, "NETCONF_PORT")); |
| 609 | char *user = apr_pstrdup(r->pool, apr_table_get(r->subprocess_env, "NETCONF_USER")); |
| 610 | char *pass = apr_pstrdup(r->pool, apr_table_get(r->subprocess_env, "NETCONF_PASS")); |
| 611 | |
| 612 | /* <MSG_TYPE><host>\0<port>\0<user>\0<pass>\0 */ |
| 613 | cred = apr_psprintf(temp_pool, " %s %s %s %s", host, port, user, pass); |
| 614 | cred[len = 0] = MSG_OPEN; |
| 615 | cred[len += strlen(host) + 1] = 0; /* <host>\0 */ |
| 616 | cred[len += strlen(port) + 1] = 0; /* <port>\0 */ |
| 617 | cred[len += strlen(user) + 1] = 0; /* <user>\0 */ |
| 618 | /* <pass>\0 is already done from printf, but we need to count index for send */ |
| 619 | cred[len += strlen(pass) + 1] = 0; |
| 620 | |
| 621 | send(sock, cred, len, 0); |
| 622 | len = recv(sock, buffer, BUFFER_SIZE, 0); |
| 623 | |
| 624 | if (len < sizeof(sck_message_t)) { |
| 625 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Invalid reply from mod_netconf daemon"); |
| 626 | apr_pool_destroy(temp_pool); |
| 627 | return HTTP_INTERNAL_SERVER_ERROR; |
| 628 | } else if (buffer[0] != MSG_OK) { |
| 629 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot connect with NETCONF server"); |
| 630 | apr_pool_destroy(temp_pool); |
| 631 | return HTTP_INTERNAL_SERVER_ERROR; |
| 632 | } else { |
| 633 | /* ok, received OK message */ |
| 634 | //apr_table_set(r->subprocess_env, "NETCONF_NSID", &(buffer[1])); |
| 635 | apr_table_set(r->main->subprocess_env, "NETCONF_NSID", "somestring"); |
| 636 | } |
| 637 | } else if (strcmp(operation, "disconnect") == 0) { |
| 638 | |
| 639 | } else if (strcmp(operation, "get-config") == 0) { |
| 640 | |
| 641 | } else { |
| 642 | return HTTP_NOT_IMPLEMENTED; |
| 643 | } |
| 644 | |
| 645 | if (sock != -1) { |
| 646 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "client closes socket"); |
| 647 | close (sock); |
| 648 | } |
| 649 | if (temp_pool != NULL) { |
| 650 | apr_pool_destroy(temp_pool); |
| 651 | } |
| 652 | |
| 653 | return OK; |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Testing content handler |
| 658 | * This function should handle NETCONF and return UI |
| 659 | */ |
| 660 | static int mod_netconf_handler(request_rec * r) |
| 661 | { |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 662 | /* pseudo code of args to apr_proc_create() */ |
| 663 | if (!r->handler || (strcmp(r->handler, "netconf") != 0)) { |
| 664 | return DECLINED; |
| 665 | } |
| 666 | |
Radek Krejci | dfaa6ea | 2012-07-23 09:04:43 +0200 | [diff] [blame^] | 667 | /* everything was done in fixups */ |
Radek Krejci | 469aab8 | 2012-07-22 18:42:20 +0200 | [diff] [blame] | 668 | return OK; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Register module hooks |
| 673 | */ |
| 674 | static void mod_netconf_register_hooks(apr_pool_t * p) |
| 675 | { |
| 676 | ap_hook_fixups(mod_netconf_fixups, NULL, NULL, APR_HOOK_FIRST); |
| 677 | ap_hook_handler(mod_netconf_handler, NULL, NULL, APR_HOOK_MIDDLE); |
| 678 | ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST); |
| 679 | } |
| 680 | |
| 681 | /* Dispatch list for API hooks */ |
| 682 | module AP_MODULE_DECLARE_DATA netconf_module = { |
| 683 | STANDARD20_MODULE_STUFF, |
| 684 | NULL, /* create per-dir config structures */ |
| 685 | NULL, /* merge per-dir config structures */ |
| 686 | mod_netconf_create_srv_conf, /* create per-server config structures */ |
| 687 | NULL, /* merge per-server config structures */ |
| 688 | NULL, /* table of config file commands */ |
| 689 | mod_netconf_register_hooks /* register hooks */ |
| 690 | }; |