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 | * |
| 7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 8 | */ |
| 9 | #include "libbb.h" |
| 10 | #include "mail.h" |
| 11 | |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 12 | // limit maximum allowed number of headers to prevent overflows. |
| 13 | // set to 0 to not limit |
| 14 | #define MAX_HEADERS 256 |
| 15 | |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 16 | static int smtp_checkp(const char *fmt, const char *param, int code) |
| 17 | { |
| 18 | char *answer; |
| 19 | const char *msg = command(fmt, param); |
| 20 | // read stdin |
| 21 | // if the string has a form \d\d\d- -- read next string. E.g. EHLO response |
| 22 | // parse first bytes to a number |
| 23 | // if code = -1 then just return this number |
| 24 | // if code != -1 then checks whether the number equals the code |
| 25 | // if not equal -> die saying msg |
| 26 | while ((answer = xmalloc_fgetline(stdin)) != NULL) |
| 27 | if (strlen(answer) <= 3 || '-' != answer[3]) |
| 28 | break; |
| 29 | if (answer) { |
| 30 | int n = atoi(answer); |
| 31 | if (timeout) |
| 32 | alarm(0); |
| 33 | free(answer); |
| 34 | if (-1 == code || n == code) |
| 35 | return n; |
| 36 | } |
| 37 | bb_error_msg_and_die("%s failed", msg); |
| 38 | } |
| 39 | |
| 40 | static int smtp_check(const char *fmt, int code) |
| 41 | { |
| 42 | return smtp_checkp(fmt, NULL, code); |
| 43 | } |
| 44 | |
| 45 | // strip argument of bad chars |
| 46 | static char *sane_address(char *str) |
| 47 | { |
| 48 | char *s = str; |
| 49 | char *p = s; |
| 50 | while (*s) { |
| 51 | if (isalnum(*s) || '_' == *s || '-' == *s || '.' == *s || '@' == *s) { |
| 52 | *p++ = *s; |
| 53 | } |
| 54 | s++; |
| 55 | } |
| 56 | *p = '\0'; |
| 57 | return str; |
| 58 | } |
| 59 | |
| 60 | static void rcptto(const char *s) |
| 61 | { |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 62 | // N.B. we don't die if recipient is rejected, for the other recipients may be accepted |
| 63 | if (250 != smtp_checkp("RCPT TO:<%s>", s, -1)) |
| 64 | bb_error_msg("Bad recipient: <%s>", s); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 68 | int sendmail_main(int argc UNUSED_PARAM, char **argv) |
| 69 | { |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 70 | char *opt_connect = opt_connect; |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 71 | char *opt_from; |
| 72 | char *s; |
| 73 | llist_t *list = NULL; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 74 | char *domain = sane_address(safe_getdomainname()); |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 75 | unsigned nheaders = 0; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 76 | int code; |
| 77 | |
| 78 | enum { |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 79 | //--- standard options |
| 80 | OPT_t = 1 << 0, // read message for recipients, append them to those on cmdline |
| 81 | OPT_f = 1 << 1, // sender address |
| 82 | OPT_o = 1 << 2, // various options. -oi IMPLIED! others are IGNORED! |
Vladimir Dronnikov | b618dba | 2009-10-04 01:34:54 +0200 | [diff] [blame] | 83 | OPT_i = 1 << 3, // IMPLIED! |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 84 | //--- BB specific options |
Vladimir Dronnikov | b618dba | 2009-10-04 01:34:54 +0200 | [diff] [blame] | 85 | OPT_w = 1 << 4, // network timeout |
| 86 | OPT_H = 1 << 5, // use external connection helper |
| 87 | OPT_S = 1 << 6, // specify connection string |
| 88 | OPT_a = 1 << 7, // authentication tokens |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | // init global variables |
| 92 | INIT_G(); |
| 93 | |
| 94 | // save initial stdin since body is piped! |
| 95 | xdup2(STDIN_FILENO, 3); |
| 96 | G.fp0 = fdopen(3, "r"); |
| 97 | |
| 98 | // parse options |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 99 | // -f is required. -H and -S are mutually exclusive |
| 100 | opt_complementary = "f:w+:H--S:S--H:a::"; |
| 101 | // N.B. since -H and -S are mutually exclusive they do not interfere in opt_connect |
| 102 | // -a is for ssmtp (http://downloads.openwrt.org/people/nico/man/man8/ssmtp.8.html) compatibility, |
| 103 | // it is still under development. |
Vladimir Dronnikov | b618dba | 2009-10-04 01:34:54 +0200 | [diff] [blame] | 104 | opts = getopt32(argv, "tf:o:iw:H:S:a::", &opt_from, NULL, &timeout, &opt_connect, &opt_connect, &list); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 105 | //argc -= optind; |
| 106 | argv += optind; |
| 107 | |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 108 | // process -a[upm]<token> options |
| 109 | if ((opts & OPT_a) && !list) |
| 110 | bb_show_usage(); |
| 111 | while (list) { |
| 112 | char *a = (char *) llist_pop(&list); |
| 113 | if ('u' == a[0]) |
| 114 | G.user = xstrdup(a+1); |
| 115 | if ('p' == a[0]) |
| 116 | G.pass = xstrdup(a+1); |
| 117 | // N.B. we support only AUTH LOGIN so far |
| 118 | //if ('m' == a[0]) |
| 119 | // G.method = xstrdup(a+1); |
| 120 | } |
| 121 | // N.B. list == NULL here |
| 122 | //bb_info_msg("OPT[%x] AU[%s], AP[%s], AM[%s], ARGV[%s]", opts, au, ap, am, *argv); |
| 123 | |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 124 | // connect to server |
| 125 | |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 126 | // connection helper ordered? -> |
| 127 | if (opts & OPT_H) { |
| 128 | const char *args[] = { "sh", "-c", opt_connect, NULL }; |
| 129 | // plug it in |
| 130 | launch_helper(args); |
| 131 | // vanilla connection |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 132 | } else { |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 133 | int fd; |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 134 | // host[:port] not explicitly specified? -> use $SMTPHOST |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 135 | // no $SMTPHOST ? -> use localhost |
| 136 | if (!(opts & OPT_S)) { |
| 137 | opt_connect = getenv("SMTPHOST"); |
| 138 | if (!opt_connect) |
| 139 | opt_connect = (char *)"127.0.0.1"; |
| 140 | } |
| 141 | // do connect |
| 142 | fd = create_and_connect_stream_or_die(opt_connect, 25); |
| 143 | // and make ourselves a simple IO filter |
| 144 | xmove_fd(fd, STDIN_FILENO); |
| 145 | xdup2(STDIN_FILENO, STDOUT_FILENO); |
| 146 | } |
| 147 | // N.B. from now we know nothing about network :) |
| 148 | |
| 149 | // wait for initial server OK |
| 150 | // N.B. if we used openssl the initial 220 answer is already swallowed during openssl TLS init procedure |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 151 | // so we need to kick the server to see whether we are ok |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 152 | code = smtp_check("NOOP", -1); |
| 153 | // 220 on plain connection, 250 on openssl-helped TLS session |
| 154 | if (220 == code) |
| 155 | smtp_check(NULL, 250); // reread the code to stay in sync |
| 156 | else if (250 != code) |
| 157 | bb_error_msg_and_die("INIT failed"); |
| 158 | |
| 159 | // we should start with modern EHLO |
| 160 | if (250 != smtp_checkp("EHLO %s", domain, -1)) { |
| 161 | smtp_checkp("HELO %s", domain, 250); |
| 162 | } |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 163 | if (ENABLE_FEATURE_CLEAN_UP) |
| 164 | free(domain); |
| 165 | |
| 166 | // perform authentication |
| 167 | if (opts & OPT_a) { |
| 168 | smtp_check("AUTH LOGIN", 334); |
| 169 | // we must read credentials unless they are given via -a[up] options |
| 170 | if (!G.user || !G.pass) |
| 171 | get_cred_or_die(4); |
| 172 | encode_base64(NULL, G.user, NULL); |
| 173 | smtp_check("", 334); |
| 174 | encode_base64(NULL, G.pass, NULL); |
| 175 | smtp_check("", 235); |
| 176 | } |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 177 | |
| 178 | // set sender |
| 179 | // N.B. we have here a very loosely defined algotythm |
| 180 | // since sendmail historically offers no means to specify secrets on cmdline. |
| 181 | // 1) server can require no authentication -> |
| 182 | // we must just provide a (possibly fake) reply address. |
| 183 | // 2) server can require AUTH -> |
| 184 | // we must provide valid username and password along with a (possibly fake) reply address. |
| 185 | // For the sake of security username and password are to be read either from console or from a secured file. |
| 186 | // Since reading from console may defeat usability, the solution is either to read from a predefined |
| 187 | // file descriptor (e.g. 4), or again from a secured file. |
| 188 | |
| 189 | // got no sender address? -> use system username as a resort |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 190 | // N.B. we marked -f as required option! |
| 191 | //if (!G.user) { |
| 192 | // // N.B. IMHO getenv("USER") can be way easily spoofed! |
| 193 | // G.user = xuid2uname(getuid()); |
| 194 | // opt_from = xasprintf("%s@%s", G.user, domain); |
| 195 | //} |
| 196 | //if (ENABLE_FEATURE_CLEAN_UP) |
| 197 | // free(domain); |
| 198 | smtp_checkp("MAIL FROM:<%s>", opt_from, 250); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 199 | |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 200 | // process message |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 201 | |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 202 | // read recipients from message and add them to those given on cmdline. |
| 203 | // this means we scan stdin for To:, Cc:, Bcc: lines until an empty line |
| 204 | // and then use the rest of stdin as message body |
| 205 | code = 0; // set "analyze headers" mode |
| 206 | while ((s = xmalloc_fgetline(G.fp0)) != NULL) { |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 207 | dump: |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 208 | // put message lines doubling leading dots |
| 209 | if (code) { |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 210 | // escape leading dots |
| 211 | // N.B. this feature is implied even if no -i (-oi) switch given |
| 212 | // N.B. we need to escape the leading dot regardless of |
| 213 | // whether it is single or not character on the line |
| 214 | if ('.' == s[0] /*&& '\0' == s[1] */) |
| 215 | printf("."); |
| 216 | // dump read line |
| 217 | printf("%s\r\n", s); |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 218 | free(s); |
| 219 | continue; |
| 220 | } |
| 221 | |
| 222 | // analyze headers |
| 223 | // To: or Cc: headers add recipients |
| 224 | if (0 == strncasecmp("To: ", s, 4) || 0 == strncasecmp("Bcc: " + 1, s, 4)) { |
| 225 | rcptto(sane_address(s+4)); |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 226 | goto addheader; |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 227 | // Bcc: header adds blind copy (hidden) recipient |
| 228 | } else if (0 == strncasecmp("Bcc: ", s, 5)) { |
| 229 | rcptto(sane_address(s+5)); |
| 230 | free(s); |
| 231 | // N.B. Bcc: vanishes from headers! |
Vladimir Dronnikov | 8dbe9bb | 2009-10-17 03:35:10 +0200 | [diff] [blame] | 232 | |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 233 | // other headers go verbatim |
Vladimir Dronnikov | 8dbe9bb | 2009-10-17 03:35:10 +0200 | [diff] [blame] | 234 | |
| 235 | // N.B. RFC2822 2.2.3 "Long Header Fields" allows for headers to occupy several lines. |
| 236 | // Continuation is denoted by prefixing additional lines with whitespace(s). |
| 237 | // Thanks (stefan.seyfried at googlemail.com) for pointing this out. |
| 238 | } else if (strchr(s, ':') || (list && skip_whitespace(s) != s)) { |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 239 | addheader: |
Vladimir Dronnikov | 8dbe9bb | 2009-10-17 03:35:10 +0200 | [diff] [blame] | 240 | // 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] | 241 | if (MAX_HEADERS && ++nheaders >= MAX_HEADERS) |
| 242 | goto bail; |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 243 | llist_add_to_end(&list, s); |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 244 | // a line without ":" (an empty line too, by definition) doesn't look like a valid header |
| 245 | // so stop "analyze headers" mode |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 246 | } else { |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 247 | reenter: |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 248 | // put recipients specified on cmdline |
| 249 | while (*argv) { |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 250 | char *t = sane_address(*argv); |
| 251 | rcptto(t); |
| 252 | //if (MAX_HEADERS && ++nheaders >= MAX_HEADERS) |
| 253 | // goto bail; |
| 254 | llist_add_to_end(&list, xasprintf("To: %s", t)); |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 255 | argv++; |
| 256 | } |
| 257 | // enter "put message" mode |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 258 | // N.B. DATA fails iff no recipients were accepted (or even provided) |
| 259 | // in this case just bail out gracefully |
| 260 | if (354 != smtp_check("DATA", -1)) |
| 261 | goto bail; |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 262 | // dump the headers |
| 263 | while (list) { |
| 264 | printf("%s\r\n", (char *) llist_pop(&list)); |
| 265 | } |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 266 | // stop analyzing headers |
| 267 | code++; |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 268 | // N.B. !s means: we read nothing, and nothing to be read in the future. |
| 269 | // just dump empty line and break the loop |
| 270 | if (!s) { |
| 271 | puts("\r"); |
| 272 | break; |
| 273 | } |
| 274 | // go dump message body |
| 275 | // N.B. "s" already contains the first non-header line, so pretend we read it from input |
| 276 | goto dump; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 279 | // odd case: we didn't stop "analyze headers" mode -> message body is empty. Reenter the loop |
| 280 | // N.B. after reenter code will be > 0 |
| 281 | if (!code) |
| 282 | goto reenter; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 283 | |
Denis Vlasenko | 88b8f0a | 2009-03-31 23:41:53 +0000 | [diff] [blame] | 284 | // finalize the message |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 285 | smtp_check(".", 250); |
Vladimir Dronnikov | 944d275 | 2009-10-15 23:50:48 +0200 | [diff] [blame] | 286 | bail: |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 287 | // ... and say goodbye |
| 288 | smtp_check("QUIT", 221); |
| 289 | // cleanup |
| 290 | if (ENABLE_FEATURE_CLEAN_UP) |
| 291 | fclose(G.fp0); |
| 292 | |
| 293 | return EXIT_SUCCESS; |
| 294 | } |