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