/* * Copyright (C) 1996-2000 Michael R. Elkins * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */ #include "mutt.h" #include "mutt_regex.h" #include "mailbox.h" #include "mime.h" #include "rfc2047.h" #include "rfc2231.h" #ifdef HAVE_PGP #include "pgp.h" #endif /* HAVE_PGP */ #include #include #include #include /* Reads an arbitrarily long header field, and looks ahead for continuation * lines. ``line'' must point to a dynamically allocated string; it is * increased if more space is required to fit the whole line. */ static char *read_rfc822_line (FILE *f, char *line, size_t *linelen) { char *buf = line; char ch; size_t offset = 0; FOREVER { if (fgets (buf, *linelen - offset, f) == NULL || /* end of file or */ (ISSPACE (*line) && !offset)) /* end of headers */ { *line = 0; return (line); } buf += strlen (buf) - 1; if (*buf == '\n') { /* we did get a full line. remove trailing space */ while (ISSPACE (*buf)) *buf-- = 0; /* we cannot come beyond line's beginning because * it begins with a non-space */ /* check to see if the next line is a continuation line */ if ((ch = fgetc (f)) != ' ' && ch != '\t') { ungetc (ch, f); return (line); /* next line is a separate header field or EOH */ } /* eat tabs and spaces from the beginning of the continuation line */ while ((ch = fgetc (f)) == ' ' || ch == '\t') ; ungetc (ch, f); *++buf = ' '; /* string is still terminated because we removed at least one whitespace char above */ } buf++; offset = buf - line; if (*linelen < offset + STRING) { /* grow the buffer */ *linelen += STRING; safe_realloc ((void **) &line, *linelen); buf = line + offset; } } /* not reached */ } static LIST *mutt_parse_references (char *s) { LIST *t, *lst = NULL; while ((s = strtok (s, " \t")) != NULL) { /* * some mail clients add other garbage besides message-ids, so do a quick * check to make sure this looks like a valid message-id */ if (*s == '<') { t = (LIST *)safe_malloc (sizeof (LIST)); t->data = safe_strdup (s); t->next = lst; lst = t; } s = NULL; } return (lst); } int mutt_check_encoding (const char *c) { if (mutt_strncasecmp ("7bit", c, sizeof ("7bit")-1) == 0) return (ENC7BIT); else if (mutt_strncasecmp ("8bit", c, sizeof ("8bit")-1) == 0) return (ENC8BIT); else if (mutt_strncasecmp ("binary", c, sizeof ("binary")-1) == 0) return (ENCBINARY); else if (mutt_strncasecmp ("quoted-printable", c, sizeof ("quoted-printable")-1) == 0) return (ENCQUOTEDPRINTABLE); else if (mutt_strncasecmp ("base64", c, sizeof("base64")-1) == 0) return (ENCBASE64); else if (mutt_strncasecmp ("x-uuencode", c, sizeof("x-uuencode")-1) == 0) return (ENCUUENCODED); #ifdef SUN_ATTACHMENT else if (mutt_strncasecmp ("uuencode", c, sizeof("uuencode")-1) == 0) return (ENCUUENCODED); #endif else return (ENCOTHER); } static PARAMETER *parse_parameters (const char *s) { PARAMETER *head = 0, *cur = 0, *new; char buffer[LONG_STRING]; const char *p; size_t i; dprint (2, (debugfile, "parse_parameters: `%s'\n", s)); while (*s) { if ((p = strpbrk (s, "=;")) == NULL) { dprint(1, (debugfile, "parse_parameters: malformed parameter: %s\n", s)); goto bail; } /* if we hit a ; now the parameter has no value, just skip it */ if (*p != ';') { i = p - s; new = mutt_new_parameter (); new->attribute = safe_malloc (i + 1); memcpy (new->attribute, s, i); new->attribute[i] = 0; /* remove whitespace from the end of the attribute name */ while (ISSPACE (new->attribute[--i])) new->attribute[i] = 0; s = p + 1; /* skip over the = */ SKIPWS (s); if (*s == '"') { s++; for (i=0; *s && *s != '"' && i < sizeof (buffer) - 1; i++, s++) { if (*s == '\\') { /* Quote the next character */ buffer[i] = s[1]; if (!*++s) break; } else buffer[i] = *s; } buffer[i] = 0; if (*s) s++; /* skip over the " */ } else { for (i=0; *s && *s != ' ' && *s != ';' && i < sizeof (buffer) - 1; i++, s++) buffer[i] = *s; buffer[i] = 0; } new->value = safe_strdup (buffer); dprint (2, (debugfile, "parse_parameter: `%s' = `%s'\n", new->attribute, new->value)); /* Add this parameter to the list */ if (head) { cur->next = new; cur = cur->next; } else head = cur = new; } else { dprint (1, (debugfile, "parse_parameters(): parameter with no value: %s\n", s)); s = p; } /* Find the next parameter */ if (*s != ';' && (s = strchr (s, ';')) == NULL) break; /* no more parameters */ do { s++; /* Move past any leading whitespace */ SKIPWS (s); } while (*s == ';'); /* skip empty parameters */ } bail: rfc2231_decode_parameters (&head); return (head); } int mutt_check_mime_type (const char *s) { if (mutt_strcasecmp ("text", s) == 0) return TYPETEXT; else if (mutt_strcasecmp ("multipart", s) == 0) return TYPEMULTIPART; #ifdef SUN_ATTACHMENT else if (mutt_strcasecmp ("x-sun-attachment", s) == 0) return TYPEMULTIPART; #endif else if (mutt_strcasecmp ("application", s) == 0) return TYPEAPPLICATION; else if (mutt_strcasecmp ("message", s) == 0) return TYPEMESSAGE; else if (mutt_strcasecmp ("image", s) == 0) return TYPEIMAGE; else if (mutt_strcasecmp ("audio", s) == 0) return TYPEAUDIO; else if (mutt_strcasecmp ("video", s) == 0) return TYPEVIDEO; else if (mutt_strcasecmp ("model", s) == 0) return TYPEMODEL; else return TYPEOTHER; } void mutt_parse_content_type (char *s, BODY *ct) { char *pc; char *subtype; safe_free((void **)&ct->subtype); mutt_free_parameter(&ct->parameter); /* First extract any existing parameters */ if ((pc = strchr(s, ';')) != NULL) { *pc++ = 0; while (*pc && ISSPACE (*pc)) pc++; ct->parameter = parse_parameters(pc); /* Some pre-RFC1521 gateways still use the "name=filename" convention, * but if a filename has already been set in the content-disposition, * let that take precedence, and don't set it here */ if ((pc = mutt_get_parameter( "name", ct->parameter)) != 0 && !ct->filename) ct->filename = safe_strdup(pc); #ifdef SUN_ATTACHMENT /* this is deep and utter perversion */ if ((pc = mutt_get_parameter ("conversions", ct->parameter)) != 0) ct->encoding = mutt_check_encoding (pc); #endif } /* Now get the subtype */ if ((subtype = strchr(s, '/'))) { *subtype++ = '\0'; for(pc = subtype; *pc && !ISSPACE(*pc) && *pc != ';'; pc++) ; *pc = '\0'; ct->subtype = safe_strdup (subtype); } /* Finally, get the major type */ ct->type = mutt_check_mime_type (s); #ifdef SUN_ATTACHMENT if (mutt_strcasecmp ("x-sun-attachment", s) == 0) ct->subtype = safe_strdup ("x-sun-attachment"); #endif if (ct->type == TYPEOTHER) { ct->xtype = safe_strdup (s); } if (ct->subtype == NULL) { /* Some older non-MIME mailers (i.e., mailtool, elm) have a content-type * field, so we can attempt to convert the type to BODY here. */ if (ct->type == TYPETEXT) ct->subtype = safe_strdup ("plain"); else if (ct->type == TYPEAUDIO) ct->subtype = safe_strdup ("basic"); else if (ct->type == TYPEMESSAGE) ct->subtype = safe_strdup ("rfc822"); else if (ct->type == TYPEOTHER) { char buffer[SHORT_STRING]; ct->type = TYPEAPPLICATION; snprintf (buffer, sizeof (buffer), "x-%s", s); ct->subtype = safe_strdup (buffer); } else ct->subtype = safe_strdup ("x-unknown"); } /* Default character set for text types. */ if (ct->type == TYPETEXT) { if (!(pc = mutt_get_parameter ("charset", ct->parameter))) mutt_set_parameter ("charset", "us-ascii", &ct->parameter); } } static void parse_content_disposition (char *s, BODY *ct) { PARAMETER *parms; if (!mutt_strncasecmp ("inline", s, 6)) ct->disposition = DISPINLINE; else if (!mutt_strncasecmp ("form-data", s, 9)) ct->disposition = DISPFORMDATA; else ct->disposition = DISPATTACH; /* Check to see if a default filename was given */ if ((s = strchr (s, ';')) != NULL) { s++; SKIPWS (s); if ((s = mutt_get_parameter ("filename", (parms = parse_parameters (s)))) != 0) mutt_str_replace (&ct->filename, s); if ((s = mutt_get_parameter ("name", parms)) != 0) ct->form_name = safe_strdup (s); mutt_free_parameter (&parms); } } /* args: * fp stream to read from * * digest 1 if reading subparts of a multipart/digest, 0 * otherwise */ BODY *mutt_read_mime_header (FILE *fp, int digest) { BODY *p = mutt_new_body(); char *c; char *line = safe_malloc (LONG_STRING); size_t linelen = LONG_STRING; p->hdr_offset = ftell(fp); p->encoding = ENC7BIT; /* default from RFC1521 */ p->type = digest ? TYPEMESSAGE : TYPETEXT; p->disposition = DISPINLINE; while (*(line = read_rfc822_line (fp, line, &linelen)) != 0) { /* Find the value of the current header */ if ((c = strchr (line, ':'))) { *c = 0; c++; SKIPWS (c); if (!*c) { dprint (1, (debugfile, "mutt_read_mime_header(): skipping empty header field: %s\n", line)); continue; } } else { dprint (1, (debugfile, "read_mime_header: bogus MIME header: %s\n", line)); break; } if (!mutt_strncasecmp ("content-", line, 8)) { if (!mutt_strcasecmp ("type", line + 8)) mutt_parse_content_type (c, p); else if (!mutt_strcasecmp ("transfer-encoding", line + 8)) p->encoding = mutt_check_encoding (c); else if (!mutt_strcasecmp ("disposition", line + 8)) parse_content_disposition (c, p); else if (!mutt_strcasecmp ("description", line + 8)) { mutt_str_replace (&p->description, c); rfc2047_decode (&p->description); } } #ifdef SUN_ATTACHMENT else if (!mutt_strncasecmp ("x-sun-", line, 6)) { if (!mutt_strcasecmp ("data-type", line + 6)) mutt_parse_content_type (c, p); else if (!mutt_strcasecmp ("encoding-info", line + 6)) p->encoding = mutt_check_encoding (c); else if (!mutt_strcasecmp ("content-lines", line + 6)) mutt_set_parameter ("content-lines", safe_strdup (c), &(p->parameter)); else if (!mutt_strcasecmp ("data-description", line + 6)) { mutt_str_replace (&p->description, c); rfc2047_decode (&p->description); } } #endif } p->offset = ftell (fp); /* Mark the start of the real data */ if (p->type == TYPETEXT && !p->subtype) p->subtype = safe_strdup ("plain"); else if (p->type == TYPEMESSAGE && !p->subtype) p->subtype = safe_strdup ("rfc822"); FREE (&line); return (p); } void mutt_parse_part (FILE *fp, BODY *b) { char *bound = 0; switch (b->type) { case TYPEMULTIPART: #ifdef SUN_ATTACHMENT if ( !mutt_strcasecmp (b->subtype, "x-sun-attachment") ) bound = "--------"; else #endif bound = mutt_get_parameter ("boundary", b->parameter); fseek (fp, b->offset, SEEK_SET); b->parts = mutt_parse_multipart (fp, bound, b->offset + b->length, mutt_strcasecmp ("digest", b->subtype) == 0); break; case TYPEMESSAGE: if (b->subtype) { fseek (fp, b->offset, SEEK_SET); if (mutt_is_message_type(b->type, b->subtype)) b->parts = mutt_parse_messageRFC822 (fp, b); else if (mutt_strcasecmp (b->subtype, "external-body") == 0) b->parts = mutt_read_mime_header (fp, 0); else return; } break; default: return; } /* try to recover from parsing error */ if (!b->parts) { b->type = TYPETEXT; mutt_str_replace (&b->subtype, "plain"); } } /* parse a MESSAGE/RFC822 body * * args: * fp stream to read from * * parent structure which contains info about the message/rfc822 * body part * * NOTE: this assumes that `parent->length' has been set! */ BODY *mutt_parse_messageRFC822 (FILE *fp, BODY *parent) { BODY *msg; parent->hdr = mutt_new_header (); parent->hdr->offset = ftell (fp); parent->hdr->env = mutt_read_rfc822_header (fp, parent->hdr, 0, 0); msg = parent->hdr->content; /* ignore the length given in the content-length since it could be wrong and we already have the info to calculate the correct length */ /* if (msg->length == -1) */ msg->length = parent->length - (msg->offset - parent->offset); /* if body of this message is empty, we can end up with a negative length */ if (msg->length < 0) msg->length = 0; mutt_parse_part(fp, msg); return (msg); } /* parse a multipart structure * * args: * fp stream to read from * * boundary body separator * * end_off length of the multipart body (used when the final * boundary is missing to avoid reading too far) * * digest 1 if reading a multipart/digest, 0 otherwise */ BODY *mutt_parse_multipart (FILE *fp, const char *boundary, long end_off, int digest) { #ifdef SUN_ATTACHMENT int lines; #endif int blen, len, crlf = 0; char buffer[LONG_STRING]; BODY *head = 0, *last = 0, *new = 0; int i;
/*
 * Author: Tatu Ylonen <ylo@cs.hut.fi>
 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
 *                    All rights reserved
 * Simple pattern matching, with '*' and '?' as wildcards.
 *
 * As far as I am concerned, the code I have written for this software
 * can be used freely for any purpose.  Any derived versions of this
 * software must be clearly marked as such, and if the derived work is
 * incompatible with the protocol description in the RFC file, it must be
 * called by a name other than "ssh" or "Secure Shell".
 */
/*
 * Copyright (c) 2000 Markus Friedl.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "includes.h"
RCSID("$OpenBSD: match.c,v 1.19 2002/03/01 13:12:10 markus Exp $");

#include "match.h"
#include "xmalloc.h"

/*
 * Returns true if the given string matches the pattern (which may contain ?
 * and * as wildcards), and zero if it does not match.
 */

int
match_pattern(const char *s, const char *pattern)
{
	for (;;) {
		/* If at end of pattern, accept if also at end of string. */
		if (!*pattern)
			return !*s;

		if (*pattern == '*') {
			/* Skip the asterisk. */
			pattern++;

			/* If at end of pattern, accept immediately. */
			if (!*pattern)
				return 1;

			/* If next character in pattern is known, optimize. */
			if (*pattern != '?' && *pattern != '*') {
				/*
				 * Look instances of the next character in
				 * pattern, and try to match starting from
				 * those.
				 */
				for (; *s; s++)
					if (*s == *pattern &&
					    match_pattern(s + 1, pattern + 1))
						return 1;
				/* Failed. */
				return 0;
			}
			/*
			 * Move ahead one character at a time and try to
			 * match at each position.
			 */
			for (; *s; s++)
				if (match_pattern(s, pattern))
					return 1;
			/* Failed. */
			return 0;
		}
		/*
		 * There must be at least one more character in the string.
		 * If we are at the end, fail.
		 */
		if (!*s)
			return 0;

		/* Check if the next character of the string is acceptable. */
		if (*pattern != '?' && *pattern != *s)
			return 0;

		/* Move to the next character, both in string and in pattern. */
		s++;
		pattern++;
	}
	/* NOTREACHED */
}

/*
 * Tries to match the string against the
 * comma-separated sequence of subpatterns (each possibly preceded by ! to
 * indicate negation).  Returns -1 if negation matches, 1 if there is
 * a positive match, 0 if there is no match at all.
 */

int
match_pattern_list(const char *string, const char *pattern, u_int len,
    int dolower)
{
	char sub[1024];
	int negated;
	int got_positive;
	u_int i, subi;

	got_positive = 0;
	for (i = 0; i < len;) {
		/* Check if the subpattern is negated. */
		if (pattern[i