blob: cd27d676ef2a4878c7466adf62e48ef25e09e776 [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
Denys Vlasenko9de75092016-07-14 19:14:54 +020013//usage: "[-tv] [-f SENDER] [-amLOGIN 4<user_pass.txt | -auUSER -apPASS]"
Denys Vlasenko2a4d7f42016-07-14 20:06:44 +020014//usage: "\n [-w SECS] [-H 'PROG ARGS' | -S HOST] [RECIPIENT_EMAIL]..."
Denys Vlasenko5707b522010-12-20 05:12:39 +010015//usage:#define sendmail_full_usage "\n\n"
16//usage: "Read email from stdin and send it\n"
17//usage: "\nStandard options:"
18//usage: "\n -t Read additional recipients from message body"
Denys Vlasenko07f417b2014-02-05 15:01:39 +010019//usage: "\n -f SENDER For use in MAIL FROM:<sender>. Can be empty string"
20//usage: "\n Default: -auUSER, or username of current UID"
Denys Vlasenko5707b522010-12-20 05:12:39 +010021//usage: "\n -o OPTIONS Various options. -oi implied, others are ignored"
Denys Vlasenko9de75092016-07-14 19:14:54 +020022//usage: "\n -i -oi synonym, implied and ignored"
Denys Vlasenko5707b522010-12-20 05:12:39 +010023//usage: "\n"
24//usage: "\nBusybox specific options:"
25//usage: "\n -v Verbose"
26//usage: "\n -w SECS Network timeout"
Denys Vlasenko9de75092016-07-14 19:14:54 +020027//usage: "\n -H 'PROG ARGS' Run connection helper. Examples:"
28//usage: "\n openssl s_client -quiet -tls1 -starttls smtp -connect smtp.gmail.com:25"
29//usage: "\n openssl s_client -quiet -tls1 -connect smtp.gmail.com:465"
30//usage: "\n -S HOST[:PORT] Server (default $SMTPHOST or 127.0.0.1)"
31//usage: "\n -amLOGIN Log in using AUTH LOGIN (-amCRAM-MD5 not supported)"
32//usage: "\n -auUSER Username for AUTH"
33//usage: "\n -apPASS Password for AUTH"
Denys Vlasenko5707b522010-12-20 05:12:39 +010034//usage: "\n"
Denys Vlasenko9de75092016-07-14 19:14:54 +020035//usage: "\nIf no -a options are given, authentication is not done."
36//usage: "\nIf -amLOGIN is given but no -au/-ap, user/password is read from fd #4."
37//usage: "\nOther options are silently ignored; -oi is implied."
Denys Vlasenko5707b522010-12-20 05:12:39 +010038//usage: IF_MAKEMIME(
Denys Vlasenko9de75092016-07-14 19:14:54 +020039//usage: "\nUse makemime to create emails with attachments."
Denys Vlasenko5707b522010-12-20 05:12:39 +010040//usage: )
41
Denys Vlasenko07f417b2014-02-05 15:01:39 +010042/* Currently we don't sanitize or escape user-supplied SENDER and RECIPIENT_EMAILs.
43 * We may need to do so. For one, '.' in usernames seems to require escaping!
44 *
45 * From http://cr.yp.to/smtp/address.html:
46 *
47 * SMTP offers three ways to encode a character inside an address:
48 *
49 * "safe": the character, if it is not <>()[].,;:@, backslash,
50 * double-quote, space, or an ASCII control character;
51 * "quoted": the character, if it is not \012, \015, backslash,
52 * or double-quote; or
53 * "slashed": backslash followed by the character.
54 *
55 * An encoded box part is either (1) a sequence of one or more slashed
56 * or safe characters or (2) a double quote, a sequence of zero or more
57 * slashed or quoted characters, and a double quote. It represents
58 * the concatenation of the characters encoded inside it.
59 *
60 * For example, the encoded box parts
61 * angels
62 * \a\n\g\e\l\s
63 * "\a\n\g\e\l\s"
64 * "angels"
65 * "ang\els"
66 * all represent the 6-byte string "angels", and the encoded box parts
67 * a\,comma
68 * \a\,\c\o\m\m\a
69 * "a,comma"
70 * all represent the 7-byte string "a,comma".
71 *
72 * An encoded address contains
73 * the byte <;
74 * optionally, a route followed by a colon;
75 * an encoded box part, the byte @, and a domain; and
76 * the byte >.
77 *
78 * It represents an Internet mail address, given by concatenating
79 * the string represented by the encoded box part, the byte @,
80 * and the domain. For example, the encoded addresses
81 * <God@heaven.af.mil>
82 * <\God@heaven.af.mil>
83 * <"God"@heaven.af.mil>
84 * <@gateway.af.mil,@uucp.local:"\G\o\d"@heaven.af.mil>
85 * all represent the Internet mail address "God@heaven.af.mil".
86 */
87
Denis Vlasenko239d06b2008-11-06 23:42:42 +000088#include "libbb.h"
89#include "mail.h"
90
Vladimir Dronnikov944d2752009-10-15 23:50:48 +020091// limit maximum allowed number of headers to prevent overflows.
92// set to 0 to not limit
93#define MAX_HEADERS 256
94
Denys Vlasenko5707b522010-12-20 05:12:39 +010095static void send_r_n(const char *s)
96{
97 if (verbose)
98 bb_error_msg("send:'%s'", s);
99 printf("%s\r\n", s);
100}
101
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000102static int smtp_checkp(const char *fmt, const char *param, int code)
103{
104 char *answer;
Denys Vlasenko5707b522010-12-20 05:12:39 +0100105 char *msg = send_mail_command(fmt, param);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000106 // read stdin
Denys Vlasenko5707b522010-12-20 05:12:39 +0100107 // if the string has a form NNN- -- read next string. E.g. EHLO response
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000108 // parse first bytes to a number
109 // if code = -1 then just return this number
110 // if code != -1 then checks whether the number equals the code
111 // if not equal -> die saying msg
Denys Vlasenko5707b522010-12-20 05:12:39 +0100112 while ((answer = xmalloc_fgetline(stdin)) != NULL) {
Denys Vlasenko51d714c2010-12-20 12:19:46 +0100113 if (verbose)
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200114 bb_error_msg("recv:'%.*s'", (int)(strchrnul(answer, '\r') - answer), answer);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000115 if (strlen(answer) <= 3 || '-' != answer[3])
116 break;
Denys Vlasenko5707b522010-12-20 05:12:39 +0100117 free(answer);
118 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000119 if (answer) {
120 int n = atoi(answer);
121 if (timeout)
122 alarm(0);
123 free(answer);
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200124 if (-1 == code || n == code) {
125 free(msg);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000126 return n;
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200127 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000128 }
129 bb_error_msg_and_die("%s failed", msg);
130}
131
132static int smtp_check(const char *fmt, int code)
133{
134 return smtp_checkp(fmt, NULL, code);
135}
136
137// strip argument of bad chars
138static char *sane_address(char *str)
139{
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100140 char *s;
Aaro Koskinen14285d12013-02-25 00:45:06 +0200141
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100142 trim(str);
143 s = str;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000144 while (*s) {
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100145 if (!isalnum(*s) && !strchr("_-.@", *s)) {
146 bb_error_msg("bad address '%s'", str);
147 /* returning "": */
148 str[0] = '\0';
149 return str;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000150 }
151 s++;
152 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000153 return str;
154}
155
Aaro Koskinen06ad9642013-02-25 00:45:08 +0200156// check for an address inside angle brackets, if not found fall back to normal
157static char *angle_address(char *str)
158{
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100159 char *s, *e;
Aaro Koskinen06ad9642013-02-25 00:45:08 +0200160
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100161 trim(str);
162 e = last_char_is(str, '>');
163 if (e) {
164 s = strrchr(str, '<');
165 if (s) {
166 *e = '\0';
167 str = s + 1;
168 }
169 }
170 return sane_address(str);
Aaro Koskinen06ad9642013-02-25 00:45:08 +0200171}
172
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000173static void rcptto(const char *s)
174{
Aaro Koskinen14285d12013-02-25 00:45:06 +0200175 if (!*s)
176 return;
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200177 // N.B. we don't die if recipient is rejected, for the other recipients may be accepted
178 if (250 != smtp_checkp("RCPT TO:<%s>", s, -1))
179 bb_error_msg("Bad recipient: <%s>", s);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000180}
181
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200182// send to a list of comma separated addresses
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100183static void rcptto_list(const char *list)
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200184{
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100185 char *str = xstrdup(list);
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200186 char *s = str;
187 char prev = 0;
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100188 int in_quote = 0;
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200189
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100190 while (*s) {
191 char ch = *s++;
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200192
193 if (ch == '"' && prev != '\\') {
194 in_quote = !in_quote;
195 } else if (!in_quote && ch == ',') {
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100196 s[-1] = '\0';
197 rcptto(angle_address(str));
198 str = s;
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200199 }
200 prev = ch;
201 }
202 if (prev != ',')
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100203 rcptto(angle_address(str));
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200204 free(str);
205}
206
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000207int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
208int sendmail_main(int argc UNUSED_PARAM, char **argv)
209{
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000210 char *opt_connect = opt_connect;
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100211 char *opt_from = NULL;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000212 char *s;
213 llist_t *list = NULL;
Ron Yorston576b1d32012-04-28 17:04:19 +0200214 char *host = sane_address(safe_gethostname());
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200215 unsigned nheaders = 0;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000216 int code;
Aaro Koskinen236f2222013-02-25 00:45:10 +0200217 enum {
218 HDR_OTHER = 0,
219 HDR_TOCC,
220 HDR_BCC,
221 } last_hdr = 0;
222 int check_hdr;
Aaro Koskinen4a732222013-02-25 00:45:11 +0200223 int has_to = 0;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000224
225 enum {
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000226 //--- standard options
227 OPT_t = 1 << 0, // read message for recipients, append them to those on cmdline
228 OPT_f = 1 << 1, // sender address
229 OPT_o = 1 << 2, // various options. -oi IMPLIED! others are IGNORED!
Vladimir Dronnikovb618dba2009-10-04 01:34:54 +0200230 OPT_i = 1 << 3, // IMPLIED!
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000231 //--- BB specific options
Vladimir Dronnikovb618dba2009-10-04 01:34:54 +0200232 OPT_w = 1 << 4, // network timeout
233 OPT_H = 1 << 5, // use external connection helper
234 OPT_S = 1 << 6, // specify connection string
235 OPT_a = 1 << 7, // authentication tokens
Denys Vlasenko5707b522010-12-20 05:12:39 +0100236 OPT_v = 1 << 8, // verbosity
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000237 };
238
239 // init global variables
240 INIT_G();
241
242 // save initial stdin since body is piped!
243 xdup2(STDIN_FILENO, 3);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100244 G.fp0 = xfdopen_for_read(3);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000245
246 // parse options
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100247 // -v is a counter, -H and -S are mutually exclusive, -a is a list
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200248 opt_complementary = "vv:H--S:S--H";
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000249 // N.B. since -H and -S are mutually exclusive they do not interfere in opt_connect
250 // -a is for ssmtp (http://downloads.openwrt.org/people/nico/man/man8/ssmtp.8.html) compatibility,
251 // it is still under development.
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200252 opts = getopt32(argv, "tf:o:iw:+H:S:a:*:v", &opt_from, NULL,
Denys Vlasenko5707b522010-12-20 05:12:39 +0100253 &timeout, &opt_connect, &opt_connect, &list, &verbose);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000254 //argc -= optind;
255 argv += optind;
256
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000257 // process -a[upm]<token> options
258 if ((opts & OPT_a) && !list)
259 bb_show_usage();
260 while (list) {
261 char *a = (char *) llist_pop(&list);
262 if ('u' == a[0])
263 G.user = xstrdup(a+1);
264 if ('p' == a[0])
265 G.pass = xstrdup(a+1);
266 // N.B. we support only AUTH LOGIN so far
267 //if ('m' == a[0])
268 // G.method = xstrdup(a+1);
269 }
270 // N.B. list == NULL here
Denys Vlasenko76b680c2016-03-30 16:04:37 +0200271 //bb_error_msg("OPT[%x] AU[%s], AP[%s], AM[%s], ARGV[%s]", opts, au, ap, am, *argv);
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000272
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000273 // connect to server
274
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000275 // connection helper ordered? ->
276 if (opts & OPT_H) {
277 const char *args[] = { "sh", "-c", opt_connect, NULL };
278 // plug it in
279 launch_helper(args);
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200280 // Now:
281 // our stdout will go to helper's stdin,
282 // helper's stdout will be available on our stdin.
283
284 // Wait for initial server message.
285 // If helper (such as openssl) invokes STARTTLS, the initial 220
286 // is swallowed by helper (and not repeated after TLS is initiated).
287 // We will send NOOP cmd to server and check the response.
288 // We should get 220+250 on plain connection, 250 on STARTTLSed session.
289 //
290 // The problem here is some servers delay initial 220 message,
291 // and consider client to be a spammer if it starts sending cmds
292 // before 220 reached it. The code below is unsafe in this regard:
293 // in non-STARTTLSed case, we potentially send NOOP before 220
294 // is sent by server.
295 // Ideas? (--delay SECS opt? --assume-starttls-helper opt?)
296 code = smtp_check("NOOP", -1);
297 if (code == 220)
298 // we got 220 - this is not STARTTLSed connection,
299 // eat 250 response to our NOOP
300 smtp_check(NULL, 250);
301 else
302 if (code != 250)
303 bb_error_msg_and_die("SMTP init failed");
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000304 } else {
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200305 // vanilla connection
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000306 int fd;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000307 // host[:port] not explicitly specified? -> use $SMTPHOST
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200308 // no $SMTPHOST? -> use localhost
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000309 if (!(opts & OPT_S)) {
310 opt_connect = getenv("SMTPHOST");
311 if (!opt_connect)
312 opt_connect = (char *)"127.0.0.1";
313 }
314 // do connect
315 fd = create_and_connect_stream_or_die(opt_connect, 25);
316 // and make ourselves a simple IO filter
317 xmove_fd(fd, STDIN_FILENO);
318 xdup2(STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000319
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200320 // Wait for initial server 220 message
321 smtp_check(NULL, 220);
322 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000323
324 // we should start with modern EHLO
Ron Yorston576b1d32012-04-28 17:04:19 +0200325 if (250 != smtp_checkp("EHLO %s", host, -1))
326 smtp_checkp("HELO %s", host, 250);
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000327
328 // perform authentication
329 if (opts & OPT_a) {
330 smtp_check("AUTH LOGIN", 334);
331 // we must read credentials unless they are given via -a[up] options
332 if (!G.user || !G.pass)
333 get_cred_or_die(4);
334 encode_base64(NULL, G.user, NULL);
335 smtp_check("", 334);
336 encode_base64(NULL, G.pass, NULL);
337 smtp_check("", 235);
338 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000339
340 // set sender
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200341 // N.B. we have here a very loosely defined algorythm
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000342 // since sendmail historically offers no means to specify secrets on cmdline.
343 // 1) server can require no authentication ->
344 // we must just provide a (possibly fake) reply address.
345 // 2) server can require AUTH ->
346 // we must provide valid username and password along with a (possibly fake) reply address.
347 // For the sake of security username and password are to be read either from console or from a secured file.
348 // Since reading from console may defeat usability, the solution is either to read from a predefined
349 // file descriptor (e.g. 4), or again from a secured file.
350
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100351 // got no sender address? use auth name, then UID username as a last resort
352 if (!opt_from) {
Kaarle Ritvanen4e03d412014-02-09 09:49:36 +0100353 opt_from = xasprintf("%s@%s",
354 G.user ? G.user : xuid2uname(getuid()),
355 xgethostbyname(host)->h_name);
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100356 }
Kaarle Ritvanen4e03d412014-02-09 09:49:36 +0100357 free(host);
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100358
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000359 smtp_checkp("MAIL FROM:<%s>", opt_from, 250);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000360
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000361 // process message
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000362
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000363 // read recipients from message and add them to those given on cmdline.
364 // this means we scan stdin for To:, Cc:, Bcc: lines until an empty line
365 // and then use the rest of stdin as message body
366 code = 0; // set "analyze headers" mode
367 while ((s = xmalloc_fgetline(G.fp0)) != NULL) {
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200368 dump:
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000369 // put message lines doubling leading dots
370 if (code) {
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000371 // escape leading dots
372 // N.B. this feature is implied even if no -i (-oi) switch given
373 // N.B. we need to escape the leading dot regardless of
374 // whether it is single or not character on the line
375 if ('.' == s[0] /*&& '\0' == s[1] */)
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200376 bb_putchar('.');
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000377 // dump read line
Denys Vlasenko5707b522010-12-20 05:12:39 +0100378 send_r_n(s);
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000379 free(s);
380 continue;
381 }
382
383 // analyze headers
384 // To: or Cc: headers add recipients
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100385 check_hdr = (0 == strncasecmp("To:", s, 3));
Aaro Koskinen4a732222013-02-25 00:45:11 +0200386 has_to |= check_hdr;
Denys Vlasenko41fea012011-11-18 22:25:35 +0100387 if (opts & OPT_t) {
Aaro Koskinen4a732222013-02-25 00:45:11 +0200388 if (check_hdr || 0 == strncasecmp("Bcc:" + 1, s, 3)) {
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200389 rcptto_list(s+3);
Aaro Koskinen236f2222013-02-25 00:45:10 +0200390 last_hdr = HDR_TOCC;
Denys Vlasenko41fea012011-11-18 22:25:35 +0100391 goto addheader;
392 }
393 // Bcc: header adds blind copy (hidden) recipient
394 if (0 == strncasecmp("Bcc:", s, 4)) {
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200395 rcptto_list(s+4);
Denys Vlasenko41fea012011-11-18 22:25:35 +0100396 free(s);
Aaro Koskinen236f2222013-02-25 00:45:10 +0200397 last_hdr = HDR_BCC;
Denys Vlasenko41fea012011-11-18 22:25:35 +0100398 continue; // N.B. Bcc: vanishes from headers!
399 }
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200400 }
Aaro Koskinene82bfef2013-02-25 00:45:12 +0200401 check_hdr = (list && isspace(s[0]));
Aaro Koskinen236f2222013-02-25 00:45:10 +0200402 if (strchr(s, ':') || check_hdr) {
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200403 // other headers go verbatim
404 // N.B. RFC2822 2.2.3 "Long Header Fields" allows for headers to occupy several lines.
405 // Continuation is denoted by prefixing additional lines with whitespace(s).
406 // Thanks (stefan.seyfried at googlemail.com) for pointing this out.
Aaro Koskinen236f2222013-02-25 00:45:10 +0200407 if (check_hdr && last_hdr != HDR_OTHER) {
408 rcptto_list(s+1);
409 if (last_hdr == HDR_BCC)
410 continue;
411 // N.B. Bcc: vanishes from headers!
412 } else {
413 last_hdr = HDR_OTHER;
414 }
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200415 addheader:
Vladimir Dronnikov8dbe9bb2009-10-17 03:35:10 +0200416 // N.B. we allow MAX_HEADERS generic headers at most to prevent attacks
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200417 if (MAX_HEADERS && ++nheaders >= MAX_HEADERS)
418 goto bail;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000419 llist_add_to_end(&list, s);
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000420 } else {
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200421 // a line without ":" (an empty line too, by definition) doesn't look like a valid header
422 // so stop "analyze headers" mode
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200423 reenter:
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000424 // put recipients specified on cmdline
Aaro Koskinene82bfef2013-02-25 00:45:12 +0200425 check_hdr = 1;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000426 while (*argv) {
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200427 char *t = sane_address(*argv);
428 rcptto(t);
429 //if (MAX_HEADERS && ++nheaders >= MAX_HEADERS)
430 // goto bail;
Aaro Koskinene82bfef2013-02-25 00:45:12 +0200431 if (!has_to) {
432 const char *hdr;
433
434 if (check_hdr && argv[1])
435 hdr = "To: %s,";
436 else if (check_hdr)
437 hdr = "To: %s";
438 else if (argv[1])
439 hdr = "To: %s," + 3;
440 else
441 hdr = "To: %s" + 3;
Aaro Koskinen4a732222013-02-25 00:45:11 +0200442 llist_add_to_end(&list,
Aaro Koskinene82bfef2013-02-25 00:45:12 +0200443 xasprintf(hdr, t));
444 check_hdr = 0;
445 }
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000446 argv++;
447 }
448 // enter "put message" mode
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200449 // N.B. DATA fails iff no recipients were accepted (or even provided)
450 // in this case just bail out gracefully
451 if (354 != smtp_check("DATA", -1))
452 goto bail;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000453 // dump the headers
454 while (list) {
Denys Vlasenko5707b522010-12-20 05:12:39 +0100455 send_r_n((char *) llist_pop(&list));
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000456 }
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000457 // stop analyzing headers
458 code++;
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200459 // N.B. !s means: we read nothing, and nothing to be read in the future.
460 // just dump empty line and break the loop
461 if (!s) {
Denys Vlasenko5707b522010-12-20 05:12:39 +0100462 send_r_n("");
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200463 break;
464 }
465 // go dump message body
466 // N.B. "s" already contains the first non-header line, so pretend we read it from input
467 goto dump;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000468 }
469 }
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200470 // odd case: we didn't stop "analyze headers" mode -> message body is empty. Reenter the loop
471 // N.B. after reenter code will be > 0
472 if (!code)
473 goto reenter;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000474
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000475 // finalize the message
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000476 smtp_check(".", 250);
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200477 bail:
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000478 // ... and say goodbye
479 smtp_check("QUIT", 221);
480 // cleanup
481 if (ENABLE_FEATURE_CLEAN_UP)
482 fclose(G.fp0);
483
484 return EXIT_SUCCESS;
485}