/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* .
*
*/
#include "httpd.h"
#define CORE_PRIVATE
#include "http_protocol.h"
#include "http_config.h"
#include "http_connection.h"
#include "http_core.h"
#include "http_request.h"
#include "ap_config.h"
#include "apr_strings.h"
#include "apr_pools.h"
#include "apr_hash.h"
#include "apr_buckets.h"
#include "apr_general.h"
#include "util_filter.h"
#include "scoreboard.h"
#include "pop.h"
#include
static request_rec *pop_create_request(pop_user_rec *ur)
{
apr_pool_t *p;
request_rec *r;
apr_pool_create(&p, ur->p);
r = apr_pcalloc(p, sizeof(*r));
r->pool = p;
r->connection = ur->c;
r->server = ur->c->base_server;
ur->c->keepalive = 0;
r->user = NULL;
r->ap_auth_type = NULL;
r->allowed_methods = ap_make_method_list(p, 2);
r->headers_in = apr_table_make(r->pool, 1);
r->subprocess_env = NULL;
r->headers_out = apr_table_make(r->pool, 1);
r->err_headers_out = apr_table_make(r->pool, 1);
r->notes = apr_table_make(r->pool, 5);
r->request_config = ap_create_request_config(r->pool);
ap_run_create_request(r);
r->per_dir_config = r->server->lookup_defaults;
r->sent_bodyct = 0; /* bytect isn't for body */
r->output_filters = ur->c->output_filters;
r->input_filters = ur->c->input_filters;
r->status = HTTP_OK; /* Until further notice. */
ap_set_module_config(r->request_config, &pop_module, ur);
return r;
}
static int process_pop_connection(conn_rec *c)
{
server_rec *s = c->base_server;
request_rec *r;
pop_user_rec *ur;
apr_pool_t *p;
apr_bucket_brigade *bb;
pop_conn_rec *conf = (pop_conn_rec *)ap_get_module_config(s->module_config,
&pop_module);
if (!conf->pop_on) {
return DECLINED;
}
ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
ap_add_output_filter("POP3_OUTPUT", NULL, NULL, c);
apr_pool_create(&p, c->pool);
ur = apr_palloc(p, sizeof(*ur));
ur->ctx = apr_palloc(p, sizeof(*ur->ctx));
ur->p = p;
ur->c = c;
ur->state = POP_AUTH;
ur->high_access = 0; /* We always start at 0. */
bb = apr_brigade_create(ur->p, c->bucket_alloc);
r = pop_create_request(ur);
ap_fprintf(c->output_filters, bb,
"+OK %s POP3 server ready (Comments to: %s)\r\n",
ap_get_server_name(r), r->server->server_admin);
ap_fflush(c->output_filters, bb);
process_pop_connection_internal(r, bb);
return OK;
}
/* This filter both ensures that we are sending CRLF at the end of every
* line, and it escapes '.' if it is the first character on a line. I have
* included both of these operations into one function, because it is
* relatively simple to do both at one time.
*/
static apr_status_t pop_crlf_escape_filter(ap_filter_t * f,
apr_bucket_brigade * bb)
{
apr_bucket *e;
apr_status_t rv;
const char *buf;
const char *pos;
while ( !APR_BRIGADE_EMPTY( bb ) ) {
e = APR_BRIGADE_FIRST(bb);
apr_size_t len = e->length;
if (e->length != 0) {
rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
if (rv != APR_SUCCESS) {
return rv;
}
/* We search the data for a LF, if we find one, we split the
* bucket so that the LF is the first character in the new
* bucket. We then create a new bucket with a CR and insert
* it before the LF bucket.
*/
pos = memchr(buf, APR_ASCII_LF, len);
while (pos) {
apr_bucket *b = NULL;
if ((pos > buf) && (*(pos - 1) != APR_ASCII_CR)) {
/* XXX: won't detect a bare LF at the beginning
* of any bucket not created by this function */
apr_bucket_split(e, pos - buf);
b = apr_bucket_immortal_create("\r", 1, f->c->bucket_alloc);
APR_BUCKET_INSERT_AFTER(e, b);
e = APR_BUCKET_NEXT(e); /* Skip the inserted bucket */
break;
}
else if (pos - buf + 1 < len) {
/* there is at least one more char left in the bucket */
if (*(pos + 1) == '.') {
apr_bucket_split(e, pos - buf + 1);
b = apr_bucket_immortal_create(".", 1,
f->c->bucket_alloc);
APR_BUCKET_INSERT_AFTER(e, b);
e = APR_BUCKET_NEXT(e); /* Skip the inserted bucket */
break;
}
pos = memchr(pos+1, APR_ASCII_LF, len-(pos-buf+1));
}
else {
/* done with this bucket */
break;
}
}
}
apr_bucket_delete(e);
}
return ap_pass_brigade(f->next, bb);
}
void ap_pop_register_handler(char *key, ap_pop_handler *func, int states,
apr_pool_t *p)
{
char *dupkey = apr_pstrdup(p, key);
pop_handler_st *hand = apr_palloc(p, sizeof(*hand));
hand->func = func;
hand->states = states;
ap_str_tolower(dupkey);
apr_hash_set(ap_pop_hash, dupkey, APR_HASH_KEY_STRING, hand);
}
static void register_hooks(apr_pool_t *p)
{
ap_pop_hash = apr_hash_make(p);
ap_hook_process_connection(process_pop_connection,NULL,NULL,
APR_HOOK_MIDDLE);
ap_pop_register_handler("DELE", ap_handle_dele, POP_TRANSACTION, p);
ap_pop_register_handler("LAST", ap_handle_last, POP_TRANSACTION, p);
ap_pop_register_handler("LIST", ap_handle_list, POP_TRANSACTION, p);
ap_pop_register_handler("NOOP", ap_handle_noop, POP_TRANSACTION, p);
ap_pop_register_handler("PASS", ap_handle_passwd, USER_ACK, p);
ap_pop_register_handler("QUIT", ap_handle_quit, POP_ALL_STATES, p);
ap_pop_register_handler("RETR", ap_handle_retr, POP_TRANSACTION, p);
ap_pop_register_handler("RSET", ap_handle_rset, POP_TRANSACTION, p);
ap_pop_register_handler("STAT", ap_handle_stat, POP_TRANSACTION, p);
ap_pop_register_handler("TOP", ap_handle_top, POP_TRANSACTION, p);
ap_pop_register_handler("UIDL", ap_handle_uidl, POP_TRANSACTION, p);
ap_pop_register_handler("USER", ap_handle_user, POP_AUTH, p);
ap_register_output_filter("POP3_OUTPUT", pop_crlf_escape_filter, NULL,
AP_FTYPE_CONNECTION);
}
static void *pop_create_server(apr_pool_t *p, server_rec *s)
{
pop_conn_rec *conf = (pop_conn_rec *)apr_pcalloc(p, sizeof(*conf));
conf->pop_on = 0;
conf->maildrop = POP_DEFAULT_MAILDROP;
return conf;
}
static const char *set_pop_protocol(cmd_parms *cmd, void *dummy, int flag)
{
server_rec *s = cmd->server;
pop_conn_rec *conf = (pop_conn_rec *)ap_get_module_config(s->module_config,
&pop_module);
const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
if (err) {
return err;
}
conf->pop_on = flag;
return NULL;
}
static const char *set_pop_maildrop(cmd_parms *cmd, void *dummy, const char *arg)
{
server_rec *s = cmd->server;
pop_conn_rec *conf = (pop_conn_rec *)ap_get_module_config(s->module_config,
&pop_module);
const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
if (err) {
return err;
}
conf->maildrop = apr_pstrdup(cmd->pool, arg);
return NULL;
}
static const command_rec pop_cmds[] = {
AP_INIT_FLAG("POP3Protocol", set_pop_protocol, NULL, RSRC_CONF,
"Whether this server is serving the POP3 protocol"),
AP_INIT_TAKE1("POP3MailDrops", set_pop_maildrop, NULL, RSRC_CONF,
"Root of the mail drops"),
{ NULL }
};
module AP_MODULE_DECLARE_DATA pop_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
pop_create_server, /* create per-server config structure */
NULL, /* merge per-server config structures */
pop_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};