mod_mime_magic.c revision 443f96ac390c06ca4fa763d3bfcfbc8f757bd756
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina/* ====================================================================
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * The Apache Software License, Version 1.1
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * Copyright (c) 2000 The Apache Software Foundation. All rights
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * reserved.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * Redistribution and use in source and binary forms, with or without
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * modification, are permitted provided that the following conditions
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * are met:
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * 1. Redistributions of source code must retain the above copyright
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * notice, this list of conditions and the following disclaimer.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * 2. Redistributions in binary form must reproduce the above copyright
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * notice, this list of conditions and the following disclaimer in
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * the documentation and/or other materials provided with the
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * distribution.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * 3. The end-user documentation included with the redistribution,
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * if any, must include the following acknowledgment:
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * "This product includes software developed by the
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * Apache Software Foundation (http://www.apache.org/)."
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * Alternately, this acknowledgment may appear in the software itself,
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * if and wherever such third-party acknowledgments normally appear.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * 4. The names "Apache" and "Apache Software Foundation" must
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * not be used to endorse or promote products derived from this
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * software without prior written permission. For written
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * permission, please contact apache@apache.org.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
50c9d542e8bf641412debaa82a4dcf67ddb72258Lukas Slebodnik * 5. Products derived from this software may not be called "Apache",
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * nor may "Apache" appear in their name, without prior written
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * permission of the Apache Software Foundation.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
bbc34d5a6e84d6c337bd89a22d33e365eb466226Pavel Březina * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
bbc34d5a6e84d6c337bd89a22d33e365eb466226Pavel Březina * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech * SUCH DAMAGE.
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech * ====================================================================
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech *
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech * This software consists of voluntary contributions made by many
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech * individuals on behalf of the Apache Software Foundation. For more
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * information on the Apache Software Foundation, please see
b0e8c1802557645e2ff6a88c54c520b0f0ff9ebbPetr Cech * <http://www.apache.org/>.
b0e8c1802557645e2ff6a88c54c520b0f0ff9ebbPetr Cech *
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * Portions of this software are based upon public domain software
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek * originally written at the National Center for Supercomputing Applications,
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek * University of Illinois, Urbana-Champaign.
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina/*
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek * mod_mime_magic: MIME type lookup via file magic numbers
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek * Copyright (c) 1996-1997 Cisco Systems, Inc.
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek *
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * This software was submitted by Cisco Systems to the Apache Software Foundation in July
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * 1997. Future revisions and derivatives of this source code must
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * acknowledge Cisco Systems as the original contributor of this module.
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * All other licensing and usage conditions are those of the Apache Software Foundation.
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina *
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * Some of this code is derived from the free version of the file command
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * originally posted to comp.sources.unix. Copyright info for that program
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * is included below as required.
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * ---------------------------------------------------------------------------
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * - Copyright (c) Ian F. Darwin, 1987. Written by Ian F. Darwin.
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina *
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * This software is not subject to any license of the American Telephone and
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * Telegraph Company or of the Regents of the University of California.
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina *
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * Permission is granted to anyone to use this software for any purpose on any
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * computer system, and to alter it and redistribute it freely, subject to
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * the following restrictions:
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina *
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * 1. The author is not responsible for the consequences of use of this
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * software, no matter how awful, even if they arise from flaws in it.
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina *
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * 2. The origin of this software must not be misrepresented, either by
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * explicit claim or by omission. Since few users ever read sources, credits
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * must appear in the documentation.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * 3. Altered versions must be plainly marked as such, and must not be
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * misrepresented as being the original software. Since few users ever read
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * sources, credits must appear in the documentation.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina * 4. This notice may not be removed or altered.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * -------------------------------------------------------------------------
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech *
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech * For compliance with Mr Darwin's terms: this has been very significantly
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech * modified from the free "file" command.
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech * - all-in-one file for compilation convenience when moving from one
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech * version of Apache to the next.
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech * - Memory allocation is done through the Apache API's apr_pool_t structure.
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech * - All functions have had necessary Apache API request or server
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * structures passed to them where necessary to call other Apache API
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * routines. (i.e. usually for logging, files, or memory allocation in
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * itself or a called function.)
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * - struct magic has been converted from an array to a single-ended linked
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * list because it only grows one record at a time, it's only accessed
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * sequentially, and the Apache API has no equivalent of realloc().
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * - Functions have been changed to get their parameters from the server
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * configuration instead of globals. (It should be reentrant now but has
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * not been tested in a threaded environment.)
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * - Places where it used to print results to stdout now saves them in a
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * list where they're used to set the MIME type in the Apache request
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * record.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * - Command-line flags have been removed since they will never be used here.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * Ian Kluft <ikluft@cisco.com>
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * Engineering Information Framework
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * Central Engineering
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina * Cisco Systems, Inc.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * San Jose, CA, USA
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * Initial installation July/August 1996
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * Misc bug fixes May 1997
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * Submission to Apache Software Foundation July 1997
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina#include "apr_strings.h"
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina#include "ap_config.h"
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina#include "httpd.h"
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina#include "http_config.h"
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina#include "http_request.h"
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina#include "http_core.h"
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina#include "http_log.h"
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina#include "http_protocol.h"
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina#include "util_script.h"
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina#ifdef HAVE_UNISTD_H
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina#include <unistd.h>
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina#endif
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina#ifdef HAVE_UTIME_H
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina#include <utime.h>
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina#endif
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina/*
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina * data structures and related constants
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina#define MODNAME "mod_mime_magic"
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina#define MIME_MAGIC_DEBUG 0
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina#define MIME_BINARY_UNKNOWN "application/octet-stream"
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina#define MIME_TEXT_UNKNOWN "text/plain"
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina#define MAXMIMESTRING 256
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina/* HOWMANY must be at least 4096 to make gzip -dcq work */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina#define HOWMANY 4096
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina/* SMALL_HOWMANY limits how much work we do to figure out text files */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina#define SMALL_HOWMANY 1024
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina#define MAXDESC 50 /* max leng of text description */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina#define MAXstring 64 /* max leng of "string" types */
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březinastruct magic {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina struct magic *next; /* link to next entry */
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina int lineno; /* line number from magic file */
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina short flag;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina#define INDIR 1 /* if '>(...)' appears, */
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina#define UNSIGNED 2 /* comparison is unsigned */
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina short cont_level; /* level of ">" */
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina struct {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina char type; /* byte short long */
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina long offset; /* offset from indirection */
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina } in;
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech long offset; /* offset to magic number */
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech unsigned char reln; /* relation (0=eq, '>'=gt, etc) */
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech char type; /* int, short, long or string. */
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech char vallen; /* length of string value, if any */
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech#define BYTE 1
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech#define SHORT 2
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech#define LONG 4
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define STRING 5
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define DATE 6
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define BESHORT 7
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define BELONG 8
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define BEDATE 9
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define LESHORT 10
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define LELONG 11
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define LEDATE 12
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina union VALUETYPE {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina unsigned char b;
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech unsigned short h;
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech unsigned long l;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char s[MAXstring];
df9e9a1f9b7dc255eb62c390163c25917b08f5a2Lukas Slebodnik unsigned char hs[2]; /* 2 bytes of a fixed-endian "short" */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina unsigned char hl[4]; /* 2 bytes of a fixed-endian "long" */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina } value; /* either number or string */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina unsigned long mask; /* mask before comparison with value */
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech char nospflag; /* supress space character */
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech /* NOTE: this string is suspected of overrunning - find it! */
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech char desc[MAXDESC]; /* description */
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech};
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech/*
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech * data structures for tar file recognition
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech * --------------------------------------------------------------------------
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * Header file for public domain tar (tape archive) program.
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina *
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * @(#)tar.h 1.20 86/10/29 Public Domain. Created 25 August 1985 by John
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * Gilmore, ihnp4!hoptoad!gnu.
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina *
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * Header block on tape.
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina *
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * I'm going to use traditional DP naming conventions here. A "block" is a big
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * chunk of stuff that we do I/O on. A "record" is a piece of info that we
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * care about. Typically many "record"s fit into a "block".
6c2e507bd1571f9c7e26c5c9d60753b29fb75578Jakub Hrozek */
6c2e507bd1571f9c7e26c5c9d60753b29fb75578Jakub Hrozek#define RECORDSIZE 512
6c2e507bd1571f9c7e26c5c9d60753b29fb75578Jakub Hrozek#define NAMSIZ 100
6c2e507bd1571f9c7e26c5c9d60753b29fb75578Jakub Hrozek#define TUNMLEN 32
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define TGNMLEN 32
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinaunion record {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char charptr[RECORDSIZE];
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina struct header {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char name[NAMSIZ];
6c2e507bd1571f9c7e26c5c9d60753b29fb75578Jakub Hrozek char mode[8];
6c2e507bd1571f9c7e26c5c9d60753b29fb75578Jakub Hrozek char uid[8];
6c2e507bd1571f9c7e26c5c9d60753b29fb75578Jakub Hrozek char gid[8];
6c2e507bd1571f9c7e26c5c9d60753b29fb75578Jakub Hrozek char size[12];
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char mtime[12];
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char chksum[8];
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char linkflag;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char linkname[NAMSIZ];
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char magic[8];
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char uname[TUNMLEN];
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char gname[TGNMLEN];
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char devmajor[8];
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char devminor[8];
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina } header;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina};
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina/* The magic field is filled with this if uname and gname are valid. */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define TMAGIC "ustar " /* 7 chars and a null */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina/*
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * file-function prototypes
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic int ascmagic(request_rec *, unsigned char *, int);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic int is_tar(unsigned char *, int);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic int softmagic(request_rec *, unsigned char *, int);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic void tryit(request_rec *, unsigned char *, int, int);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic int zmagic(request_rec *, unsigned char *, int);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic int getvalue(server_rec *, struct magic *, char **);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic int hextoint(int);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic char *getstr(server_rec *, char *, char *, int, int *);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic int parse(server_rec *, apr_pool_t *p, char *, int);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic int match(request_rec *, unsigned char *, int);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic int mget(request_rec *, union VALUETYPE *, unsigned char *,
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina struct magic *, int);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic int mcheck(request_rec *, union VALUETYPE *, struct magic *);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic void mprint(request_rec *, union VALUETYPE *, struct magic *);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic int uncompress(request_rec *, int,
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina unsigned char **, int);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic long from_oct(int, char *);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic int fsmagic(request_rec *r, const char *fn);
df9e9a1f9b7dc255eb62c390163c25917b08f5a2Lukas Slebodnik
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina/*
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * includes for ASCII substring recognition formerly "names.h" in file
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * command
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina *
df9e9a1f9b7dc255eb62c390163c25917b08f5a2Lukas Slebodnik * Original notes: names and types used by ascmagic in file(1). These tokens are
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * here because they can appear anywhere in the first HOWMANY bytes, while
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * tokens in /etc/magic must appear at fixed offsets into the file. Don't
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * make HOWMANY too high unless you have a very fast CPU.
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina/* these types are used to index the apr_table_t 'types': keep em in sync! */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina/* HTML inserted in first because this is a web server module now */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define L_HTML 0 /* HTML */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define L_C 1 /* first and foremost on UNIX */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define L_FORT 2 /* the oldest one */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define L_MAKE 3 /* Makefiles */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define L_PLI 4 /* PL/1 */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define L_MACH 5 /* some kinda assembler */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define L_ENG 6 /* English */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define L_PAS 7 /* Pascal */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define L_MAIL 8 /* Electronic mail */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define L_NEWS 9 /* Usenet Netnews */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic char *types[] =
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina{
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "text/html", /* HTML */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "text/plain", /* "c program text", */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "text/plain", /* "fortran program text", */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "text/plain", /* "make commands text", */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "text/plain", /* "pl/1 program text", */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "text/plain", /* "assembler program text", */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "text/plain", /* "English text", */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "text/plain", /* "pascal program text", */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "message/rfc822", /* "mail text", */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "message/news", /* "news text", */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "application/binary", /* "can't happen error on names.h/types", */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina 0
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina};
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic struct names {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char *name;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina short type;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina} names[] = {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* These must be sorted by eye for optimal hit rate */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* Add to this list only after substantial meditation */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "<html>", L_HTML
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "<HTML>", L_HTML
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "<head>", L_HTML
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "<HEAD>", L_HTML
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "<title>", L_HTML
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "<TITLE>", L_HTML
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "<h1>", L_HTML
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina {
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech "<H1>", L_HTML
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina {
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech "<!--", L_HTML
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech },
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech {
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech "<!DOCTYPE HTML", L_HTML
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "/*", L_C
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }, /* must precede "The", "the", etc. */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "#include", L_C
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech },
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech {
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech "char", L_C
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech },
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech {
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech "The", L_ENG
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "the", L_ENG
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "double", L_C
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "extern", L_C
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "float", L_C
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "real", L_C
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "struct", L_C
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "union", L_C
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "CFLAGS", L_MAKE
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "LDFLAGS", L_MAKE
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "all:", L_MAKE
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek ".PRECIOUS", L_MAKE
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina /*
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek * Too many files of text have these words in them. Find another way to
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * recognize Fortrash.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina#ifdef NOTDEF
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "subroutine", L_FORT
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "function", L_FORT
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "block", L_FORT
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "common", L_FORT
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "dimension", L_FORT
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "integer", L_FORT
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "data", L_FORT
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek#endif /* NOTDEF */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina ".ascii", L_MACH
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina ".asciiz", L_MACH
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina ".byte", L_MACH
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina ".even", L_MACH
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina ".globl", L_MACH
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "clr", L_MACH
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "(input,", L_PAS
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "dcl", L_PLI
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "Received:", L_MAIL
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina ">From", L_MAIL
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina "Return-Path:", L_MAIL
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina "Cc:", L_MAIL
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "Newsgroups:", L_NEWS
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "Path:", L_NEWS
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "Organization:", L_NEWS
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina },
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina NULL, 0
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina }
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina};
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina#define NNAMES ((sizeof(names)/sizeof(struct names)) - 1)
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina/*
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina * Result String List (RSL)
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina *
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina * The file(1) command prints its output. Instead, we store the various
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina * "printed" strings in a list (allocating memory as we go) and concatenate
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina * them at the end when we finally know how much space they'll need.
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březinatypedef struct magic_rsl_s {
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina char *str; /* string, possibly a fragment */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina struct magic_rsl_s *next; /* pointer to next fragment */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina} magic_rsl;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina/*
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina * Apache module configuration structures
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina/* per-server info */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březinatypedef struct {
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina const char *magicfile; /* where magic be found */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina struct magic *magic; /* head of magic config list */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina struct magic *last;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina} magic_server_config_rec;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina/* per-request info */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březinatypedef struct {
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina magic_rsl *head; /* result string list */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina magic_rsl *tail;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina unsigned suf_recursion; /* recursion depth in suffix check */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina} magic_req_rec;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina/*
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * configuration functions - called by Apache API routines
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březinamodule mime_magic_module;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březinastatic void *create_magic_server_config(apr_pool_t *p, server_rec *d)
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina{
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina /* allocate the config - use pcalloc because it needs to be zeroed */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina return apr_pcalloc(p, sizeof(magic_server_config_rec));
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina}
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březinastatic void *merge_magic_server_config(apr_pool_t *p, void *basev, void *addv)
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina{
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina magic_server_config_rec *base = (magic_server_config_rec *) basev;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina magic_server_config_rec *add = (magic_server_config_rec *) addv;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina magic_server_config_rec *new = (magic_server_config_rec *)
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina apr_palloc(p, sizeof(magic_server_config_rec));
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina new->magicfile = add->magicfile ? add->magicfile : base->magicfile;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina new->magic = NULL;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina new->last = NULL;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina return new;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina}
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březinastatic const char *set_magicfile(cmd_parms *cmd, void *dummy, const char *arg)
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina{
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina magic_server_config_rec *conf = (magic_server_config_rec *)
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina ap_get_module_config(cmd->server->module_config,
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina &mime_magic_module);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina if (!conf) {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina return MODNAME ": server structure not allocated";
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina conf->magicfile = arg;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina return NULL;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina}
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina/*
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * configuration file commands - exported to Apache API
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březinastatic const command_rec mime_magic_cmds[] =
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina{
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina AP_INIT_TAKE1("MimeMagicFile", set_magicfile, NULL, RSRC_CONF,
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina "Path to MIME Magic file (in file(1) format)"),
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina {NULL}
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina};
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina/*
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * RSL (result string list) processing routines
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * These collect strings that would have been printed in fragments by file(1)
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * into a list of magic_rsl structures with the strings. When complete,
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * they're concatenated together to become the MIME content and encoding
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * types.
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina *
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * return value conventions for these functions: functions which return int:
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina * failure = -1, other = result functions which return pointers: failure = 0,
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * other = result
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina/* allocate a per-request structure and put it in the request record */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březinastatic magic_req_rec *magic_set_config(request_rec *r)
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina{
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina magic_req_rec *req_dat = (magic_req_rec *) apr_palloc(r->pool,
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina sizeof(magic_req_rec));
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina req_dat->head = req_dat->tail = (magic_rsl *) NULL;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina ap_set_module_config(r->request_config, &mime_magic_module, req_dat);
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina return req_dat;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina}
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina/* add a string to the result string list for this request */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina/* it is the responsibility of the caller to allocate "str" */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březinastatic int magic_rsl_add(request_rec *r, char *str)
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina{
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina magic_req_rec *req_dat = (magic_req_rec *)
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina ap_get_module_config(r->request_config, &mime_magic_module);
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina magic_rsl *rsl;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina /* make sure we have a list to put it in */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina if (!req_dat) {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, APR_EINVAL, r,
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina MODNAME ": request config should not be NULL");
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina if (!(req_dat = magic_set_config(r))) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* failure */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina return -1;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina /* allocate the list entry */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina rsl = (magic_rsl *) apr_palloc(r->pool, sizeof(magic_rsl));
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina /* fill it */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina rsl->str = str;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina rsl->next = (magic_rsl *) NULL;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina /* append to the list */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina if (req_dat->head && req_dat->tail) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina req_dat->tail->next = rsl;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina req_dat->tail = rsl;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina }
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina else {
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech req_dat->head = req_dat->tail = rsl;
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech }
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* success */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina return 0;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina}
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina/* RSL hook for puts-type functions */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březinastatic int magic_rsl_puts(request_rec *r, char *str)
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina{
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina return magic_rsl_add(r, str);
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina}
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina/* RSL hook for printf-type functions */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březinastatic int magic_rsl_printf(request_rec *r, char *str,...)
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina{
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina va_list ap;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina char buf[MAXMIMESTRING];
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* assemble the string into the buffer */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina va_start(ap, str);
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina apr_vsnprintf(buf, sizeof(buf), str, ap);
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina va_end(ap);
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* add the buffer to the list */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina return magic_rsl_add(r, strdup(buf));
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina}
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina/* RSL hook for putchar-type functions */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březinastatic int magic_rsl_putchar(request_rec *r, char c)
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina{
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina char str[2];
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* high overhead for 1 char - just hope they don't do this much */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina str[0] = c;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina str[1] = '\0';
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina return magic_rsl_add(r, str);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina}
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina/* allocate and copy a contiguous string from a result string list */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březinastatic char *rsl_strdup(request_rec *r, int start_frag, int start_pos, int len)
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina{
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char *result; /* return value */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina int cur_frag, /* current fragment number/counter */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina cur_pos, /* current position within fragment */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina res_pos; /* position in result string */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina magic_rsl *frag; /* list-traversal pointer */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina magic_req_rec *req_dat = (magic_req_rec *)
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina ap_get_module_config(r->request_config, &mime_magic_module);
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* allocate the result string */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina result = (char *) apr_palloc(r->pool, len + 1);
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* loop through and collect the string */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina res_pos = 0;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina for (frag = req_dat->head, cur_frag = 0;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina frag->next;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina frag = frag->next, cur_frag++) {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* loop to the first fragment */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina if (cur_frag < start_frag)
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina continue;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* loop through and collect chars */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina for (cur_pos = (cur_frag == start_frag) ? start_pos : 0;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina frag->str[cur_pos];
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina cur_pos++) {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina if (cur_frag >= start_frag
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina && cur_pos >= start_pos
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina && res_pos <= len) {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina result[res_pos++] = frag->str[cur_pos];
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina if (res_pos > len) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina break;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina }
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* clean up and return */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina result[res_pos] = 0;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina#if MIME_MAGIC_DEBUG
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina MODNAME ": rsl_strdup() %d chars: %s", res_pos - 1, result);
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina#endif
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina return result;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina}
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina/* states for the state-machine algorithm in magic_rsl_to_request() */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinatypedef enum {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina rsl_leading_space, rsl_type, rsl_subtype, rsl_separator, rsl_encoding
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina} rsl_states;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina/* process the RSL and set the MIME info in the request record */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březinastatic int magic_rsl_to_request(request_rec *r)
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina{
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina int cur_frag, /* current fragment number/counter */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina cur_pos, /* current position within fragment */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina type_frag, /* content type starting point: fragment */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina type_pos, /* content type starting point: position */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina type_len, /* content type length */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina encoding_frag, /* content encoding starting point: fragment */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina encoding_pos, /* content encoding starting point: position */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina encoding_len; /* content encoding length */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina magic_rsl *frag; /* list-traversal pointer */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina rsl_states state;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina magic_req_rec *req_dat = (magic_req_rec *)
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina ap_get_module_config(r->request_config, &mime_magic_module);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* check if we have a result */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina if (!req_dat || !req_dat->head) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* empty - no match, we defer to other Apache modules */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina return DECLINED;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina }
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* start searching for the type and encoding */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina state = rsl_leading_space;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina type_frag = type_pos = type_len = 0;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina encoding_frag = encoding_pos = encoding_len = 0;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina for (frag = req_dat->head, cur_frag = 0;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina frag && frag->next;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina frag = frag->next, cur_frag++) {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* loop through the characters in the fragment */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina for (cur_pos = 0; frag->str[cur_pos]; cur_pos++) {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina if (apr_isspace(frag->str[cur_pos])) {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* process whitespace actions for each state */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina if (state == rsl_leading_space) {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* eat whitespace in this state */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina continue;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina else if (state == rsl_type) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* whitespace: type has no slash! */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina return DECLINED;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina else if (state == rsl_subtype) {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* whitespace: end of MIME type */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina state++;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina continue;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina else if (state == rsl_separator) {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* eat whitespace in this state */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina continue;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina }
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina else if (state == rsl_encoding) {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* whitespace: end of MIME encoding */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* we're done */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina frag = req_dat->tail;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina break;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina else {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* should not be possible */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* abandon malfunctioning module */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech MODNAME ": bad state %d (ws)", state);
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech return DECLINED;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* NOTREACHED */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina else if (state == rsl_type &&
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina frag->str[cur_pos] == '/') {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* copy the char and go to rsl_subtype state */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina type_len++;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina state++;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina else {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* process non-space actions for each state */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina if (state == rsl_leading_space) {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* non-space: begin MIME type */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina state++;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina type_frag = cur_frag;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina type_pos = cur_pos;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina type_len = 1;
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina continue;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina else if (state == rsl_type ||
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina state == rsl_subtype) {
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina /* non-space: adds to type */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina type_len++;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina continue;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina else if (state == rsl_separator) {
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /* non-space: begin MIME encoding */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina state++;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina encoding_frag = cur_frag;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina encoding_pos = cur_pos;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina encoding_len = 1;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina continue;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina else if (state == rsl_encoding) {
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /* non-space: adds to encoding */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina encoding_len++;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina continue;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina else {
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /* should not be possible */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /* abandon malfunctioning module */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina MODNAME ": bad state %d (ns)", state);
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina return DECLINED;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /* NOTREACHED */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /* NOTREACHED */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /* if we ended prior to state rsl_subtype, we had incomplete info */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina if (state != rsl_subtype && state != rsl_separator &&
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina state != rsl_encoding) {
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /* defer to other modules */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina return DECLINED;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* save the info in the request record */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina if (state == rsl_subtype || state == rsl_encoding ||
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina state == rsl_encoding) {
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina char *tmp;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina tmp = rsl_strdup(r, type_frag, type_pos, type_len);
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /* XXX: this could be done at config time I'm sure... but I'm
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina * confused by all this magic_rsl stuff. -djg */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina ap_content_type_tolower(tmp);
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina r->content_type = tmp;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina if (state == rsl_encoding) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char *tmp;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina tmp = rsl_strdup(r, encoding_frag,
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina encoding_pos, encoding_len);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* XXX: this could be done at config time I'm sure... but I'm
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * confused by all this magic_rsl stuff. -djg */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina ap_str_tolower(tmp);
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina r->content_encoding = tmp;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /* detect memory allocation errors */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina if (!r->content_type ||
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina (state == rsl_encoding && !r->content_encoding)) {
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina return HTTP_INTERNAL_SERVER_ERROR;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /* success! */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina return OK;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina}
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina/*
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina * magic_process - process input file r Apache API request record
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * (formerly called "process" in file command, prefix added for clarity) Opens
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * the file and reads a fixed-size buffer to begin processing the contents.
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic int magic_process(request_rec *r)
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina{
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina apr_file_t *fd = NULL;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina unsigned char buf[HOWMANY + 1]; /* one extra for terminating '\0' */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina int nbytes = 0; /* number of bytes read from a datafile */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina int result;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /*
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina * first try judging the file based on its filesystem status
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina switch ((result = fsmagic(r, r->filename))) {
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina case DONE:
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina magic_rsl_putchar(r, '\n');
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina return OK;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina case OK:
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina break;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina default:
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* fatal error, bail out */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina return result;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina if (apr_open(&fd, r->filename, APR_READ, APR_OS_DEFAULT, r->pool) != APR_SUCCESS) {
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /* We can't open it, but we were able to stat it. */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina MODNAME ": can't read `%s'", r->filename);
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /* let some other handler decide what the problem is */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina return DECLINED;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /*
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * try looking at the first HOWMANY bytes
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina nbytes = sizeof(buf) - 1;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina if ((result = apr_read(fd, (char *) buf, &nbytes)) != APR_SUCCESS) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina ap_log_rerror(APLOG_MARK, APLOG_ERR, result, r,
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina MODNAME ": read failed: %s", r->filename);
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina return HTTP_INTERNAL_SERVER_ERROR;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina if (nbytes == 0)
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina magic_rsl_puts(r, MIME_TEXT_UNKNOWN);
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina else {
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina buf[nbytes++] = '\0'; /* null-terminate it */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina tryit(r, buf, nbytes, 1);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina (void) apr_close(fd);
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina (void) magic_rsl_putchar(r, '\n');
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech return OK;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina}
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březinastatic void tryit(request_rec *r, unsigned char *buf, int nb, int checkzmagic)
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina{
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /*
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina * Try compression stuff
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina if (checkzmagic == 1) {
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina if (zmagic(r, buf, nb) == 1)
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina return;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /*
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina * try tests in /etc/magic (or surrogate magic file)
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina */
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina if (softmagic(r, buf, nb) == 1)
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina return;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina /*
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina * try known keywords, check for ascii-ness too.
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina if (ascmagic(r, buf, nb) == 1)
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina return;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina /*
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina * abandon hope, all ye who remain here
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina}
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina#define EATAB {while (apr_isspace((unsigned char) *l)) ++l;}
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina/*
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina * apprentice - load configuration from the magic file r
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina * API request record
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březinastatic int apprentice(server_rec *s, apr_pool_t *p)
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina{
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina apr_file_t *f = NULL;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina apr_status_t result;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina char line[BUFSIZ + 1];
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina int errs = 0;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina int lineno;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina#if MIME_MAGIC_DEBUG
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina int rule = 0;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina struct magic *m, *prevm;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina#endif
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina magic_server_config_rec *conf = (magic_server_config_rec *)
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina ap_get_module_config(s->module_config, &mime_magic_module);
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina const char *fname = ap_server_root_relative(p, conf->magicfile);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina result = apr_open(&f, fname, APR_READ | APR_BUFFERED, APR_OS_DEFAULT, p);
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina if (result != APR_SUCCESS) {
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina ap_log_error(APLOG_MARK, APLOG_ERR, result, s,
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina MODNAME ": can't read magic file %s", fname);
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina return -1;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina /* set up the magic list (empty) */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina conf->magic = conf->last = NULL;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina /* parse it */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina for (lineno = 1; apr_fgets(line, BUFSIZ, f) == APR_SUCCESS; lineno++) {
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina int ws_offset;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina /* delete newline */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina if (line[0]) {
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina line[strlen(line) - 1] = '\0';
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina }
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina /* skip leading whitespace */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina ws_offset = 0;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina while (line[ws_offset] && apr_isspace(line[ws_offset])) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina ws_offset++;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina }
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina /* skip blank lines */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina if (line[ws_offset] == 0) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina continue;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina }
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina /* comment, do not parse */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina if (line[ws_offset] == '#')
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina continue;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina#if MIME_MAGIC_DEBUG
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina /* if we get here, we're going to use it so count it */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina rule++;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#endif
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina /* parse it */
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina if (parse(s, p, line + ws_offset, lineno) != 0)
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina ++errs;
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina }
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina (void) apr_close(f);
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina#if MIME_MAGIC_DEBUG
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, s,
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina MODNAME ": apprentice conf=%x file=%s m=%s m->next=%s last=%s",
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina conf,
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina conf->magicfile ? conf->magicfile : "NULL",
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina conf->magic ? "set" : "NULL",
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina (conf->magic && conf->magic->next) ? "set" : "NULL",
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina conf->last ? "set" : "NULL");
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, s,
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina MODNAME ": apprentice read %d lines, %d rules, %d errors",
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina lineno, rule, errs);
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina#endif
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina#if MIME_MAGIC_DEBUG
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina prevm = 0;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, s,
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina MODNAME ": apprentice test");
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina for (m = conf->magic; m; m = m->next) {
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina if (apr_isprint((((unsigned long) m) >> 24) & 255) &&
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina apr_isprint((((unsigned long) m) >> 16) & 255) &&
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina apr_isprint((((unsigned long) m) >> 8) & 255) &&
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina apr_isprint(((unsigned long) m) & 255)) {
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, s,
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina MODNAME ": apprentice: POINTER CLOBBERED! "
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina "m=\"%c%c%c%c\" line=%d",
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina (((unsigned long) m) >> 24) & 255,
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina (((unsigned long) m) >> 16) & 255,
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina (((unsigned long) m) >> 8) & 255,
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina ((unsigned long) m) & 255,
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina prevm ? prevm->lineno : -1);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina break;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina prevm = m;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina }
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina#endif
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina return (errs ? -1 : 0);
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina}
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina/*
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina * extend the sign bit if the comparison is to be signed
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březinastatic unsigned long signextend(server_rec *s, struct magic *m, unsigned long v)
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina{
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina if (!(m->flag & UNSIGNED))
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina switch (m->type) {
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina /*
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina * Do not remove the casts below. They are vital. When later
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina * compared with the data, the sign extension must have happened.
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina case BYTE:
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina v = (char) v;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina break;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina case SHORT:
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina case BESHORT:
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina case LESHORT:
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina v = (short) v;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina break;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina case DATE:
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina case BEDATE:
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina case LEDATE:
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina case LONG:
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina case BELONG:
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina case LELONG:
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina v = (long) v;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina break;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina case STRING:
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina break;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina default:
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, s,
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina MODNAME ": can't happen: m->type=%d", m->type);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina return -1;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina }
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina return v;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina}
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina/*
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina * parse one line from magic file, put into magic[index++] if valid
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březinastatic int parse(server_rec *serv, apr_pool_t *p, char *l, int lineno)
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina{
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina struct magic *m;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina char *t, *s;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina magic_server_config_rec *conf = (magic_server_config_rec *)
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina ap_get_module_config(serv->module_config, &mime_magic_module);
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina /* allocate magic structure entry */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina m = (struct magic *) apr_pcalloc(p, sizeof(struct magic));
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina /* append to linked list */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina m->next = NULL;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina if (!conf->magic || !conf->last) {
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina conf->magic = conf->last = m;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina }
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina else {
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina conf->last->next = m;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina conf->last = m;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina /* set values in magic structure */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina m->flag = 0;
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech m->cont_level = 0;
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech m->lineno = lineno;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina while (*l == '>') {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina ++l; /* step over */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina m->cont_level++;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina if (m->cont_level != 0 && *l == '(') {
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina ++l; /* step over */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina m->flag |= INDIR;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina }
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina /* get offset, then skip over it */
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina m->offset = (int) strtol(l, &t, 0);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina if (l == t) {
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, serv,
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina MODNAME ": offset %s invalid", l);
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina l = t;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina if (m->flag & INDIR) {
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina m->in.type = LONG;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina m->in.offset = 0;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina /*
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina * read [.lbs][+-]nnnnn)
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina */
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina if (*l == '.') {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina switch (*++l) {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina case 'l':
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->in.type = LONG;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina break;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina case 's':
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->in.type = SHORT;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina break;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina case 'b':
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->in.type = BYTE;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina break;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina default:
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, serv,
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina MODNAME ": indirect offset type %c invalid", *l);
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina break;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina l++;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina s = l;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina if (*l == '+' || *l == '-')
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina l++;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina if (apr_isdigit((unsigned char) *l)) {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->in.offset = strtol(l, &t, 0);
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina if (*s == '-')
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->in.offset = -m->in.offset;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina else
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina t = l;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina if (*t++ != ')') {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, serv,
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina MODNAME ": missing ')' in indirect offset");
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina l = t;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina while (apr_isdigit((unsigned char) *l))
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina ++l;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina EATAB;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina#define NBYTE 4
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define NSHORT 5
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define NLONG 4
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina#define NSTRING 6
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define NDATE 4
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define NBESHORT 7
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina#define NBELONG 6
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina#define NBEDATE 6
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina#define NLESHORT 7
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina#define NLELONG 6
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina#define NLEDATE 6
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina if (*l == 'u') {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina ++l;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->flag |= UNSIGNED;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina /* get type, skip it */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina if (strncmp(l, "byte", NBYTE) == 0) {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->type = BYTE;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina l += NBYTE;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina else if (strncmp(l, "short", NSHORT) == 0) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina m->type = SHORT;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina l += NSHORT;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina else if (strncmp(l, "long", NLONG) == 0) {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->type = LONG;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina l += NLONG;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina else if (strncmp(l, "string", NSTRING) == 0) {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->type = STRING;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina l += NSTRING;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina else if (strncmp(l, "date", NDATE) == 0) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina m->type = DATE;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina l += NDATE;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina else if (strncmp(l, "beshort", NBESHORT) == 0) {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->type = BESHORT;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina l += NBESHORT;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina else if (strncmp(l, "belong", NBELONG) == 0) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina m->type = BELONG;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina l += NBELONG;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina else if (strncmp(l, "bedate", NBEDATE) == 0) {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->type = BEDATE;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina l += NBEDATE;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina else if (strncmp(l, "leshort", NLESHORT) == 0) {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->type = LESHORT;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina l += NLESHORT;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina else if (strncmp(l, "lelong", NLELONG) == 0) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina m->type = LELONG;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina l += NLELONG;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina else if (strncmp(l, "ledate", NLEDATE) == 0) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina m->type = LEDATE;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina l += NLEDATE;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina else {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, serv,
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina MODNAME ": type %s invalid", l);
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina return -1;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina if (*l == '&') {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina ++l;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina m->mask = signextend(serv, m, strtol(l, &l, 0));
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina else
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->mask = ~0L;
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech EATAB;
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina switch (*l) {
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina case '>':
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina case '<':
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina /* Old-style anding: "0 byte &0x80 dynamically linked" */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina case '&':
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina case '^':
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina case '=':
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->reln = *l;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina ++l;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina break;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina case '!':
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina if (m->type != STRING) {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->reln = *l;
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina ++l;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina break;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina }
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina /* FALL THROUGH */
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina default:
3688374991afb34bbaf2b7843683fc13dd77879dPavel Březina if (*l == 'x' && apr_isspace((unsigned char) l[1])) {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina m->reln = *l;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina ++l;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina goto GetDesc; /* Bill The Cat */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek m->reln = '=';
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek break;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek EATAB;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if (getvalue(serv, m, &l))
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return -1;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /*
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * now get last part - the description
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek GetDesc:
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek EATAB;
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech if (l[0] == '\b') {
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech ++l;
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech m->nospflag = 1;
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech }
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech else if ((l[0] == '\\') && (l[1] == 'b')) {
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech ++l;
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech ++l;
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech m->nospflag = 1;
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech }
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech else
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech m->nospflag = 0;
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech strncpy(m->desc, l, sizeof(m->desc) - 1);
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech m->desc[sizeof(m->desc) - 1] = '\0';
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech#if MIME_MAGIC_DEBUG
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, serv,
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech MODNAME ": parse line=%d m=%x next=%x cont=%d desc=%s",
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech lineno, m, m->next, m->cont_level, m->desc);
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech#endif /* MIME_MAGIC_DEBUG */
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech return 0;
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech}
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech/*
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech * Read a numeric value from a pointer, into the value union of a magic
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech * pointer, according to the magic type. Update the string pointer to point
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech * just after the number read. Return 0 for success, non-zero for failure.
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech */
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cechstatic int getvalue(server_rec *s, struct magic *m, char **p)
b0e8c1802557645e2ff6a88c54c520b0f0ff9ebbPetr Cech{
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech int slen;
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech if (m->type == STRING) {
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech *p = getstr(s, *p, m->value.s, sizeof(m->value.s), &slen);
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech m->vallen = slen;
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech }
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech else if (m->reln != 'x')
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech m->value.l = signextend(s, m, strtol(*p, p, 0));
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech return 0;
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech}
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech/*
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech * Convert a string containing C character escapes. Stop at an unescaped
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech * space or tab. Copy the converted version to "p", returning its length in
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech * *slen. Return updated scan pointer as function result.
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech */
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cechstatic char *getstr(server_rec *serv, register char *s, register char *p,
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech int plen, int *slen)
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech{
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech char *origs = s, *origp = p;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech char *pmax = p + plen - 1;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech register int c;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech register int val;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech while ((c = *s++) != '\0') {
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech if (apr_isspace((unsigned char) c))
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech break;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech if (p >= pmax) {
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, serv,
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech MODNAME ": string too long: %s", origs);
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech break;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech }
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech if (c == '\\') {
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech switch (c = *s++) {
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case '\0':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech goto out;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech default:
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech *p++ = (char) c;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech break;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case 'n':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech *p++ = '\n';
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech break;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case 'r':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech *p++ = '\r';
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech break;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case 'b':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech *p++ = '\b';
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech break;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case 't':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech *p++ = '\t';
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech break;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case 'f':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech *p++ = '\f';
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech break;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case 'v':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech *p++ = '\v';
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech break;
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech /* \ and up to 3 octal digits */
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case '0':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case '1':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case '2':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case '3':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case '4':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case '5':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case '6':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech case '7':
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech val = c - '0';
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech c = *s++; /* try for 2 */
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech if (c >= '0' && c <= '7') {
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech val = (val << 3) | (c - '0');
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek c = *s++; /* try for 3 */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if (c >= '0' && c <= '7')
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek val = (val << 3) | (c - '0');
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek else
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek --s;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek else
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek --s;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek *p++ = (char) val;
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech break;
c4d4fe1603420fe8f3d256a3a446974699563ff3Petr Cech
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /* \x and up to 3 hex digits */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek case 'x':
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek val = 'x'; /* Default if no digits */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek c = hextoint(*s++); /* Get next char */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if (c >= 0) {
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek val = c;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek c = hextoint(*s++);
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if (c >= 0) {
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek val = (val << 4) + c;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek c = hextoint(*s++);
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if (c >= 0) {
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek val = (val << 4) + c;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek else
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek --s;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek else
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek --s;
b0e8c1802557645e2ff6a88c54c520b0f0ff9ebbPetr Cech }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek else
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek --s;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek *p++ = (char) val;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek break;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek else
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek *p++ = (char) c;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek out:
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek *p = '\0';
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek *slen = p - origp;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return s;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek}
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek/* Single hex char to int; -1 if not a hex char. */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozekstatic int hextoint(int c)
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek{
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if (apr_isdigit((unsigned char) c))
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return c - '0';
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if ((c >= 'a') && (c <= 'f'))
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return c + 10 - 'a';
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if ((c >= 'A') && (c <= 'F'))
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return c + 10 - 'A';
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return -1;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek}
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek/*
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * return DONE to indicate it's been handled
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * return OK to indicate it's a regular file still needing handling
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * other returns indicate a failure of some sort
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozekstatic int fsmagic(request_rec *r, const char *fn)
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek{
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek switch (r->finfo.filetype) {
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek case APR_DIR:
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek magic_rsl_puts(r, DIR_MAGIC_TYPE);
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return DONE;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek case APR_CHR:
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /*
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * (void) magic_rsl_printf(r,"character special (%d/%d)",
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * major(sb->st_rdev), minor(sb->st_rdev));
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return DONE;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek case APR_BLK:
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /*
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * (void) magic_rsl_printf(r,"block special (%d/%d)",
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * major(sb->st_rdev), minor(sb->st_rdev));
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return DONE;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /* TODO add code to handle V7 MUX and Blit MUX files */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek case APR_PIPE:
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /*
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * magic_rsl_puts(r,"fifo (named pipe)");
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return DONE;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek case APR_LNK:
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /* We used stat(), the only possible reason for this is that the
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * symlink is broken.
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek MODNAME ": broken symlink (%s)", fn);
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return HTTP_INTERNAL_SERVER_ERROR;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek case APR_SOCK:
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return DONE;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek case APR_REG:
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek break;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek default:
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek MODNAME ": invalid mode 0%o.", (unsigned int)r->finfo.protection);
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return HTTP_INTERNAL_SERVER_ERROR;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /*
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * regular file, check next possibility
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if (r->finfo.size == 0) {
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek magic_rsl_puts(r, MIME_TEXT_UNKNOWN);
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return DONE;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek return OK;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek}
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek/*
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * softmagic - lookup one file in database (already read from /etc/magic by
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * apprentice.c). Passed the name and FILE * of one file to be typed.
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech */
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech /* ARGSUSED1 *//* nbytes passed for regularity, maybe need later */
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cechstatic int softmagic(request_rec *r, unsigned char *buf, int nbytes)
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech{
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech if (match(r, buf, nbytes))
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech return 1;
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech return 0;
5928fcbb57b92bfd18ad15aaaf4a5e1ab8dabe61Petr Cech}
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech/*
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * Go through the whole list, stopping if you find a match. Process all the
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * continuations of that match before returning.
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech *
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * We support multi-level continuations:
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech *
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * At any time when processing a successful top-level match, there is a current
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * continuation level; it represents the level of the last successfully
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * matched continuation.
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech *
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * Continuations above that level are skipped as, if we see one, it means that
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * the continuation that controls them - i.e, the lower-level continuation
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * preceding them - failed to match.
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech *
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * Continuations below that level are processed as, if we see one, it means
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * we've finished processing or skipping higher-level continuations under the
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * control of a successful or unsuccessful lower-level continuation, and are
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * now seeing the next lower-level continuation and should process it. The
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * current continuation level reverts to the level of the one we're seeing.
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech *
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * Continuations at the current level are processed as, if we see one, there's
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * no lower-level continuation that may have failed.
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech *
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * If a continuation matches, we bump the current continuation level so that
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech * higher-level continuations are processed.
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech */
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cechstatic int match(request_rec *r, unsigned char *s, int nbytes)
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech{
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech#if MIME_MAGIC_DEBUG
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech int rule_counter = 0;
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech#endif
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech int cont_level = 0;
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech int need_separator = 0;
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech union VALUETYPE p;
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech magic_server_config_rec *conf = (magic_server_config_rec *)
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech ap_get_module_config(r->server->module_config, &mime_magic_module);
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech struct magic *m;
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech#if MIME_MAGIC_DEBUG
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech MODNAME ": match conf=%x file=%s m=%s m->next=%s last=%s",
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech conf,
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech conf->magicfile ? conf->magicfile : "NULL",
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech conf->magic ? "set" : "NULL",
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech (conf->magic && conf->magic->next) ? "set" : "NULL",
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech conf->last ? "set" : "NULL");
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech#endif
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech#if MIME_MAGIC_DEBUG
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech for (m = conf->magic; m; m = m->next) {
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech if (apr_isprint((((unsigned long) m) >> 24) & 255) &&
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech apr_isprint((((unsigned long) m) >> 16) & 255) &&
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech apr_isprint((((unsigned long) m) >> 8) & 255) &&
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech apr_isprint(((unsigned long) m) & 255)) {
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech MODNAME ": match: POINTER CLOBBERED! "
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech "m=\"%c%c%c%c\"",
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech (((unsigned long) m) >> 24) & 255,
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech (((unsigned long) m) >> 16) & 255,
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech (((unsigned long) m) >> 8) & 255,
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech ((unsigned long) m) & 255);
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech break;
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech }
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech }
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech#endif
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech for (m = conf->magic; m; m = m->next) {
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech#if MIME_MAGIC_DEBUG
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech rule_counter++;
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech MODNAME ": line=%d desc=%s", m->lineno, m->desc);
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech#endif
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech /* check if main entry matches */
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech if (!mget(r, &p, s, m, nbytes) ||
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech !mcheck(r, &p, m)) {
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech struct magic *m_cont;
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech /*
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech * main entry didn't match, flush its continuations
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech */
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech if (!m->next || (m->next->cont_level == 0)) {
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech continue;
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech }
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech m_cont = m->next;
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech while (m_cont && (m_cont->cont_level != 0)) {
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech#if MIME_MAGIC_DEBUG
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech rule_counter++;
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech MODNAME ": line=%d mc=%x mc->next=%x cont=%d desc=%s",
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech m_cont->lineno, m_cont,
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech m_cont->next, m_cont->cont_level,
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech m_cont->desc);
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech#endif
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech /*
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech * this trick allows us to keep *m in sync when the continue
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * advances the pointer
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek m = m_cont;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek m_cont = m_cont->next;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek continue;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /* if we get here, the main entry rule was a match */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /* this will be the last run through the loop */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek#if MIME_MAGIC_DEBUG
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek MODNAME ": rule matched, line=%d type=%d %s",
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek m->lineno, m->type,
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek (m->type == STRING) ? m->value.s : "");
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek#endif
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /* print the match */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek mprint(r, &p, m);
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /*
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * If we printed something, we'll need to print a blank before we
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * print something else.
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if (m->desc[0])
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek need_separator = 1;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /* and any continuations that match */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek cont_level++;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /*
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * while (m && m->next && m->next->cont_level != 0 && ( m = m->next
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * ))
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek m = m->next;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek while (m && (m->cont_level != 0)) {
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek#if MIME_MAGIC_DEBUG
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek MODNAME ": match line=%d cont=%d type=%d %s",
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek m->lineno, m->cont_level, m->type,
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek (m->type == STRING) ? m->value.s : "");
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek#endif
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if (cont_level >= m->cont_level) {
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if (cont_level > m->cont_level) {
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /*
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * We're at the end of the level "cont_level"
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * continuations.
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek cont_level = m->cont_level;
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if (mget(r, &p, s, m, nbytes) &&
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek mcheck(r, &p, m)) {
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /*
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * This continuation matched. Print its message, with a
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * blank before it if the previous item printed and this
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek * item isn't empty.
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /* space if previous printed */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if (need_separator
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek && (m->nospflag == 0)
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek && (m->desc[0] != '\0')
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek ) {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina (void) magic_rsl_putchar(r, ' ');
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina need_separator = 0;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina }
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina mprint(r, &p, m);
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina if (m->desc[0])
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina need_separator = 1;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina /*
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * If we see any continuations at a higher level, process
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * them.
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek */
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina cont_level++;
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina }
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina }
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina /* move to next continuation record */
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina m = m->next;
54ed1b1214dbf9da1f481e8d193c81ce4312516bPavel Březina }
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina#if MIME_MAGIC_DEBUG
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina MODNAME ": matched after %d rules", rule_counter);
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina#endif
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina return 1; /* all through */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina }
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina#if MIME_MAGIC_DEBUG
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina MODNAME ": failed after %d rules", rule_counter);
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina#endif
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina return 0; /* no match at all */
28ebfa4373d1e7ce45b5d70a3619df1c074a661ePavel Březina}
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březinastatic void mprint(request_rec *r, union VALUETYPE *p, struct magic *m)
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina{
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina char *pp, *rt;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina unsigned long v;
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina
3a5ea81007bd38ce511c37f65cc45d4b6b95ec44Pavel Březina switch (m->type) {
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina case BYTE:
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina v = p->b;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina break;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina case SHORT:
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina case BESHORT:
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina case LESHORT:
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina v = p->h;
641d684ee88c6540a4cf1d74d258614f615699fePavel Březina break;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina
e87b2a6e94c1066b3044fe683825ff5b4f8716c2Pavel Březina case LONG:
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina case BELONG:
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina case LELONG:
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina v = p->l;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina break;
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina case STRING:
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina if (m->reln == '=') {
71965bb18407ff45ada9e47cb6def086e48663c6Pavel Březina (void) magic_rsl_printf(r, m->desc, m->value.s);
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek }
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek else {
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech (void) magic_rsl_printf(r, m->desc, p->s);
a6a5a08a357d2adbb653b81bacc602ca3543c4c4Petr Cech }
fe6dd669d1e8606862879127f92c177bb7fdc1bdPetr Cech return;
16212bbb2aaa55d0587515e72c0018479ae51be9Petr Cech
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech case DATE:
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek case BEDATE:
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek case LEDATE:
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek /* XXX: not multithread safe */
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek pp = ctime((time_t *) & p->l);
fd04b25eaa5cd105da4122854d8bc1e702760e60Jakub Hrozek if ((rt = strchr(pp, '\n')) != NULL)
da79bee1472a06b89be2df903fb0bd8ce600c610Petr Cech *rt = '\0';
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina (void) magic_rsl_printf(r, m->desc, pp);
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina return;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina default:
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina MODNAME ": invalid m->type (%d) in mprint().",
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina m->type);
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina return;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina }
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina v = signextend(r->server, m, v) & m->mask;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina (void) magic_rsl_printf(r, m->desc, (unsigned long) v);
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina}
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina/*
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina * Convert the byte order of the data we are looking at
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina */
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březinastatic int mconvert(request_rec *r, union VALUETYPE *p, struct magic *m)
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina{
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina char *rt;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina switch (m->type) {
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina case BYTE:
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina case SHORT:
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina case LONG:
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina case DATE:
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek return 1;
360a4be4266d6a72be99dfd252623dc0527f5b84Pavel Březina case STRING:
/* Null terminate and eat the return */
p->s[sizeof(p->s) - 1] = '\0';
if ((rt = strchr(p->s, '\n')) != NULL)
*rt = '\0';
return 1;
case BESHORT:
p->h = (short) ((p->hs[0] << 8) | (p->hs[1]));
return 1;
case BELONG:
case BEDATE:
p->l = (long)
((p->hl[0] << 24) | (p->hl[1] << 16) | (p->hl[2] << 8) | (p->hl[3]));
return 1;
case LESHORT:
p->h = (short) ((p->hs[1] << 8) | (p->hs[0]));
return 1;
case LELONG:
case LEDATE:
p->l = (long)
((p->hl[3] << 24) | (p->hl[2] << 16) | (p->hl[1] << 8) | (p->hl[0]));
return 1;
default:
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
MODNAME ": invalid type %d in mconvert().", m->type);
return 0;
}
}
static int mget(request_rec *r, union VALUETYPE *p, unsigned char *s,
struct magic *m, int nbytes)
{
long offset = m->offset;
if (offset + sizeof(union VALUETYPE) > nbytes)
return 0;
memcpy(p, s + offset, sizeof(union VALUETYPE));
if (!mconvert(r, p, m))
return 0;
if (m->flag & INDIR) {
switch (m->in.type) {
case BYTE:
offset = p->b + m->in.offset;
break;
case SHORT:
offset = p->h + m->in.offset;
break;
case LONG:
offset = p->l + m->in.offset;
break;
}
if (offset + sizeof(union VALUETYPE) > nbytes)
return 0;
memcpy(p, s + offset, sizeof(union VALUETYPE));
if (!mconvert(r, p, m))
return 0;
}
return 1;
}
static int mcheck(request_rec *r, union VALUETYPE *p, struct magic *m)
{
register unsigned long l = m->value.l;
register unsigned long v;
int matched;
if ((m->value.s[0] == 'x') && (m->value.s[1] == '\0')) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
MODNAME ": BOINK");
return 1;
}
switch (m->type) {
case BYTE:
v = p->b;
break;
case SHORT:
case BESHORT:
case LESHORT:
v = p->h;
break;
case LONG:
case BELONG:
case LELONG:
case DATE:
case BEDATE:
case LEDATE:
v = p->l;
break;
case STRING:
l = 0;
/*
* What we want here is: v = strncmp(m->value.s, p->s, m->vallen);
* but ignoring any nulls. bcmp doesn't give -/+/0 and isn't
* universally available anyway.
*/
v = 0;
{
register unsigned char *a = (unsigned char *) m->value.s;
register unsigned char *b = (unsigned char *) p->s;
register int len = m->vallen;
while (--len >= 0)
if ((v = *b++ - *a++) != 0)
break;
}
break;
default:
/* bogosity, pretend that it just wasn't a match */
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
MODNAME ": invalid type %d in mcheck().", m->type);
return 0;
}
v = signextend(r->server, m, v) & m->mask;
switch (m->reln) {
case 'x':
#if MIME_MAGIC_DEBUG
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
"%lu == *any* = 1", v);
#endif
matched = 1;
break;
case '!':
matched = v != l;
#if MIME_MAGIC_DEBUG
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
"%lu != %lu = %d", v, l, matched);
#endif
break;
case '=':
matched = v == l;
#if MIME_MAGIC_DEBUG
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
"%lu == %lu = %d", v, l, matched);
#endif
break;
case '>':
if (m->flag & UNSIGNED) {
matched = v > l;
#if MIME_MAGIC_DEBUG
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
"%lu > %lu = %d", v, l, matched);
#endif
}
else {
matched = (long) v > (long) l;
#if MIME_MAGIC_DEBUG
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
"%ld > %ld = %d", v, l, matched);
#endif
}
break;
case '<':
if (m->flag & UNSIGNED) {
matched = v < l;
#if MIME_MAGIC_DEBUG
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
"%lu < %lu = %d", v, l, matched);
#endif
}
else {
matched = (long) v < (long) l;
#if MIME_MAGIC_DEBUG
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
"%ld < %ld = %d", v, l, matched);
#endif
}
break;
case '&':
matched = (v & l) == l;
#if MIME_MAGIC_DEBUG
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
"((%lx & %lx) == %lx) = %d", v, l, l, matched);
#endif
break;
case '^':
matched = (v & l) != l;
#if MIME_MAGIC_DEBUG
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
"((%lx & %lx) != %lx) = %d", v, l, l, matched);
#endif
break;
default:
/* bogosity, pretend it didn't match */
matched = 0;
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
MODNAME ": mcheck: can't happen: invalid relation %d.",
m->reln);
break;
}
return matched;
}
/* an optimization over plain strcmp() */
#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
static int ascmagic(request_rec *r, unsigned char *buf, int nbytes)
{
int has_escapes = 0;
unsigned char *s;
char nbuf[HOWMANY + 1]; /* one extra for terminating '\0' */
char *token;
register struct names *p;
int small_nbytes;
/* these are easy, do them first */
/*
* for troff, look for . + letter + letter or .\"; this must be done to
* disambiguate tar archives' ./file and other trash from real troff
* input.
*/
if (*buf == '.') {
unsigned char *tp = buf + 1;
while (apr_isspace(*tp))
++tp; /* skip leading whitespace */
if ((apr_isalnum(*tp) || *tp == '\\') &&
(apr_isalnum(*(tp + 1)) || *tp == '"')) {
magic_rsl_puts(r, "application/x-troff");
return 1;
}
}
if ((*buf == 'c' || *buf == 'C') && apr_isspace(*(buf + 1))) {
/* Fortran */
magic_rsl_puts(r, "text/plain");
return 1;
}
/* look for tokens from names.h - this is expensive!, so we'll limit
* ourselves to only SMALL_HOWMANY bytes */
small_nbytes = (nbytes > SMALL_HOWMANY) ? SMALL_HOWMANY : nbytes;
/* make a copy of the buffer here because strtok() will destroy it */
s = (unsigned char *) memcpy(nbuf, buf, small_nbytes);
s[small_nbytes] = '\0';
has_escapes = (memchr(s, '\033', small_nbytes) != NULL);
/* XXX: not multithread safe */
while ((token = strtok((char *) s, " \t\n\r\f")) != NULL) {
s = NULL; /* make strtok() keep on tokin' */
for (p = names; p < names + NNAMES; p++) {
if (STREQ(p->name, token)) {
magic_rsl_puts(r, types[p->type]);
if (has_escapes)
magic_rsl_puts(r, " (with escape sequences)");
return 1;
}
}
}
switch (is_tar(buf, nbytes)) {
case 1:
/* V7 tar archive */
magic_rsl_puts(r, "application/x-tar");
return 1;
case 2:
/* POSIX tar archive */
magic_rsl_puts(r, "application/x-tar");
return 1;
}
/* all else fails, but it is ascii... */
if (has_escapes) {
/* text with escape sequences */
/* we leave this open for further differentiation later */
magic_rsl_puts(r, "text/plain");
}
else {
/* plain text */
magic_rsl_puts(r, "text/plain");
}
return 1;
}
/*
* compress routines: zmagic() - returns 0 if not recognized, uncompresses
* and prints information if recognized uncompress(s, method, old, n, newch)
* - uncompress old into new, using method, return sizeof new
*/
static struct {
char *magic;
int maglen;
char *argv[3];
int silent;
char *encoding; /* MUST be lowercase */
} compr[] = {
/* we use gzip here rather than uncompress because we have to pass
* it a full filename -- and uncompress only considers filenames
* ending with .Z
*/
{
"\037\235", 2, {
"gzip", "-dcq", NULL
}, 0, "x-compress"
},
{
"\037\213", 2, {
"gzip", "-dcq", NULL
}, 1, "x-gzip"
},
/*
* XXX pcat does not work, cause I don't know how to make it read stdin,
* so we use gzip
*/
{
"\037\036", 2, {
"gzip", "-dcq", NULL
}, 0, "x-gzip"
},
};
static int ncompr = sizeof(compr) / sizeof(compr[0]);
static int zmagic(request_rec *r, unsigned char *buf, int nbytes)
{
unsigned char *newbuf;
int newsize;
int i;
for (i = 0; i < ncompr; i++) {
if (nbytes < compr[i].maglen)
continue;
if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0)
break;
}
if (i == ncompr)
return 0;
if ((newsize = uncompress(r, i, &newbuf, nbytes)) > 0) {
tryit(r, newbuf, newsize, 0);
/* set encoding type in the request record */
r->content_encoding = compr[i].encoding;
}
return 1;
}
struct uncompress_parms {
request_rec *r;
int method;
};
static int uncompress_child(struct uncompress_parms *parm, apr_pool_t *cntxt,
BUFF **script_in)
{
int rc = 1;
char *new_argv[4];
char **env;
request_rec *r = parm->r;
apr_pool_t *child_context = cntxt;
apr_procattr_t *procattr;
apr_proc_t *procnew;
apr_socket_t *sock;
env = ap_create_environment(child_context, r->subprocess_env);
if ((apr_createprocattr_init(&procattr, child_context) != APR_SUCCESS) ||
(apr_setprocattr_io(procattr, APR_FULL_BLOCK,
APR_FULL_BLOCK, APR_NO_PIPE) != APR_SUCCESS) ||
(apr_setprocattr_dir(procattr, r->filename) != APR_SUCCESS) ||
(apr_setprocattr_cmdtype(procattr, APR_PROGRAM) != APR_SUCCESS)) {
/* Something bad happened, tell the world. */
ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_ENOPROC, r,
"couldn't setup child process: %s", r->filename);
}
else {
new_argv[0] = compr[parm->method].argv[0];
new_argv[1] = compr[parm->method].argv[1];
new_argv[2] = r->filename;
new_argv[3] = NULL;
if (compr[parm->method].silent) {
close(STDERR_FILENO);
}
procnew = apr_pcalloc(child_context, sizeof(*procnew));
rc = apr_create_process(procnew, compr[parm->method].argv[0],
new_argv, env, procattr, child_context);
if (rc != APR_SUCCESS) {
/* Bad things happened. Everyone should have cleaned up. */
ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_ENOPROC, r,
MODNAME ": could not execute `%s'.",
compr[parm->method].argv[0]);
}
else {
apr_note_subprocess(child_context, procnew, kill_after_timeout);
/* Fill in BUFF structure for parents pipe to child's stdout */
/* XXX This is a hack. The correct solution is to create a
* pipe bucket, and just pass it down. Since that bucket type
* hasn't been written, we can hack it for the moment.
*/
apr_socket_from_file(&sock, procnew->out);
if (script_in) {
*script_in = ap_bcreate(child_context, B_RD);
}
ap_bpush_socket(*script_in, sock);
}
}
return (rc);
}
static int uncompress(request_rec *r, int method,
unsigned char **newch, int n)
{
struct uncompress_parms parm;
BUFF *bout = NULL;
apr_pool_t *sub_context;
apr_status_t rv;
parm.r = r;
parm.method = method;
/* We make a sub_pool so that we can collect our child early, otherwise
* there are cases (i.e. generating directory indicies with mod_autoindex)
* where we would end up with LOTS of zombies.
*/
if (apr_create_pool(&sub_context, r->pool) != APR_SUCCESS)
return -1;
if ((rv = uncompress_child(&parm, sub_context, &bout)) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
MODNAME ": couldn't spawn uncompress process: %s", r->uri);
return -1;
}
*newch = (unsigned char *) apr_palloc(r->pool, n);
rv = ap_bread(bout, *newch, n, &n);
if (n == 0) {
apr_destroy_pool(sub_context);
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
MODNAME ": read failed %s", r->filename);
return -1;
}
apr_destroy_pool(sub_context);
return n;
}
/*
* is_tar() -- figure out whether file is a tar archive.
*
* Stolen (by author of file utility) from the public domain tar program: Public
* Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
*
* @(#)list.c 1.18 9/23/86 Public Domain - gnu $Id: mod_mime_magic.c,v 1.7
* 1997/06/24 00:41:02 ikluft Exp ikluft $
*
* Comments changed and some code/comments reformatted for file command by Ian
* Darwin.
*/
#define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
/*
* Return 0 if the checksum is bad (i.e., probably not a tar archive), 1 for
* old UNIX tar file, 2 for Unix Std (POSIX) tar file.
*/
static int is_tar(unsigned char *buf, int nbytes)
{
register union record *header = (union record *) buf;
register int i;
register long sum, recsum;
register char *p;
if (nbytes < sizeof(union record))
return 0;
recsum = from_oct(8, header->header.chksum);
sum = 0;
p = header->charptr;
for (i = sizeof(union record); --i >= 0;) {
/*
* We can't use unsigned char here because of old compilers, e.g. V7.
*/
sum += 0xFF & *p++;
}
/* Adjust checksum to count the "chksum" field as blanks. */
for (i = sizeof(header->header.chksum); --i >= 0;)
sum -= 0xFF & header->header.chksum[i];
sum += ' ' * sizeof header->header.chksum;
if (sum != recsum)
return 0; /* Not a tar archive */
if (0 == strcmp(header->header.magic, TMAGIC))
return 2; /* Unix Standard tar archive */
return 1; /* Old fashioned tar archive */
}
/*
* Quick and dirty octal conversion.
*
* Result is -1 if the field is invalid (all blank, or nonoctal).
*/
static long from_oct(int digs, char *where)
{
register long value;
while (apr_isspace(*where)) { /* Skip spaces */
where++;
if (--digs <= 0)
return -1; /* All blank field */
}
value = 0;
while (digs > 0 && isodigit(*where)) { /* Scan til nonoctal */
value = (value << 3) | (*where++ - '0');
--digs;
}
if (digs > 0 && *where && !apr_isspace(*where))
return -1; /* Ended on non-space/nul */
return value;
}
/*
* Check for file-revision suffix
*
* This is for an obscure document control system used on an intranet.
* The web representation of each file's revision has an @1, @2, etc
* appended with the revision number. This needs to be stripped off to
* find the file suffix, which can be recognized by sending the name back
* through a sub-request. The base file name (without the @num suffix)
* must exist because its type will be used as the result.
*/
static int revision_suffix(request_rec *r)
{
int suffix_pos, result;
char *sub_filename;
request_rec *sub;
#if MIME_MAGIC_DEBUG
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
MODNAME ": revision_suffix checking %s", r->filename);
#endif /* MIME_MAGIC_DEBUG */
/* check for recognized revision suffix */
suffix_pos = strlen(r->filename) - 1;
if (!apr_isdigit(r->filename[suffix_pos])) {
return 0;
}
while (suffix_pos >= 0 && apr_isdigit(r->filename[suffix_pos]))
suffix_pos--;
if (suffix_pos < 0 || r->filename[suffix_pos] != '@') {
return 0;
}
/* perform sub-request for the file name without the suffix */
result = 0;
sub_filename = apr_pstrndup(r->pool, r->filename, suffix_pos);
#if MIME_MAGIC_DEBUG
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
MODNAME ": subrequest lookup for %s", sub_filename);
#endif /* MIME_MAGIC_DEBUG */
sub = ap_sub_req_lookup_file(sub_filename, r);
/* extract content type/encoding/language from sub-request */
if (sub->content_type) {
r->content_type = apr_pstrdup(r->pool, sub->content_type);
#if MIME_MAGIC_DEBUG
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
MODNAME ": subrequest %s got %s",
sub_filename, r->content_type);
#endif /* MIME_MAGIC_DEBUG */
if (sub->content_encoding)
r->content_encoding =
apr_pstrdup(r->pool, sub->content_encoding);
if (sub->content_language)
r->content_language =
apr_pstrdup(r->pool, sub->content_language);
result = 1;
}
/* clean up */
ap_destroy_sub_req(sub);
return result;
}
/*
* initialize the module
*/
static void magic_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *main_server)
{
int result;
magic_server_config_rec *conf;
magic_server_config_rec *main_conf;
server_rec *s;
#if MIME_MAGIC_DEBUG
struct magic *m, *prevm;
#endif /* MIME_MAGIC_DEBUG */
main_conf = ap_get_module_config(main_server->module_config, &mime_magic_module);
for (s = main_server; s; s = s->next) {
conf = ap_get_module_config(s->module_config, &mime_magic_module);
if (conf->magicfile == NULL && s != main_server) {
/* inherits from the parent */
*conf = *main_conf;
}
else if (conf->magicfile) {
result = apprentice(s, p);
if (result == -1)
return;
#if MIME_MAGIC_DEBUG
prevm = 0;
ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, s,
MODNAME ": magic_init 1 test");
for (m = conf->magic; m; m = m->next) {
if (apr_isprint((((unsigned long) m) >> 24) & 255) &&
apr_isprint((((unsigned long) m) >> 16) & 255) &&
apr_isprint((((unsigned long) m) >> 8) & 255) &&
apr_isprint(((unsigned long) m) & 255)) {
ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, s,
MODNAME ": magic_init 1: POINTER CLOBBERED! "
"m=\"%c%c%c%c\" line=%d",
(((unsigned long) m) >> 24) & 255,
(((unsigned long) m) >> 16) & 255,
(((unsigned long) m) >> 8) & 255,
((unsigned long) m) & 255,
prevm ? prevm->lineno : -1);
break;
}
prevm = m;
}
#endif
}
}
}
/*
* Find the Content-Type from any resource this module has available
*/
static int magic_find_ct(request_rec *r)
{
int result;
magic_server_config_rec *conf;
/* the file has to exist */
if (r->finfo.protection == 0 || !r->filename) {
return DECLINED;
}
/* was someone else already here? */
if (r->content_type) {
return DECLINED;
}
conf = ap_get_module_config(r->server->module_config, &mime_magic_module);
if (!conf || !conf->magic) {
return DECLINED;
}
/* initialize per-request info */
if (!magic_set_config(r)) {
return HTTP_INTERNAL_SERVER_ERROR;
}
/* try excluding file-revision suffixes */
if (revision_suffix(r) != 1) {
/* process it based on the file contents */
if ((result = magic_process(r)) != OK) {
return result;
}
}
/* if we have any results, put them in the request structure */
return magic_rsl_to_request(r);
}
static void register_hooks(void)
{
static const char * const aszPre[]={ "mod_mime.c", NULL };
/* mod_mime_magic should be run after mod_mime, if at all. */
ap_hook_type_checker(magic_find_ct, aszPre, NULL, AP_HOOK_MIDDLE);
ap_hook_post_config(magic_init, NULL, NULL, AP_HOOK_FIRST);
}
/*
* Apache API module interface
*/
module mime_magic_module =
{
STANDARD20_MODULE_STUFF,
NULL, /* dir config creator */
NULL, /* dir merger --- default is to override */
create_magic_server_config, /* server config */
merge_magic_server_config, /* merge server config */
mime_magic_cmds, /* command apr_table_t */
NULL, /* handlers */
register_hooks /* register hooks */
};