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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/* $OpenBSD: makemap.c,v 1.24 2009/11/08 23:08:56 gilles Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
* Copyright (c) 2008-2009 Jacek Masiulaniec <jacekm@dobremiasto.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/tree.h>
#include <sys/queue.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <ctype.h>
#include <db.h>
#include <err.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
#include "smtpd.h"
#define PATH_ALIASES "/etc/mail/aliases"
extern char *__progname;
__dead void usage(void);
int parse_map(char *);
int parse_entry(char *, size_t, size_t);
int parse_mapentry(char *, size_t, size_t);
int parse_setentry(char *, size_t, size_t);
int make_plain(DBT *, char *);
int make_aliases(DBT *, char *);
char *conf_aliases(char *);
DB *db;
char *source;
char *oflag;
int dbputs;
enum program {
P_MAKEMAP,
P_NEWALIASES
} mode;
enum output_type {
T_PLAIN,
T_ALIASES,
T_SET
} type;
/*
* Stub functions so that makemap compiles using minimum object files.
*/
void
purge_config(struct smtpd *env, u_int8_t what)
{
bzero(env, sizeof(struct smtpd));
}
int
ssl_load_certfile(struct smtpd *env, const char *name, u_int8_t flags)
{
return (0);
}
int
main(int argc, char *argv[])
{
struct stat sb;
char dbname[MAXPATHLEN];
char *opts;
char *conf;
int ch;
log_init(1);
mode = strcmp(__progname, "newaliases") ? P_MAKEMAP : P_NEWALIASES;
conf = CONF_FILE;
type = T_PLAIN;
opts = "ho:t:";
if (mode == P_NEWALIASES)
opts = "f:h";
while ((ch = getopt(argc, argv, opts)) != -1) {
switch (ch) {
case 'f':
conf = optarg;
break;
case 'o':
oflag = optarg;
break;
case 't':
if (strcmp(optarg, "aliases") == 0)
type = T_ALIASES;
else if (strcmp(optarg, "set") == 0)
type = T_SET;
else
errx(1, "unsupported type '%s'", optarg);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (mode == P_NEWALIASES) {
if (geteuid())
errx(1, "need root privileges");
if (argc != 0)
usage();
type = T_ALIASES;
source = conf_aliases(conf);
} else {
if (argc != 1)
usage();
source = argv[0];
}
if (oflag == NULL && asprintf(&oflag, "%s.db", source) == -1)
err(1, "asprintf");
if (stat(source, &sb) == -1)
err(1, "stat: %s", source);
if (! bsnprintf(dbname, sizeof(dbname), "%s.XXXXXXXXXXX", oflag))
errx(1, "path too long");
if (mkstemp(dbname) == -1)
err(1, "mkstemp");
db = dbopen(dbname, O_EXLOCK|O_RDWR|O_SYNC, 0644, DB_HASH, NULL);
if (db == NULL) {
warn("dbopen: %s", dbname);
goto bad;
}
if (fchmod(db->fd(db), sb.st_mode) == -1 ||
fchown(db->fd(db), sb.st_uid, sb.st_gid) == -1) {
warn("couldn't carry ownership and perms to %s", dbname);
goto bad;
}
if (! parse_map(source))
goto bad;
if (db->close(db) == -1) {
warn("dbclose: %s", dbname);
goto bad;
}
if (rename(dbname, oflag) == -1) {
warn("rename");
goto bad;
}
if (mode == P_NEWALIASES)
printf("%s: %d aliases\n", source, dbputs);
else if (dbputs == 0)
warnx("warning: empty map created: %s", oflag);
return 0;
bad:
unlink(dbname);
return 1;
}
int
parse_map(char *filename)
{
FILE *fp;
char *line;
size_t len;
size_t lineno = 0;
char delim[] = { '\\', 0, 0 };
fp = fopen(filename, "r");
if (fp == NULL) {
warn("%s", filename);
return 0;
}
if (flock(fileno(fp), LOCK_SH|LOCK_NB) == -1) {
if (errno == EWOULDBLOCK)
warnx("%s is locked", filename);
else
warn("%s: flock", filename);
fclose(fp);
return 0;
}
while ((line = fparseln(fp, &len, &lineno, delim, 0)) != NULL) {
if (! parse_entry(line, len, lineno)) {
free(line);
fclose(fp);
return 0;
}
free(line);
}
fclose(fp);
return 1;
}
int
parse_entry(char *line, size_t len, size_t lineno)
{
switch (type) {
case T_PLAIN:
case T_ALIASES:
return parse_mapentry(line, len, lineno);
case T_SET:
return parse_setentry(line, len, lineno);
}
return 0;
}
int
parse_mapentry(char *line, size_t len, size_t lineno)
{
DBT key;
DBT val;
DBT domkey;
DBT domval;
char *keyp;
char *valp;
char *domp;
keyp = line;
while (isspace((int)*keyp))
keyp++;
if (*keyp == '\0' || *keyp == '#')
return 1;
valp = keyp;
strsep(&valp, " \t:");
if (valp == NULL || valp == keyp)
goto bad;
/* Check for dups. */
key.data = keyp;
key.size = strlen(keyp) + 1;
if (db->get(db, &key, &val, 0) == 0) {
warnx("%s:%zd: duplicate entry for %s", source, lineno, keyp);
return 0;
}
if (type == T_PLAIN) {
if (! make_plain(&val, valp))
goto bad;
}
else if (type == T_ALIASES) {
lowercase(key.data, key.data, strlen(key.data) + 1);
if (! make_aliases(&val, valp))
goto bad;
}
if (db->put(db, &key, &val, 0) == -1) {
warn("dbput");
return 0;
}
/* add key for domain */
if ((domp = strrchr(key.data, '@')) != NULL) {
domkey.data = domp + 1;
domkey.size = strlen(domkey.data) + 1;
domval.data = "<empty>";
domval.size = strlen(domval.data) + 1;
if (db->put(db, &domkey, &domval, 0) == -1) {
warn("dbput");
return 0;
}
}
dbputs++;
free(val.data);
return 1;
bad:
warnx("%s:%zd: invalid entry", source, lineno);
return 0;
}
int
parse_setentry(char *line, size_t len, size_t lineno)
{
DBT key;
DBT val;
char *keyp;
keyp = line;
while (isspace((int)*keyp))
keyp++;
if (*keyp == '\0' || *keyp == '#')
return 1;
val.data = "<set>";
val.size = strlen(val.data) + 1;
/* Check for dups. */
key.data = keyp;
key.size = strlen(keyp) + 1;
if (db->get(db, &key, &val, 0) == 0) {
warnx("%s:%zd: duplicate entry for %s", source, lineno, keyp);
return 0;
}
if (db->put(db, &key, &val, 0) == -1) {
warn("dbput");
return 0;
}
dbputs++;
return 1;
}
int
make_plain(DBT *val, char *text)
{
val->data = strdup(text);
if (val->data == NULL)
err(1, "malloc");
val->size = strlen(text) + 1;
return (val->size);
}
int
make_aliases(DBT *val, char *text)
{
struct alias a;
char *subrcpt;
char *endp;
val->data = NULL;
val->size = 0;
while ((subrcpt = strsep(&text, ",")) != NULL) {
/* subrcpt: strip initial whitespace. */
while (isspace((int)*subrcpt))
++subrcpt;
if (*subrcpt == '\0')
goto error;
/* subrcpt: strip trailing whitespace. */
endp = subrcpt + strlen(subrcpt) - 1;
while (subrcpt < endp && isspace((int)*endp))
*endp-- = '\0';
if (! alias_parse(&a, subrcpt))
goto error;
val->data = realloc(val->data, val->size + sizeof(a));
if (val->data == NULL)
err(1, "get_targets: realloc");
memcpy((u_int8_t *)val->data + val->size, &a, sizeof(a));
val->size += sizeof(a);
}
return (val->size);
error:
free(val->data);
return 0;
}
char *
conf_aliases(char *cfgpath)
{
struct smtpd env;
struct map *map;
char *path;
char *p;
if (parse_config(&env, cfgpath, 0))
exit(1);
map = map_findbyname(&env, "aliases");
if (map == NULL)
return (PATH_ALIASES);
path = strdup(map->m_config);
if (path == NULL)
err(1, NULL);
p = strstr(path, ".db");
if (p == NULL || p[3] != '\0')
errx(1, "%s: %s: no .db suffix present", cfgpath, path);
*p = '\0';
return (path);
}
void
usage(void)
{
if (mode == P_NEWALIASES)
fprintf(stderr, "usage: %s [-f file]\n", __progname);
else
fprintf(stderr, "usage: %s [-o dbfile] [-t type] file\n",
__progname);
exit(1);
}
|