GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/smtpd/smtpd/../delivery_filename.c Lines: 0 41 0.0 %
Date: 2017-11-13 Branches: 0 50 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: delivery_filename.c,v 1.14 2015/12/28 19:47:57 millert Exp $	*/
2
3
/*
4
 * Copyright (c) 2011 Gilles Chehade <gilles@poolp.org>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <sys/types.h>
20
#include <sys/queue.h>
21
#include <sys/tree.h>
22
#include <sys/socket.h>
23
#include <sys/stat.h>
24
25
#include <ctype.h>
26
#include <err.h>
27
#include <errno.h>
28
#include <event.h>
29
#include <fcntl.h>
30
#include <imsg.h>
31
#include <paths.h>
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <time.h>
36
#include <unistd.h>
37
#include <limits.h>
38
39
#include "smtpd.h"
40
#include "log.h"
41
42
extern char	**environ;
43
44
/* filename backend */
45
static void delivery_filename_open(struct deliver *);
46
47
struct delivery_backend delivery_backend_filename = {
48
	0, delivery_filename_open
49
};
50
51
52
static void
53
delivery_filename_open(struct deliver *deliver)
54
{
55
	struct stat	 sb;
56
	size_t		 sz = 0;
57
	ssize_t		 len;
58
	int		 fd;
59
	FILE		*fp;
60
	char		*ln = NULL;
61
	char		*msg;
62
	int		 n;
63
	int		 escape_from;
64
65
#define error(m)	{ msg = m; goto err; }
66
#define error2(m)	{ msg = m; goto err2; }
67
68
	setproctitle("file delivery");
69
	fd = open(deliver->to, O_CREAT | O_APPEND | O_WRONLY, 0600);
70
	if (fd < 0)
71
		error("open");
72
	if (fstat(fd, &sb) < 0)
73
		error("fstat");
74
	if (S_ISREG(sb.st_mode) && flock(fd, LOCK_EX) < 0)
75
		error("flock");
76
	fp = fdopen(fd, "a");
77
	if (fp == NULL)
78
		error("fdopen");
79
80
	escape_from = 0;
81
	while ((len = getline(&ln, &sz, stdin)) != -1) {
82
		if (ln[len - 1] == '\n')
83
			ln[len - 1] = '\0';
84
		if (strncmp(ln, "From ", 5) == 0) {
85
			if (escape_from == 0)
86
				escape_from = 1;
87
			else
88
				putc('>', fp);
89
		}
90
		fputs(ln, fp);
91
		putc('\n', fp);
92
		if (ferror(fp))
93
			break;
94
	}
95
	free(ln);
96
	if (ferror(stdin))
97
		error2("read error");
98
	putc('\n', fp);
99
	if (fflush(fp) == EOF || ferror(fp))
100
		error2("write error");
101
	if (fsync(fd) == -1) {
102
		if (errno != EINVAL)
103
			error2("fsync");
104
	}
105
	if (fclose(fp) == EOF)
106
		error2("fclose");
107
	_exit(0);
108
109
err2:
110
	n = errno;
111
	ftruncate(fd, sb.st_size);
112
	errno = n;
113
114
err:
115
	perror(msg);
116
	_exit(1);
117
}