summaryrefslogtreecommitdiffstats
path: root/src/global/match_service.c
blob: 856355eddbb6809b32159482325079f59f9c222f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*++
/* NAME
/*	match_service 3
/* SUMMARY
/*	simple master.cf service name.type pattern matcher
/* SYNOPSIS
/*	#include <match_service.h>
/*
/*	ARGV	*match_service_init(pattern_list)
/*	const char *pattern_list;
/*
/*	ARGV	*match_service_init_argv(pattern_list)
/*	char	**pattern_list;
/*
/*	int	match_service_match(list, name_type)
/*	ARGV	*list;
/*	const char *name_type;
/*
/*	void match_service_free(list)
/*	ARGV	*list;
/* DESCRIPTION
/*	This module implements pattern matching for Postfix master.cf
/*	services.  This is more precise than using domain_list(3),
/*	because match_service(3) won't treat a dotted service name
/*	as a domain hierarchy. Moreover, this module has the advantage
/*	that it does not drag in all the LDAP, SQL and other map
/*	lookup client code into programs that don't need it.
/*
/*	Each pattern is of the form "name/type" or "type", where
/*	"name" and "type" are the first two fields of a master.cf
/*	entry. Patterns are separated by whitespace and/or commas.
/*	Matches are case insensitive. Patterns are matched in the
/*	specified order, and the matching process stops at the first
/*	match.  In order to reverse the result of a pattern match,
/*	precede a pattern with an exclamation point (!).
/*
/*	For backwards compatibility, the form name.type is still
/*	supported.
/*
/*	match_service_init() parses the pattern list. The result
/*	must be passed to match_service_match() or match_service_free().
/*
/*	match_service_init_argv() provides an alternate interface
/*	for pre-parsed strings.
/*
/*	match_service_match() matches one service name.type string
/*	against the specified pattern list.
/*
/*	match_service_free() releases storage allocated by
/*	match_service_init().
/* DIAGNOSTICS
/*	Fatal error: out of memory, malformed pattern.
/*	Panic: malformed search string.
/* SEE ALSO
/*	domain_list(3) match domain names.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

#include <sys_defs.h>
#include <string.h>

#ifdef STRCASECMP_IN_STRINGS_H
#include <strings.h>
#endif

/* Utility library. */

#include <msg.h>
#include <argv.h>
#include <mymalloc.h>
#include <stringops.h>
#include <match_service.h>

/* match_service_compat - backwards compatibility */

static void match_service_compat(ARGV *argv)
{
    char  **cpp;
    char   *cp;

    for (cpp = argv->argv; *cpp; cpp++) {
	if (strrchr(*cpp, '/') == 0 && (cp = strrchr(*cpp, '.')) != 0)
	    *cp = '/';
    }
}

/* match_service_init - initialize pattern list */

ARGV   *match_service_init(const char *patterns)
{
    const char *delim = CHARS_COMMA_SP;
    ARGV   *list = argv_alloc(1);
    char   *saved_patterns = mystrdup(patterns);
    char   *bp = saved_patterns;
    const char *item;

    while ((item = mystrtok(&bp, delim)) != 0)
	argv_add(list, item, (char *) 0);
    argv_terminate(list);
    myfree(saved_patterns);
    match_service_compat(list);
    return (list);
}

/* match_service_init_argv - impedance adapter */

ARGV   *match_service_init_argv(char **patterns)
{
    ARGV   *list = argv_alloc(1);
    char  **cpp;

    for (cpp = patterns; *cpp; cpp++)
	argv_add(list, *cpp, (char *) 0);
    argv_terminate(list);
    match_service_compat(list);
    return (list);
}

/* match_service_match - match service name.type against pattern list */

int     match_service_match(ARGV *list, const char *name_type)
{
    const char *myname = "match_service_match";
    const char *type;
    char  **cpp;
    char   *pattern;
    int     match;

    /*
     * Quick check for empty list.
     */
    if (list->argv[0] == 0)
	return (0);

    /*
     * Sanity check.
     */
    if ((type = strrchr(name_type, '/')) == 0 || *++type == 0)
	msg_panic("%s: malformed service: \"%s\"; need \"name/type\" format",
		  myname, name_type);

    /*
     * Iterate over all patterns in the list, stop at the first match.
     */
    for (cpp = list->argv; (pattern = *cpp) != 0; cpp++) {
	if (msg_verbose)
	    msg_info("%s: %s ~? %s", myname, name_type, pattern);
	for (match = 1; *pattern == '!'; pattern++)
	    match = !match;
	if (strcasecmp(strchr(pattern, '/') ? name_type : type, pattern) == 0) {
	    if (msg_verbose)
		msg_info("%s: %s: found match", myname, name_type);
	    return (match);
	}
    }
    if (msg_verbose)
	msg_info("%s: %s: no match", myname, name_type);
    return (0);
}

/* match_service_free - release storage */

void    match_service_free(ARGV *list)
{
    argv_free(list);
}