blob: defbd955263c9202b22a5a5eec6d853d01616418 [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 Vlasenkoc19f7582016-11-23 09:58:03 +01009//config:config SENDMAIL
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "sendmail (14 kb)"
Denys Vlasenkoc19f7582016-11-23 09:58:03 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: Barebones sendmail.
Denys Vlasenkoc19f7582016-11-23 09:58:03 +010014
15//applet:IF_SENDMAIL(APPLET(sendmail, BB_DIR_USR_SBIN, BB_SUID_DROP))
Denys Vlasenko5707b522010-12-20 05:12:39 +010016
Denys Vlasenkod616ab62011-05-22 03:46:33 +020017//kbuild:lib-$(CONFIG_SENDMAIL) += sendmail.o mail.o
18
Denys Vlasenko5707b522010-12-20 05:12:39 +010019//usage:#define sendmail_trivial_usage
Denys Vlasenko9de75092016-07-14 19:14:54 +020020//usage: "[-tv] [-f SENDER] [-amLOGIN 4<user_pass.txt | -auUSER -apPASS]"
Denys Vlasenko2a4d7f42016-07-14 20:06:44 +020021//usage: "\n [-w SECS] [-H 'PROG ARGS' | -S HOST] [RECIPIENT_EMAIL]..."
Denys Vlasenko5707b522010-12-20 05:12:39 +010022//usage:#define sendmail_full_usage "\n\n"
23//usage: "Read email from stdin and send it\n"
24//usage: "\nStandard options:"
25//usage: "\n -t Read additional recipients from message body"
Denys Vlasenko07f417b2014-02-05 15:01:39 +010026//usage: "\n -f SENDER For use in MAIL FROM:<sender>. Can be empty string"
27//usage: "\n Default: -auUSER, or username of current UID"
Denys Vlasenko5707b522010-12-20 05:12:39 +010028//usage: "\n -o OPTIONS Various options. -oi implied, others are ignored"
Denys Vlasenko9de75092016-07-14 19:14:54 +020029//usage: "\n -i -oi synonym, implied and ignored"
Denys Vlasenko5707b522010-12-20 05:12:39 +010030//usage: "\n"
31//usage: "\nBusybox specific options:"
32//usage: "\n -v Verbose"
33//usage: "\n -w SECS Network timeout"
Denys Vlasenko9de75092016-07-14 19:14:54 +020034//usage: "\n -H 'PROG ARGS' Run connection helper. Examples:"
35//usage: "\n openssl s_client -quiet -tls1 -starttls smtp -connect smtp.gmail.com:25"
36//usage: "\n openssl s_client -quiet -tls1 -connect smtp.gmail.com:465"
Denys Vlasenkob9f56e82016-09-07 13:16:33 +020037//usage: "\n $SMTP_ANTISPAM_DELAY: seconds to wait after helper connect"
Denys Vlasenko9de75092016-07-14 19:14:54 +020038//usage: "\n -S HOST[:PORT] Server (default $SMTPHOST or 127.0.0.1)"
39//usage: "\n -amLOGIN Log in using AUTH LOGIN (-amCRAM-MD5 not supported)"
40//usage: "\n -auUSER Username for AUTH"
41//usage: "\n -apPASS Password for AUTH"
Denys Vlasenko5707b522010-12-20 05:12:39 +010042//usage: "\n"
Denys Vlasenko9de75092016-07-14 19:14:54 +020043//usage: "\nIf no -a options are given, authentication is not done."
44//usage: "\nIf -amLOGIN is given but no -au/-ap, user/password is read from fd #4."
45//usage: "\nOther options are silently ignored; -oi is implied."
Denys Vlasenko5707b522010-12-20 05:12:39 +010046//usage: IF_MAKEMIME(
Denys Vlasenko9de75092016-07-14 19:14:54 +020047//usage: "\nUse makemime to create emails with attachments."
Denys Vlasenko5707b522010-12-20 05:12:39 +010048//usage: )
49
Denys Vlasenko07f417b2014-02-05 15:01:39 +010050/* Currently we don't sanitize or escape user-supplied SENDER and RECIPIENT_EMAILs.
51 * We may need to do so. For one, '.' in usernames seems to require escaping!
52 *
53 * From http://cr.yp.to/smtp/address.html:
54 *
55 * SMTP offers three ways to encode a character inside an address:
56 *
57 * "safe": the character, if it is not <>()[].,;:@, backslash,
58 * double-quote, space, or an ASCII control character;
59 * "quoted": the character, if it is not \012, \015, backslash,
60 * or double-quote; or
61 * "slashed": backslash followed by the character.
62 *
63 * An encoded box part is either (1) a sequence of one or more slashed
64 * or safe characters or (2) a double quote, a sequence of zero or more
65 * slashed or quoted characters, and a double quote. It represents
66 * the concatenation of the characters encoded inside it.
67 *
68 * For example, the encoded box parts
69 * angels
70 * \a\n\g\e\l\s
71 * "\a\n\g\e\l\s"
72 * "angels"
73 * "ang\els"
74 * all represent the 6-byte string "angels", and the encoded box parts
75 * a\,comma
76 * \a\,\c\o\m\m\a
77 * "a,comma"
78 * all represent the 7-byte string "a,comma".
79 *
80 * An encoded address contains
81 * the byte <;
82 * optionally, a route followed by a colon;
83 * an encoded box part, the byte @, and a domain; and
84 * the byte >.
85 *
86 * It represents an Internet mail address, given by concatenating
87 * the string represented by the encoded box part, the byte @,
88 * and the domain. For example, the encoded addresses
89 * <God@heaven.af.mil>
90 * <\God@heaven.af.mil>
91 * <"God"@heaven.af.mil>
92 * <@gateway.af.mil,@uucp.local:"\G\o\d"@heaven.af.mil>
93 * all represent the Internet mail address "God@heaven.af.mil".
94 */
95
Denis Vlasenko239d06b2008-11-06 23:42:42 +000096#include "libbb.h"
97#include "mail.h"
98
Vladimir Dronnikov944d2752009-10-15 23:50:48 +020099// limit maximum allowed number of headers to prevent overflows.
100// set to 0 to not limit
101#define MAX_HEADERS 256
102
Denys Vlasenko5707b522010-12-20 05:12:39 +0100103static void send_r_n(const char *s)
104{
105 if (verbose)
106 bb_error_msg("send:'%s'", s);
107 printf("%s\r\n", s);
108}
109
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000110static int smtp_checkp(const char *fmt, const char *param, int code)
111{
112 char *answer;
Denys Vlasenko5707b522010-12-20 05:12:39 +0100113 char *msg = send_mail_command(fmt, param);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000114 // read stdin
Denys Vlasenko5707b522010-12-20 05:12:39 +0100115 // if the string has a form NNN- -- read next string. E.g. EHLO response
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000116 // parse first bytes to a number
117 // if code = -1 then just return this number
118 // if code != -1 then checks whether the number equals the code
119 // if not equal -> die saying msg
Denys Vlasenko5707b522010-12-20 05:12:39 +0100120 while ((answer = xmalloc_fgetline(stdin)) != NULL) {
Denys Vlasenko51d714c2010-12-20 12:19:46 +0100121 if (verbose)
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200122 bb_error_msg("recv:'%.*s'", (int)(strchrnul(answer, '\r') - answer), answer);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000123 if (strlen(answer) <= 3 || '-' != answer[3])
124 break;
Denys Vlasenko5707b522010-12-20 05:12:39 +0100125 free(answer);
126 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000127 if (answer) {
128 int n = atoi(answer);
129 if (timeout)
130 alarm(0);
131 free(answer);
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200132 if (-1 == code || n == code) {
133 free(msg);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000134 return n;
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200135 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000136 }
137 bb_error_msg_and_die("%s failed", msg);
138}
139
140static int smtp_check(const char *fmt, int code)
141{
142 return smtp_checkp(fmt, NULL, code);
143}
144
145// strip argument of bad chars
146static char *sane_address(char *str)
147{
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100148 char *s;
Aaro Koskinen14285d12013-02-25 00:45:06 +0200149
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100150 trim(str);
151 s = str;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000152 while (*s) {
Denys Vlasenkoc39ee042017-02-12 21:57:22 +0100153 if (!isalnum(*s) && !strchr("+_-.@", *s)) {
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100154 bb_error_msg("bad address '%s'", str);
155 /* returning "": */
156 str[0] = '\0';
157 return str;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000158 }
159 s++;
160 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000161 return str;
162}
163
Aaro Koskinen06ad9642013-02-25 00:45:08 +0200164// check for an address inside angle brackets, if not found fall back to normal
165static char *angle_address(char *str)
166{
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100167 char *s, *e;
Aaro Koskinen06ad9642013-02-25 00:45:08 +0200168
Denys Vlasenko20077c12017-08-05 17:50:35 +0200169 e = trim(str);
170 if (e != str && e[-1] == '>') {
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100171 s = strrchr(str, '<');
172 if (s) {
173 *e = '\0';
174 str = s + 1;
175 }
176 }
177 return sane_address(str);
Aaro Koskinen06ad9642013-02-25 00:45:08 +0200178}
179
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000180static void rcptto(const char *s)
181{
Aaro Koskinen14285d12013-02-25 00:45:06 +0200182 if (!*s)
183 return;
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200184 // N.B. we don't die if recipient is rejected, for the other recipients may be accepted
185 if (250 != smtp_checkp("RCPT TO:<%s>", s, -1))
186 bb_error_msg("Bad recipient: <%s>", s);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000187}
188
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200189// send to a list of comma separated addresses
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100190static void rcptto_list(const char *list)
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200191{
Denys Vlasenko3505e382017-08-22 15:53:16 +0200192 char *free_me = xstrdup(list);
193 char *str = free_me;
194 char *s = free_me;
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200195 char prev = 0;
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100196 int in_quote = 0;
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200197
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100198 while (*s) {
199 char ch = *s++;
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200200
201 if (ch == '"' && prev != '\\') {
202 in_quote = !in_quote;
203 } else if (!in_quote && ch == ',') {
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100204 s[-1] = '\0';
205 rcptto(angle_address(str));
206 str = s;
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200207 }
208 prev = ch;
209 }
210 if (prev != ',')
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100211 rcptto(angle_address(str));
Denys Vlasenko3505e382017-08-22 15:53:16 +0200212 free(free_me);
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200213}
214
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000215int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
216int sendmail_main(int argc UNUSED_PARAM, char **argv)
217{
Denys Vlasenkob9f56e82016-09-07 13:16:33 +0200218 char *opt_connect;
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100219 char *opt_from = NULL;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000220 char *s;
221 llist_t *list = NULL;
Ron Yorston576b1d32012-04-28 17:04:19 +0200222 char *host = sane_address(safe_gethostname());
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200223 unsigned nheaders = 0;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000224 int code;
Aaro Koskinen236f2222013-02-25 00:45:10 +0200225 enum {
226 HDR_OTHER = 0,
227 HDR_TOCC,
228 HDR_BCC,
229 } last_hdr = 0;
230 int check_hdr;
Aaro Koskinen4a732222013-02-25 00:45:11 +0200231 int has_to = 0;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000232
233 enum {
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000234 //--- standard options
235 OPT_t = 1 << 0, // read message for recipients, append them to those on cmdline
236 OPT_f = 1 << 1, // sender address
237 OPT_o = 1 << 2, // various options. -oi IMPLIED! others are IGNORED!
Vladimir Dronnikovb618dba2009-10-04 01:34:54 +0200238 OPT_i = 1 << 3, // IMPLIED!
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000239 //--- BB specific options
Vladimir Dronnikovb618dba2009-10-04 01:34:54 +0200240 OPT_w = 1 << 4, // network timeout
241 OPT_H = 1 << 5, // use external connection helper
242 OPT_S = 1 << 6, // specify connection string
243 OPT_a = 1 << 7, // authentication tokens
Denys Vlasenko5707b522010-12-20 05:12:39 +0100244 OPT_v = 1 << 8, // verbosity
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000245 };
246
247 // init global variables
248 INIT_G();
249
Denys Vlasenkob9f56e82016-09-07 13:16:33 +0200250 // default HOST[:PORT] is $SMTPHOST, or localhost
251 opt_connect = getenv("SMTPHOST");
252 if (!opt_connect)
253 opt_connect = (char *)"127.0.0.1";
254
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000255 // save initial stdin since body is piped!
256 xdup2(STDIN_FILENO, 3);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +0100257 G.fp0 = xfdopen_for_read(3);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000258
259 // parse options
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000260 // N.B. since -H and -S are mutually exclusive they do not interfere in opt_connect
261 // -a is for ssmtp (http://downloads.openwrt.org/people/nico/man/man8/ssmtp.8.html) compatibility,
262 // it is still under development.
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200263 opts = getopt32(argv, "^"
264 "tf:o:iw:+H:S:a:*:v"
265 "\0"
266 // -v is a counter, -H and -S are mutually exclusive, -a is a list
267 "vv:H--S:S--H",
268 &opt_from, NULL,
269 &timeout, &opt_connect, &opt_connect, &list, &verbose
270 );
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000271 //argc -= optind;
272 argv += optind;
273
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000274 // process -a[upm]<token> options
275 if ((opts & OPT_a) && !list)
276 bb_show_usage();
277 while (list) {
278 char *a = (char *) llist_pop(&list);
279 if ('u' == a[0])
280 G.user = xstrdup(a+1);
281 if ('p' == a[0])
282 G.pass = xstrdup(a+1);
283 // N.B. we support only AUTH LOGIN so far
284 //if ('m' == a[0])
285 // G.method = xstrdup(a+1);
286 }
287 // N.B. list == NULL here
Denys Vlasenko76b680c2016-03-30 16:04:37 +0200288 //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 +0000289
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000290 // connect to server
291
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000292 // connection helper ordered? ->
293 if (opts & OPT_H) {
Denys Vlasenkob9f56e82016-09-07 13:16:33 +0200294 const char *delay;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000295 const char *args[] = { "sh", "-c", opt_connect, NULL };
296 // plug it in
297 launch_helper(args);
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200298 // Now:
299 // our stdout will go to helper's stdin,
300 // helper's stdout will be available on our stdin.
301
302 // Wait for initial server message.
303 // If helper (such as openssl) invokes STARTTLS, the initial 220
304 // is swallowed by helper (and not repeated after TLS is initiated).
305 // We will send NOOP cmd to server and check the response.
306 // We should get 220+250 on plain connection, 250 on STARTTLSed session.
307 //
308 // The problem here is some servers delay initial 220 message,
309 // and consider client to be a spammer if it starts sending cmds
310 // before 220 reached it. The code below is unsafe in this regard:
311 // in non-STARTTLSed case, we potentially send NOOP before 220
312 // is sent by server.
Denys Vlasenkob9f56e82016-09-07 13:16:33 +0200313 //
314 // If $SMTP_ANTISPAM_DELAY is set, we pause before sending NOOP.
315 //
316 delay = getenv("SMTP_ANTISPAM_DELAY");
317 if (delay)
318 sleep(atoi(delay));
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200319 code = smtp_check("NOOP", -1);
320 if (code == 220)
321 // we got 220 - this is not STARTTLSed connection,
322 // eat 250 response to our NOOP
323 smtp_check(NULL, 250);
324 else
325 if (code != 250)
326 bb_error_msg_and_die("SMTP init failed");
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000327 } else {
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200328 // vanilla connection
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000329 int fd;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000330 fd = create_and_connect_stream_or_die(opt_connect, 25);
331 // and make ourselves a simple IO filter
332 xmove_fd(fd, STDIN_FILENO);
333 xdup2(STDIN_FILENO, STDOUT_FILENO);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000334
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200335 // Wait for initial server 220 message
336 smtp_check(NULL, 220);
337 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000338
339 // we should start with modern EHLO
Ron Yorston576b1d32012-04-28 17:04:19 +0200340 if (250 != smtp_checkp("EHLO %s", host, -1))
341 smtp_checkp("HELO %s", host, 250);
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000342
343 // perform authentication
344 if (opts & OPT_a) {
345 smtp_check("AUTH LOGIN", 334);
346 // we must read credentials unless they are given via -a[up] options
347 if (!G.user || !G.pass)
348 get_cred_or_die(4);
349 encode_base64(NULL, G.user, NULL);
350 smtp_check("", 334);
351 encode_base64(NULL, G.pass, NULL);
352 smtp_check("", 235);
353 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000354
355 // set sender
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200356 // N.B. we have here a very loosely defined algorythm
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000357 // since sendmail historically offers no means to specify secrets on cmdline.
358 // 1) server can require no authentication ->
359 // we must just provide a (possibly fake) reply address.
360 // 2) server can require AUTH ->
361 // we must provide valid username and password along with a (possibly fake) reply address.
362 // For the sake of security username and password are to be read either from console or from a secured file.
363 // Since reading from console may defeat usability, the solution is either to read from a predefined
364 // file descriptor (e.g. 4), or again from a secured file.
365
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100366 // got no sender address? use auth name, then UID username as a last resort
367 if (!opt_from) {
Kaarle Ritvanen4e03d412014-02-09 09:49:36 +0100368 opt_from = xasprintf("%s@%s",
369 G.user ? G.user : xuid2uname(getuid()),
370 xgethostbyname(host)->h_name);
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100371 }
Kaarle Ritvanen4e03d412014-02-09 09:49:36 +0100372 free(host);
Denys Vlasenko07f417b2014-02-05 15:01:39 +0100373
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000374 smtp_checkp("MAIL FROM:<%s>", opt_from, 250);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000375
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000376 // process message
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000377
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000378 // read recipients from message and add them to those given on cmdline.
379 // this means we scan stdin for To:, Cc:, Bcc: lines until an empty line
380 // and then use the rest of stdin as message body
381 code = 0; // set "analyze headers" mode
382 while ((s = xmalloc_fgetline(G.fp0)) != NULL) {
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200383 dump:
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000384 // put message lines doubling leading dots
385 if (code) {
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000386 // escape leading dots
387 // N.B. this feature is implied even if no -i (-oi) switch given
388 // N.B. we need to escape the leading dot regardless of
389 // whether it is single or not character on the line
390 if ('.' == s[0] /*&& '\0' == s[1] */)
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200391 bb_putchar('.');
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000392 // dump read line
Denys Vlasenko5707b522010-12-20 05:12:39 +0100393 send_r_n(s);
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000394 free(s);
395 continue;
396 }
397
398 // analyze headers
399 // To: or Cc: headers add recipients
Denys Vlasenkoa42f5302013-03-18 18:47:16 +0100400 check_hdr = (0 == strncasecmp("To:", s, 3));
Aaro Koskinen4a732222013-02-25 00:45:11 +0200401 has_to |= check_hdr;
Denys Vlasenko41fea012011-11-18 22:25:35 +0100402 if (opts & OPT_t) {
Aaro Koskinen4a732222013-02-25 00:45:11 +0200403 if (check_hdr || 0 == strncasecmp("Bcc:" + 1, s, 3)) {
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200404 rcptto_list(s+3);
Aaro Koskinen236f2222013-02-25 00:45:10 +0200405 last_hdr = HDR_TOCC;
Denys Vlasenko41fea012011-11-18 22:25:35 +0100406 goto addheader;
407 }
408 // Bcc: header adds blind copy (hidden) recipient
409 if (0 == strncasecmp("Bcc:", s, 4)) {
Aaro Koskinena8ba0a02013-02-25 00:45:09 +0200410 rcptto_list(s+4);
Denys Vlasenko41fea012011-11-18 22:25:35 +0100411 free(s);
Aaro Koskinen236f2222013-02-25 00:45:10 +0200412 last_hdr = HDR_BCC;
Denys Vlasenko41fea012011-11-18 22:25:35 +0100413 continue; // N.B. Bcc: vanishes from headers!
414 }
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200415 }
Aaro Koskinene82bfef2013-02-25 00:45:12 +0200416 check_hdr = (list && isspace(s[0]));
Aaro Koskinen236f2222013-02-25 00:45:10 +0200417 if (strchr(s, ':') || check_hdr) {
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200418 // other headers go verbatim
419 // N.B. RFC2822 2.2.3 "Long Header Fields" allows for headers to occupy several lines.
420 // Continuation is denoted by prefixing additional lines with whitespace(s).
421 // Thanks (stefan.seyfried at googlemail.com) for pointing this out.
Aaro Koskinen236f2222013-02-25 00:45:10 +0200422 if (check_hdr && last_hdr != HDR_OTHER) {
423 rcptto_list(s+1);
424 if (last_hdr == HDR_BCC)
425 continue;
426 // N.B. Bcc: vanishes from headers!
427 } else {
428 last_hdr = HDR_OTHER;
429 }
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200430 addheader:
Vladimir Dronnikov8dbe9bb2009-10-17 03:35:10 +0200431 // N.B. we allow MAX_HEADERS generic headers at most to prevent attacks
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200432 if (MAX_HEADERS && ++nheaders >= MAX_HEADERS)
433 goto bail;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000434 llist_add_to_end(&list, s);
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000435 } else {
Denys Vlasenko34c469a2011-09-18 03:01:49 +0200436 // a line without ":" (an empty line too, by definition) doesn't look like a valid header
437 // so stop "analyze headers" mode
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200438 reenter:
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000439 // put recipients specified on cmdline
Aaro Koskinene82bfef2013-02-25 00:45:12 +0200440 check_hdr = 1;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000441 while (*argv) {
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200442 char *t = sane_address(*argv);
443 rcptto(t);
444 //if (MAX_HEADERS && ++nheaders >= MAX_HEADERS)
445 // goto bail;
Aaro Koskinene82bfef2013-02-25 00:45:12 +0200446 if (!has_to) {
447 const char *hdr;
448
449 if (check_hdr && argv[1])
450 hdr = "To: %s,";
451 else if (check_hdr)
452 hdr = "To: %s";
453 else if (argv[1])
454 hdr = "To: %s," + 3;
455 else
456 hdr = "To: %s" + 3;
Aaro Koskinen4a732222013-02-25 00:45:11 +0200457 llist_add_to_end(&list,
Aaro Koskinene82bfef2013-02-25 00:45:12 +0200458 xasprintf(hdr, t));
459 check_hdr = 0;
460 }
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000461 argv++;
462 }
463 // enter "put message" mode
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200464 // N.B. DATA fails iff no recipients were accepted (or even provided)
465 // in this case just bail out gracefully
466 if (354 != smtp_check("DATA", -1))
467 goto bail;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000468 // dump the headers
469 while (list) {
Denys Vlasenko5707b522010-12-20 05:12:39 +0100470 send_r_n((char *) llist_pop(&list));
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000471 }
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000472 // stop analyzing headers
473 code++;
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200474 // N.B. !s means: we read nothing, and nothing to be read in the future.
475 // just dump empty line and break the loop
476 if (!s) {
Denys Vlasenko5707b522010-12-20 05:12:39 +0100477 send_r_n("");
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200478 break;
479 }
480 // go dump message body
481 // N.B. "s" already contains the first non-header line, so pretend we read it from input
482 goto dump;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000483 }
484 }
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200485 // odd case: we didn't stop "analyze headers" mode -> message body is empty. Reenter the loop
486 // N.B. after reenter code will be > 0
487 if (!code)
488 goto reenter;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000489
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000490 // finalize the message
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000491 smtp_check(".", 250);
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200492 bail:
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000493 // ... and say goodbye
494 smtp_check("QUIT", 221);
495 // cleanup
496 if (ENABLE_FEATURE_CLEAN_UP)
497 fclose(G.fp0);
498
499 return EXIT_SUCCESS;
500}