]> granicus.if.org Git - apache/blob - modules/metadata/mod_mime_magic.c
Fix all of those annoying --i -> - ap_context_t problems caused by a
[apache] / modules / metadata / mod_mime_magic.c
1 /* ====================================================================
2  * Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
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
14  *    distribution.
15  *
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/)."
20  *
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
24  *    apache@apache.org.
25  *
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.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the Apache Group
33  *    for use in the Apache HTTP server project (http://www.apache.org/)."
34  *
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  * ====================================================================
48  *
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/>.
55  *
56  */
57
58 /*
59  * mod_mime_magic: MIME type lookup via file magic numbers
60  * Copyright (c) 1996-1997 Cisco Systems, Inc.
61  *
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.
66  *
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.
72  *
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.
75  *
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:
79  *
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.
82  *
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.
86  *
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.
90  *
91  * 4. This notice may not be removed or altered.
92  * -------------------------------------------------------------------------
93  *
94  * For compliance with Mr Darwin's terms: this has been very significantly
95  * modified from the free "file" command.
96  * - all ap_context_t n-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
111  *   record.
112  * - Command-line flags have been removed since they will never be used here.
113  *
114  * Ian Kluft <ikluft@cisco.com>
115  * Engineering Information Framework
116  * Central Engineering
117  * Cisco Systems, Inc.
118  * San Jose, CA, USA
119  *
120  * Initial installation          July/August 1996
121  * Misc bug fixes                May 1997
122  * Submission to Apache Group    July 1997
123  *
124  */
125
126 #include "httpd.h"
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"
132
133 #include <utime.h>
134
135
136 /*
137  * data structures and related constants
138  */
139
140 #define MODNAME        "mod_mime_magic"
141 #define MIME_MAGIC_DEBUG        0
142
143 #define MIME_BINARY_UNKNOWN    "application/octet-stream"
144 #define MIME_TEXT_UNKNOWN    "text/plain"
145
146 #define MAXMIMESTRING        256
147
148 /* HOWMANY must be at least 4096 to make gzip -dcq work */
149 #define HOWMANY 4096
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 */
154
155 struct magic {
156     struct magic *next;         /* link to next entry */
157     int lineno;                 /* line number from magic file */
158
159     short flag;
160 #define INDIR    1              /* if '>(...)' appears,  */
161 #define    UNSIGNED 2           /* comparison is unsigned */
162     short cont_level;           /* level of ">" */
163     struct {
164         char type;              /* byte short long */
165         long offset;            /* offset from indirection */
166     } in;
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 */
171 #define BYTE    1
172 #define SHORT    2
173 #define LONG    4
174 #define STRING    5
175 #define DATE    6
176 #define BESHORT    7
177 #define BELONG    8
178 #define BEDATE    9
179 #define LESHORT    10
180 #define LELONG    11
181 #define LEDATE    12
182     union VALUETYPE {
183         unsigned char b;
184         unsigned short h;
185         unsigned long l;
186         char s[MAXstring];
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 */
192
193     /* NOTE: this string is suspected of overrunning - find it! */
194     char desc[MAXDESC];         /* description */
195 };
196
197 /*
198  * data structures for tar file recognition
199  * --------------------------------------------------------------------------
200  * Header file for public domain tar (tape archive) program.
201  *
202  * @(#)tar.h 1.20 86/10/29    Public Domain. Created 25 August 1985 by John
203  * Gilmore, ihnp4!hoptoad!gnu.
204  *
205  * Header block on tape.
206  *
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".
210  */
211 #define RECORDSIZE    512
212 #define NAMSIZ    100
213 #define TUNMLEN    32
214 #define TGNMLEN    32
215
216 union record {
217     char charptr[RECORDSIZE];
218     struct header {
219         char name[NAMSIZ];
220         char mode[8];
221         char uid[8];
222         char gid[8];
223         char size[12];
224         char mtime[12];
225         char chksum[8];
226         char linkflag;
227         char linkname[NAMSIZ];
228         char magic[8];
229         char uname[TUNMLEN];
230         char gname[TGNMLEN];
231         char devmajor[8];
232         char devminor[8];
233     } header;
234 };
235
236 /* The magic field is filled with this if uname and gname are valid. */
237 #define    TMAGIC        "ustar  "      /* 7 chars and a null */
238
239 /*
240  * file-function prototypes
241  */
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);
247
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);
252
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 *);
258
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);
263
264 /*
265  * includes for ASCII substring recognition formerly "names.h" in file
266  * command
267  *
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.
272  */
273
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 */
286
287 static char *types[] =
288 {
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", */
300     0
301 };
302
303 static struct names {
304     char *name;
305     short type;
306 } names[] = {
307
308     /* These must be sorted by eye for optimal hit rate */
309     /* Add to this list only after substantial meditation */
310     {
311         "<html>", L_HTML
312     },
313     {
314         "<HTML>", L_HTML
315     },
316     {
317         "<head>", L_HTML
318     },
319     {
320         "<HEAD>", L_HTML
321     },
322     {
323         "<title>", L_HTML
324     },
325     {
326         "<TITLE>", L_HTML
327     },
328     {
329         "<h1>", L_HTML
330     },
331     {
332         "<H1>", L_HTML
333     },
334     {
335         "<!--", L_HTML
336     },
337     {
338         "<!DOCTYPE HTML", L_HTML
339     },
340     {
341         "/*", L_C
342     },                          /* must precede "The", "the", etc. */
343     {
344         "#include", L_C
345     },
346     {
347         "char", L_C
348     },
349     {
350         "The", L_ENG
351     },
352     {
353         "the", L_ENG
354     },
355     {
356         "double", L_C
357     },
358     {
359         "extern", L_C
360     },
361     {
362         "float", L_C
363     },
364     {
365         "real", L_C
366     },
367     {
368         "struct", L_C
369     },
370     {
371         "union", L_C
372     },
373     {
374         "CFLAGS", L_MAKE
375     },
376     {
377         "LDFLAGS", L_MAKE
378     },
379     {
380         "all:", L_MAKE
381     },
382     {
383         ".PRECIOUS", L_MAKE
384     },
385     /*
386      * Too many files of text have these words in them.  Find another way to
387      * recognize Fortrash.
388      */
389 #ifdef    NOTDEF
390     {
391         "subroutine", L_FORT
392     },
393     {
394         "function", L_FORT
395     },
396     {
397         "block", L_FORT
398     },
399     {
400         "common", L_FORT
401     },
402     {
403         "dimension", L_FORT
404     },
405     {
406         "integer", L_FORT
407     },
408     {
409         "data", L_FORT
410     },
411 #endif /* NOTDEF */
412     {
413         ".ascii", L_MACH
414     },
415     {
416         ".asciiz", L_MACH
417     },
418     {
419         ".byte", L_MACH
420     },
421     {
422         ".even", L_MACH
423     },
424     {
425         ".globl", L_MACH
426     },
427     {
428         "clr", L_MACH
429     },
430     {
431         "(input,", L_PAS
432     },
433     {
434         "dcl", L_PLI
435     },
436     {
437         "Received:", L_MAIL
438     },
439     {
440         ">From", L_MAIL
441     },
442     {
443         "Return-Path:", L_MAIL
444     },
445     {
446         "Cc:", L_MAIL
447     },
448     {
449         "Newsgroups:", L_NEWS
450     },
451     {
452         "Path:", L_NEWS
453     },
454     {
455         "Organization:", L_NEWS
456     },
457     {
458         NULL, 0
459     }
460 };
461
462 #define NNAMES ((sizeof(names)/sizeof(struct names)) - 1)
463
464 /*
465  * Result String List (RSL)
466  *
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.
470  */
471
472 typedef struct magic_rsl_s {
473     char *str;                  /* string, possibly a fragment */
474     struct magic_rsl_s *next;   /* pointer to next fragment */
475 } magic_rsl;
476
477 /*
478  * Apache module configuration structures
479  */
480
481 /* per-server info */
482 typedef struct {
483     char *magicfile;            /* where magic be found */
484     struct magic *magic;        /* head of magic config list */
485     struct magic *last;
486 } magic_server_config_rec;
487
488 /* per-request info */
489 typedef struct {
490     magic_rsl *head;            /* result string list */
491     magic_rsl *tail;
492     unsigned suf_recursion;     /* recursion depth in suffix check */
493 } magic_req_rec;
494
495 /*
496  * configuration functions - called by Apache API routines
497  */
498
499 module mime_magic_module;
500
501 static void *create_magic_server_config(ap_context_t *p, server_rec *d)
502 {
503     /* allocate the config - use pcalloc because it needs to be zeroed */
504     return ap_pcalloc(p, sizeof(magic_server_config_rec));
505 }
506
507 static void *merge_magic_server_config(ap_context_t *p, void *basev, void *addv)
508 {
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));
513
514     new->magicfile = add->magicfile ? add->magicfile : base->magicfile;
515     new->magic = NULL;
516     new->last = NULL;
517     return new;
518 }
519
520 static const char *set_magicfile(cmd_parms *cmd, char *d, char *arg)
521 {
522     magic_server_config_rec *conf = (magic_server_config_rec *)
523     ap_get_module_config(cmd->server->module_config,
524                       &mime_magic_module);
525
526     if (!conf) {
527         return MODNAME ": server structure not allocated";
528     }
529     conf->magicfile = arg;
530     return NULL;
531 }
532
533 /*
534  * configuration file commands - exported to Apache API
535  */
536
537 static const command_rec mime_magic_cmds[] =
538 {
539     {"MimeMagicFile", set_magicfile, NULL, RSRC_CONF, TAKE1,
540      "Path to MIME Magic file (in file(1) format)"},
541     {NULL}
542 };
543
544 /*
545  * RSL (result string list) processing routines
546  *
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
550  * types.
551  *
552  * return value conventions for these functions: functions which return int:
553  * failure = -1, other = result functions which return pointers: failure = 0,
554  * other = result
555  */
556
557 /* allocate a per-request structure and put it in the request record */
558 static magic_req_rec *magic_set_config(request_rec *r)
559 {
560     magic_req_rec *req_dat = (magic_req_rec *) ap_palloc(r->pool,
561                                                       sizeof(magic_req_rec));
562
563     req_dat->head = req_dat->tail = (magic_rsl *) NULL;
564     ap_set_module_config(r->request_config, &mime_magic_module, req_dat);
565     return req_dat;
566 }
567
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)
571 {
572     magic_req_rec *req_dat = (magic_req_rec *)
573                     ap_get_module_config(r->request_config, &mime_magic_module);
574     magic_rsl *rsl;
575
576     /* make sure we have a list to put it in */
577     if (!req_dat) {
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))) {
581             /* failure */
582             return -1;
583         }
584     }
585
586     /* allocate the list entry */
587     rsl = (magic_rsl *) ap_palloc(r->pool, sizeof(magic_rsl));
588
589     /* fill it */
590     rsl->str = str;
591     rsl->next = (magic_rsl *) NULL;
592
593     /* append to the list */
594     if (req_dat->head && req_dat->tail) {
595         req_dat->tail->next = rsl;
596         req_dat->tail = rsl;
597     }
598     else {
599         req_dat->head = req_dat->tail = rsl;
600     }
601
602     /* success */
603     return 0;
604 }
605
606 /* RSL hook for puts-type functions */
607 static int magic_rsl_puts(request_rec *r, char *str)
608 {
609     return magic_rsl_add(r, str);
610 }
611
612 /* RSL hook for printf-type functions */
613 static int magic_rsl_printf(request_rec *r, char *str,...)
614 {
615     va_list ap;
616
617     char buf[MAXMIMESTRING];
618
619     /* assemble the string into the buffer */
620     va_start(ap, str);
621     ap_vsnprintf(buf, sizeof(buf), str, ap);
622     va_end(ap);
623
624     /* add the buffer to the list */
625     return magic_rsl_add(r, strdup(buf));
626 }
627
628 /* RSL hook for putchar-type functions */
629 static int magic_rsl_putchar(request_rec *r, char c)
630 {
631     char str[2];
632
633     /* high overhead for 1 char - just hope they don't do this much */
634     str[0] = c;
635     str[1] = '\0';
636     return magic_rsl_add(r, str);
637 }
638
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)
641 {
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);
649
650     /* allocate the result string */
651     result = (char *) ap_palloc(r->pool, len + 1);
652
653     /* loop through and collect the string */
654     res_pos = 0;
655     for (frag = req_dat->head, cur_frag = 0;
656          frag->next;
657          frag = frag->next, cur_frag++) {
658         /* loop to the first fragment */
659         if (cur_frag < start_frag)
660             continue;
661
662         /* loop through and collect chars */
663         for (cur_pos = (cur_frag == start_frag) ? start_pos : 0;
664              frag->str[cur_pos];
665              cur_pos++) {
666             if (cur_frag >= start_frag
667                 && cur_pos >= start_pos
668                 && res_pos <= len) {
669                 result[res_pos++] = frag->str[cur_pos];
670                 if (res_pos > len) {
671                     break;
672                 }
673             }
674         }
675     }
676
677     /* clean up and return */
678     result[res_pos] = 0;
679 #if MIME_MAGIC_DEBUG
680     ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
681              MODNAME ": rsl_strdup() %d chars: %s", res_pos - 1, result);
682 #endif
683     return result;
684 }
685
686 /* states for the state-machine algorithm in magic_rsl_to_request() */
687 typedef enum {
688     rsl_leading_space, rsl_type, rsl_subtype, rsl_separator, rsl_encoding
689 } rsl_states;
690
691 /* process the RSL and set the MIME info in the request record */
692 static int magic_rsl_to_request(request_rec *r)
693 {
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 */
702
703     magic_rsl *frag;            /* list-traversal pointer */
704     rsl_states state;
705
706     magic_req_rec *req_dat = (magic_req_rec *)
707                     ap_get_module_config(r->request_config, &mime_magic_module);
708
709     /* check if we have a result */
710     if (!req_dat || !req_dat->head) {
711         /* empty - no match, we defer to other Apache modules */
712         return DECLINED;
713     }
714
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;
720          frag && frag->next;
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 */
728                     continue;
729                 }
730                 else if (state == rsl_type) {
731                     /* whitespace: type has no slash! */
732                     return DECLINED;
733                 }
734                 else if (state == rsl_subtype) {
735                     /* whitespace: end of MIME type */
736                     state++;
737                     continue;
738                 }
739                 else if (state == rsl_separator) {
740                     /* eat whitespace in this state */
741                     continue;
742                 }
743                 else if (state == rsl_encoding) {
744                     /* whitespace: end of MIME encoding */
745                     /* we're done */
746                     frag = req_dat->tail;
747                     break;
748                 }
749                 else {
750                     /* should not be possible */
751                     /* abandon malfunctioning module */
752                     ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, r,
753                                 MODNAME ": bad state %d (ws)", state);
754                     return DECLINED;
755                 }
756                 /* NOTREACHED */
757             }
758             else if (state == rsl_type &&
759                      frag->str[cur_pos] == '/') {
760                 /* copy the char and go to rsl_subtype state */
761                 type_len++;
762                 state++;
763             }
764             else {
765                 /* process non-space actions for each state */
766                 if (state == rsl_leading_space) {
767                     /* non-space: begin MIME type */
768                     state++;
769                     type_frag = cur_frag;
770                     type_pos = cur_pos;
771                     type_len = 1;
772                     continue;
773                 }
774                 else if (state == rsl_type ||
775                          state == rsl_subtype) {
776                     /* non-space: adds to type */
777                     type_len++;
778                     continue;
779                 }
780                 else if (state == rsl_separator) {
781                     /* non-space: begin MIME encoding */
782                     state++;
783                     encoding_frag = cur_frag;
784                     encoding_pos = cur_pos;
785                     encoding_len = 1;
786                     continue;
787                 }
788                 else if (state == rsl_encoding) {
789                     /* non-space: adds to encoding */
790                     encoding_len++;
791                     continue;
792                 }
793                 else {
794                     /* should not be possible */
795                     /* abandon malfunctioning module */
796                     ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, r,
797                                 MODNAME ": bad state %d (ns)", state);
798                     return DECLINED;
799                 }
800                 /* NOTREACHED */
801             }
802             /* NOTREACHED */
803         }
804     }
805
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 */
810         return DECLINED;
811     }
812
813     /* save the info in the request record */
814     if (state == rsl_subtype || state == rsl_encoding ||
815         state == rsl_encoding) {
816         char *tmp;
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;
822     }
823     if (state == rsl_encoding) {
824         char *tmp;
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 */
829         ap_str_tolower(tmp);
830         r->content_encoding = tmp;
831     }
832
833     /* detect memory allocation errors */
834     if (!r->content_type ||
835         (state == rsl_encoding && !r->content_encoding)) {
836         return HTTP_INTERNAL_SERVER_ERROR;
837     }
838
839     /* success! */
840     return OK;
841 }
842
843 /*
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.
847  */
848 static int magic_process(request_rec *r)
849 {
850     int fd = 0;
851     unsigned char buf[HOWMANY + 1];     /* one extra for terminating '\0' */
852     int nbytes = 0;             /* number of bytes read from a datafile */
853     int result;
854
855     /*
856      * first try judging the file based on its filesystem status
857      */
858     switch ((result = fsmagic(r, r->filename))) {
859     case DONE:
860         magic_rsl_putchar(r, '\n');
861         return OK;
862     case OK:
863         break;
864     default:
865         /* fatal error, bail out */
866         return result;
867     }
868
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, r,
872                     MODNAME ": can't read `%s'", r->filename);
873         /* let some other handler decide what the problem is */
874         return DECLINED;
875     }
876
877     /*
878      * try looking at the first HOWMANY bytes
879      */
880     if ((nbytes = read(fd, (char *) buf, sizeof(buf) - 1)) == -1) {
881         ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
882                     MODNAME ": read failed: %s", r->filename);
883         return HTTP_INTERNAL_SERVER_ERROR;
884     }
885
886     if (nbytes == 0)
887         magic_rsl_puts(r, MIME_TEXT_UNKNOWN);
888     else {
889         buf[nbytes++] = '\0';   /* null-terminate it */
890         tryit(r, buf, nbytes, 1); 
891     }
892
893     (void) ap_pclosef(r->pool, fd);
894     (void) magic_rsl_putchar(r, '\n');
895
896     return OK;
897 }
898
899
900 static void tryit(request_rec *r, unsigned char *buf, int nb, int checkzmagic)
901 {
902     /*
903      * Try compression stuff
904      */
905         if (checkzmagic == 1) {  
906                         if (zmagic(r, buf, nb) == 1)
907                         return;
908         }
909
910     /*
911      * try tests in /etc/magic (or surrogate magic file)
912      */
913     if (softmagic(r, buf, nb) == 1)
914         return;
915
916     /*
917      * try known keywords, check for ascii-ness too.
918      */
919     if (ascmagic(r, buf, nb) == 1)
920         return;
921
922     /*
923      * abandon hope, all ye who remain here
924      */
925     magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
926 }
927
928 #define    EATAB {while (ap_isspace((unsigned char) *l))  ++l;}
929
930 /*
931  * apprentice - load configuration from the magic file r
932  *  API request record
933  */
934 static int apprentice(server_rec *s, ap_context_t *p)
935 {
936     FILE *f;
937     char line[BUFSIZ + 1];
938     int errs = 0;
939     int lineno;
940 #if MIME_MAGIC_DEBUG
941     int rule = 0;
942     struct magic *m, *prevm;
943 #endif
944     char *fname;
945
946     magic_server_config_rec *conf = (magic_server_config_rec *)
947                     ap_get_module_config(s->module_config, &mime_magic_module);
948
949     fname = ap_server_root_relative(p, conf->magicfile);
950     f = ap_pfopen(p, fname, "r");
951     if (f == NULL) {
952         ap_log_error(APLOG_MARK, APLOG_ERR, s,
953                     MODNAME ": can't read magic file %s", fname);
954         return -1;
955     }
956
957     /* set up the magic list (empty) */
958     conf->magic = conf->last = NULL;
959
960     /* parse it */
961     for (lineno = 1; fgets(line, BUFSIZ, f) != NULL; lineno++) {
962         int ws_offset;
963
964         /* delete newline */
965         if (line[0]) {
966             line[strlen(line) - 1] = '\0';
967         }
968
969         /* skip leading whitespace */
970         ws_offset = 0;
971         while (line[ws_offset] && ap_isspace(line[ws_offset])) {
972             ws_offset++;
973         }
974
975         /* skip blank lines */
976         if (line[ws_offset] == 0) {
977             continue;
978         }
979
980         /* comment, do not parse */
981         if (line[ws_offset] == '#')
982             continue;
983
984 #if MIME_MAGIC_DEBUG
985         /* if we get here, we're going to use it so count it */
986         rule++;
987 #endif
988
989         /* parse it */
990         if (parse(s, p, line + ws_offset, lineno) != 0)
991             ++errs;
992     }
993
994     (void) ap_pfclose(p, f);
995
996 #if MIME_MAGIC_DEBUG
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",
999                 conf,
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);
1007 #endif
1008
1009 #if MIME_MAGIC_DEBUG
1010     prevm = 0;
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);
1026             break;
1027         }
1028         prevm = m;
1029     }
1030 #endif
1031
1032     return (errs ? -1 : 0);
1033 }
1034
1035 /*
1036  * extend the sign bit if the comparison is to be signed
1037  */
1038 static unsigned long signextend(server_rec *s, struct magic *m, unsigned long v)
1039 {
1040     if (!(m->flag & UNSIGNED))
1041         switch (m->type) {
1042             /*
1043              * Do not remove the casts below.  They are vital. When later
1044              * compared with the data, the sign extension must have happened.
1045              */
1046         case BYTE:
1047             v = (char) v;
1048             break;
1049         case SHORT:
1050         case BESHORT:
1051         case LESHORT:
1052             v = (short) v;
1053             break;
1054         case DATE:
1055         case BEDATE:
1056         case LEDATE:
1057         case LONG:
1058         case BELONG:
1059         case LELONG:
1060             v = (long) v;
1061             break;
1062         case STRING:
1063             break;
1064         default:
1065             ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, s,
1066                         MODNAME ": can't happen: m->type=%d", m->type);
1067             return -1;
1068         }
1069     return v;
1070 }
1071
1072 /*
1073  * parse one line from magic file, put into magic[index++] if valid
1074  */
1075 static int parse(server_rec *serv, ap_context_t *p, char *l, int lineno)
1076 {
1077     struct magic *m;
1078     char *t, *s;
1079     magic_server_config_rec *conf = (magic_server_config_rec *)
1080                     ap_get_module_config(serv->module_config, &mime_magic_module);
1081
1082     /* allocate magic structure entry */
1083     m = (struct magic *) ap_pcalloc(p, sizeof(struct magic));
1084
1085     /* append to linked list */
1086     m->next = NULL;
1087     if (!conf->magic || !conf->last) {
1088         conf->magic = conf->last = m;
1089     }
1090     else {
1091         conf->last->next = m;
1092         conf->last = m;
1093     }
1094
1095     /* set values in magic structure */
1096     m->flag = 0;
1097     m->cont_level = 0;
1098     m->lineno = lineno;
1099
1100     while (*l == '>') {
1101         ++l;                    /* step over */
1102         m->cont_level++;
1103     }
1104
1105     if (m->cont_level != 0 && *l == '(') {
1106         ++l;                    /* step over */
1107         m->flag |= INDIR;
1108     }
1109
1110     /* get offset, then skip over it */
1111     m->offset = (int) strtol(l, &t, 0);
1112     if (l == t) {
1113         ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, serv,
1114                     MODNAME ": offset %s invalid", l);
1115     }
1116     l = t;
1117
1118     if (m->flag & INDIR) {
1119         m->in.type = LONG;
1120         m->in.offset = 0;
1121         /*
1122          * read [.lbs][+-]nnnnn)
1123          */
1124         if (*l == '.') {
1125             switch (*++l) {
1126             case 'l':
1127                 m->in.type = LONG;
1128                 break;
1129             case 's':
1130                 m->in.type = SHORT;
1131                 break;
1132             case 'b':
1133                 m->in.type = BYTE;
1134                 break;
1135             default:
1136                 ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, serv,
1137                         MODNAME ": indirect offset type %c invalid", *l);
1138                 break;
1139             }
1140             l++;
1141         }
1142         s = l;
1143         if (*l == '+' || *l == '-')
1144             l++;
1145         if (ap_isdigit((unsigned char) *l)) {
1146             m->in.offset = strtol(l, &t, 0);
1147             if (*s == '-')
1148                 m->in.offset = -m->in.offset;
1149         }
1150         else
1151             t = l;
1152         if (*t++ != ')') {
1153             ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, serv,
1154                         MODNAME ": missing ')' in indirect offset");
1155         }
1156         l = t;
1157     }
1158
1159
1160     while (ap_isdigit((unsigned char) *l))
1161         ++l;
1162     EATAB;
1163
1164 #define NBYTE           4
1165 #define NSHORT          5
1166 #define NLONG           4
1167 #define NSTRING         6
1168 #define NDATE           4
1169 #define NBESHORT        7
1170 #define NBELONG         6
1171 #define NBEDATE         6
1172 #define NLESHORT        7
1173 #define NLELONG         6
1174 #define NLEDATE         6
1175
1176     if (*l == 'u') {
1177         ++l;
1178         m->flag |= UNSIGNED;
1179     }
1180
1181     /* get type, skip it */
1182     if (strncmp(l, "byte", NBYTE) == 0) {
1183         m->type = BYTE;
1184         l += NBYTE;
1185     }
1186     else if (strncmp(l, "short", NSHORT) == 0) {
1187         m->type = SHORT;
1188         l += NSHORT;
1189     }
1190     else if (strncmp(l, "long", NLONG) == 0) {
1191         m->type = LONG;
1192         l += NLONG;
1193     }
1194     else if (strncmp(l, "string", NSTRING) == 0) {
1195         m->type = STRING;
1196         l += NSTRING;
1197     }
1198     else if (strncmp(l, "date", NDATE) == 0) {
1199         m->type = DATE;
1200         l += NDATE;
1201     }
1202     else if (strncmp(l, "beshort", NBESHORT) == 0) {
1203         m->type = BESHORT;
1204         l += NBESHORT;
1205     }
1206     else if (strncmp(l, "belong", NBELONG) == 0) {
1207         m->type = BELONG;
1208         l += NBELONG;
1209     }
1210     else if (strncmp(l, "bedate", NBEDATE) == 0) {
1211         m->type = BEDATE;
1212         l += NBEDATE;
1213     }
1214     else if (strncmp(l, "leshort", NLESHORT) == 0) {
1215         m->type = LESHORT;
1216         l += NLESHORT;
1217     }
1218     else if (strncmp(l, "lelong", NLELONG) == 0) {
1219         m->type = LELONG;
1220         l += NLELONG;
1221     }
1222     else if (strncmp(l, "ledate", NLEDATE) == 0) {
1223         m->type = LEDATE;
1224         l += NLEDATE;
1225     }
1226     else {
1227         ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, serv,
1228                     MODNAME ": type %s invalid", l);
1229         return -1;
1230     }
1231     /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
1232     if (*l == '&') {
1233         ++l;
1234         m->mask = signextend(serv, m, strtol(l, &l, 0));
1235     }
1236     else
1237         m->mask = ~0L;
1238     EATAB;
1239
1240     switch (*l) {
1241     case '>':
1242     case '<':
1243         /* Old-style anding: "0 byte &0x80 dynamically linked" */
1244     case '&':
1245     case '^':
1246     case '=':
1247         m->reln = *l;
1248         ++l;
1249         break;
1250     case '!':
1251         if (m->type != STRING) {
1252             m->reln = *l;
1253             ++l;
1254             break;
1255         }
1256         /* FALL THROUGH */
1257     default:
1258         if (*l == 'x' && ap_isspace((unsigned char) l[1])) {
1259             m->reln = *l;
1260             ++l;
1261             goto GetDesc;       /* Bill The Cat */
1262         }
1263         m->reln = '=';
1264         break;
1265     }
1266     EATAB;
1267
1268     if (getvalue(serv, m, &l))
1269         return -1;
1270     /*
1271      * now get last part - the description
1272      */
1273   GetDesc:
1274     EATAB;
1275     if (l[0] == '\b') {
1276         ++l;
1277         m->nospflag = 1;
1278     }
1279     else if ((l[0] == '\\') && (l[1] == 'b')) {
1280         ++l;
1281         ++l;
1282         m->nospflag = 1;
1283     }
1284     else
1285         m->nospflag = 0;
1286     strncpy(m->desc, l, sizeof(m->desc) - 1);
1287     m->desc[sizeof(m->desc) - 1] = '\0';
1288
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 */
1294
1295     return 0;
1296 }
1297
1298 /*
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.
1302  */
1303 static int getvalue(server_rec *s, struct magic *m, char **p)
1304 {
1305     int slen;
1306
1307     if (m->type == STRING) {
1308         *p = getstr(s, *p, m->value.s, sizeof(m->value.s), &slen);
1309         m->vallen = slen;
1310     }
1311     else if (m->reln != 'x')
1312         m->value.l = signextend(s, m, strtol(*p, p, 0));
1313     return 0;
1314 }
1315
1316 /*
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.
1320  */
1321 static char *getstr(server_rec *serv, register char *s, register char *p,
1322                     int plen, int *slen)
1323 {
1324     char *origs = s, *origp = p;
1325     char *pmax = p + plen - 1;
1326     register int c;
1327     register int val;
1328
1329     while ((c = *s++) != '\0') {
1330         if (ap_isspace((unsigned char) c))
1331             break;
1332         if (p >= pmax) {
1333             ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, serv,
1334                         MODNAME ": string too long: %s", origs);
1335             break;
1336         }
1337         if (c == '\\') {
1338             switch (c = *s++) {
1339
1340             case '\0':
1341                 goto out;
1342
1343             default:
1344                 *p++ = (char) c;
1345                 break;
1346
1347             case 'n':
1348                 *p++ = '\n';
1349                 break;
1350
1351             case 'r':
1352                 *p++ = '\r';
1353                 break;
1354
1355             case 'b':
1356                 *p++ = '\b';
1357                 break;
1358
1359             case 't':
1360                 *p++ = '\t';
1361                 break;
1362
1363             case 'f':
1364                 *p++ = '\f';
1365                 break;
1366
1367             case 'v':
1368                 *p++ = '\v';
1369                 break;
1370
1371                 /* \ and up to 3 octal digits */
1372             case '0':
1373             case '1':
1374             case '2':
1375             case '3':
1376             case '4':
1377             case '5':
1378             case '6':
1379             case '7':
1380                 val = c - '0';
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');
1387                     else
1388                         --s;
1389                 }
1390                 else
1391                     --s;
1392                 *p++ = (char) val;
1393                 break;
1394
1395                 /* \x and up to 3 hex digits */
1396             case 'x':
1397                 val = 'x';      /* Default if no digits */
1398                 c = hextoint(*s++);     /* Get next char */
1399                 if (c >= 0) {
1400                     val = c;
1401                     c = hextoint(*s++);
1402                     if (c >= 0) {
1403                         val = (val << 4) + c;
1404                         c = hextoint(*s++);
1405                         if (c >= 0) {
1406                             val = (val << 4) + c;
1407                         }
1408                         else
1409                             --s;
1410                     }
1411                     else
1412                         --s;
1413                 }
1414                 else
1415                     --s;
1416                 *p++ = (char) val;
1417                 break;
1418             }
1419         }
1420         else
1421             *p++ = (char) c;
1422     }
1423   out:
1424     *p = '\0';
1425     *slen = p - origp;
1426     return s;
1427 }
1428
1429
1430 /* Single hex char to int; -1 if not a hex char. */
1431 static int hextoint(int c)
1432 {
1433     if (ap_isdigit((unsigned char) c))
1434         return c - '0';
1435     if ((c >= 'a') && (c <= 'f'))
1436         return c + 10 - 'a';
1437     if ((c >= 'A') && (c <= 'F'))
1438         return c + 10 - 'A';
1439     return -1;
1440 }
1441
1442
1443 /*
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
1447  */
1448 static int fsmagic(request_rec *r, const char *fn)
1449 {
1450     switch (r->finfo.st_mode & S_IFMT) {
1451     case S_IFDIR:
1452         magic_rsl_puts(r, DIR_MAGIC_TYPE);
1453         return DONE;
1454     case S_IFCHR:
1455         /*
1456          * (void) magic_rsl_printf(r,"character special (%d/%d)",
1457          * major(sb->st_rdev), minor(sb->st_rdev));
1458          */
1459         (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
1460         return DONE;
1461 #ifdef S_IFBLK
1462     case S_IFBLK:
1463         /*
1464          * (void) magic_rsl_printf(r,"block special (%d/%d)",
1465          * major(sb->st_rdev), minor(sb->st_rdev));
1466          */
1467         (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
1468         return DONE;
1469         /* TODO add code to handle V7 MUX and Blit MUX files */
1470 #endif
1471 #ifdef    S_IFIFO
1472     case S_IFIFO:
1473         /*
1474          * magic_rsl_puts(r,"fifo (named pipe)");
1475          */
1476         (void) magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
1477         return DONE;
1478 #endif
1479 #ifdef    S_IFLNK
1480     case S_IFLNK:
1481         /* We used stat(), the only possible reason for this is that the
1482          * symlink is broken.
1483          */
1484         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, r,
1485                     MODNAME ": broken symlink (%s)", fn);
1486         return HTTP_INTERNAL_SERVER_ERROR;
1487 #endif
1488 #ifdef    S_IFSOCK
1489 #ifndef __COHERENT__
1490     case S_IFSOCK:
1491         magic_rsl_puts(r, MIME_BINARY_UNKNOWN);
1492         return DONE;
1493 #endif
1494 #endif
1495     case S_IFREG:
1496         break;
1497     default:
1498         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, r,
1499                     MODNAME ": invalid mode 0%o.", (unsigned int)r->finfo.st_mode);
1500         return HTTP_INTERNAL_SERVER_ERROR;
1501     }
1502
1503     /*
1504      * regular file, check next possibility
1505      */
1506     if (r->finfo.st_size == 0) {
1507         magic_rsl_puts(r, MIME_TEXT_UNKNOWN);
1508         return DONE;
1509     }
1510     return OK;
1511 }
1512
1513 /*
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.
1516  */
1517                 /* ARGSUSED1 *//* nbytes passed for regularity, maybe need later */
1518 static int softmagic(request_rec *r, unsigned char *buf, int nbytes)
1519 {
1520     if (match(r, buf, nbytes))
1521         return 1;
1522
1523     return 0;
1524 }
1525
1526 /*
1527  * Go through the whole list, stopping if you find a match.  Process all the
1528  * continuations of that match before returning.
1529  *
1530  * We support multi-level continuations:
1531  *
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.
1535  *
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.
1539  *
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.
1545  *
1546  * Continuations at the current level are processed as, if we see one, there's
1547  * no lower-level continuation that may have failed.
1548  *
1549  * If a continuation matches, we bump the current continuation level so that
1550  * higher-level continuations are processed.
1551  */
1552 static int match(request_rec *r, unsigned char *s, int nbytes)
1553 {
1554 #if MIME_MAGIC_DEBUG
1555     int rule_counter = 0;
1556 #endif
1557     int cont_level = 0;
1558     int need_separator = 0;
1559     union VALUETYPE p;
1560     magic_server_config_rec *conf = (magic_server_config_rec *)
1561                 ap_get_module_config(r->server->module_config, &mime_magic_module);
1562     struct magic *m;
1563
1564 #if MIME_MAGIC_DEBUG
1565     ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
1566                 MODNAME ": match conf=%x file=%s m=%s m->next=%s last=%s",
1567                 conf,
1568                 conf->magicfile ? conf->magicfile : "NULL",
1569                 conf->magic ? "set" : "NULL",
1570                 (conf->magic && conf->magic->next) ? "set" : "NULL",
1571                 conf->last ? "set" : "NULL");
1572 #endif
1573
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, r,
1581                         MODNAME ": match: POINTER CLOBBERED! "
1582                         "m=\"%c%c%c%c\"",
1583                         (((unsigned long) m) >> 24) & 255,
1584                         (((unsigned long) m) >> 16) & 255,
1585                         (((unsigned long) m) >> 8) & 255,
1586                         ((unsigned long) m) & 255);
1587             break;
1588         }
1589     }
1590 #endif
1591
1592     for (m = conf->magic; m; m = m->next) {
1593 #if MIME_MAGIC_DEBUG
1594         rule_counter++;
1595         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
1596                     MODNAME ": line=%d desc=%s", m->lineno, m->desc);
1597 #endif
1598
1599         /* check if main entry matches */
1600         if (!mget(r, &p, s, m, nbytes) ||
1601             !mcheck(r, &p, m)) {
1602             struct magic *m_cont;
1603
1604             /*
1605              * main entry didn't match, flush its continuations
1606              */
1607             if (!m->next || (m->next->cont_level == 0)) {
1608                 continue;
1609             }
1610
1611             m_cont = m->next;
1612             while (m_cont && (m_cont->cont_level != 0)) {
1613 #if MIME_MAGIC_DEBUG
1614                 rule_counter++;
1615                 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, 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,
1619                             m_cont->desc);
1620 #endif
1621                 /*
1622                  * this trick allows us to keep *m in sync when the continue
1623                  * advances the pointer
1624                  */
1625                 m = m_cont;
1626                 m_cont = m_cont->next;
1627             }
1628             continue;
1629         }
1630
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, r,
1635                     MODNAME ": rule matched, line=%d type=%d %s",
1636                     m->lineno, m->type,
1637                     (m->type == STRING) ? m->value.s : "");
1638 #endif
1639
1640         /* print the match */
1641         mprint(r, &p, m);
1642
1643         /*
1644          * If we printed something, we'll need to print a blank before we
1645          * print something else.
1646          */
1647         if (m->desc[0])
1648             need_separator = 1;
1649         /* and any continuations that match */
1650         cont_level++;
1651         /*
1652          * while (m && m->next && m->next->cont_level != 0 && ( m = m->next
1653          * ))
1654          */
1655         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, 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 : "");
1662 #endif
1663             if (cont_level >= m->cont_level) {
1664                 if (cont_level > m->cont_level) {
1665                     /*
1666                      * We're at the end of the level "cont_level"
1667                      * continuations.
1668                      */
1669                     cont_level = m->cont_level;
1670                 }
1671                 if (mget(r, &p, s, m, nbytes) &&
1672                     mcheck(r, &p, m)) {
1673                     /*
1674                      * This continuation matched. Print its message, with a
1675                      * blank before it if the previous item printed and this
1676                      * item isn't empty.
1677                      */
1678                     /* space if previous printed */
1679                     if (need_separator
1680                         && (m->nospflag == 0)
1681                         && (m->desc[0] != '\0')
1682                         ) {
1683                         (void) magic_rsl_putchar(r, ' ');
1684                         need_separator = 0;
1685                     }
1686                     mprint(r, &p, m);
1687                     if (m->desc[0])
1688                         need_separator = 1;
1689
1690                     /*
1691                      * If we see any continuations at a higher level, process
1692                      * them.
1693                      */
1694                     cont_level++;
1695                 }
1696             }
1697
1698             /* move to next continuation record */
1699             m = m->next;
1700         }
1701 #if MIME_MAGIC_DEBUG
1702         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
1703                     MODNAME ": matched after %d rules", rule_counter);
1704 #endif
1705         return 1;               /* all through */
1706     }
1707 #if MIME_MAGIC_DEBUG
1708     ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
1709                 MODNAME ": failed after %d rules", rule_counter);
1710 #endif
1711     return 0;                   /* no match at all */
1712 }
1713
1714 static void mprint(request_rec *r, union VALUETYPE *p, struct magic *m)
1715 {
1716     char *pp, *rt;
1717     unsigned long v;
1718
1719     switch (m->type) {
1720     case BYTE:
1721         v = p->b;
1722         break;
1723
1724     case SHORT:
1725     case BESHORT:
1726     case LESHORT:
1727         v = p->h;
1728         break;
1729
1730     case LONG:
1731     case BELONG:
1732     case LELONG:
1733         v = p->l;
1734         break;
1735
1736     case STRING:
1737         if (m->reln == '=') {
1738             (void) magic_rsl_printf(r, m->desc, m->value.s);
1739         }
1740         else {
1741             (void) magic_rsl_printf(r, m->desc, p->s);
1742         }
1743         return;
1744
1745     case DATE:
1746     case BEDATE:
1747     case LEDATE:
1748         /* XXX: not multithread safe */
1749         pp = ctime((time_t *) & p->l);
1750         if ((rt = strchr(pp, '\n')) != NULL)
1751             *rt = '\0';
1752         (void) magic_rsl_printf(r, m->desc, pp);
1753         return;
1754     default:
1755         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, r,
1756                     MODNAME ": invalid m->type (%d) in mprint().",
1757                     m->type);
1758         return;
1759     }
1760
1761     v = signextend(r->server, m, v) & m->mask;
1762     (void) magic_rsl_printf(r, m->desc, (unsigned long) v);
1763 }
1764
1765 /*
1766  * Convert the byte order of the data we are looking at
1767  */
1768 static int mconvert(request_rec *r, union VALUETYPE *p, struct magic *m)
1769 {
1770     char *rt;
1771
1772     switch (m->type) {
1773     case BYTE:
1774     case SHORT:
1775     case LONG:
1776     case DATE:
1777         return 1;
1778     case STRING:
1779         /* Null terminate and eat the return */
1780         p->s[sizeof(p->s) - 1] = '\0';
1781         if ((rt = strchr(p->s, '\n')) != NULL)
1782             *rt = '\0';
1783         return 1;
1784     case BESHORT:
1785         p->h = (short) ((p->hs[0] << 8) | (p->hs[1]));
1786         return 1;
1787     case BELONG:
1788     case BEDATE:
1789         p->l = (long)
1790             ((p->hl[0] << 24) | (p->hl[1] << 16) | (p->hl[2] << 8) | (p->hl[3]));
1791         return 1;
1792     case LESHORT:
1793         p->h = (short) ((p->hs[1] << 8) | (p->hs[0]));
1794         return 1;
1795     case LELONG:
1796     case LEDATE:
1797         p->l = (long)
1798             ((p->hl[3] << 24) | (p->hl[2] << 16) | (p->hl[1] << 8) | (p->hl[0]));
1799         return 1;
1800     default:
1801         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, r,
1802                     MODNAME ": invalid type %d in mconvert().", m->type);
1803         return 0;
1804     }
1805 }
1806
1807
1808 static int mget(request_rec *r, union VALUETYPE *p, unsigned char *s,
1809                 struct magic *m, int nbytes)
1810 {
1811     long offset = m->offset;
1812
1813     if (offset + sizeof(union VALUETYPE) > nbytes)
1814                   return 0;
1815
1816     memcpy(p, s + offset, sizeof(union VALUETYPE));
1817
1818     if (!mconvert(r, p, m))
1819         return 0;
1820
1821     if (m->flag & INDIR) {
1822
1823         switch (m->in.type) {
1824         case BYTE:
1825             offset = p->b + m->in.offset;
1826             break;
1827         case SHORT:
1828             offset = p->h + m->in.offset;
1829             break;
1830         case LONG:
1831             offset = p->l + m->in.offset;
1832             break;
1833         }
1834
1835         if (offset + sizeof(union VALUETYPE) > nbytes)
1836                       return 0;
1837
1838         memcpy(p, s + offset, sizeof(union VALUETYPE));
1839
1840         if (!mconvert(r, p, m))
1841             return 0;
1842     }
1843     return 1;
1844 }
1845
1846 static int mcheck(request_rec *r, union VALUETYPE *p, struct magic *m)
1847 {
1848     register unsigned long l = m->value.l;
1849     register unsigned long v;
1850     int matched;
1851
1852     if ((m->value.s[0] == 'x') && (m->value.s[1] == '\0')) {
1853         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, r,
1854                     MODNAME ": BOINK");
1855         return 1;
1856     }
1857
1858     switch (m->type) {
1859     case BYTE:
1860         v = p->b;
1861         break;
1862
1863     case SHORT:
1864     case BESHORT:
1865     case LESHORT:
1866         v = p->h;
1867         break;
1868
1869     case LONG:
1870     case BELONG:
1871     case LELONG:
1872     case DATE:
1873     case BEDATE:
1874     case LEDATE:
1875         v = p->l;
1876         break;
1877
1878     case STRING:
1879         l = 0;
1880         /*
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.
1884          */
1885         v = 0;
1886         {
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;
1890
1891             while (--len >= 0)
1892                 if ((v = *b++ - *a++) != 0)
1893                     break;
1894         }
1895         break;
1896     default:
1897         /*  bogosity, pretend that it just wasn't a match */
1898         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, r,
1899                     MODNAME ": invalid type %d in mcheck().", m->type);
1900         return 0;
1901     }
1902
1903     v = signextend(r->server, m, v) & m->mask;
1904
1905     switch (m->reln) {
1906     case 'x':
1907 #if MIME_MAGIC_DEBUG
1908         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
1909                     "%lu == *any* = 1", v);
1910 #endif
1911         matched = 1;
1912         break;
1913
1914     case '!':
1915         matched = v != l;
1916 #if MIME_MAGIC_DEBUG
1917         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
1918                     "%lu != %lu = %d", v, l, matched);
1919 #endif
1920         break;
1921
1922     case '=':
1923         matched = v == l;
1924 #if MIME_MAGIC_DEBUG
1925         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
1926                     "%lu == %lu = %d", v, l, matched);
1927 #endif
1928         break;
1929
1930     case '>':
1931         if (m->flag & UNSIGNED) {
1932             matched = v > l;
1933 #if MIME_MAGIC_DEBUG
1934             ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
1935                         "%lu > %lu = %d", v, l, matched);
1936 #endif
1937         }
1938         else {
1939             matched = (long) v > (long) l;
1940 #if MIME_MAGIC_DEBUG
1941             ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
1942                         "%ld > %ld = %d", v, l, matched);
1943 #endif
1944         }
1945         break;
1946
1947     case '<':
1948         if (m->flag & UNSIGNED) {
1949             matched = v < l;
1950 #if MIME_MAGIC_DEBUG
1951             ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
1952                         "%lu < %lu = %d", v, l, matched);
1953 #endif
1954         }
1955         else {
1956             matched = (long) v < (long) l;
1957 #if MIME_MAGIC_DEBUG
1958             ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
1959                         "%ld < %ld = %d", v, l, matched);
1960 #endif
1961         }
1962         break;
1963
1964     case '&':
1965         matched = (v & l) == l;
1966 #if MIME_MAGIC_DEBUG
1967         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
1968                     "((%lx & %lx) == %lx) = %d", v, l, l, matched);
1969 #endif
1970         break;
1971
1972     case '^':
1973         matched = (v & l) != l;
1974 #if MIME_MAGIC_DEBUG
1975         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
1976                     "((%lx & %lx) != %lx) = %d", v, l, l, matched);
1977 #endif
1978         break;
1979
1980     default:
1981         /* bogosity, pretend it didn't match */
1982         matched = 0;
1983         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_ERR, r,
1984                     MODNAME ": mcheck: can't happen: invalid relation %d.",
1985                     m->reln);
1986         break;
1987     }
1988
1989     return matched;
1990 }
1991
1992 /* an optimization over plain strcmp() */
1993 #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
1994
1995 static int ascmagic(request_rec *r, unsigned char *buf, int nbytes)
1996 {
1997     int has_escapes = 0;
1998     unsigned char *s;
1999     char nbuf[HOWMANY + 1];     /* one extra for terminating '\0' */
2000     char *token;
2001     register struct names *p;
2002     int small_nbytes;
2003
2004     /* these are easy, do them first */
2005
2006     /*
2007      * for troff, look for . + letter + letter or .\"; this must be done to
2008      * disambiguate tar archives' ./file and other trash from real troff
2009      * input.
2010      */
2011     if (*buf == '.') {
2012         unsigned char *tp = buf + 1;
2013
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");
2019             return 1;
2020         }
2021     }
2022     if ((*buf == 'c' || *buf == 'C') && ap_isspace(*(buf + 1))) {
2023         /* Fortran */
2024         magic_rsl_puts(r, "text/plain");
2025         return 1;
2026     }
2027
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]);
2041                 if (has_escapes)
2042                     magic_rsl_puts(r, " (with escape sequences)");
2043                 return 1;
2044             }
2045         }
2046     }
2047
2048     switch (is_tar(buf, nbytes)) {
2049     case 1:
2050         /* V7 tar archive */
2051         magic_rsl_puts(r, "application/x-tar");
2052         return 1;
2053     case 2:
2054         /* POSIX tar archive */
2055         magic_rsl_puts(r, "application/x-tar");
2056         return 1;
2057     }
2058
2059     /* all else fails, but it is ascii... */
2060     if (has_escapes) {
2061         /* text with escape sequences */
2062         /* we leave this open for further differentiation later */
2063         magic_rsl_puts(r, "text/plain");
2064     }
2065     else {
2066         /* plain text */
2067         magic_rsl_puts(r, "text/plain");
2068     }
2069     return 1;
2070 }
2071
2072
2073 /*
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
2077  */
2078
2079 static struct {
2080     char *magic;
2081     int maglen;
2082     char *argv[3];
2083     int silent;
2084     char *encoding;     /* MUST be lowercase */
2085 } compr[] = {
2086
2087     /* we use gzip here rather than uncompress because we have to pass
2088      * it a full filename -- and uncompress only considers filenames
2089      * ending with .Z
2090      */
2091     {
2092         "\037\235", 2, {
2093             "gzip", "-dcq", NULL
2094         }, 0, "x-compress"
2095     },
2096     {
2097         "\037\213", 2, {
2098             "gzip", "-dcq", NULL
2099         }, 1, "x-gzip"
2100     },
2101     /*
2102      * XXX pcat does not work, cause I don't know how to make it read stdin,
2103      * so we use gzip
2104      */
2105     {
2106         "\037\036", 2, {
2107             "gzip", "-dcq", NULL
2108         }, 0, "x-gzip"
2109     },
2110 };
2111
2112 static int ncompr = sizeof(compr) / sizeof(compr[0]);
2113
2114 static int zmagic(request_rec *r, unsigned char *buf, int nbytes)
2115 {
2116     unsigned char *newbuf;
2117     int newsize;
2118     int i;
2119
2120     for (i = 0; i < ncompr; i++) {
2121         if (nbytes < compr[i].maglen)
2122             continue;
2123         if (memcmp(buf, compr[i].magic, compr[i].maglen) == 0)
2124             break;
2125     }
2126
2127     if (i == ncompr)
2128         return 0;
2129
2130     if ((newsize = uncompress(r, i, &newbuf, nbytes)) > 0) {
2131         tryit(r, newbuf, newsize, 0);
2132
2133         /* set encoding type in the request record */
2134         r->content_encoding = compr[i].encoding;
2135     }
2136     return 1;
2137 }
2138
2139
2140 struct uncompress_parms {
2141     request_rec *r;
2142     int method;
2143 };
2144
2145 static int uncompress_child(void *data, child_info *pinfo)
2146 {
2147     struct uncompress_parms *parm = data;
2148     int child_pid;
2149     char *new_argv[4];
2150
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;
2154     new_argv[3] = NULL;
2155
2156     if (compr[parm->method].silent) {
2157         close(STDERR_FILENO);
2158     }
2159
2160     child_pid = ap_spawnvp(compr[parm->method].argv[0],
2161                         new_argv);
2162     if (child_pid == -1)
2163         ap_log_rerror(APLOG_MARK, APLOG_ERR, parm->r,
2164                 MODNAME ": could not execute `%s'.",
2165                 compr[parm->method].argv[0]);
2166     return (child_pid);
2167 }
2168
2169
2170 static int uncompress(request_rec *r, int method, 
2171                       unsigned char **newch, int n)
2172 {
2173     struct uncompress_parms parm;
2174     BUFF *bout;
2175     ap_context_t *sub_pool;
2176
2177     parm.r = r;
2178     parm.method = method;
2179
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.
2183      */
2184     sub_pool = ap_make_sub_pool(r->pool);
2185
2186     if (!ap_bspawn_child(sub_pool, uncompress_child, &parm, kill_always,
2187                          NULL, &bout, NULL)) {
2188         ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
2189                     MODNAME ": couldn't spawn uncompress process: %s", r->uri);
2190         return -1;
2191     }
2192
2193     *newch = (unsigned char *) ap_palloc(r->pool, n);
2194     if ((n = ap_bread(bout, *newch, n)) <= 0) {
2195         ap_destroy_pool(sub_pool);
2196         ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
2197             MODNAME ": read failed %s", r->filename);
2198         return -1;
2199     }
2200     ap_destroy_pool(sub_pool);
2201     return n;
2202 }
2203
2204 /*
2205  * is_tar() -- figure out whether file is a tar archive.
2206  *
2207  * Stolen (by author of file utility) from the public domain tar program: Public
2208  * Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
2209  *
2210  * @(#)list.c 1.18 9/23/86 Public Domain - gnu $Id: mod_mime_magic.c,v 1.7
2211  * 1997/06/24 00:41:02 ikluft Exp ikluft $
2212  *
2213  * Comments changed and some code/comments reformatted for file command by Ian
2214  * Darwin.
2215  */
2216
2217 #define    isodigit(c)    ( ((c) >= '0') && ((c) <= '7') )
2218
2219 /*
2220  * Return 0 if the checksum is bad (i.e., probably not a tar archive), 1 for
2221  * old UNIX tar file, 2 for Unix Std (POSIX) tar file.
2222  */
2223
2224 static int is_tar(unsigned char *buf, int nbytes)
2225 {
2226     register union record *header = (union record *) buf;
2227     register int i;
2228     register long sum, recsum;
2229     register char *p;
2230
2231     if (nbytes < sizeof(union record))
2232                return 0;
2233
2234     recsum = from_oct(8, header->header.chksum);
2235
2236     sum = 0;
2237     p = header->charptr;
2238     for (i = sizeof(union record); --i >= 0;) {
2239         /*
2240          * We can't use unsigned char here because of old compilers, e.g. V7.
2241          */
2242         sum += 0xFF & *p++;
2243     }
2244
2245     /* Adjust checksum to count the "chksum" field as blanks. */
2246     for (i = sizeof(header->header.chksum); --i >= 0;)
2247         sum -= 0xFF & header->header.chksum[i];
2248     sum += ' ' * sizeof header->header.chksum;
2249
2250     if (sum != recsum)
2251         return 0;               /* Not a tar archive */
2252
2253     if (0 == strcmp(header->header.magic, TMAGIC))
2254         return 2;               /* Unix Standard tar archive */
2255
2256     return 1;                   /* Old fashioned tar archive */
2257 }
2258
2259
2260 /*
2261  * Quick and dirty octal conversion.
2262  *
2263  * Result is -1 if the field is invalid (all blank, or nonoctal).
2264  */
2265 static long from_oct(int digs, char *where)
2266 {
2267     register long value;
2268
2269     while (ap_isspace(*where)) {        /* Skip spaces */
2270         where++;
2271         if (--digs <= 0)
2272             return -1;          /* All blank field */
2273     }
2274     value = 0;
2275     while (digs > 0 && isodigit(*where)) {      /* Scan til nonoctal */
2276         value = (value << 3) | (*where++ - '0');
2277         --digs;
2278     }
2279
2280     if (digs > 0 && *where && !ap_isspace(*where))
2281         return -1;              /* Ended on non-space/nul */
2282
2283     return value;
2284 }
2285
2286 /*
2287  * Check for file-revision suffix
2288  *
2289  * This is for an obscure document control system used on an intranet.
2290  * The web representation of each file's revision has an @1, @2, etc
2291  * appended with the revision number.  This needs to be stripped off to
2292  * find the file suffix, which can be recognized by sending the name back
2293  * through a sub-request.  The base file name (without the @num suffix)
2294  * must exist because its type will be used as the result.
2295  */
2296 static int revision_suffix(request_rec *r)
2297 {
2298     int suffix_pos, result;
2299     char *sub_filename;
2300     request_rec *sub;
2301
2302 #if MIME_MAGIC_DEBUG
2303     ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
2304                 MODNAME ": revision_suffix checking %s", r->filename);
2305 #endif /* MIME_MAGIC_DEBUG */
2306
2307     /* check for recognized revision suffix */
2308     suffix_pos = strlen(r->filename) - 1;
2309     if (!ap_isdigit(r->filename[suffix_pos])) {
2310         return 0;
2311     }
2312     while (suffix_pos >= 0 && ap_isdigit(r->filename[suffix_pos]))
2313         suffix_pos--;
2314     if (suffix_pos < 0 || r->filename[suffix_pos] != '@') {
2315         return 0;
2316     }
2317
2318     /* perform sub-request for the file name without the suffix */
2319     result = 0;
2320     sub_filename = ap_pstrndup(r->pool, r->filename, suffix_pos);
2321 #if MIME_MAGIC_DEBUG
2322     ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
2323                 MODNAME ": subrequest lookup for %s", sub_filename);
2324 #endif /* MIME_MAGIC_DEBUG */
2325     sub = ap_sub_req_lookup_file(sub_filename, r);
2326
2327     /* extract content type/encoding/language from sub-request */
2328     if (sub->content_type) {
2329         r->content_type = ap_pstrdup(r->pool, sub->content_type);
2330 #if MIME_MAGIC_DEBUG
2331         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, r,
2332                     MODNAME ": subrequest %s got %s",
2333                     sub_filename, r->content_type);
2334 #endif /* MIME_MAGIC_DEBUG */
2335         if (sub->content_encoding)
2336             r->content_encoding =
2337                 ap_pstrdup(r->pool, sub->content_encoding);
2338         if (sub->content_language)
2339             r->content_language =
2340                 ap_pstrdup(r->pool, sub->content_language);
2341         result = 1;
2342     }
2343
2344     /* clean up */
2345     ap_destroy_sub_req(sub);
2346
2347     return result;
2348 }
2349
2350 /*
2351  * initialize the module
2352  */
2353
2354 static void magic_init(server_rec *main_server, ap_context_t *p)
2355 {
2356     int result;
2357     magic_server_config_rec *conf;
2358     magic_server_config_rec *main_conf;
2359     server_rec *s;
2360 #if MIME_MAGIC_DEBUG
2361     struct magic *m, *prevm;
2362 #endif /* MIME_MAGIC_DEBUG */
2363
2364     main_conf = ap_get_module_config(main_server->module_config, &mime_magic_module);
2365     for (s = main_server; s; s = s->next) {
2366         conf = ap_get_module_config(s->module_config, &mime_magic_module);
2367         if (conf->magicfile == NULL && s != main_server) {
2368             /* inherits from the parent */
2369             *conf = *main_conf;
2370         }
2371         else if (conf->magicfile) {
2372             result = apprentice(s, p);
2373             if (result == -1)
2374                 return;
2375 #if MIME_MAGIC_DEBUG
2376             prevm = 0;
2377             ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, s,
2378                         MODNAME ": magic_init 1 test");
2379             for (m = conf->magic; m; m = m->next) {
2380                 if (ap_isprint((((unsigned long) m) >> 24) & 255) &&
2381                     ap_isprint((((unsigned long) m) >> 16) & 255) &&
2382                     ap_isprint((((unsigned long) m) >> 8) & 255) &&
2383                     ap_isprint(((unsigned long) m) & 255)) {
2384                     ap_log_error(APLOG_MARK, APLOG_NOERRNO | APLOG_DEBUG, s,
2385                                 MODNAME ": magic_init 1: POINTER CLOBBERED! "
2386                                 "m=\"%c%c%c%c\" line=%d",
2387                                 (((unsigned long) m) >> 24) & 255,
2388                                 (((unsigned long) m) >> 16) & 255,
2389                                 (((unsigned long) m) >> 8) & 255,
2390                                 ((unsigned long) m) & 255,
2391                                 prevm ? prevm->lineno : -1);
2392                     break;
2393                 }
2394                 prevm = m;
2395             }
2396 #endif
2397         }
2398     }
2399 }
2400
2401 /*
2402  * Find the Content-Type from any resource this module has available
2403  */
2404
2405 static int magic_find_ct(request_rec *r)
2406 {
2407     int result;
2408     magic_server_config_rec *conf;
2409
2410     /* the file has to exist */
2411     if (r->finfo.st_mode == 0 || !r->filename) {
2412         return DECLINED;
2413     }
2414
2415     /* was someone else already here? */
2416     if (r->content_type) {
2417         return DECLINED;
2418     }
2419
2420     conf = ap_get_module_config(r->server->module_config, &mime_magic_module);
2421     if (!conf || !conf->magic) {
2422         return DECLINED;
2423     }
2424
2425     /* initialize per-request info */
2426     if (!magic_set_config(r)) {
2427         return HTTP_INTERNAL_SERVER_ERROR;
2428     }
2429
2430     /* try excluding file-revision suffixes */
2431     if (revision_suffix(r) != 1) {
2432         /* process it based on the file contents */
2433         if ((result = magic_process(r)) != OK) {
2434             return result;
2435         }
2436     }
2437
2438     /* if we have any results, put them in the request structure */
2439     return magic_rsl_to_request(r);
2440 }
2441
2442 /*
2443  * Apache API module interface
2444  */
2445
2446 module mime_magic_module =
2447 {
2448     STANDARD_MODULE_STUFF,
2449     magic_init,                 /* initializer */
2450     NULL,                       /* dir config creator */
2451     NULL,                       /* dir merger --- default is to override */
2452     create_magic_server_config, /* server config */
2453     merge_magic_server_config,  /* merge server config */
2454     mime_magic_cmds,            /* command ap_table_t */
2455     NULL,                       /* handlers */
2456     NULL,                       /* filename translation */
2457     NULL,                       /* check_user_id */
2458     NULL,                       /* check auth */
2459     NULL,                       /* check access */
2460     magic_find_ct,              /* type_checker */
2461     NULL,                       /* fixups */
2462     NULL,                       /* logger */
2463     NULL,                       /* header parser */
2464     NULL,                       /* child_init */
2465     NULL,                       /* child_exit */
2466     NULL                        /* post read-request */
2467 };