xref: /netbsd-src/external/ibm-public/postfix/dist/src/global/map_search.c (revision 15a984a0d95c8f96abe9717ee6241762c55dc106)
1 /*	$NetBSD: map_search.c,v 1.3 2022/10/08 16:12:45 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	map_search_search 3
6 /* SUMMARY
7 /*	lookup table search list support
8 /* SYNOPSIS
9 /*	#include <map_search_search.h>
10 /*
11 /*	typedef struct {
12 /* .in +4
13 /*	    char   *map_type_name;	/* type:name, owned */
14 /*	    char   *search_order;	/* null or owned */
15 /* .in -4
16 /*	} MAP_SEARCH;
17 /*
18 /*	void	map_search_init(
19 /*	const NAME_CODE *search_actions)
20 /*
21 /*	const MAP_SEARCH *map_search_create(
22 /*	const char *map_spec)
23 /*
24 /*	const MAP_SEARCH *map_search_lookup(
25 /*	const char *map_spec);
26 /* DESCRIPTION
27 /*	This module implements configurable search order support
28 /*	for Postfix lookup tables.
29 /*
30 /*	map_search_init() must be called once, before other functions
31 /*	in this module.
32 /*
33 /*	map_search_create() creates a MAP_SEARCH instance for
34 /*	map_spec, ignoring duplicate requests.
35 /*
36 /*	map_search_lookup() looks up the MAP_SEARCH instance that
37 /*	was created by map_search_create().
38 /*
39 /*	Arguments:
40 /* .IP search_actions
41 /*	The mapping from search action string form to numeric form.
42 /*	The numbers must be in the range [1..126] (inclusive). The
43 /*	value 0 is reserved for the MAP_SEARCH.search_order terminator,
44 /*	and the value MAP_SEARCH_CODE_UNKNOWN is reserved for the
45 /*	'not found' result. The argument is copied (the pointer
46 /*	value, not the table).
47 /* .IP map_spec
48 /*	lookup table and optional search order: either maptype:mapname,
49 /*	or { maptype:mapname, { search = name, name }}. The search
50 /*	attribute is optional. The comma is equivalent to whitespace.
51 /* DIAGNOSTICS
52 /*	map_search_create() returns a null pointer when a map_spec
53 /*	is a) malformed, b) specifies an unexpected attribute name,
54 /*	c) the search attribute contains an unknown name. Thus,
55 /*	map_search_create() will never return a search_order that
56 /*	contains the value MAP_SEARCH_CODE_UNKNOWN.
57 /*
58 /*	Panic: interface violations. Fatal errors: out of memory.
59 /* LICENSE
60 /* .ad
61 /* .fi
62 /*	The Secure Mailer license must be distributed with this software.
63 /* AUTHOR(S)
64 /*	Wietse Venema
65 /*	Google, Inc.
66 /*	111 8th Avenue
67 /*	New York, NY 10011, USA
68 /*--*/
69 
70  /*
71   * System library.
72   */
73 #include <sys_defs.h>
74 #include <string.h>
75 
76 #ifdef STRCASECMP_IN_STRINGS_H
77 #include <strings.h>
78 #endif
79 
80  /*
81   * Utility library.
82   */
83 #include <htable.h>
84 #include <msg.h>
85 #include <mymalloc.h>
86 #include <name_code.h>
87 #include <stringops.h>
88 #include <vstring.h>
89 
90  /*
91   * Global library.
92   */
93 #include <map_search.h>
94 
95  /*
96   * Application-specific.
97   */
98 static HTABLE *map_search_table;
99 static const NAME_CODE *map_search_actions;
100 
101 #define STR(x) vstring_str(x)
102 
103 /* map_search_init - one-time initialization */
104 
105 void    map_search_init(const NAME_CODE *search_actions)
106 {
107     if (map_search_table != 0 || map_search_actions != 0)
108 	msg_panic("map_search_init: multiple calls");
109     map_search_table = htable_create(100);
110     map_search_actions = search_actions;
111 }
112 
113 /* map_search_create - store MAP_SEARCH instance */
114 
115 const MAP_SEARCH *map_search_create(const char *map_spec)
116 {
117     char   *copy_of_map_spec = 0;
118     char   *bp = 0;
119     const char *const_err;
120     char   *heap_err = 0;
121     VSTRING *search_order = 0;
122     const char *map_type_name;
123     char   *attr_name_val = 0;
124     char   *attr_name = 0;
125     char   *attr_value = 0;
126     MAP_SEARCH *map_search;
127     char   *atom;
128     int     code;
129 
130     /*
131      * Sanity check.
132      */
133     if (map_search_table == 0 || map_search_actions == 0)
134 	msg_panic("map_search_create: missing initialization");
135 
136     /*
137      * Allow exact duplicates. This won't catch duplicates that differ only
138      * in their use of whitespace or comma.
139      */
140     if ((map_search =
141 	 (MAP_SEARCH *) htable_find(map_search_table, map_spec)) != 0)
142 	return (map_search);
143 
144     /*
145      * Macro for readability and safety. Let the compiler worry about code
146      * duplication and redundant conditions.
147      */
148 #define MAP_SEARCH_CREATE_RETURN(x) do { \
149 	if (copy_of_map_spec) myfree(copy_of_map_spec); \
150 	if (heap_err) myfree(heap_err); \
151 	if (search_order) vstring_free(search_order); \
152 	return (x); \
153     } while (0)
154 
155     /*
156      * Long form specifies maptype_mapname and optional search attribute.
157      */
158     if (*map_spec == CHARS_BRACE[0]) {
159 	bp = copy_of_map_spec = mystrdup(map_spec);
160 	if ((heap_err = extpar(&bp, CHARS_BRACE, EXTPAR_FLAG_STRIP)) != 0) {
161 	    msg_warn("malformed map specification: '%s'", heap_err);
162 	    MAP_SEARCH_CREATE_RETURN(0);
163 	} else if ((map_type_name = mystrtok(&bp, CHARS_COMMA_SP)) == 0) {
164 	    msg_warn("empty map specification: '%s'", map_spec);
165 	    MAP_SEARCH_CREATE_RETURN(0);
166 	}
167     } else {
168 	map_type_name = map_spec;
169     }
170 
171     /*
172      * Sanity check the map spec before parsing attributes.
173      */
174     if (strchr(map_type_name, ':') == 0) {
175 	msg_warn("malformed map specification: '%s'", map_spec);
176 	msg_warn("expected maptype:mapname instead of '%s'", map_type_name);
177 	MAP_SEARCH_CREATE_RETURN(0);
178     }
179 
180     /*
181      * Parse the attribute list. XXX This does not detect multiple attributes
182      * with the same attribute name.
183      */
184     if (bp != 0) {
185 	while ((attr_name_val = mystrtokq(&bp, CHARS_COMMA_SP, CHARS_BRACE)) != 0) {
186 	    if (*attr_name_val == CHARS_BRACE[0]) {
187 		if ((heap_err = extpar(&attr_name_val, CHARS_BRACE,
188 				       EXTPAR_FLAG_STRIP)) != 0) {
189 		    msg_warn("malformed map attribute: %s", heap_err);
190 		    MAP_SEARCH_CREATE_RETURN(0);
191 		}
192 	    }
193 	    if ((const_err = split_nameval(attr_name_val, &attr_name,
194 					   &attr_value)) != 0) {
195 		msg_warn("malformed map attribute in '%s': '%s'",
196 			 map_spec, const_err);
197 		MAP_SEARCH_CREATE_RETURN(0);
198 	    }
199 	    if (strcasecmp(attr_name, MAP_SEARCH_ATTR_NAME_SEARCH) != 0) {
200 		msg_warn("unknown map attribute in '%s': '%s'",
201 			 map_spec, attr_name);
202 		MAP_SEARCH_CREATE_RETURN(0);
203 	    }
204 	}
205     }
206 
207     /*
208      * Parse the search list if any.
209      */
210     if (attr_name != 0) {
211 	search_order = vstring_alloc(10);
212 	while ((atom = mystrtok(&attr_value, CHARS_COMMA_SP)) != 0) {
213 	    if ((code = name_code(map_search_actions, NAME_CODE_FLAG_NONE,
214 				  atom)) == MAP_SEARCH_CODE_UNKNOWN) {
215 		msg_warn("unknown search type '%s' in '%s'", atom, map_spec);
216 		MAP_SEARCH_CREATE_RETURN(0);
217 	    }
218 	    VSTRING_ADDCH(search_order, code);
219 	}
220 	VSTRING_TERMINATE(search_order);
221     }
222 
223     /*
224      * Bundle up the result.
225      */
226     map_search = (MAP_SEARCH *) mymalloc(sizeof(*map_search));
227     map_search->map_type_name = mystrdup(map_type_name);
228     if (search_order) {
229 	map_search->search_order = vstring_export(search_order);
230 	search_order = 0;
231     } else {
232 	map_search->search_order = 0;
233     }
234 
235     /*
236      * Save the ACL to cache.
237      */
238     (void) htable_enter(map_search_table, map_spec, map_search);
239 
240     MAP_SEARCH_CREATE_RETURN(map_search);
241 }
242 
243 /* map_search_lookup - lookup MAP_SEARCH instance */
244 
245 const MAP_SEARCH *map_search_lookup(const char *map_spec)
246 {
247 
248     /*
249      * Sanity check.
250      */
251     if (map_search_table == 0 || map_search_actions == 0)
252 	msg_panic("map_search_lookup: missing initialization");
253 
254     return ((MAP_SEARCH *) htable_find(map_search_table, map_spec));
255 }
256 
257  /*
258   * Test driver.
259   */
260 #ifdef TEST
261 #include <stdlib.h>
262 
263  /*
264   * Test search actions.
265   */
266 #define TEST_NAME_1	"one"
267 #define TEST_NAME_2	"two"
268 #define TEST_CODE_1	1
269 #define TEST_CODE_2	2
270 
271 #define BAD_NAME	"bad"
272 
273 static const NAME_CODE search_actions[] = {
274     TEST_NAME_1, TEST_CODE_1,
275     TEST_NAME_2, TEST_CODE_2,
276     0, MAP_SEARCH_CODE_UNKNOWN,
277 };
278 
279 /* Helpers to simplify tests. */
280 
281 static const char *string_or_null(const char *s)
282 {
283     return (s ? s : "(null)");
284 }
285 
286 static char *escape_order(VSTRING *buf, const char *search_order)
287 {
288     return (STR(escape(buf, search_order, strlen(search_order))));
289 }
290 
291 int     main(int argc, char **argv)
292 {
293     /* Test cases with inputs and expected outputs. */
294     typedef struct TEST_CASE {
295 	const char *map_spec;
296 	int     exp_return;		/* 0=fail, 1=success */
297 	const char *exp_map_type_name;	/* 0 or match */
298 	const char *exp_search_order;	/* 0 or match */
299     } TEST_CASE;
300     static TEST_CASE test_cases[] = {
301 	{"type", 0, 0, 0},
302 	{"type:name", 1, "type:name", 0},
303 	{"{type:name}", 1, "type:name", 0},
304 	{"{type:name", 0, 0, 0},	/* } */
305 	{"{type}", 0, 0, 0},
306 	{"{type:name foo}", 0, 0, 0},
307 	{"{type:name foo=bar}", 0, 0, 0},
308 	{"{type:name search_order=}", 1, "type:name", ""},
309 	{"{type:name search_order=one, two}", 0, 0, 0},
310 	{"{type:name {search_order=one, two}}", 1, "type:name", "\01\02"},
311 	{"{type:name {search_order=one, two, bad}}", 0, 0, 0},
312 	{"{inline:{a=b} {search_order=one, two}}", 1, "inline:{a=b}", "\01\02"},
313 	{0},
314     };
315     TEST_CASE *test_case;
316 
317     /* Actual results. */
318     const MAP_SEARCH *map_search_from_create;
319     const MAP_SEARCH *map_search_from_create_2nd;
320     const MAP_SEARCH *map_search_from_lookup;
321 
322     /* Findings. */
323     int     tests_failed = 0;
324     int     test_failed;
325 
326     /* Scratch */
327     VSTRING *expect_escaped = vstring_alloc(100);
328     VSTRING *actual_escaped = vstring_alloc(100);
329 
330     map_search_init(search_actions);
331 
332     for (tests_failed = 0, test_case = test_cases; test_case->map_spec;
333 	 tests_failed += test_failed, test_case++) {
334 	test_failed = 0;
335 	msg_info("test case %d: '%s'",
336 		 (int) (test_case - test_cases), test_case->map_spec);
337 	map_search_from_create = map_search_create(test_case->map_spec);
338 	if (!test_case->exp_return != !map_search_from_create) {
339 	    if (map_search_from_create)
340 		msg_warn("test case %d return expected %s actual {%s, %s}",
341 			 (int) (test_case - test_cases),
342 			 test_case->exp_return ? "success" : "fail",
343 			 map_search_from_create->map_type_name,
344 			 escape_order(actual_escaped,
345 				      map_search_from_create->search_order));
346 	    else
347 		msg_warn("test case %d return expected %s actual %s",
348 			 (int) (test_case - test_cases), "success",
349 			 map_search_from_create ? "success" : "fail");
350 	    test_failed = 1;
351 	    continue;
352 	}
353 	if (test_case->exp_return == 0)
354 	    continue;
355 	map_search_from_lookup = map_search_lookup(test_case->map_spec);
356 	if (map_search_from_create != map_search_from_lookup) {
357 	    msg_warn("test case %d map_search_lookup expected=%p actual=%p",
358 		     (int) (test_case - test_cases),
359 		     map_search_from_create, map_search_from_lookup);
360 	    test_failed = 1;
361 	}
362 	map_search_from_create_2nd = map_search_create(test_case->map_spec);
363 	if (map_search_from_create != map_search_from_create_2nd) {
364 	    msg_warn("test case %d repeated map_search_create "
365 		     "expected=%p actual=%p",
366 		     (int) (test_case - test_cases),
367 		     map_search_from_create, map_search_from_create_2nd);
368 	    test_failed = 1;
369 	}
370 	if (strcmp(string_or_null(test_case->exp_map_type_name),
371 		   string_or_null(map_search_from_create->map_type_name))) {
372 	    msg_warn("test case %d map_type_name expected=%s actual=%s",
373 		     (int) (test_case - test_cases),
374 		     string_or_null(test_case->exp_map_type_name),
375 		     string_or_null(map_search_from_create->map_type_name));
376 	    test_failed = 1;
377 	}
378 	if (strcmp(string_or_null(test_case->exp_search_order),
379 		   string_or_null(map_search_from_create->search_order))) {
380 	    msg_warn("test case %d search_order expected=%s actual=%s",
381 		     (int) (test_case - test_cases),
382 		     escape_order(expect_escaped,
383 			       string_or_null(test_case->exp_search_order)),
384 		     escape_order(actual_escaped,
385 		     string_or_null(map_search_from_create->search_order)));
386 	    test_failed = 1;
387 	}
388     }
389     vstring_free(expect_escaped);
390     vstring_free(actual_escaped);
391 
392     if (tests_failed)
393 	msg_info("tests failed: %d", tests_failed);
394     exit(tests_failed != 0);
395 }
396 
397 #endif
398