Blame modules/metadata/mod_ident.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * mod_ident: Handle RFC 1413 ident request
Packit 90a5c9
 * obtained from rfc1413.c
Packit 90a5c9
 *
Packit 90a5c9
 * rfc1413() speaks a common subset of the RFC 1413, AUTH, TAP and IDENT
Packit 90a5c9
 * protocols. The code queries an RFC 1413 etc. compatible daemon on a remote
Packit 90a5c9
 * host to look up the owner of a connection. The information should not be
Packit 90a5c9
 * used for authentication purposes. This routine intercepts alarm signals.
Packit 90a5c9
 *
Packit 90a5c9
 * Author: Wietse Venema, Eindhoven University of Technology,
Packit 90a5c9
 * The Netherlands.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/* Some small additions for Apache --- ditch the "sccsid" var if
Packit 90a5c9
 * compiling with gcc (it *has* changed), include ap_config.h for the
Packit 90a5c9
 * prototypes it defines on at least one system (SunlOSs) which has
Packit 90a5c9
 * them missing from the standard header files, and one minor change
Packit 90a5c9
 * below (extra parens around assign "if (foo = bar) ..." to shut up
Packit 90a5c9
 * gcc -Wall).
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/* Rewritten by David Robinson */
Packit 90a5c9
Packit 90a5c9
#include "apr.h"
Packit 90a5c9
#include "apr_network_io.h"
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "apr_optional.h"
Packit 90a5c9
Packit 90a5c9
#define APR_WANT_STDIO
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
Packit 90a5c9
#include "httpd.h"              /* for server_rec, conn_rec, etc. */
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "http_log.h"           /* for aplog_error */
Packit 90a5c9
#include "util_ebcdic.h"
Packit 90a5c9
Packit 90a5c9
/* Whether we should enable rfc1413 identity checking */
Packit 90a5c9
#ifndef DEFAULT_RFC1413
Packit 90a5c9
#define DEFAULT_RFC1413    0
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#define RFC1413_UNSET 2
Packit 90a5c9
Packit 90a5c9
/* request timeout (sec) */
Packit 90a5c9
#ifndef RFC1413_TIMEOUT
Packit 90a5c9
#define RFC1413_TIMEOUT   30
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
/* Local stuff. */
Packit 90a5c9
Packit 90a5c9
/* Semi-well-known port */
Packit 90a5c9
#define RFC1413_PORT     113
Packit 90a5c9
Packit 90a5c9
/* maximum allowed length of userid */
Packit 90a5c9
#define RFC1413_USERLEN  512
Packit 90a5c9
Packit 90a5c9
/* rough limit on the amount of data we accept. */
Packit 90a5c9
#define RFC1413_MAXDATA 1000
Packit 90a5c9
Packit 90a5c9
/* default username, if it could not determined */
Packit 90a5c9
#define FROM_UNKNOWN  "unknown"
Packit 90a5c9
Packit 90a5c9
typedef struct {
Packit 90a5c9
    int do_rfc1413;
Packit 90a5c9
    int timeout_unset;
Packit 90a5c9
    apr_time_t timeout;
Packit 90a5c9
} ident_config_rec;
Packit 90a5c9
Packit 90a5c9
static apr_status_t rfc1413_connect(apr_socket_t **newsock, conn_rec *conn,
Packit 90a5c9
                                    server_rec *srv, apr_time_t timeout)
Packit 90a5c9
{
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    apr_sockaddr_t *localsa, *destsa;
Packit 90a5c9
Packit 90a5c9
    if ((rv = apr_sockaddr_info_get(&localsa, conn->local_ip, APR_UNSPEC,
Packit 90a5c9
                              0, /* ephemeral port */
Packit 90a5c9
                              0, conn->pool)) != APR_SUCCESS) {
Packit 90a5c9
        /* This should not fail since we have a numeric address string
Packit 90a5c9
         * as the host. */
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, srv, APLOGNO(01492)
Packit 90a5c9
                     "rfc1413: apr_sockaddr_info_get(%s) failed",
Packit 90a5c9
                     conn->local_ip);
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if ((rv = apr_sockaddr_info_get(&destsa, conn->client_ip,
Packit 90a5c9
                              localsa->family, /* has to match */
Packit 90a5c9
                              RFC1413_PORT, 0, conn->pool)) != APR_SUCCESS) {
Packit 90a5c9
        /* This should not fail since we have a numeric address string
Packit 90a5c9
         * as the host. */
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, srv, APLOGNO(01493)
Packit 90a5c9
                     "rfc1413: apr_sockaddr_info_get(%s) failed",
Packit 90a5c9
                     conn->client_ip);
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if ((rv = apr_socket_create(newsock,
Packit 90a5c9
                                localsa->family, /* has to match */
Packit 90a5c9
                                SOCK_STREAM, 0, conn->pool)) != APR_SUCCESS) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, srv, APLOGNO(01494)
Packit 90a5c9
                     "rfc1413: error creating query socket");
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if ((rv = apr_socket_timeout_set(*newsock, timeout)) != APR_SUCCESS) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, srv, APLOGNO(01495)
Packit 90a5c9
                     "rfc1413: error setting query socket timeout");
Packit 90a5c9
        apr_socket_close(*newsock);
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Bind the local and remote ends of the query socket to the same
Packit 90a5c9
 * IP addresses as the connection under investigation. We go
Packit 90a5c9
 * through all this trouble because the local or remote system
Packit 90a5c9
 * might have more than one network address. The RFC1413 etc.
Packit 90a5c9
 * client sends only port numbers; the server takes the IP
Packit 90a5c9
 * addresses from the query socket.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
    if ((rv = apr_socket_bind(*newsock, localsa)) != APR_SUCCESS) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_CRIT, rv, srv, APLOGNO(01496)
Packit 90a5c9
                     "rfc1413: Error binding query socket to local port");
Packit 90a5c9
        apr_socket_close(*newsock);
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * errors from connect usually imply the remote machine doesn't support
Packit 90a5c9
 * the service; don't log such an error
Packit 90a5c9
 */
Packit 90a5c9
    if ((rv = apr_socket_connect(*newsock, destsa)) != APR_SUCCESS) {
Packit 90a5c9
        apr_socket_close(*newsock);
Packit 90a5c9
        return rv;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t rfc1413_query(apr_socket_t *sock, conn_rec *conn,
Packit 90a5c9
                                  server_rec *srv)
Packit 90a5c9
{
Packit 90a5c9
    apr_port_t rmt_port, our_port;
Packit 90a5c9
    apr_port_t sav_rmt_port, sav_our_port;
Packit 90a5c9
    apr_size_t i;
Packit 90a5c9
    char *cp;
Packit 90a5c9
    char buffer[RFC1413_MAXDATA + 1];
Packit 90a5c9
    char user[RFC1413_USERLEN + 1];     /* XXX */
Packit 90a5c9
    apr_size_t buflen;
Packit 90a5c9
Packit 90a5c9
    sav_our_port = conn->local_addr->port;
Packit 90a5c9
    sav_rmt_port = conn->client_addr->port;
Packit 90a5c9
Packit 90a5c9
    /* send the data */
Packit 90a5c9
    buflen = apr_snprintf(buffer, sizeof(buffer), "%hu,%hu\r\n", sav_rmt_port,
Packit 90a5c9
                          sav_our_port);
Packit 90a5c9
    ap_xlate_proto_to_ascii(buffer, buflen);
Packit 90a5c9
Packit 90a5c9
    /* send query to server. Handle short write. */
Packit 90a5c9
    i = 0;
Packit 90a5c9
    while (i < buflen) {
Packit 90a5c9
        apr_size_t j = strlen(buffer + i);
Packit 90a5c9
        apr_status_t status;
Packit 90a5c9
        status  = apr_socket_send(sock, buffer+i, &j);
Packit 90a5c9
        if (status != APR_SUCCESS) {
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_CRIT, status, srv, APLOGNO(01497)
Packit 90a5c9
                         "write: rfc1413: error sending request");
Packit 90a5c9
            return status;
Packit 90a5c9
        }
Packit 90a5c9
        else if (j > 0) {
Packit 90a5c9
            i+=j;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Read response from server. - the response should be newline
Packit 90a5c9
     * terminated according to rfc - make sure it doesn't stomp its
Packit 90a5c9
     * way out of the buffer.
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    i = 0;
Packit 90a5c9
    memset(buffer, '\0', sizeof(buffer));
Packit 90a5c9
    /*
Packit 90a5c9
     * Note that the strchr function below checks for \012 instead of '\n'
Packit 90a5c9
     * this allows it to work on both ASCII and EBCDIC machines.
Packit 90a5c9
     */
Packit 90a5c9
    while ((cp = strchr(buffer, '\012')) == NULL && i < sizeof(buffer) - 1) {
Packit 90a5c9
        apr_size_t j = sizeof(buffer) - 1 - i;
Packit 90a5c9
        apr_status_t status;
Packit 90a5c9
        status = apr_socket_recv(sock, buffer+i, &j);
Packit 90a5c9
        if (status != APR_SUCCESS) {
Packit 90a5c9
            ap_log_error(APLOG_MARK, APLOG_CRIT, status, srv, APLOGNO(01498)
Packit 90a5c9
                         "read: rfc1413: error reading response");
Packit 90a5c9
            return status;
Packit 90a5c9
        }
Packit 90a5c9
        else if (j > 0) {
Packit 90a5c9
            i+=j;
Packit 90a5c9
        }
Packit 90a5c9
        else if (status == APR_SUCCESS && j == 0) {
Packit 90a5c9
            /* Oops... we ran out of data before finding newline */
Packit 90a5c9
            return APR_EINVAL;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
/* RFC1413_USERLEN = 512 */
Packit 90a5c9
    ap_xlate_proto_from_ascii(buffer, i);
Packit 90a5c9
    if (sscanf(buffer, "%hu , %hu : USERID :%*[^:]:%512s", &rmt_port, &our_port,
Packit 90a5c9
               user) != 3 || sav_rmt_port != rmt_port
Packit 90a5c9
        || sav_our_port != our_port)
Packit 90a5c9
        return APR_EINVAL;
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Strip trailing carriage return. It is part of the
Packit 90a5c9
     * protocol, not part of the data.
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    if ((cp = strchr(user, '\r')))
Packit 90a5c9
        *cp = '\0';
Packit 90a5c9
Packit 90a5c9
    conn->remote_logname = apr_pstrdup(conn->pool, user);
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *set_idcheck(cmd_parms *cmd, void *d_, int arg)
Packit 90a5c9
{
Packit 90a5c9
    ident_config_rec *d = d_;
Packit 90a5c9
Packit 90a5c9
    d->do_rfc1413 = arg ? 1 : 0;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *set_timeout(cmd_parms *cmd, void *d_, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    ident_config_rec *d = d_;
Packit 90a5c9
Packit 90a5c9
    d->timeout = apr_time_from_sec(atoi(arg));
Packit 90a5c9
    d->timeout_unset = 0;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *create_ident_dir_config(apr_pool_t *p, char *d)
Packit 90a5c9
{
Packit 90a5c9
    ident_config_rec *conf = apr_palloc(p, sizeof(*conf));
Packit 90a5c9
Packit 90a5c9
    conf->do_rfc1413 = DEFAULT_RFC1413 | RFC1413_UNSET;
Packit 90a5c9
    conf->timeout = apr_time_from_sec(RFC1413_TIMEOUT);
Packit 90a5c9
    conf->timeout_unset = 1;
Packit 90a5c9
Packit 90a5c9
    return (void *)conf;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *merge_ident_dir_config(apr_pool_t *p, void *old_, void *new_)
Packit 90a5c9
{
Packit 90a5c9
    ident_config_rec *conf = (ident_config_rec *)apr_pcalloc(p, sizeof(*conf));
Packit 90a5c9
    ident_config_rec *old = (ident_config_rec *) old_;
Packit 90a5c9
    ident_config_rec *new = (ident_config_rec *) new_;
Packit 90a5c9
Packit 90a5c9
    conf->timeout = new->timeout_unset
Packit 90a5c9
                        ? old->timeout
Packit 90a5c9
                        : new->timeout;
Packit 90a5c9
Packit 90a5c9
    conf->do_rfc1413 = new->do_rfc1413 & RFC1413_UNSET
Packit 90a5c9
                           ? old->do_rfc1413
Packit 90a5c9
                           : new->do_rfc1413;
Packit 90a5c9
Packit 90a5c9
    return (void *)conf;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const command_rec ident_cmds[] =
Packit 90a5c9
{
Packit 90a5c9
    AP_INIT_FLAG("IdentityCheck", set_idcheck, NULL, RSRC_CONF|ACCESS_CONF,
Packit 90a5c9
                 "Enable identd (RFC 1413) user lookups - SLOW"),
Packit 90a5c9
    AP_INIT_TAKE1("IdentityCheckTimeout", set_timeout, NULL,
Packit 90a5c9
                  RSRC_CONF|ACCESS_CONF,
Packit 90a5c9
                  "Identity check (RFC 1413) timeout duration (sec)"),
Packit 90a5c9
    {NULL}
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
module AP_MODULE_DECLARE_DATA ident_module;
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Optional function for the core to the actual ident request
Packit 90a5c9
 */
Packit 90a5c9
static const char *ap_ident_lookup(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    ident_config_rec *conf;
Packit 90a5c9
    apr_socket_t *sock;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    conn_rec *conn = r->connection;
Packit 90a5c9
    server_rec *srv = r->server;
Packit 90a5c9
Packit 90a5c9
    conf = ap_get_module_config(r->per_dir_config, &ident_module);
Packit 90a5c9
Packit 90a5c9
    /* return immediately if ident requests are disabled */
Packit 90a5c9
    if (!(conf->do_rfc1413 & ~RFC1413_UNSET)) {
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    rv = rfc1413_connect(&sock, conn, srv, conf->timeout);
Packit 90a5c9
    if (rv == APR_SUCCESS) {
Packit 90a5c9
        rv = rfc1413_query(sock, conn, srv);
Packit 90a5c9
        apr_socket_close(sock);
Packit 90a5c9
    }
Packit 90a5c9
    if (rv != APR_SUCCESS) {
Packit 90a5c9
        conn->remote_logname = FROM_UNKNOWN;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return (const char *)conn->remote_logname;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void register_hooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    APR_REGISTER_OPTIONAL_FN(ap_ident_lookup);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(ident) =
Packit 90a5c9
{
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    create_ident_dir_config,       /* dir config creater */
Packit 90a5c9
    merge_ident_dir_config,        /* dir merger --- default is to override */
Packit 90a5c9
    NULL,                          /* server config */
Packit 90a5c9
    NULL,                          /* merge server config */
Packit 90a5c9
    ident_cmds,                    /* command apr_table_t */
Packit 90a5c9
    register_hooks                 /* register hooks */
Packit 90a5c9
};