50cfe8bbbaf4279375802531268e2bf0155215fetrawick/* Licensed to the Apache Software Foundation (ASF) under one or more
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * contributor license agreements. See the NOTICE file distributed with
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * this work for additional information regarding copyright ownership.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * The ASF licenses this file to You under the Apache License, Version 2.0
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * (the "License"); you may not use this file except in compliance with
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * the License. You may obtain a copy of the License at
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * http://www.apache.org/licenses/LICENSE-2.0
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * Unless required by applicable law or agreed to in writing, software
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * distributed under the License is distributed on an "AS IS" BASIS,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * See the License for the specific language governing permissions and
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * limitations under the License.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "apr_hash.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "apr_lib.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "apr_strings.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "ap_provider.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "httpd.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "http_config.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "http_core.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "http_protocol.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "http_request.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "http_log.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "util_script.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "ap_provider.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "mod_auth.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#include "util_fcgi.h"
2e3867b767f5f34e6ab6414670f3306806913810trawick#include "ap_mmn.h"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickmodule AP_MODULE_DECLARE_DATA authnz_fcgi_module;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawicktypedef struct {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *name; /* provider name */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *backend; /* backend address, as configured */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *host;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_port_t port;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_sockaddr_t *backend_addrs;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int is_authn;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int is_authz;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick} fcgi_provider_conf;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawicktypedef struct {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *name; /* provider name */
bbb64779da3514ea1fee8b806f5ad30d29ea45abtrawick const char *default_user; /* this is user if authorizer returns
bbb64779da3514ea1fee8b806f5ad30d29ea45abtrawick * success and a user expression yields
bbb64779da3514ea1fee8b806f5ad30d29ea45abtrawick * empty string
bbb64779da3514ea1fee8b806f5ad30d29ea45abtrawick */
bbb64779da3514ea1fee8b806f5ad30d29ea45abtrawick ap_expr_info_t *user_expr; /* expr to evaluate to set r->user */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick char authoritative; /* fail request if user is rejected? */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick char require_basic_auth; /* fail if client didn't send credentials? */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick} fcgi_dir_conf;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawicktypedef struct {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* If an "authnz" provider successfully authenticates, record
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * the provider name here for checking during authz.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *successful_authnz_provider;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick} fcgi_request_notes;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic apr_hash_t *fcgi_authn_providers, *fcgi_authz_providers;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#define FCGI_IO_TIMEOUT apr_time_from_sec(30)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#ifndef NON200_RESPONSE_BUF_LEN
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#define NON200_RESPONSE_BUF_LEN 8192
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#endif
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick/* fcgi://{hostname|IPv4|IPv6}:port[/] */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#define FCGI_BACKEND_REGEX_STR "m%^fcgi://(.*):(\\d{1,5})/?$%"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick/*
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * utility function to connect to a peer; generally useful, but
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * wait for AF_UNIX support in this mod before thinking about how
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * to make it available to other modules
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic apr_status_t connect_to_peer(apr_socket_t **newsock,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick request_rec *r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_sockaddr_t *backend_addrs,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *backend_name,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_interval_time_t timeout)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_status_t rv = APR_EINVAL; /* returned if no backend addr was provided
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int connected = 0;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_sockaddr_t *addr = backend_addrs;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick while (addr && !connected) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int loglevel = addr->next ? APLOG_DEBUG : APLOG_ERR;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = apr_socket_create(newsock, addr->family,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick SOCK_STREAM, 0, r->pool);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv != APR_SUCCESS) {
7a42584e6f09102c6776454f090b98a58fd590ebtrawick ap_log_rerror(APLOG_MARK, loglevel, rv, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02494) "error creating family %d socket "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "for target %s",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick addr->family, backend_name);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick addr = addr->next;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick continue;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_socket_opt_set(*newsock, APR_TCP_NODELAY, 1);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_socket_timeout_set(*newsock,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick timeout ? timeout : r->server->timeout);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = apr_socket_connect(*newsock, addr);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv != APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_socket_close(*newsock);
7a42584e6f09102c6776454f090b98a58fd590ebtrawick ap_log_rerror(APLOG_MARK, loglevel, rv, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02495) "attempt to connect to %pI (%s) "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "failed", addr, backend_name);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick addr = addr->next;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick continue;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick connected = 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return rv;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick#undef FN_LOG_MARK
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic void log_provider_info(const fcgi_provider_conf *conf, request_rec *r)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02496) "name %s, backend %s, host %s, port %d, "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "first address %pI, %c%c",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->name,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->backend,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->host,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick (int)conf->port,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->backend_addrs,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->is_authn ? 'N' : '_',
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->is_authz ? 'Z' : '_');
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic void setupenv(request_rec *r, const char *password, const char *apache_role)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_add_common_vars(r);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_add_cgi_vars(r);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_setn(r->subprocess_env, "FCGI_ROLE", AP_FCGI_AUTHORIZER_STR);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (apache_role) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_setn(r->subprocess_env, "FCGI_APACHE_ROLE", apache_role);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (password) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_setn(r->subprocess_env, "REMOTE_PASSWD", password);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* Drop the variables CONTENT_LENGTH, PATH_INFO, PATH_TRANSLATED,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * SCRIPT_NAME and most Hop-By-Hop headers - EXCEPT we will pass
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * PROXY_AUTH to allow CGI to perform proxy auth for httpd
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_unset(r->subprocess_env, "CONTENT_LENGTH");
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_unset(r->subprocess_env, "PATH_INFO");
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_unset(r->subprocess_env, "PATH_TRANSLATED");
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_unset(r->subprocess_env, "SCRIPT_NAME");
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_unset(r->subprocess_env, "HTTP_KEEP_ALIVE");
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_unset(r->subprocess_env, "HTTP_TE");
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_unset(r->subprocess_env, "HTTP_TRAILER");
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_unset(r->subprocess_env, "HTTP_TRANSFER_ENCODING");
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_unset(r->subprocess_env, "HTTP_UPGRADE");
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* Connection hop-by-hop header to prevent the CGI from hanging */
210116e11414ce7a3789652725be298b5f073056jailletc apr_table_setn(r->subprocess_env, "HTTP_CONNECTION", "close");
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic apr_status_t recv_data(const fcgi_provider_conf *conf,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick request_rec *r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_socket_t *s,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick char *buf,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_size_t *buflen)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_status_t rv;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = apr_socket_recv(s, buf, buflen);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv != APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02497) "Couldn't read from backend %s",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->backend);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return rv;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
2e3867b767f5f34e6ab6414670f3306806913810trawick
2e3867b767f5f34e6ab6414670f3306806913810trawick#if AP_MODULE_MAGIC_AT_LEAST(20130702,2)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rdata(APLOG_MARK, APLOG_TRACE5, r, "FastCGI data received",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick buf, *buflen, AP_LOG_DATA_SHOW_OFFSET);
2e3867b767f5f34e6ab6414670f3306806913810trawick#endif
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return APR_SUCCESS;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic apr_status_t recv_data_full(const fcgi_provider_conf *conf,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick request_rec *r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_socket_t *s,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick char *buf,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_size_t buflen)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_size_t readlen;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_size_t cumulative_len = 0;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_status_t rv;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick do {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick readlen = buflen - cumulative_len;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = recv_data(conf, r, s, buf + cumulative_len, &readlen);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv != APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return rv;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick cumulative_len += readlen;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick } while (cumulative_len < buflen);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return APR_SUCCESS;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic apr_status_t sendv_data(const fcgi_provider_conf *conf,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick request_rec *r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_socket_t *s,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick struct iovec *vec,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int nvec,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_size_t *len)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_size_t to_write = 0, written = 0;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_status_t rv = APR_SUCCESS;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int i, offset;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick for (i = 0; i < nvec; i++) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick to_write += vec[i].iov_len;
2e3867b767f5f34e6ab6414670f3306806913810trawick#if AP_MODULE_MAGIC_AT_LEAST(20130702,2)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rdata(APLOG_MARK, APLOG_TRACE5, r, "FastCGI data sent",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick vec[i].iov_base, vec[i].iov_len, AP_LOG_DATA_SHOW_OFFSET);
2e3867b767f5f34e6ab6414670f3306806913810trawick#endif
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick offset = 0;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick while (to_write) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_size_t n = 0;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = apr_socket_sendv(s, vec + offset, nvec - offset, &n);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv != APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02498) "Sending data to %s failed",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->backend);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (n > 0) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick written += n;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (written >= to_write)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break; /* short circuit out */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick for (i = offset; i < nvec; ) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (n >= vec[i].iov_len) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick offset++;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick n -= vec[i++].iov_len;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick } else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick vec[i].iov_len -= n;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick vec[i].iov_base = (char *) vec[i].iov_base + n;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *len = written;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return rv;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic apr_status_t send_begin_request(request_rec *r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const fcgi_provider_conf *conf,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_socket_t *s, int role,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_uint16_t request_id)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick struct iovec vec[2];
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_fcgi_header header;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick unsigned char farray[AP_FCGI_HEADER_LEN];
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_fcgi_begin_request_body brb;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick unsigned char abrb[AP_FCGI_HEADER_LEN];
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_size_t len;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_fcgi_fill_in_header(&header, AP_FCGI_BEGIN_REQUEST, request_id,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick sizeof(abrb), 0);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_fcgi_fill_in_request_body(&brb, role, 0 /* *NOT* AP_FCGI_KEEP_CONN */);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_fcgi_header_to_array(&header, farray);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_fcgi_begin_request_body_to_array(&brb, abrb);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick vec[0].iov_base = (void *)farray;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick vec[0].iov_len = sizeof(farray);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick vec[1].iov_base = (void *)abrb;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick vec[1].iov_len = sizeof(abrb);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return sendv_data(conf, r, s, vec, 2, &len);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic apr_status_t send_environment(apr_socket_t *s,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const fcgi_provider_conf *conf,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick request_rec *r, apr_uint16_t request_id,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_pool_t *temp_pool)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *fn = "send_environment";
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const apr_array_header_t *envarr;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const apr_table_entry_t *elts;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick struct iovec vec[2];
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_fcgi_header header;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick unsigned char farray[AP_FCGI_HEADER_LEN];
50cfe8bbbaf4279375802531268e2bf0155215fetrawick char *body;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_status_t rv;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_size_t avail_len, len, required_len;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int i, next_elem, starting_elem;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick envarr = apr_table_elts(r->subprocess_env);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick elts = (const apr_table_entry_t *) envarr->elts;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (APLOG_R_IS_LEVEL(r, APLOG_TRACE2)) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick for (i = 0; i < envarr->nelts; ++i) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!elts[i].key) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick continue;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "%s: '%s': '%s'",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, elts[i].key,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick !strcmp(elts[i].key, "REMOTE_PASSWD") ?
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "XXXXXXXX" : elts[i].val);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* Send envvars over in as many FastCGI records as it takes, */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick next_elem = 0; /* starting with the first one */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick avail_len = 16 * 1024; /* our limit per record, which could have been up
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * to AP_FCGI_MAX_CONTENT_LEN
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick while (next_elem < envarr->nelts) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick starting_elem = next_elem;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick required_len = ap_fcgi_encoded_env_len(r->subprocess_env,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick avail_len,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick &next_elem);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!required_len) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (next_elem < envarr->nelts) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02499) "couldn't encode envvar '%s' in %"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APR_SIZE_T_FMT " bytes",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick elts[next_elem].key, avail_len);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* skip this envvar and continue */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ++next_elem;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick continue;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* only an unused element at the end of the array */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02500) "required len for encoding envvars: %"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APR_SIZE_T_FMT ", %d/%d elems processed so far",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick required_len, next_elem, envarr->nelts);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick body = apr_palloc(temp_pool, required_len);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = ap_fcgi_encode_env(r, r->subprocess_env, body, required_len,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick &starting_elem);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* we pre-compute, so we can't run out of space */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_assert(rv == APR_SUCCESS);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* compute and encode must be in sync */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_assert(starting_elem == next_elem);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_fcgi_fill_in_header(&header, AP_FCGI_PARAMS, request_id,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick (apr_uint16_t)required_len, 0);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_fcgi_header_to_array(&header, farray);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick vec[0].iov_base = (void *)farray;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick vec[0].iov_len = sizeof(farray);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick vec[1].iov_base = body;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick vec[1].iov_len = required_len;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = sendv_data(conf, r, s, vec, 2, &len);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_pool_clear(temp_pool);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return rv;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* Envvars sent, so say we're done */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_fcgi_fill_in_header(&header, AP_FCGI_PARAMS, request_id, 0, 0);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_fcgi_header_to_array(&header, farray);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick vec[0].iov_base = (void *)farray;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick vec[0].iov_len = sizeof(farray);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return sendv_data(conf, r, s, vec, 1, &len);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick/*
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * This header-state logic is from mod_proxy_fcgi.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawickenum {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick HDR_STATE_READING_HEADERS,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick HDR_STATE_GOT_CR,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick HDR_STATE_GOT_CRLF,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick HDR_STATE_GOT_CRLFCR,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick HDR_STATE_GOT_LF,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick HDR_STATE_DONE_WITH_HEADERS
50cfe8bbbaf4279375802531268e2bf0155215fetrawick};
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick/* Try to find the end of the script headers in the response from the back
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * end fastcgi server. STATE holds the current header parsing state for this
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * request.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * Returns 0 if it can't find the end of the headers, and 1 if it found the
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * end of the headers. */
49487586e883b50aa47a1da04f1d7ccb78e70b7dylavicstatic int handle_headers(request_rec *r, int *state,
49487586e883b50aa47a1da04f1d7ccb78e70b7dylavic const char *readbuf, apr_size_t readlen)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *itr = readbuf;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
49487586e883b50aa47a1da04f1d7ccb78e70b7dylavic while (readlen--) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (*itr == '\r') {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick switch (*state) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick case HDR_STATE_GOT_CRLF:
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *state = HDR_STATE_GOT_CRLFCR;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick default:
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *state = HDR_STATE_GOT_CR;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else if (*itr == '\n') {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick switch (*state) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick case HDR_STATE_GOT_LF:
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *state = HDR_STATE_DONE_WITH_HEADERS;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick case HDR_STATE_GOT_CR:
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *state = HDR_STATE_GOT_CRLF;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick case HDR_STATE_GOT_CRLFCR:
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *state = HDR_STATE_DONE_WITH_HEADERS;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick default:
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *state = HDR_STATE_GOT_LF;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
a179659fefc666307e246490dd68973486441ff1ylavic else {
72a881b78500a91dae1264cfd9cabb236ba05e95ylavic *state = HDR_STATE_READING_HEADERS;
a179659fefc666307e246490dd68973486441ff1ylavic }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (*state == HDR_STATE_DONE_WITH_HEADERS)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ++itr;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (*state == HDR_STATE_DONE_WITH_HEADERS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return 0;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick/*
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * handle_response() is based on mod_proxy_fcgi's dispatch()
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic apr_status_t handle_response(const fcgi_provider_conf *conf,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick request_rec *r, apr_socket_t *s,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_pool_t *temp_pool,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_uint16_t request_id,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick char *rspbuf,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_size_t *rspbuflen)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_bucket *b;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_bucket_brigade *ob;
875918e15bac6f68ce1569f9e15fc81f3caeab9frjung apr_size_t orspbuflen = 0;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_status_t rv = APR_SUCCESS;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *fn = "handle_response";
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int header_state = HDR_STATE_READING_HEADERS;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int seen_end_of_headers = 0, done = 0;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rspbuflen) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick orspbuflen = *rspbuflen;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *rspbuflen = 0; /* unless we actually read something */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ob = apr_brigade_create(r->pool, r->connection->bucket_alloc);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick while (!done && rv == APR_SUCCESS) { /* Keep reading FastCGI records until
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * we get AP_FCGI_END_REQUEST (done)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * or an error occurs.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_size_t readbuflen;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_uint16_t clen;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_uint16_t rid;
824a9ee628128474e166986dcb3a51310e7798faylavic char readbuf[AP_IOBUFSIZE];
50cfe8bbbaf4279375802531268e2bf0155215fetrawick unsigned char farray[AP_FCGI_HEADER_LEN];
50cfe8bbbaf4279375802531268e2bf0155215fetrawick unsigned char plen;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick unsigned char type;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick unsigned char version;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = recv_data_full(conf, r, s, (char *)farray, AP_FCGI_HEADER_LEN);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv != APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02501) "%s: Error occurred before reading "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "entire header", fn);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_fcgi_header_fields_from_array(&version, &type, &rid, &clen, &plen,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick farray);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (version != AP_FCGI_VERSION_1) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02502) "%s: Got bogus FastCGI header "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "version %d", fn, (int)version);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = APR_EINVAL;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rid != request_id) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02503) "%s: Got bogus FastCGI header "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "request id %d, expected %d",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, rid, request_id);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = APR_EINVAL;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick recv_again: /* if we need to keep reading more of a record's content */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
824a9ee628128474e166986dcb3a51310e7798faylavic if (clen > sizeof(readbuf)) {
824a9ee628128474e166986dcb3a51310e7798faylavic readbuflen = sizeof(readbuf);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick } else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick readbuflen = clen;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /*
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * Now get the actual content of the record.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (readbuflen != 0) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = recv_data(conf, r, s, readbuf, &readbuflen);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv != APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick switch (type) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick case AP_FCGI_STDOUT: /* Response headers and optional body */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (clen != 0) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick b = apr_bucket_transient_create(readbuf,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick readbuflen,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick r->connection->bucket_alloc);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APR_BRIGADE_INSERT_TAIL(ob, b);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!seen_end_of_headers) {
49487586e883b50aa47a1da04f1d7ccb78e70b7dylavic int st = handle_headers(r, &header_state,
49487586e883b50aa47a1da04f1d7ccb78e70b7dylavic readbuf, readbuflen);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (st == 1) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int status;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick seen_end_of_headers = 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick status =
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_scan_script_header_err_brigade_ex(r, ob,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick NULL,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOG_MODULE_INDEX);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02504) "%s: script header "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "parsing -> %d/%d",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, status, r->status);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rspbuf) { /* caller wants to see response body,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * if any
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_status_t tmprv;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rspbuflen) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *rspbuflen = orspbuflen;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick tmprv = apr_brigade_flatten(ob, rspbuf, rspbuflen);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (tmprv != APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* should not occur for these bucket types;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * does not indicate overflow
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, tmprv, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02505) "%s: error "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "flattening response body",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (status != OK) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick r->status = status;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02506) "%s: Error parsing "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "script headers from %s",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, conf->backend);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = APR_EINVAL;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_pool_clear(temp_pool);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* We're still looking for the end of the
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * headers, so this part of the data will need
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * to persist. */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_bucket_setaside(b, temp_pool);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* If we didn't read all the data go back and get the
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * rest of it. */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (clen > readbuflen) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick clen -= readbuflen;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick goto recv_again;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick case AP_FCGI_STDERR: /* Text to log */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (clen) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02507) "%s: Logged from %s: '%s'",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, conf->backend, readbuf);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (clen > readbuflen) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick clen -= readbuflen;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick goto recv_again; /* continue reading this record */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick case AP_FCGI_END_REQUEST:
50cfe8bbbaf4279375802531268e2bf0155215fetrawick done = 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick default:
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02508) "%s: Got bogus FastCGI record type "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "%d", fn, type);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
0d0d3ba82362da5eed586c40333c03078cb34571ylavic /* Leave on above switch's inner error. */
0d0d3ba82362da5eed586c40333c03078cb34571ylavic if (rv != APR_SUCCESS) {
0d0d3ba82362da5eed586c40333c03078cb34571ylavic break;
0d0d3ba82362da5eed586c40333c03078cb34571ylavic }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /*
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * Read/discard any trailing padding.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
504c4aa29f414902675559aa95d99b8707e03383ylavic if (plen) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = recv_data_full(conf, r, s, readbuf, plen);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv != APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02509) "%s: Error occurred reading "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "padding",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick break;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_brigade_cleanup(ob);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv == APR_SUCCESS && !seen_end_of_headers) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = APR_EINVAL;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02510) "%s: Never reached end of script headers",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return rv;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick/* almost from mod_fcgid */
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic int mod_fcgid_modify_auth_header(void *vars,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *key, const char *val)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* When the application gives a 200 response, the server ignores response
50cfe8bbbaf4279375802531268e2bf0155215fetrawick headers whose names aren't prefixed with Variable- prefix, and ignores
50cfe8bbbaf4279375802531268e2bf0155215fetrawick any response content */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (strncasecmp(key, "Variable-", 9) == 0)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_setn(vars, key, val);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic int fix_auth_header(void *vr, const char *key, const char *val)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick request_rec *r = vr;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r, "moving %s->%s", key, val);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_unset(r->err_headers_out, key);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_setn(r->subprocess_env, key + 9, val);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic void req_rsp(request_rec *r, const fcgi_provider_conf *conf,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *password, const char *apache_role,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick char *rspbuf, apr_size_t *rspbuflen)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *fn = "req_rsp";
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_pool_t *temp_pool;
450a2ff1d5ebc7a2a695bcc1bab1972390415e9arjung apr_size_t orspbuflen = 0;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_socket_t *s;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_status_t rv;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_t *saved_subprocess_env =
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_copy(r->pool, r->subprocess_env);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rspbuflen) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick orspbuflen = *rspbuflen;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *rspbuflen = 0; /* unless we actually read something */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_pool_create(&temp_pool, r->pool);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick setupenv(r, password, apache_role);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
7a42584e6f09102c6776454f090b98a58fd590ebtrawick rv = connect_to_peer(&s, r, conf->backend_addrs,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->backend, FCGI_IO_TIMEOUT);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv == APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_uint16_t request_id = 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = send_begin_request(r, conf, s, AP_FCGI_AUTHORIZER, request_id);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv != APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02511) "%s: Failed writing request to %s",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, conf->backend);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv == APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = send_environment(s, conf, r, request_id, temp_pool);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv != APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02512) "%s: Failed writing environment "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "to %s", fn, conf->backend);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
21a7a1304e24c6206608a59ca1f0a3c8b14c3b54trawick /* The responder owns the request body, not the authorizer.
21a7a1304e24c6206608a59ca1f0a3c8b14c3b54trawick * Don't even send an empty AP_FCGI_STDIN block. libfcgi doesn't care,
21a7a1304e24c6206608a59ca1f0a3c8b14c3b54trawick * but it wasn't sent to authorizers by mod_fastcgi or mod_fcgi and
21a7a1304e24c6206608a59ca1f0a3c8b14c3b54trawick * may be unhandled by the app. Additionally, the FastCGI spec does
21a7a1304e24c6206608a59ca1f0a3c8b14c3b54trawick * not mention FCGI_STDIN in the Authorizer description, though it
21a7a1304e24c6206608a59ca1f0a3c8b14c3b54trawick * does describe FCGI_STDIN elsewhere in more general terms than
21a7a1304e24c6206608a59ca1f0a3c8b14c3b54trawick * simply a wrapper for the client's request body.
21a7a1304e24c6206608a59ca1f0a3c8b14c3b54trawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv == APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rspbuflen) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *rspbuflen = orspbuflen;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = handle_response(conf, r, s, temp_pool, request_id, rspbuf,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rspbuflen);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv != APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02514) "%s: Failed handling response "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "from %s", fn, conf->backend);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_socket_close(s);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv != APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* some sort of mechanical problem */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick r->status = HTTP_INTERNAL_SERVER_ERROR;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02515) "%s: Received HTTP status %d",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, r->status);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick r->subprocess_env = saved_subprocess_env;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (r->status == HTTP_OK) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* An Authorizer application's 200 response may include headers
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * whose names are prefixed with Variable-, and they should be
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * available to subsequent phases via subprocess_env (and yanked
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * from the client response).
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
710362cca9d3cd3ec3de8907998598cd6fa79087trawick apr_table_t *vars = apr_table_make(temp_pool, /* not used to allocate
710362cca9d3cd3ec3de8907998598cd6fa79087trawick * any values that end up
710362cca9d3cd3ec3de8907998598cd6fa79087trawick * in r->(anything)
710362cca9d3cd3ec3de8907998598cd6fa79087trawick */
710362cca9d3cd3ec3de8907998598cd6fa79087trawick 10);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_do(mod_fcgid_modify_auth_header, vars,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick r->err_headers_out, NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_table_do(fix_auth_header, r, vars, NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
710362cca9d3cd3ec3de8907998598cd6fa79087trawick
710362cca9d3cd3ec3de8907998598cd6fa79087trawick apr_pool_destroy(temp_pool);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic int fcgi_check_authn(request_rec *r)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *fn = "fcgi_check_authn";
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_dir_conf *dconf = ap_get_module_config(r->per_dir_config,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick &authnz_fcgi_module);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *password = NULL;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const fcgi_provider_conf *conf;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *prov;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *auth_type;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick char rspbuf[NON200_RESPONSE_BUF_LEN + 1]; /* extra byte for '\0' */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_size_t rspbuflen = sizeof rspbuf - 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int res;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick prov = dconf && dconf->name ? dconf->name : NULL;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!prov || !strcasecmp(prov, "None")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return DECLINED;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick auth_type = ap_auth_type(r);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02516) "%s, prov %s, authoritative %s, "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "require-basic %s, user expr? %s type %s",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, prov,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dconf->authoritative ? "yes" : "no",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dconf->require_basic_auth ? "yes" : "no",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dconf->user_expr ? "yes" : "no",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick auth_type);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (auth_type && !strcasecmp(auth_type, "Basic")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if ((res = ap_get_basic_auth_pw(r, &password))) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02517) "%s: couldn't retrieve basic auth "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "password", fn);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (dconf->require_basic_auth) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return res;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick password = NULL;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf = apr_hash_get(fcgi_authn_providers, prov, APR_HASH_KEY_STRING);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!conf) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02518) "%s: can't find config for provider %s",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, prov);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return HTTP_INTERNAL_SERVER_ERROR;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (APLOGrdebug(r)) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick log_provider_info(conf, r);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick req_rsp(r, conf, password, AP_FCGI_APACHE_ROLE_AUTHENTICATOR_STR,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rspbuf, &rspbuflen);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (r->status == HTTP_OK) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (dconf->user_expr) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *err;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *user = ap_expr_str_exec(r, dconf->user_expr,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick &err);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (user && strlen(user)) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick r->user = apr_pstrdup(r->pool, user);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02519) "%s: Setting user to '%s'",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, r->user);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
bbb64779da3514ea1fee8b806f5ad30d29ea45abtrawick else if (user && dconf->default_user) {
bbb64779da3514ea1fee8b806f5ad30d29ea45abtrawick r->user = apr_pstrdup(r->pool, dconf->default_user);
bbb64779da3514ea1fee8b806f5ad30d29ea45abtrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else if (user) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02520) "%s: Failure extracting user "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "after calling authorizer: user expression "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "yielded empty string (variable not set?)",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick r->status = HTTP_INTERNAL_SERVER_ERROR;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else {
bbb64779da3514ea1fee8b806f5ad30d29ea45abtrawick /* unexpected error, not even an empty string was returned */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02521) "%s: Failure extracting user "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "after calling authorizer: %s",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, err);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick r->status = HTTP_INTERNAL_SERVER_ERROR;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (conf->is_authz) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* combined authn/authz phase, so app won't be invoked for authz
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * Remember that the request was successfully authorized by this
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * provider.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_request_notes *rnotes = apr_palloc(r->pool, sizeof(*rnotes));
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rnotes->successful_authnz_provider = conf->name;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_set_module_config(r->request_config, &authnz_fcgi_module,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rnotes);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* From the spec:
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * For Authorizer response status values other than "200" (OK), the
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * Web server denies access and sends the response status, headers,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * and content back to the HTTP client.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * But:
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * This only makes sense if this authorizer is authoritative.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rspbuflen > 0 && !dconf->authoritative) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02522) "%s: Ignoring response body from non-"
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "authoritative authorizer", fn);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else if (rspbuflen > 0) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rspbuflen == sizeof rspbuf - 1) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* apr_brigade_flatten() interface :( */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02523) "%s: possible overflow handling "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "response body", fn);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rspbuf[rspbuflen] = '\0'; /* we reserved an extra byte for '\0' */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_custom_response(r, r->status, rspbuf); /* API makes a copy */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return r->status == HTTP_OK ?
50cfe8bbbaf4279375802531268e2bf0155215fetrawick OK : dconf->authoritative ? r->status : DECLINED;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic authn_status fcgi_check_password(request_rec *r, const char *user,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *password)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *fn = "fcgi_check_password";
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *prov = apr_table_get(r->notes, AUTHN_PROVIDER_NAME_NOTE);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const fcgi_provider_conf *conf;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02524) "%s(%s, XXX): provider %s",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, user, prov);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!prov) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02525) "%s: provider note isn't set", fn);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return AUTH_GENERAL_ERROR;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf = apr_hash_get(fcgi_authn_providers, prov, APR_HASH_KEY_STRING);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!conf) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02526) "%s: can't find config for provider %s",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, prov);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return AUTH_GENERAL_ERROR;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (APLOGrdebug(r)) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick log_provider_info(conf, r);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick req_rsp(r, conf, password,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* combined authn and authz: FCGI_APACHE_ROLE not set */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->is_authz ? NULL : AP_FCGI_APACHE_ROLE_AUTHENTICATOR_STR,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick NULL, NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (r->status == HTTP_OK) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (conf->is_authz) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* combined authn/authz phase, so app won't be invoked for authz
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * Remember that the request was successfully authorized by this
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * provider.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_request_notes *rnotes = apr_palloc(r->pool, sizeof(*rnotes));
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rnotes->successful_authnz_provider = conf->name;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_set_module_config(r->request_config, &authnz_fcgi_module,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rnotes);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return AUTH_GRANTED;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else if (r->status == HTTP_INTERNAL_SERVER_ERROR) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return AUTH_GENERAL_ERROR;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return AUTH_DENIED;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic const authn_provider fcgi_authn_provider = {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick &fcgi_check_password,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick NULL /* get-realm-hash not supported */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick};
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic authz_status fcgi_authz_check(request_rec *r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *require_line,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const void *parsed_require_line)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *fn = "fcgi_authz_check";
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *prov = apr_table_get(r->notes, AUTHZ_PROVIDER_NAME_NOTE);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const fcgi_provider_conf *conf;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02527) "%s(%s)", fn, require_line);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!prov) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02528) "%s: provider note isn't set", fn);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return AUTHZ_GENERAL_ERROR;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf = apr_hash_get(fcgi_authz_providers, prov, APR_HASH_KEY_STRING);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!conf) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02529) "%s: can't find config for provider %s",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fn, prov);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return AUTHZ_GENERAL_ERROR;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (APLOGrdebug(r)) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick log_provider_info(conf, r);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!r->user) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return AUTHZ_DENIED_NO_USER;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (conf->is_authn) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* combined authn/authz phase, so app won't be invoked for authz
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * If the provider already successfully authorized this request,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * success.
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_request_notes *rnotes = ap_get_module_config(r->request_config,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick &authnz_fcgi_module);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rnotes
50cfe8bbbaf4279375802531268e2bf0155215fetrawick && rnotes->successful_authnz_provider
50cfe8bbbaf4279375802531268e2bf0155215fetrawick && !strcmp(rnotes->successful_authnz_provider, conf->name)) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return AUTHZ_GRANTED;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return AUTHZ_DENIED;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick req_rsp(r, conf, NULL, AP_FCGI_APACHE_ROLE_AUTHORIZER_STR, NULL, NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (r->status == HTTP_OK) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return AUTHZ_GRANTED;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else if (r->status == HTTP_INTERNAL_SERVER_ERROR) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return AUTHZ_GENERAL_ERROR;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return AUTHZ_DENIED;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic const char *fcgi_authz_parse(cmd_parms *cmd, const char *require_line,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const void **parsed_require_line)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* Allowed form: Require [not] registered-provider-name<EOS>
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (strcmp(require_line, "")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return "mod_authnz_fcgi doesn't support restrictions on providers "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "(i.e., multiple require args)";
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return NULL;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic const authz_provider fcgi_authz_provider = {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick &fcgi_authz_check,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick &fcgi_authz_parse,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick};
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic const char *fcgi_check_authn_provider(cmd_parms *cmd,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick void *d,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int argc,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick char *const argv[])
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *dname = "AuthnzFcgiCheckAuthnProvider";
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_dir_conf *dc = d;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int ca = 0;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (ca >= argc) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_pstrcat(cmd->pool, dname, ": No provider given", NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dc->name = argv[ca];
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ca++;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!strcasecmp(dc->name, "None")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (ca < argc) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return "Options aren't supported with \"None\"";
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick while (ca < argc) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *var = argv[ca], *val;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int badarg = 0;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ca++;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* at present, everything needs an argument */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (ca >= argc) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_pstrcat(cmd->pool, dname, ": ", var,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "needs an argument", NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick val = argv[ca];
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ca++;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!strcasecmp(var, "Authoritative")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!strcasecmp(val, "On")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dc->authoritative = 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else if (!strcasecmp(val, "Off")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dc->authoritative = 0;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick badarg = 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
bbb64779da3514ea1fee8b806f5ad30d29ea45abtrawick else if (!strcasecmp(var, "DefaultUser")) {
bbb64779da3514ea1fee8b806f5ad30d29ea45abtrawick dc->default_user = val;
bbb64779da3514ea1fee8b806f5ad30d29ea45abtrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else if (!strcasecmp(var, "RequireBasicAuth")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!strcasecmp(val, "On")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dc->require_basic_auth = 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else if (!strcasecmp(val, "Off")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dc->require_basic_auth = 0;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick badarg = 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else if (!strcasecmp(var, "UserExpr")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *err;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int flags = AP_EXPR_FLAG_DONT_VARY | AP_EXPR_FLAG_RESTRICTED
50cfe8bbbaf4279375802531268e2bf0155215fetrawick | AP_EXPR_FLAG_STRING_RESULT;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dc->user_expr = ap_expr_parse_cmd(cmd, val,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick flags, &err, NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (err) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_psprintf(cmd->pool, "%s: Error parsing '%s': '%s'",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dname, val, err);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_pstrcat(cmd->pool, dname, ": Unexpected option '",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick var, "'", NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (badarg) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_pstrcat(cmd->pool, dname, ": Bad argument '",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick val, "' to option '", var, "'", NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return NULL;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick/* AuthnzFcgiAuthDefineProvider {authn|authz|authnz} provider-name \
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * fcgi://backendhost:backendport/
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic const char *fcgi_define_provider(cmd_parms *cmd,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick void *d,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick int argc,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick char *const argv[])
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *dname = "AuthnzFcgiDefineProvider";
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_rxplus_t *fcgi_backend_regex;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_status_t rv;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick char *host;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick const char *err, *stype;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_provider_conf *conf = apr_pcalloc(cmd->pool, sizeof(*conf));
450a2ff1d5ebc7a2a695bcc1bab1972390415e9arjung int ca = 0, rc, port;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_backend_regex = ap_rxplus_compile(cmd->pool, FCGI_BACKEND_REGEX_STR);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!fcgi_backend_regex) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_psprintf(cmd->pool,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "%s: failed to compile regexec '%s'",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dname, FCGI_BACKEND_REGEX_STR);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (err)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return err;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (ca >= argc) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_pstrcat(cmd->pool, dname, ": No type given", NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick stype = argv[ca];
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ca++;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!strcasecmp(stype, "authn")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->is_authn = 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else if (!strcasecmp(stype, "authz")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->is_authz = 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else if (!strcasecmp(stype, "authnz")) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->is_authn = conf->is_authz = 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_pstrcat(cmd->pool,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dname,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ": Invalid provider type ",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick stype,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (ca >= argc) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_pstrcat(cmd->pool, dname, ": No provider name given", NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->name = argv[ca];
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ca++;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (ca >= argc) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_pstrcat(cmd->pool, dname, ": No backend-address given",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rc = ap_rxplus_exec(cmd->pool, fcgi_backend_regex, argv[ca], NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (!rc || ap_rxplus_nmatch(fcgi_backend_regex) != 3) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_pstrcat(cmd->pool,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dname, ": backend-address '",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick argv[ca],
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "' has invalid form",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick host = ap_rxplus_pmatch(cmd->pool, fcgi_backend_regex, 1);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (host[0] == '[' && host[strlen(host) - 1] == ']') {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick host += 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick host[strlen(host) - 1] = '\0';
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
450a2ff1d5ebc7a2a695bcc1bab1972390415e9arjung port = atoi(ap_rxplus_pmatch(cmd->pool, fcgi_backend_regex, 2));
450a2ff1d5ebc7a2a695bcc1bab1972390415e9arjung if (port > 65535) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_pstrcat(cmd->pool,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dname, ": backend-address '",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick argv[ca],
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "' has invalid port",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->backend = argv[ca];
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->host = host;
450a2ff1d5ebc7a2a695bcc1bab1972390415e9arjung conf->port = port;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ca++;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick rv = apr_sockaddr_info_get(&conf->backend_addrs, conf->host,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APR_UNSPEC, conf->port, 0, cmd->pool);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (rv != APR_SUCCESS) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, rv, NULL,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APLOGNO(02530) "Address %s could not be resolved",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->backend);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_pstrcat(cmd->pool,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dname,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ": Error resolving backend address",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (ca != argc) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return apr_pstrcat(cmd->pool,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dname,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ": Unexpected parameter ",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick argv[ca],
50cfe8bbbaf4279375802531268e2bf0155215fetrawick NULL);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (conf->is_authn) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_hash_set(fcgi_authn_providers, conf->name, APR_HASH_KEY_STRING,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_register_auth_provider(cmd->pool, AUTHN_PROVIDER_GROUP,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->name,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick AUTHN_PROVIDER_VERSION,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick &fcgi_authn_provider,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick AP_AUTH_INTERNAL_PER_CONF);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (conf->is_authz) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_hash_set(fcgi_authz_providers, conf->name, APR_HASH_KEY_STRING,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_register_auth_provider(cmd->pool, AUTHZ_PROVIDER_GROUP,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick conf->name,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick AUTHZ_PROVIDER_VERSION,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick &fcgi_authz_provider,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick AP_AUTH_INTERNAL_PER_CONF);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return NULL;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic const command_rec fcgi_cmds[] = {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick AP_INIT_TAKE_ARGV("AuthnzFcgiDefineProvider",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_define_provider,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick NULL,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick RSRC_CONF,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "Define a FastCGI authn and/or authz provider"),
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick AP_INIT_TAKE_ARGV("AuthnzFcgiCheckAuthnProvider",
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_check_authn_provider,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick NULL,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick OR_FILEINFO,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "Enable/disable a FastCGI authorizer to handle "
50cfe8bbbaf4279375802531268e2bf0155215fetrawick "check_authn phase"),
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick {NULL}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick};
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic int fcgi_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick apr_pool_t *ptemp)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_authn_providers = apr_hash_make(pconf);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_authz_providers = apr_hash_make(pconf);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return OK;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic void fcgi_register_hooks(apr_pool_t *p)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick static const char * const auth_basic_runs_after_me[] =
50cfe8bbbaf4279375802531268e2bf0155215fetrawick {"mod_auth_basic.c", NULL}; /* to allow for custom response */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_hook_pre_config(fcgi_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick ap_hook_check_authn(fcgi_check_authn, NULL, auth_basic_runs_after_me,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick APR_HOOK_MIDDLE, AP_AUTH_INTERNAL_PER_CONF);
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic void *create_dir_conf(apr_pool_t *p, char *dummy)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_dir_conf *dconf = apr_pcalloc(p, sizeof(fcgi_dir_conf));
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick dconf->authoritative = 1;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return dconf;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickstatic void *merge_dir_conf(apr_pool_t *p, void *basev, void *overridesv)
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_dir_conf *a = (fcgi_dir_conf *)apr_pcalloc(p, sizeof(*a));
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_dir_conf *base = (fcgi_dir_conf *)basev,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick *over = (fcgi_dir_conf *)overridesv;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick /* currently we just have a single directive applicable to a
50cfe8bbbaf4279375802531268e2bf0155215fetrawick * directory, so if it is set then grab all fields from fcgi_dir_conf
50cfe8bbbaf4279375802531268e2bf0155215fetrawick */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick if (over->name) {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick memcpy(a, over, sizeof(*a));
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick else {
50cfe8bbbaf4279375802531268e2bf0155215fetrawick memcpy(a, base, sizeof(*a));
50cfe8bbbaf4279375802531268e2bf0155215fetrawick }
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawick return a;
50cfe8bbbaf4279375802531268e2bf0155215fetrawick}
50cfe8bbbaf4279375802531268e2bf0155215fetrawick
50cfe8bbbaf4279375802531268e2bf0155215fetrawickAP_DECLARE_MODULE(authnz_fcgi) =
50cfe8bbbaf4279375802531268e2bf0155215fetrawick{
50cfe8bbbaf4279375802531268e2bf0155215fetrawick STANDARD20_MODULE_STUFF,
50cfe8bbbaf4279375802531268e2bf0155215fetrawick create_dir_conf, /* dir config creater */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick merge_dir_conf, /* dir merger */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick NULL, /* server config */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick NULL, /* merge server config */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_cmds, /* command apr_table_t */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick fcgi_register_hooks /* register hooks */
50cfe8bbbaf4279375802531268e2bf0155215fetrawick};