Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * bare bones sendmail |
| 4 | * |
| 5 | * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 9 | |
Denys Vlasenko | d616ab6 | 2011-05-22 03:46:33 +0200 | [diff] [blame] | 10 | //kbuild:lib-$(CONFIG_SENDMAIL) += sendmail.o mail.o |
| 11 | |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 12 | //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" |
| 18 | //usage: "\n -f SENDER Sender (required)" |
| 19 | //usage: "\n -o OPTIONS Various options. -oi implied, others are ignored" |
| 20 | //usage: "\n -i -oi synonym. implied and ignored" |
| 21 | //usage: "\n" |
| 22 | //usage: "\nBusybox specific options:" |
| 23 | //usage: "\n -v Verbose" |
| 24 | //usage: "\n -w SECS Network timeout" |
| 25 | //usage: "\n -H 'PROG ARGS' Run connection helper" |
| 26 | //usage: "\n Examples:" |
| 27 | //usage: "\n -H 'exec openssl s_client -quiet -tls1 -starttls smtp" |
| 28 | //usage: "\n -connect smtp.gmail.com:25' <email.txt" |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 29 | //usage: "\n [4<username_and_passwd.txt | -auUSER -apPASS]" |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 30 | //usage: "\n -H 'exec openssl s_client -quiet -tls1" |
| 31 | //usage: "\n -connect smtp.gmail.com:465' <email.txt" |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 32 | //usage: "\n [4<username_and_passwd.txt | -auUSER -apPASS]" |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 33 | //usage: "\n -S HOST[:PORT] Server" |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 34 | //usage: "\n -auUSER Username for AUTH LOGIN" |
| 35 | //usage: "\n -apPASS Password for AUTH LOGIN" |
| 36 | ////usage: "\n -amMETHOD Authentication method. Ignored. LOGIN is implied" |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 37 | //usage: "\n" |
| 38 | //usage: "\nOther options are silently ignored; -oi -t is implied" |
| 39 | //usage: IF_MAKEMIME( |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 40 | //usage: "\nUse makemime to create emails with attachments" |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 41 | //usage: ) |
| 42 | |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 43 | #include "libbb.h" |
| 44 | #include "mail.h" |
| 45 | |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 46 | // limit maximum allowed number of headers to prevent overflows. |
| 47 | // set to 0 to not limit |
| 48 | #define MAX_HEADERS 256 |
| 49 | |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 50 | static void send_r_n(const char *s) |
| 51 | { |
| 52 | if (verbose) |
| 53 | bb_error_msg("send:'%s'", s); |
| 54 | printf("%s\r\n", s); |
| 55 | } |
| 56 | |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 57 | static int smtp_checkp(const char *fmt, const char *param, int code) |
| 58 | { |
| 59 | char *answer; |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 60 | char *msg = send_mail_command(fmt, param); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 61 | // read stdin |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 62 | // if the string has a form NNN- -- read next string. E.g. EHLO response |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 63 | // parse first bytes to a number |
| 64 | // if code = -1 then just return this number |
| 65 | // if code != -1 then checks whether the number equals the code |
| 66 | // if not equal -> die saying msg |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 67 | while ((answer = xmalloc_fgetline(stdin)) != NULL) { |
Denys Vlasenko | 51d714c | 2010-12-20 12:19:46 +0100 | [diff] [blame] | 68 | if (verbose) |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 69 | bb_error_msg("recv:'%.*s'", (int)(strchrnul(answer, '\r') - answer), answer); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 70 | if (strlen(answer) <= 3 || '-' != answer[3]) |
| 71 | break; |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 72 | free(answer); |
| 73 | } |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 74 | if (answer) { |
| 75 | int n = atoi(answer); |
| 76 | if (timeout) |
| 77 | alarm(0); |
| 78 | free(answer); |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 79 | if (-1 == code || n == code) { |
| 80 | free(msg); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 81 | return n; |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 82 | } |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 83 | } |
| 84 | bb_error_msg_and_die("%s failed", msg); |
| 85 | } |
| 86 | |
| 87 | static int smtp_check(const char *fmt, int code) |
| 88 | { |
| 89 | return smtp_checkp(fmt, NULL, code); |
| 90 | } |
| 91 | |
| 92 | // strip argument of bad chars |
| 93 | static char *sane_address(char *str) |
| 94 | { |
Denys Vlasenko | a42f530 | 2013-03-18 18:47:16 +0100 | [diff] [blame] | 95 | char *s; |
Aaro Koskinen | 14285d1 | 2013-02-25 00:45:06 +0200 | [diff] [blame] | 96 | |
Denys Vlasenko | a42f530 | 2013-03-18 18:47:16 +0100 | [diff] [blame] | 97 | trim(str); |
| 98 | s = str; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 99 | while (*s) { |
Denys Vlasenko | a42f530 | 2013-03-18 18:47:16 +0100 | [diff] [blame] | 100 | if (!isalnum(*s) && !strchr("_-.@", *s)) { |
| 101 | bb_error_msg("bad address '%s'", str); |
| 102 | /* returning "": */ |
| 103 | str[0] = '\0'; |
| 104 | return str; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 105 | } |
| 106 | s++; |
| 107 | } |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 108 | return str; |
| 109 | } |
| 110 | |
Aaro Koskinen | 06ad964 | 2013-02-25 00:45:08 +0200 | [diff] [blame] | 111 | // check for an address inside angle brackets, if not found fall back to normal |
| 112 | static char *angle_address(char *str) |
| 113 | { |
Denys Vlasenko | a42f530 | 2013-03-18 18:47:16 +0100 | [diff] [blame] | 114 | char *s, *e; |
Aaro Koskinen | 06ad964 | 2013-02-25 00:45:08 +0200 | [diff] [blame] | 115 | |
Denys Vlasenko | a42f530 | 2013-03-18 18:47:16 +0100 | [diff] [blame] | 116 | trim(str); |
| 117 | e = last_char_is(str, '>'); |
| 118 | if (e) { |
| 119 | s = strrchr(str, '<'); |
| 120 | if (s) { |
| 121 | *e = '\0'; |
| 122 | str = s + 1; |
| 123 | } |
| 124 | } |
| 125 | return sane_address(str); |
Aaro Koskinen | 06ad964 | 2013-02-25 00:45:08 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 128 | static void rcptto(const char *s) |
| 129 | { |
Aaro Koskinen | 14285d1 | 2013-02-25 00:45:06 +0200 | [diff] [blame] | 130 | if (!*s) |
| 131 | return; |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 132 | // N.B. we don't die if recipient is rejected, for the other recipients may be accepted |
| 133 | if (250 != smtp_checkp("RCPT TO:<%s>", s, -1)) |
| 134 | bb_error_msg("Bad recipient: <%s>", s); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Aaro Koskinen | a8ba0a0 | 2013-02-25 00:45:09 +0200 | [diff] [blame] | 137 | // send to a list of comma separated addresses |
Denys Vlasenko | a42f530 | 2013-03-18 18:47:16 +0100 | [diff] [blame] | 138 | static void rcptto_list(const char *list) |
Aaro Koskinen | a8ba0a0 | 2013-02-25 00:45:09 +0200 | [diff] [blame] | 139 | { |
Denys Vlasenko | a42f530 | 2013-03-18 18:47:16 +0100 | [diff] [blame] | 140 | char *str = xstrdup(list); |
Aaro Koskinen | a8ba0a0 | 2013-02-25 00:45:09 +0200 | [diff] [blame] | 141 | char *s = str; |
| 142 | char prev = 0; |
Denys Vlasenko | a42f530 | 2013-03-18 18:47:16 +0100 | [diff] [blame] | 143 | int in_quote = 0; |
Aaro Koskinen | a8ba0a0 | 2013-02-25 00:45:09 +0200 | [diff] [blame] | 144 | |
Denys Vlasenko | a42f530 | 2013-03-18 18:47:16 +0100 | [diff] [blame] | 145 | while (*s) { |
| 146 | char ch = *s++; |
Aaro Koskinen | a8ba0a0 | 2013-02-25 00:45:09 +0200 | [diff] [blame] | 147 | |
| 148 | if (ch == '"' && prev != '\\') { |
| 149 | in_quote = !in_quote; |
| 150 | } else if (!in_quote && ch == ',') { |
Denys Vlasenko | a42f530 | 2013-03-18 18:47:16 +0100 | [diff] [blame] | 151 | s[-1] = '\0'; |
| 152 | rcptto(angle_address(str)); |
| 153 | str = s; |
Aaro Koskinen | a8ba0a0 | 2013-02-25 00:45:09 +0200 | [diff] [blame] | 154 | } |
| 155 | prev = ch; |
| 156 | } |
| 157 | if (prev != ',') |
Denys Vlasenko | a42f530 | 2013-03-18 18:47:16 +0100 | [diff] [blame] | 158 | rcptto(angle_address(str)); |
Aaro Koskinen | a8ba0a0 | 2013-02-25 00:45:09 +0200 | [diff] [blame] | 159 | free(str); |
| 160 | } |
| 161 | |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 162 | int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 163 | int sendmail_main(int argc UNUSED_PARAM, char **argv) |
| 164 | { |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 165 | char *opt_connect = opt_connect; |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 166 | char *opt_from; |
| 167 | char *s; |
| 168 | llist_t *list = NULL; |
Ron Yorston | 576b1d3 | 2012-04-28 17:04:19 +0200 | [diff] [blame] | 169 | char *host = sane_address(safe_gethostname()); |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 170 | unsigned nheaders = 0; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 171 | int code; |
Aaro Koskinen | 236f222 | 2013-02-25 00:45:10 +0200 | [diff] [blame] | 172 | enum { |
| 173 | HDR_OTHER = 0, |
| 174 | HDR_TOCC, |
| 175 | HDR_BCC, |
| 176 | } last_hdr = 0; |
| 177 | int check_hdr; |
Aaro Koskinen | 4a73222 | 2013-02-25 00:45:11 +0200 | [diff] [blame] | 178 | int has_to = 0; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 179 | |
| 180 | enum { |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 181 | //--- standard options |
| 182 | OPT_t = 1 << 0, // read message for recipients, append them to those on cmdline |
| 183 | OPT_f = 1 << 1, // sender address |
| 184 | OPT_o = 1 << 2, // various options. -oi IMPLIED! others are IGNORED! |
Vladimir Dronnikov | b618dba | 2009-10-04 01:34:54 +0200 | [diff] [blame] | 185 | OPT_i = 1 << 3, // IMPLIED! |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 186 | //--- BB specific options |
Vladimir Dronnikov | b618dba | 2009-10-04 01:34:54 +0200 | [diff] [blame] | 187 | OPT_w = 1 << 4, // network timeout |
| 188 | OPT_H = 1 << 5, // use external connection helper |
| 189 | OPT_S = 1 << 6, // specify connection string |
| 190 | OPT_a = 1 << 7, // authentication tokens |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 191 | OPT_v = 1 << 8, // verbosity |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | // init global variables |
| 195 | INIT_G(); |
| 196 | |
| 197 | // save initial stdin since body is piped! |
| 198 | xdup2(STDIN_FILENO, 3); |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 199 | G.fp0 = xfdopen_for_read(3); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 200 | |
| 201 | // parse options |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 202 | // -v is a counter, -f is required. -H and -S are mutually exclusive, -a is a list |
| 203 | opt_complementary = "vv:f:w+:H--S:S--H:a::"; |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 204 | // N.B. since -H and -S are mutually exclusive they do not interfere in opt_connect |
| 205 | // -a is for ssmtp (http://downloads.openwrt.org/people/nico/man/man8/ssmtp.8.html) compatibility, |
| 206 | // it is still under development. |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 207 | opts = getopt32(argv, "tf:o:iw:H:S:a::v", &opt_from, NULL, |
| 208 | &timeout, &opt_connect, &opt_connect, &list, &verbose); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 209 | //argc -= optind; |
| 210 | argv += optind; |
| 211 | |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 212 | // process -a[upm]<token> options |
| 213 | if ((opts & OPT_a) && !list) |
| 214 | bb_show_usage(); |
| 215 | while (list) { |
| 216 | char *a = (char *) llist_pop(&list); |
| 217 | if ('u' == a[0]) |
| 218 | G.user = xstrdup(a+1); |
| 219 | if ('p' == a[0]) |
| 220 | G.pass = xstrdup(a+1); |
| 221 | // N.B. we support only AUTH LOGIN so far |
| 222 | //if ('m' == a[0]) |
| 223 | // G.method = xstrdup(a+1); |
| 224 | } |
| 225 | // N.B. list == NULL here |
| 226 | //bb_info_msg("OPT[%x] AU[%s], AP[%s], AM[%s], ARGV[%s]", opts, au, ap, am, *argv); |
| 227 | |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 228 | // connect to server |
| 229 | |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 230 | // connection helper ordered? -> |
| 231 | if (opts & OPT_H) { |
| 232 | const char *args[] = { "sh", "-c", opt_connect, NULL }; |
| 233 | // plug it in |
| 234 | launch_helper(args); |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 235 | // Now: |
| 236 | // our stdout will go to helper's stdin, |
| 237 | // helper's stdout will be available on our stdin. |
| 238 | |
| 239 | // Wait for initial server message. |
| 240 | // If helper (such as openssl) invokes STARTTLS, the initial 220 |
| 241 | // is swallowed by helper (and not repeated after TLS is initiated). |
| 242 | // We will send NOOP cmd to server and check the response. |
| 243 | // We should get 220+250 on plain connection, 250 on STARTTLSed session. |
| 244 | // |
| 245 | // The problem here is some servers delay initial 220 message, |
| 246 | // and consider client to be a spammer if it starts sending cmds |
| 247 | // before 220 reached it. The code below is unsafe in this regard: |
| 248 | // in non-STARTTLSed case, we potentially send NOOP before 220 |
| 249 | // is sent by server. |
| 250 | // Ideas? (--delay SECS opt? --assume-starttls-helper opt?) |
| 251 | code = smtp_check("NOOP", -1); |
| 252 | if (code == 220) |
| 253 | // we got 220 - this is not STARTTLSed connection, |
| 254 | // eat 250 response to our NOOP |
| 255 | smtp_check(NULL, 250); |
| 256 | else |
| 257 | if (code != 250) |
| 258 | bb_error_msg_and_die("SMTP init failed"); |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 259 | } else { |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 260 | // vanilla connection |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 261 | int fd; |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 262 | // host[:port] not explicitly specified? -> use $SMTPHOST |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 263 | // no $SMTPHOST? -> use localhost |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 264 | if (!(opts & OPT_S)) { |
| 265 | opt_connect = getenv("SMTPHOST"); |
| 266 | if (!opt_connect) |
| 267 | opt_connect = (char *)"127.0.0.1"; |
| 268 | } |
| 269 | // do connect |
| 270 | fd = create_and_connect_stream_or_die(opt_connect, 25); |
| 271 | // and make ourselves a simple IO filter |
| 272 | xmove_fd(fd, STDIN_FILENO); |
| 273 | xdup2(STDIN_FILENO, STDOUT_FILENO); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 274 | |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 275 | // Wait for initial server 220 message |
| 276 | smtp_check(NULL, 220); |
| 277 | } |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 278 | |
| 279 | // we should start with modern EHLO |
Ron Yorston | 576b1d3 | 2012-04-28 17:04:19 +0200 | [diff] [blame] | 280 | if (250 != smtp_checkp("EHLO %s", host, -1)) |
| 281 | smtp_checkp("HELO %s", host, 250); |
| 282 | free(host); |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 283 | |
| 284 | // perform authentication |
| 285 | if (opts & OPT_a) { |
| 286 | smtp_check("AUTH LOGIN", 334); |
| 287 | // we must read credentials unless they are given via -a[up] options |
| 288 | if (!G.user || !G.pass) |
| 289 | get_cred_or_die(4); |
| 290 | encode_base64(NULL, G.user, NULL); |
| 291 | smtp_check("", 334); |
| 292 | encode_base64(NULL, G.pass, NULL); |
| 293 | smtp_check("", 235); |
| 294 | } |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 295 | |
| 296 | // set sender |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 297 | // N.B. we have here a very loosely defined algorythm |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 298 | // since sendmail historically offers no means to specify secrets on cmdline. |
| 299 | // 1) server can require no authentication -> |
| 300 | // we must just provide a (possibly fake) reply address. |
| 301 | // 2) server can require AUTH -> |
| 302 | // we must provide valid username and password along with a (possibly fake) reply address. |
| 303 | // For the sake of security username and password are to be read either from console or from a secured file. |
| 304 | // Since reading from console may defeat usability, the solution is either to read from a predefined |
| 305 | // file descriptor (e.g. 4), or again from a secured file. |
| 306 | |
| 307 | // got no sender address? -> use system username as a resort |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 308 | // N.B. we marked -f as required option! |
| 309 | //if (!G.user) { |
| 310 | // // N.B. IMHO getenv("USER") can be way easily spoofed! |
| 311 | // G.user = xuid2uname(getuid()); |
| 312 | // opt_from = xasprintf("%s@%s", G.user, domain); |
| 313 | //} |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 314 | smtp_checkp("MAIL FROM:<%s>", opt_from, 250); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 315 | |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 316 | // process message |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 317 | |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 318 | // read recipients from message and add them to those given on cmdline. |
| 319 | // this means we scan stdin for To:, Cc:, Bcc: lines until an empty line |
| 320 | // and then use the rest of stdin as message body |
| 321 | code = 0; // set "analyze headers" mode |
| 322 | while ((s = xmalloc_fgetline(G.fp0)) != NULL) { |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 323 | dump: |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 324 | // put message lines doubling leading dots |
| 325 | if (code) { |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 326 | // escape leading dots |
| 327 | // N.B. this feature is implied even if no -i (-oi) switch given |
| 328 | // N.B. we need to escape the leading dot regardless of |
| 329 | // whether it is single or not character on the line |
| 330 | if ('.' == s[0] /*&& '\0' == s[1] */) |
| 331 | printf("."); |
| 332 | // dump read line |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 333 | send_r_n(s); |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 334 | free(s); |
| 335 | continue; |
| 336 | } |
| 337 | |
| 338 | // analyze headers |
| 339 | // To: or Cc: headers add recipients |
Denys Vlasenko | a42f530 | 2013-03-18 18:47:16 +0100 | [diff] [blame] | 340 | check_hdr = (0 == strncasecmp("To:", s, 3)); |
Aaro Koskinen | 4a73222 | 2013-02-25 00:45:11 +0200 | [diff] [blame] | 341 | has_to |= check_hdr; |
Denys Vlasenko | 41fea01 | 2011-11-18 22:25:35 +0100 | [diff] [blame] | 342 | if (opts & OPT_t) { |
Aaro Koskinen | 4a73222 | 2013-02-25 00:45:11 +0200 | [diff] [blame] | 343 | if (check_hdr || 0 == strncasecmp("Bcc:" + 1, s, 3)) { |
Aaro Koskinen | a8ba0a0 | 2013-02-25 00:45:09 +0200 | [diff] [blame] | 344 | rcptto_list(s+3); |
Aaro Koskinen | 236f222 | 2013-02-25 00:45:10 +0200 | [diff] [blame] | 345 | last_hdr = HDR_TOCC; |
Denys Vlasenko | 41fea01 | 2011-11-18 22:25:35 +0100 | [diff] [blame] | 346 | goto addheader; |
| 347 | } |
| 348 | // Bcc: header adds blind copy (hidden) recipient |
| 349 | if (0 == strncasecmp("Bcc:", s, 4)) { |
Aaro Koskinen | a8ba0a0 | 2013-02-25 00:45:09 +0200 | [diff] [blame] | 350 | rcptto_list(s+4); |
Denys Vlasenko | 41fea01 | 2011-11-18 22:25:35 +0100 | [diff] [blame] | 351 | free(s); |
Aaro Koskinen | 236f222 | 2013-02-25 00:45:10 +0200 | [diff] [blame] | 352 | last_hdr = HDR_BCC; |
Denys Vlasenko | 41fea01 | 2011-11-18 22:25:35 +0100 | [diff] [blame] | 353 | continue; // N.B. Bcc: vanishes from headers! |
| 354 | } |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 355 | } |
Aaro Koskinen | e82bfef | 2013-02-25 00:45:12 +0200 | [diff] [blame] | 356 | check_hdr = (list && isspace(s[0])); |
Aaro Koskinen | 236f222 | 2013-02-25 00:45:10 +0200 | [diff] [blame] | 357 | if (strchr(s, ':') || check_hdr) { |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 358 | // other headers go verbatim |
| 359 | // N.B. RFC2822 2.2.3 "Long Header Fields" allows for headers to occupy several lines. |
| 360 | // Continuation is denoted by prefixing additional lines with whitespace(s). |
| 361 | // Thanks (stefan.seyfried at googlemail.com) for pointing this out. |
Aaro Koskinen | 236f222 | 2013-02-25 00:45:10 +0200 | [diff] [blame] | 362 | if (check_hdr && last_hdr != HDR_OTHER) { |
| 363 | rcptto_list(s+1); |
| 364 | if (last_hdr == HDR_BCC) |
| 365 | continue; |
| 366 | // N.B. Bcc: vanishes from headers! |
| 367 | } else { |
| 368 | last_hdr = HDR_OTHER; |
| 369 | } |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 370 | addheader: |
Vladimir Dronnikov | 8dbe9bb | 2009-10-17 03:35:10 +0200 | [diff] [blame] | 371 | // N.B. we allow MAX_HEADERS generic headers at most to prevent attacks |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 372 | if (MAX_HEADERS && ++nheaders >= MAX_HEADERS) |
| 373 | goto bail; |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 374 | llist_add_to_end(&list, s); |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 375 | } else { |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 376 | // a line without ":" (an empty line too, by definition) doesn't look like a valid header |
| 377 | // so stop "analyze headers" mode |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 378 | reenter: |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 379 | // put recipients specified on cmdline |
Aaro Koskinen | e82bfef | 2013-02-25 00:45:12 +0200 | [diff] [blame] | 380 | check_hdr = 1; |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 381 | while (*argv) { |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 382 | char *t = sane_address(*argv); |
| 383 | rcptto(t); |
| 384 | //if (MAX_HEADERS && ++nheaders >= MAX_HEADERS) |
| 385 | // goto bail; |
Aaro Koskinen | e82bfef | 2013-02-25 00:45:12 +0200 | [diff] [blame] | 386 | if (!has_to) { |
| 387 | const char *hdr; |
| 388 | |
| 389 | if (check_hdr && argv[1]) |
| 390 | hdr = "To: %s,"; |
| 391 | else if (check_hdr) |
| 392 | hdr = "To: %s"; |
| 393 | else if (argv[1]) |
| 394 | hdr = "To: %s," + 3; |
| 395 | else |
| 396 | hdr = "To: %s" + 3; |
Aaro Koskinen | 4a73222 | 2013-02-25 00:45:11 +0200 | [diff] [blame] | 397 | llist_add_to_end(&list, |
Aaro Koskinen | e82bfef | 2013-02-25 00:45:12 +0200 | [diff] [blame] | 398 | xasprintf(hdr, t)); |
| 399 | check_hdr = 0; |
| 400 | } |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 401 | argv++; |
| 402 | } |
| 403 | // enter "put message" mode |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 404 | // N.B. DATA fails iff no recipients were accepted (or even provided) |
| 405 | // in this case just bail out gracefully |
| 406 | if (354 != smtp_check("DATA", -1)) |
| 407 | goto bail; |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 408 | // dump the headers |
| 409 | while (list) { |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 410 | send_r_n((char *) llist_pop(&list)); |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 411 | } |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 412 | // stop analyzing headers |
| 413 | code++; |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 414 | // N.B. !s means: we read nothing, and nothing to be read in the future. |
| 415 | // just dump empty line and break the loop |
| 416 | if (!s) { |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 417 | send_r_n(""); |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 418 | break; |
| 419 | } |
| 420 | // go dump message body |
| 421 | // N.B. "s" already contains the first non-header line, so pretend we read it from input |
| 422 | goto dump; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 425 | // odd case: we didn't stop "analyze headers" mode -> message body is empty. Reenter the loop |
| 426 | // N.B. after reenter code will be > 0 |
| 427 | if (!code) |
| 428 | goto reenter; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 429 | |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 430 | // finalize the message |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 431 | smtp_check(".", 250); |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 432 | bail: |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 433 | // ... and say goodbye |
| 434 | smtp_check("QUIT", 221); |
| 435 | // cleanup |
| 436 | if (ENABLE_FEATURE_CLEAN_UP) |
| 437 | fclose(G.fp0); |
| 438 | |
| 439 | return EXIT_SUCCESS; |
| 440 | } |