X-Git-Url: https://mop.ddnsfree.com/gitweb/?a=blobdiff_plain;f=mod_proxy_fdpass.c;h=ff3e684b213004fff888ec6bb6c3d1ae5a03e7a7;hb=34a20c86b2a1e8b901cdf1e8583e261dea69b80b;hp=e2eaccebfe7933fbe8b224d29f33caed4fec5f2e;hpb=9e509bb2915d79990170c692988a5658b0828d21;p=mod-proxy-fdpass.git diff --git a/mod_proxy_fdpass.c b/mod_proxy_fdpass.c index e2eacce..ff3e684 100644 --- a/mod_proxy_fdpass.c +++ b/mod_proxy_fdpass.c @@ -34,15 +34,17 @@ module AP_MODULE_DECLARE_DATA proxy_fdpass_module; +#define MOP_DEBUG 0 + static int proxy_fdpass_canon(request_rec *r, char *url) { const char *path; - { + if (MOP_DEBUG == 1) { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT); + mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); sprintf(mop_bf, "proxy_http_canon: start\n"); write(mop_fd, mop_bf, strlen(mop_bf)); close(mop_fd); @@ -60,8 +62,8 @@ static int proxy_fdpass_canon(request_rec *r, char *url) r->filename = apr_pstrcat(r->pool, "proxy:fd://", path, NULL); - ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, - "proxy: FD: set r->filename to %s", r->filename); + /* ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, + "proxy: FD: set r->filename to %s", r->filename); */ return OK; } @@ -88,12 +90,12 @@ static apr_status_t socket_connect_un(request_rec *r, apr_socket_t *sock, } do { - ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, - "proxy: FD: pre_connect"); + /* ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, + "proxy: FD: pre_connect"); */ rv = connect(rawsock, (struct sockaddr*)sa, sizeof(*sa) /* + strlen(sa->sun_path)*/ ); - ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, - "proxy: FD: post_connect %d", rv); + /* ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, + "proxy: FD: post_connect %d", rv); */ } while (rv == -1 && errno == EINTR); if ((rv == -1) && (errno == EINPROGRESS || errno == EALREADY) @@ -133,9 +135,9 @@ static apr_status_t get_socket_from_path(request_rec *r, apr_pool_t *p, ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "proxy: FD: Failed to connect to '%s' %d xxx", url, rv); - */ ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "proxy: FD: get_socket_from_path::START"); + */ rv = apr_socket_create(&s, AF_UNIX, SOCK_STREAM, 0, p); @@ -160,28 +162,35 @@ static apr_status_t get_socket_from_path(request_rec *r, apr_pool_t *p, return APR_SUCCESS; } +#define ANCIL_FD_BUFFER(n) \ + struct { \ + struct cmsghdr h; \ + int fd[n]; \ + } static apr_status_t send_socket(apr_pool_t *p, apr_socket_t *s, - apr_socket_t *outbound) + apr_socket_t *outbound, + apr_socket_t *ctrlsock) { apr_status_t rv; apr_os_sock_t rawsock; apr_os_sock_t srawsock; + apr_os_sock_t sctrlsock; struct msghdr msg; struct cmsghdr *cmsg; struct iovec iov; char b = '\0', *buf; + ANCIL_FD_BUFFER(2) ancil_buf; - { + if (MOP_DEBUG == 1) { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); + mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); sprintf(mop_bf, "send_socket: start\n"); write(mop_fd, mop_bf, strlen(mop_bf)); close(mop_fd); - } rv = apr_os_sock_get(&rawsock, outbound); @@ -193,17 +202,21 @@ static apr_status_t send_socket(apr_pool_t *p, if (rv != APR_SUCCESS) { return rv; } + + rv = apr_os_sock_get(&sctrlsock, ctrlsock); + if (rv != APR_SUCCESS) { + return rv; + } - { + if (MOP_DEBUG == 1) { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); + mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); write(mop_fd, "XX", 2); write(mop_fd, &srawsock, sizeof(apr_os_sock_t)); write(mop_fd, "XX", 2); close(mop_fd); - } memset(&msg, 0, sizeof(msg)); @@ -214,27 +227,28 @@ static apr_status_t send_socket(apr_pool_t *p, iov.iov_base = &b; iov.iov_len = 1; - cmsg = apr_palloc(p, sizeof(*cmsg) + sizeof(rawsock)); - cmsg->cmsg_len = sizeof(*cmsg) + sizeof(rawsock); + msg.msg_control = &ancil_buf; + msg.msg_controllen = sizeof(struct cmsghdr) + sizeof(rawsock) * 2; + + // cmsg = apr_palloc(p, sizeof(*cmsg) + sizeof(rawsock)); + cmsg = CMSG_FIRSTHDR(&msg); + cmsg->cmsg_len = sizeof(*cmsg) + sizeof(rawsock) * 2; cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; - memcpy(CMSG_DATA(cmsg), &rawsock, sizeof(rawsock)); - - msg.msg_control = cmsg; - msg.msg_controllen = cmsg->cmsg_len; + ((int *)CMSG_DATA(cmsg))[0] = rawsock; + ((int *)CMSG_DATA(cmsg))[1] = sctrlsock; rv = sendmsg(srawsock, &msg, 0); - { + if (MOP_DEBUG == 1) { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); + mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); sprintf(mop_bf, "SENT BYTES: %d\n", rv); write(mop_fd, mop_bf, strlen(mop_bf)); close(mop_fd); - } if (rv == -1) { @@ -255,6 +269,89 @@ static int headers_builder(void *rec, const char *key, const char *value) sprintf(s, "%s%s:%s\n", s, key, value); } +#define CTRL_BUFF_MAX_SZ (8*1024) + +#define DEFAULT_ENCTYPE "application/x-www-form-urlencoded" + +int util_read(request_rec *r, const char **rbuf) +{ + int rc; + + if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK) { + return rc; + } + + if (ap_should_client_block(r)) { + char argsbuffer[HUGE_STRING_LEN]; + int rsize, len_read, rpos=0; + long length = r->remaining; + *rbuf = (char *)apr_pcalloc(r->pool, length +1); + if ((len_read = ap_get_client_block(r, argsbuffer, + sizeof(argsbuffer))) > 0) { + if ((rpos + len_read) > length) { + rsize = length - rpos; + } else { + rsize = len_read; + } + + memcpy((char *)*rbuf + rpos, argsbuffer, rsize); + rpos += rsize; + } + + } + + return rc; +} + +int read_post(request_rec *r, const char **data) +{ + const char *type; + char *p, s_type[256]; + int rc = OK; + + s_type[255] = '\0'; + if (MOP_DEBUG == 1) { + int mop_fd; + char mop_bf[512]; + + mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); + sprintf(mop_bf, "read_post: start: numb: %d %d head_in: [%s]\n", r->method_number, M_POST, apr_table_get(r->headers_in, "Content-Type")); + write(mop_fd, mop_bf, strlen(mop_bf)); + close(mop_fd); + } + + + if (r->method_number != M_POST) { + return rc; + } + + type = apr_table_get(r->headers_in, "Content-Type"); + strncpy(s_type, type, 255); + if (p = strchr(s_type, ';')) { + *p = '\0'; + } + if (strcasecmp(s_type, DEFAULT_ENCTYPE) != 0) { + return DECLINED; + } + + if ((rc = util_read(r, data)) != OK) { + return rc; + } + + if (MOP_DEBUG == 1) { + int mop_fd; + char mop_bf[512]; + + mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); + sprintf(mop_bf, "read_post: finish\n"); + write(mop_fd, mop_bf, strlen(mop_bf)); + close(mop_fd); + } + + return OK; +} + + // TODO: sanitize calloc static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker, proxy_server_conf *conf, @@ -266,40 +363,83 @@ static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker, apr_socket_t *clientsock; char *buf; char *headers_out = NULL; + int ctrlrawsock[2]; + apr_socket_t *ctrlsock = NULL, *clientctrlsock = NULL; + apr_size_t wrlen; + const char *post_data = NULL; - if ((headers_out = calloc(8*1024, 1)) != NULL) { - apr_table_do(headers_builder, headers_out, r->headers_in, NULL); + ap_filter_t *f; + ap_filter_rec_t *fg; + + if (strncasecmp(url, "fd://", 5) == 0) { + url += 5; + } + else { + return DECLINED; } + rv = get_socket_from_path(r, r->pool, url, &sock); - { + if (rv != APR_SUCCESS) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, + "proxy: FD: Failed to connect to '%s' %d xxx", + url, rv); + return HTTP_INTERNAL_SERVER_ERROR; + } + + fg = ap_get_output_filter_handle("HTTP_HEADER"); + + /* + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, + "proxy: FD: filter fg: %lx func %lx", fg, ap_http_header_filter); + */ + + for (f = r->output_filters ; f != NULL ; f = f->next) { + /* ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, + "proxy: FD: filter loop: %lx", f->frec); + */ + if (f->frec == fg) { + /* + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, + "proxy: FD: filter found, remove it"); + */ + ap_remove_output_filter(f); + break; + } + } + + if ((headers_out = calloc(CTRL_BUFF_MAX_SZ, 1)) != NULL) { + sprintf(headers_out, "The-Request:%s\n", r->the_request); + apr_table_do(headers_builder, headers_out, r->headers_in, NULL); + } + read_post(r, &post_data); + + if (MOP_DEBUG == 1) { int mop_fd; char mop_bf[512]; - mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); + mop_fd = open("/tmp/apache_mop.log", O_WRONLY | O_APPEND | O_CREAT, 0644); sprintf(mop_bf, "proxy_fdpass_handler: start\n"); write(mop_fd, mop_bf, strlen(mop_bf)); write(mop_fd, headers_out, strlen(headers_out)); close(mop_fd); - } - /* - if (headers_out) - free(headers_out); - */ - if (strncasecmp(url, "fd://", 5) == 0) { - url += 5; + /* create a couple of sockets and pass one to the client for headers and so on */ + if (socketpair(AF_UNIX, SOCK_STREAM, 0, ctrlrawsock)) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, + "proxy: FD: Failed create socketpair"); + return HTTP_INTERNAL_SERVER_ERROR; } - else { - return DECLINED; + rv = apr_os_sock_put(&ctrlsock, &(ctrlrawsock[0]), r->connection->pool); + if (rv != APR_SUCCESS) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, + "proxy: FD: apr_os_sock_put failed"); + return HTTP_INTERNAL_SERVER_ERROR; } - - rv = get_socket_from_path(r, r->pool, url, &sock); - + rv = apr_os_sock_put(&clientctrlsock, &(ctrlrawsock[1]), r->connection->pool); if (rv != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, - "proxy: FD: Failed to connect to '%s' %d xxx", - url, rv); + "proxy: FD: apr_os_sock_put failed"); return HTTP_INTERNAL_SERVER_ERROR; } @@ -324,21 +464,43 @@ static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker, } } + /* if ((buf = apr_table_get(r->headers_in, "Host"))) { ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "proxy: FD: Host is: [%s]", buf); } + */ /* XXXXX: THIS IS AN EVIL HACK */ /* There should really be a (documented) public API for this ! */ clientsock = ap_get_module_config(r->connection->conn_config, &core_module); - rv = send_socket(r->pool, sock, clientsock); + rv = send_socket(r->pool, sock, clientsock, clientctrlsock); if (rv != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "proxy: FD: send_socket failed:"); return HTTP_INTERNAL_SERVER_ERROR; } + strcat(headers_out, "\n"); + wrlen = strlen(headers_out); + rv = apr_socket_send(ctrlsock, headers_out, &wrlen); + if (rv != APR_SUCCESS) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, + "proxy: FD: send headers failed"); + return HTTP_INTERNAL_SERVER_ERROR; + } + if (post_data) { + wrlen = strlen(post_data); + rv = apr_socket_send(ctrlsock, post_data, &wrlen); + if (rv != APR_SUCCESS) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, + "proxy: FD: send post failed"); + return HTTP_INTERNAL_SERVER_ERROR; + } + } + apr_socket_shutdown(ctrlsock, APR_SHUTDOWN_READWRITE); + if (headers_out) + free(headers_out); { apr_socket_t *dummy; @@ -369,8 +531,7 @@ static int standard_flush(request_rec *r) r->connection->keepalive = AP_CONN_CLOSE; /* MOP NOTE: set here the content type */ - // ap_set_content_type(r, apr_pstrdup(p, "text/plain")); - + // ap_set_content_type(r, apr_pstrdup(p, NO_CONTENT_TYPE)); bb = apr_brigade_create(r->pool, r->connection->bucket_alloc); e = apr_bucket_flush_create(r->connection->bucket_alloc);