1 /* $NetBSD: postalias.c,v 1.5 2023/12/23 20:30:44 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 #include <dict_db.h>
270
271 /* Global library. */
272
273 #include <tok822.h>
274 #include <mail_conf.h>
275 #include <mail_dict.h>
276 #include <mail_params.h>
277 #include <mail_version.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
postalias(char * map_type,char * path_name,int postalias_flags,int open_flags,int dict_flags)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 * Override the default per-table cache size for DB map (re)builds. We
349 * can't do this in the mkmap* functions because those don't have access
350 * to Postfix parameter settings.
351 *
352 * db_cache_size" is defined in util/dict_open.c and defaults to 128kB,
353 * which works well for the lookup code.
354 *
355 * We use a larger per-table cache when building ".db" files. For "hash"
356 * files performance degrades rapidly unless the memory pool is O(file
357 * size).
358 *
359 * For "btree" files performance is good with sorted input even for small
360 * memory pools, but with random input degrades rapidly unless the memory
361 * pool is O(file size).
362 */
363 dict_db_cache_size = var_db_create_buf;
364
365 /*
366 * Open the database, create it when it does not exist, truncate it when
367 * it does exist, and lock out any spectators.
368 */
369 mkmap = mkmap_open(map_type, path_name, open_flags, dict_flags);
370
371 /*
372 * And restore the umask, in case it matters.
373 */
374 if ((postalias_flags & POSTALIAS_FLAG_SAVE_PERM) && S_ISREG(st.st_mode))
375 umask(saved_mask);
376
377 /*
378 * Trap "exceptions" so that we can restart a bulk-mode update after a
379 * recoverable error.
380 */
381 for (;;) {
382 if (dict_isjmp(mkmap->dict) != 0
383 && dict_setjmp(mkmap->dict) != 0
384 && vstream_fseek(source_fp, SEEK_SET, 0) < 0)
385 msg_fatal("seek %s: %m", VSTREAM_PATH(source_fp));
386
387 /*
388 * Add records to the database.
389 */
390 last_line = 0;
391 while (readllines(line_buffer, source_fp, &last_line, &lineno)) {
392
393 /*
394 * First some UTF-8 checks sans casefolding.
395 */
396 if ((mkmap->dict->flags & DICT_FLAG_UTF8_ACTIVE)
397 && !allascii(STR(line_buffer))
398 && !valid_utf8_string(STR(line_buffer), LEN(line_buffer))) {
399 msg_warn("%s, line %d: non-UTF-8 input \"%s\""
400 " -- ignoring this line",
401 VSTREAM_PATH(source_fp), lineno, STR(line_buffer));
402 continue;
403 }
404
405 /*
406 * Tokenize the input, so that we do the right thing when a
407 * quoted localpart contains special characters such as "@", ":"
408 * and so on.
409 */
410 if ((tok_list = tok822_scan(STR(line_buffer), (TOK822 **) 0)) == 0)
411 continue;
412
413 /*
414 * Enforce the key:value format. Disallow missing keys,
415 * multi-address keys, or missing values. In order to specify an
416 * empty string or value, enclose it in double quotes.
417 */
418 if ((colon = tok822_find_type(tok_list, ':')) == 0
419 || colon->prev == 0 || colon->next == 0
420 || tok822_rfind_type(colon, ',')) {
421 msg_warn("%s, line %d: need name:value pair",
422 VSTREAM_PATH(source_fp), lineno);
423 tok822_free_tree(tok_list);
424 continue;
425 }
426
427 /*
428 * Key must be local. XXX We should use the Postfix rewriting and
429 * resolving services to handle all address forms correctly.
430 * However, we can't count on the mail system being up when the
431 * alias database is being built, so we're guessing a bit.
432 */
433 if (tok822_rfind_type(colon, '@') || tok822_rfind_type(colon, '%')) {
434 msg_warn("%s, line %d: name must be local",
435 VSTREAM_PATH(source_fp), lineno);
436 tok822_free_tree(tok_list);
437 continue;
438 }
439
440 /*
441 * Split the input into key and value parts, and convert from
442 * token representation back to string representation. Convert
443 * the key to internal (unquoted) form, because the resolver
444 * produces addresses in internal form. Convert the value to
445 * external (quoted) form, because it will have to be re-parsed
446 * upon lookup. Discard the token representation when done.
447 */
448 key_list = tok_list;
449 tok_list = 0;
450 value_list = tok822_cut_after(colon);
451 tok822_unlink(colon);
452 tok822_free(colon);
453
454 tok822_internalize(key_buffer, key_list, TOK822_STR_DEFL);
455 tok822_free_tree(key_list);
456
457 tok822_externalize(value_buffer, value_list, TOK822_STR_DEFL);
458 tok822_free_tree(value_list);
459
460 /*
461 * Store the value under a case-insensitive key.
462 */
463 mkmap_append(mkmap, STR(key_buffer), STR(value_buffer));
464 if (mkmap->dict->error)
465 msg_fatal("table %s:%s: write error: %m",
466 mkmap->dict->type, mkmap->dict->name);
467 }
468 break;
469 }
470
471 /*
472 * Update or append sendmail and NIS signatures.
473 */
474 if ((open_flags & O_TRUNC) == 0)
475 mkmap->dict->flags |= DICT_FLAG_DUP_REPLACE;
476
477 /*
478 * Sendmail compatibility: add the @:@ signature to indicate that the
479 * database is complete. This might be needed by NIS clients running
480 * sendmail.
481 */
482 mkmap_append(mkmap, "@", "@");
483 if (mkmap->dict->error)
484 msg_fatal("table %s:%s: write error: %m",
485 mkmap->dict->type, mkmap->dict->name);
486
487 /*
488 * NIS compatibility: add time and master info. Unlike other information,
489 * this information MUST be written without a trailing null appended to
490 * key or value.
491 */
492 mkmap->dict->flags &= ~DICT_FLAG_TRY1NULL;
493 mkmap->dict->flags |= DICT_FLAG_TRY0NULL;
494 vstring_sprintf(value_buffer, "%010ld", (long) time((time_t *) 0));
495 #if (defined(HAS_NIS) || defined(HAS_NISPLUS))
496 mkmap->dict->flags &= ~DICT_FLAG_FOLD_FIX;
497 mkmap_append(mkmap, "YP_LAST_MODIFIED", STR(value_buffer));
498 mkmap_append(mkmap, "YP_MASTER_NAME", var_myhostname);
499 #endif
500
501 /*
502 * Close the alias database, and release the lock.
503 */
504 mkmap_close(mkmap);
505
506 /*
507 * Cleanup. We're about to terminate, but it is a good sanity check.
508 */
509 vstring_free(value_buffer);
510 vstring_free(key_buffer);
511 vstring_free(line_buffer);
512 if (source_fp != VSTREAM_IN)
513 vstream_fclose(source_fp);
514 }
515
516 /* postalias_queries - apply multiple requests from stdin */
517
postalias_queries(VSTREAM * in,char ** maps,const int map_count,const int dict_flags)518 static int postalias_queries(VSTREAM *in, char **maps, const int map_count,
519 const int dict_flags)
520 {
521 int found = 0;
522 VSTRING *keybuf = vstring_alloc(100);
523 DICT **dicts;
524 const char *map_name;
525 const char *value;
526 int n;
527
528 /*
529 * Sanity check.
530 */
531 if (map_count <= 0)
532 msg_panic("postalias_queries: bad map count");
533
534 /*
535 * Prepare to open maps lazily.
536 */
537 dicts = (DICT **) mymalloc(sizeof(*dicts) * map_count);
538 for (n = 0; n < map_count; n++)
539 dicts[n] = 0;
540
541 /*
542 * Perform all queries. Open maps on the fly, to avoid opening
543 * unnecessary maps.
544 */
545 while (vstring_get_nonl(keybuf, in) != VSTREAM_EOF) {
546 for (n = 0; n < map_count; n++) {
547 if (dicts[n] == 0)
548 dicts[n] = ((map_name = split_at(maps[n], ':')) != 0 ?
549 dict_open3(maps[n], map_name, O_RDONLY, dict_flags) :
550 dict_open3(var_db_type, maps[n], O_RDONLY, dict_flags));
551 if ((value = dict_get(dicts[n], STR(keybuf))) != 0) {
552 if (*value == 0) {
553 msg_warn("table %s:%s: key %s: empty string result is not allowed",
554 dicts[n]->type, dicts[n]->name, STR(keybuf));
555 msg_warn("table %s:%s should return NO RESULT in case of NOT FOUND",
556 dicts[n]->type, dicts[n]->name);
557 }
558 vstream_printf("%s: %s\n", STR(keybuf), value);
559 found = 1;
560 break;
561 }
562 if (dicts[n]->error)
563 msg_fatal("table %s:%s: query error: %m",
564 dicts[n]->type, dicts[n]->name);
565 }
566 }
567 if (found)
568 vstream_fflush(VSTREAM_OUT);
569
570 /*
571 * Cleanup.
572 */
573 for (n = 0; n < map_count; n++)
574 if (dicts[n])
575 dict_close(dicts[n]);
576 myfree((void *) dicts);
577 vstring_free(keybuf);
578
579 return (found);
580 }
581
582 /* postalias_query - query a map and print the result to stdout */
583
postalias_query(const char * map_type,const char * map_name,const char * key,int dict_flags)584 static int postalias_query(const char *map_type, const char *map_name,
585 const char *key, int dict_flags)
586 {
587 DICT *dict;
588 const char *value;
589
590 dict = dict_open3(map_type, map_name, O_RDONLY, dict_flags);
591 if ((value = dict_get(dict, key)) != 0) {
592 if (*value == 0) {
593 msg_warn("table %s:%s: key %s: empty string result is not allowed",
594 map_type, map_name, key);
595 msg_warn("table %s:%s should return NO RESULT in case of NOT FOUND",
596 map_type, map_name);
597 }
598 vstream_printf("%s\n", value);
599 }
600 if (dict->error)
601 msg_fatal("table %s:%s: query error: %m", dict->type, dict->name);
602 vstream_fflush(VSTREAM_OUT);
603 dict_close(dict);
604 return (value != 0);
605 }
606
607 /* postalias_deletes - apply multiple requests from stdin */
608
postalias_deletes(VSTREAM * in,char ** maps,const int map_count,int dict_flags)609 static int postalias_deletes(VSTREAM *in, char **maps, const int map_count,
610 int dict_flags)
611 {
612 int found = 0;
613 VSTRING *keybuf = vstring_alloc(100);
614 DICT **dicts;
615 const char *map_name;
616 int n;
617 int open_flags;
618
619 /*
620 * Sanity check.
621 */
622 if (map_count <= 0)
623 msg_panic("postalias_deletes: bad map count");
624
625 /*
626 * Open maps ahead of time.
627 */
628 dicts = (DICT **) mymalloc(sizeof(*dicts) * map_count);
629 for (n = 0; n < map_count; n++) {
630 map_name = split_at(maps[n], ':');
631 if (map_name && strcmp(maps[n], DICT_TYPE_PROXY) == 0)
632 open_flags = O_RDWR | O_CREAT; /* XXX */
633 else
634 open_flags = O_RDWR;
635 dicts[n] = (map_name != 0 ?
636 dict_open3(maps[n], map_name, open_flags, dict_flags) :
637 dict_open3(var_db_type, maps[n], open_flags, dict_flags));
638 }
639
640 /*
641 * Perform all requests.
642 */
643 while (vstring_get_nonl(keybuf, in) != VSTREAM_EOF) {
644 for (n = 0; n < map_count; n++) {
645 found |= (dict_del(dicts[n], STR(keybuf)) == 0);
646 if (dicts[n]->error)
647 msg_fatal("table %s:%s: delete error: %m",
648 dicts[n]->type, dicts[n]->name);
649 }
650 }
651
652 /*
653 * Cleanup.
654 */
655 for (n = 0; n < map_count; n++)
656 if (dicts[n])
657 dict_close(dicts[n]);
658 myfree((void *) dicts);
659 vstring_free(keybuf);
660
661 return (found);
662 }
663
664 /* postalias_delete - delete a key value pair from a map */
665
postalias_delete(const char * map_type,const char * map_name,const char * key,int dict_flags)666 static int postalias_delete(const char *map_type, const char *map_name,
667 const char *key, int dict_flags)
668 {
669 DICT *dict;
670 int status;
671 int open_flags;
672
673 if (strcmp(map_type, DICT_TYPE_PROXY) == 0)
674 open_flags = O_RDWR | O_CREAT; /* XXX */
675 else
676 open_flags = O_RDWR;
677 dict = dict_open3(map_type, map_name, open_flags, dict_flags);
678 status = dict_del(dict, key);
679 if (dict->error)
680 msg_fatal("table %s:%s: delete error: %m", dict->type, dict->name);
681 dict_close(dict);
682 return (status == 0);
683 }
684
685 /* postalias_seq - print all map entries to stdout */
686
postalias_seq(const char * map_type,const char * map_name,int dict_flags)687 static void postalias_seq(const char *map_type, const char *map_name,
688 int dict_flags)
689 {
690 DICT *dict;
691 const char *key;
692 const char *value;
693 int func;
694
695 if (strcmp(map_type, DICT_TYPE_PROXY) == 0)
696 msg_fatal("can't sequence maps via the proxy service");
697 dict = dict_open3(map_type, map_name, O_RDONLY, dict_flags);
698 for (func = DICT_SEQ_FUN_FIRST; /* void */ ; func = DICT_SEQ_FUN_NEXT) {
699 if (dict_seq(dict, func, &key, &value) != 0)
700 break;
701 if (*key == 0) {
702 msg_warn("table %s:%s: empty lookup key value is not allowed",
703 map_type, map_name);
704 } else if (*value == 0) {
705 msg_warn("table %s:%s: key %s: empty string result is not allowed",
706 map_type, map_name, key);
707 msg_warn("table %s:%s should return NO RESULT in case of NOT FOUND",
708 map_type, map_name);
709 }
710 vstream_printf("%s: %s\n", key, value);
711 }
712 if (dict->error)
713 msg_fatal("table %s:%s: sequence error: %m", dict->type, dict->name);
714 vstream_fflush(VSTREAM_OUT);
715 dict_close(dict);
716 }
717
718 /* usage - explain */
719
usage(char * myname)720 static NORETURN usage(char *myname)
721 {
722 msg_fatal("usage: %s [-Nfinoprsuvw] [-c config_dir] [-d key] [-q key] [map_type:]file...",
723 myname);
724 }
725
726 MAIL_VERSION_STAMP_DECLARE;
727
main(int argc,char ** argv)728 int main(int argc, char **argv)
729 {
730 char *path_name;
731 int ch;
732 int fd;
733 char *slash;
734 struct stat st;
735 int postalias_flags = POSTALIAS_FLAG_AS_OWNER | POSTALIAS_FLAG_SAVE_PERM;
736 int open_flags = O_RDWR | O_CREAT | O_TRUNC;
737 int dict_flags = (DICT_FLAG_DUP_WARN | DICT_FLAG_FOLD_FIX
738 | DICT_FLAG_UTF8_REQUEST);
739 char *query = 0;
740 char *delkey = 0;
741 int sequence = 0;
742 int found;
743 ARGV *import_env;
744
745 /*
746 * Fingerprint executables and core dumps.
747 */
748 MAIL_VERSION_STAMP_ALLOCATE;
749
750 /*
751 * Be consistent with file permissions.
752 */
753 umask(022);
754
755 /*
756 * To minimize confusion, make sure that the standard file descriptors
757 * are open before opening anything else. XXX Work around for 44BSD where
758 * fstat can return EBADF on an open file descriptor.
759 */
760 for (fd = 0; fd < 3; fd++)
761 if (fstat(fd, &st) == -1
762 && (close(fd), open("/dev/null", O_RDWR, 0)) != fd)
763 msg_fatal("open /dev/null: %m");
764
765 /*
766 * Process environment options as early as we can. We are not set-uid,
767 * and we are supposed to be running in a controlled environment.
768 */
769 if (getenv(CONF_ENV_VERB))
770 msg_verbose = 1;
771
772 /*
773 * Initialize. Set up logging. Read the global configuration file after
774 * parsing command-line arguments.
775 */
776 if ((slash = strrchr(argv[0], '/')) != 0 && slash[1])
777 argv[0] = slash + 1;
778 msg_vstream_init(argv[0], VSTREAM_ERR);
779 maillog_client_init(mail_task(argv[0]), MAILLOG_CLIENT_FLAG_NONE);
780
781 /*
782 * Check the Postfix library version as soon as we enable logging.
783 */
784 MAIL_VERSION_CHECK;
785
786 /*
787 * Parse JCL.
788 */
789 while ((ch = GETOPT(argc, argv, "Nc:d:finopq:rsuvw")) > 0) {
790 switch (ch) {
791 default:
792 usage(argv[0]);
793 break;
794 case 'N':
795 dict_flags |= DICT_FLAG_TRY1NULL;
796 dict_flags &= ~DICT_FLAG_TRY0NULL;
797 break;
798 case 'c':
799 if (setenv(CONF_ENV_PATH, optarg, 1) < 0)
800 msg_fatal("out of memory");
801 break;
802 case 'd':
803 if (sequence || query || delkey)
804 msg_fatal("specify only one of -s -q or -d");
805 delkey = optarg;
806 break;
807 case 'f':
808 dict_flags &= ~DICT_FLAG_FOLD_FIX;
809 break;
810 case 'i':
811 open_flags &= ~O_TRUNC;
812 break;
813 case 'n':
814 dict_flags |= DICT_FLAG_TRY0NULL;
815 dict_flags &= ~DICT_FLAG_TRY1NULL;
816 break;
817 case 'o':
818 postalias_flags &= ~POSTALIAS_FLAG_AS_OWNER;
819 break;
820 case 'p':
821 postalias_flags &= ~POSTALIAS_FLAG_SAVE_PERM;
822 break;
823 case 'q':
824 if (sequence || query || delkey)
825 msg_fatal("specify only one of -s -q or -d");
826 query = optarg;
827 break;
828 case 'r':
829 dict_flags &= ~(DICT_FLAG_DUP_WARN | DICT_FLAG_DUP_IGNORE);
830 dict_flags |= DICT_FLAG_DUP_REPLACE;
831 break;
832 case 's':
833 if (query || delkey)
834 msg_fatal("specify only one of -s or -q or -d");
835 sequence = 1;
836 break;
837 case 'u':
838 dict_flags &= ~DICT_FLAG_UTF8_REQUEST;
839 break;
840 case 'v':
841 msg_verbose++;
842 break;
843 case 'w':
844 dict_flags &= ~(DICT_FLAG_DUP_WARN | DICT_FLAG_DUP_REPLACE);
845 dict_flags |= DICT_FLAG_DUP_IGNORE;
846 break;
847 }
848 }
849 mail_conf_read();
850 /* Enforce consistent operation of different Postfix parts. */
851 import_env = mail_parm_split(VAR_IMPORT_ENVIRON, var_import_environ);
852 update_env(import_env->argv);
853 argv_free(import_env);
854 /* Re-evaluate mail_task() after reading main.cf. */
855 maillog_client_init(mail_task(argv[0]), MAILLOG_CLIENT_FLAG_NONE);
856 mail_dict_init();
857
858 /*
859 * Use the map type specified by the user, or fall back to a default
860 * database type.
861 */
862 if (delkey) { /* remove entry */
863 if (optind + 1 > argc)
864 usage(argv[0]);
865 if (strcmp(delkey, "-") == 0)
866 exit(postalias_deletes(VSTREAM_IN, argv + optind, argc - optind,
867 dict_flags | DICT_FLAG_LOCK) == 0);
868 found = 0;
869 while (optind < argc) {
870 if ((path_name = split_at(argv[optind], ':')) != 0) {
871 found |= postalias_delete(argv[optind], path_name, delkey,
872 dict_flags | DICT_FLAG_LOCK);
873 } else {
874 found |= postalias_delete(var_db_type, argv[optind], delkey,
875 dict_flags | DICT_FLAG_LOCK);
876 }
877 optind++;
878 }
879 exit(found ? 0 : 1);
880 } else if (query) { /* query map(s) */
881 if (optind + 1 > argc)
882 usage(argv[0]);
883 if (strcmp(query, "-") == 0)
884 exit(postalias_queries(VSTREAM_IN, argv + optind, argc - optind,
885 dict_flags | DICT_FLAG_LOCK) == 0);
886 while (optind < argc) {
887 if ((path_name = split_at(argv[optind], ':')) != 0) {
888 found = postalias_query(argv[optind], path_name, query,
889 dict_flags | DICT_FLAG_LOCK);
890 } else {
891 found = postalias_query(var_db_type, argv[optind], query,
892 dict_flags | DICT_FLAG_LOCK);
893 }
894 if (found)
895 exit(0);
896 optind++;
897 }
898 exit(1);
899 } else if (sequence) {
900 while (optind < argc) {
901 if ((path_name = split_at(argv[optind], ':')) != 0) {
902 postalias_seq(argv[optind], path_name,
903 dict_flags | DICT_FLAG_LOCK);
904 } else {
905 postalias_seq(var_db_type, argv[optind],
906 dict_flags | DICT_FLAG_LOCK);
907 }
908 exit(0);
909 }
910 exit(1);
911 } else { /* create/update map(s) */
912 if (optind + 1 > argc)
913 usage(argv[0]);
914 while (optind < argc) {
915 if ((path_name = split_at(argv[optind], ':')) != 0) {
916 postalias(argv[optind], path_name, postalias_flags,
917 open_flags, dict_flags);
918 } else {
919 postalias(var_db_type, argv[optind], postalias_flags,
920 open_flags, dict_flags);
921 }
922 optind++;
923 }
924 exit(0);
925 }
926 }
927