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

« back to all changes in this revision

Viewing changes to src/global/mail_conf.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
/*      mail_conf 3
 
4
/* SUMMARY
 
5
/*      global configuration parameter management
 
6
/* SYNOPSIS
 
7
/*      #include <mail_conf.h>
 
8
/*
 
9
/*      void    mail_conf_read()
 
10
/*
 
11
/*      void    mail_conf_suck()
 
12
/*
 
13
/*      void    mail_conf_update(name, value)
 
14
/*      const char *name;
 
15
/*      const char *value;
 
16
/*
 
17
/*      const char *mail_conf_lookup(name)
 
18
/*      const char *name;
 
19
/*
 
20
/*      const char *mail_conf_eval(string)
 
21
/*      const char *string;
 
22
/*
 
23
/*      const char *mail_conf_lookup_eval(name)
 
24
/*      const char *name;
 
25
/* DESCRIPTION
 
26
/*      mail_conf_suck() reads the global Postfix configuration file, and
 
27
/*      stores its values into a global configuration dictionary.
 
28
/*
 
29
/*      mail_conf_read() invokes mail_conf_suck() and assigns the values
 
30
/*      to global variables by calling mail_params_init().
 
31
/*
 
32
/*      The following routines are wrappers around the generic dictionary
 
33
/*      access routines.
 
34
/*
 
35
/*      mail_conf_update() updates the named global parameter. This has
 
36
/*      no effect on parameters whose value has already been looked up.
 
37
/*      The update succeeds or the program terminates with fatal error.
 
38
/*
 
39
/*      mail_conf_lookup() looks up the value of the named parameter.
 
40
/*      A null pointer result means the parameter was not found.
 
41
/*      The result is volatile and should be copied if it is to be
 
42
/*      used for any appreciable amount of time.
 
43
/*
 
44
/*      mail_conf_eval() recursively expands any $parameters in the
 
45
/*      string argument. The result is volatile and should be copied
 
46
/*      if it is to be used for any appreciable amount of time.
 
47
/*
 
48
/*      mail_conf_lookup_eval() looks up the named parameter, and expands any
 
49
/*      $parameters in the result. The result is volatile and should be
 
50
/*      copied if it is to be used for any appreciable amount of time.
 
51
/* DIAGNOSTICS
 
52
/*      Fatal errors: malformed numerical value.
 
53
/* ENVIRONMENT
 
54
/*      MAIL_CONFIG, non-default configuration database
 
55
/*      MAIL_VERBOSE, enable verbose mode
 
56
/* FILES
 
57
/*      /etc/postfix: default Postfix configuration directory.
 
58
/* SEE ALSO
 
59
/*      dict(3) generic dictionary manager
 
60
/*      mail_conf_int(3) integer-valued parameters
 
61
/*      mail_conf_str(3) string-valued parameters
 
62
/* LICENSE
 
63
/* .ad
 
64
/* .fi
 
65
/*      The Secure Mailer license must be distributed with this software.
 
66
/* AUTHOR(S)
 
67
/*      Wietse Venema
 
68
/*      IBM T.J. Watson Research
 
69
/*      P.O. Box 704
 
70
/*      Yorktown Heights, NY 10598, USA
 
71
/*--*/
 
72
 
 
73
/* System library. */
 
74
 
 
75
#include <sys_defs.h>
 
76
#include <unistd.h>
 
77
#include <stdlib.h>
 
78
#include <string.h>
 
79
 
 
80
/* Utility library. */
 
81
 
 
82
#include <msg.h>
 
83
#include <mymalloc.h>
 
84
#include <vstream.h>
 
85
#include <vstring.h>
 
86
#include <dict.h>
 
87
#include <safe.h>
 
88
#include <stringops.h>
 
89
#include <readlline.h>
 
90
 
 
91
/* Global library. */
 
92
 
 
93
#include "mail_params.h"
 
94
#include "mail_conf.h"
 
95
 
 
96
/* mail_conf_checkdir - authorize non-default directory */
 
97
 
 
98
static void mail_conf_checkdir(const char *config_dir)
 
99
{
 
100
    VSTRING *buf;
 
101
    VSTREAM *fp;
 
102
    char   *path;
 
103
    char   *name;
 
104
    char   *value;
 
105
    char   *cp;
 
106
    int     found = 0;
 
107
 
 
108
    /*
 
109
     * If running set-[ug]id, require that a non-default configuration
 
110
     * directory name is blessed as a bona fide configuration directory in
 
111
     * the default main.cf file.
 
112
     */
 
113
    path = concatenate(DEF_CONFIG_DIR, "/", "main.cf", (char *) 0);
 
114
    if ((fp = vstream_fopen(path, O_RDONLY, 0)) == 0)
 
115
        msg_fatal("open file %s: %m", path);
 
116
 
 
117
    buf = vstring_alloc(1);
 
118
    while (found == 0 && readlline(buf, fp, (int *) 0)) {
 
119
        if (split_nameval(vstring_str(buf), &name, &value) == 0
 
120
            && strcmp(name, VAR_CONFIG_DIRS) == 0) {
 
121
            while (found == 0 && (cp = mystrtok(&value, ", \t\r\n")) != 0)
 
122
                if (strcmp(cp, config_dir) == 0)
 
123
                    found = 1;
 
124
        }
 
125
    }
 
126
    if (vstream_fclose(fp))
 
127
        msg_fatal("read file %s: %m", path);
 
128
    vstring_free(buf);
 
129
 
 
130
    if (found == 0) {
 
131
        msg_error("untrusted configuration directory name: %s", config_dir);
 
132
        msg_fatal("specify \"%s = %s\" in %s",
 
133
                  VAR_CONFIG_DIRS, config_dir, path);
 
134
    }
 
135
    myfree(path);
 
136
}
 
137
 
 
138
/* mail_conf_read - read global configuration file */
 
139
 
 
140
void    mail_conf_read(void)
 
141
{
 
142
    mail_conf_suck();
 
143
    mail_params_init();
 
144
}
 
145
 
 
146
/* mail_conf_suck - suck in the global configuration file */
 
147
 
 
148
void    mail_conf_suck(void)
 
149
{
 
150
    char   *config_dir;
 
151
    char   *path;
 
152
 
 
153
    /*
 
154
     * Permit references to unknown configuration variable names. We rely on
 
155
     * a separate configuration checking tool to spot misspelled names and
 
156
     * other kinds of trouble. Enter the configuration directory into the
 
157
     * default dictionary.
 
158
     */
 
159
    dict_unknown_allowed = 1;
 
160
    if (var_config_dir)
 
161
        myfree(var_config_dir);
 
162
    if ((config_dir = getenv(CONF_ENV_PATH)) == 0)
 
163
        config_dir = DEF_CONFIG_DIR;
 
164
    var_config_dir = mystrdup(config_dir);
 
165
    set_mail_conf_str(VAR_CONFIG_DIR, var_config_dir);
 
166
 
 
167
    /*
 
168
     * If the configuration directory name comes from a different trust
 
169
     * domain, require that it is listed in the default main.cf file.
 
170
     */
 
171
    if (strcmp(var_config_dir, DEF_CONFIG_DIR) != 0     /* non-default */
 
172
        && safe_getenv(CONF_ENV_PATH) == 0      /* non-default */
 
173
        && geteuid() != 0)                      /* untrusted */
 
174
        mail_conf_checkdir(var_config_dir);
 
175
    path = concatenate(var_config_dir, "/", "main.cf", (char *) 0);
 
176
    dict_load_file(CONFIG_DICT, path);
 
177
    myfree(path);
 
178
}
 
179
 
 
180
/* mail_conf_eval - expand macros in string */
 
181
 
 
182
const char *mail_conf_eval(const char *string)
 
183
{
 
184
#define RECURSIVE       1
 
185
 
 
186
    return (dict_eval(CONFIG_DICT, string, RECURSIVE));
 
187
}
 
188
 
 
189
/* mail_conf_lookup - lookup named variable */
 
190
 
 
191
const char *mail_conf_lookup(const char *name)
 
192
{
 
193
    return (dict_lookup(CONFIG_DICT, name));
 
194
}
 
195
 
 
196
/* mail_conf_lookup_eval - expand named variable */
 
197
 
 
198
const char *mail_conf_lookup_eval(const char *name)
 
199
{
 
200
    const char *value;
 
201
 
 
202
#define RECURSIVE       1
 
203
 
 
204
    if ((value = dict_lookup(CONFIG_DICT, name)) != 0)
 
205
        value = dict_eval(CONFIG_DICT, value, RECURSIVE);
 
206
    return (value);
 
207
}
 
208
 
 
209
/* mail_conf_update - update parameter */
 
210
 
 
211
void    mail_conf_update(const char *key, const char *value)
 
212
{
 
213
    dict_update(CONFIG_DICT, key, value);
 
214
}