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 *NOCLOBBER 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 /* Incremental mode. */ 283 source_fp = VSTREAM_IN; 284 vstream_control(source_fp, VSTREAM_CTL_PATH, "stdin", VSTREAM_CTL_END); 285 } else { 286 /* Create database. */ 287 if (strcmp(map_type, DICT_TYPE_PROXY) == 0) 288 msg_fatal("can't create maps via the proxy service"); 289 dict_flags |= DICT_FLAG_BULK_UPDATE; 290 if ((source_fp = vstream_fopen(path_name, O_RDONLY, 0)) == 0) 291 msg_fatal("open %s: %m", path_name); 292 } 293 if (fstat(vstream_fileno(source_fp), &st) < 0) 294 msg_fatal("fstat %s: %m", path_name); 295 296 /* 297 * Turn off group/other read permissions as indicated in the source file. 298 */ 299 if ((postalias_flags & POSTALIAS_FLAG_SAVE_PERM) && S_ISREG(st.st_mode)) 300 saved_mask = umask(022 | (~st.st_mode & 077)); 301 302 /* 303 * If running as root, run as the owner of the source file, so that the 304 * result shows proper ownership, and so that a bug in postalias does not 305 * allow privilege escalation. 306 */ 307 if ((postalias_flags & POSTALIAS_FLAG_AS_OWNER) && getuid() == 0 308 && (st.st_uid != geteuid() || st.st_gid != getegid())) 309 set_eugid(st.st_uid, st.st_gid); 310 311 312 /* 313 * Open the database, create it when it does not exist, truncate it when 314 * it does exist, and lock out any spectators. 315 */ 316 mkmap = mkmap_open(map_type, path_name, open_flags, dict_flags); 317 318 /* 319 * And restore the umask, in case it matters. 320 */ 321 if ((postalias_flags & POSTALIAS_FLAG_SAVE_PERM) && S_ISREG(st.st_mode)) 322 umask(saved_mask); 323 324 /* 325 * Trap "exceptions" so that we can restart a bulk-mode update after a 326 * recoverable error. 327 */ 328 for (;;) { 329 if (dict_isjmp(mkmap->dict) != 0 330 && dict_setjmp(mkmap->dict) != 0 331 && vstream_fseek(source_fp, SEEK_SET, 0) < 0) 332 msg_fatal("seek %s: %m", VSTREAM_PATH(source_fp)); 333 334 /* 335 * Add records to the database. 336 */ 337 lineno = 0; 338 while (readlline(line_buffer, source_fp, &lineno)) { 339 340 /* 341 * Tokenize the input, so that we do the right thing when a 342 * quoted localpart contains special characters such as "@", ":" 343 * and so on. 344 */ 345 if ((tok_list = tok822_scan(STR(line_buffer), (TOK822 **) 0)) == 0) 346 continue; 347 348 /* 349 * Enforce the key:value format. Disallow missing keys, 350 * multi-address keys, or missing values. In order to specify an 351 * empty string or value, enclose it in double quotes. 352 */ 353 if ((colon = tok822_find_type(tok_list, ':')) == 0 354 || colon->prev == 0 || colon->next == 0 355 || tok822_rfind_type(colon, ',')) { 356 msg_warn("%s, line %d: need name:value pair", 357 VSTREAM_PATH(source_fp), lineno); 358 tok822_free_tree(tok_list); 359 continue; 360 } 361 362 /* 363 * Key must be local. XXX We should use the Postfix rewriting and 364 * resolving services to handle all address forms correctly. 365 * However, we can't count on the mail system being up when the 366 * alias database is being built, so we're guessing a bit. 367 */ 368 if (tok822_rfind_type(colon, '@') || tok822_rfind_type(colon, '%')) { 369 msg_warn("%s, line %d: name must be local", 370 VSTREAM_PATH(source_fp), lineno); 371 tok822_free_tree(tok_list); 372 continue; 373 } 374 375 /* 376 * Split the input into key and value parts, and convert from 377 * token representation back to string representation. Convert 378 * the key to internal (unquoted) form, because the resolver 379 * produces addresses in internal form. Convert the value to 380 * external (quoted) form, because it will have to be re-parsed 381 * upon lookup. Discard the token representation when done. 382 */ 383 key_list = tok_list; 384 tok_list = 0; 385 value_list = tok822_cut_after(colon); 386 tok822_unlink(colon); 387 tok822_free(colon); 388 389 tok822_internalize(key_buffer, key_list, TOK822_STR_DEFL); 390 tok822_free_tree(key_list); 391 392 tok822_externalize(value_buffer, value_list, TOK822_STR_DEFL); 393 tok822_free_tree(value_list); 394 395 /* 396 * Store the value under a case-insensitive key. 397 */ 398 mkmap_append(mkmap, STR(key_buffer), STR(value_buffer)); 399 if (mkmap->dict->error) 400 msg_fatal("table %s:%s: write error: %m", 401 mkmap->dict->type, mkmap->dict->name); 402 } 403 break; 404 } 405 406 /* 407 * Update or append sendmail and NIS signatures. 408 */ 409 if ((open_flags & O_TRUNC) == 0) 410 mkmap->dict->flags |= DICT_FLAG_DUP_REPLACE; 411 412 /* 413 * Sendmail compatibility: add the @:@ signature to indicate that the 414 * database is complete. This might be needed by NIS clients running 415 * sendmail. 416 */ 417 mkmap_append(mkmap, "@", "@"); 418 if (mkmap->dict->error) 419 msg_fatal("table %s:%s: write error: %m", 420 mkmap->dict->type, mkmap->dict->name); 421 422 /* 423 * NIS compatibility: add time and master info. Unlike other information, 424 * this information MUST be written without a trailing null appended to 425 * key or value. 426 */ 427 mkmap->dict->flags &= ~DICT_FLAG_TRY1NULL; 428 mkmap->dict->flags |= DICT_FLAG_TRY0NULL; 429 vstring_sprintf(value_buffer, "%010ld", (long) time((time_t *) 0)); 430#if (defined(HAS_NIS) || defined(HAS_NISPLUS)) 431 mkmap->dict->flags &= ~DICT_FLAG_FOLD_FIX; 432 mkmap_append(mkmap, "YP_LAST_MODIFIED", STR(value_buffer)); 433 mkmap_append(mkmap, "YP_MASTER_NAME", var_myhostname); 434#endif 435 436 /* 437 * Close the alias database, and release the lock. 438 */ 439 mkmap_close(mkmap); 440 441 /* 442 * Cleanup. We're about to terminate, but it is a good sanity check. 443 */ 444 vstring_free(value_buffer); 445 vstring_free(key_buffer); 446 vstring_free(line_buffer); 447 if (source_fp != VSTREAM_IN) 448 vstream_fclose(source_fp); 449} 450 451/* postalias_queries - apply multiple requests from stdin */ 452 453static int postalias_queries(VSTREAM *in, char **maps, const int map_count, 454 const int dict_flags) 455{ 456 int found = 0; 457 VSTRING *keybuf = vstring_alloc(100); 458 DICT **dicts; 459 const char *map_name; 460 const char *value; 461 int n; 462 463 /* 464 * Sanity check. 465 */ 466 if (map_count <= 0) 467 msg_panic("postalias_queries: bad map count"); 468 469 /* 470 * Prepare to open maps lazily. 471 */ 472 dicts = (DICT **) mymalloc(sizeof(*dicts) * map_count); 473 for (n = 0; n < map_count; n++) 474 dicts[n] = 0; 475 476 /* 477 * Perform all queries. Open maps on the fly, to avoid opening unecessary 478 * maps. 479 */ 480 while (vstring_get_nonl(keybuf, in) != VSTREAM_EOF) { 481 for (n = 0; n < map_count; n++) { 482 if (dicts[n] == 0) 483 dicts[n] = ((map_name = split_at(maps[n], ':')) != 0 ? 484 dict_open3(maps[n], map_name, O_RDONLY, dict_flags) : 485 dict_open3(var_db_type, maps[n], O_RDONLY, dict_flags)); 486 if ((value = dict_get(dicts[n], STR(keybuf))) != 0) { 487 if (*value == 0) { 488 msg_warn("table %s:%s: key %s: empty string result is not allowed", 489 dicts[n]->type, dicts[n]->name, STR(keybuf)); 490 msg_warn("table %s:%s should return NO RESULT in case of NOT FOUND", 491 dicts[n]->type, dicts[n]->name); 492 } 493 vstream_printf("%s: %s\n", STR(keybuf), value); 494 found = 1; 495 break; 496 } 497 if (dicts[n]->error) 498 msg_fatal("table %s:%s: query error: %m", 499 dicts[n]->type, dicts[n]->name); 500 } 501 } 502 if (found) 503 vstream_fflush(VSTREAM_OUT); 504 505 /* 506 * Cleanup. 507 */ 508 for (n = 0; n < map_count; n++) 509 if (dicts[n]) 510 dict_close(dicts[n]); 511 myfree((char *) dicts); 512 vstring_free(keybuf); 513 514 return (found); 515} 516 517/* postalias_query - query a map and print the result to stdout */ 518 519static int postalias_query(const char *map_type, const char *map_name, 520 const char *key, int dict_flags) 521{ 522 DICT *dict; 523 const char *value; 524 525 dict = dict_open3(map_type, map_name, O_RDONLY, dict_flags); 526 if ((value = dict_get(dict, key)) != 0) { 527 if (*value == 0) { 528 msg_warn("table %s:%s: key %s: empty string result is not allowed", 529 map_type, map_name, key); 530 msg_warn("table %s:%s should return NO RESULT in case of NOT FOUND", 531 map_type, map_name); 532 } 533 vstream_printf("%s\n", value); 534 } 535 if (dict->error) 536 msg_fatal("table %s:%s: query error: %m", dict->type, dict->name); 537 vstream_fflush(VSTREAM_OUT); 538 dict_close(dict); 539 return (value != 0); 540} 541 542/* postalias_deletes - apply multiple requests from stdin */ 543 544static int postalias_deletes(VSTREAM *in, char **maps, const int map_count, 545 int dict_flags) 546{ 547 int found = 0; 548 VSTRING *keybuf = vstring_alloc(100); 549 DICT **dicts; 550 const char *map_name; 551 int n; 552 int open_flags; 553 554 /* 555 * Sanity check. 556 */ 557 if (map_count <= 0) 558 msg_panic("postalias_deletes: bad map count"); 559 560 /* 561 * Open maps ahead of time. 562 */ 563 dicts = (DICT **) mymalloc(sizeof(*dicts) * map_count); 564 for (n = 0; n < map_count; n++) { 565 map_name = split_at(maps[n], ':'); 566 if (map_name && strcmp(maps[n], DICT_TYPE_PROXY) == 0) 567 open_flags = O_RDWR | O_CREAT; /* XXX */ 568 else 569 open_flags = O_RDWR; 570 dicts[n] = (map_name != 0 ? 571 dict_open3(maps[n], map_name, open_flags, dict_flags) : 572 dict_open3(var_db_type, maps[n], open_flags, dict_flags)); 573 } 574 575 /* 576 * Perform all requests. 577 */ 578 while (vstring_get_nonl(keybuf, in) != VSTREAM_EOF) { 579 for (n = 0; n < map_count; n++) { 580 found |= (dict_del(dicts[n], STR(keybuf)) == 0); 581 if (dicts[n]->error) 582 msg_fatal("table %s:%s: delete error: %m", 583 dicts[n]->type, dicts[n]->name); 584 } 585 } 586 587 /* 588 * Cleanup. 589 */ 590 for (n = 0; n < map_count; n++) 591 if (dicts[n]) 592 dict_close(dicts[n]); 593 myfree((char *) dicts); 594 vstring_free(keybuf); 595 596 return (found); 597} 598 599/* postalias_delete - delete a key value pair from a map */ 600 601static int postalias_delete(const char *map_type, const char *map_name, 602 const char *key, int dict_flags) 603{ 604 DICT *dict; 605 int status; 606 int open_flags; 607 608 if (strcmp(map_type, DICT_TYPE_PROXY) == 0) 609 open_flags = O_RDWR | O_CREAT; /* XXX */ 610 else 611 open_flags = O_RDWR; 612 dict = dict_open3(map_type, map_name, open_flags, dict_flags); 613 status = dict_del(dict, key); 614 if (dict->error) 615 msg_fatal("table %s:%s: delete error: %m", dict->type, dict->name); 616 dict_close(dict); 617 return (status == 0); 618} 619 620/* postalias_seq - print all map entries to stdout */ 621 622static void postalias_seq(const char *map_type, const char *map_name, 623 int dict_flags) 624{ 625 DICT *dict; 626 const char *key; 627 const char *value; 628 int func; 629 630 if (strcmp(map_type, DICT_TYPE_PROXY) == 0) 631 msg_fatal("can't sequence maps via the proxy service"); 632 dict = dict_open3(map_type, map_name, O_RDONLY, dict_flags); 633 for (func = DICT_SEQ_FUN_FIRST; /* void */ ; func = DICT_SEQ_FUN_NEXT) { 634 if (dict_seq(dict, func, &key, &value) != 0) 635 break; 636 if (*key == 0) { 637 msg_warn("table %s:%s: empty lookup key value is not allowed", 638 map_type, map_name); 639 } else if (*value == 0) { 640 msg_warn("table %s:%s: key %s: empty string result is not allowed", 641 map_type, map_name, key); 642 msg_warn("table %s:%s should return NO RESULT in case of NOT FOUND", 643 map_type, map_name); 644 } 645 vstream_printf("%s: %s\n", key, value); 646 } 647 if (dict->error) 648 msg_fatal("table %s:%s: sequence error: %m", dict->type, dict->name); 649 vstream_fflush(VSTREAM_OUT); 650 dict_close(dict); 651} 652 653/* usage - explain */ 654 655static NORETURN usage(char *myname) 656{ 657 msg_fatal("usage: %s [-Nfinoprsvw] [-c config_dir] [-d key] [-q key] [map_type:]file...", 658 myname); 659} 660 661MAIL_VERSION_STAMP_DECLARE; 662 663int main(int argc, char **argv) 664{ 665 char *path_name; 666 int ch; 667 int fd; 668 char *slash; 669 struct stat st; 670 int postalias_flags = POSTALIAS_FLAG_AS_OWNER | POSTALIAS_FLAG_SAVE_PERM; 671 int open_flags = O_RDWR | O_CREAT | O_TRUNC; 672 int dict_flags = DICT_FLAG_DUP_WARN | DICT_FLAG_FOLD_FIX; 673 char *query = 0; 674 char *delkey = 0; 675 int sequence = 0; 676 int found; 677 678 /* 679 * Fingerprint executables and core dumps. 680 */ 681 MAIL_VERSION_STAMP_ALLOCATE; 682 683 /* 684 * Be consistent with file permissions. 685 */ 686 umask(022); 687 688 /* 689 * To minimize confusion, make sure that the standard file descriptors 690 * are open before opening anything else. XXX Work around for 44BSD where 691 * fstat can return EBADF on an open file descriptor. 692 */ 693 for (fd = 0; fd < 3; fd++) 694 if (fstat(fd, &st) == -1 695 && (close(fd), open("/dev/null", O_RDWR, 0)) != fd) 696 msg_fatal("open /dev/null: %m"); 697 698 /* 699 * Process environment options as early as we can. We are not set-uid, 700 * and we are supposed to be running in a controlled environment. 701 */ 702 if (getenv(CONF_ENV_VERB)) 703 msg_verbose = 1; 704 705 /* 706 * Initialize. Set up logging, read the global configuration file and 707 * extract configuration information. 708 */ 709 if ((slash = strrchr(argv[0], '/')) != 0 && slash[1]) 710 argv[0] = slash + 1; 711 msg_vstream_init(argv[0], VSTREAM_ERR); 712 msg_syslog_init(mail_task(argv[0]), LOG_PID, LOG_FACILITY); 713 714 /* 715 * Check the Postfix library version as soon as we enable logging. 716 */ 717 MAIL_VERSION_CHECK; 718 719 /* 720 * Parse JCL. 721 */ 722 while ((ch = GETOPT(argc, argv, "Nc:d:finopq:rsvw")) > 0) { 723 switch (ch) { 724 default: 725 usage(argv[0]); 726 break; 727 case 'N': 728 dict_flags |= DICT_FLAG_TRY1NULL; 729 dict_flags &= ~DICT_FLAG_TRY0NULL; 730 break; 731 case 'c': 732 if (setenv(CONF_ENV_PATH, optarg, 1) < 0) 733 msg_fatal("out of memory"); 734 break; 735 case 'd': 736 if (sequence || query || delkey) 737 msg_fatal("specify only one of -s -q or -d"); 738 delkey = optarg; 739 break; 740 case 'f': 741 dict_flags &= ~DICT_FLAG_FOLD_FIX; 742 break; 743 case 'i': 744 open_flags &= ~O_TRUNC; 745 break; 746 case 'n': 747 dict_flags |= DICT_FLAG_TRY0NULL; 748 dict_flags &= ~DICT_FLAG_TRY1NULL; 749 break; 750 case 'o': 751 postalias_flags &= ~POSTALIAS_FLAG_AS_OWNER; 752 break; 753 case 'p': 754 postalias_flags &= ~POSTALIAS_FLAG_SAVE_PERM; 755 break; 756 case 'q': 757 if (sequence || query || delkey) 758 msg_fatal("specify only one of -s -q or -d"); 759 query = optarg; 760 break; 761 case 'r': 762 dict_flags &= ~(DICT_FLAG_DUP_WARN | DICT_FLAG_DUP_IGNORE); 763 dict_flags |= DICT_FLAG_DUP_REPLACE; 764 break; 765 case 's': 766 if (query || delkey) 767 msg_fatal("specify only one of -s or -q or -d"); 768 sequence = 1; 769 break; 770 case 'v': 771 msg_verbose++; 772 break; 773 case 'w': 774 dict_flags &= ~(DICT_FLAG_DUP_WARN | DICT_FLAG_DUP_REPLACE); 775 dict_flags |= DICT_FLAG_DUP_IGNORE; 776 break; 777 } 778 } 779 mail_conf_read(); 780 if (strcmp(var_syslog_name, DEF_SYSLOG_NAME) != 0) 781 msg_syslog_init(mail_task(argv[0]), LOG_PID, LOG_FACILITY); 782 mail_dict_init(); 783 784 /* 785 * Use the map type specified by the user, or fall back to a default 786 * database type. 787 */ 788 if (delkey) { /* remove entry */ 789 if (optind + 1 > argc) 790 usage(argv[0]); 791 if (strcmp(delkey, "-") == 0) 792 exit(postalias_deletes(VSTREAM_IN, argv + optind, argc - optind, 793 dict_flags | DICT_FLAG_LOCK) == 0); 794 found = 0; 795 while (optind < argc) { 796 if ((path_name = split_at(argv[optind], ':')) != 0) { 797 found |= postalias_delete(argv[optind], path_name, delkey, 798 dict_flags | DICT_FLAG_LOCK); 799 } else { 800 found |= postalias_delete(var_db_type, argv[optind], delkey, 801 dict_flags | DICT_FLAG_LOCK); 802 } 803 optind++; 804 } 805 exit(found ? 0 : 1); 806 } else if (query) { /* query map(s) */ 807 if (optind + 1 > argc) 808 usage(argv[0]); 809 if (strcmp(query, "-") == 0) 810 exit(postalias_queries(VSTREAM_IN, argv + optind, argc - optind, 811 dict_flags | DICT_FLAG_LOCK) == 0); 812 while (optind < argc) { 813 if ((path_name = split_at(argv[optind], ':')) != 0) { 814 found = postalias_query(argv[optind], path_name, query, 815 dict_flags | DICT_FLAG_LOCK); 816 } else { 817 found = postalias_query(var_db_type, argv[optind], query, 818 dict_flags | DICT_FLAG_LOCK); 819 } 820 if (found) 821 exit(0); 822 optind++; 823 } 824 exit(1); 825 } else if (sequence) { 826 while (optind < argc) { 827 if ((path_name = split_at(argv[optind], ':')) != 0) { 828 postalias_seq(argv[optind], path_name, 829 dict_flags | DICT_FLAG_LOCK); 830 } else { 831 postalias_seq(var_db_type, argv[optind], 832 dict_flags | DICT_FLAG_LOCK); 833 } 834 exit(0); 835 } 836 exit(1); 837 } else { /* create/update map(s) */ 838 if (optind + 1 > argc) 839 usage(argv[0]); 840 while (optind < argc) { 841 if ((path_name = split_at(argv[optind], ':')) != 0) { 842 postalias(argv[optind], path_name, postalias_flags, 843 open_flags, dict_flags); 844 } else { 845 postalias(var_db_type, argv[optind], postalias_flags, 846 open_flags, dict_flags); 847 } 848 optind++; 849 } 850 exit(0); 851 } 852} 853