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