~ubuntu-branches/ubuntu/hoary/postfix/hoary-security

« back to all changes in this revision

Viewing changes to src/postalias/postalias.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-10-06 11:50:33 UTC
  • Revision ID: james.westby@ubuntu.com-20041006115033-ooo6yfg6kmoteu04
Tags: upstream-2.1.3
ImportĀ upstreamĀ versionĀ 2.1.3

Show diffs side-by-side

added added

removed removed

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