summaryrefslogtreecommitdiffstats
path: root/src/global/map_search.c
blob: b10f7d516829f802527ecf4f46ec627702d34b83 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*++
/* NAME
/*	map_search_search 3
/* SUMMARY
/*	lookup table search list support
/* SYNOPSIS
/*	#include <map_search_search.h>
/*
/*	typedef struct {
/* .in +4
/*	    char   *map_type_name;	/* type:name, owned */
/*	    char   *search_order;	/* null or owned */
/* .in -4
/*	} MAP_SEARCH;
/*
/*	void	map_search_init(
/*	const NAME_CODE *search_actions)
/*
/*	const MAP_SEARCH *map_search_create(
/*	const char *map_spec)
/*
/*	const MAP_SEARCH *map_search_lookup(
/*	const char *map_spec);
/* DESCRIPTION
/*	This module implements configurable search order support
/*	for Postfix lookup tables.
/*
/*	map_search_init() must be called once, before other functions
/*	in this module.
/*
/*	map_search_create() creates a MAP_SEARCH instance for
/*	map_spec, ignoring duplicate requests.
/*
/*	map_search_lookup() looks up the MAP_SEARCH instance that
/*	was created by map_search_create().
/*
/*	Arguments:
/* .IP search_actions
/*	The mapping from search action string form to numeric form.
/*	The numbers must be in the range [1..126] (inclusive). The
/*	value 0 is reserved for the MAP_SEARCH.search_order terminator,
/*	and the value MAP_SEARCH_CODE_UNKNOWN is reserved for the
/*	'not found' result. The argument is copied (the pointer
/*	value, not the table).
/* .IP map_spec
/*	lookup table and optional search order: either maptype:mapname,
/*	or { maptype:mapname, { search = name, name }}. The search
/*	attribute is optional. The comma is equivalent to whitespace.
/* DIAGNOSTICS
/*	map_search_create() returns a null pointer when a map_spec
/*	is a) malformed, b) specifies an unexpected attribute name,
/*	c) the search attribute contains an unknown name. Thus,
/*	map_search_create() will never return a search_order that
/*	contains the value MAP_SEARCH_CODE_UNKNOWN.
/*
/*	Panic: interface violations. Fatal errors: out of memory.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	Google, Inc.
/*	111 8th Avenue
/*	New York, NY 10011, USA
/*--*/

 /*
  * System library.
  */
#include <sys_defs.h>
#include <string.h>

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

 /*
  * Utility library.
  */
#include <htable.h>
#include <msg.h>
#include <mymalloc.h>
#include <name_code.h>
#include <stringops.h>
#include <vstring.h>

 /*
  * Global library.
  */
#include <map_search.h>

 /*
  * Application-specific.
  */
static HTABLE *map_search_table;
static const NAME_CODE *map_search_actions;

#define STR(x) vstring_str(x)

/* map_search_init - one-time initialization */

void    map_search_init(const NAME_CODE *search_actions)
{
    if (map_search_table != 0 || map_search_actions != 0)
	msg_panic("map_search_init: multiple calls");
    map_search_table = htable_create(100);
    map_search_actions = search_actions;
}

/* map_search_create - store MAP_SEARCH instance */

const MAP_SEARCH *map_search_create(const char *map_spec)
{
    char   *copy_of_map_spec = 0;
    char   *bp = 0;
    const char *const_err;
    char   *heap_err = 0;
    VSTRING *search_order = 0;
    const char *map_type_name;
    char   *attr_name_val = 0;
    char   *attr_name = 0;
    char   *attr_value = 0;
    MAP_SEARCH *map_search;
    char   *atom;
    int     code;

    /*
     * Sanity check.
     */
    if (map_search_table == 0 || map_search_actions == 0)
	msg_panic("map_search_create: missing initialization");

    /*
     * Allow exact duplicates. This won't catch duplicates that differ only
     * in their use of whitespace or comma.
     */
    if ((map_search =
	 (MAP_SEARCH *) htable_find(map_search_table, map_spec)) != 0)
	return (map_search);

    /*
     * Macro for readability and safety. Let the compiler worry about code
     * duplication and redundant conditions.
     */
#define MAP_SEARCH_CREATE_RETURN(x) do { \
	if (copy_of_map_spec) myfree(copy_of_map_spec); \
	if (heap_err) myfree(heap_err); \
	if (search_order) vstring_free(search_order); \
	return (x); \
    } while (0)

    /*
     * Long form specifies maptype_mapname and optional search attribute.
     */
    if (*map_spec == CHARS_BRACE[0]) {
	bp = copy_of_map_spec = mystrdup(map_spec);
	if ((heap_err = extpar(&bp, CHARS_BRACE, EXTPAR_FLAG_STRIP)) != 0) {
	    msg_warn("malformed map specification: '%s'", heap_err);
	    MAP_SEARCH_CREATE_RETURN(0);
	} else if ((map_type_name = mystrtokq(&bp, CHARS_COMMA_SP,
					      CHARS_BRACE)) == 0) {
	    msg_warn("empty map specification: '%s'", map_spec);
	    MAP_SEARCH_CREATE_RETURN(0);
	}
    } else {
	map_type_name = map_spec;
    }

    /*
     * Sanity check the map spec before parsing attributes.
     */
    if (strchr(map_type_name, ':') == 0) {
	msg_warn("malformed map specification: '%s'", map_spec);
	msg_warn("expected maptype:mapname instead of '%s'", map_type_name);
	MAP_SEARCH_CREATE_RETURN(0);
    }

    /*
     * Parse the attribute list. XXX This does not detect multiple attributes
     * with the same attribute name.
     */
    if (bp != 0) {
	while ((attr_name_val = mystrtokq(&bp, CHARS_COMMA_SP, CHARS_BRACE)) != 0) {
	    if (*attr_name_val == CHARS_BRACE[0]) {
		if ((heap_err = extpar(&attr_name_val, CHARS_BRACE,
				       EXTPAR_FLAG_STRIP)) != 0) {
		    msg_warn("malformed map attribute: %s", heap_err);
		    MAP_SEARCH_CREATE_RETURN(0);
		}
	    }
	    if ((const_err = split_nameval(attr_name_val, &attr_name,
					   &attr_value)) != 0) {
		msg_warn("malformed map attribute in '%s': '%s'",
			 map_spec, const_err);
		MAP_SEARCH_CREATE_RETURN(0);
	    }
	    if (strcasecmp(attr_name, MAP_SEARCH_ATTR_NAME_SEARCH) != 0) {
		msg_warn("unknown map attribute in '%s': '%s'",
			 map_spec, attr_name);
		MAP_SEARCH_CREATE_RETURN(0);
	    }
	}
    }

    /*
     * Parse the search list if any.
     */
    if (attr_name != 0) {
	search_order = vstring_alloc(10);
	while ((atom = mystrtok(&attr_value, CHARS_COMMA_SP)) != 0) {
	    if ((code = name_code(map_search_actions, NAME_CODE_FLAG_NONE,
				  atom)) == MAP_SEARCH_CODE_UNKNOWN) {
		msg_warn("unknown search type '%s' in '%s'", atom, map_spec);
		MAP_SEARCH_CREATE_RETURN(0);
	    }
	    VSTRING_ADDCH(search_order, code);
	}
	VSTRING_TERMINATE(search_order);
    }

    /*
     * Bundle up the result.
     */
    map_search = (MAP_SEARCH *) mymalloc(sizeof(*map_search));
    map_search->map_type_name = mystrdup(map_type_name);
    if (search_order) {
	map_search->search_order = vstring_export(search_order);
	search_order = 0;
    } else {
	map_search->search_order = 0;
    }

    /*
     * Save the ACL to cache.
     */
    (void) htable_enter(map_search_table, map_spec, map_search);

    MAP_SEARCH_CREATE_RETURN(map_search);
}

/* map_search_lookup - lookup MAP_SEARCH instance */

const MAP_SEARCH *map_search_lookup(const char *map_spec)
{

    /*
     * Sanity check.
     */
    if (map_search_table == 0 || map_search_actions == 0)
	msg_panic("map_search_lookup: missing initialization");

    return ((MAP_SEARCH *) htable_find(map_search_table, map_spec));
}

 /*
  * Test driver.
  */
#ifdef TEST
#include <stdlib.h>

 /*
  * Test search actions.
  */
#define TEST_NAME_1	"one"
#define TEST_NAME_2	"two"
#define TEST_CODE_1	1
#define TEST_CODE_2	2

#define BAD_NAME	"bad"

static const NAME_CODE search_actions[] = {
    TEST_NAME_1, TEST_CODE_1,
    TEST_NAME_2, TEST_CODE_2,
    0, MAP_SEARCH_CODE_UNKNOWN,
};

/* Helpers to simplify tests. */

static const char *string_or_null(const char *s)
{
    return (s ? s : "(null)");
}

static char *escape_order(VSTRING *buf, const char *search_order)
{
    return (STR(escape(buf, search_order, strlen(search_order))));
}

int     main(int argc, char **argv)
{
    /* Test cases with inputs and expected outputs. */
    typedef struct TEST_CASE {
	const char *map_spec;
	int     exp_return;		/* 0=fail, 1=success */
	const char *exp_map_type_name;	/* 0 or match */
	const char *exp_search_order;	/* 0 or match */
    } TEST_CASE;
    static TEST_CASE test_cases[] = {
	{"type", 0, 0, 0},
	{"type:name", 1, "type:name", 0},
	{"{type:name}", 1, "type:name", 0},
	{"{type:name", 0, 0, 0},	/* } */
	{"{type}", 0, 0, 0},
	{"{type:name foo}", 0, 0, 0},
	{"{type:name foo=bar}", 0, 0, 0},
	{"{type:name search_order=}", 1, "type:name", ""},
	{"{type:name search_order=one, two}", 0, 0, 0},
	{"{type:name {search_order=one, two}}", 1, "type:name", "\01\02"},
	{"{type:name {search_order=one, two, bad}}", 0, 0, 0},
	{"{inline:{a=b} {search_order=one, two}}", 1, "inline:{a=b}", "\01\02"},
	{"{inline:{a=b, c=d} {search_order=one, two}}", 1, "inline:{a=b, c=d}", "\01\02"},
	{0},
    };
    TEST_CASE *test_case;

    /* Actual results. */
    const MAP_SEARCH *map_search_from_create;
    const MAP_SEARCH *map_search_from_create_2nd;
    const MAP_SEARCH *map_search_from_lookup;

    /* Findings. */
    int     tests_failed = 0;
    int     test_failed;

    /* Scratch */
    VSTRING *expect_escaped = vstring_alloc(100);
    VSTRING *actual_escaped = vstring_alloc(100);

    map_search_init(search_actions);

    for (tests_failed = 0, test_case = test_cases; test_case->map_spec;
	 tests_failed += test_failed, test_case++) {
	test_failed = 0;
	msg_info("test case %d: '%s'",
		 (int) (test_case - test_cases), test_case->map_spec);
	map_search_from_create = map_search_create(test_case->map_spec);
	if (!test_case->exp_return != !map_search_from_create) {
	    if (map_search_from_create)
		msg_warn("test case %d return expected %s actual {%s, %s}",
			 (int) (test_case - test_cases),
			 test_case->exp_return ? "success" : "fail",
			 map_search_from_create->map_type_name,
			 escape_order(actual_escaped,
				      map_search_from_create->search_order));
	    else
		msg_warn("test case %d return expected %s actual %s",
			 (int) (test_case - test_cases), "success",
			 map_search_from_create ? "success" : "fail");
	    test_failed = 1;
	    continue;
	}
	if (test_case->exp_return == 0)
	    continue;
	map_search_from_lookup = map_search_lookup(test_case->map_spec);
	if (map_search_from_create != map_search_from_lookup) {
	    msg_warn("test case %d map_search_lookup expected=%p actual=%p",
		     (int) (test_case - test_cases),
		     map_search_from_create, map_search_from_lookup);
	    test_failed = 1;
	}
	map_search_from_create_2nd = map_search_create(test_case->map_spec);
	if (map_search_from_create != map_search_from_create_2nd) {
	    msg_warn("test case %d repeated map_search_create "
		     "expected=%p actual=%p",
		     (int) (test_case - test_cases),
		     map_search_from_create, map_search_from_create_2nd);
	    test_failed = 1;
	}
	if (strcmp(string_or_null(test_case->exp_map_type_name),
		   string_or_null(map_search_from_create->map_type_name))) {
	    msg_warn("test case %d map_type_name expected=%s actual=%s",
		     (int) (test_case - test_cases),
		     string_or_null(test_case->exp_map_type_name),
		     string_or_null(map_search_from_create->map_type_name));
	    test_failed = 1;
	}
	if (strcmp(string_or_null(test_case->exp_search_order),
		   string_or_null(map_search_from_create->search_order))) {
	    msg_warn("test case %d search_order expected=%s actual=%s",
		     (int) (test_case - test_cases),
		     escape_order(expect_escaped,
			       string_or_null(test_case->exp_search_order)),
		     escape_order(actual_escaped,
		     string_or_null(map_search_from_create->search_order)));
	    test_failed = 1;
	}
    }
    vstring_free(expect_escaped);
    vstring_free(actual_escaped);

    if (tests_failed)
	msg_info("tests failed: %d", tests_failed);
    exit(tests_failed != 0);
}

#endif