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