xref: /netbsd-src/external/ibm-public/postfix/dist/src/postalias/postalias.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /*	$NetBSD: postalias.c,v 1.4 2022/10/08 16:12:46 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	postalias 1
6 /* SUMMARY
7 /*	Postfix alias database maintenance
8 /* SYNOPSIS
9 /* .fi
10 /*	\fBpostalias\fR [\fB-Nfinoprsuvw\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 \fBpostalias\fR(1) command creates or queries one or more Postfix
15 /*	alias databases, or updates an existing one. The input and output
16 /*	file formats are expected to be compatible with Sendmail version 8,
17 /*	and are expected to be suitable for use as NIS alias maps.
18 /*
19 /*	If the result files do not exist they will be created with the
20 /*	same group and other read permissions as their source file.
21 /*
22 /*	While a database update is in progress, signal delivery is
23 /*	postponed, and an exclusive, advisory, lock is placed on the
24 /*	entire database, in order to avoid surprises in spectator
25 /*	processes.
26 /*
27 /*	The format of Postfix alias input files is described in
28 /*	\fBaliases\fR(5).
29 /*
30 /*	By default the lookup key is mapped to lowercase to make
31 /*	the lookups case insensitive; as of Postfix 2.3 this case
32 /*	folding happens only with tables whose lookup keys are
33 /*	fixed-case strings such as btree:, dbm: or hash:. With
34 /*	earlier versions, the lookup key is folded even with tables
35 /*	where a lookup field can match both upper and lower case
36 /*	text, such as regexp: and pcre:. This resulted in loss of
37 /*	information with $\fInumber\fR substitutions.
38 /*
39 /*	Options:
40 /* .IP "\fB-c \fIconfig_dir\fR"
41 /*	Read the \fBmain.cf\fR configuration file in the named directory
42 /*	instead of the default configuration directory.
43 /* .IP "\fB-d \fIkey\fR"
44 /*	Search the specified maps for \fIkey\fR and remove one entry per map.
45 /*	The exit status is zero when the requested information was found.
46 /*
47 /*	If a key value of \fB-\fR is specified, the program reads key
48 /*	values from the standard input stream. The exit status is zero
49 /*	when at least one of the requested keys was found.
50 /* .IP \fB-f\fR
51 /*	Do not fold the lookup key to lower case while creating or querying
52 /*	a table.
53 /*
54 /*	With Postfix version 2.3 and later, this option has no
55 /*	effect for regular expression tables. There, case folding
56 /*	is controlled by appending a flag to a pattern.
57 /* .IP \fB-i\fR
58 /*	Incremental mode. Read entries from standard input and do not
59 /*	truncate an existing database. By default, \fBpostalias\fR(1) creates
60 /*	a new database from the entries in \fIfile_name\fR.
61 /* .IP \fB-N\fR
62 /*	Include the terminating null character that terminates lookup keys
63 /*	and values. By default, \fBpostalias\fR(1) does whatever
64 /*	is the default for
65 /*	the host operating system.
66 /* .IP \fB-n\fR
67 /*	Don't include the terminating null character that terminates lookup
68 /*	keys and values. By default, \fBpostalias\fR(1) does whatever
69 /*	is the default for
70 /*	the host operating system.
71 /* .IP \fB-o\fR
72 /*	Do not release root privileges when processing a non-root
73 /*	input file. By default, \fBpostalias\fR(1) drops root privileges
74 /*	and runs as the source file owner instead.
75 /* .IP \fB-p\fR
76 /*	Do not inherit the file access permissions from the input file
77 /*	when creating a new file.  Instead, create a new file with default
78 /*	access permissions (mode 0644).
79 /* .IP "\fB-q \fIkey\fR"
80 /*	Search the specified maps for \fIkey\fR and write the first value
81 /*	found to the standard output stream. The exit status is zero
82 /*	when the requested information was found.
83 /*
84 /*	Note: this performs a single query with the key as specified,
85 /*	and does not make iterative queries with substrings of the
86 /*	key as described in the aliases(5) manual page.
87 /*
88 /*	If a key value of \fB-\fR is specified, the program reads key
89 /*	values from the standard input stream and writes one line of
90 /*	\fIkey: value\fR output for each key that was found. The exit
91 /*	status is zero when at least one of the requested keys was found.
92 /* .IP \fB-r\fR
93 /*	When updating a table, do not complain about attempts to update
94 /*	existing entries, and make those updates anyway.
95 /* .IP \fB-s\fR
96 /*	Retrieve all database elements, and write one line of
97 /*	\fIkey: value\fR output for each element. The elements are
98 /*	printed in database order, which is not necessarily the same
99 /*	as the original input order.
100 /*	This feature is available in Postfix version 2.2 and later,
101 /*	and is not available for all database types.
102 /* .IP \fB-u\fR
103 /*	Disable UTF-8 support. UTF-8 support is enabled by default
104 /*	when "smtputf8_enable = yes". It requires that keys and
105 /*	values are valid UTF-8 strings.
106 /* .IP \fB-v\fR
107 /*	Enable verbose logging for debugging purposes. Multiple \fB-v\fR
108 /*	options make the software increasingly verbose.
109 /* .IP \fB-w\fR
110 /*	When updating a table, do not complain about attempts to update
111 /*	existing entries, and ignore those attempts.
112 /* .PP
113 /*	Arguments:
114 /* .IP \fIfile_type\fR
115 /*	The database type. To find out what types are supported, use
116 /*	the "\fBpostconf -m\fR" command.
117 /*
118 /*	The \fBpostalias\fR(1) command can query any supported file type,
119 /*	but it can create only the following file types:
120 /* .RS
121 /* .IP \fBbtree\fR
122 /*	The output is a btree file, named \fIfile_name\fB.db\fR.
123 /*	This is available on systems with support for \fBdb\fR databases.
124 /* .IP \fBcdb\fR
125 /*	The output is one file named \fIfile_name\fB.cdb\fR.
126 /*	This is available on systems with support for \fBcdb\fR databases.
127 /* .IP \fBdbm\fR
128 /*	The output consists of two files, named \fIfile_name\fB.pag\fR and
129 /*	\fIfile_name\fB.dir\fR.
130 /*	This is available on systems with support for \fBdbm\fR databases.
131 /* .IP \fBfail\fR
132 /*	A table that reliably fails all requests. The lookup table
133 /*	name is used for logging only. This table exists to simplify
134 /*	Postfix error tests.
135 /* .IP \fBhash\fR
136 /*	The output is a hashed file, named \fIfile_name\fB.db\fR.
137 /*	This is available on systems with support for \fBdb\fR databases.
138 /* .IP \fBlmdb\fR
139 /*	The output is a btree-based file, named \fIfile_name\fB.lmdb\fR.
140 /*	\fBlmdb\fR supports concurrent writes and reads from different
141 /*	processes, unlike other supported file-based tables.
142 /*	This is available on systems with support for \fBlmdb\fR databases.
143 /* .IP \fBsdbm\fR
144 /*	The output consists of two files, named \fIfile_name\fB.pag\fR and
145 /*	\fIfile_name\fB.dir\fR.
146 /*	This is available on systems with support for \fBsdbm\fR databases.
147 /* .PP
148 /*	When no \fIfile_type\fR is specified, the software uses the database
149 /*	type specified via the \fBdefault_database_type\fR configuration
150 /*	parameter.
151 /*	The default value for this parameter depends on the host environment.
152 /* .RE
153 /* .IP \fIfile_name\fR
154 /*	The name of the alias database source file when creating a database.
155 /* DIAGNOSTICS
156 /*	Problems are logged to the standard error stream and to
157 /*	\fBsyslogd\fR(8) or \fBpostlogd\fR(8). No output means that
158 /*	no problems were detected. Duplicate entries are skipped and are
159 /*	flagged with a warning.
160 /*
161 /*	\fBpostalias\fR(1) terminates with zero exit status in case of success
162 /*	(including successful "\fBpostalias -q\fR" lookup) and terminates
163 /*	with non-zero exit status in case of failure.
164 /* ENVIRONMENT
165 /* .ad
166 /* .fi
167 /* .IP \fBMAIL_CONFIG\fR
168 /*	Directory with Postfix configuration files.
169 /* .IP \fBMAIL_VERBOSE\fR
170 /*	Enable verbose logging for debugging purposes.
171 /* CONFIGURATION PARAMETERS
172 /* .ad
173 /* .fi
174 /*	The following \fBmain.cf\fR parameters are especially relevant to
175 /*	this program.
176 /*
177 /*	The text below provides only a parameter summary. See
178 /*	\fBpostconf\fR(5) for more details including examples.
179 /* .IP "\fBalias_database (see 'postconf -d' output)\fR"
180 /*	The alias databases for \fBlocal\fR(8) delivery that are updated with
181 /*	"\fBnewaliases\fR" or with "\fBsendmail -bi\fR".
182 /* .IP "\fBconfig_directory (see 'postconf -d' output)\fR"
183 /*	The default location of the Postfix main.cf and master.cf
184 /*	configuration files.
185 /* .IP "\fBberkeley_db_create_buffer_size (16777216)\fR"
186 /*	The per-table I/O buffer size for programs that create Berkeley DB
187 /*	hash or btree tables.
188 /* .IP "\fBberkeley_db_read_buffer_size (131072)\fR"
189 /*	The per-table I/O buffer size for programs that read Berkeley DB
190 /*	hash or btree tables.
191 /* .IP "\fBdefault_database_type (see 'postconf -d' output)\fR"
192 /*	The default database type for use in \fBnewaliases\fR(1), \fBpostalias\fR(1)
193 /*	and \fBpostmap\fR(1) commands.
194 /* .IP "\fBimport_environment (see 'postconf -d' output)\fR"
195 /*	The list of environment variables that a privileged Postfix
196 /*	process will import from a non-Postfix parent process, or name=value
197 /*	environment overrides.
198 /* .IP "\fBsmtputf8_enable (yes)\fR"
199 /*	Enable preliminary SMTPUTF8 support for the protocols described
200 /*	in RFC 6531, RFC 6532, and RFC 6533.
201 /* .IP "\fBsyslog_facility (mail)\fR"
202 /*	The syslog facility of Postfix logging.
203 /* .IP "\fBsyslog_name (see 'postconf -d' output)\fR"
204 /*	A prefix that is prepended to the process name in syslog
205 /*	records, so that, for example, "smtpd" becomes "prefix/smtpd".
206 /* .PP
207 /*	Available in Postfix 2.11 and later:
208 /* .IP "\fBlmdb_map_size (16777216)\fR"
209 /*	The initial OpenLDAP LMDB database size limit in bytes.
210 /* STANDARDS
211 /*	RFC 822 (ARPA Internet Text Messages)
212 /* SEE ALSO
213 /*	aliases(5), format of alias database input file.
214 /*	local(8), Postfix local delivery agent.
215 /*	postconf(1), supported database types
216 /*	postconf(5), configuration parameters
217 /*	postmap(1), create/update/query lookup tables
218 /*	newaliases(1), Sendmail compatibility interface.
219 /*	postlogd(8), Postfix logging
220 /*	syslogd(8), system logging
221 /* README FILES
222 /* .ad
223 /* .fi
224 /*	Use "\fBpostconf readme_directory\fR" or
225 /*	"\fBpostconf html_directory\fR" to locate this information.
226 /* .na
227 /* .nf
228 /*	DATABASE_README, Postfix lookup table overview
229 /* LICENSE
230 /* .ad
231 /* .fi
232 /*	The Secure Mailer license must be distributed with this software.
233 /* AUTHOR(S)
234 /*	Wietse Venema
235 /*	IBM T.J. Watson Research
236 /*	P.O. Box 704
237 /*	Yorktown Heights, NY 10598, USA
238 /*
239 /*	Wietse Venema
240 /*	Google, Inc.
241 /*	111 8th Avenue
242 /*	New York, NY 10011, USA
243 /*--*/
244 
245 /* System library. */
246 
247 #include <sys_defs.h>
248 #include <sys/stat.h>
249 #include <stdlib.h>
250 #include <unistd.h>
251 #include <fcntl.h>
252 #include <ctype.h>
253 #include <string.h>
254 
255 /* Utility library. */
256 
257 #include <msg.h>
258 #include <mymalloc.h>
259 #include <vstring.h>
260 #include <vstream.h>
261 #include <msg_vstream.h>
262 #include <readlline.h>
263 #include <stringops.h>
264 #include <split_at.h>
265 #include <vstring_vstream.h>
266 #include <set_eugid.h>
267 #include <warn_stat.h>
268 #include <clean_env.h>
269 
270 /* Global library. */
271 
272 #include <tok822.h>
273 #include <mail_conf.h>
274 #include <mail_dict.h>
275 #include <mail_params.h>
276 #include <mail_version.h>
277 #include <mkmap.h>
278 #include <mail_task.h>
279 #include <dict_proxy.h>
280 #include <mail_parm_split.h>
281 #include <maillog_client.h>
282 
283 /* Application-specific. */
284 
285 #define STR	vstring_str
286 #define LEN	VSTRING_LEN
287 
288 #define POSTALIAS_FLAG_AS_OWNER	(1<<0)	/* open dest as owner of source */
289 #define POSTALIAS_FLAG_SAVE_PERM	(1<<1)	/* copy access permission
290 						 * from source */
291 
292 /* postalias - create or update alias database */
293 
294 static void postalias(char *map_type, char *path_name, int postalias_flags,
295 		              int open_flags, int dict_flags)
296 {
297     VSTREAM *NOCLOBBER source_fp;
298     VSTRING *line_buffer;
299     MKMAP  *mkmap;
300     int     lineno;
301     int     last_line;
302     VSTRING *key_buffer;
303     VSTRING *value_buffer;
304     TOK822 *tok_list;
305     TOK822 *key_list;
306     TOK822 *colon;
307     TOK822 *value_list;
308     struct stat st;
309     mode_t  saved_mask;
310 
311     /*
312      * Initialize.
313      */
314     line_buffer = vstring_alloc(100);
315     key_buffer = vstring_alloc(100);
316     value_buffer = vstring_alloc(100);
317     if ((open_flags & O_TRUNC) == 0) {
318 	/* Incremental mode. */
319 	source_fp = VSTREAM_IN;
320 	vstream_control(source_fp, CA_VSTREAM_CTL_PATH("stdin"), CA_VSTREAM_CTL_END);
321     } else {
322 	/* Create database. */
323 	if (strcmp(map_type, DICT_TYPE_PROXY) == 0)
324 	    msg_fatal("can't create maps via the proxy service");
325 	dict_flags |= DICT_FLAG_BULK_UPDATE;
326 	if ((source_fp = vstream_fopen(path_name, O_RDONLY, 0)) == 0)
327 	    msg_fatal("open %s: %m", path_name);
328     }
329     if (fstat(vstream_fileno(source_fp), &st) < 0)
330 	msg_fatal("fstat %s: %m", path_name);
331 
332     /*
333      * Turn off group/other read permissions as indicated in the source file.
334      */
335     if ((postalias_flags & POSTALIAS_FLAG_SAVE_PERM) && S_ISREG(st.st_mode))
336 	saved_mask = umask(022 | (~st.st_mode & 077));
337 
338     /*
339      * If running as root, run as the owner of the source file, so that the
340      * result shows proper ownership, and so that a bug in postalias does not
341      * allow privilege escalation.
342      */
343     if ((postalias_flags & POSTALIAS_FLAG_AS_OWNER) && getuid() == 0
344 	&& (st.st_uid != geteuid() || st.st_gid != getegid()))
345 	set_eugid(st.st_uid, st.st_gid);
346 
347     /*
348      * Open the database, create it when it does not exist, truncate it when
349      * it does exist, and lock out any spectators.
350      */
351     mkmap = mkmap_open(map_type, path_name, open_flags, dict_flags);
352 
353     /*
354      * And restore the umask, in case it matters.
355      */
356     if ((postalias_flags & POSTALIAS_FLAG_SAVE_PERM) && S_ISREG(st.st_mode))
357 	umask(saved_mask);
358 
359     /*
360      * Trap "exceptions" so that we can restart a bulk-mode update after a
361      * recoverable error.
362      */
363     for (;;) {
364 	if (dict_isjmp(mkmap->dict) != 0
365 	    && dict_setjmp(mkmap->dict) != 0
366 	    && vstream_fseek(source_fp, SEEK_SET, 0) < 0)
367 	    msg_fatal("seek %s: %m", VSTREAM_PATH(source_fp));
368 
369 	/*
370 	 * Add records to the database.
371 	 */
372 	last_line = 0;
373 	while (readllines(line_buffer, source_fp, &last_line, &lineno)) {
374 
375 	    /*
376 	     * First some UTF-8 checks sans casefolding.
377 	     */
378 	    if ((mkmap->dict->flags & DICT_FLAG_UTF8_ACTIVE)
379 		&& !allascii(STR(line_buffer))
380 		&& !valid_utf8_string(STR(line_buffer), LEN(line_buffer))) {
381 		msg_warn("%s, line %d: non-UTF-8 input \"%s\""
382 			 " -- ignoring this line",
383 			 VSTREAM_PATH(source_fp), lineno, STR(line_buffer));
384 		continue;
385 	    }
386 
387 	    /*
388 	     * Tokenize the input, so that we do the right thing when a
389 	     * quoted localpart contains special characters such as "@", ":"
390 	     * and so on.
391 	     */
392 	    if ((tok_list = tok822_scan(STR(line_buffer), (TOK822 **) 0)) == 0)
393 		continue;
394 
395 	    /*
396 	     * Enforce the key:value format. Disallow missing keys,
397 	     * multi-address keys, or missing values. In order to specify an
398 	     * empty string or value, enclose it in double quotes.
399 	     */
400 	    if ((colon = tok822_find_type(tok_list, ':')) == 0
401 		|| colon->prev == 0 || colon->next == 0
402 		|| tok822_rfind_type(colon, ',')) {
403 		msg_warn("%s, line %d: need name:value pair",
404 			 VSTREAM_PATH(source_fp), lineno);
405 		tok822_free_tree(tok_list);
406 		continue;
407 	    }
408 
409 	    /*
410 	     * Key must be local. XXX We should use the Postfix rewriting and
411 	     * resolving services to handle all address forms correctly.
412 	     * However, we can't count on the mail system being up when the
413 	     * alias database is being built, so we're guessing a bit.
414 	     */
415 	    if (tok822_rfind_type(colon, '@') || tok822_rfind_type(colon, '%')) {
416 		msg_warn("%s, line %d: name must be local",
417 			 VSTREAM_PATH(source_fp), lineno);
418 		tok822_free_tree(tok_list);
419 		continue;
420 	    }
421 
422 	    /*
423 	     * Split the input into key and value parts, and convert from
424 	     * token representation back to string representation. Convert
425 	     * the key to internal (unquoted) form, because the resolver
426 	     * produces addresses in internal form. Convert the value to
427 	     * external (quoted) form, because it will have to be re-parsed
428 	     * upon lookup. Discard the token representation when done.
429 	     */
430 	    key_list = tok_list;
431 	    tok_list = 0;
432 	    value_list = tok822_cut_after(colon);
433 	    tok822_unlink(colon);
434 	    tok822_free(colon);
435 
436 	    tok822_internalize(key_buffer, key_list, TOK822_STR_DEFL);
437 	    tok822_free_tree(key_list);
438 
439 	    tok822_externalize(value_buffer, value_list, TOK822_STR_DEFL);
440 	    tok822_free_tree(value_list);
441 
442 	    /*
443 	     * Store the value under a case-insensitive key.
444 	     */
445 	    mkmap_append(mkmap, STR(key_buffer), STR(value_buffer));
446 	    if (mkmap->dict->error)
447 		msg_fatal("table %s:%s: write error: %m",
448 			  mkmap->dict->type, mkmap->dict->name);
449 	}
450 	break;
451     }
452 
453     /*
454      * Update or append sendmail and NIS signatures.
455      */
456     if ((open_flags & O_TRUNC) == 0)
457 	mkmap->dict->flags |= DICT_FLAG_DUP_REPLACE;
458 
459     /*
460      * Sendmail compatibility: add the @:@ signature to indicate that the
461      * database is complete. This might be needed by NIS clients running
462      * sendmail.
463      */
464     mkmap_append(mkmap, "@", "@");
465     if (mkmap->dict->error)
466 	msg_fatal("table %s:%s: write error: %m",
467 		  mkmap->dict->type, mkmap->dict->name);
468 
469     /*
470      * NIS compatibility: add time and master info. Unlike other information,
471      * this information MUST be written without a trailing null appended to
472      * key or value.
473      */
474     mkmap->dict->flags &= ~DICT_FLAG_TRY1NULL;
475     mkmap->dict->flags |= DICT_FLAG_TRY0NULL;
476     vstring_sprintf(value_buffer, "%010ld", (long) time((time_t *) 0));
477 #if (defined(HAS_NIS) || defined(HAS_NISPLUS))
478     mkmap->dict->flags &= ~DICT_FLAG_FOLD_FIX;
479     mkmap_append(mkmap, "YP_LAST_MODIFIED", STR(value_buffer));
480     mkmap_append(mkmap, "YP_MASTER_NAME", var_myhostname);
481 #endif
482 
483     /*
484      * Close the alias database, and release the lock.
485      */
486     mkmap_close(mkmap);
487 
488     /*
489      * Cleanup. We're about to terminate, but it is a good sanity check.
490      */
491     vstring_free(value_buffer);
492     vstring_free(key_buffer);
493     vstring_free(line_buffer);
494     if (source_fp != VSTREAM_IN)
495 	vstream_fclose(source_fp);
496 }
497 
498 /* postalias_queries - apply multiple requests from stdin */
499 
500 static int postalias_queries(VSTREAM *in, char **maps, const int map_count,
501 			             const int dict_flags)
502 {
503     int     found = 0;
504     VSTRING *keybuf = vstring_alloc(100);
505     DICT  **dicts;
506     const char *map_name;
507     const char *value;
508     int     n;
509 
510     /*
511      * Sanity check.
512      */
513     if (map_count <= 0)
514 	msg_panic("postalias_queries: bad map count");
515 
516     /*
517      * Prepare to open maps lazily.
518      */
519     dicts = (DICT **) mymalloc(sizeof(*dicts) * map_count);
520     for (n = 0; n < map_count; n++)
521 	dicts[n] = 0;
522 
523     /*
524      * Perform all queries. Open maps on the fly, to avoid opening
525      * unnecessary maps.
526      */
527     while (vstring_get_nonl(keybuf, in) != VSTREAM_EOF) {
528 	for (n = 0; n < map_count; n++) {
529 	    if (dicts[n] == 0)
530 		dicts[n] = ((map_name = split_at(maps[n], ':')) != 0 ?
531 		       dict_open3(maps[n], map_name, O_RDONLY, dict_flags) :
532 		    dict_open3(var_db_type, maps[n], O_RDONLY, dict_flags));
533 	    if ((value = dict_get(dicts[n], STR(keybuf))) != 0) {
534 		if (*value == 0) {
535 		    msg_warn("table %s:%s: key %s: empty string result is not allowed",
536 			     dicts[n]->type, dicts[n]->name, STR(keybuf));
537 		    msg_warn("table %s:%s should return NO RESULT in case of NOT FOUND",
538 			     dicts[n]->type, dicts[n]->name);
539 		}
540 		vstream_printf("%s:	%s\n", STR(keybuf), value);
541 		found = 1;
542 		break;
543 	    }
544 	    if (dicts[n]->error)
545 		msg_fatal("table %s:%s: query error: %m",
546 			  dicts[n]->type, dicts[n]->name);
547 	}
548     }
549     if (found)
550 	vstream_fflush(VSTREAM_OUT);
551 
552     /*
553      * Cleanup.
554      */
555     for (n = 0; n < map_count; n++)
556 	if (dicts[n])
557 	    dict_close(dicts[n]);
558     myfree((void *) dicts);
559     vstring_free(keybuf);
560 
561     return (found);
562 }
563 
564 /* postalias_query - query a map and print the result to stdout */
565 
566 static int postalias_query(const char *map_type, const char *map_name,
567 			           const char *key, int dict_flags)
568 {
569     DICT   *dict;
570     const char *value;
571 
572     dict = dict_open3(map_type, map_name, O_RDONLY, dict_flags);
573     if ((value = dict_get(dict, key)) != 0) {
574 	if (*value == 0) {
575 	    msg_warn("table %s:%s: key %s: empty string result is not allowed",
576 		     map_type, map_name, key);
577 	    msg_warn("table %s:%s should return NO RESULT in case of NOT FOUND",
578 		     map_type, map_name);
579 	}
580 	vstream_printf("%s\n", value);
581     }
582     if (dict->error)
583 	msg_fatal("table %s:%s: query error: %m", dict->type, dict->name);
584     vstream_fflush(VSTREAM_OUT);
585     dict_close(dict);
586     return (value != 0);
587 }
588 
589 /* postalias_deletes - apply multiple requests from stdin */
590 
591 static int postalias_deletes(VSTREAM *in, char **maps, const int map_count,
592 			             int dict_flags)
593 {
594     int     found = 0;
595     VSTRING *keybuf = vstring_alloc(100);
596     DICT  **dicts;
597     const char *map_name;
598     int     n;
599     int     open_flags;
600 
601     /*
602      * Sanity check.
603      */
604     if (map_count <= 0)
605 	msg_panic("postalias_deletes: bad map count");
606 
607     /*
608      * Open maps ahead of time.
609      */
610     dicts = (DICT **) mymalloc(sizeof(*dicts) * map_count);
611     for (n = 0; n < map_count; n++) {
612 	map_name = split_at(maps[n], ':');
613 	if (map_name && strcmp(maps[n], DICT_TYPE_PROXY) == 0)
614 	    open_flags = O_RDWR | O_CREAT;	/* XXX */
615 	else
616 	    open_flags = O_RDWR;
617 	dicts[n] = (map_name != 0 ?
618 		    dict_open3(maps[n], map_name, open_flags, dict_flags) :
619 		  dict_open3(var_db_type, maps[n], open_flags, dict_flags));
620     }
621 
622     /*
623      * Perform all requests.
624      */
625     while (vstring_get_nonl(keybuf, in) != VSTREAM_EOF) {
626 	for (n = 0; n < map_count; n++) {
627 	    found |= (dict_del(dicts[n], STR(keybuf)) == 0);
628 	    if (dicts[n]->error)
629 		msg_fatal("table %s:%s: delete error: %m",
630 			  dicts[n]->type, dicts[n]->name);
631 	}
632     }
633 
634     /*
635      * Cleanup.
636      */
637     for (n = 0; n < map_count; n++)
638 	if (dicts[n])
639 	    dict_close(dicts[n]);
640     myfree((void *) dicts);
641     vstring_free(keybuf);
642 
643     return (found);
644 }
645 
646 /* postalias_delete - delete a key value pair from a map */
647 
648 static int postalias_delete(const char *map_type, const char *map_name,
649 			            const char *key, int dict_flags)
650 {
651     DICT   *dict;
652     int     status;
653     int     open_flags;
654 
655     if (strcmp(map_type, DICT_TYPE_PROXY) == 0)
656 	open_flags = O_RDWR | O_CREAT;		/* XXX */
657     else
658 	open_flags = O_RDWR;
659     dict = dict_open3(map_type, map_name, open_flags, dict_flags);
660     status = dict_del(dict, key);
661     if (dict->error)
662 	msg_fatal("table %s:%s: delete error: %m", dict->type, dict->name);
663     dict_close(dict);
664     return (status == 0);
665 }
666 
667 /* postalias_seq - print all map entries to stdout */
668 
669 static void postalias_seq(const char *map_type, const char *map_name,
670 			          int dict_flags)
671 {
672     DICT   *dict;
673     const char *key;
674     const char *value;
675     int     func;
676 
677     if (strcmp(map_type, DICT_TYPE_PROXY) == 0)
678 	msg_fatal("can't sequence maps via the proxy service");
679     dict = dict_open3(map_type, map_name, O_RDONLY, dict_flags);
680     for (func = DICT_SEQ_FUN_FIRST; /* void */ ; func = DICT_SEQ_FUN_NEXT) {
681 	if (dict_seq(dict, func, &key, &value) != 0)
682 	    break;
683 	if (*key == 0) {
684 	    msg_warn("table %s:%s: empty lookup key value is not allowed",
685 		     map_type, map_name);
686 	} else if (*value == 0) {
687 	    msg_warn("table %s:%s: key %s: empty string result is not allowed",
688 		     map_type, map_name, key);
689 	    msg_warn("table %s:%s should return NO RESULT in case of NOT FOUND",
690 		     map_type, map_name);
691 	}
692 	vstream_printf("%s:	%s\n", key, value);
693     }
694     if (dict->error)
695 	msg_fatal("table %s:%s: sequence error: %m", dict->type, dict->name);
696     vstream_fflush(VSTREAM_OUT);
697     dict_close(dict);
698 }
699 
700 /* usage - explain */
701 
702 static NORETURN usage(char *myname)
703 {
704     msg_fatal("usage: %s [-Nfinoprsuvw] [-c config_dir] [-d key] [-q key] [map_type:]file...",
705 	      myname);
706 }
707 
708 MAIL_VERSION_STAMP_DECLARE;
709 
710 int     main(int argc, char **argv)
711 {
712     char   *path_name;
713     int     ch;
714     int     fd;
715     char   *slash;
716     struct stat st;
717     int     postalias_flags = POSTALIAS_FLAG_AS_OWNER | POSTALIAS_FLAG_SAVE_PERM;
718     int     open_flags = O_RDWR | O_CREAT | O_TRUNC;
719     int     dict_flags = (DICT_FLAG_DUP_WARN | DICT_FLAG_FOLD_FIX
720 			  | DICT_FLAG_UTF8_REQUEST);
721     char   *query = 0;
722     char   *delkey = 0;
723     int     sequence = 0;
724     int     found;
725     ARGV   *import_env;
726 
727     /*
728      * Fingerprint executables and core dumps.
729      */
730     MAIL_VERSION_STAMP_ALLOCATE;
731 
732     /*
733      * Be consistent with file permissions.
734      */
735     umask(022);
736 
737     /*
738      * To minimize confusion, make sure that the standard file descriptors
739      * are open before opening anything else. XXX Work around for 44BSD where
740      * fstat can return EBADF on an open file descriptor.
741      */
742     for (fd = 0; fd < 3; fd++)
743 	if (fstat(fd, &st) == -1
744 	    && (close(fd), open("/dev/null", O_RDWR, 0)) != fd)
745 	    msg_fatal("open /dev/null: %m");
746 
747     /*
748      * Process environment options as early as we can. We are not set-uid,
749      * and we are supposed to be running in a controlled environment.
750      */
751     if (getenv(CONF_ENV_VERB))
752 	msg_verbose = 1;
753 
754     /*
755      * Initialize. Set up logging. Read the global configuration file after
756      * parsing command-line arguments.
757      */
758     if ((slash = strrchr(argv[0], '/')) != 0 && slash[1])
759 	argv[0] = slash + 1;
760     msg_vstream_init(argv[0], VSTREAM_ERR);
761     maillog_client_init(mail_task(argv[0]), MAILLOG_CLIENT_FLAG_NONE);
762 
763     /*
764      * Check the Postfix library version as soon as we enable logging.
765      */
766     MAIL_VERSION_CHECK;
767 
768     /*
769      * Parse JCL.
770      */
771     while ((ch = GETOPT(argc, argv, "Nc:d:finopq:rsuvw")) > 0) {
772 	switch (ch) {
773 	default:
774 	    usage(argv[0]);
775 	    break;
776 	case 'N':
777 	    dict_flags |= DICT_FLAG_TRY1NULL;
778 	    dict_flags &= ~DICT_FLAG_TRY0NULL;
779 	    break;
780 	case 'c':
781 	    if (setenv(CONF_ENV_PATH, optarg, 1) < 0)
782 		msg_fatal("out of memory");
783 	    break;
784 	case 'd':
785 	    if (sequence || query || delkey)
786 		msg_fatal("specify only one of -s -q or -d");
787 	    delkey = optarg;
788 	    break;
789 	case 'f':
790 	    dict_flags &= ~DICT_FLAG_FOLD_FIX;
791 	    break;
792 	case 'i':
793 	    open_flags &= ~O_TRUNC;
794 	    break;
795 	case 'n':
796 	    dict_flags |= DICT_FLAG_TRY0NULL;
797 	    dict_flags &= ~DICT_FLAG_TRY1NULL;
798 	    break;
799 	case 'o':
800 	    postalias_flags &= ~POSTALIAS_FLAG_AS_OWNER;
801 	    break;
802 	case 'p':
803 	    postalias_flags &= ~POSTALIAS_FLAG_SAVE_PERM;
804 	    break;
805 	case 'q':
806 	    if (sequence || query || delkey)
807 		msg_fatal("specify only one of -s -q or -d");
808 	    query = optarg;
809 	    break;
810 	case 'r':
811 	    dict_flags &= ~(DICT_FLAG_DUP_WARN | DICT_FLAG_DUP_IGNORE);
812 	    dict_flags |= DICT_FLAG_DUP_REPLACE;
813 	    break;
814 	case 's':
815 	    if (query || delkey)
816 		msg_fatal("specify only one of -s or -q or -d");
817 	    sequence = 1;
818 	    break;
819 	case 'u':
820 	    dict_flags &= ~DICT_FLAG_UTF8_REQUEST;
821 	    break;
822 	case 'v':
823 	    msg_verbose++;
824 	    break;
825 	case 'w':
826 	    dict_flags &= ~(DICT_FLAG_DUP_WARN | DICT_FLAG_DUP_REPLACE);
827 	    dict_flags |= DICT_FLAG_DUP_IGNORE;
828 	    break;
829 	}
830     }
831     mail_conf_read();
832     /* Enforce consistent operation of different Postfix parts. */
833     import_env = mail_parm_split(VAR_IMPORT_ENVIRON, var_import_environ);
834     update_env(import_env->argv);
835     argv_free(import_env);
836     /* Re-evaluate mail_task() after reading main.cf. */
837     maillog_client_init(mail_task(argv[0]), MAILLOG_CLIENT_FLAG_NONE);
838     mail_dict_init();
839 
840     /*
841      * Use the map type specified by the user, or fall back to a default
842      * database type.
843      */
844     if (delkey) {				/* remove entry */
845 	if (optind + 1 > argc)
846 	    usage(argv[0]);
847 	if (strcmp(delkey, "-") == 0)
848 	    exit(postalias_deletes(VSTREAM_IN, argv + optind, argc - optind,
849 				   dict_flags | DICT_FLAG_LOCK) == 0);
850 	found = 0;
851 	while (optind < argc) {
852 	    if ((path_name = split_at(argv[optind], ':')) != 0) {
853 		found |= postalias_delete(argv[optind], path_name, delkey,
854 					  dict_flags | DICT_FLAG_LOCK);
855 	    } else {
856 		found |= postalias_delete(var_db_type, argv[optind], delkey,
857 					  dict_flags | DICT_FLAG_LOCK);
858 	    }
859 	    optind++;
860 	}
861 	exit(found ? 0 : 1);
862     } else if (query) {				/* query map(s) */
863 	if (optind + 1 > argc)
864 	    usage(argv[0]);
865 	if (strcmp(query, "-") == 0)
866 	    exit(postalias_queries(VSTREAM_IN, argv + optind, argc - optind,
867 				   dict_flags | DICT_FLAG_LOCK) == 0);
868 	while (optind < argc) {
869 	    if ((path_name = split_at(argv[optind], ':')) != 0) {
870 		found = postalias_query(argv[optind], path_name, query,
871 					dict_flags | DICT_FLAG_LOCK);
872 	    } else {
873 		found = postalias_query(var_db_type, argv[optind], query,
874 					dict_flags | DICT_FLAG_LOCK);
875 	    }
876 	    if (found)
877 		exit(0);
878 	    optind++;
879 	}
880 	exit(1);
881     } else if (sequence) {
882 	while (optind < argc) {
883 	    if ((path_name = split_at(argv[optind], ':')) != 0) {
884 		postalias_seq(argv[optind], path_name,
885 			      dict_flags | DICT_FLAG_LOCK);
886 	    } else {
887 		postalias_seq(var_db_type, argv[optind],
888 			      dict_flags | DICT_FLAG_LOCK);
889 	    }
890 	    exit(0);
891 	}
892 	exit(1);
893     } else {					/* create/update map(s) */
894 	if (optind + 1 > argc)
895 	    usage(argv[0]);
896 	while (optind < argc) {
897 	    if ((path_name = split_at(argv[optind], ':')) != 0) {
898 		postalias(argv[optind], path_name, postalias_flags,
899 			  open_flags, dict_flags);
900 	    } else {
901 		postalias(var_db_type, argv[optind], postalias_flags,
902 			  open_flags, dict_flags);
903 	    }
904 	    optind++;
905 	}
906 	exit(0);
907     }
908 }
909