-
Notifications
You must be signed in to change notification settings - Fork 51
/
conf.c
263 lines (233 loc) · 6.81 KB
/
conf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* Copyright (c) 2008 The DragonFly Project. All rights reserved.
*
* This code is derived from software contributed to The DragonFly Project
* by Matthias Schmidt <[email protected]>, University of Marburg,
* Germany.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of The DragonFly Project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific, prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <stdarg.h>
#include "dma.h"
#define DP ": \t"
#define EQS " \t"
/*
* Remove trailing \n's
*/
void
trim_line(char *line)
{
size_t linelen;
char *p;
if ((p = strchr(line, '\n')))
*p = (char)0;
/* Escape leading dot in every case */
linelen = strlen(line);
if (line[0] == '.') {
if ((linelen + 2) > 1000) {
syslog(LOG_CRIT, "Cannot escape leading dot. Buffer overflow");
exit(EX_DATAERR);
}
memmove((line + 1), line, (linelen + 1));
line[0] = '.';
}
}
static void
chomp(char *str)
{
size_t len = strlen(str);
if (len == 0)
return;
if (str[len - 1] == '\n')
str[len - 1] = 0;
}
/*
* Read the SMTP authentication config file
*
* file format is:
* user|host:password
*
* A line starting with # is treated as comment and ignored.
*/
void
parse_authfile(const char *path)
{
char line[2048];
struct authuser *au;
FILE *a;
char *data;
int lineno = 0;
a = fopen(path, "r");
if (a == NULL) {
errlog(EX_NOINPUT, "can not open auth file `%s'", path);
/* NOTREACHED */
}
while (!feof(a)) {
if (fgets(line, sizeof(line), a) == NULL)
break;
lineno++;
chomp(line);
/* We hit a comment */
if (*line == '#')
continue;
/* Ignore empty lines */
if (*line == 0)
continue;
au = calloc(1, sizeof(*au));
if (au == NULL)
errlog(EX_OSERR, NULL);
data = strdup(line);
au->login = strsep(&data, "|");
au->host = strsep(&data, DP);
au->password = data;
if (au->login == NULL ||
au->host == NULL ||
au->password == NULL) {
errlogx(EX_CONFIG, "syntax error in authfile %s:%d", path, lineno);
/* NOTREACHED */
}
SLIST_INSERT_HEAD(&authusers, au, next);
}
fclose(a);
}
/*
* XXX TODO
* Check for bad things[TM]
*/
void
parse_conf(const char *config_path)
{
char *word;
char *data;
FILE *conf;
char line[2048];
int lineno = 0;
conf = fopen(config_path, "r");
if (conf == NULL) {
/* Don't treat a non-existing config file as error */
if (errno == ENOENT)
return;
errlog(EX_NOINPUT, "can not open config `%s'", config_path);
/* NOTREACHED */
}
while (!feof(conf)) {
if (fgets(line, sizeof(line), conf) == NULL)
break;
lineno++;
chomp(line);
/* We hit a comment */
if (strchr(line, '#'))
*strchr(line, '#') = 0;
data = line;
word = strsep(&data, EQS);
/* Ignore empty lines */
if (word == NULL || *word == 0)
continue;
if (data != NULL && *data != 0)
data = strdup(data);
else
data = NULL;
if (strcmp(word, "SMARTHOST") == 0 && data != NULL)
config.smarthost = data;
else if (strcmp(word, "PORT") == 0 && data != NULL)
config.port = atoi(data);
else if (strcmp(word, "ALIASES") == 0 && data != NULL)
config.aliases = data;
else if (strcmp(word, "SPOOLDIR") == 0 && data != NULL)
config.spooldir = data;
else if (strcmp(word, "AUTHPATH") == 0 && data != NULL)
config.authpath= data;
else if (strcmp(word, "CERTFILE") == 0 && data != NULL)
config.certfile = data;
else if (strcmp(word, "MAILNAME") == 0 && data != NULL)
config.mailname = data;
else if (strcmp(word, "MASQUERADE") == 0 && data != NULL) {
char *user = NULL, *host = NULL;
if (strrchr(data, '@')) {
host = strrchr(data, '@');
*host = 0;
host++;
user = data;
} else {
host = data;
}
if (host && *host == 0)
host = NULL;
if (user && *user == 0)
user = NULL;
config.masquerade_host = host;
config.masquerade_user = user;
} else if (strcmp(word, "STARTTLS") == 0 && data == NULL)
config.features |= STARTTLS;
else if (strcmp(word, "FINGERPRINT") == 0) {
if (strlen(data) != SHA256_DIGEST_LENGTH * 2) {
errlogx(EX_CONFIG, "invalid sha256 fingerprint length");
}
unsigned char *fingerprint = malloc(SHA256_DIGEST_LENGTH);
if (fingerprint == NULL) {
errlogx(EX_CONFIG, "fingerprint allocation failed");
}
unsigned int i;
for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
if(sscanf(data + 2 * i, "%02hhx", &fingerprint[i]) != 1) {
errlogx(EX_CONFIG, "failed to read fingerprint");
}
}
free(data);
config.fingerprint = fingerprint;
} else if (strcmp(word, "OPPORTUNISTIC_TLS") == 0 && data == NULL)
config.features |= TLS_OPP;
else if (strcmp(word, "SECURETRANSFER") == 0 && data == NULL)
config.features |= SECURETRANSFER;
else if (strcmp(word, "DEFER") == 0 && data == NULL)
config.features |= DEFER;
else if (strcmp(word, "INSECURE") == 0 && data == NULL)
config.features |= INSECURE;
else if (strcmp(word, "FULLBOUNCE") == 0 && data == NULL)
config.features |= FULLBOUNCE;
else if (strcmp(word, "NULLCLIENT") == 0 && data == NULL)
config.features |= NULLCLIENT;
else if (strcmp(word, "VERIFYCERT") == 0 && data == NULL)
config.features |= VERIFYCERT;
else {
errlogx(EX_CONFIG, "syntax error in %s:%d", config_path, lineno);
/* NOTREACHED */
}
}
if ((config.features & NULLCLIENT) && config.smarthost == NULL) {
errlogx(EX_CONFIG, "%s: NULLCLIENT requires SMARTHOST", config_path);
/* NOTREACHED */
}
fclose(conf);
}