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
|
/*
* Copyright (c) 2017 Gilles Chehade <gilles@poolp.org>
*
* 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/socket.h>
#include <sys/un.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
enum phase {
PHASE_BANNER,
PHASE_HELO,
PHASE_MAILFROM,
PHASE_RCPTTO,
PHASE_DATA,
PHASE_EOM,
PHASE_QUIT
};
struct session {
const char *lhlo;
const char *mailfrom;
char **rcpts;
int n_rcpts;
};
static FILE *lmtp_connect(const char *);
static void lmtp_engine(FILE *, struct session *);
static void stream_file(FILE *);
int
main(int argc, char *argv[])
{
int ch;
FILE *conn;
const char *destination = "inet:localhost";
struct session session;
if (! geteuid())
errx(1, "mail.lmtp: may not be executed as root");
session.lhlo = "localhost";
session.mailfrom = NULL;
while ((ch = getopt(argc, argv, "d:l:f:")) != -1) {
switch (ch) {
case 'd':
destination = optarg;
break;
case 'l':
session.lhlo = optarg;
break;
case 'f':
session.mailfrom = optarg;
break;
default:
break;
}
}
argc -= optind;
argv += optind;
if (session.mailfrom == NULL)
errx(1, "sender must be specified with -f");
if (argc == 0)
errx(1, "no recipient was specified");
session.rcpts = argv;
session.n_rcpts = argc;
conn = lmtp_connect(destination);
lmtp_engine(conn, &session);
return (0);
}
static FILE *
lmtp_connect_inet(const char *destination)
{
struct addrinfo hints, *res, *res0;
char *destcopy = NULL;
const char *hostname = NULL;
const char *servname = NULL;
const char *cause = NULL;
char *p;
int n, s = -1, save_errno;
if ((destcopy = strdup(destination)) == NULL)
err(1, NULL);
servname = "25";
hostname = destcopy;
p = destcopy;
if (*p == '[') {
if ((p = strchr(destcopy, ']')) == NULL)
errx(1, "inet: invalid address syntax");
/* remove [ and ] */
*p = '\0';
hostname++;
if (strncasecmp(hostname, "IPv6:", 5) == 0)
hostname += 5;
/* extract port if any */
switch (*(p+1)) {
case ':':
servname = p+2;
break;
case '\0':
break;
default:
errx(1, "inet: invalid address syntax");
}
}
else if ((p = strchr(destcopy, ':')) != NULL) {
*p++ = '\0';
servname = p;
}
memset(&hints, 0, sizeof hints);
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICSERV;
n = getaddrinfo(hostname, servname, &hints, &res0);
if (n)
errx(1, "inet: %s", gai_strerror(n));
for (res = res0; res; res = res->ai_next) {
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s == -1) {
cause = "socket";
continue;
}
if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
cause = "connect";
save_errno = errno;
close(s);
errno = save_errno;
s = -1;
continue;
}
break;
}
freeaddrinfo(res0);
if (s == -1)
errx(1, "%s", cause);
return fdopen(s, "r+");
}
static FILE *
lmtp_connect_unix(const char *destination)
{
struct sockaddr_un addr;
int s;
if (*destination != '/')
errx(1, "unix: path must be absolute");
if ((s = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1)
err(1, NULL);
memset(&addr, 0, sizeof addr);
addr.sun_family = AF_UNIX;
if (strlcpy(addr.sun_path, destination, sizeof addr.sun_path)
>= sizeof addr.sun_path)
errx(1, "unix: socket path is too long");
if (connect(s, (struct sockaddr *)&addr, sizeof addr) == -1)
err(1, "connect");
return fdopen(s, "r+");
}
static FILE *
lmtp_connect(const char *destination)
{
if (strncasecmp(destination, "unix:", 5) == 0)
return lmtp_connect_unix(destination + 5);
if (strncasecmp(destination, "inet:", 5) == 0)
return lmtp_connect_inet(destination + 5);
errx(1, "invalid destination address, must start with 'unix:' or 'inet:'");
}
static void
lmtp_engine(FILE *conn, struct session *session)
{
char *line = NULL;
size_t linesize = 0;
ssize_t linelen;
enum phase phase = PHASE_BANNER;
do {
if ((linelen = getline(&line, &linesize, conn)) == -1)
err(1, "getline");
line[strcspn(line, "\n")] = '\0';
if (linelen < 4 ||
!isdigit(line[0]) ||
!isdigit(line[1]) ||
!isdigit(line[2]) ||
(line[3] != ' ' && line[3] != '-'))
errx(1, "LMTP server sent an invalid line");
if (line[0] != (phase == PHASE_DATA ? '3' : '2'))
errx(1, "LMTP server error: %s", line);
if (line[3] == '-')
continue;
switch (phase) {
case PHASE_BANNER:
fprintf(conn, "LHLO %s\r\n", session->lhlo);
phase++;
break;
case PHASE_HELO:
fprintf(conn, "MAIL FROM:<%s>\r\n", session->mailfrom);
phase++;
break;
case PHASE_MAILFROM:
fprintf(conn, "RCPT TO:<%s>\r\n", session->rcpts[session->n_rcpts - 1]);
if (session->n_rcpts - 1 == 0) {
phase++;
break;
}
session->n_rcpts--;
break;
case PHASE_RCPTTO:
fprintf(conn, "DATA\r\n");
phase++;
break;
case PHASE_DATA:
stream_file(conn);
fprintf(conn, ".\r\n");
phase++;
break;
case PHASE_EOM:
fprintf(conn, "QUIT\r\n");
phase++;
break;
case PHASE_QUIT:
exit(0);
}
if (ferror(stdin))
err(1, "getline");
} while (1);
}
static void
stream_file(FILE *conn)
{
char *line = NULL;
size_t linesize = 0;
ssize_t linelen;
while ((linelen = getline(&line, &linesize, stdin)) != -1) {
line[strcspn(line, "\n")] = '\0';
if (strcmp(line, ".") == 0)
fprintf(conn, ".");
fprintf(conn, "%s\r\n", line);
}
free(line);
if (ferror(stdin))
err(1, "getline");
}
|