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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
/* $OpenBSD: util.c,v 1.30 2009/12/13 22:02:55 jacekm Exp $ */
/*
* Copyright (c) 2000,2001 Markus Friedl. All rights reserved.
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
* Copyright (c) 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/param.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <err.h>
#include <ctype.h>
#include <errno.h>
#include <event.h>
#include <libgen.h>
#include <netdb.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "smtpd.h"
int
bsnprintf(char *str, size_t size, const char *format, ...)
{
int ret;
va_list ap;
va_start(ap, format);
ret = vsnprintf(str, size, format, ap);
va_end(ap);
if (ret == -1 || ret >= (int)size)
return 0;
return 1;
}
/* Close file, signifying temporary error condition (if any) to the caller. */
int
safe_fclose(FILE *fp)
{
if (ferror(fp)) {
fclose(fp);
return 0;
}
if (fflush(fp)) {
fclose(fp);
if (errno == ENOSPC)
return 0;
fatal("safe_fclose: fflush");
}
if (fsync(fileno(fp)))
fatal("safe_fclose: fsync");
if (fclose(fp))
fatal("safe_fclose: fclose");
return 1;
}
int
hostname_match(char *hostname, char *pattern)
{
while (*pattern != '\0' && *hostname != '\0') {
if (*pattern == '*') {
while (*pattern == '*')
pattern++;
while (*hostname != '\0' &&
tolower((int)*hostname) != tolower((int)*pattern))
hostname++;
continue;
}
if (tolower((int)*pattern) != tolower((int)*hostname))
return 0;
pattern++;
hostname++;
}
return (*hostname == '\0' && *pattern == '\0');
}
int
recipient_to_path(struct path *path, char *recipient)
{
char *username;
char *hostname;
username = recipient;
hostname = strrchr(username, '@');
if (username[0] == '\0') {
*path->user = '\0';
*path->domain = '\0';
return 1;
}
if (hostname == NULL) {
if (strcasecmp(username, "postmaster") != 0)
return 0;
hostname = "localhost";
} else {
*hostname++ = '\0';
}
if (strlcpy(path->user, username, sizeof(path->user))
>= sizeof(path->user))
return 0;
if (strlcpy(path->domain, hostname, sizeof(path->domain))
>= sizeof(path->domain))
return 0;
return 1;
}
int
valid_localpart(char *s)
{
#define IS_ATEXT(c) (isalnum((int)(c)) || strchr("!#$%&'*+-/=?^_`{|}~", (c)))
nextatom:
if (! IS_ATEXT(*s) || *s == '\0')
return 0;
while (*(++s) != '\0') {
if (*s == '.')
break;
if (IS_ATEXT(*s))
continue;
return 0;
}
if (*s == '.') {
s++;
goto nextatom;
}
return 1;
}
int
valid_domainpart(char *s)
{
nextsub:
if (!isalnum((int)*s))
return 0;
while (*(++s) != '\0') {
if (*s == '.')
break;
if (isalnum((int)*s) || *s == '-')
continue;
return 0;
}
if (s[-1] == '-')
return 0;
if (*s == '.') {
s++;
goto nextsub;
}
return 1;
}
char *
ss_to_text(struct sockaddr_storage *ss)
{
static char buf[NI_MAXHOST + 5];
char *p;
buf[0] = '\0';
p = buf;
if (ss->ss_family == PF_INET6) {
strlcpy(buf, "IPv6:", sizeof(buf));
p = buf + 5;
}
if (getnameinfo((struct sockaddr *)ss, ss->ss_len, p,
NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
fatalx("ss_to_text: getnameinfo");
return (buf);
}
int
valid_message_id(char *mid)
{
u_int8_t cnt;
/* [0-9]{10}\.[a-zA-Z0-9]{16} */
for (cnt = 0; cnt < 10; ++cnt, ++mid)
if (! isdigit((int)*mid))
return 0;
if (*mid++ != '.')
return 0;
for (cnt = 0; cnt < 16; ++cnt, ++mid)
if (! isalnum((int)*mid))
return 0;
return (*mid == '\0');
}
int
valid_message_uid(char *muid)
{
u_int8_t cnt;
/* [0-9]{10}\.[a-zA-Z0-9]{16}\.[0-9]{0,} */
for (cnt = 0; cnt < 10; ++cnt, ++muid)
if (! isdigit((int)*muid))
return 0;
if (*muid++ != '.')
return 0;
for (cnt = 0; cnt < 16; ++cnt, ++muid)
if (! isalnum((int)*muid))
return 0;
if (*muid++ != '.')
return 0;
for (cnt = 0; *muid != '\0'; ++cnt, ++muid)
if (! isdigit((int)*muid))
return 0;
return (cnt != 0);
}
char *
time_to_text(time_t when)
{
struct tm *lt;
static char buf[40];
char *day[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char *month[] = {"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"};
lt = localtime(&when);
if (lt == NULL || when == 0)
fatalx("time_to_text: localtime");
/* We do not use strftime because it is subject to locale substitution*/
if (! bsnprintf(buf, sizeof(buf), "%s, %d %s %d %02d:%02d:%02d %c%02d%02d (%s)",
day[lt->tm_wday], lt->tm_mday, month[lt->tm_mon],
lt->tm_year + 1900,
lt->tm_hour, lt->tm_min, lt->tm_sec,
lt->tm_gmtoff >= 0 ? '+' : '-',
abs((int)lt->tm_gmtoff / 3600),
abs((int)lt->tm_gmtoff % 3600) / 60,
lt->tm_zone))
fatalx("time_to_text: bsnprintf");
return buf;
}
/*
* Check file for security. Based on usr.bin/ssh/auth.c.
*/
int
secure_file(int fd, char *path, struct passwd *pw, int mayread)
{
char buf[MAXPATHLEN];
char homedir[MAXPATHLEN];
struct stat st;
char *cp;
if (realpath(path, buf) == NULL)
return 0;
if (realpath(pw->pw_dir, homedir) == NULL)
homedir[0] = '\0';
/* Check the open file to avoid races. */
if (fstat(fd, &st) < 0 ||
!S_ISREG(st.st_mode) ||
(st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
(st.st_mode & (mayread ? 022 : 066)) != 0)
return 0;
/* For each component of the canonical path, walking upwards. */
for (;;) {
if ((cp = dirname(buf)) == NULL)
return 0;
strlcpy(buf, cp, sizeof(buf));
if (stat(buf, &st) < 0 ||
(st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
(st.st_mode & 022) != 0)
return 0;
/* We can stop checking after reaching homedir level. */
if (strcmp(homedir, buf) == 0)
break;
/*
* dirname should always complete with a "/" path,
* but we can be paranoid and check for "." too
*/
if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
break;
}
return 1;
}
void
addargs(arglist *args, char *fmt, ...)
{
va_list ap;
char *cp;
u_int nalloc;
int r;
va_start(ap, fmt);
r = vasprintf(&cp, fmt, ap);
va_end(ap);
if (r == -1)
fatal("addargs: argument too long");
nalloc = args->nalloc;
if (args->list == NULL) {
nalloc = 32;
args->num = 0;
} else if (args->num+2 >= nalloc)
nalloc *= 2;
if (SIZE_T_MAX / nalloc < sizeof(char *))
fatalx("addargs: nalloc * size > SIZE_T_MAX");
args->list = realloc(args->list, nalloc * sizeof(char *));
if (args->list == NULL)
fatal("addargs: realloc");
args->nalloc = nalloc;
args->list[args->num++] = cp;
args->list[args->num] = NULL;
}
void
lowercase(char *buf, char *s, size_t len)
{
if (len == 0)
fatalx("lowercase: len == 0");
if (strlcpy(buf, s, len) >= len)
fatalx("lowercase: truncation");
while (*buf != '\0') {
*buf = tolower((int)*buf);
buf++;
}
}
void
message_set_errormsg(struct message *messagep, char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vsnprintf(messagep->session_errorline, MAX_LINE_SIZE, fmt, ap);
if (ret >= MAX_LINE_SIZE)
strlcpy(messagep->session_errorline + (MAX_LINE_SIZE - 4), "...", 4);
/* this should not happen */
if (ret == -1)
err(1, "vsnprintf");
va_end(ap);
}
char *
message_get_errormsg(struct message *messagep)
{
return messagep->session_errorline;
}
void
sa_set_port(struct sockaddr *sa, int port)
{
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
struct addrinfo hints, *res;
int error;
error = getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST);
if (error)
fatalx("sa_set_port: getnameinfo failed");
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICHOST|AI_NUMERICSERV;
snprintf(sbuf, sizeof(sbuf), "%d", port);
error = getaddrinfo(hbuf, sbuf, &hints, &res);
if (error)
fatalx("sa_set_port: getaddrinfo failed");
memcpy(sa, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
}
struct path *
path_dup(struct path *path)
{
struct path *pathp;
pathp = calloc(sizeof(struct path), 1);
if (pathp == NULL)
fatal("calloc");
*pathp = *path;
return pathp;
}
u_int64_t
generate_uid(void)
{
u_int64_t id;
struct timeval tp;
if (gettimeofday(&tp, NULL) == -1)
fatal("generate_uid: time");
id = (u_int32_t)tp.tv_sec;
id <<= 32;
id |= (u_int32_t)tp.tv_usec;
usleep(1);
return (id);
}
void
fdlimit(int n)
{
struct rlimit rl;
if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
fatal("fdlimit: getrlimit");
if ((u_int)n > rl.rlim_max) {
log_warnx("fdlimit: need %d, limit %llu", n, rl.rlim_max);
fatalx("fdlimit: hard limit reached");
}
rl.rlim_cur = n;
if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
fatal("fdlimit: getrlimit");
}
int
availdesc(void)
{
int avail;
avail = getdtablesize();
avail -= 3; /* stdin, stdout, stderr */
avail -= PROC_COUNT; /* imsg channels */
avail -= 5; /* safety buffer */
return (avail);
}
|