mod_proxy_fdpass.c revision df87aca8c6fb25d30168f36a347ff171aaa46bda
842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
2d2eda71267231c2526be701fe655db125852c1ffielding *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * http://www.apache.org/licenses/LICENSE-2.0
2d2eda71267231c2526be701fe655db125852c1ffielding *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * See the License for the specific language governing permissions and
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * limitations under the License.
2d2eda71267231c2526be701fe655db125852c1ffielding */
2d2eda71267231c2526be701fe655db125852c1ffielding
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#include "mod_proxy.h"
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#ifndef CMSG_DATA
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#error This module only works on unix platforms with the correct OS support
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#endif
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#include <sys/types.h>
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#include <sys/socket.h>
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh#include <sys/un.h>
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding/* for apr_wait_for_io_or_timeout */
2d2eda71267231c2526be701fe655db125852c1ffielding#include "apr_support.h"
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding#include "mod_proxy_fdpass.h"
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffieldingmodule AP_MODULE_DECLARE_DATA proxy_fdpass_module;
d2227b43aaee9a78350dc107bdc404859ad1f938rbb
d2227b43aaee9a78350dc107bdc404859ad1f938rbbstatic int proxy_fdpass_canon(request_rec *r, char *url)
d2227b43aaee9a78350dc107bdc404859ad1f938rbb{
d2227b43aaee9a78350dc107bdc404859ad1f938rbb const char *path;
45acd673a68181802b112e97e84fa3813ddd3ec1stoddard
2d2eda71267231c2526be701fe655db125852c1ffielding if (strncasecmp(url, "fd://", 5) == 0) {
d2227b43aaee9a78350dc107bdc404859ad1f938rbb url += 5;
742318b93e89c311f66b55f426c4d9cf2c14628bjim }
d2227b43aaee9a78350dc107bdc404859ad1f938rbb else {
d2227b43aaee9a78350dc107bdc404859ad1f938rbb return DECLINED;
d2227b43aaee9a78350dc107bdc404859ad1f938rbb }
d2227b43aaee9a78350dc107bdc404859ad1f938rbb
ebc18d48bea83ee5ed7a1b4e30007e5192539829wrowe path = ap_server_root_relative(r->pool, url);
2d2eda71267231c2526be701fe655db125852c1ffielding
d2227b43aaee9a78350dc107bdc404859ad1f938rbb r->filename = apr_pstrcat(r->pool, "proxy:fd://", path, NULL);
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
d2227b43aaee9a78350dc107bdc404859ad1f938rbb ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
d2227b43aaee9a78350dc107bdc404859ad1f938rbb "proxy: FD: set r->filename to %s", r->filename);
d2227b43aaee9a78350dc107bdc404859ad1f938rbb return OK;
d2227b43aaee9a78350dc107bdc404859ad1f938rbb}
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
2d2eda71267231c2526be701fe655db125852c1ffielding/* TODO: In APR 2.x: Extend apr_sockaddr_t to possibly be a path !!! */
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianhstatic apr_status_t socket_connect_un(apr_socket_t *sock,
742318b93e89c311f66b55f426c4d9cf2c14628bjim struct sockaddr_un *sa)
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh{
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh apr_status_t rv;
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh apr_os_sock_t rawsock;
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh apr_interval_time_t t;
c5d006b2861d49c61bcf79316163e30611c6fd08trawick
c5d006b2861d49c61bcf79316163e30611c6fd08trawick rv = apr_os_sock_get(&rawsock, sock);
c5d006b2861d49c61bcf79316163e30611c6fd08trawick if (rv) {
2d2eda71267231c2526be701fe655db125852c1ffielding return rv;
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna }
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna rv = apr_socket_timeout_get(sock, &t);
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna if (rv) {
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna return rv;
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna }
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna do {
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna rv = connect(rawsock, (struct sockaddr*)sa, sizeof(*sa) + strlen(sa->sun_path));
742318b93e89c311f66b55f426c4d9cf2c14628bjim } while (rv == -1 && errno == EINTR);
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna if ((rv == -1) && (errno == EINPROGRESS || errno == EALREADY)
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna && (t > 0)) {
742318b93e89c311f66b55f426c4d9cf2c14628bjim rv = apr_wait_for_io_or_timeout(NULL, sock, 0);
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna if (rv != APR_SUCCESS) {
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna return rv;
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna }
742318b93e89c311f66b55f426c4d9cf2c14628bjim }
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna if (rv == -1 && errno != EISCONN) {
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna return errno;
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna }
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna
20f1b1a67eef5ab0f3295608c89964a7dca4fdd1pquerna return APR_SUCCESS;
d2227b43aaee9a78350dc107bdc404859ad1f938rbb}
742318b93e89c311f66b55f426c4d9cf2c14628bjim
d2227b43aaee9a78350dc107bdc404859ad1f938rbbstatic apr_status_t get_socket_from_path(apr_pool_t *p,
d2227b43aaee9a78350dc107bdc404859ad1f938rbb const char* path,
45acd673a68181802b112e97e84fa3813ddd3ec1stoddard apr_socket_t **out_sock)
2d2eda71267231c2526be701fe655db125852c1ffielding{
d2227b43aaee9a78350dc107bdc404859ad1f938rbb struct sockaddr_un sa;
742318b93e89c311f66b55f426c4d9cf2c14628bjim apr_socket_t *s;
d2227b43aaee9a78350dc107bdc404859ad1f938rbb apr_status_t rv;
d2227b43aaee9a78350dc107bdc404859ad1f938rbb *out_sock = NULL;
2d2eda71267231c2526be701fe655db125852c1ffielding
45acd673a68181802b112e97e84fa3813ddd3ec1stoddard rv = apr_socket_create(&s, AF_UNIX, SOCK_STREAM, 0, p);
2d2eda71267231c2526be701fe655db125852c1ffielding
d2227b43aaee9a78350dc107bdc404859ad1f938rbb if (rv) {
d2227b43aaee9a78350dc107bdc404859ad1f938rbb return rv;
d2227b43aaee9a78350dc107bdc404859ad1f938rbb }
d2227b43aaee9a78350dc107bdc404859ad1f938rbb
d2227b43aaee9a78350dc107bdc404859ad1f938rbb sa.sun_family = AF_UNIX;
d2227b43aaee9a78350dc107bdc404859ad1f938rbb apr_cpystrn(sa.sun_path, path, sizeof(sa.sun_path));
d2227b43aaee9a78350dc107bdc404859ad1f938rbb
2d2eda71267231c2526be701fe655db125852c1ffielding rv = socket_connect_un(s, &sa);
2d2eda71267231c2526be701fe655db125852c1ffielding if (rv) {
3d96ee83babeec32482c9082c9426340cee8c44dwrowe return rv;
c21ddeacdc8bb17fe61b9e2d7d1fa6f2f7e40d84dreid }
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding *out_sock = s;
2d2eda71267231c2526be701fe655db125852c1ffielding
2d2eda71267231c2526be701fe655db125852c1ffielding return APR_SUCCESS;
2d2eda71267231c2526be701fe655db125852c1ffielding}
11e076839c8d5a82d55e710194d0daac51390dbdsf
9d129b55f5a43abf43865c6b0eb6dd19bc22aba8ianh
static apr_status_t send_socket(apr_pool_t *p,
apr_socket_t *s,
apr_socket_t *outbound)
{
apr_status_t rv;
apr_os_sock_t rawsock;
apr_os_sock_t srawsock;
struct msghdr msg;
struct cmsghdr *cmsg;
struct iovec iov;
char b = '\0';
rv = apr_os_sock_get(&rawsock, outbound);
if (rv) {
return rv;
}
rv = apr_os_sock_get(&srawsock, s);
if (rv) {
return rv;
}
memset(&msg, 0, sizeof(msg));
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
iov.iov_base = &b;
iov.iov_len = 1;
cmsg = apr_palloc(p, sizeof(*cmsg) + sizeof(rawsock));
cmsg->cmsg_len = sizeof(*cmsg) + sizeof(rawsock);
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;
rv = sendmsg(srawsock, &msg, 0);
if (rv == -1) {
return errno;
}
return APR_SUCCESS;
}
static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker,
proxy_server_conf *conf,
char *url, const char *proxyname,
apr_port_t proxyport)
{
apr_status_t rv;
apr_socket_t *sock;
apr_socket_t *clientsock;
if (strncasecmp(url, "fd://", 5) == 0) {
url += 5;
}
else {
return DECLINED;
}
rv = get_socket_from_path(r->pool, url, &sock);
if (rv) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"proxy: FD: Failed to connect to '%s'",
url);
return HTTP_INTERNAL_SERVER_ERROR;
}
{
int status;
const char *flush_method = worker->flusher ? worker->flusher : "flush";
proxy_fdpass_flush *flush = ap_lookup_provider(PROXY_FDPASS_FLUSHER, flush_method, "0");
if (!flush) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"proxy: FD: Unable to find configured flush "
"provider '%s'", flush_method);
return HTTP_INTERNAL_SERVER_ERROR;
}
status = flush->flusher(r);
if (status) {
return status;
}
}
/* 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);
if (rv) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"proxy: FD: send_socket failed:");
return HTTP_INTERNAL_SERVER_ERROR;
}
{
apr_socket_t *dummy;
/* Create a dummy unconnected socket, and set it as the one we were
* connected to, so that when the core closes it, it doesn't close
* the tcp connection to the client.
*/
rv = apr_socket_create(&dummy, APR_INET, SOCK_STREAM, APR_PROTO_TCP, r->connection->pool);
if (rv) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
"proxy: FD: failed to create dummy socket");
return HTTP_INTERNAL_SERVER_ERROR;
}
ap_set_module_config(r->connection->conn_config, &core_module, dummy);
}
return OK;
}
static int standard_flush(request_rec *r)
{
int status;
apr_bucket_brigade *bb;
apr_bucket *e;
r->connection->keepalive = AP_CONN_CLOSE;
bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
e = apr_bucket_flush_create(r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, e);
status = ap_pass_brigade(r->output_filters, bb);
if (status != OK) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
"proxy: FD: ap_pass_brigade failed:");
return status;
}
return OK;
}
static const proxy_fdpass_flush builtin_flush =
{
"flush",
&standard_flush,
NULL
};
static void register_hooks(apr_pool_t *p)
{
ap_register_provider(p, PROXY_FDPASS_FLUSHER, "flush", "0", &builtin_flush);
proxy_hook_scheme_handler(proxy_fdpass_handler, NULL, NULL, APR_HOOK_FIRST);
proxy_hook_canon_handler(proxy_fdpass_canon, NULL, NULL, APR_HOOK_FIRST);
}
module AP_MODULE_DECLARE_DATA proxy_fdpass_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
NULL, /* command apr_table_t */
register_hooks /* register hooks */
};