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