1 /* ====================================================================
2 * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Group
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
21 * 4. The names "Apache Server" and "Apache Group" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
26 * 5. Products derived from this software may not be called "Apache"
27 * nor may "Apache" appear in their names without prior written
28 * permission of the Apache Group.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the Apache Group
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
35 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Group and was originally based
51 * on public domain software written at the National Center for
52 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53 * For more information on the Apache Group and the Apache HTTP server
54 * project, please see <http://www.apache.org/>.
59 * mod_mime_magic: MIME type lookup via file magic numbers
60 * Copyright (c) 1996-1997 Cisco Systems, Inc.
62 * This software was submitted by Cisco Systems to the Apache Group in July
63 * 1997. Future revisions and derivatives of this source code must
64 * acknowledge Cisco Systems as the original contributor of this module.
65 * All other licensing and usage conditions are those of the Apache Group.
67 * Some of this code is derived from the free version of the file command
68 * originally posted to comp.sources.unix. Copyright info for that program
69 * is included below as required.
70 * ---------------------------------------------------------------------------
71 * - Copyright (c) Ian F. Darwin, 1987. Written by Ian F. Darwin.
73 * This software is not subject to any license of the American Telephone and
74 * Telegraph Company or of the Regents of the University of California.
76 * Permission is granted to anyone to use this software for any purpose on any
77 * computer system, and to alter it and redistribute it freely, subject to
78 * the following restrictions:
80 * 1. The author is not responsible for the consequences of use of this
81 * software, no matter how awful, even if they arise from flaws in it.
83 * 2. The origin of this software must not be misrepresented, either by
84 * explicit claim or by omission. Since few users ever read sources, credits
85 * must appear in the documentation.
87 * 3. Altered versions must be plainly marked as such, and must not be
88 * misrepresented as being the original software. Since few users ever read
89 * sources, credits must appear in the documentation.
91 * 4. This notice may not be removed or altered.
92 * -------------------------------------------------------------------------
94 * For compliance with Mr Darwin's terms: this has been very significantly
95 * modified from the free "file" command.
96 * - all-in-one file for compilation convenience when moving from one
97 * version of Apache to the next.
98 * - Memory allocation is done through the Apache API's ap_context_t structure.
99 * - All functions have had necessary Apache API request or server
100 * structures passed to them where necessary to call other Apache API
101 * routines. (i.e. usually for logging, files, or memory allocation in
102 * itself or a called function.)
103 * - struct magic has been converted from an array to a single-ended linked
104 * list because it only grows one record at a time, it's only accessed
105 * sequentially, and the Apache API has no equivalent of realloc().
106 * - Functions have been changed to get their parameters from the server
107 * configuration instead of globals. (It should be reentrant now but has
108 * not been tested in a threaded environment.)
109 * - Places where it used to print results to stdout now saves them in a
110 * list where they're used to set the MIME type in the Apache request
112 * - Command-line flags have been removed since they will never be used here.
114 * Ian Kluft <ikluft@cisco.com>
115 * Engineering Information Framework
116 * Central Engineering
117 * Cisco Systems, Inc.
120 * Initial installation July/August 1996
121 * Misc bug fixes May 1997
122 * Submission to Apache Group July 1997
127 #include "http_config.h"
128 #include "http_request.h"
129 #include "http_core.h"
130 #include "http_log.h"
131 #include "http_protocol.h"
137 * data structures and related constants
140 #define MODNAME "mod_mime_magic"
141 #define MIME_MAGIC_DEBUG 0
143 #define MIME_BINARY_UNKNOWN "application/octet-stream"
144 #define MIME_TEXT_UNKNOWN "text/plain"
146 #define MAXMIMESTRING 256
148 /* HOWMANY must be at least 4096 to make gzip -dcq work */
150 /* SMALL_HOWMANY limits how much work we do to figure out text files */
151 #define SMALL_HOWMANY 1024
152 #define MAXDESC 50 /* max leng of text description */
153 #define MAXstring 64 /* max leng of "string" types */
156 struct magic *next; /* link to next entry */
157 int lineno; /* line number from magic file */
160 #define INDIR 1 /* if '>(...)' appears, */
161 #define UNSIGNED 2 /* comparison is unsigned */
162 short cont_level; /* level of ">" */
164 char type; /* byte short long */
165 long offset; /* offset from indirection */
167 long offset; /* offset to magic number */
168 unsigned char reln; /* relation (0=eq, '>'=gt, etc) */
169 char type; /* int, short, long or string. */
170 char vallen; /* length of string value, if any */
187 unsigned char hs[2]; /* 2 bytes of a fixed-endian "short" */
188 unsigned char hl[4]; /* 2 bytes of a fixed-endian "long" */
189 } value; /* either number or string */
190 unsigned long mask; /* mask before comparison with value */
191 char nospflag; /* supress space character */
193 /* NOTE: this string is suspected of overrunning - find it! */
194 char desc[MAXDESC]; /* description */
198 * data structures for tar file recognition
199 * --------------------------------------------------------------------------
200 * Header file for public domain tar (tape archive) program.
202 * @(#)tar.h 1.20 86/10/29 Public Domain. Created 25 August 1985 by John
203 * Gilmore, ihnp4!hoptoad!gnu.
205 * Header block on tape.
207 * I'm going to use traditional DP naming conventions here. A "block" is a big
208 * chunk of stuff that we do I/O on. A "record" is a piece of info that we
209 * care about. Typically many "record"s fit into a "block".
211 #define RECORDSIZE 512
217 char charptr[RECORDSIZE];
227 char linkname[NAMSIZ];
236 /* The magic field is filled with this if uname and gname are valid. */
237 #define TMAGIC "ustar " /* 7 chars and a null */
240 * file-function prototypes
242 static int ascmagic(request_rec *, unsigned char *, int);
243 static int is_tar(unsigned char *, int);
244 static int softmagic(request_rec *, unsigned char *, int);
245 static void tryit(request_rec *, unsigned char *, int, int);
246 static int zmagic(request_rec *, unsigned char *, int);
248 static int getvalue(server_rec *, struct magic *, char **);
249 static int hextoint(int);
250 static char *getstr(server_rec *, char *, char *, int, int *);
251 static int parse(server_rec *, ap_context_t *p, char *, int);
253 static int match(request_rec *, unsigned char *, int);
254 static int mget(request_rec *, union VALUETYPE *, unsigned char *,
255 struct magic *, int);
256 static int mcheck(request_rec *, union VALUETYPE *, struct magic *);
257 static void mprint(request_rec *, union VALUETYPE *, struct magic *);
259 static int uncompress(request_rec *, int,
260 unsigned char **, int);
261 static long from_oct(int, char *);
262 static int fsmagic(request_rec *r, const char *fn);
265 * includes for ASCII substring recognition formerly "names.h" in file
268 * Original notes: names and types used by ascmagic in file(1). These tokens are
269 * here because they can appear anywhere in the first HOWMANY bytes, while
270 * tokens in /etc/magic must appear at fixed offsets into the file. Don't
271 * make HOWMANY too high unless you have a very fast CPU.
274 /* these types are used to index the ap_table_t 'types': keep em in sync! */
275 /* HTML inserted in first because this is a web server module now */
276 #define L_HTML 0 /* HTML */
277 #define L_C 1 /* first and foremost on UNIX */
278 #define L_FORT 2 /* the oldest one */
279 #define L_MAKE 3 /* Makefiles */
280 #define L_PLI 4 /* PL/1 */
281 #define L_MACH 5 /* some kinda assembler */
282 #define L_ENG 6 /* English */
283 #define L_PAS 7 /* Pascal */
284 #define L_MAIL 8 /* Electronic mail */
285 #define L_NEWS 9 /* Usenet Netnews */
287 static char *types[] =
289 "text/html", /* HTML */
290 "text/plain", /* "c program text", */
291 "text/plain", /* "fortran program text", */
292 "text/plain", /* "make commands text", */
293 "text/plain", /* "pl/1 program text", */
294 "text/plain", /* "assembler program text", */
295 "text/plain", /* "English text", */
296 "text/plain", /* "pascal program text", */
297 "message/rfc822", /* "mail text", */
298 "message/news", /* "news text", */
299 "application/binary", /* "can't happen error on names.h/types", */
303 static struct names {
308 /* These must be sorted by eye for optimal hit rate */
309 /* Add to this list only after substantial meditation */
338 "<!DOCTYPE HTML", L_HTML
342 }, /* must precede "The", "the", etc. */
386 * Too many files of text have these words in them. Find another way to
387 * recognize Fortrash.
443 "Return-Path:", L_MAIL
449 "Newsgroups:", L_NEWS
455 "Organization:", L_NEWS
462 #define NNAMES ((sizeof(names)/sizeof(struct names)) - 1)
465 * Result String List (RSL)
467 * The file(1) command prints its output. Instead, we store the various
468 * "printed" strings in a list (allocating memory as we go) and concatenate
469 * them at the end when we finally know how much space they'll need.
472 typedef struct magic_rsl_s {
473 char *str; /* string, possibly a fragment */
474 struct magic_rsl_s *next; /* pointer to next fragment */
478 * Apache module configuration structures
481 /* per-server info */
483 char *magicfile; /* where magic be found */
484 struct magic *magic; /* head of magic config list */
486 } magic_server_config_rec;
488 /* per-request info */
490 magic_rsl *head; /* result string list */
492 unsigned suf_recursion; /* recursion depth in suffix check */
496 * configuration functions - called by Apache API routines
499 module mime_magic_module;
501 static void *create_magic_server_config(ap_context_t *p, server_rec *d)
503 /* allocate the config - use pcalloc because it needs to be zeroed */
504 return ap_pcalloc(p, sizeof(magic_server_config_rec));
507 static void *merge_magic_server_config(ap_context_t *p, void *basev, void *addv)
509 magic_server_config_rec *base = (magic_server_config_rec *) basev;
510 magic_server_config_rec *add = (magic_server_config_rec *) addv;
511 magic_server_config_rec *new = (magic_server_config_rec *)
512 ap_palloc(p, sizeof(magic_server_config_rec));
514 new->magicfile = add->magicfile ? add->magicfile : base->magicfile;
520 static const char *set_magicfile(cmd_parms *cmd, char *d, char *arg)
522 magic_server_config_rec *conf = (magic_server_config_rec *)
523 ap_get_module_config(cmd->server->module_config,
527 return MODNAME ": server structure not allocated";
529 conf->magicfile = arg;
534 * configuration file commands - exported to Apache API
537 static const command_rec mime_magic_cmds[] =
539 {"MimeMagicFile", set_magicfile, NULL, RSRC_CONF, TAKE1,
540 "Path to MIME Magic file (in file(1) format)"},
545 * RSL (result string list) processing routines
547 * These collect strings that would have been printed in fragments by file(1)
548 * into a list of magic_rsl structures with the strings. When complete,
549 * they're concatenated together to become the MIME content and encoding
552 * return value conventions for these functions: functions which return int:
553 * failure = -1, other = result functions which return pointers: failure = 0,
557 /* allocate a per-request structure and put it in the request record */
558 static magic_req_rec *magic_set_config(request_rec *r)
560 magic_req_rec *req_dat = (magic_req_rec *) ap_palloc(r->pool,
561 sizeof(magic_req_rec));
563 req_dat->head = req_dat->tail = (magic_rsl *) NULL;
564 ap_set_module_config(r->request_config, &mime_magic_module, req_dat);
568 /* add a string to the result string list for this request */
569 /* it is the responsibility of the caller to allocate "str" */
570 static int magic_rsl_add(request_rec *r, char *str)
572 magic_req_rec *req_dat = (magic_req_rec *)
573 ap_get_module_config(r->request_config, &mime_magic_module);
576 /* make sure we have a list to put it in */
578 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, r,
579 MODNAME ": request config should not be NULL");
580 if (!(req_dat = magic_set_config(r))) {
586 /* allocate the list entry */
587 rsl = (magic_rsl *) ap_palloc(r->pool, sizeof(magic_rsl));
591 rsl->next = (magic_rsl *) NULL;
593 /* append to the list */
594 if (req_dat->head && req_dat->tail) {
595 req_dat->tail->next = rsl;
599 req_dat->head = req_dat->tail = rsl;
606 /* RSL hook for puts-type functions */
607 static int magic_rsl_puts(request_rec *r, char *str)
609 return magic_rsl_add(r, str);
612 /* RSL hook for printf-type functions */
613 static int magic_rsl_printf(request_rec *r, char *str,...)
617 char buf[MAXMIMESTRING];
619 /* assemble the string into the buffer */
621 ap_vsnprintf(buf, sizeof(buf), str, ap);
624 /* add the buffer to the list */
625 return magic_rsl_add(r, strdup(buf));
628 /* RSL hook for putchar-type functions */
629 static int magic_rsl_putchar(request_rec *r, char c)
633 /* high overhead for 1 char - just hope they don't do this much */
636 return magic_rsl_add(r, str);
639 /* allocate and copy a contiguous string from a result string list */
640 static char *rsl_strdup(request_rec *r, int start_frag, int start_pos, int len)
642 char *result; /* return value */
643 int cur_frag, /* current fragment number/counter */
644 cur_pos, /* current position within fragment */
645 res_pos; /* position in result string */
646 magic_rsl *frag; /* list-traversal pointer */
647 magic_req_rec *req_dat = (magic_req_rec *)
648 ap_get_module_config(r->request_config, &mime_magic_module);
650 /* allocate the result string */
651 result = (char *) ap_palloc(r->pool, len + 1);
653 /* loop through and collect the string */
655 for (frag = req_dat->head, cur_frag = 0;
657 frag = frag->next, cur_frag++) {
658 /* loop to the first fragment */
659 if (cur_frag < start_frag)
662 /* loop through and collect chars */
663 for (cur_pos = (cur_frag == start_frag) ? start_pos : 0;
666 if (cur_frag >= start_frag
667 && cur_pos >= start_pos
669 result[res_pos++] = frag->str[cur_pos];
677 /* clean up and return */
680 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
681 MODNAME ": rsl_strdup() %d chars: %s", res_pos - 1, result);
686 /* states for the state-machine algorithm in magic_rsl_to_request() */
688 rsl_leading_space, rsl_type, rsl_subtype, rsl_separator, rsl_encoding
691 /* process the RSL and set the MIME info in the request record */
692 static int magic_rsl_to_request(request_rec *r)
694 int cur_frag, /* current fragment number/counter */
695 cur_pos, /* current position within fragment */
696 type_frag, /* content type starting point: fragment */
697 type_pos, /* content type starting point: position */
698 type_len, /* content type length */
699 encoding_frag, /* content encoding starting point: fragment */
700 encoding_pos, /* content encoding starting point: position */
701 encoding_len; /* content encoding length */
703 magic_rsl *frag; /* list-traversal pointer */
706 magic_req_rec *req_dat = (magic_req_rec *)
707 ap_get_module_config(r->request_config, &mime_magic_module);
709 /* check if we have a result */
710 if (!req_dat || !req_dat->head) {
711 /* empty - no match, we defer to other Apache modules */
715 /* start searching for the type and encoding */
716 state = rsl_leading_space;
717 type_frag = type_pos = type_len = 0;
718 encoding_frag = encoding_pos = encoding_len = 0;
719 for (frag = req_dat->head, cur_frag = 0;
721 frag = frag->next, cur_frag++) {
722 /* loop through the characters in the fragment */
723 for (cur_pos = 0; frag->str[cur_pos]; cur_pos++) {
724 if (ap_isspace(frag->str[cur_pos])) {
725 /* process whitespace actions for each state */
726 if (state == rsl_leading_space) {
727 /* eat whitespace in this state */
730 else if (state == rsl_type) {
731 /* whitespace: type has no slash! */
734 else if (state == rsl_subtype) {
735 /* whitespace: end of MIME type */
739 else if (state == rsl_separator) {
740 /* eat whitespace in this state */
743 else if (state == rsl_encoding) {
744 /* whitespace: end of MIME encoding */
746 frag = req_dat->tail;
750 /* should not be possible */
751 /* abandon malfunctioning module */
752 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
753 MODNAME ": bad state %d (ws)", state);
758 else if (state == rsl_type &&
759 frag->str[cur_pos] == '/') {
760 /* copy the char and go to rsl_subtype state */
765 /* process non-space actions for each state */
766 if (state == rsl_leading_space) {
767 /* non-space: begin MIME type */
769 type_frag = cur_frag;
774 else if (state == rsl_type ||
775 state == rsl_subtype) {
776 /* non-space: adds to type */
780 else if (state == rsl_separator) {
781 /* non-space: begin MIME encoding */
783 encoding_frag = cur_frag;
784 encoding_pos = cur_pos;
788 else if (state == rsl_encoding) {
789 /* non-space: adds to encoding */
794 /* should not be possible */
795 /* abandon malfunctioning module */
796 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
797 MODNAME ": bad state %d (ns)", state);
806 /* if we ended prior to state rsl_subtype, we had incomplete info */
807 if (state != rsl_subtype && state != rsl_separator &&
808 state != rsl_encoding) {
809 /* defer to other modules */
813 /* save the info in the request record */
814 if (state == rsl_subtype || state == rsl_encoding ||
815 state == rsl_encoding) {
817 tmp = rsl_strdup(r, type_frag, type_pos, type_len);
818 /* XXX: this could be done at config time I'm sure... but I'm
819 * confused by all this magic_rsl stuff. -djg */
820 ap_content_type_tolower(tmp);
821 r->content_type = tmp;
823 if (state == rsl_encoding) {
825 tmp = rsl_strdup(r, encoding_frag,
826 encoding_pos, encoding_len);
827 /* XXX: this could be done at config time I'm sure... but I'm
828 * confused by all this magic_rsl stuff. -djg */
830 r->content_encoding = tmp;
833 /* detect memory allocation errors */
834 if (!r->content_type ||
835 (state == rsl_encoding && !r->content_encoding)) {
836 return HTTP_INTERNAL_SERVER_ERROR;
844 * magic_process - process input file r Apache API request record
845 * (formerly called "process" in file command, prefix added for clarity) Opens
846 * the file and reads a fixed-size buffer to begin processing the contents.
848 static int magic_process(request_rec *r)
851 unsigned char buf[HOWMANY + 1]; /* one extra for terminating '\0' */
852 int nbytes = 0; /* number of bytes read from a datafile */
856 * first try judging the file based on its filesystem status
858 switch ((result = fsmagic(r, r->filename))) {
860 magic_rsl_putchar(r, '\n');
865 /* fatal error, bail out */
869 if ((fd = ap_popenf(r->pool, r->filename, O_RDONLY, 0)) < 0) {
870 /* We can't open it, but we were able to stat it. */
871 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
872 MODNAME ": can't read `%s'", r->filename);
873 /* let some other handler decide what the problem is */
878 * try looking at the first HOWMANY bytes
880 if ((nbytes = read(fd, (char *) buf, sizeof(buf) - 1)) == -1) {
881 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
882 MODNAME ": read failed: %s", r->filename);
883 return HTTP_INTERNAL_SERVER_ERROR;
887 magic_rsl_puts(r, MIME_TEXT_UNKNOWN);
889 buf[nbytes++] = '\0'; /* null-terminate it */
890 tryit(r, buf, nbytes, 1);
893 (void) ap_pclosef(r->pool, fd);
894 (void) magic_rsl_putchar(r, '\n');
900 static void tryit(request_rec *r, unsigned char *buf, int nb, int checkzmagic)
903 * Try compression stuff
905 if (checkzmagic == 1) {
906 if (zmagic(r, buf, nb) == 1)
911 * try tests in /etc/magic (or surrogate magic file)
913 if (softmagic(r, buf, nb) == 1)
917 * try known keywords, check for ascii-ness too.
919 if (ascmagic(r, buf, nb) == 1)
923 * abandon hope, all ye who remain here
925 magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
928 #define EATAB {while (ap_isspace((unsigned char) *l)) ++l;}
931 * apprentice - load configuration from the magic file r
934 static int apprentice(server_rec *s, ap_context_t *p)
937 char line[BUFSIZ + 1];
942 struct magic *m, *prevm;
946 magic_server_config_rec *conf = (magic_server_config_rec *)
947 ap_get_module_config(s->module_config, &mime_magic_module);
949 fname = ap_server_root_relative(p, conf->magicfile);
950 f = ap_pfopen(p, fname, "r");
952 ap_log_error(APLOG_MARK, APLOG_ERR, s,
953 MODNAME ": can't read magic file %s", fname);
957 /* set up the magic list (empty) */
958 conf->magic = conf->last = NULL;
961 for (lineno = 1; fgets(line, BUFSIZ, f) != NULL; lineno++) {
966 line[strlen(line) - 1] = '\0';
969 /* skip leading whitespace */
971 while (line[ws_offset] && ap_isspace(line[ws_offset])) {
975 /* skip blank lines */
976 if (line[ws_offset] == 0) {
980 /* comment, do not parse */
981 if (line[ws_offset] == '#')
985 /* if we get here, we're going to use it so count it */
990 if (parse(s, p, line + ws_offset, lineno) != 0)
994 (void) ap_pfclose(p, f);
997 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, s,
998 MODNAME ": apprentice conf=%x file=%s m=%s m->next=%s last=%s",
1000 conf->magicfile ? conf->magicfile : "NULL",
1001 conf->magic ? "set" : "NULL",
1002 (conf->magic && conf->magic->next) ? "set" : "NULL",
1003 conf->last ? "set" : "NULL");
1004 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, s,
1005 MODNAME ": apprentice read %d lines, %d rules, %d errors",
1006 lineno, rule, errs);
1009 #if MIME_MAGIC_DEBUG
1011 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, s,
1012 MODNAME ": apprentice test");
1013 for (m = conf->magic; m; m = m->next) {
1014 if (ap_isprint((((unsigned long) m) >> 24) & 255) &&
1015 ap_isprint((((unsigned long) m) >> 16) & 255) &&
1016 ap_isprint((((unsigned long) m) >> 8) & 255) &&
1017 ap_isprint(((unsigned long) m) & 255)) {
1018 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, s,
1019 MODNAME ": apprentice: POINTER CLOBBERED! "
1020 "m=\"%c%c%c%c\" line=%d",
1021 (((unsigned long) m) >> 24) & 255,
1022 (((unsigned long) m) >> 16) & 255,
1023 (((unsigned long) m) >> 8) & 255,
1024 ((unsigned long) m) & 255,
1025 prevm ? prevm->lineno : -1);
1032 return (errs ? -1 : 0);
1036 * extend the sign bit if the comparison is to be signed
1038 static unsigned long signextend(server_rec *s, struct magic *m, unsigned long v)
1040 if (!(m->flag & UNSIGNED))
1043 * Do not remove the casts below. They are vital. When later
1044 * compared with the data, the sign extension must have happened.
1065 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, s,
1066 MODNAME ": can't happen: m->type=%d", m->type);
1073 * parse one line from magic file, put into magic[index++] if valid
1075 static int parse(server_rec *serv, ap_context_t *p, char *l, int lineno)
1079 magic_server_config_rec *conf = (magic_server_config_rec *)
1080 ap_get_module_config(serv->module_config, &mime_magic_module);
1082 /* allocate magic structure entry */
1083 m = (struct magic *) ap_pcalloc(p, sizeof(struct magic));
1085 /* append to linked list */
1087 if (!conf->magic || !conf->last) {
1088 conf->magic = conf->last = m;
1091 conf->last->next = m;
1095 /* set values in magic structure */
1101 ++l; /* step over */
1105 if (m->cont_level != 0 && *l == '(') {
1106 ++l; /* step over */
1110 /* get offset, then skip over it */
1111 m->offset = (int) strtol(l, &t, 0);
1113 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, serv,
1114 MODNAME ": offset %s invalid", l);
1118 if (m->flag & INDIR) {
1122 * read [.lbs][+-]nnnnn)
1136 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, serv,
1137 MODNAME ": indirect offset type %c invalid", *l);
1143 if (*l == '+' || *l == '-')
1145 if (ap_isdigit((unsigned char) *l)) {
1146 m->in.offset = strtol(l, &t, 0);
1148 m->in.offset = -m->in.offset;
1153 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, serv,
1154 MODNAME ": missing ')' in indirect offset");
1160 while (ap_isdigit((unsigned char) *l))
1178 m->flag |= UNSIGNED;
1181 /* get type, skip it */
1182 if (strncmp(l, "byte", NBYTE) == 0) {
1186 else if (strncmp(l, "short", NSHORT) == 0) {
1190 else if (strncmp(l, "long", NLONG) == 0) {
1194 else if (strncmp(l, "string", NSTRING) == 0) {
1198 else if (strncmp(l, "date", NDATE) == 0) {
1202 else if (strncmp(l, "beshort", NBESHORT) == 0) {
1206 else if (strncmp(l, "belong", NBELONG) == 0) {
1210 else if (strncmp(l, "bedate", NBEDATE) == 0) {
1214 else if (strncmp(l, "leshort", NLESHORT) == 0) {
1218 else if (strncmp(l, "lelong", NLELONG) == 0) {
1222 else if (strncmp(l, "ledate", NLEDATE) == 0) {
1227 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, serv,
1228 MODNAME ": type %s invalid", l);
1231 /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
1234 m->mask = signextend(serv, m, strtol(l, &l, 0));
1243 /* Old-style anding: "0 byte &0x80 dynamically linked" */
1251 if (m->type != STRING) {
1258 if (*l == 'x' && ap_isspace((unsigned char) l[1])) {
1261 goto GetDesc; /* Bill The Cat */
1268 if (getvalue(serv, m, &l))
1271 * now get last part - the description
1279 else if ((l[0] == '\\') && (l[1] == 'b')) {
1286 strncpy(m->desc, l, sizeof(m->desc) - 1);
1287 m->desc[sizeof(m->desc) - 1] = '\0';
1289 #if MIME_MAGIC_DEBUG
1290 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, serv,
1291 MODNAME ": parse line=%d m=%x next=%x cont=%d desc=%s",
1292 lineno, m, m->next, m->cont_level, m->desc);
1293 #endif /* MIME_MAGIC_DEBUG */
1299 * Read a numeric value from a pointer, into the value union of a magic
1300 * pointer, according to the magic type. Update the string pointer to point
1301 * just after the number read. Return 0 for success, non-zero for failure.
1303 static int getvalue(server_rec *s, struct magic *m, char **p)
1307 if (m->type == STRING) {
1308 *p = getstr(s, *p, m->value.s, sizeof(m->value.s), &slen);
1311 else if (m->reln != 'x')
1312 m->value.l = signextend(s, m, strtol(*p, p, 0));
1317 * Convert a string containing C character escapes. Stop at an unescaped
1318 * space or tab. Copy the converted version to "p", returning its length in
1319 * *slen. Return updated scan pointer as function result.
1321 static char *getstr(server_rec *serv, register char *s, register char *p,
1322 int plen, int *slen)
1324 char *origs = s, *origp = p;
1325 char *pmax = p + plen - 1;
1329 while ((c = *s++) != '\0') {
1330 if (ap_isspace((unsigned char) c))
1333 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, serv,
1334 MODNAME ": string too long: %s", origs);
1371 /* \ and up to 3 octal digits */
1381 c = *s++; /* try for 2 */
1382 if (c >= '0' && c <= '7') {
1383 val = (val << 3) | (c - '0');
1384 c = *s++; /* try for 3 */
1385 if (c >= '0' && c <= '7')
1386 val = (val << 3) | (c - '0');
1395 /* \x and up to 3 hex digits */
1397 val = 'x'; /* Default if no digits */
1398 c = hextoint(*s++); /* Get next char */
1403 val = (val << 4) + c;
1406 val = (val << 4) + c;
1430 /* Single hex char to int; -1 if not a hex char. */
1431 static int hextoint(int c)
1433 if (ap_isdigit((unsigned char) c))
1435 if ((c >= 'a') && (c <= 'f'))
1436 return c + 10 - 'a';
1437 if ((c >= 'A') && (c <= 'F'))
1438 return c + 10 - 'A';
1444 * return DONE to indicate it's been handled
1445 * return OK to indicate it's a regular file still needing handling
1446 * other returns indicate a failure of some sort
1448 static int fsmagic(request_rec *r, const char *fn)
1450 switch (r->finfo.st_mode & S_IFMT) {
1452 magic_rsl_puts(r, DIR_MAGIC_TYPE);
1456 * (void) magic_rsl_printf(r,"character special (%d/%d)",
1457 * major(sb->st_rdev), minor(sb->st_rdev));
1459 (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
1464 * (void) magic_rsl_printf(r,"block special (%d/%d)",
1465 * major(sb->st_rdev), minor(sb->st_rdev));
1467 (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
1469 /* TODO add code to handle V7 MUX and Blit MUX files */
1474 * magic_rsl_puts(r,"fifo (named pipe)");
1476 (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
1481 /* We used stat(), the only possible reason for this is that the
1482 * symlink is broken.
1484 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
1485 MODNAME ": broken symlink (%s)", fn);
1486 return HTTP_INTERNAL_SERVER_ERROR;
1489 #ifndef __COHERENT__
1491 magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
1498 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
1499 MODNAME ": invalid mode 0%o.", (unsigned int)r->finfo.st_mode);
1500 return HTTP_INTERNAL_SERVER_ERROR;
1504 * regular file, check next possibility
1506 if (r->finfo.st_size == 0) {
1507 magic_rsl_puts(r, MIME_TEXT_UNKNOWN);
1514 * softmagic - lookup one file in database (already read from /etc/magic by
1515 * apprentice.c). Passed the name and FILE * of one file to be typed.
1517 /* ARGSUSED1 *//* nbytes passed for regularity, maybe need later */
1518 static int softmagic(request_rec *r, unsigned char *buf, int nbytes)
1520 if (match(r, buf, nbytes))
1527 * Go through the whole list, stopping if you find a match. Process all the
1528 * continuations of that match before returning.
1530 * We support multi-level continuations:
1532 * At any time when processing a successful top-level match, there is a current
1533 * continuation level; it represents the level of the last successfully
1534 * matched continuation.
1536 * Continuations above that level are skipped as, if we see one, it means that
1537 * the continuation that controls them - i.e, the lower-level continuation
1538 * preceding them - failed to match.
1540 * Continuations below that level are processed as, if we see one, it means
1541 * we've finished processing or skipping higher-level continuations under the
1542 * control of a successful or unsuccessful lower-level continuation, and are
1543 * now seeing the next lower-level continuation and should process it. The
1544 * current continuation level reverts to the level of the one we're seeing.
1546 * Continuations at the current level are processed as, if we see one, there's
1547 * no lower-level continuation that may have failed.
1549 * If a continuation matches, we bump the current continuation level so that
1550 * higher-level continuations are processed.
1552 static int match(request_rec *r, unsigned char *s, int nbytes)
1554 #if MIME_MAGIC_DEBUG
1555 int rule_counter = 0;
1558 int need_separator = 0;
1560 magic_server_config_rec *conf = (magic_server_config_rec *)
1561 ap_get_module_config(r->server->module_config, &mime_magic_module);
1564 #if MIME_MAGIC_DEBUG
1565 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1566 MODNAME ": match conf=%x file=%s m=%s m->next=%s last=%s",
1568 conf->magicfile ? conf->magicfile : "NULL",
1569 conf->magic ? "set" : "NULL",
1570 (conf->magic && conf->magic->next) ? "set" : "NULL",
1571 conf->last ? "set" : "NULL");
1574 #if MIME_MAGIC_DEBUG
1575 for (m = conf->magic; m; m = m->next) {
1576 if (ap_isprint((((unsigned long) m) >> 24) & 255) &&
1577 ap_isprint((((unsigned long) m) >> 16) & 255) &&
1578 ap_isprint((((unsigned long) m) >> 8) & 255) &&
1579 ap_isprint(((unsigned long) m) & 255)) {
1580 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1581 MODNAME ": match: POINTER CLOBBERED! "
1583 (((unsigned long) m) >> 24) & 255,
1584 (((unsigned long) m) >> 16) & 255,
1585 (((unsigned long) m) >> 8) & 255,
1586 ((unsigned long) m) & 255);
1592 for (m = conf->magic; m; m = m->next) {
1593 #if MIME_MAGIC_DEBUG
1595 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1596 MODNAME ": line=%d desc=%s", m->lineno, m->desc);
1599 /* check if main entry matches */
1600 if (!mget(r, &p, s, m, nbytes) ||
1601 !mcheck(r, &p, m)) {
1602 struct magic *m_cont;
1605 * main entry didn't match, flush its continuations
1607 if (!m->next || (m->next->cont_level == 0)) {
1612 while (m_cont && (m_cont->cont_level != 0)) {
1613 #if MIME_MAGIC_DEBUG
1615 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1616 MODNAME ": line=%d mc=%x mc->next=%x cont=%d desc=%s",
1617 m_cont->lineno, m_cont,
1618 m_cont->next, m_cont->cont_level,
1622 * this trick allows us to keep *m in sync when the continue
1623 * advances the pointer
1626 m_cont = m_cont->next;
1631 /* if we get here, the main entry rule was a match */
1632 /* this will be the last run through the loop */
1633 #if MIME_MAGIC_DEBUG
1634 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1635 MODNAME ": rule matched, line=%d type=%d %s",
1637 (m->type == STRING) ? m->value.s : "");
1640 /* print the match */
1644 * If we printed something, we'll need to print a blank before we
1645 * print something else.
1649 /* and any continuations that match */
1652 * while (m && m->next && m->next->cont_level != 0 && ( m = m->next
1656 while (m && (m->cont_level != 0)) {
1657 #if MIME_MAGIC_DEBUG
1658 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1659 MODNAME ": match line=%d cont=%d type=%d %s",
1660 m->lineno, m->cont_level, m->type,
1661 (m->type == STRING) ? m->value.s : "");
1663 if (cont_level >= m->cont_level) {
1664 if (cont_level > m->cont_level) {
1666 * We're at the end of the level "cont_level"
1669 cont_level = m->cont_level;
1671 if (mget(r, &p, s, m, nbytes) &&
1674 * This continuation matched. Print its message, with a
1675 * blank before it if the previous item printed and this
1678 /* space if previous printed */
1680 && (m->nospflag == 0)
1681 && (m->desc[0] != '\0')
1683 (void) magic_rsl_putchar(r, ' ');
1691 * If we see any continuations at a higher level, process
1698 /* move to next continuation record */
1701 #if MIME_MAGIC_DEBUG
1702 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1703 MODNAME ": matched after %d rules", rule_counter);
1705 return 1; /* all through */
1707 #if MIME_MAGIC_DEBUG
1708 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1709 MODNAME ": failed after %d rules", rule_counter);
1711 return 0; /* no match at all */
1714 static void mprint(request_rec *r, union VALUETYPE *p, struct magic *m)
1737 if (m->reln == '=') {
1738 (void) magic_rsl_printf(r, m->desc, m->value.s);
1741 (void) magic_rsl_printf(r, m->desc, p->s);
1748 /* XXX: not multithread safe */
1749 pp = ctime((time_t *) & p->l);
1750 if ((rt = strchr(pp, '\n')) != NULL)
1752 (void) magic_rsl_printf(r, m->desc, pp);
1755 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
1756 MODNAME ": invalid m->type (%d) in mprint().",
1761 v = signextend(r->server, m, v) & m->mask;
1762 (void) magic_rsl_printf(r, m->desc, (unsigned long) v);
1766 * Convert the byte order of the data we are looking at
1768 static int mconvert(request_rec *r, union VALUETYPE *p, struct magic *m)
1779 /* Null terminate and eat the return */
1780 p->s[sizeof(p->s) - 1] = '\0';
1781 if ((rt = strchr(p->s, '\n')) != NULL)
1785 p->h = (short) ((p->hs[0] << 8) | (p->hs[1]));
1790 ((p->hl[0] << 24) | (p->hl[1] << 16) | (p->hl[2] << 8) | (p->hl[3]));
1793 p->h = (short) ((p->hs[1] << 8) | (p->hs[0]));
1798 ((p->hl[3] << 24) | (p->hl[2] << 16) | (p->hl[1] << 8) | (p->hl[0]));
1801 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
1802 MODNAME ": invalid type %d in mconvert().", m->type);
1808 static int mget(request_rec *r, union VALUETYPE *p, unsigned char *s,
1809 struct magic *m, int nbytes)
1811 long offset = m->offset;
1813 if (offset + sizeof(union VALUETYPE) > nbytes)
1816 memcpy(p, s + offset, sizeof(union VALUETYPE));
1818 if (!mconvert(r, p, m))
1821 if (m->flag & INDIR) {
1823 switch (m->in.type) {
1825 offset = p->b + m->in.offset;
1828 offset = p->h + m->in.offset;
1831 offset = p->l + m->in.offset;
1835 if (offset + sizeof(union VALUETYPE) > nbytes)
1838 memcpy(p, s + offset, sizeof(union VALUETYPE));
1840 if (!mconvert(r, p, m))
1846 static int mcheck(request_rec *r, union VALUETYPE *p, struct magic *m)
1848 register unsigned long l = m->value.l;
1849 register unsigned long v;
1852 if ((m->value.s[0] == 'x') && (m->value.s[1] == '\0')) {
1853 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
1881 * What we want here is: v = strncmp(m->value.s, p->s, m->vallen);
1882 * but ignoring any nulls. bcmp doesn't give -/+/0 and isn't
1883 * universally available anyway.
1887 register unsigned char *a = (unsigned char *) m->value.s;
1888 register unsigned char *b = (unsigned char *) p->s;
1889 register int len = m->vallen;
1892 if ((v = *b++ - *a++) != 0)
1897 /* bogosity, pretend that it just wasn't a match */
1898 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
1899 MODNAME ": invalid type %d in mcheck().", m->type);
1903 v = signextend(r->server, m, v) & m->mask;
1907 #if MIME_MAGIC_DEBUG
1908 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1909 "%lu == *any* = 1", v);
1916 #if MIME_MAGIC_DEBUG
1917 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1918 "%lu != %lu = %d", v, l, matched);
1924 #if MIME_MAGIC_DEBUG
1925 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1926 "%lu == %lu = %d", v, l, matched);
1931 if (m->flag & UNSIGNED) {
1933 #if MIME_MAGIC_DEBUG
1934 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1935 "%lu > %lu = %d", v, l, matched);
1939 matched = (long) v > (long) l;
1940 #if MIME_MAGIC_DEBUG
1941 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1942 "%ld > %ld = %d", v, l, matched);
1948 if (m->flag & UNSIGNED) {
1950 #if MIME_MAGIC_DEBUG
1951 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1952 "%lu < %lu = %d", v, l, matched);
1956 matched = (long) v < (long) l;
1957 #if MIME_MAGIC_DEBUG
1958 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1959 "%ld < %ld = %d", v, l, matched);
1965 matched = (v & l) == l;
1966 #if MIME_MAGIC_DEBUG
1967 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1968 "((%lx & %lx) == %lx) = %d", v, l, l, matched);
1973 matched = (v & l) != l;
1974 #if MIME_MAGIC_DEBUG
1975 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
1976 "((%lx & %lx) != %lx) = %d", v, l, l, matched);
1981 /* bogosity, pretend it didn't match */
1983 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, 0, r,
1984 MODNAME ": mcheck: can't happen: invalid relation %d.",
1992 /* an optimization over plain strcmp() */
1993 #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
1995 static int ascmagic(request_rec *r, unsigned char *buf, int nbytes)
1997 int has_escapes = 0;
1999 char nbuf[HOWMANY + 1]; /* one extra for terminating '\0' */
2001 register struct names *p;
2004 /* these are easy, do them first */
2007 * for troff, look for . + letter + letter or .\"; this must be done to
2008 * disambiguate tar archives' ./file and other trash from real troff
2012 unsigned char *tp = buf + 1;
2014 while (ap_isspace(*tp))
2015 ++tp; /* skip leading whitespace */
2016 if ((ap_isalnum(*tp) || *tp == '\\') &&
2017 (ap_isalnum(*(tp + 1)) || *tp == '"')) {
2018 magic_rsl_puts(r, "application/x-troff");
2022 if ((*buf == 'c' || *buf == 'C') && ap_isspace(*(buf + 1))) {
2024 magic_rsl_puts(r, "text/plain");
2028 /* look for tokens from names.h - this is expensive!, so we'll limit
2029 * ourselves to only SMALL_HOWMANY bytes */
2030 small_nbytes = (nbytes > SMALL_HOWMANY) ? SMALL_HOWMANY : nbytes;
2031 /* make a copy of the buffer here because strtok() will destroy it */
2032 s = (unsigned char *) memcpy(nbuf, buf, small_nbytes);
2033 s[small_nbytes] = '\0';
2034 has_escapes = (memchr(s, '\033', small_nbytes) != NULL);
2035 /* XXX: not multithread safe */
2036 while ((token = strtok((char *) s, " \t\n\r\f")) != NULL) {
2037 s = NULL; /* make strtok() keep on tokin' */
2038 for (p = names; p < names + NNAMES; p++) {
2039 if (STREQ(p->name, token)) {
2040 magic_rsl_puts(r, types[p->type]);
2042 magic_rsl_puts(r, " (with escape sequences)");
2048 switch (is_tar(buf, nbytes)) {
2050 /* V7 tar archive */
2051 magic_rsl_puts(r, "application/x-tar");
2054 /* POSIX tar archive */
2055 magic_rsl_puts(r, "application/x-tar");
2059 /* all else fails, but it is ascii... */
2061 /* text with escape sequences */
2062 /* we leave this open for further differentiation later */
2063 magic_rsl_puts(r, "text/plain");
2067 magic_rsl_puts(r, "text/plain");
2074 * compress routines: zmagic() - returns 0 if not recognized, uncompresses
2075 * and prints information if recognized uncompress(s, method, old, n, newch)
2076 * - uncompress old into new, using method, return sizeof new
2084 char *encoding; /* MUST be lowercase */
2087 /* we use gzip here rather than uncompress because we have to pass
2088 * it a full filename -- and uncompress only considers filenames
2093 "gzip", "-dcq", NULL
2098 "gzip", "-dcq", NULL
2102 * XXX pcat does not work, cause I don't know how to make it read stdin,
2107 "gzip", "-dcq", NULL
2112 static int ncompr = sizeof(compr) / sizeof(compr[0]);
2114 static int zmagic(request_rec *r, unsigned char *buf, int nbytes)
2116 unsigned char *newbuf;
2120 for (i = 0; i < ncompr; i++) {
2121 if (nbytes < compr[i].maglen)
2123 if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0)
2130 if ((newsize = uncompress(r, i, &newbuf, nbytes)) > 0) {
2131 tryit(r, newbuf, newsize, 0);
2133 /* set encoding type in the request record */
2134 r->content_encoding = compr[i].encoding;
2140 struct uncompress_parms {
2145 static int uncompress_child(void *data, child_info *pinfo)
2147 struct uncompress_parms *parm = data;
2151 new_argv[0] = compr[parm->method].argv[0];
2152 new_argv[1] = compr[parm->method].argv[1];
2153 new_argv[2] = parm->r->filename;
2156 if (compr[parm->method].silent) {
2157 close(STDERR_FILENO);
2160 child_pid = ap_spawnvp(compr[parm->method].argv[0],
2162 if (child_pid == -1)
2163 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, parm->r,
2164 MODNAME ": could not execute `%s'.",
2165 compr[parm->method].argv[0]);
2170 static int uncompress(request_rec *r, int method,
2171 unsigned char **newch, int n)
2173 struct uncompress_parms parm;
2175 ap_context_t *sub_pool;
2178 parm.method = method;
2180 /* We make a sub_pool so that we can collect our child early, otherwise
2181 * there are cases (i.e. generating directory indicies with mod_autoindex)
2182 * where we would end up with LOTS of zombies.
2184 if (ap_create_context(&sub_pool, r->pool) != APR_SUCCESS)
2187 if (!ap_bspawn_child(sub_pool, uncompress_child, &parm, kill_always,
2188 NULL, &bout, NULL)) {
2189 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
2190 MODNAME ": couldn't spawn uncompress process: %s", r->uri);
2194 *newch = (unsigned char *) ap_palloc(r->pool, n);
2195 if ((n = ap_bread(bout, *newch, n)) <= 0) {
2196 ap_destroy_pool(sub_pool);
2197 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
2198 MODNAME ": read failed %s", r->filename);
2201 ap_destroy_pool(sub_pool);
2206 * is_tar() -- figure out whether file is a tar archive.
2208 * Stolen (by author of file utility) from the public domain tar program: Public
2209 * Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
2211 * @(#)list.c 1.18 9/23/86 Public Domain - gnu $Id: mod_mime_magic.c,v 1.7
2212 * 1997/06/24 00:41:02 ikluft Exp ikluft $
2214 * Comments changed and some code/comments reformatted for file command by Ian
2218 #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
2221 * Return 0 if the checksum is bad (i.e., probably not a tar archive), 1 for
2222 * old UNIX tar file, 2 for Unix Std (POSIX) tar file.
2225 static int is_tar(unsigned char *buf, int nbytes)
2227 register union record *header = (union record *) buf;
2229 register long sum, recsum;
2232 if (nbytes < sizeof(union record))
2235 recsum = from_oct(8, header->header.chksum);
2238 p = header->charptr;
2239 for (i = sizeof(union record); --i >= 0;) {
2241 * We can't use unsigned char here because of old compilers, e.g. V7.
2246 /* Adjust checksum to count the "chksum" field as blanks. */
2247 for (i = sizeof(header->header.chksum); --i >= 0;)
2248 sum -= 0xFF & header->header.chksum[i];
2249 sum += ' ' * sizeof header->header.chksum;
2252 return 0; /* Not a tar archive */
2254 if (0 == strcmp(header->header.magic, TMAGIC))
2255 return 2; /* Unix Standard tar archive */
2257 return 1; /* Old fashioned tar archive */
2262 * Quick and dirty octal conversion.
2264 * Result is -1 if the field is invalid (all blank, or nonoctal).
2266 static long from_oct(int digs, char *where)
2268 register long value;
2270 while (ap_isspace(*where)) { /* Skip spaces */
2273 return -1; /* All blank field */
2276 while (digs > 0 && isodigit(*where)) { /* Scan til nonoctal */
2277 value = (value << 3) | (*where++ - '0');
2281 if (digs > 0 && *where && !ap_isspace(*where))
2282 return -1; /* Ended on non-space/nul */
2288 * Check for file-revision suffix
2290 * This is for an obscure document control system used on an intranet.
2291 * The web representation of each file's revision has an @1, @2, etc
2292 * appended with the revision number. This needs to be stripped off to
2293 * find the file suffix, which can be recognized by sending the name back
2294 * through a sub-request. The base file name (without the @num suffix)
2295 * must exist because its type will be used as the result.
2297 static int revision_suffix(request_rec *r)
2299 int suffix_pos, result;
2303 #if MIME_MAGIC_DEBUG
2304 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
2305 MODNAME ": revision_suffix checking %s", r->filename);
2306 #endif /* MIME_MAGIC_DEBUG */
2308 /* check for recognized revision suffix */
2309 suffix_pos = strlen(r->filename) - 1;
2310 if (!ap_isdigit(r->filename[suffix_pos])) {
2313 while (suffix_pos >= 0 && ap_isdigit(r->filename[suffix_pos]))
2315 if (suffix_pos < 0 || r->filename[suffix_pos] != '@') {
2319 /* perform sub-request for the file name without the suffix */
2321 sub_filename = ap_pstrndup(r->pool, r->filename, suffix_pos);
2322 #if MIME_MAGIC_DEBUG
2323 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
2324 MODNAME ": subrequest lookup for %s", sub_filename);
2325 #endif /* MIME_MAGIC_DEBUG */
2326 sub = ap_sub_req_lookup_file(sub_filename, r);
2328 /* extract content type/encoding/language from sub-request */
2329 if (sub->content_type) {
2330 r->content_type = ap_pstrdup(r->pool, sub->content_type);
2331 #if MIME_MAGIC_DEBUG
2332 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 0, r,
2333 MODNAME ": subrequest %s got %s",
2334 sub_filename, r->content_type);
2335 #endif /* MIME_MAGIC_DEBUG */
2336 if (sub->content_encoding)
2337 r->content_encoding =
2338 ap_pstrdup(r->pool, sub->content_encoding);
2339 if (sub->content_language)
2340 r->content_language =
2341 ap_pstrdup(r->pool, sub->content_language);
2346 ap_destroy_sub_req(sub);
2352 * initialize the module
2355 static void magic_init(server_rec *main_server, ap_context_t *p)
2358 magic_server_config_rec *conf;
2359 magic_server_config_rec *main_conf;
2361 #if MIME_MAGIC_DEBUG
2362 struct magic *m, *prevm;
2363 #endif /* MIME_MAGIC_DEBUG */
2365 main_conf = ap_get_module_config(main_server->module_config, &mime_magic_module);
2366 for (s = main_server; s; s = s->next) {
2367 conf = ap_get_module_config(s->module_config, &mime_magic_module);
2368 if (conf->magicfile == NULL && s != main_server) {
2369 /* inherits from the parent */
2372 else if (conf->magicfile) {
2373 result = apprentice(s, p);
2376 #if MIME_MAGIC_DEBUG
2378 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, s,
2379 MODNAME ": magic_init 1 test");
2380 for (m = conf->magic; m; m = m->next) {
2381 if (ap_isprint((((unsigned long) m) >> 24) & 255) &&
2382 ap_isprint((((unsigned long) m) >> 16) & 255) &&
2383 ap_isprint((((unsigned long) m) >> 8) & 255) &&
2384 ap_isprint(((unsigned long) m) & 255)) {
2385 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, s,
2386 MODNAME ": magic_init 1: POINTER CLOBBERED! "
2387 "m=\"%c%c%c%c\" line=%d",
2388 (((unsigned long) m) >> 24) & 255,
2389 (((unsigned long) m) >> 16) & 255,
2390 (((unsigned long) m) >> 8) & 255,
2391 ((unsigned long) m) & 255,
2392 prevm ? prevm->lineno : -1);
2403 * Find the Content-Type from any resource this module has available
2406 static int magic_find_ct(request_rec *r)
2409 magic_server_config_rec *conf;
2411 /* the file has to exist */
2412 if (r->finfo.st_mode == 0 || !r->filename) {
2416 /* was someone else already here? */
2417 if (r->content_type) {
2421 conf = ap_get_module_config(r->server->module_config, &mime_magic_module);
2422 if (!conf || !conf->magic) {
2426 /* initialize per-request info */
2427 if (!magic_set_config(r)) {
2428 return HTTP_INTERNAL_SERVER_ERROR;
2431 /* try excluding file-revision suffixes */
2432 if (revision_suffix(r) != 1) {
2433 /* process it based on the file contents */
2434 if ((result = magic_process(r)) != OK) {
2439 /* if we have any results, put them in the request structure */
2440 return magic_rsl_to_request(r);
2444 * Apache API module interface
2447 module mime_magic_module =
2449 STANDARD_MODULE_STUFF,
2450 magic_init, /* initializer */
2451 NULL, /* dir config creator */
2452 NULL, /* dir merger --- default is to override */
2453 create_magic_server_config, /* server config */
2454 merge_magic_server_config, /* merge server config */
2455 mime_magic_cmds, /* command ap_table_t */
2456 NULL, /* handlers */
2457 NULL, /* filename translation */
2458 NULL, /* check_user_id */
2459 NULL, /* check auth */
2460 NULL, /* check access */
2461 magic_find_ct, /* type_checker */
2464 NULL, /* header parser */
2465 NULL, /* child_init */
2466 NULL, /* child_exit */
2467 NULL /* post read-request */