
/* ====================================================================
 * Copyright (c) 1995 The Apache Group.  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. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the Apache Group
 *    for use in the Apache HTTP server project (http://www.apache.org/)."
 *
 * 4. The names "Apache Server" and "Apache Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission.
 *
 * 5. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the Apache Group
 *    for use in the Apache HTTP server project (http://www.apache.org/)."
 *
 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``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 GROUP 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 Group and was originally based
 * on public domain software written at the National Center for
 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
 * For more information on the Apache Group and the Apache HTTP server
 * project, please see <http://www.apache.org/>.
 *
 */


/*
 * http_afs.c: Stuff for dealing with files in AFS
 * 
 * By Matthew Gray
 *
 * Based on mod_alias.c:
 * Original by Rob McCool, rewritten in succession by David Robinson
 * and rst.
 * 
 */
#if defined(SOLARIS2)
#define AFS_ST_DEV 1
#elif defined(ULTRIX)
#define AFS_ST_DEV -30975
#elif defined(SUNOS4)
#define AFS_ST_DEV 1
#else
#define AFS_ST_DEV 1
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include "httpd.h"
#include "http_config.h"

typedef struct {
    char *real;
    char *fake;
    char *forced_type;
} afs_entry;

typedef struct {
    array_header *afschecks;
} afs_server_conf;

module afs_module;

void *create_afs_config (pool *p, server_rec *s)
{
    afs_server_conf *a =
      (afs_server_conf *)pcalloc (p, sizeof(afs_server_conf));

    a->afschecks = make_array (p, 20, sizeof(afs_entry));
    return a;
}

void *merge_afs_config (pool *p, void *basev, void *overridesv)
{
    afs_server_conf *a =
	(afs_server_conf *)pcalloc (p, sizeof(afs_server_conf));
    afs_server_conf *base = (afs_server_conf *)basev,
	*overrides = (afs_server_conf *)overridesv;

    a->afschecks = append_arrays (p, overrides->afschecks, base->afschecks);
    return a;
}

char *add_afscheck(cmd_parms *cmd, void *dummy, char *f, char *url)
{
     server_rec *s = cmd->server;
     afs_server_conf *conf =
       (afs_server_conf *)get_module_config(s->module_config,&afs_module);
     afs_entry *new = push_array (conf->afschecks);
     new->fake=f; new->real=url;
     return NULL;
}

command_rec afs_cmds[] = {
{ "AFS", add_afscheck, NULL, RSRC_CONF, TAKE2,
    "a document that must be in AFS, or be redirected" },
{ NULL }
};

int afs_matches (char *uri, char *afs_fakename)
{
    char *end_fakename = afs_fakename + strlen (afs_fakename);
    char *afsp = afs_fakename, *urip = uri;

    while (afsp < end_fakename) {
	if (*afsp == '/') {
	    /* any number of '/' in the afs matches any number in
	     * the supplied URI, but there must be at least one...
	     */
	    if (*urip != '/') return 0;
	    
	    while (*afsp == '/') ++ afsp;
	    while (*urip == '/') ++ urip;
	}
	else {
	    /* Other characters are compared literally */
	    if (*urip++ != *afsp++) return 0;
	}
    }

    /* Check last afs path component matched all the way */

    if (afsp[-1] != '/' && *urip != '\0' && *urip != '/')
	return 0;

    /* Return number of characters from URI which matched (may be
     * greater than length of afs, since we may have matched
     * doubled slashes)
     */

    return urip - uri;
}

char *try_afs_list (request_rec *r, array_header *afses, int doesc)
{
    afs_entry *entries = (afs_entry *)afses->elts;
    int i;
    int statr;
    struct stat finfo;
    char *thefile;
    
    for (i = 0; i < afses->nelts; ++i) {
        afs_entry *p = &entries[i];
        int l = afs_matches (r->uri, p->fake);

        if (l > 0) {
	     thefile = pstrcat(r->pool, document_root(r), r->uri, NULL);
	     statr = stat(thefile, &finfo);
	     if(statr != -1 && finfo.st_dev != AFS_ST_DEV) {
		  return pstrcat(r->pool, p->real, NULL);
	     }
	     else {
		  return pstrcat(r->pool, thefile, NULL);
	     }

	    if (doesc) {
		char *escurl;
		/* would like to use os_escape_path here, but can't */
		escurl = escape_uri(r->pool, r->uri + l);
		return pstrcat(r->pool, p->real, escurl, NULL);
	    } else
		return pstrcat(r->pool, p->real, r->uri + l, NULL);
        }
    }

    return NULL;
}

int translate_afs_redir(request_rec *r)
{
    void *sconf = r->server->module_config;
    afs_server_conf *conf =
        (afs_server_conf *)get_module_config(sconf, &afs_module);
    char *ret;

    if (r->uri[0] != '/' && r->uri[0] != '\0') 
        return BAD_REQUEST;

    if ((ret = try_afs_list (r, conf->afschecks, 0)) != NULL) {
	 r->filename = ret;
	 return OK;
    }
    

    return DECLINED;
}

module afs_module = {
   STANDARD_MODULE_STUFF,
   NULL,			/* initializer */
   NULL,			/* dir config creater */
   NULL,			/* dir merger --- default is to override */
   create_afs_config,		/* server config */
   merge_afs_config,		/* merge server configs */
   afs_cmds,			/* command table */
   NULL,			/* handlers */
   translate_afs_redir,	/* filename translation */
   NULL,			/* check_user_id */
   NULL,			/* check auth */
   NULL,			/* check access */
   NULL,		/* type_checker */
   NULL,			/* fixups */
   NULL				/* logger */
};
