1 /*	$NetBSD: postmap.c,v 1.1.1.1 2009/06/23 10:08:51 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	postmap 1
6 /* SUMMARY
7 /*	Postfix lookup table management
8 /* SYNOPSIS
9 /* .fi
10 /*	\fBpostmap\fR [\fB-Nbfhimnoprsvw\fR] [\fB-c \fIconfig_dir\fR]
11 /*	[\fB-d \fIkey\fR] [\fB-q \fIkey\fR]
12 /*		[\fIfile_type\fR:]\fIfile_name\fR ...
13 /* DESCRIPTION
14 /*	The \fBpostmap\fR(1) command creates or queries one or more Postfix
15 /*	lookup tables, or updates an existing one. The input and output
16 /*	file formats are expected to be compatible with:
17 /*
18 /* .nf
19 /*	    \fBmakemap \fIfile_type\fR \fIfile_name\fR < \fIfile_name\fR
20 /* .fi
21 /*
22 /*	If the result files do not exist they will be created with the
23 /*	same group and other read permissions as their source file.
24 /*
25 /*	While the table update is in progress, signal delivery is
26 /*	postponed, and an exclusive, advisory, lock is placed on the
27 /*	entire table, in order to avoid surprises in spectator
28 /*	processes.
29 /* INPUT FILE FORMAT
30 /* .ad
31 /* .fi
32 /*	The format of a lookup table input file is as follows:
33 /* .IP \(bu
34 /*	A table entry has the form
35 /* .sp
36 /* .nf
37 /*	     \fIkey\fR whitespace \fIvalue\fR
38 /* .fi
39 /* .IP \(bu
40 /*	Empty lines and whitespace-only lines are ignored, as
41 /*	are lines whose first non-whitespace character is a `#'.
42 /* .IP \(bu
43 /*	A logical line starts with non-whitespace text. A line that
44 /*	starts with whitespace continues a logical line.
45 /* .PP
46 /*	The \fIkey\fR and \fIvalue\fR are processed as is, except that
47 /*	surrounding white space is stripped off. Unlike with Postfix alias
48 /*	databases, quotes cannot be used to protect lookup keys that contain
49 /*	special characters such as `#' or whitespace.
50 /*
51 /*	By default the lookup key is mapped to lowercase to make
52 /*	the lookups case insensitive; as of Postfix 2.3 this case
53 /*	folding happens only with tables whose lookup keys are
54 /*	fixed-case strings such as btree:, dbm: or hash:. With
55 /*	earlier versions, the lookup key is folded even with tables
56 /*	where a lookup field can match both upper and lower case
57 /*	text, such as regexp: and pcre:. This resulted in loss of
58 /*	information with $\fInumber\fR substitutions.
59 /* COMMAND-LINE ARGUMENTS
60 /* .ad
61 /* .fi
62 /* .IP \fB-b\fR
63 /*	Enable message body query mode. When reading lookup keys
64 /*	from standard input with "\fB-q -\fR", process the input
65 /*	as if it is an email message in RFC 2822 format.  Each line
66 /*	of body content becomes one lookup key.
67 /* .sp
68 /*	By default, the \fB-b\fR option starts generating lookup
69 /*	keys at the first non-header line, and stops when the end
70 /*	of the message is reached.
71 /*	To simulate \fBbody_checks\fR(5) processing, enable MIME
72 /*	parsing with \fB-m\fR. With this, the \fB-b\fR option
73 /*	generates no body-style lookup keys for attachment MIME
74 /*	headers and for attached message/* headers.
75 /* .sp
76 /*	This feature is available in Postfix version 2.6 and later.
77 /* .IP "\fB-c \fIconfig_dir\fR"
78 /*	Read the \fBmain.cf\fR configuration file in the named directory
79 /*	instead of the default configuration directory.
80 /* .IP "\fB-d \fIkey\fR"
81 /*	Search the specified maps for \fIkey\fR and remove one entry per map.
82 /*	The exit status is zero when the requested information was found.
83 /*
84 /*	If a key value of \fB-\fR is specified, the program reads key
85 /*	values from the standard input stream. The exit status is zero
86 /*	when at least one of the requested keys was found.
87 /* .IP \fB-f\fR
88 /*	Do not fold the lookup key to lower case while creating or querying
89 /*	a table.
90 /*
91 /*	With Postfix version 2.3 and later, this option has no
92 /*	effect for regular expression tables. There, case folding
93 /*	is controlled by appending a flag to a pattern.
94 /* .IP \fB-h\fR
95 /*	Enable message header query mode. When reading lookup keys
96 /*	from standard input with "\fB-q -\fR", process the input
97 /*	as if it is an email message in RFC 2822 format.  Each
98 /*	logical header line becomes one lookup key. A multi-line
99 /*	header becomes one lookup key with one or more embedded
100 /*	newline characters.
101 /* .sp
102 /*	By default, the \fB-h\fR option generates lookup keys until
103 /*	the first non-header line is reached.
104 /*	To simulate \fBheader_checks\fR(5) processing, enable MIME
105 /*	parsing with \fB-m\fR. With this, the \fB-h\fR option also
106 /*	generates header-style lookup keys for attachment MIME
107 /*	headers and for attached message/* headers.
108 /* .sp
109 /*	This feature is available in Postfix version 2.6 and later.
110 /* .IP \fB-i\fR
111 /*	Incremental mode. Read entries from standard input and do not
112 /*	truncate an existing database. By default, \fBpostmap\fR(1) creates
113 /*	a new database from the entries in \fBfile_name\fR.
114 /* .IP \fB-m\fR
115 /*	Enable MIME parsing with "\fB-b\fR" and "\fB-h\fR".
116 /* .sp
117 /*	This feature is available in Postfix version 2.6 and later.
118 /* .IP \fB-N\fR
119 /*	Include the terminating null character that terminates lookup keys
120 /*	and values. By default, \fBpostmap\fR(1) does whatever is
121 /*	the default for
122 /*	the host operating system.
123 /* .IP \fB-n\fR
124 /*	Don't include the terminating null character that terminates lookup
125 /*	keys and values. By default, \fBpostmap\fR(1) does whatever
126 /*	is the default for
127 /*	the host operating system.
128 /* .IP \fB-o\fR
129 /*	Do not release root privileges when processing a non-root
130 /*	input file. By default, \fBpostmap\fR(1) drops root privileges
131 /*	and runs as the source file owner instead.
132 /* .IP \fB-p\fR
133 /*	Do not inherit the file access permissions from the input file
134 /*	when creating a new file.  Instead, create a new file with default
135 /*	access permissions (mode 0644).
136 /* .IP "\fB-q \fIkey\fR"
137 /*	Search the specified maps for \fIkey\fR and write the first value
138 /*	found to the standard output stream. The exit status is zero
139 /*	when the requested information was found.
140 /*
141 /*	If a key value of \fB-\fR is specified, the program reads key
142 /*	values from the standard input stream and writes one line of
143 /*	\fIkey value\fR output for each key that was found. The exit
144 /*	status is zero when at least one of the requested keys was found.
145 /* .IP \fB-r\fR
146 /*	When updating a table, do not complain about attempts to update
147 /*	existing entries, and make those updates anyway.
148 /* .IP \fB-s\fR
149 /*	Retrieve all database elements, and write one line of
150 /*	\fIkey value\fR output for each element. The elements are
151 /*	printed in database order, which is not necessarily the same
152 /*	as the original input order.
153 /* .sp
154 /*	This feature is available in Postfix version 2.2 and later,
155 /*	and is not available for all database types.
156 /* .IP \fB-v\fR
157 /*	Enable verbose logging for debugging purposes. Multiple \fB-v\fR
158 /*	options make the software increasingly verbose.
159 /* .IP \fB-w\fR
160 /*	When updating a table, do not complain about attempts to update
161 /*	existing entries, and ignore those attempts.
162 /* .PP
163 /*	Arguments:
164 /* .IP \fIfile_type\fR
165 /*	The database type. To find out what types are supported, use
166 /*	the "\fBpostconf -m\fR" command.
167 /*
168 /*	The \fBpostmap\fR(1) command can query any supported file type,
169 /*	but it can create only the following file types:
170 /* .RS
171 /* .IP \fBbtree\fR
172 /*	The output file is a btree file, named \fIfile_name\fB.db\fR.
173 /*	This is available on systems with support for \fBdb\fR databases.
174 /* .IP \fBcdb\fR
175 /*	The output consists of one file, named \fIfile_name\fB.cdb\fR.
176 /*	This is available on systems with support for \fBcdb\fR databases.
177 /* .IP \fBdbm\fR
178 /*	The output consists of two files, named \fIfile_name\fB.pag\fR and
179 /*	\fIfile_name\fB.dir\fR.
180 /*	This is available on systems with support for \fBdbm\fR databases.
181 /* .IP \fBhash\fR
182 /*	The output file is a hashed file, named \fIfile_name\fB.db\fR.
183 /*	This is available on systems with support for \fBdb\fR databases.
184 /* .IP \fBsdbm\fR
185 /*	The output consists of two files, named \fIfile_name\fB.pag\fR and
186 /*	\fIfile_name\fB.dir\fR.
187 /*	This is available on systems with support for \fBsdbm\fR databases.
188 /* .PP
189 /*	When no \fIfile_type\fR is specified, the software uses the database
190 /*	type specified via the \fBdefault_database_type\fR configuration
191 /*	parameter.
192 /* .RE
193 /* .IP \fIfile_name\fR
194 /*	The name of the lookup table source file when rebuilding a database.
195 /* DIAGNOSTICS
196 /*	Problems are logged to the standard error stream and to
197 /*	\fBsyslogd\fR(8).
198 /*	No output means that no problems were detected. Duplicate entries are
199 /*	skipped and are flagged with a warning.
200 /*
201 /*	\fBpostmap\fR(1) terminates with zero exit status in case of success
202 /*	(including successful "\fBpostmap -q\fR" lookup) and terminates
203 /*	with non-zero exit status in case of failure.
204 /* ENVIRONMENT
205 /* .ad
206 /* .fi
207 /* .IP \fBMAIL_CONFIG\fR
208 /*	Directory with Postfix configuration files.
209 /* .IP \fBMAIL_VERBOSE\fR
210 /*	Enable verbose logging for debugging purposes.
211 /* CONFIGURATION PARAMETERS
212 /* .ad
213 /* .fi
214 /*	The following \fBmain.cf\fR parameters are especially relevant to
215 /*	this program.
216 /*	The text below provides only a parameter summary. See
217 /*	\fBpostconf\fR(5) for more details including examples.
218 /* .IP "\fBberkeley_db_create_buffer_size (16777216)\fR"
219 /*	The per-table I/O buffer size for programs that create Berkeley DB
220 /*	hash or btree tables.
221 /* .IP "\fBberkeley_db_read_buffer_size (131072)\fR"
222 /*	The per-table I/O buffer size for programs that read Berkeley DB
223 /*	hash or btree tables.
224 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
225 /*	The default location of the Postfix main.cf and master.cf
226 /*	configuration files.
227 /* .IP "\fBdefault_database_type (see 'postconf -d' output)\fR"
228 /*	The default database type for use in \fBnewaliases\fR(1), \fBpostalias\fR(1)
229 /*	and \fBpostmap\fR(1) commands.
230 /* .IP "\fBsyslog_facility (mail)\fR"
231 /*	The syslog facility of Postfix logging.
232 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
233 /*	The mail system name that is prepended to the process name in syslog
234 /*	records, so that "smtpd" becomes, for example, "postfix/smtpd".
235 /* SEE ALSO
236 /*	postalias(1), create/update/query alias database
237 /*	postconf(1), supported database types
238 /*	postconf(5), configuration parameters
239 /*	syslogd(8), system logging
240 /* README FILES
241 /* .ad
242 /* .fi
243 /*	Use "\fBpostconf readme_directory\fR" or
244 /*	"\fBpostconf html_directory\fR" to locate this information.
245 /* .na
246 /* .nf
247 /*	DATABASE_README, Postfix lookup table overview
248 /* LICENSE
249 /* .ad
250 /* .fi
251 /*	The Secure Mailer license must be distributed with this software.
252 /* AUTHOR(S)
253 /*	Wietse Venema
254 /*	IBM T.J. Watson Research
255 /*	P.O. Box 704
256 /*	Yorktown Heights, NY 10598, USA
257 /*--*/
258 
259 /* System library. */
260 
261 #include <sys_defs.h>
262 #include <sys/stat.h>
263 #include <stdlib.h>
264 #include <unistd.h>
265 #include <fcntl.h>
266 #include <ctype.h>
267 #include <string.h>
268 
269 /* Utility library. */
270 
271 #include <msg.h>
272 #include <mymalloc.h>
273 #include <vstring.h>
274 #include <vstream.h>
275 #include <msg_vstream.h>
276 #include <msg_syslog.h>
277 #include <readlline.h>
278 #include <stringops.h>
279 #include <split_at.h>
280 #include <vstring_vstream.h>
281 #include <set_eugid.h>
282 
283 /* Global library. */
284 
285 #include <mail_conf.h>
286 #include <mail_dict.h>
287 #include <mail_params.h>
288 #include <mail_version.h>
289 #include <mkmap.h>
290 #include <mail_task.h>
291 #include <dict_proxy.h>
292 #include <mime_state.h>
293 #include <rec_type.h>
294 
295 /* Application-specific. */
296 
297 #define STR	vstring_str
298 #define LEN	VSTRING_LEN
299 
300 #define POSTMAP_FLAG_AS_OWNER	(1<<0)	/* open dest as owner of source */
301 #define POSTMAP_FLAG_SAVE_PERM	(1<<1)	/* copy access permission from source */
302 #define POSTMAP_FLAG_HEADER_KEY	(1<<2)	/* apply to header text */
303 #define POSTMAP_FLAG_BODY_KEY	(1<<3)	/* apply to body text */
304 #define POSTMAP_FLAG_MIME_KEY	(1<<4)	/* enable MIME parsing */
305 
306 #define POSTMAP_FLAG_HB_KEY (POSTMAP_FLAG_HEADER_KEY | POSTMAP_FLAG_BODY_KEY)
307 #define POSTMAP_FLAG_FULL_KEY (POSTMAP_FLAG_BODY_KEY | POSTMAP_FLAG_MIME_KEY)
308 #define POSTMAP_FLAG_ANY_KEY (POSTMAP_FLAG_HB_KEY | POSTMAP_FLAG_MIME_KEY)
309 
310  /*
311   * MIME Engine call-back state for generating lookup keys from an email
312   * message read from standard input.
313   */
314 typedef struct {
315     DICT  **dicts;			/* map handles */
316     char  **maps;			/* map names */
317     int     map_count;			/* yes, indeed */
318     int     dict_flags;			/* query flags */
319     int     header_done;		/* past primary header */
320     int     found;			/* result */
321 } POSTMAP_KEY_STATE;
322 
323 /* postmap - create or update mapping database */
324 
325 static void postmap(char *map_type, char *path_name, int postmap_flags,
326 		            int open_flags, int dict_flags)
327 {
328     VSTREAM *source_fp;
329     VSTRING *line_buffer;
330     MKMAP  *mkmap;
331     int     lineno;
332     char   *key;
333     char   *value;
334     struct stat st;
335     mode_t  saved_mask;
336 
337     /*
338      * Initialize.
339      */
340     line_buffer = vstring_alloc(100);
341     if ((open_flags & O_TRUNC) == 0) {
342 	source_fp = VSTREAM_IN;
343 	vstream_control(source_fp, VSTREAM_CTL_PATH, "stdin", VSTREAM_CTL_END);
344     } else if (strcmp(map_type, DICT_TYPE_PROXY) == 0) {
345 	msg_fatal("can't create maps via the proxy service");
346     } else if ((source_fp = vstream_fopen(path_name, O_RDONLY, 0)) == 0) {
347 	msg_fatal("open %s: %m", path_name);
348     }
349     if (fstat(vstream_fileno(source_fp), &st) < 0)
350 	msg_fatal("fstat %s: %m", path_name);
351 
352     /*
353      * Turn off group/other read permissions as indicated in the source file.
354      */
355     if ((postmap_flags & POSTMAP_FLAG_SAVE_PERM) && S_ISREG(st.st_mode))
356 	saved_mask = umask(022 | (~st.st_mode & 077));
357 
358     /*
359      * If running as root, run as the owner of the source file, so that the
360      * result shows proper ownership, and so that a bug in postmap does not
361      * allow privilege escalation.
362      */
363     if ((postmap_flags & POSTMAP_FLAG_AS_OWNER) && getuid() == 0
364 	&& (st.st_uid != geteuid() || st.st_gid != getegid()))
365 	set_eugid(st.st_uid, st.st_gid);
366 
367     /*
368      * Open the database, optionally create it when it does not exist,
369      * optionally truncate it when it does exist, and lock out any
370      * spectators.
371      */
372     mkmap = mkmap_open(map_type, path_name, open_flags, dict_flags);
373 
374     /*
375      * And restore the umask, in case it matters.
376      */
377     if ((postmap_flags & POSTMAP_FLAG_SAVE_PERM) && S_ISREG(st.st_mode))
378 	umask(saved_mask);
379 
380     /*
381      * Add records to the database.
382      */
383     lineno = 0;
384     while (readlline(line_buffer, source_fp, &lineno)) {
385 
386 	/*
387 	 * Split on the first whitespace character, then trim leading and
388 	 * trailing whitespace from key and value.
389 	 */
390 	key = STR(line_buffer);
391 	value = key + strcspn(key, " \t\r\n");
392 	if (*value)
393 	    *value++ = 0;
394 	while (ISSPACE(*value))
395 	    value++;
396 	trimblanks(key, 0)[0] = 0;
397 	trimblanks(value, 0)[0] = 0;
398 
399 	/*
400 	 * Enforce the "key whitespace value" format. Disallow missing keys
401 	 * or missing values.
402 	 */
403 	if (*key == 0 || *value == 0) {
404 	    msg_warn("%s, line %d: expected format: key whitespace value",
405 		     VSTREAM_PATH(source_fp), lineno);
406 	    continue;
407 	}
408 	if (key[strlen(key) - 1] == ':')
409 	    msg_warn("%s, line %d: record is in \"key: value\" format; is this an alias file?",
410 		     VSTREAM_PATH(source_fp), lineno);
411 
412 	/*
413 	 * Store the value under a case-insensitive key.
414 	 */
415 	mkmap_append(mkmap, key, value);
416     }
417 
418     /*
419      * Close the mapping database, and release the lock.
420      */
421     mkmap_close(mkmap);
422 
423     /*
424      * Cleanup. We're about to terminate, but it is a good sanity check.
425      */
426     vstring_free(line_buffer);
427     if (source_fp != VSTREAM_IN)
428 	vstream_fclose(source_fp);
429 }
430 
431 /* postmap_body - MIME engine body call-back routine */
432 
433 static void postmap_body(void *ptr, int unused_rec_type,
434 			         const char *keybuf,
435 			         ssize_t unused_len,
436 			         off_t unused_offset)
437 {
438     POSTMAP_KEY_STATE *state = (POSTMAP_KEY_STATE *) ptr;
439     DICT  **dicts = state->dicts;
440     char  **maps = state->maps;
441     int     map_count = state->map_count;
442     int     dict_flags = state->dict_flags;
443     const char *map_name;
444     const char *value;
445     int     n;
446 
447     for (n = 0; n < map_count; n++) {
448 	if (dicts[n] == 0)
449 	    dicts[n] = ((map_name = split_at(maps[n], ':')) != 0 ?
450 			dict_open3(maps[n], map_name, O_RDONLY, dict_flags) :
451 		    dict_open3(var_db_type, maps[n], O_RDONLY, dict_flags));
452 	if ((value = dict_get(dicts[n], keybuf)) != 0) {
453 	    if (*value == 0) {
454 		msg_warn("table %s:%s: key %s: empty string result is not allowed",
455 			 dicts[n]->type, dicts[n]->name, keybuf);
456 		msg_warn("table %s:%s should return NO RESULT in case of NOT FOUND",
457 			 dicts[n]->type, dicts[n]->name);
458 	    }
459 	    vstream_printf("%s	%s\n", keybuf, value);
460 	    state->found = 1;
461 	    break;
462 	}
463     }
464 }
465 
466 /* postmap_header - MIME engine header call-back routine */
467 
468 static void postmap_header(void *ptr, int unused_header_class,
469 			           const HEADER_OPTS *unused_header_info,
470 			           VSTRING *header_buf,
471 			           off_t offset)
472 {
473 
474     /*
475      * Don't re-invent an already working wheel.
476      */
477     postmap_body(ptr, 0, STR(header_buf), LEN(header_buf), offset);
478 }
479 
480 /* postmap_head_end - MIME engine end-of-header call-back routine */
481 
482 static void postmap_head_end(void *ptr)
483 {
484     POSTMAP_KEY_STATE *state = (POSTMAP_KEY_STATE *) ptr;
485 
486     /*
487      * Don't process the message body when we only examine primary headers.
488      */
489     state->header_done = 1;
490 }
491 
492 /* postmap_queries - apply multiple requests from stdin */
493 
494 static int postmap_queries(VSTREAM *in, char **maps, const int map_count,
495 			           const int postmap_flags,
496 			           const int dict_flags)
497 {
498     int     found = 0;
499     VSTRING *keybuf = vstring_alloc(100);
500     DICT  **dicts;
501     const char *map_name;
502     const char *value;
503     int     n;
504 
505     /*
506      * Sanity check.
507      */
508     if (map_count <= 0)
509 	msg_panic("postmap_queries: bad map count");
510 
511     /*
512      * Prepare to open maps lazily.
513      */
514     dicts = (DICT **) mymalloc(sizeof(*dicts) * map_count);
515     for (n = 0; n < map_count; n++)
516 	dicts[n] = 0;
517 
518     /*
519      * Perform all queries. Open maps on the fly, to avoid opening unecessary
520      * maps.
521      */
522     if ((postmap_flags & POSTMAP_FLAG_HB_KEY) == 0) {
523 	while (vstring_get_nonl(keybuf, in) != VSTREAM_EOF) {
524 	    for (n = 0; n < map_count; n++) {
525 		if (dicts[n] == 0)
526 		    dicts[n] = ((map_name = split_at(maps[n], ':')) != 0 ?
527 		       dict_open3(maps[n], map_name, O_RDONLY, dict_flags) :
528 		    dict_open3(var_db_type, maps[n], O_RDONLY, dict_flags));
529 		if ((value = dict_get(dicts[n], STR(keybuf))) != 0) {
530 		    if (*value == 0) {
531 			msg_warn("table %s:%s: key %s: empty string result is not allowed",
532 			       dicts[n]->type, dicts[n]->name, STR(keybuf));
533 			msg_warn("table %s:%s should return NO RESULT in case of NOT FOUND",
534 				 dicts[n]->type, dicts[n]->name);
535 		    }
536 		    vstream_printf("%s	%s\n", STR(keybuf), value);
537 		    found = 1;
538 		    break;
539 		}
540 	    }
541 	}
542     } else {
543 	POSTMAP_KEY_STATE key_state;
544 	MIME_STATE *mime_state;
545 	int     mime_errs = 0;
546 
547 	/*
548 	 * Bundle up the request and instantiate a MIME parsing engine.
549 	 */
550 	key_state.dicts = dicts;
551 	key_state.maps = maps;
552 	key_state.map_count = map_count;
553 	key_state.dict_flags = dict_flags;
554 	key_state.header_done = 0;
555 	key_state.found = 0;
556 	mime_state =
557 	    mime_state_alloc((postmap_flags & POSTMAP_FLAG_MIME_KEY) ?
558 			     0 : MIME_OPT_DISABLE_MIME,
559 			     (postmap_flags & POSTMAP_FLAG_HEADER_KEY) ?
560 			     postmap_header : (MIME_STATE_HEAD_OUT) 0,
561 			     (postmap_flags & POSTMAP_FLAG_FULL_KEY) ?
562 			     (MIME_STATE_ANY_END) 0 : postmap_head_end,
563 			     (postmap_flags & POSTMAP_FLAG_BODY_KEY) ?
564 			     postmap_body : (MIME_STATE_BODY_OUT) 0,
565 			     (MIME_STATE_ANY_END) 0,
566 			     (MIME_STATE_ERR_PRINT) 0,
567 			     (void *) &key_state);
568 
569 	/*
570 	 * Process the input message.
571 	 */
572 	while (vstring_get_nonl(keybuf, in) != VSTREAM_EOF
573 	       && key_state.header_done == 0 && mime_errs == 0)
574 	    mime_errs = mime_state_update(mime_state, REC_TYPE_NORM,
575 					  STR(keybuf), LEN(keybuf));
576 
577 	/*
578 	 * Flush the MIME engine output buffer and tidy up loose ends.
579 	 */
580 	if (mime_errs == 0)
581 	    mime_errs = mime_state_update(mime_state, REC_TYPE_END, "", 0);
582 	if (mime_errs)
583 	    msg_fatal("message format error: %s",
584 		      mime_state_detail(mime_errs)->text);
585 	mime_state_free(mime_state);
586 	found = key_state.found;
587     }
588     if (found)
589 	vstream_fflush(VSTREAM_OUT);
590 
591     /*
592      * Cleanup.
593      */
594     for (n = 0; n < map_count; n++)
595 	if (dicts[n])
596 	    dict_close(dicts[n]);
597     myfree((char *) dicts);
598     vstring_free(keybuf);
599 
600     return (found);
601 }
602 
603 /* postmap_query - query a map and print the result to stdout */
604 
605 static int postmap_query(const char *map_type, const char *map_name,
606 			         const char *key, int dict_flags)
607 {
608     DICT   *dict;
609     const char *value;
610 
611     dict = dict_open3(map_type, map_name, O_RDONLY, dict_flags);
612     if ((value = dict_get(dict, key)) != 0) {
613 	if (*value == 0) {
614 	    msg_warn("table %s:%s: key %s: empty string result is not allowed",
615 		     map_type, map_name, key);
616 	    msg_warn("table %s:%s should return NO RESULT in case of NOT FOUND",
617 		     map_type, map_name);
618 	}
619 	vstream_printf("%s\n", value);
620     }
621     vstream_fflush(VSTREAM_OUT);
622     dict_close(dict);
623     return (value != 0);
624 }
625 
626 /* postmap_deletes - apply multiple requests from stdin */
627 
628 static int postmap_deletes(VSTREAM *in, char **maps, const int map_count,
629 			           int dict_flags)
630 {
631     int     found = 0;
632     VSTRING *keybuf = vstring_alloc(100);
633     DICT  **dicts;
634     const char *map_name;
635     int     n;
636     int     open_flags;
637 
638     /*
639      * Sanity check.
640      */
641     if (map_count <= 0)
642 	msg_panic("postmap_deletes: bad map count");
643 
644     /*
645      * Open maps ahead of time.
646      */
647     dicts = (DICT **) mymalloc(sizeof(*dicts) * map_count);
648     for (n = 0; n < map_count; n++) {
649 	map_name = split_at(maps[n], ':');
650 	if (map_name && strcmp(maps[n], DICT_TYPE_PROXY) == 0)
651 	    open_flags = O_RDWR | O_CREAT;	/* XXX */
652 	else
653 	    open_flags = O_RDWR;
654 	dicts[n] = (map_name != 0 ?
655 		    dict_open3(maps[n], map_name, open_flags, dict_flags) :
656 		  dict_open3(var_db_type, maps[n], open_flags, dict_flags));
657     }
658 
659     /*
660      * Perform all requests.
661      */
662     while (vstring_get_nonl(keybuf, in) != VSTREAM_EOF)
663 	for (n = 0; n < map_count; n++)
664 	    found |= (dict_del(dicts[n], STR(keybuf)) == 0);
665 
666     /*
667      * Cleanup.
668      */
669     for (n = 0; n < map_count; n++)
670 	if (dicts[n])
671 	    dict_close(dicts[n]);
672     myfree((char *) dicts);
673     vstring_free(keybuf);
674 
675     return (found);
676 }
677 
678 /* postmap_delete - delete a (key, value) pair from a map */
679 
680 static int postmap_delete(const char *map_type, const char *map_name,
681 			          const char *key, int dict_flags)
682 {
683     DICT   *dict;
684     int     status;
685     int     open_flags;
686 
687     if (strcmp(map_type, DICT_TYPE_PROXY) == 0)
688 	open_flags = O_RDWR | O_CREAT;		/* XXX */
689     else
690 	open_flags = O_RDWR;
691     dict = dict_open3(map_type, map_name, open_flags, dict_flags);
692     status = dict_del(dict, key);
693     dict_close(dict);
694     return (status == 0);
695 }
696 
697 /* postmap_seq - print all map entries to stdout */
698 
699 static void postmap_seq(const char *map_type, const char *map_name,
700 			        int dict_flags)
701 {
702     DICT   *dict;
703     const char *key;
704     const char *value;
705     int     func;
706 
707     if (strcmp(map_type, DICT_TYPE_PROXY) == 0)
708 	msg_fatal("can't sequence maps via the proxy service");
709     dict = dict_open3(map_type, map_name, O_RDONLY, dict_flags);
710     for (func = DICT_SEQ_FUN_FIRST; /* void */ ; func = DICT_SEQ_FUN_NEXT) {
711 	if (dict_seq(dict, func, &key, &value) != 0)
712 	    break;
713 	if (*key == 0) {
714 	    msg_warn("table %s:%s: empty lookup key value is not allowed",
715 		     map_type, map_name);
716 	} else if (*value == 0) {
717 	    msg_warn("table %s:%s: key %s: empty string result is not allowed",
718 		     map_type, map_name, key);
719 	    msg_warn("table %s:%s should return NO RESULT in case of NOT FOUND",
720 		     map_type, map_name);
721 	}
722 	vstream_printf("%s	%s\n", key, value);
723     }
724     vstream_fflush(VSTREAM_OUT);
725     dict_close(dict);
726 }
727 
728 /* usage - explain */
729 
730 static NORETURN usage(char *myname)
731 {
732     msg_fatal("usage: %s [-Nfinoprsvw] [-c config_dir] [-d key] [-q key] [map_type:]file...",
733 	      myname);
734 }
735 
736 MAIL_VERSION_STAMP_DECLARE;
737 
738 int     main(int argc, char **argv)
739 {
740     char   *path_name;
741     int     ch;
742     int     fd;
743     char   *slash;
744     struct stat st;
745     int     postmap_flags = POSTMAP_FLAG_AS_OWNER | POSTMAP_FLAG_SAVE_PERM;
746     int     open_flags = O_RDWR | O_CREAT | O_TRUNC;
747     int     dict_flags = DICT_FLAG_DUP_WARN | DICT_FLAG_FOLD_FIX;
748     char   *query = 0;
749     char   *delkey = 0;
750     int     sequence = 0;
751     int     found;
752 
753     /*
754      * Fingerprint executables and core dumps.
755      */
756     MAIL_VERSION_STAMP_ALLOCATE;
757 
758     /*
759      * Be consistent with file permissions.
760      */
761     umask(022);
762 
763     /*
764      * To minimize confusion, make sure that the standard file descriptors
765      * are open before opening anything else. XXX Work around for 44BSD where
766      * fstat can return EBADF on an open file descriptor.
767      */
768     for (fd = 0; fd < 3; fd++)
769 	if (fstat(fd, &st) == -1
770 	    && (close(fd), open("/dev/null", O_RDWR, 0)) != fd)
771 	    msg_fatal("open /dev/null: %m");
772 
773     /*
774      * Process environment options as early as we can. We are not set-uid,
775      * and we are supposed to be running in a controlled environment.
776      */
777     if (getenv(CONF_ENV_VERB))
778 	msg_verbose = 1;
779 
780     /*
781      * Initialize. Set up logging, read the global configuration file and
782      * extract configuration information.
783      */
784     if ((slash = strrchr(argv[0], '/')) != 0 && slash[1])
785 	argv[0] = slash + 1;
786     msg_vstream_init(argv[0], VSTREAM_ERR);
787     msg_syslog_init(mail_task(argv[0]), LOG_PID, LOG_FACILITY);
788 
789     /*
790      * Parse JCL.
791      */
792     while ((ch = GETOPT(argc, argv, "Nbc:d:fhimnopq:rsvw")) > 0) {
793 	switch (ch) {
794 	default:
795 	    usage(argv[0]);
796 	    break;
797 	case 'N':
798 	    dict_flags |= DICT_FLAG_TRY1NULL;
799 	    dict_flags &= ~DICT_FLAG_TRY0NULL;
800 	    break;
801 	case 'b':
802 	    postmap_flags |= POSTMAP_FLAG_BODY_KEY;
803 	    break;
804 	case 'c':
805 	    if (setenv(CONF_ENV_PATH, optarg, 1) < 0)
806 		msg_fatal("out of memory");
807 	    break;
808 	case 'd':
809 	    if (sequence || query || delkey)
810 		msg_fatal("specify only one of -s -q or -d");
811 	    delkey = optarg;
812 	    break;
813 	case 'f':
814 	    dict_flags &= ~DICT_FLAG_FOLD_FIX;
815 	    break;
816 	case 'h':
817 	    postmap_flags |= POSTMAP_FLAG_HEADER_KEY;
818 	    break;
819 	case 'i':
820 	    open_flags &= ~O_TRUNC;
821 	    break;
822 	case 'm':
823 	    postmap_flags |= POSTMAP_FLAG_MIME_KEY;
824 	    break;
825 	case 'n':
826 	    dict_flags |= DICT_FLAG_TRY0NULL;
827 	    dict_flags &= ~DICT_FLAG_TRY1NULL;
828 	    break;
829 	case 'o':
830 	    postmap_flags &= ~POSTMAP_FLAG_AS_OWNER;
831 	    break;
832 	case 'p':
833 	    postmap_flags &= ~POSTMAP_FLAG_SAVE_PERM;
834 	    break;
835 	case 'q':
836 	    if (sequence || query || delkey)
837 		msg_fatal("specify only one of -s -q or -d");
838 	    query = optarg;
839 	    break;
840 	case 'r':
841 	    dict_flags &= ~(DICT_FLAG_DUP_WARN | DICT_FLAG_DUP_IGNORE);
842 	    dict_flags |= DICT_FLAG_DUP_REPLACE;
843 	    break;
844 	case 's':
845 	    if (query || delkey)
846 		msg_fatal("specify only one of -s or -q or -d");
847 	    sequence = 1;
848 	    break;
849 	case 'v':
850 	    msg_verbose++;
851 	    break;
852 	case 'w':
853 	    dict_flags &= ~(DICT_FLAG_DUP_WARN | DICT_FLAG_DUP_REPLACE);
854 	    dict_flags |= DICT_FLAG_DUP_IGNORE;
855 	    break;
856 	}
857     }
858     mail_conf_read();
859     if (strcmp(var_syslog_name, DEF_SYSLOG_NAME) != 0)
860 	msg_syslog_init(mail_task(argv[0]), LOG_PID, LOG_FACILITY);
861     mail_dict_init();
862     if ((query == 0 || strcmp(query, "-") != 0)
863 	&& (postmap_flags & POSTMAP_FLAG_ANY_KEY))
864 	msg_fatal("specify -b -h or -m only with \"-q -\"");
865 
866     /*
867      * Use the map type specified by the user, or fall back to a default
868      * database type.
869      */
870     if (delkey) {				/* remove entry */
871 	if (optind + 1 > argc)
872 	    usage(argv[0]);
873 	if (strcmp(delkey, "-") == 0)
874 	    exit(postmap_deletes(VSTREAM_IN, argv + optind, argc - optind,
875 				 dict_flags | DICT_FLAG_LOCK) == 0);
876 	found = 0;
877 	while (optind < argc) {
878 	    if ((path_name = split_at(argv[optind], ':')) != 0) {
879 		found |= postmap_delete(argv[optind], path_name, delkey,
880 					dict_flags | DICT_FLAG_LOCK);
881 	    } else {
882 		found |= postmap_delete(var_db_type, argv[optind], delkey,
883 					dict_flags | DICT_FLAG_LOCK);
884 	    }
885 	    optind++;
886 	}
887 	exit(found ? 0 : 1);
888     } else if (query) {				/* query map(s) */
889 	if (optind + 1 > argc)
890 	    usage(argv[0]);
891 	if (strcmp(query, "-") == 0)
892 	    exit(postmap_queries(VSTREAM_IN, argv + optind, argc - optind,
893 			  postmap_flags, dict_flags | DICT_FLAG_LOCK) == 0);
894 	while (optind < argc) {
895 	    if ((path_name = split_at(argv[optind], ':')) != 0) {
896 		found = postmap_query(argv[optind], path_name, query,
897 				      dict_flags | DICT_FLAG_LOCK);
898 	    } else {
899 		found = postmap_query(var_db_type, argv[optind], query,
900 				      dict_flags | DICT_FLAG_LOCK);
901 	    }
902 	    if (found)
903 		exit(0);
904 	    optind++;
905 	}
906 	exit(1);
907     } else if (sequence) {
908 	while (optind < argc) {
909 	    if ((path_name = split_at(argv[optind], ':')) != 0) {
910 		postmap_seq(argv[optind], path_name,
911 			    dict_flags | DICT_FLAG_LOCK);
912 	    } else {
913 		postmap_seq(var_db_type, argv[optind],
914 			    dict_flags | DICT_FLAG_LOCK);
915 	    }
916 	    exit(0);
917 	}
918 	exit(1);
919     } else {					/* create/update map(s) */
920 	if (optind + 1 > argc)
921 	    usage(argv[0]);
922 	while (optind < argc) {
923 	    if ((path_name = split_at(argv[optind], ':')) != 0) {
924 		postmap(argv[optind], path_name, postmap_flags,
925 			open_flags, dict_flags);
926 	    } else {
927 		postmap(var_db_type, argv[optind], postmap_flags,
928 			open_flags, dict_flags);
929 	    }
930 	    optind++;
931 	}
932 	exit(0);
933     }
934 }
935