|
Packit |
90a5c9 |
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
Packit |
90a5c9 |
* contributor license agreements. See the NOTICE file distributed with
|
|
Packit |
90a5c9 |
* this work for additional information regarding copyright ownership.
|
|
Packit |
90a5c9 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
Packit |
90a5c9 |
* (the "License"); you may not use this file except in compliance with
|
|
Packit |
90a5c9 |
* the License. You may obtain a copy of the License at
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
90a5c9 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
90a5c9 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
90a5c9 |
* See the License for the specific language governing permissions and
|
|
Packit |
90a5c9 |
* limitations under the License.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "mod_proxy.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include <sys/types.h>
|
|
Packit |
90a5c9 |
#include <sys/socket.h>
|
|
Packit |
90a5c9 |
#include <sys/un.h>
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#ifndef CMSG_DATA
|
|
Packit |
90a5c9 |
#error This module only works on unix platforms with the correct OS support
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "mod_proxy_fdpass.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
module AP_MODULE_DECLARE_DATA proxy_fdpass_module;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int proxy_fdpass_canon(request_rec *r, char *url)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
const char *path;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (strncasecmp(url, "fd://", 5) == 0) {
|
|
Packit |
90a5c9 |
url += 5;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
return DECLINED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
path = ap_server_root_relative(r->pool, url);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
r->filename = apr_pstrcat(r->pool, "proxy:fd://", path, NULL);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01151)
|
|
Packit |
90a5c9 |
"set r->filename to %s", r->filename);
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t get_socket_from_path(apr_pool_t *p,
|
|
Packit |
90a5c9 |
const char* path,
|
|
Packit |
90a5c9 |
apr_socket_t **out_sock)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_socket_t *s;
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
*out_sock = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = apr_socket_create(&s, AF_UNIX, SOCK_STREAM, 0, p);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = ap_proxy_connect_uds(s, path, p);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
*out_sock = s;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t send_socket(apr_pool_t *p,
|
|
Packit |
90a5c9 |
apr_socket_t *s,
|
|
Packit |
90a5c9 |
apr_socket_t *outbound)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
apr_os_sock_t rawsock;
|
|
Packit |
90a5c9 |
apr_os_sock_t srawsock;
|
|
Packit |
90a5c9 |
struct msghdr msg;
|
|
Packit |
90a5c9 |
struct cmsghdr *cmsg;
|
|
Packit |
90a5c9 |
struct iovec iov;
|
|
Packit |
90a5c9 |
char b = '\0';
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = apr_os_sock_get(&rawsock, outbound);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = apr_os_sock_get(&srawsock, s);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
memset(&msg, 0, sizeof(msg));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
msg.msg_iov = &iov;
|
|
Packit |
90a5c9 |
msg.msg_iovlen = 1;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
iov.iov_base = &b;
|
|
Packit |
90a5c9 |
iov.iov_len = 1;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
cmsg = apr_palloc(p, sizeof(*cmsg) + sizeof(rawsock));
|
|
Packit |
90a5c9 |
cmsg->cmsg_len = sizeof(*cmsg) + sizeof(rawsock);
|
|
Packit |
90a5c9 |
cmsg->cmsg_level = SOL_SOCKET;
|
|
Packit |
90a5c9 |
cmsg->cmsg_type = SCM_RIGHTS;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
memcpy(CMSG_DATA(cmsg), &rawsock, sizeof(rawsock));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
msg.msg_control = cmsg;
|
|
Packit |
90a5c9 |
msg.msg_controllen = cmsg->cmsg_len;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = sendmsg(srawsock, &msg, 0);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv == -1) {
|
|
Packit |
90a5c9 |
return errno;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int proxy_fdpass_handler(request_rec *r, proxy_worker *worker,
|
|
Packit |
90a5c9 |
proxy_server_conf *conf,
|
|
Packit |
90a5c9 |
char *url, const char *proxyname,
|
|
Packit |
90a5c9 |
apr_port_t proxyport)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
apr_socket_t *sock;
|
|
Packit |
90a5c9 |
apr_socket_t *clientsock;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (strncasecmp(url, "fd://", 5) == 0) {
|
|
Packit |
90a5c9 |
url += 5;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
return DECLINED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = get_socket_from_path(r->pool, url, &sock);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01152)
|
|
Packit |
90a5c9 |
"Failed to connect to '%s'", url);
|
|
Packit |
90a5c9 |
return HTTP_INTERNAL_SERVER_ERROR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
int status;
|
|
Packit |
90a5c9 |
const char *flush_method = *worker->s->flusher ? worker->s->flusher : "flush";
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
proxy_fdpass_flush *flush = ap_lookup_provider(PROXY_FDPASS_FLUSHER,
|
|
Packit |
90a5c9 |
flush_method, "0");
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!flush) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01153)
|
|
Packit |
90a5c9 |
"Unable to find configured flush provider '%s'",
|
|
Packit |
90a5c9 |
flush_method);
|
|
Packit |
90a5c9 |
return HTTP_INTERNAL_SERVER_ERROR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = flush->flusher(r);
|
|
Packit |
90a5c9 |
if (status) {
|
|
Packit |
90a5c9 |
return status;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
clientsock = ap_get_conn_socket(r->connection);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = send_socket(r->pool, sock, clientsock);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01154) "send_socket failed:");
|
|
Packit |
90a5c9 |
return HTTP_INTERNAL_SERVER_ERROR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_socket_t *dummy;
|
|
Packit |
90a5c9 |
/* Create a dummy unconnected socket, and set it as the one we were
|
|
Packit |
90a5c9 |
* connected to, so that when the core closes it, it doesn't close
|
|
Packit |
90a5c9 |
* the tcp connection to the client.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
rv = apr_socket_create(&dummy, APR_INET, SOCK_STREAM, APR_PROTO_TCP,
|
|
Packit |
90a5c9 |
r->connection->pool);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01155)
|
|
Packit |
90a5c9 |
"failed to create dummy socket");
|
|
Packit |
90a5c9 |
return HTTP_INTERNAL_SERVER_ERROR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ap_set_core_module_config(r->connection->conn_config, dummy);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int standard_flush(request_rec *r)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
int status;
|
|
Packit |
90a5c9 |
apr_bucket_brigade *bb;
|
|
Packit |
90a5c9 |
apr_bucket *e;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
r->connection->keepalive = AP_CONN_CLOSE;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
|
|
Packit |
90a5c9 |
e = apr_bucket_flush_create(r->connection->bucket_alloc);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
APR_BRIGADE_INSERT_TAIL(bb, e);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = ap_pass_brigade(r->output_filters, bb);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (status != OK) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01156)
|
|
Packit |
90a5c9 |
"ap_pass_brigade failed:");
|
|
Packit |
90a5c9 |
return status;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const proxy_fdpass_flush builtin_flush =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
"flush",
|
|
Packit |
90a5c9 |
&standard_flush,
|
|
Packit |
90a5c9 |
NULL
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void register_hooks(apr_pool_t *p)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_register_provider(p, PROXY_FDPASS_FLUSHER, "flush", "0", &builtin_flush);
|
|
Packit |
90a5c9 |
proxy_hook_scheme_handler(proxy_fdpass_handler, NULL, NULL, APR_HOOK_FIRST);
|
|
Packit |
90a5c9 |
proxy_hook_canon_handler(proxy_fdpass_canon, NULL, NULL, APR_HOOK_FIRST);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
AP_DECLARE_MODULE(proxy_fdpass) = {
|
|
Packit |
90a5c9 |
STANDARD20_MODULE_STUFF,
|
|
Packit |
90a5c9 |
NULL, /* create per-directory config structure */
|
|
Packit |
90a5c9 |
NULL, /* merge per-directory config structures */
|
|
Packit |
90a5c9 |
NULL, /* create per-server config structure */
|
|
Packit |
90a5c9 |
NULL, /* merge per-server config structures */
|
|
Packit |
90a5c9 |
NULL, /* command apr_table_t */
|
|
Packit |
90a5c9 |
register_hooks /* register hooks */
|
|
Packit |
90a5c9 |
};
|