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
|
/* $OpenBSD: mail.local.c,v 1.36 2019/06/28 13:32:53 deraadt Exp $ */
/*-
* Copyright (c) 1996-1998 Theo de Raadt <deraadt@theos.com>
* Copyright (c) 1996-1998 David Mazieres <dm@lcs.mit.edu>
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* 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 University 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 REGENTS 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 REGENTS 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 <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <syslog.h>
#include <fcntl.h>
#include <netdb.h>
#include <pwd.h>
#include <time.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pathnames.h"
#include "mail.local.h"
int
main(int argc, char *argv[])
{
struct passwd *pw;
int ch, fd, eval, lockfile=1, holdme=0;
uid_t uid;
char *from;
openlog("mail.local", LOG_PERROR, LOG_MAIL);
from = NULL;
while ((ch = getopt(argc, argv, "lLdf:r:H")) != -1)
switch (ch) {
case 'd': /* backward compatible */
break;
case 'f':
case 'r': /* backward compatible */
if (from)
merr(FATAL, "multiple -f options");
from = optarg;
break;
case 'l':
lockfile=1;
break;
case 'L':
lockfile=0;
break;
case 'H':
holdme=1;
break;
default:
usage();
}
argc -= optind;
argv += optind;
/* Support -H flag for backwards compat */
if (holdme) {
execl(_PATH_LOCKSPOOL, "lockspool", (char *)NULL);
merr(FATAL, "execl: lockspool: %s", strerror(errno));
} else {
if (!*argv)
usage();
if (geteuid() != 0)
merr(FATAL, "may only be run by the superuser");
}
/*
* If from not specified, use the name from getlogin() if the
* uid matches, otherwise, use the name from the password file
* corresponding to the uid.
*/
uid = getuid();
if (!from && (!(from = getlogin()) ||
!(pw = getpwnam(from)) || pw->pw_uid != uid))
from = (pw = getpwuid(uid)) ? pw->pw_name : "???";
fd = storemail(from);
for (eval = 0; *argv; ++argv)
eval |= deliver(fd, *argv, lockfile);
exit(eval);
}
int
storemail(char *from)
{
FILE *fp = NULL;
time_t tval;
int fd, eline;
size_t len;
char *line, *tbuf;
if ((tbuf = strdup(_PATH_LOCTMP)) == NULL)
merr(FATAL, "unable to allocate memory");
if ((fd = mkstemp(tbuf)) == -1 || !(fp = fdopen(fd, "w+")))
merr(FATAL, "unable to open temporary file");
(void)unlink(tbuf);
free(tbuf);
(void)time(&tval);
(void)fprintf(fp, "From %s %s", from, ctime(&tval));
for (eline = 1, tbuf = NULL; (line = fgetln(stdin, &len));) {
/* We have to NUL-terminate the line since fgetln does not */
if (line[len - 1] == '\n')
line[len - 1] = '\0';
else {
/* No trailing newline, so alloc space and copy */
if ((tbuf = malloc(len + 1)) == NULL)
merr(FATAL, "unable to allocate memory");
memcpy(tbuf, line, len);
tbuf[len] = '\0';
line = tbuf;
}
if (line[0] == '\0')
eline = 1;
else {
if (eline && line[0] == 'F' && len > 5 &&
!memcmp(line, "From ", 5))
(void)putc('>', fp);
eline = 0;
}
(void)fprintf(fp, "%s\n", line);
if (ferror(fp))
break;
}
free(tbuf);
/* Output a newline; note, empty messages are allowed. */
(void)putc('\n', fp);
(void)fflush(fp);
if (ferror(fp))
merr(FATAL, "temporary file write error");
return(fd);
}
int
deliver(int fd, char *name, int lockfile)
{
struct stat sb, fsb;
struct passwd *pw;
int mbfd=-1, rval=1, lfd=-1;
char biffmsg[100], buf[8*1024], path[PATH_MAX];
off_t curoff;
size_t off;
ssize_t nr, nw;
/*
* Disallow delivery to unknown names -- special mailboxes can be
* handled in the sendmail aliases file.
*/
if (!(pw = getpwnam(name))) {
merr(NOTFATAL, "unknown name: %s", name);
return(1);
}
(void)snprintf(path, sizeof path, "%s/%s", _PATH_MAILDIR, name);
if (lockfile) {
lfd = getlock(name, pw);
if (lfd == -1)
return (1);
}
/* after this point, always exit via bad to remove lockfile */
retry:
if (lstat(path, &sb)) {
if (errno != ENOENT) {
merr(NOTFATAL, "%s: %s", path, strerror(errno));
goto bad;
}
if ((mbfd = open(path, O_APPEND|O_CREAT|O_EXCL|O_WRONLY|O_EXLOCK,
S_IRUSR|S_IWUSR)) == -1) {
if (errno == EEXIST) {
/* file appeared since lstat */
goto retry;
} else {
merr(NOTFATAL, "%s: %s", path, strerror(errno));
goto bad;
}
}
/*
* Set the owner and group. Historically, binmail repeated
* this at each mail delivery. We no longer do this, assuming
* that if the ownership or permissions were changed there
* was a reason for doing so.
*/
if (fchown(mbfd, pw->pw_uid, pw->pw_gid) == -1) {
merr(NOTFATAL, "chown %u:%u: %s",
pw->pw_uid, pw->pw_gid, name);
goto bad;
}
} else {
if (sb.st_nlink != 1 || !S_ISREG(sb.st_mode)) {
merr(NOTFATAL, "%s: linked or special file", path);
goto bad;
}
if ((mbfd = open(path, O_APPEND|O_WRONLY|O_EXLOCK,
S_IRUSR|S_IWUSR)) == -1) {
merr(NOTFATAL, "%s: %s", path, strerror(errno));
goto bad;
}
if (fstat(mbfd, &fsb) == -1) {
/* relating error to path may be bad style */
merr(NOTFATAL, "%s: %s", path, strerror(errno));
goto bad;
}
if (sb.st_dev != fsb.st_dev || sb.st_ino != fsb.st_ino) {
merr(NOTFATAL, "%s: changed after open", path);
goto bad;
}
/* paranoia? */
if (fsb.st_nlink != 1 || !S_ISREG(fsb.st_mode)) {
merr(NOTFATAL, "%s: linked or special file", path);
goto bad;
}
}
curoff = lseek(mbfd, 0, SEEK_END);
(void)snprintf(biffmsg, sizeof biffmsg, "%s@%lld\n", name, curoff);
if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
merr(NOTFATAL, "temporary file: %s", strerror(errno));
goto bad;
}
while ((nr = read(fd, buf, sizeof(buf))) > 0)
for (off = 0; off < nr; off += nw)
if ((nw = write(mbfd, buf + off, nr - off)) == -1) {
merr(NOTFATAL, "%s: %s", path, strerror(errno));
(void)ftruncate(mbfd, curoff);
goto bad;
}
if (nr == 0) {
rval = 0;
} else {
(void)ftruncate(mbfd, curoff);
merr(FATAL, "temporary file: %s", strerror(errno));
}
bad:
if (lfd != -1) {
rellock();
close(lfd);
}
if (mbfd != -1) {
(void)fsync(mbfd); /* Don't wait for update. */
(void)close(mbfd); /* Implicit unlock. */
}
if (!rval)
notifybiff(biffmsg);
return(rval);
}
void
notifybiff(char *msg)
{
static struct addrinfo *res0;
struct addrinfo hints, *res;
static int f = -1;
size_t len;
int error;
if (res0 == NULL) {
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
error = getaddrinfo("localhost", "biff", &hints, &res0);
if (error) {
/* Be silent if biff service not available. */
if (error != EAI_SERVICE) {
merr(NOTFATAL, "localhost: %s",
gai_strerror(error));
}
return;
}
}
if (f == -1) {
for (res = res0; res != NULL; res = res->ai_next) {
f = socket(res->ai_family, res->ai_socktype,
res->ai_protocol);
if (f != -1)
break;
}
}
if (f == -1) {
merr(NOTFATAL, "socket: %s", strerror(errno));
return;
}
len = strlen(msg) + 1; /* XXX */
if (sendto(f, msg, len, 0, res->ai_addr, res->ai_addrlen) != len)
merr(NOTFATAL, "sendto biff: %s", strerror(errno));
}
void
usage(void)
{
merr(FATAL, "usage: mail.local [-Ll] [-f from] user ...");
}
|