-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathextsmail.c
145 lines (107 loc) · 4.24 KB
/
extsmail.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
// Copyright (C)2008 Laurence Tratt http://tratt.net/laurie/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "Config.h"
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <unistd.h>
#include "conf.h"
#include "common.h"
extern char* __progname;
int main(int argc, char** argv)
{
#ifdef HAVE_PLEDGE
if (pledge("stdio rpath wpath cpath flock getpw", NULL) == -1)
err(EXIT_FAILURE, "pledge");
#endif
Conf *conf = read_conf(NULL);
// Check that everything to do with the spool dir is OK.
if (!check_spool_dir(conf))
exit(1);
// Create the spool file.
char *sp; // spool path
if (asprintf(&sp, "%s%s%s%sXXXXXXXXXX", conf->spool_dir, DIR_SEP, MSGS_DIR,
DIR_SEP) == -1) {
errx(1, "main: asprintf: unable to allocate memory");
}
umask(S_IRWXG | S_IRWXO | S_IXUSR);
int sfd;
if ((sfd = mkstemp(sp)) == -1)
err(1, "mkstemp: when creating spool file %s", sp);
// We immediately try to gain an exclusive lock on the newly created spool
// file. If, in between the spool file being created, and us gaining the
// lock extsmaild gains a lock it will notice that the file is currently
// 0 bytes long, and that therefore the file is incomplete. extsmaild will
// then relinquish its lock, allowing us to gain it and write the file in
// full.
if (flock(sfd, LOCK_EX) == -1)
err(1, "flock: when locking spool file %s", sp);
// Open the spool file for writing. The format of the spool file is:
//
// 1) The file format version number (of the format "v1" etc.)
// 2) Newline
// 3) The number of command line arguments
// 4) Newline
// 5) Each command line argument, with the format "<len(arg)>\n<arg>\n".
// 6) The mail contents as read from stdin
FILE *sf;
if ((sf = fdopen(sfd, "w")) == NULL)
err(1, "main: fdopen");
# define SPOOL_WRITE(fmt, ...) if (fprintf(sf, fmt, __VA_ARGS__) == -1) \
err(1, "%s: When writing to spool file", sp)
// Write out the file format version number
SPOOL_WRITE("%s\n", VERSION1_ID);
// Write out all the command-line args
SPOOL_WRITE("%d\n", argc - 1);
for (int i = 1; i < argc; i += 1)
SPOOL_WRITE("%zd\n%s\n", strlen(argv[i]), argv[i]);
# define BUF_SIZE 1024
char buf[BUF_SIZE];
size_t total_nr = 0; // All bytes read from stdin.
while (1) {
size_t nr; // Number of bytes read
if ((nr = fread(buf, 1, BUF_SIZE, stdin)) < BUF_SIZE
&& ferror(stdin)) {
errx(1, "main: ferror");
}
if (fwrite(buf, 1, nr, sf) < nr)
errx(1, "main: fwrite: when writing to spool file");
total_nr += nr;
if (feof(stdin) != 0)
break;
}
fflush(sf);
fclose(sf);
// If we didn't read any bytes in from stdin, then we remove the spool file
// since there's no message to send. This is designed to prevent
// annoying-ness when extsmail is incorrectly called and ctrl-D pressed
// immediately.
if (total_nr == 0)
unlink(sp);
flock(sfd, LOCK_UN);
close(sfd);
return 0;
}