1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * mod_mime_magic: MIME type lookup via file magic numbers
19 * Copyright (c) 1996-1997 Cisco Systems, Inc.
21 * This software was submitted by Cisco Systems to the Apache Software Foundation in July
22 * 1997. Future revisions and derivatives of this source code must
23 * acknowledge Cisco Systems as the original contributor of this module.
24 * All other licensing and usage conditions are those of the Apache Software Foundation.
26 * Some of this code is derived from the free version of the file command
27 * originally posted to comp.sources.unix. Copyright info for that program
28 * is included below as required.
29 * ---------------------------------------------------------------------------
30 * - Copyright (c) Ian F. Darwin, 1987. Written by Ian F. Darwin.
32 * This software is not subject to any license of the American Telephone and
33 * Telegraph Company or of the Regents of the University of California.
35 * Permission is granted to anyone to use this software for any purpose on any
36 * computer system, and to alter it and redistribute it freely, subject to
37 * the following restrictions:
39 * 1. The author is not responsible for the consequences of use of this
40 * software, no matter how awful, even if they arise from flaws in it.
42 * 2. The origin of this software must not be misrepresented, either by
43 * explicit claim or by omission. Since few users ever read sources, credits
44 * must appear in the documentation.
46 * 3. Altered versions must be plainly marked as such, and must not be
47 * misrepresented as being the original software. Since few users ever read
48 * sources, credits must appear in the documentation.
50 * 4. This notice may not be removed or altered.
51 * -------------------------------------------------------------------------
53 * For compliance with Mr Darwin's terms: this has been very significantly
54 * modified from the free "file" command.
55 * - all-in-one file for compilation convenience when moving from one
56 * version of Apache to the next.
57 * - Memory allocation is done through the Apache API's apr_pool_t structure.
58 * - All functions have had necessary Apache API request or server
59 * structures passed to them where necessary to call other Apache API
60 * routines. (i.e. usually for logging, files, or memory allocation in
61 * itself or a called function.)
62 * - struct magic has been converted from an array to a single-ended linked
63 * list because it only grows one record at a time, it's only accessed
64 * sequentially, and the Apache API has no equivalent of realloc().
65 * - Functions have been changed to get their parameters from the server
66 * configuration instead of globals. (It should be reentrant now but has
67 * not been tested in a threaded environment.)
68 * - Places where it used to print results to stdout now saves them in a
69 * list where they're used to set the MIME type in the Apache request
71 * - Command-line flags have been removed since they will never be used here.
73 * Ian Kluft <ikluft@cisco.com>
74 * Engineering Information Framework
79 * Initial installation July/August 1996
80 * Misc bug fixes May 1997
81 * Submission to Apache Software Foundation July 1997
86 #include "apr_strings.h"
88 #define APR_WANT_STRFUNC
95 #include "ap_config.h"
97 #include "http_config.h"
98 #include "http_request.h"
99 #include "http_core.h"
100 #include "http_log.h"
101 #include "http_protocol.h"
102 #include "util_script.h"
104 /* ### this isn't set by configure? does anybody set this? */
110 * data structures and related constants
113 #define MODNAME "mod_mime_magic"
114 #define MIME_MAGIC_DEBUG 0
116 #define MIME_BINARY_UNKNOWN "application/octet-stream"
117 #define MIME_TEXT_UNKNOWN "text/plain"
119 #define MAXMIMESTRING 256
121 /* HOWMANY must be at least 4096 to make gzip -dcq work */
123 /* SMALL_HOWMANY limits how much work we do to figure out text files */
124 #define SMALL_HOWMANY 1024
125 #define MAXDESC 50 /* max leng of text description */
126 #define MAXstring 64 /* max leng of "string" types */
129 struct magic *next; /* link to next entry */
130 int lineno; /* line number from magic file */
133 #define INDIR 1 /* if '>(...)' appears, */
134 #define UNSIGNED 2 /* comparison is unsigned */
135 short cont_level; /* level of ">" */
137 char type; /* byte short long */
138 long offset; /* offset from indirection */
140 long offset; /* offset to magic number */
141 unsigned char reln; /* relation (0=eq, '>'=gt, etc) */
142 char type; /* int, short, long or string. */
143 char vallen; /* length of string value, if any */
160 unsigned char hs[2]; /* 2 bytes of a fixed-endian "short" */
161 unsigned char hl[4]; /* 2 bytes of a fixed-endian "long" */
162 } value; /* either number or string */
163 unsigned long mask; /* mask before comparison with value */
164 char nospflag; /* supress space character */
166 /* NOTE: this string is suspected of overrunning - find it! */
167 char desc[MAXDESC]; /* description */
171 * data structures for tar file recognition
172 * --------------------------------------------------------------------------
173 * Header file for public domain tar (tape archive) program.
175 * @(#)tar.h 1.20 86/10/29 Public Domain. Created 25 August 1985 by John
176 * Gilmore, ihnp4!hoptoad!gnu.
178 * Header block on tape.
180 * I'm going to use traditional DP naming conventions here. A "block" is a big
181 * chunk of stuff that we do I/O on. A "record" is a piece of info that we
182 * care about. Typically many "record"s fit into a "block".
184 #define RECORDSIZE 512
190 char charptr[RECORDSIZE];
200 char linkname[NAMSIZ];
209 /* The magic field is filled with this if uname and gname are valid. */
210 #define TMAGIC "ustar " /* 7 chars and a null */
213 * file-function prototypes
215 static int ascmagic(request_rec *, unsigned char *, apr_size_t);
216 static int is_tar(unsigned char *, apr_size_t);
217 static int softmagic(request_rec *, unsigned char *, apr_size_t);
218 static int tryit(request_rec *, unsigned char *, apr_size_t, int);
219 static int zmagic(request_rec *, unsigned char *, apr_size_t);
221 static int getvalue(server_rec *, struct magic *, char **);
222 static int hextoint(int);
223 static char *getstr(server_rec *, char *, char *, int, int *);
224 static int parse(server_rec *, apr_pool_t *p, char *, int);
226 static int match(request_rec *, unsigned char *, apr_size_t);
227 static int mget(request_rec *, union VALUETYPE *, unsigned char *,
228 struct magic *, apr_size_t);
229 static int mcheck(request_rec *, union VALUETYPE *, struct magic *);
230 static void mprint(request_rec *, union VALUETYPE *, struct magic *);
232 static int uncompress(request_rec *, int,
233 unsigned char **, apr_size_t);
234 static long from_oct(int, char *);
235 static int fsmagic(request_rec *r, const char *fn);
238 * includes for ASCII substring recognition formerly "names.h" in file
241 * Original notes: names and types used by ascmagic in file(1). These tokens are
242 * here because they can appear anywhere in the first HOWMANY bytes, while
243 * tokens in /etc/magic must appear at fixed offsets into the file. Don't
244 * make HOWMANY too high unless you have a very fast CPU.
247 /* these types are used to index the apr_table_t 'types': keep em in sync! */
248 /* HTML inserted in first because this is a web server module now */
249 #define L_HTML 0 /* HTML */
250 #define L_C 1 /* first and foremost on UNIX */
251 #define L_FORT 2 /* the oldest one */
252 #define L_MAKE 3 /* Makefiles */
253 #define L_PLI 4 /* PL/1 */
254 #define L_MACH 5 /* some kinda assembler */
255 #define L_ENG 6 /* English */
256 #define L_PAS 7 /* Pascal */
257 #define L_MAIL 8 /* Electronic mail */
258 #define L_NEWS 9 /* Usenet Netnews */
260 static const char *types[] =
262 "text/html", /* HTML */
263 "text/plain", /* "c program text", */
264 "text/plain", /* "fortran program text", */
265 "text/plain", /* "make commands text", */
266 "text/plain", /* "pl/1 program text", */
267 "text/plain", /* "assembler program text", */
268 "text/plain", /* "English text", */
269 "text/plain", /* "pascal program text", */
270 "message/rfc822", /* "mail text", */
271 "message/news", /* "news text", */
272 "application/binary", /* "can't happen error on names.h/types", */
276 static const struct names {
281 /* These must be sorted by eye for optimal hit rate */
282 /* Add to this list only after substantial meditation */
311 "<!DOCTYPE HTML", L_HTML
315 }, /* must precede "The", "the", etc. */
359 * Too many files of text have these words in them. Find another way to
360 * recognize Fortrash.
416 "Return-Path:", L_MAIL
422 "Newsgroups:", L_NEWS
428 "Organization:", L_NEWS
435 #define NNAMES ((sizeof(names)/sizeof(struct names)) - 1)
438 * Result String List (RSL)
440 * The file(1) command prints its output. Instead, we store the various
441 * "printed" strings in a list (allocating memory as we go) and concatenate
442 * them at the end when we finally know how much space they'll need.
445 typedef struct magic_rsl_s {
446 const char *str; /* string, possibly a fragment */
447 struct magic_rsl_s *next; /* pointer to next fragment */
451 * Apache module configuration structures
454 /* per-server info */
456 const char *magicfile; /* where magic be found */
457 struct magic *magic; /* head of magic config list */
459 } magic_server_config_rec;
461 /* per-request info */
463 magic_rsl *head; /* result string list */
465 unsigned suf_recursion; /* recursion depth in suffix check */
469 * configuration functions - called by Apache API routines
472 module AP_MODULE_DECLARE_DATA mime_magic_module;
474 static void *create_magic_server_config(apr_pool_t *p, server_rec *d)
476 /* allocate the config - use pcalloc because it needs to be zeroed */
477 return apr_pcalloc(p, sizeof(magic_server_config_rec));
480 static void *merge_magic_server_config(apr_pool_t *p, void *basev, void *addv)
482 magic_server_config_rec *base = (magic_server_config_rec *) basev;
483 magic_server_config_rec *add = (magic_server_config_rec *) addv;
484 magic_server_config_rec *new = (magic_server_config_rec *)
485 apr_palloc(p, sizeof(magic_server_config_rec));
487 new->magicfile = add->magicfile ? add->magicfile : base->magicfile;
493 static const char *set_magicfile(cmd_parms *cmd, void *dummy, const char *arg)
495 magic_server_config_rec *conf = (magic_server_config_rec *)
496 ap_get_module_config(cmd->server->module_config,
500 return MODNAME ": server structure not allocated";
502 conf->magicfile = arg;
507 * configuration file commands - exported to Apache API
510 static const command_rec mime_magic_cmds[] =
512 AP_INIT_TAKE1("MimeMagicFile", set_magicfile, NULL, RSRC_CONF,
513 "Path to MIME Magic file (in file(1) format)"),
518 * RSL (result string list) processing routines
520 * These collect strings that would have been printed in fragments by file(1)
521 * into a list of magic_rsl structures with the strings. When complete,
522 * they're concatenated together to become the MIME content and encoding
525 * return value conventions for these functions: functions which return int:
526 * failure = -1, other = result functions which return pointers: failure = 0,
530 /* allocate a per-request structure and put it in the request record */
531 static magic_req_rec *magic_set_config(request_rec *r)
533 magic_req_rec *req_dat = (magic_req_rec *) apr_palloc(r->pool,
534 sizeof(magic_req_rec));
536 req_dat->head = req_dat->tail = (magic_rsl *) NULL;
537 ap_set_module_config(r->request_config, &mime_magic_module, req_dat);
541 /* add a string to the result string list for this request */
542 /* it is the responsibility of the caller to allocate "str" */
543 static int magic_rsl_add(request_rec *r, const char *str)
545 magic_req_rec *req_dat = (magic_req_rec *)
546 ap_get_module_config(r->request_config, &mime_magic_module);
549 /* make sure we have a list to put it in */
551 ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_EINVAL, r,
552 MODNAME ": request config should not be NULL");
553 if (!(req_dat = magic_set_config(r))) {
559 /* allocate the list entry */
560 rsl = (magic_rsl *) apr_palloc(r->pool, sizeof(magic_rsl));
564 rsl->next = (magic_rsl *) NULL;
566 /* append to the list */
567 if (req_dat->head && req_dat->tail) {
568 req_dat->tail->next = rsl;
572 req_dat->head = req_dat->tail = rsl;
579 /* RSL hook for puts-type functions */
580 static int magic_rsl_puts(request_rec *r, const char *str)
582 return magic_rsl_add(r, str);
585 /* RSL hook for printf-type functions */
586 static int magic_rsl_printf(request_rec *r, char *str,...)
590 char buf[MAXMIMESTRING];
592 /* assemble the string into the buffer */
594 apr_vsnprintf(buf, sizeof(buf), str, ap);
597 /* add the buffer to the list */
598 return magic_rsl_add(r, apr_pstrdup(r->pool, buf));
601 /* RSL hook for putchar-type functions */
602 static int magic_rsl_putchar(request_rec *r, char c)
606 /* high overhead for 1 char - just hope they don't do this much */
609 return magic_rsl_add(r, str);
612 /* allocate and copy a contiguous string from a result string list */
613 static char *rsl_strdup(request_rec *r, int start_frag, int start_pos, int len)
615 char *result; /* return value */
616 int cur_frag, /* current fragment number/counter */
617 cur_pos, /* current position within fragment */
618 res_pos; /* position in result string */
619 magic_rsl *frag; /* list-traversal pointer */
620 magic_req_rec *req_dat = (magic_req_rec *)
621 ap_get_module_config(r->request_config, &mime_magic_module);
623 /* allocate the result string */
624 result = (char *) apr_palloc(r->pool, len + 1);
626 /* loop through and collect the string */
628 for (frag = req_dat->head, cur_frag = 0;
630 frag = frag->next, cur_frag++) {
631 /* loop to the first fragment */
632 if (cur_frag < start_frag)
635 /* loop through and collect chars */
636 for (cur_pos = (cur_frag == start_frag) ? start_pos : 0;
639 if (cur_frag >= start_frag
640 && cur_pos >= start_pos
642 result[res_pos++] = frag->str[cur_pos];
650 /* clean up and return */
653 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
654 MODNAME ": rsl_strdup() %d chars: %s", res_pos - 1, result);
659 /* states for the state-machine algorithm in magic_rsl_to_request() */
661 rsl_leading_space, rsl_type, rsl_subtype, rsl_separator, rsl_encoding
664 /* process the RSL and set the MIME info in the request record */
665 static int magic_rsl_to_request(request_rec *r)
667 int cur_frag, /* current fragment number/counter */
668 cur_pos, /* current position within fragment */
669 type_frag, /* content type starting point: fragment */
670 type_pos, /* content type starting point: position */
671 type_len, /* content type length */
672 encoding_frag, /* content encoding starting point: fragment */
673 encoding_pos, /* content encoding starting point: position */
674 encoding_len; /* content encoding length */
676 magic_rsl *frag; /* list-traversal pointer */
679 magic_req_rec *req_dat = (magic_req_rec *)
680 ap_get_module_config(r->request_config, &mime_magic_module);
682 /* check if we have a result */
683 if (!req_dat || !req_dat->head) {
684 /* empty - no match, we defer to other Apache modules */
688 /* start searching for the type and encoding */
689 state = rsl_leading_space;
690 type_frag = type_pos = type_len = 0;
691 encoding_frag = encoding_pos = encoding_len = 0;
692 for (frag = req_dat->head, cur_frag = 0;
694 frag = frag->next, cur_frag++) {
695 /* loop through the characters in the fragment */
696 for (cur_pos = 0; frag->str[cur_pos]; cur_pos++) {
697 if (apr_isspace(frag->str[cur_pos])) {
698 /* process whitespace actions for each state */
699 if (state == rsl_leading_space) {
700 /* eat whitespace in this state */
703 else if (state == rsl_type) {
704 /* whitespace: type has no slash! */
707 else if (state == rsl_subtype) {
708 /* whitespace: end of MIME type */
712 else if (state == rsl_separator) {
713 /* eat whitespace in this state */
716 else if (state == rsl_encoding) {
717 /* whitespace: end of MIME encoding */
719 frag = req_dat->tail;
723 /* should not be possible */
724 /* abandon malfunctioning module */
725 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
726 MODNAME ": bad state %d (ws)", state);
731 else if (state == rsl_type &&
732 frag->str[cur_pos] == '/') {
733 /* copy the char and go to rsl_subtype state */
738 /* process non-space actions for each state */
739 if (state == rsl_leading_space) {
740 /* non-space: begin MIME type */
742 type_frag = cur_frag;
747 else if (state == rsl_type ||
748 state == rsl_subtype) {
749 /* non-space: adds to type */
753 else if (state == rsl_separator) {
754 /* non-space: begin MIME encoding */
756 encoding_frag = cur_frag;
757 encoding_pos = cur_pos;
761 else if (state == rsl_encoding) {
762 /* non-space: adds to encoding */
767 /* should not be possible */
768 /* abandon malfunctioning module */
769 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
770 MODNAME ": bad state %d (ns)", state);
779 /* if we ended prior to state rsl_subtype, we had incomplete info */
780 if (state != rsl_subtype && state != rsl_separator &&
781 state != rsl_encoding) {
782 /* defer to other modules */
786 /* save the info in the request record */
787 if (state == rsl_subtype || state == rsl_encoding ||
788 state == rsl_encoding) {
790 tmp = rsl_strdup(r, type_frag, type_pos, type_len);
791 /* XXX: this could be done at config time I'm sure... but I'm
792 * confused by all this magic_rsl stuff. -djg */
793 ap_content_type_tolower(tmp);
794 ap_set_content_type(r, tmp);
796 if (state == rsl_encoding) {
798 tmp = rsl_strdup(r, encoding_frag,
799 encoding_pos, encoding_len);
800 /* XXX: this could be done at config time I'm sure... but I'm
801 * confused by all this magic_rsl stuff. -djg */
803 r->content_encoding = tmp;
806 /* detect memory allocation or other errors */
807 if (!r->content_type ||
808 (state == rsl_encoding && !r->content_encoding)) {
809 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
810 MODNAME ": unexpected state %d; could be caused by bad "
811 "data in magic file",
813 return HTTP_INTERNAL_SERVER_ERROR;
821 * magic_process - process input file r Apache API request record
822 * (formerly called "process" in file command, prefix added for clarity) Opens
823 * the file and reads a fixed-size buffer to begin processing the contents.
825 static int magic_process(request_rec *r)
827 apr_file_t *fd = NULL;
828 unsigned char buf[HOWMANY + 1]; /* one extra for terminating '\0' */
829 apr_size_t nbytes = 0; /* number of bytes read from a datafile */
833 * first try judging the file based on its filesystem status
835 switch ((result = fsmagic(r, r->filename))) {
837 magic_rsl_putchar(r, '\n');
842 /* fatal error, bail out */
846 if (apr_file_open(&fd, r->filename, APR_READ, APR_OS_DEFAULT, r->pool) != APR_SUCCESS) {
847 /* We can't open it, but we were able to stat it. */
848 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
849 MODNAME ": can't read `%s'", r->filename);
850 /* let some other handler decide what the problem is */
855 * try looking at the first HOWMANY bytes
857 nbytes = sizeof(buf) - 1;
858 if ((result = apr_file_read(fd, (char *) buf, &nbytes)) != APR_SUCCESS) {
859 ap_log_rerror(APLOG_MARK, APLOG_ERR, result, r,
860 MODNAME ": read failed: %s", r->filename);
861 return HTTP_INTERNAL_SERVER_ERROR;
868 buf[nbytes++] = '\0'; /* null-terminate it */
869 result = tryit(r, buf, nbytes, 1);
875 (void) apr_file_close(fd);
876 (void) magic_rsl_putchar(r, '\n');
882 static int tryit(request_rec *r, unsigned char *buf, apr_size_t nb,
886 * Try compression stuff
888 if (checkzmagic == 1) {
889 if (zmagic(r, buf, nb) == 1)
894 * try tests in /etc/magic (or surrogate magic file)
896 if (softmagic(r, buf, nb) == 1)
900 * try known keywords, check for ascii-ness too.
902 if (ascmagic(r, buf, nb) == 1)
906 * abandon hope, all ye who remain here
911 #define EATAB {while (apr_isspace(*l)) ++l;}
914 * apprentice - load configuration from the magic file r
917 static int apprentice(server_rec *s, apr_pool_t *p)
919 apr_file_t *f = NULL;
921 char line[BUFSIZ + 1];
926 struct magic *m, *prevm;
928 magic_server_config_rec *conf = (magic_server_config_rec *)
929 ap_get_module_config(s->module_config, &mime_magic_module);
930 const char *fname = ap_server_root_relative(p, conf->magicfile);
933 ap_log_error(APLOG_MARK, APLOG_ERR, APR_EBADPATH, s,
934 MODNAME ": Invalid magic file path %s", conf->magicfile);
937 if ((result = apr_file_open(&f, fname, APR_READ | APR_BUFFERED,
938 APR_OS_DEFAULT, p)) != APR_SUCCESS) {
939 ap_log_error(APLOG_MARK, APLOG_ERR, result, s,
940 MODNAME ": can't read magic file %s", fname);
944 /* set up the magic list (empty) */
945 conf->magic = conf->last = NULL;
948 for (lineno = 1; apr_file_gets(line, BUFSIZ, f) == APR_SUCCESS; lineno++) {
950 char *last = line + strlen(line) - 1; /* guaranteed that len >= 1 since an
951 * "empty" line contains a '\n'
954 /* delete newline and any other trailing whitespace */
956 && apr_isspace(*last)) {
961 /* skip leading whitespace */
963 while (line[ws_offset] && apr_isspace(line[ws_offset])) {
967 /* skip blank lines */
968 if (line[ws_offset] == 0) {
972 /* comment, do not parse */
973 if (line[ws_offset] == '#')
977 /* if we get here, we're going to use it so count it */
982 if (parse(s, p, line + ws_offset, lineno) != 0)
986 (void) apr_file_close(f);
989 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
990 MODNAME ": apprentice conf=%x file=%s m=%s m->next=%s last=%s",
992 conf->magicfile ? conf->magicfile : "NULL",
993 conf->magic ? "set" : "NULL",
994 (conf->magic && conf->magic->next) ? "set" : "NULL",
995 conf->last ? "set" : "NULL");
996 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
997 MODNAME ": apprentice read %d lines, %d rules, %d errors",
1001 #if MIME_MAGIC_DEBUG
1003 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1004 MODNAME ": apprentice test");
1005 for (m = conf->magic; m; m = m->next) {
1006 if (apr_isprint((((unsigned long) m) >> 24) & 255) &&
1007 apr_isprint((((unsigned long) m) >> 16) & 255) &&
1008 apr_isprint((((unsigned long) m) >> 8) & 255) &&
1009 apr_isprint(((unsigned long) m) & 255)) {
1010 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1011 MODNAME ": apprentice: POINTER CLOBBERED! "
1012 "m=\"%c%c%c%c\" line=%d",
1013 (((unsigned long) m) >> 24) & 255,
1014 (((unsigned long) m) >> 16) & 255,
1015 (((unsigned long) m) >> 8) & 255,
1016 ((unsigned long) m) & 255,
1017 prevm ? prevm->lineno : -1);
1024 return (errs ? -1 : 0);
1028 * extend the sign bit if the comparison is to be signed
1030 static unsigned long signextend(server_rec *s, struct magic *m, unsigned long v)
1032 if (!(m->flag & UNSIGNED))
1035 * Do not remove the casts below. They are vital. When later
1036 * compared with the data, the sign extension must have happened.
1057 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
1058 MODNAME ": can't happen: m->type=%d", m->type);
1065 * parse one line from magic file, put into magic[index++] if valid
1067 static int parse(server_rec *serv, apr_pool_t *p, char *l, int lineno)
1071 magic_server_config_rec *conf = (magic_server_config_rec *)
1072 ap_get_module_config(serv->module_config, &mime_magic_module);
1074 /* allocate magic structure entry */
1075 m = (struct magic *) apr_pcalloc(p, sizeof(struct magic));
1077 /* append to linked list */
1079 if (!conf->magic || !conf->last) {
1080 conf->magic = conf->last = m;
1083 conf->last->next = m;
1087 /* set values in magic structure */
1093 ++l; /* step over */
1097 if (m->cont_level != 0 && *l == '(') {
1098 ++l; /* step over */
1102 /* get offset, then skip over it */
1103 m->offset = (int) strtol(l, &t, 0);
1105 ap_log_error(APLOG_MARK, APLOG_ERR, 0, serv,
1106 MODNAME ": offset %s invalid", l);
1110 if (m->flag & INDIR) {
1114 * read [.lbs][+-]nnnnn)
1128 ap_log_error(APLOG_MARK, APLOG_ERR, 0, serv,
1129 MODNAME ": indirect offset type %c invalid", *l);
1135 if (*l == '+' || *l == '-')
1137 if (apr_isdigit((unsigned char) *l)) {
1138 m->in.offset = strtol(l, &t, 0);
1140 m->in.offset = -m->in.offset;
1145 ap_log_error(APLOG_MARK, APLOG_ERR, 0, serv,
1146 MODNAME ": missing ')' in indirect offset");
1152 while (apr_isdigit((unsigned char) *l))
1170 m->flag |= UNSIGNED;
1173 /* get type, skip it */
1174 if (strncmp(l, "byte", NBYTE) == 0) {
1178 else if (strncmp(l, "short", NSHORT) == 0) {
1182 else if (strncmp(l, "long", NLONG) == 0) {
1186 else if (strncmp(l, "string", NSTRING) == 0) {
1190 else if (strncmp(l, "date", NDATE) == 0) {
1194 else if (strncmp(l, "beshort", NBESHORT) == 0) {
1198 else if (strncmp(l, "belong", NBELONG) == 0) {
1202 else if (strncmp(l, "bedate", NBEDATE) == 0) {
1206 else if (strncmp(l, "leshort", NLESHORT) == 0) {
1210 else if (strncmp(l, "lelong", NLELONG) == 0) {
1214 else if (strncmp(l, "ledate", NLEDATE) == 0) {
1219 ap_log_error(APLOG_MARK, APLOG_ERR, 0, serv,
1220 MODNAME ": type %s invalid", l);
1223 /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
1226 m->mask = signextend(serv, m, strtol(l, &l, 0));
1235 /* Old-style anding: "0 byte &0x80 dynamically linked" */
1243 if (m->type != STRING) {
1250 if (*l == 'x' && apr_isspace(l[1])) {
1253 goto GetDesc; /* Bill The Cat */
1260 if (getvalue(serv, m, &l))
1263 * now get last part - the description
1271 else if ((l[0] == '\\') && (l[1] == 'b')) {
1278 strncpy(m->desc, l, sizeof(m->desc) - 1);
1279 m->desc[sizeof(m->desc) - 1] = '\0';
1281 #if MIME_MAGIC_DEBUG
1282 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, serv,
1283 MODNAME ": parse line=%d m=%x next=%x cont=%d desc=%s",
1284 lineno, m, m->next, m->cont_level, m->desc);
1285 #endif /* MIME_MAGIC_DEBUG */
1291 * Read a numeric value from a pointer, into the value union of a magic
1292 * pointer, according to the magic type. Update the string pointer to point
1293 * just after the number read. Return 0 for success, non-zero for failure.
1295 static int getvalue(server_rec *s, struct magic *m, char **p)
1299 if (m->type == STRING) {
1300 *p = getstr(s, *p, m->value.s, sizeof(m->value.s), &slen);
1303 else if (m->reln != 'x')
1304 m->value.l = signextend(s, m, strtol(*p, p, 0));
1309 * Convert a string containing C character escapes. Stop at an unescaped
1310 * space or tab. Copy the converted version to "p", returning its length in
1311 * *slen. Return updated scan pointer as function result.
1313 static char *getstr(server_rec *serv, register char *s, register char *p,
1314 int plen, int *slen)
1316 char *origs = s, *origp = p;
1317 char *pmax = p + plen - 1;
1321 while ((c = *s++) != '\0') {
1325 ap_log_error(APLOG_MARK, APLOG_ERR, 0, serv,
1326 MODNAME ": string too long: %s", origs);
1363 /* \ and up to 3 octal digits */
1373 c = *s++; /* try for 2 */
1374 if (c >= '0' && c <= '7') {
1375 val = (val << 3) | (c - '0');
1376 c = *s++; /* try for 3 */
1377 if (c >= '0' && c <= '7')
1378 val = (val << 3) | (c - '0');
1387 /* \x and up to 3 hex digits */
1389 val = 'x'; /* Default if no digits */
1390 c = hextoint(*s++); /* Get next char */
1395 val = (val << 4) + c;
1398 val = (val << 4) + c;
1422 /* Single hex char to int; -1 if not a hex char. */
1423 static int hextoint(int c)
1427 if ((c >= 'a') && (c <= 'f'))
1428 return c + 10 - 'a';
1429 if ((c >= 'A') && (c <= 'F'))
1430 return c + 10 - 'A';
1436 * return DONE to indicate it's been handled
1437 * return OK to indicate it's a regular file still needing handling
1438 * other returns indicate a failure of some sort
1440 static int fsmagic(request_rec *r, const char *fn)
1442 switch (r->finfo.filetype) {
1444 magic_rsl_puts(r, DIR_MAGIC_TYPE);
1448 * (void) magic_rsl_printf(r,"character special (%d/%d)",
1449 * major(sb->st_rdev), minor(sb->st_rdev));
1451 (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
1455 * (void) magic_rsl_printf(r,"block special (%d/%d)",
1456 * major(sb->st_rdev), minor(sb->st_rdev));
1458 (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
1460 /* TODO add code to handle V7 MUX and Blit MUX files */
1463 * magic_rsl_puts(r,"fifo (named pipe)");
1465 (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
1468 /* We used stat(), the only possible reason for this is that the
1469 * symlink is broken.
1471 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1472 MODNAME ": broken symlink (%s)", fn);
1473 return HTTP_INTERNAL_SERVER_ERROR;
1475 magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
1480 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1481 MODNAME ": invalid file type %d.", r->finfo.filetype);
1482 return HTTP_INTERNAL_SERVER_ERROR;
1486 * regular file, check next possibility
1488 if (r->finfo.size == 0) {
1489 magic_rsl_puts(r, MIME_TEXT_UNKNOWN);
1496 * softmagic - lookup one file in database (already read from /etc/magic by
1497 * apprentice.c). Passed the name and FILE * of one file to be typed.
1499 /* ARGSUSED1 *//* nbytes passed for regularity, maybe need later */
1500 static int softmagic(request_rec *r, unsigned char *buf, apr_size_t nbytes)
1502 if (match(r, buf, nbytes))
1509 * Go through the whole list, stopping if you find a match. Process all the
1510 * continuations of that match before returning.
1512 * We support multi-level continuations:
1514 * At any time when processing a successful top-level match, there is a current
1515 * continuation level; it represents the level of the last successfully
1516 * matched continuation.
1518 * Continuations above that level are skipped as, if we see one, it means that
1519 * the continuation that controls them - i.e, the lower-level continuation
1520 * preceding them - failed to match.
1522 * Continuations below that level are processed as, if we see one, it means
1523 * we've finished processing or skipping higher-level continuations under the
1524 * control of a successful or unsuccessful lower-level continuation, and are
1525 * now seeing the next lower-level continuation and should process it. The
1526 * current continuation level reverts to the level of the one we're seeing.
1528 * Continuations at the current level are processed as, if we see one, there's
1529 * no lower-level continuation that may have failed.
1531 * If a continuation matches, we bump the current continuation level so that
1532 * higher-level continuations are processed.
1534 static int match(request_rec *r, unsigned char *s, apr_size_t nbytes)
1536 #if MIME_MAGIC_DEBUG
1537 int rule_counter = 0;
1540 int need_separator = 0;
1542 magic_server_config_rec *conf = (magic_server_config_rec *)
1543 ap_get_module_config(r->server->module_config, &mime_magic_module);
1546 #if MIME_MAGIC_DEBUG
1547 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1548 MODNAME ": match conf=%x file=%s m=%s m->next=%s last=%s",
1550 conf->magicfile ? conf->magicfile : "NULL",
1551 conf->magic ? "set" : "NULL",
1552 (conf->magic && conf->magic->next) ? "set" : "NULL",
1553 conf->last ? "set" : "NULL");
1556 #if MIME_MAGIC_DEBUG
1557 for (m = conf->magic; m; m = m->next) {
1558 if (apr_isprint((((unsigned long) m) >> 24) & 255) &&
1559 apr_isprint((((unsigned long) m) >> 16) & 255) &&
1560 apr_isprint((((unsigned long) m) >> 8) & 255) &&
1561 apr_isprint(((unsigned long) m) & 255)) {
1562 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1563 MODNAME ": match: POINTER CLOBBERED! "
1565 (((unsigned long) m) >> 24) & 255,
1566 (((unsigned long) m) >> 16) & 255,
1567 (((unsigned long) m) >> 8) & 255,
1568 ((unsigned long) m) & 255);
1574 for (m = conf->magic; m; m = m->next) {
1575 #if MIME_MAGIC_DEBUG
1577 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1578 MODNAME ": line=%d desc=%s", m->lineno, m->desc);
1581 /* check if main entry matches */
1582 if (!mget(r, &p, s, m, nbytes) ||
1583 !mcheck(r, &p, m)) {
1584 struct magic *m_cont;
1587 * main entry didn't match, flush its continuations
1589 if (!m->next || (m->next->cont_level == 0)) {
1594 while (m_cont && (m_cont->cont_level != 0)) {
1595 #if MIME_MAGIC_DEBUG
1597 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1598 MODNAME ": line=%d mc=%x mc->next=%x cont=%d desc=%s",
1599 m_cont->lineno, m_cont,
1600 m_cont->next, m_cont->cont_level,
1604 * this trick allows us to keep *m in sync when the continue
1605 * advances the pointer
1608 m_cont = m_cont->next;
1613 /* if we get here, the main entry rule was a match */
1614 /* this will be the last run through the loop */
1615 #if MIME_MAGIC_DEBUG
1616 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1617 MODNAME ": rule matched, line=%d type=%d %s",
1619 (m->type == STRING) ? m->value.s : "");
1622 /* print the match */
1626 * If we printed something, we'll need to print a blank before we
1627 * print something else.
1631 /* and any continuations that match */
1634 * while (m && m->next && m->next->cont_level != 0 && ( m = m->next
1638 while (m && (m->cont_level != 0)) {
1639 #if MIME_MAGIC_DEBUG
1640 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1641 MODNAME ": match line=%d cont=%d type=%d %s",
1642 m->lineno, m->cont_level, m->type,
1643 (m->type == STRING) ? m->value.s : "");
1645 if (cont_level >= m->cont_level) {
1646 if (cont_level > m->cont_level) {
1648 * We're at the end of the level "cont_level"
1651 cont_level = m->cont_level;
1653 if (mget(r, &p, s, m, nbytes) &&
1656 * This continuation matched. Print its message, with a
1657 * blank before it if the previous item printed and this
1660 /* space if previous printed */
1662 && (m->nospflag == 0)
1663 && (m->desc[0] != '\0')
1665 (void) magic_rsl_putchar(r, ' ');
1673 * If we see any continuations at a higher level, process
1680 /* move to next continuation record */
1683 #if MIME_MAGIC_DEBUG
1684 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1685 MODNAME ": matched after %d rules", rule_counter);
1687 return 1; /* all through */
1689 #if MIME_MAGIC_DEBUG
1690 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1691 MODNAME ": failed after %d rules", rule_counter);
1693 return 0; /* no match at all */
1696 static void mprint(request_rec *r, union VALUETYPE *p, struct magic *m)
1700 char time_str[APR_CTIME_LEN];
1720 if (m->reln == '=') {
1721 (void) magic_rsl_printf(r, m->desc, m->value.s);
1724 (void) magic_rsl_printf(r, m->desc, p->s);
1731 apr_ctime(time_str, apr_time_from_sec(*(time_t *)&p->l));
1733 (void) magic_rsl_printf(r, m->desc, pp);
1736 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1737 MODNAME ": invalid m->type (%d) in mprint().",
1742 v = signextend(r->server, m, v) & m->mask;
1743 (void) magic_rsl_printf(r, m->desc, (unsigned long) v);
1747 * Convert the byte order of the data we are looking at
1749 static int mconvert(request_rec *r, union VALUETYPE *p, struct magic *m)
1760 /* Null terminate and eat the return */
1761 p->s[sizeof(p->s) - 1] = '\0';
1762 if ((rt = strchr(p->s, '\n')) != NULL)
1766 p->h = (short) ((p->hs[0] << 8) | (p->hs[1]));
1771 ((p->hl[0] << 24) | (p->hl[1] << 16) | (p->hl[2] << 8) | (p->hl[3]));
1774 p->h = (short) ((p->hs[1] << 8) | (p->hs[0]));
1779 ((p->hl[3] << 24) | (p->hl[2] << 16) | (p->hl[1] << 8) | (p->hl[0]));
1782 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1783 MODNAME ": invalid type %d in mconvert().", m->type);
1789 static int mget(request_rec *r, union VALUETYPE *p, unsigned char *s,
1790 struct magic *m, apr_size_t nbytes)
1792 long offset = m->offset;
1794 if (offset + sizeof(union VALUETYPE) > nbytes)
1797 memcpy(p, s + offset, sizeof(union VALUETYPE));
1799 if (!mconvert(r, p, m))
1802 if (m->flag & INDIR) {
1804 switch (m->in.type) {
1806 offset = p->b + m->in.offset;
1809 offset = p->h + m->in.offset;
1812 offset = p->l + m->in.offset;
1816 if (offset + sizeof(union VALUETYPE) > nbytes)
1819 memcpy(p, s + offset, sizeof(union VALUETYPE));
1821 if (!mconvert(r, p, m))
1827 static int mcheck(request_rec *r, union VALUETYPE *p, struct magic *m)
1829 register unsigned long l = m->value.l;
1830 register unsigned long v;
1833 if ((m->value.s[0] == 'x') && (m->value.s[1] == '\0')) {
1834 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1862 * What we want here is: v = strncmp(m->value.s, p->s, m->vallen);
1863 * but ignoring any nulls. bcmp doesn't give -/+/0 and isn't
1864 * universally available anyway.
1868 register unsigned char *a = (unsigned char *) m->value.s;
1869 register unsigned char *b = (unsigned char *) p->s;
1870 register int len = m->vallen;
1873 if ((v = *b++ - *a++) != 0)
1878 /* bogosity, pretend that it just wasn't a match */
1879 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1880 MODNAME ": invalid type %d in mcheck().", m->type);
1884 v = signextend(r->server, m, v) & m->mask;
1888 #if MIME_MAGIC_DEBUG
1889 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1890 "%lu == *any* = 1", v);
1897 #if MIME_MAGIC_DEBUG
1898 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1899 "%lu != %lu = %d", v, l, matched);
1905 #if MIME_MAGIC_DEBUG
1906 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1907 "%lu == %lu = %d", v, l, matched);
1912 if (m->flag & UNSIGNED) {
1914 #if MIME_MAGIC_DEBUG
1915 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1916 "%lu > %lu = %d", v, l, matched);
1920 matched = (long) v > (long) l;
1921 #if MIME_MAGIC_DEBUG
1922 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1923 "%ld > %ld = %d", v, l, matched);
1929 if (m->flag & UNSIGNED) {
1931 #if MIME_MAGIC_DEBUG
1932 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1933 "%lu < %lu = %d", v, l, matched);
1937 matched = (long) v < (long) l;
1938 #if MIME_MAGIC_DEBUG
1939 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1940 "%ld < %ld = %d", v, l, matched);
1946 matched = (v & l) == l;
1947 #if MIME_MAGIC_DEBUG
1948 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1949 "((%lx & %lx) == %lx) = %d", v, l, l, matched);
1954 matched = (v & l) != l;
1955 #if MIME_MAGIC_DEBUG
1956 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
1957 "((%lx & %lx) != %lx) = %d", v, l, l, matched);
1962 /* bogosity, pretend it didn't match */
1964 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1965 MODNAME ": mcheck: can't happen: invalid relation %d.",
1973 /* an optimization over plain strcmp() */
1974 #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
1976 static int ascmagic(request_rec *r, unsigned char *buf, apr_size_t nbytes)
1978 int has_escapes = 0;
1980 char nbuf[HOWMANY + 1]; /* one extra for terminating '\0' */
1982 const struct names *p;
1986 /* these are easy, do them first */
1989 * for troff, look for . + letter + letter or .\"; this must be done to
1990 * disambiguate tar archives' ./file and other trash from real troff
1994 unsigned char *tp = buf + 1;
1996 while (apr_isspace(*tp))
1997 ++tp; /* skip leading whitespace */
1998 if ((apr_isalnum(*tp) || *tp == '\\') &&
1999 (apr_isalnum(*(tp + 1)) || *tp == '"')) {
2000 magic_rsl_puts(r, "application/x-troff");
2004 if ((*buf == 'c' || *buf == 'C') && apr_isspace(*(buf + 1))) {
2006 magic_rsl_puts(r, "text/plain");
2010 /* look for tokens from names.h - this is expensive!, so we'll limit
2011 * ourselves to only SMALL_HOWMANY bytes */
2012 small_nbytes = (nbytes > SMALL_HOWMANY) ? SMALL_HOWMANY : nbytes;
2013 /* make a copy of the buffer here because apr_strtok() will destroy it */
2014 s = (unsigned char *) memcpy(nbuf, buf, small_nbytes);
2015 s[small_nbytes] = '\0';
2016 has_escapes = (memchr(s, '\033', small_nbytes) != NULL);
2017 while ((token = apr_strtok((char *) s, " \t\n\r\f", &strtok_state)) != NULL) {
2018 s = NULL; /* make apr_strtok() keep on tokin' */
2019 for (p = names; p < names + NNAMES; p++) {
2020 if (STREQ(p->name, token)) {
2021 magic_rsl_puts(r, types[p->type]);
2023 magic_rsl_puts(r, " (with escape sequences)");
2029 switch (is_tar(buf, nbytes)) {
2031 /* V7 tar archive */
2032 magic_rsl_puts(r, "application/x-tar");
2035 /* POSIX tar archive */
2036 magic_rsl_puts(r, "application/x-tar");
2040 /* all else fails, but it is ascii... */
2046 * compress routines: zmagic() - returns 0 if not recognized, uncompresses
2047 * and prints information if recognized uncompress(s, method, old, n, newch)
2048 * - uncompress old into new, using method, return sizeof new
2056 char *encoding; /* MUST be lowercase */
2059 /* we use gzip here rather than uncompress because we have to pass
2060 * it a full filename -- and uncompress only considers filenames
2065 "gzip", "-dcq", NULL
2070 "gzip", "-dcq", NULL
2074 * XXX pcat does not work, cause I don't know how to make it read stdin,
2079 "gzip", "-dcq", NULL
2084 static int ncompr = sizeof(compr) / sizeof(compr[0]);
2086 static int zmagic(request_rec *r, unsigned char *buf, apr_size_t nbytes)
2088 unsigned char *newbuf;
2092 for (i = 0; i < ncompr; i++) {
2093 if (nbytes < compr[i].maglen)
2095 if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0)
2102 if ((newsize = uncompress(r, i, &newbuf, HOWMANY)) > 0) {
2103 /* set encoding type in the request record */
2104 r->content_encoding = compr[i].encoding;
2106 newbuf[newsize-1] = '\0'; /* null-terminate uncompressed data */
2107 /* Try to detect the content type of the uncompressed data */
2108 if (tryit(r, newbuf, newsize, 0) != OK) {
2116 struct uncompress_parms {
2121 static int create_uncompress_child(struct uncompress_parms *parm, apr_pool_t *cntxt,
2122 apr_file_t **pipe_in)
2125 const char *new_argv[4];
2126 request_rec *r = parm->r;
2127 apr_pool_t *child_context = cntxt;
2128 apr_procattr_t *procattr;
2129 apr_proc_t *procnew;
2131 /* XXX missing 1.3 logic:
2133 * what happens when !compr[parm->method].silent?
2134 * Should we create the err pipe, read it, and copy to the log?
2137 if ((apr_procattr_create(&procattr, child_context) != APR_SUCCESS) ||
2138 (apr_procattr_io_set(procattr, APR_FULL_BLOCK,
2139 APR_FULL_BLOCK, APR_NO_PIPE) != APR_SUCCESS) ||
2140 (apr_procattr_dir_set(procattr,
2141 ap_make_dirstr_parent(r->pool, r->filename)) != APR_SUCCESS) ||
2142 (apr_procattr_cmdtype_set(procattr, APR_PROGRAM_PATH) != APR_SUCCESS)) {
2143 /* Something bad happened, tell the world. */
2144 ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_ENOPROC, r,
2145 "couldn't setup child process: %s", r->filename);
2148 new_argv[0] = compr[parm->method].argv[0];
2149 new_argv[1] = compr[parm->method].argv[1];
2150 new_argv[2] = r->filename;
2153 procnew = apr_pcalloc(child_context, sizeof(*procnew));
2154 rc = apr_proc_create(procnew, compr[parm->method].argv[0],
2155 new_argv, NULL, procattr, child_context);
2157 if (rc != APR_SUCCESS) {
2158 /* Bad things happened. Everyone should have cleaned up. */
2159 ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_ENOPROC, r,
2160 MODNAME ": could not execute `%s'.",
2161 compr[parm->method].argv[0]);
2164 apr_pool_note_subprocess(child_context, procnew, APR_KILL_AFTER_TIMEOUT);
2165 *pipe_in = procnew->out;
2172 static int uncompress(request_rec *r, int method,
2173 unsigned char **newch, apr_size_t n)
2175 struct uncompress_parms parm;
2176 apr_file_t *pipe_out = NULL;
2177 apr_pool_t *sub_context;
2181 parm.method = method;
2183 /* We make a sub_pool so that we can collect our child early, otherwise
2184 * there are cases (i.e. generating directory indicies with mod_autoindex)
2185 * where we would end up with LOTS of zombies.
2187 if (apr_pool_create(&sub_context, r->pool) != APR_SUCCESS)
2190 if ((rv = create_uncompress_child(&parm, sub_context, &pipe_out)) != APR_SUCCESS) {
2191 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
2192 MODNAME ": couldn't spawn uncompress process: %s", r->uri);
2196 *newch = (unsigned char *) apr_palloc(r->pool, n);
2197 rv = apr_file_read(pipe_out, *newch, &n);
2199 apr_pool_destroy(sub_context);
2200 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
2201 MODNAME ": read failed from uncompress of %s", r->filename);
2204 apr_pool_destroy(sub_context);
2209 * is_tar() -- figure out whether file is a tar archive.
2211 * Stolen (by author of file utility) from the public domain tar program: Public
2212 * Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
2214 * @(#)list.c 1.18 9/23/86 Public Domain - gnu $Id: mod_mime_magic.c,v 1.7
2215 * 1997/06/24 00:41:02 ikluft Exp ikluft $
2217 * Comments changed and some code/comments reformatted for file command by Ian
2221 #define isodigit(c) (((unsigned char)(c) >= '0') && ((unsigned char)(c) <= '7'))
2224 * Return 0 if the checksum is bad (i.e., probably not a tar archive), 1 for
2225 * old UNIX tar file, 2 for Unix Std (POSIX) tar file.
2228 static int is_tar(unsigned char *buf, apr_size_t nbytes)
2230 register union record *header = (union record *) buf;
2232 register long sum, recsum;
2235 if (nbytes < sizeof(union record))
2238 recsum = from_oct(8, header->header.chksum);
2241 p = header->charptr;
2242 for (i = sizeof(union record); --i >= 0;) {
2244 * We can't use unsigned char here because of old compilers, e.g. V7.
2249 /* Adjust checksum to count the "chksum" field as blanks. */
2250 for (i = sizeof(header->header.chksum); --i >= 0;)
2251 sum -= 0xFF & header->header.chksum[i];
2252 sum += ' ' * sizeof header->header.chksum;
2255 return 0; /* Not a tar archive */
2257 if (0 == strcmp(header->header.magic, TMAGIC))
2258 return 2; /* Unix Standard tar archive */
2260 return 1; /* Old fashioned tar archive */
2265 * Quick and dirty octal conversion.
2267 * Result is -1 if the field is invalid (all blank, or nonoctal).
2269 static long from_oct(int digs, char *where)
2271 register long value;
2273 while (apr_isspace(*where)) { /* Skip spaces */
2276 return -1; /* All blank field */
2279 while (digs > 0 && isodigit(*where)) { /* Scan til nonoctal */
2280 value = (value << 3) | (*where++ - '0');
2284 if (digs > 0 && *where && !apr_isspace(*where))
2285 return -1; /* Ended on non-space/nul */
2291 * Check for file-revision suffix
2293 * This is for an obscure document control system used on an intranet.
2294 * The web representation of each file's revision has an @1, @2, etc
2295 * appended with the revision number. This needs to be stripped off to
2296 * find the file suffix, which can be recognized by sending the name back
2297 * through a sub-request. The base file name (without the @num suffix)
2298 * must exist because its type will be used as the result.
2300 static int revision_suffix(request_rec *r)
2302 int suffix_pos, result;
2306 #if MIME_MAGIC_DEBUG
2307 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2308 MODNAME ": revision_suffix checking %s", r->filename);
2309 #endif /* MIME_MAGIC_DEBUG */
2311 /* check for recognized revision suffix */
2312 suffix_pos = strlen(r->filename) - 1;
2313 if (!apr_isdigit(r->filename[suffix_pos])) {
2316 while (suffix_pos >= 0 && apr_isdigit(r->filename[suffix_pos]))
2318 if (suffix_pos < 0 || r->filename[suffix_pos] != '@') {
2322 /* perform sub-request for the file name without the suffix */
2324 sub_filename = apr_pstrndup(r->pool, r->filename, suffix_pos);
2325 #if MIME_MAGIC_DEBUG
2326 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2327 MODNAME ": subrequest lookup for %s", sub_filename);
2328 #endif /* MIME_MAGIC_DEBUG */
2329 sub = ap_sub_req_lookup_file(sub_filename, r, NULL);
2331 /* extract content type/encoding/language from sub-request */
2332 if (sub->content_type) {
2333 ap_set_content_type(r, apr_pstrdup(r->pool, sub->content_type));
2334 #if MIME_MAGIC_DEBUG
2335 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2336 MODNAME ": subrequest %s got %s",
2337 sub_filename, r->content_type);
2338 #endif /* MIME_MAGIC_DEBUG */
2339 if (sub->content_encoding)
2340 r->content_encoding =
2341 apr_pstrdup(r->pool, sub->content_encoding);
2342 if (sub->content_languages) {
2344 r->content_languages = apr_array_copy(r->pool,
2345 sub->content_languages);
2346 for (n = 0; n < r->content_languages->nelts; ++n) {
2347 char **lang = ((char **)r->content_languages->elts) + n;
2348 *lang = apr_pstrdup(r->pool, *lang);
2355 ap_destroy_sub_req(sub);
2361 * initialize the module
2363 static int magic_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *main_server)
2366 magic_server_config_rec *conf;
2367 magic_server_config_rec *main_conf;
2369 #if MIME_MAGIC_DEBUG
2370 struct magic *m, *prevm;
2371 #endif /* MIME_MAGIC_DEBUG */
2373 main_conf = ap_get_module_config(main_server->module_config, &mime_magic_module);
2374 for (s = main_server; s; s = s->next) {
2375 conf = ap_get_module_config(s->module_config, &mime_magic_module);
2376 if (conf->magicfile == NULL && s != main_server) {
2377 /* inherits from the parent */
2380 else if (conf->magicfile) {
2381 result = apprentice(s, p);
2384 #if MIME_MAGIC_DEBUG
2386 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
2387 MODNAME ": magic_init 1 test");
2388 for (m = conf->magic; m; m = m->next) {
2389 if (apr_isprint((((unsigned long) m) >> 24) & 255) &&
2390 apr_isprint((((unsigned long) m) >> 16) & 255) &&
2391 apr_isprint((((unsigned long) m) >> 8) & 255) &&
2392 apr_isprint(((unsigned long) m) & 255)) {
2393 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
2394 MODNAME ": magic_init 1: POINTER CLOBBERED! "
2395 "m=\"%c%c%c%c\" line=%d",
2396 (((unsigned long) m) >> 24) & 255,
2397 (((unsigned long) m) >> 16) & 255,
2398 (((unsigned long) m) >> 8) & 255,
2399 ((unsigned long) m) & 255,
2400 prevm ? prevm->lineno : -1);
2412 * Find the Content-Type from any resource this module has available
2415 static int magic_find_ct(request_rec *r)
2418 magic_server_config_rec *conf;
2420 /* the file has to exist */
2421 if (r->finfo.filetype == 0 || !r->filename) {
2425 /* was someone else already here? */
2426 if (r->content_type) {
2430 conf = ap_get_module_config(r->server->module_config, &mime_magic_module);
2431 if (!conf || !conf->magic) {
2435 /* initialize per-request info */
2436 if (!magic_set_config(r)) {
2437 return HTTP_INTERNAL_SERVER_ERROR;
2440 /* try excluding file-revision suffixes */
2441 if (revision_suffix(r) != 1) {
2442 /* process it based on the file contents */
2443 if ((result = magic_process(r)) != OK) {
2448 /* if we have any results, put them in the request structure */
2449 return magic_rsl_to_request(r);
2452 static void register_hooks(apr_pool_t *p)
2454 static const char * const aszPre[]={ "mod_mime.c", NULL };
2456 /* mod_mime_magic should be run after mod_mime, if at all. */
2458 ap_hook_type_checker(magic_find_ct, aszPre, NULL, APR_HOOK_MIDDLE);
2459 ap_hook_post_config(magic_init, NULL, NULL, APR_HOOK_FIRST);
2463 * Apache API module interface
2466 AP_DECLARE_MODULE(mime_magic) =
2468 STANDARD20_MODULE_STUFF,
2469 NULL, /* dir config creator */
2470 NULL, /* dir merger --- default is to override */
2471 create_magic_server_config, /* server config */
2472 merge_magic_server_config, /* merge server config */
2473 mime_magic_cmds, /* command apr_table_t */
2474 register_hooks /* register hooks */