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); |
| 527 | /* |
| 528 | apr_signal_init(srv->pool); |
| 529 | apr_signal(SIGTERM, signal_handler); |
| 530 | if (isterminated == 1) { |
| 531 | return OK; |
| 532 | } |
| 533 | apr_signal_init(srv->pool); |
| 534 | apr_signal(SIGTERM, signal_handler); |
| 535 | */ |
| 536 | if (srv->forkproc == NULL) { |
| 537 | srv->forkproc = apr_pcalloc(srv->pool, sizeof(apr_proc_t)); |
| 538 | apr_status_t res = apr_proc_fork(srv->forkproc, srv->pool); |
| 539 | switch (res) { |
| 540 | case APR_INCHILD: |
| 541 | /* set signal handler */ |
| 542 | apr_signal_init(srv->pool); |
| 543 | apr_signal(SIGTERM, signal_handler); |
| 544 | |
| 545 | /* log start of the separated NETCONF communication process */ |
| 546 | ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, "mod_netconf master process created (PID %d)", getpid()); |
| 547 | |
| 548 | /* start main loop providing NETCONF communication */ |
| 549 | forked_proc(srv->pool, s); |
| 550 | |
| 551 | /* I never should be here */ |
| 552 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "mod_netconf master process broke the main loop"); |
| 553 | exit(APR_EGENERAL); |
| 554 | break; |
| 555 | case APR_INPARENT: |
| 556 | /* register child to be killed (SIGTERM) when the module config's pool dies */ |
| 557 | apr_pool_note_subprocess(srv->pool, srv->forkproc, APR_KILL_AFTER_TIMEOUT); |
| 558 | break; |
| 559 | default: |
| 560 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "apr_proc_fork() failed"); |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | return OK; |
| 566 | } |
| 567 | |
| 568 | static int mod_netconf_fixups(request_rec *r) |
| 569 | { |
| 570 | apr_pool_t *temp_pool = NULL; |
| 571 | char* operation; |
| 572 | char *cred; |
| 573 | int len; |
| 574 | int sock = -1; |
| 575 | char buffer[BUFFER_SIZE]; |
| 576 | struct sockaddr_un addr; |
| 577 | |
| 578 | /* process only requests for the mod_netconf handler */ |
| 579 | if (!r->handler || (strcmp(r->handler, "netconf") != 0)) { |
| 580 | return DECLINED; |
| 581 | } |
| 582 | |
| 583 | /* allow running module only as subrequest */ |
| 584 | if (r->main == NULL) { |
| 585 | return DECLINED; |
| 586 | } |
| 587 | |
| 588 | /* create temporary pool */ |
| 589 | apr_pool_create(&temp_pool, NULL); |
| 590 | |
| 591 | operation = apr_pstrdup(temp_pool, apr_table_get(r->subprocess_env, "NETCONF_OP")); |
| 592 | if (operation == NULL) { |
| 593 | apr_pool_destroy(temp_pool); |
| 594 | return HTTP_INTERNAL_SERVER_ERROR; |
| 595 | } |
| 596 | |
| 597 | /* connect to the daemon */ |
| 598 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 599 | if (sock == -1) { |
| 600 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot create client's mod_netconf socket"); |
| 601 | apr_pool_destroy(temp_pool); |
| 602 | return HTTP_INTERNAL_SERVER_ERROR; |
| 603 | } |
| 604 | addr.sun_family = AF_UNIX; |
| 605 | strncpy(addr.sun_path, SOCKET_FILENAME, sizeof(addr.sun_path)); |
| 606 | len = strlen(addr.sun_path) + sizeof(addr.sun_family); |
| 607 | if (connect(sock, (struct sockaddr *) &addr, len) == -1) { |
| 608 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot connect to the mod_netconf daemon"); |
| 609 | apr_pool_destroy(temp_pool); |
| 610 | return HTTP_INTERNAL_SERVER_ERROR; |
| 611 | } |
| 612 | |
| 613 | /* process the request */ |
| 614 | if (strcmp(operation, "connect") == 0) { |
| 615 | char *host = apr_pstrdup(r->pool, apr_table_get(r->subprocess_env, "NETCONF_HOST")); |
| 616 | char *port = apr_pstrdup(r->pool, apr_table_get(r->subprocess_env, "NETCONF_PORT")); |
| 617 | char *user = apr_pstrdup(r->pool, apr_table_get(r->subprocess_env, "NETCONF_USER")); |
| 618 | char *pass = apr_pstrdup(r->pool, apr_table_get(r->subprocess_env, "NETCONF_PASS")); |
| 619 | |
| 620 | /* <MSG_TYPE><host>\0<port>\0<user>\0<pass>\0 */ |
| 621 | cred = apr_psprintf(temp_pool, " %s %s %s %s", host, port, user, pass); |
| 622 | cred[len = 0] = MSG_OPEN; |
| 623 | cred[len += strlen(host) + 1] = 0; /* <host>\0 */ |
| 624 | cred[len += strlen(port) + 1] = 0; /* <port>\0 */ |
| 625 | cred[len += strlen(user) + 1] = 0; /* <user>\0 */ |
| 626 | /* <pass>\0 is already done from printf, but we need to count index for send */ |
| 627 | cred[len += strlen(pass) + 1] = 0; |
| 628 | |
| 629 | send(sock, cred, len, 0); |
| 630 | len = recv(sock, buffer, BUFFER_SIZE, 0); |
| 631 | |
| 632 | if (len < sizeof(sck_message_t)) { |
| 633 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Invalid reply from mod_netconf daemon"); |
| 634 | apr_pool_destroy(temp_pool); |
| 635 | return HTTP_INTERNAL_SERVER_ERROR; |
| 636 | } else if (buffer[0] != MSG_OK) { |
| 637 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot connect with NETCONF server"); |
| 638 | apr_pool_destroy(temp_pool); |
| 639 | return HTTP_INTERNAL_SERVER_ERROR; |
| 640 | } else { |
| 641 | /* ok, received OK message */ |
| 642 | //apr_table_set(r->subprocess_env, "NETCONF_NSID", &(buffer[1])); |
| 643 | apr_table_set(r->main->subprocess_env, "NETCONF_NSID", "somestring"); |
| 644 | } |
| 645 | } else if (strcmp(operation, "disconnect") == 0) { |
| 646 | |
| 647 | } else if (strcmp(operation, "get-config") == 0) { |
| 648 | |
| 649 | } else { |
| 650 | return HTTP_NOT_IMPLEMENTED; |
| 651 | } |
| 652 | |
| 653 | if (sock != -1) { |
| 654 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "client closes socket"); |
| 655 | close (sock); |
| 656 | } |
| 657 | if (temp_pool != NULL) { |
| 658 | apr_pool_destroy(temp_pool); |
| 659 | } |
| 660 | |
| 661 | return OK; |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * Testing content handler |
| 666 | * This function should handle NETCONF and return UI |
| 667 | */ |
| 668 | static int mod_netconf_handler(request_rec * r) |
| 669 | { |
| 670 | char buffer[BUFFER_SIZE]; |
| 671 | struct sockaddr_un addr; |
| 672 | int sock = -1; |
| 673 | int len; |
| 674 | apr_pool_t *temp_pool = NULL; |
| 675 | char *cred, *data, *print_session_key; |
| 676 | sck_message_t msg; |
| 677 | request_rec *redirect; |
| 678 | |
| 679 | /* pseudo code of args to apr_proc_create() */ |
| 680 | if (!r->handler || (strcmp(r->handler, "netconf") != 0)) { |
| 681 | return DECLINED; |
| 682 | } |
| 683 | |
| 684 | return OK; |
| 685 | |
| 686 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, "NETCONF_NSID: %s", apr_table_get(r->subprocess_env, "NETCONF_NSID")); |
| 687 | //ap_internal_redirect(r->main->uri, r); |
| 688 | redirect = ap_sub_req_lookup_uri("/display.php", r, NULL); |
| 689 | ap_internal_fast_redirect(redirect, r); |
| 690 | return OK; |
| 691 | |
| 692 | r->content_type = "text/html;charset=UTF-8"; |
| 693 | |
| 694 | /* allow running module only as subrequest */ |
| 695 | if (r->main == NULL) { |
| 696 | return DECLINED; |
| 697 | } |
| 698 | |
| 699 | |
| 700 | /* create temporary pool */ |
| 701 | apr_pool_create(&temp_pool, NULL); |
| 702 | |
| 703 | ap_rputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \ |
| 704 | \"http://www.w3.org/TR/html4/loose.dtd\">", r); |
| 705 | ap_rputs("<html><head><title>mod_netconf testing</title></head><body>", r); |
| 706 | |
| 707 | ap_rputs("<h2>PHP call</h2>\n<ul>\n", r); |
| 708 | ap_rprintf(r, "<li>NETCONF_OP: %s</li></ul>", apr_table_get(r->subprocess_env, "NETCONF_OP")); |
| 709 | |
| 710 | ap_rputs("<h2>Operations list</h2>\n<ul>\n", r); |
| 711 | |
| 712 | ap_rputs("<li>Preparing socket... ", r); |
| 713 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 714 | if (sock == -1) { |
| 715 | ap_rputs("FAILED</li></ul>\n", r); |
| 716 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot create socket"); |
| 717 | goto close_page; |
| 718 | } else { |
| 719 | ap_rputs("OK</li>", r); |
| 720 | } |
| 721 | |
| 722 | ap_rputs("<li>Connecting to the mod_netconf daemon... ", r); |
| 723 | addr.sun_family = AF_UNIX; |
| 724 | strncpy(addr.sun_path, SOCKET_FILENAME, sizeof(addr.sun_path)); |
| 725 | len = strlen(addr.sun_path) + sizeof(addr.sun_family); |
| 726 | if (connect(sock, (struct sockaddr *) &addr, len) == -1) { |
| 727 | ap_rputs("FAILED </li></ul>\n", r); |
| 728 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot connect to the mod_daemon"); |
| 729 | goto close_page; |
| 730 | } else { |
| 731 | ap_rputs("OK</li>", r); |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * Open NETCONF session |
| 736 | */ |
| 737 | ap_rputs("<li>Openning NETCONF session... ", r); |
| 738 | |
| 739 | char *host = "localhost"; |
| 740 | char *port = "830"; |
| 741 | char *user = "krejci"; |
| 742 | char *pass = "FO8967kr"; |
| 743 | |
| 744 | /* <MSG_TYPE><host>\0<port>\0<user>\0<pass>\0 */ |
| 745 | cred = apr_psprintf(temp_pool, " %s %s %s %s", host, port, user, pass); |
| 746 | cred[len = 0] = MSG_OPEN; |
| 747 | cred[len += strlen(host) + 1] = 0; /* <host>\0 */ |
| 748 | cred[len += strlen(port) + 1] = 0; /* <port>\0 */ |
| 749 | cred[len += strlen(user) + 1] = 0; /* <user>\0 */ |
| 750 | /* <pass>\0 is already done from printf, but we need to count index for send */ |
| 751 | cred[len += strlen(pass) + 1] = 0; |
| 752 | |
| 753 | send(sock, cred, len, 0); |
| 754 | len = recv(sock, buffer, BUFFER_SIZE, 0); |
| 755 | |
| 756 | if (len < sizeof(sck_message_t)) { |
| 757 | ap_rputs("FAILED</li></ul>\n", r); |
| 758 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Invalid reply from mod_netconf daemon"); |
| 759 | apr_pool_destroy(temp_pool); |
| 760 | goto close_page; |
| 761 | } else if (buffer[0] != MSG_OK) { |
| 762 | ap_rputs("FAILED (unable to connect to NETCONF server)</li></ul>\n", r); |
| 763 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot connect with NETCONF server"); |
| 764 | apr_pool_destroy(temp_pool); |
| 765 | goto close_page; |
| 766 | } else { |
| 767 | /* ok, received OK message */ |
| 768 | apr_cpystrn(msg.session_key, &(buffer[1]), APR_SHA1_DIGESTSIZE + 1); |
| 769 | ap_rprintf(r, "OK (session key %s)</li>\n", print_session_key = ap_escape_html(temp_pool, msg.session_key)); |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * Get device configuration data |
| 774 | */ |
| 775 | |
| 776 | ap_rputs("<li>Getting device running data... ", r); |
| 777 | msg.type = MSG_DATA; |
| 778 | send(sock, (void*)(&msg), sizeof(msg), 0); |
| 779 | len = recv(sock, buffer, BUFFER_SIZE, 0); |
| 780 | |
| 781 | if (len < 1) { |
| 782 | ap_rputs("FAILED</li></ul>\n", r); |
| 783 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Invalid reply from mod_netconf daemon"); |
| 784 | apr_pool_destroy(temp_pool); |
| 785 | goto close_page; |
| 786 | } else if (buffer[0] != MSG_DATA) { |
| 787 | ap_rputs("FAILED (get-config failed)</li></ul>\n", r); |
| 788 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot perform get-config operation"); |
| 789 | apr_pool_destroy(temp_pool); |
| 790 | goto close_page; |
| 791 | } else { |
| 792 | /* ok, received OK message */ |
| 793 | ap_rputs("OK</li>\n", r); |
| 794 | data = ap_escape_html(temp_pool, &buffer[1]); |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | * Close the NETCONF session |
| 799 | */ |
| 800 | ap_rprintf(r, "<li>Closing NETCONF connection %s... ", print_session_key); |
| 801 | msg.type = MSG_CLOSE; |
| 802 | send(sock, (void*)(&msg), sizeof(msg), 0); |
| 803 | len = recv(sock, buffer, BUFFER_SIZE, 0); |
| 804 | |
| 805 | if (len < sizeof(sck_message_t)) { |
| 806 | ap_rputs("FAILED</li></ul>\n", r); |
| 807 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Invalid reply from mod_netconf daemon"); |
| 808 | apr_pool_destroy(temp_pool); |
| 809 | goto close_page; |
| 810 | } else if (buffer[0] != MSG_OK) { |
| 811 | ap_rputs("FAILED (unable to close NETCONF session)</li></ul>\n", r); |
| 812 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Cannot close the NETCONF session"); |
| 813 | apr_pool_destroy(temp_pool); |
| 814 | goto close_page; |
| 815 | } else { |
| 816 | /* ok, received OK message */ |
| 817 | ap_rputs("OK</li>\n", r); |
| 818 | } |
| 819 | ap_rputs("</ul>\n", r); |
| 820 | |
| 821 | ap_rputs("<h2>Data</h2>\n", r); |
| 822 | ap_rprintf(r, "%s", data); |
| 823 | |
| 824 | close_page: |
| 825 | ap_rputs("</body></html>", r); |
| 826 | |
| 827 | /* clean up */ |
| 828 | if (sock != -1) { |
| 829 | close (sock); |
| 830 | } |
| 831 | if (temp_pool != NULL) { |
| 832 | apr_pool_destroy(temp_pool); |
| 833 | } |
| 834 | |
| 835 | return OK; |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * Register module hooks |
| 840 | */ |
| 841 | static void mod_netconf_register_hooks(apr_pool_t * p) |
| 842 | { |
| 843 | ap_hook_fixups(mod_netconf_fixups, NULL, NULL, APR_HOOK_FIRST); |
| 844 | ap_hook_handler(mod_netconf_handler, NULL, NULL, APR_HOOK_MIDDLE); |
| 845 | ap_hook_post_config(mod_netconf_master_init, NULL, NULL, APR_HOOK_LAST); |
| 846 | } |
| 847 | |
| 848 | /* Dispatch list for API hooks */ |
| 849 | module AP_MODULE_DECLARE_DATA netconf_module = { |
| 850 | STANDARD20_MODULE_STUFF, |
| 851 | NULL, /* create per-dir config structures */ |
| 852 | NULL, /* merge per-dir config structures */ |
| 853 | mod_netconf_create_srv_conf, /* create per-server config structures */ |
| 854 | NULL, /* merge per-server config structures */ |
| 855 | NULL, /* table of config file commands */ |
| 856 | mod_netconf_register_hooks /* register hooks */ |
| 857 | }; |