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