blob: 9455b4e7a284de1b168c520e3627b55f3d83cf4f [file] [log] [blame]
Denis Vlasenko239d06b2008-11-06 23:42:42 +00001/* vi: set sw=4 ts=4: */
2/*
3 * bare bones sendmail
4 *
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko239d06b2008-11-06 23:42:42 +00008 */
Denys Vlasenko5707b522010-12-20 05:12:39 +01009
Denys Vlasenkod616ab62011-05-22 03:46:33 +020010//kbuild:lib-$(CONFIG_SENDMAIL) += sendmail.o mail.o
11
Denys Vlasenko5707b522010-12-20 05:12:39 +010012//usage:#define sendmail_trivial_usage
13//usage: "[OPTIONS] [RECIPIENT_EMAIL]..."
14//usage:#define sendmail_full_usage "\n\n"
15//usage: "Read email from stdin and send it\n"
16//usage: "\nStandard options:"
17//usage: "\n -t Read additional recipients from message body"
Denys Vlasenko07f417b2014-02-05 15:01:39 +010018//usage: "\n -f SENDER For use in MAIL FROM:<sender>. Can be empty string"
19//usage: "\n Default: -auUSER, or username of current UID"
Denys Vlasenko5707b522010-12-20 05:12:39 +010020//usage: "\n -o OPTIONS Various options. -oi implied, others are ignored"
21//usage: "\n -i -oi synonym. implied and ignored"
22//usage: "\n"
23//usage: "\nBusybox specific options:"
24//usage: "\n -v Verbose"
25//usage: "\n -w SECS Network timeout"
26//usage: "\n -H 'PROG ARGS' Run connection helper"
27//usage: "\n Examples:"
28//usage: "\n -H 'exec openssl s_client -quiet -tls1 -starttls smtp"
29//usage: "\n -connect smtp.gmail.com:25' <email.txt"
Denys Vlasenko34c469a2011-09-18 03:01:49 +020030//usage: "\n [4<username_and_passwd.txt | -auUSER -apPASS]"
Denys Vlasenko5707b522010-12-20 05:12:39 +010031//usage: "\n -H 'exec openssl s_client -quiet -tls1"
32//usage: "\n -connect smtp.gmail.com:465' <email.txt"
Denys Vlasenko34c469a2011-09-18 03:01:49 +020033//usage: "\n [4<username_and_passwd.txt | -auUSER -apPASS]"
Denys Vlasenko5707b522010-12-20 05:12:39 +010034//usage: "\n -S HOST[:PORT] Server"
Denys Vlasenko34c469a2011-09-18 03:01:49 +020035//usage: "\n -auUSER Username for AUTH LOGIN"
36//usage: "\n -apPASS Password for AUTH LOGIN"
37////usage: "\n -amMETHOD Authentication method. Ignored. LOGIN is implied"
Denys Vlasenko5707b522010-12-20 05:12:39 +010038//usage: "\n"
39//usage: "\nOther options are silently ignored; -oi -t is implied"
40//usage: IF_MAKEMIME(
Denys Vlasenko34c469a2011-09-18 03:01:49 +020041//usage: "\nUse makemime to create emails with attachments"
Denys Vlasenko5707b522010-12-20 05:12:39 +010042//usage: )
43
Denys Vlasenko07f417b2014-02-05 15:01:39 +010044/* Currently we don't sanitize or escape user-supplied SENDER and RECIPIENT_EMAILs.
45 * We may need to do so. For one, '.' in usernames seems to require escaping!
46 *
47 * From http://cr.yp.to/smtp/address.html:
48 *
49 * SMTP offers three ways to encode a character inside an address:
50 *
51 * "safe": the character, if it is not <>()[].,;:@, backslash,
52 * double-quote, space, or an ASCII control character;
53 * "quoted": the character, if it is not \012, \015, backslash,
54 * or double-quote; or
55 * "slashed": backslash followed by the character.
56 *
57 * An encoded box part is either (1) a sequence of one or more slashed
58 * or safe characters or (2) a double quote, a sequence of zero or more
59 * slashed or quoted characters, and a double quote. It represents
60 * the concatenation of the characters encoded inside it.
61 *
62 * For example, the encoded box parts
63 * angels
64 * \a\n\g\e\l\s
65 * "\a\n\g\e\l\s"
66 * "angels"
67 * "ang\els"
68 * all represent the 6-byte string "angels", and the encoded box parts
69 * a\,comma
70 * \a\,\c\o\m\m\a
71 * "a,comma"
72 * all represent the 7-byte string "a,comma".
73 *
74 * An encoded address contains
75 * the byte <;
76 * optionally, a route followed by a colon;
77 * an encoded box part, the byte @, and a domain; and
78 * the byte >.
79 *
80 * It represents an Internet mail address, given by concatenating
81 * the string represented by the encoded box part, the byte @,
82 * and the domain. For example, the encoded addresses
83 * <God@heaven.af.mil>
84 * <\God@heaven.af.mil>
85 * <"God"@heaven.af.mil>
86 * <@gateway.af.mil,@uucp.local:"\G\o\d"@heaven.af.mil>
87 * all represent the Internet mail address "God@heaven.af.mil".
88 */
89
Denis Vlasenko239d06b2008-11-06 23:42:42 +000090#include "libbb.h"
91#include "mail.h"
92
Vladimir Dronnikov944d2752009-10-15 23:50:48 +020093// limit maximum allowed number of headers to prevent overflows.
94// set to 0 to not limit
95#define MAX_HEADERS 256
96
Denys Vlasenko5707b522010-12-20 05:12:39 +010097static void send_r_n(const char *s)
98{
99 if (verbose)
100 bb_error_msg("send:'%s'", s);
101 printf("%s\r\n", s);
102}
103
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000104static int smtp_checkp(const char *fmt, const char *param, int code)
105{
106 char *answer;
Denys Vlasenko5707b522010-12-20 05:12:39 +0100107 char *msg = send_mail_command(fmt, param);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000108 // read stdin
Denys Vlasenko5707b522010-12-20 05:12:39 +0100109 // if the string has a form NNN- -- read next string. E.g. EHLO response
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000110 // parse first bytes to a number
111 // if code = -1 then just return this number
112 // if code != -1 then checks whether the number equals the code
113 // if not equal -> die saying msg
Denys Vlasenko5707b522010-12-20 05:12:39 +0100114 while ((answer = xmalloc_fgetline(stdin)) != NULL) {
Denys Vlasenko51d714c2010-12-20 12:19:46 +0100115 if (verbose)
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200116 bb_error_msg("recv:'%.*s'", (int)(strchrnul(answer, '\r') - answer), answer);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000117 if (strlen(answer) <= 3 || '-' != answer[3])
118 break;
Denys Vlasenko5707b522010-12-20 05:12:39 +0100119 free(answer);
120 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000121 if (answer) {
122 int n = atoi(answer);
123 if (timeout)
124 alarm(0);
125 free(answer);
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200126 if (-1 == code || n == code) {
127 free(msg);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000128 return n;
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200129 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000130 }
131 bb_error_msg_and_die("%s failed", msg);
132}
133
134static int smtp_check(const char *fmt, int code)
135{
136 return smtp_checkp(fmt, NULL, code);
137}
138
139// strip argument of bad chars
140static char *sane_address(char *str)
141{
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100142 char *s;
Aaro Koskinen14285d12013-02-25 00:45:06 +0200143
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100144 trim(str);
145 s = str;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000146 while (*s) {
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100147 if (!isalnum(*s) && !strchr("_-.@", *s)) {
148 bb_error_msg("bad address '%s'", str);
149 /* returning "": */
150 str[0] = '\0';
151 return str;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000152 }
153 s++;
154 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000155 return str;
156}
157
Aaro Koskinen06ad9642013-02-25 00:45:08 +0200158// check for an address inside angle brackets, if not found fall back to normal
159static char *angle_address(char *str)
160{
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100161 char *s, *e;
Aaro Koskinen06ad9642013-02-25 00:45:08 +0200162
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100163 trim(str);
164 e = last_char_is(str, '>');
165 if (e) {
166 s = strrchr(str, '<');
167 if (s) {
168 *e = '\0';
169 str = s + 1;
170 }
171 }
172 return sane_address(str);
Aaro Koskinen06ad9642013-02-25 00:45:08 +0200173}
174
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000175static void rcptto(const char *s)
176{
Aaro Koskinen14285d12013-02-25 00:45:06 +0200177 if (!*s)
178 return;
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200179 // N.B. we don't die if recipient is rejected, for the other recipients may be accepted
180 if (250 != smtp_checkp("RCPT TO:<%s>", s, -1))
181 bb_error_msg("Bad recipient: <%s>", s);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000182}
183
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200184// send to a list of comma separated addresses
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100185static void rcptto_list(const char *list)
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200186{
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100187 char *str = xstrdup(list);
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200188 char *s = str;
189 char prev = 0;
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100190 int in_quote = 0;
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200191
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100192 while (*s) {
193 char ch = *s++;
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200194
195 if (ch == '"' && prev != '\\') {
196 in_quote = !in_quote;
197 } else if (!in_quote && ch == ',') {
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100198 s[-1] = '\0';
199 rcptto(angle_address(str));
200 str = s;
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200201 }
202 prev = ch;
203 }
204 if (prev != ',')
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100205 rcptto(angle_address(str));
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200206 free(str);
207}
208
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000209int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
210int sendmail_main(int argc UNUSED_PARAM, char **argv)
211{
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000212 char *opt_connect = opt_connect;
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100213 char *opt_from = NULL;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000214 char *s;
215 llist_t *list = NULL;
Ron Yorston576b1d32012-04-28 17:04:19 +0200216 char *host = sane_address(safe_gethostname());
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200217 unsigned nheaders = 0;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000218 int code;
Aaro Koskinen236f2222013-02-25 00:45:10 +0200219 enum {
220 HDR_OTHER = 0,
221 HDR_TOCC,
222 HDR_BCC,
223 } last_hdr = 0;
224 int check_hdr;
Aaro Koskinen4a732222013-02-25 00:45:11 +0200225 int has_to = 0;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000226
227 enum {
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000228 //--- standard options
229 OPT_t = 1 << 0, // read message for recipients, append them to those on cmdline
230 OPT_f = 1 << 1, // sender address
231 OPT_o = 1 << 2, // various options. -oi IMPLIED! others are IGNORED!
Vladimir Dronnikovb618dba2009-10-04 01:34:54 +0200232 OPT_i = 1 << 3, // IMPLIED!
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000233 //--- BB specific options
Vladimir Dronnikovb618dba2009-10-04 01:34:54 +0200234 OPT_w = 1 << 4, // network timeout
235 OPT_H = 1 << 5, // use external connection helper
236 OPT_S = 1 << 6, // specify connection string
237 OPT_a = 1 << 7, // authentication tokens
Denys Vlasenko5707b522010-12-20 05:12:39 +0100238 OPT_v = 1 << 8, // verbosity
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000239 };
240
241 // init global variables
242 INIT_G();
243
244 // save initial stdin since body is piped!
245 xdup2(STDIN_FILENO, 3);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100246 G.fp0 = xfdopen_for_read(3);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000247
248 // parse options
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100249 // -v is a counter, -H and -S are mutually exclusive, -a is a list
250 opt_complementary = "vv:w+:H--S:S--H:a::";
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000251 // N.B. since -H and -S are mutually exclusive they do not interfere in opt_connect
252 // -a is for ssmtp (http://downloads.openwrt.org/people/nico/man/man8/ssmtp.8.html) compatibility,
253 // it is still under development.
Denys Vlasenko5707b522010-12-20 05:12:39 +0100254 opts = getopt32(argv, "tf:o:iw:H:S:a::v", &opt_from, NULL,
255 &timeout, &opt_connect, &opt_connect, &list, &verbose);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000256 //argc -= optind;
257 argv += optind;
258
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000259 // process -a[upm]<token> options
260 if ((opts & OPT_a) && !list)
261 bb_show_usage();
262 while (list) {
263 char *a = (char *) llist_pop(&list);
264 if ('u' == a[0])
265 G.user = xstrdup(a+1);
266 if ('p' == a[0])
267 G.pass = xstrdup(a+1);
268 // N.B. we support only AUTH LOGIN so far
269 //if ('m' == a[0])
270 // G.method = xstrdup(a+1);
271 }
272 // N.B. list == NULL here
273 //bb_info_msg("OPT[%x] AU[%s], AP[%s], AM[%s], ARGV[%s]", opts, au, ap, am, *argv);
274
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000275 // connect to server
276
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000277 // connection helper ordered? ->
278 if (opts & OPT_H) {
279 const char *args[] = { "sh", "-c", opt_connect, NULL };
280 // plug it in
281 launch_helper(args);
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200282 // Now:
283 // our stdout will go to helper's stdin,
284 // helper's stdout will be available on our stdin.
285
286 // Wait for initial server message.
287 // If helper (such as openssl) invokes STARTTLS, the initial 220
288 // is swallowed by helper (and not repeated after TLS is initiated).
289 // We will send NOOP cmd to server and check the response.
290 // We should get 220+250 on plain connection, 250 on STARTTLSed session.
291 //
292 // The problem here is some servers delay initial 220 message,
293 // and consider client to be a spammer if it starts sending cmds
294 // before 220 reached it. The code below is unsafe in this regard:
295 // in non-STARTTLSed case, we potentially send NOOP before 220
296 // is sent by server.
297 // Ideas? (--delay SECS opt? --assume-starttls-helper opt?)
298 code = smtp_check("NOOP", -1);
299 if (code == 220)
300 // we got 220 - this is not STARTTLSed connection,
301 // eat 250 response to our NOOP
302 smtp_check(NULL, 250);
303 else
304 if (code != 250)
305 bb_error_msg_and_die("SMTP init failed");
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000306 } else {
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200307 // vanilla connection
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000308 int fd;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000309 // host[:port] not explicitly specified? -> use $SMTPHOST
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200310 // no $SMTPHOST? -> use localhost
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000311 if (!(opts & OPT_S)) {
312 opt_connect = getenv("SMTPHOST");
313 if (!opt_connect)
314 opt_connect = (char *)"127.0.0.1";
315 }
316 // do connect
317 fd = create_and_connect_stream_or_die(opt_connect, 25);
318 // and make ourselves a simple IO filter
319 xmove_fd(fd, STDIN_FILENO);
320 xdup2(STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000321
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200322 // Wait for initial server 220 message
323 smtp_check(NULL, 220);
324 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000325
326 // we should start with modern EHLO
Ron Yorston576b1d32012-04-28 17:04:19 +0200327 if (250 != smtp_checkp("EHLO %s", host, -1))
328 smtp_checkp("HELO %s", host, 250);
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000329
330 // perform authentication
331 if (opts & OPT_a) {
332 smtp_check("AUTH LOGIN", 334);
333 // we must read credentials unless they are given via -a[up] options
334 if (!G.user || !G.pass)
335 get_cred_or_die(4);
336 encode_base64(NULL, G.user, NULL);
337 smtp_check("", 334);
338 encode_base64(NULL, G.pass, NULL);
339 smtp_check("", 235);
340 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000341
342 // set sender
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200343 // N.B. we have here a very loosely defined algorythm
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000344 // since sendmail historically offers no means to specify secrets on cmdline.
345 // 1) server can require no authentication ->
346 // we must just provide a (possibly fake) reply address.
347 // 2) server can require AUTH ->
348 // we must provide valid username and password along with a (possibly fake) reply address.
349 // For the sake of security username and password are to be read either from console or from a secured file.
350 // Since reading from console may defeat usability, the solution is either to read from a predefined
351 // file descriptor (e.g. 4), or again from a secured file.
352
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100353 // got no sender address? use auth name, then UID username as a last resort
354 if (!opt_from) {
Kaarle Ritvanen4e03d412014-02-09 09:49:36 +0100355 opt_from = xasprintf("%s@%s",
356 G.user ? G.user : xuid2uname(getuid()),
357 xgethostbyname(host)->h_name);
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100358 }
Kaarle Ritvanen4e03d412014-02-09 09:49:36 +0100359 free(host);
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100360
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000361 smtp_checkp("MAIL FROM:<%s>", opt_from, 250);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000362
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000363 // process message
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000364
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000365 // read recipients from message and add them to those given on cmdline.
366 // this means we scan stdin for To:, Cc:, Bcc: lines until an empty line
367 // and then use the rest of stdin as message body
368 code = 0; // set "analyze headers" mode
369 while ((s = xmalloc_fgetline(G.fp0)) != NULL) {
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200370 dump:
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000371 // put message lines doubling leading dots
372 if (code) {
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000373 // escape leading dots
374 // N.B. this feature is implied even if no -i (-oi) switch given
375 // N.B. we need to escape the leading dot regardless of
376 // whether it is single or not character on the line
377 if ('.' == s[0] /*&& '\0' == s[1] */)
378 printf(".");
379 // dump read line
Denys Vlasenko5707b522010-12-20 05:12:39 +0100380 send_r_n(s);
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000381 free(s);
382 continue;
383 }
384
385 // analyze headers
386 // To: or Cc: headers add recipients
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100387 check_hdr = (0 == strncasecmp("To:", s, 3));
Aaro Koskinen4a732222013-02-25 00:45:11 +0200388 has_to |= check_hdr;
Denys Vlasenko41fea012011-11-18 22:25:35 +0100389 if (opts & OPT_t) {
Aaro Koskinen4a732222013-02-25 00:45:11 +0200390 if (check_hdr || 0 == strncasecmp("Bcc:" + 1, s, 3)) {
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200391 rcptto_list(s+3);
Aaro Koskinen236f2222013-02-25 00:45:10 +0200392 last_hdr = HDR_TOCC;
Denys Vlasenko41fea012011-11-18 22:25:35 +0100393 goto addheader;
394 }
395 // Bcc: header adds blind copy (hidden) recipient
396 if (0 == strncasecmp("Bcc:", s, 4)) {
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200397 rcptto_list(s+4);
Denys Vlasenko41fea012011-11-18 22:25:35 +0100398 free(s);
Aaro Koskinen236f2222013-02-25 00:45:10 +0200399 last_hdr = HDR_BCC;
Denys Vlasenko41fea012011-11-18 22:25:35 +0100400 continue; // N.B. Bcc: vanishes from headers!
401 }
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200402 }
Aaro Koskinene82bfef2013-02-25 00:45:12 +0200403 check_hdr = (list && isspace(s[0]));
Aaro Koskinen236f2222013-02-25 00:45:10 +0200404 if (strchr(s, ':') || check_hdr) {
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200405 // other headers go verbatim
406 // N.B. RFC2822 2.2.3 "Long Header Fields" allows for headers to occupy several lines.
407 // Continuation is denoted by prefixing additional lines with whitespace(s).
408 // Thanks (stefan.seyfried at googlemail.com) for pointing this out.
Aaro Koskinen236f2222013-02-25 00:45:10 +0200409 if (check_hdr && last_hdr != HDR_OTHER) {
410 rcptto_list(s+1);
411 if (last_hdr == HDR_BCC)
412 continue;
413 // N.B. Bcc: vanishes from headers!
414 } else {
415 last_hdr = HDR_OTHER;
416 }
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200417 addheader:
Vladimir Dronnikov8dbe9bb2009-10-17 03:35:10 +0200418 // N.B. we allow MAX_HEADERS generic headers at most to prevent attacks
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200419 if (MAX_HEADERS && ++nheaders >= MAX_HEADERS)
420 goto bail;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000421 llist_add_to_end(&list, s);
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000422 } else {
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200423 // a line without ":" (an empty line too, by definition) doesn't look like a valid header
424 // so stop "analyze headers" mode
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200425 reenter:
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000426 // put recipients specified on cmdline
Aaro Koskinene82bfef2013-02-25 00:45:12 +0200427 check_hdr = 1;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000428 while (*argv) {
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200429 char *t = sane_address(*argv);
430 rcptto(t);
431 //if (MAX_HEADERS && ++nheaders >= MAX_HEADERS)
432 // goto bail;
Aaro Koskinene82bfef2013-02-25 00:45:12 +0200433 if (!has_to) {
434 const char *hdr;
435
436 if (check_hdr && argv[1])
437 hdr = "To: %s,";
438 else if (check_hdr)
439 hdr = "To: %s";
440 else if (argv[1])
441 hdr = "To: %s," + 3;
442 else
443 hdr = "To: %s" + 3;
Aaro Koskinen4a732222013-02-25 00:45:11 +0200444 llist_add_to_end(&list,
Aaro Koskinene82bfef2013-02-25 00:45:12 +0200445 xasprintf(hdr, t));
446 check_hdr = 0;
447 }
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000448 argv++;
449 }
450 // enter "put message" mode
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200451 // N.B. DATA fails iff no recipients were accepted (or even provided)
452 // in this case just bail out gracefully
453 if (354 != smtp_check("DATA", -1))
454 goto bail;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000455 // dump the headers
456 while (list) {
Denys Vlasenko5707b522010-12-20 05:12:39 +0100457 send_r_n((char *) llist_pop(&list));
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000458 }
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000459 // stop analyzing headers
460 code++;
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200461 // N.B. !s means: we read nothing, and nothing to be read in the future.
462 // just dump empty line and break the loop
463 if (!s) {
Denys Vlasenko5707b522010-12-20 05:12:39 +0100464 send_r_n("");
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200465 break;
466 }
467 // go dump message body
468 // N.B. "s" already contains the first non-header line, so pretend we read it from input
469 goto dump;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000470 }
471 }
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200472 // odd case: we didn't stop "analyze headers" mode -> message body is empty. Reenter the loop
473 // N.B. after reenter code will be > 0
474 if (!code)
475 goto reenter;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000476
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000477 // finalize the message
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000478 smtp_check(".", 250);
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200479 bail:
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000480 // ... and say goodbye
481 smtp_check("QUIT", 221);
482 // cleanup
483 if (ENABLE_FEATURE_CLEAN_UP)
484 fclose(G.fp0);
485
486 return EXIT_SUCCESS;
487}