Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 3 | * bare bones sendmail |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 11 | struct globals { |
| 12 | pid_t helper_pid; |
| 13 | unsigned timeout; |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 14 | FILE *fp0; // initial stdin |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 15 | // arguments for SSL connection helper |
| 16 | const char *xargs[9]; |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 17 | }; |
| 18 | #define G (*ptr_to_globals) |
| 19 | #define helper_pid (G.helper_pid) |
| 20 | #define timeout (G.timeout ) |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 21 | #define fp0 (G.fp0 ) |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 22 | #define xargs (G.xargs ) |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 23 | #define INIT_G() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 24 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 25 | xargs[0] = "openssl"; \ |
| 26 | xargs[1] = "s_client"; \ |
| 27 | xargs[2] = "-quiet"; \ |
| 28 | xargs[3] = "-connect"; \ |
Denis Vlasenko | 3dee8e2 | 2008-06-27 21:24:08 +0000 | [diff] [blame] | 29 | /*xargs[4] = "localhost";*/ \ |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 30 | xargs[5] = "-tls1"; \ |
| 31 | xargs[6] = "-starttls"; \ |
| 32 | xargs[7] = "smtp"; \ |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 33 | } while (0) |
| 34 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 35 | #define opt_connect (xargs[4]) |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 37 | static void uuencode(char *fname, const char *text) |
| 38 | { |
| 39 | enum { |
| 40 | SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */ |
| 41 | DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), |
| 42 | }; |
| 43 | |
| 44 | #define src_buf text |
| 45 | FILE *fp = fp; |
| 46 | ssize_t len = len; |
| 47 | char dst_buf[DST_BUF_SIZE + 1]; |
| 48 | |
| 49 | if (fname) { |
| 50 | fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : fp0; |
| 51 | src_buf = bb_common_bufsiz1; |
| 52 | // N.B. strlen(NULL) segfaults! |
| 53 | } else if (text) { |
| 54 | // though we do not call uuencode(NULL, NULL) explicitly |
| 55 | // still we do not want to break things suddenly |
| 56 | len = strlen(text); |
| 57 | } else |
| 58 | return; |
| 59 | |
| 60 | while (1) { |
| 61 | size_t size; |
| 62 | if (fname) { |
| 63 | size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp); |
| 64 | if ((ssize_t)size < 0) |
| 65 | bb_perror_msg_and_die(bb_msg_read_error); |
| 66 | } else { |
| 67 | size = len; |
| 68 | if (len > SRC_BUF_SIZE) |
| 69 | size = SRC_BUF_SIZE; |
| 70 | } |
| 71 | if (!size) |
| 72 | break; |
| 73 | // encode the buffer we just read in |
| 74 | bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64); |
| 75 | if (fname) { |
| 76 | printf("\r\n"); |
| 77 | } else { |
| 78 | src_buf += size; |
| 79 | len -= size; |
| 80 | } |
| 81 | fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout); |
| 82 | } |
| 83 | if (fname) |
| 84 | fclose(fp); |
| 85 | #undef src_buf |
| 86 | } |
| 87 | |
| 88 | |
| 89 | #if ENABLE_FEATURE_SENDMAIL_SSL |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 90 | static void kill_helper(void) |
| 91 | { |
| 92 | // TODO!!!: is there more elegant way to terminate child on program failure? |
| 93 | if (helper_pid > 0) |
| 94 | kill(helper_pid, SIGTERM); |
| 95 | } |
| 96 | |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 97 | // generic signal handler |
| 98 | static void signal_handler(int signo) |
| 99 | { |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 100 | #define err signo |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 101 | if (SIGALRM == signo) { |
| 102 | kill_helper(); |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 103 | bb_error_msg_and_die("timed out"); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 104 | } |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 105 | |
| 106 | // SIGCHLD. reap zombies |
| 107 | if (wait_any_nohang(&err) > 0) |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 108 | if (WIFEXITED(err)) { |
| 109 | // if (WEXITSTATUS(err)) |
| 110 | bb_error_msg_and_die("child exited (%d)", WEXITSTATUS(err)); |
| 111 | // else |
| 112 | // kill(0, SIGCONT); |
| 113 | } |
Denis Vlasenko | 3dee8e2 | 2008-06-27 21:24:08 +0000 | [diff] [blame] | 114 | #undef err |
| 115 | } |
| 116 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 117 | static void launch_helper(const char **argv) |
| 118 | { |
| 119 | // setup vanilla unidirectional pipes interchange |
| 120 | int idx; |
| 121 | int pipes[4]; |
Denis Vlasenko | 3dee8e2 | 2008-06-27 21:24:08 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 123 | xpipe(pipes); |
| 124 | xpipe(pipes+2); |
Denis Vlasenko | fa0b56d | 2008-07-01 16:09:07 +0000 | [diff] [blame] | 125 | helper_pid = vfork(); |
| 126 | if (helper_pid < 0) |
| 127 | bb_perror_msg_and_die("vfork"); |
Denis Vlasenko | 3dee8e2 | 2008-06-27 21:24:08 +0000 | [diff] [blame] | 128 | idx = (!helper_pid) * 2; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 129 | xdup2(pipes[idx], STDIN_FILENO); |
| 130 | xdup2(pipes[3-idx], STDOUT_FILENO); |
| 131 | if (ENABLE_FEATURE_CLEAN_UP) |
| 132 | for (int i = 4; --i >= 0; ) |
| 133 | if (pipes[i] > STDOUT_FILENO) |
| 134 | close(pipes[i]); |
| 135 | if (!helper_pid) { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 136 | // child: try to execute connection helper |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 137 | BB_EXECVP(*argv, (char **)argv); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 138 | _exit(127); |
| 139 | } |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 140 | // parent: check whether child is alive |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 141 | bb_signals(0 |
| 142 | + (1 << SIGCHLD) |
| 143 | + (1 << SIGALRM) |
| 144 | , signal_handler); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 145 | signal_handler(SIGCHLD); |
| 146 | // child seems OK -> parent goes on |
| 147 | } |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 148 | #else |
| 149 | #define kill_helper() ((void)0) |
| 150 | #define launch_helper(x) bb_error_msg_and_die("no SSL support") |
| 151 | #endif |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 152 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 153 | static const char *command(const char *fmt, const char *param) |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 154 | { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 155 | const char *msg = fmt; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 156 | alarm(timeout); |
| 157 | if (msg) { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 158 | msg = xasprintf(fmt, param); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 159 | printf("%s\r\n", msg); |
| 160 | } |
| 161 | fflush(stdout); |
| 162 | return msg; |
| 163 | } |
| 164 | |
| 165 | static int smtp_checkp(const char *fmt, const char *param, int code) |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 166 | { |
| 167 | char *answer; |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 168 | const char *msg = command(fmt, param); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 169 | // read stdin |
| 170 | // if the string has a form \d\d\d- -- read next string. E.g. EHLO response |
| 171 | // parse first bytes to a number |
| 172 | // if code = -1 then just return this number |
| 173 | // if code != -1 then checks whether the number equals the code |
| 174 | // if not equal -> die saying msg |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 175 | while ((answer = xmalloc_fgetline(stdin)) != NULL) |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 176 | if (strlen(answer) <= 3 || '-' != answer[3]) |
| 177 | break; |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 178 | if (answer) { |
| 179 | int n = atoi(answer); |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 180 | alarm(0); |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 181 | free(answer); |
| 182 | if (-1 == code || n == code) |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 183 | return n; |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 184 | } |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 185 | kill_helper(); |
| 186 | bb_error_msg_and_die("%s failed", msg); |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 189 | static inline int smtp_check(const char *fmt, int code) |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 190 | { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 191 | return smtp_checkp(fmt, NULL, code); |
| 192 | } |
| 193 | |
Denis Vlasenko | a2980c6 | 2008-02-02 17:54:35 +0000 | [diff] [blame] | 194 | // strip argument of bad chars |
| 195 | static char *sane(char *str) |
| 196 | { |
| 197 | char *s = str; |
| 198 | char *p = s; |
| 199 | while (*s) { |
| 200 | if (isalnum(*s) || '_' == *s || '-' == *s || '.' == *s || '@' == *s) { |
| 201 | *p++ = *s; |
| 202 | } |
| 203 | s++; |
| 204 | } |
| 205 | *p = '\0'; |
| 206 | return str; |
| 207 | } |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 208 | |
Denis Vlasenko | 8d07560 | 2008-08-10 20:46:39 +0000 | [diff] [blame] | 209 | // NB: parse_url can modify url[] (despite const), but only if '@' is there |
| 210 | static const char *parse_url(const char *url, const char **user, const char **pass) |
Denis Vlasenko | 6ea75e2 | 2008-06-28 21:46:41 +0000 | [diff] [blame] | 211 | { |
| 212 | // parse [user[:pass]@]host |
| 213 | // return host |
| 214 | char *s = strchr(url, '@'); |
| 215 | *user = *pass = NULL; |
| 216 | if (s) { |
| 217 | *s++ = '\0'; |
| 218 | *user = url; |
| 219 | url = s; |
| 220 | s = strchr(*user, ':'); |
| 221 | if (s) { |
| 222 | *s++ = '\0'; |
| 223 | *pass = s; |
| 224 | } |
| 225 | } |
| 226 | return url; |
| 227 | } |
| 228 | |
Denis Vlasenko | 7354244 | 2008-07-17 19:37:09 +0000 | [diff] [blame] | 229 | static void rcptto(const char *s) |
| 230 | { |
| 231 | smtp_checkp("RCPT TO:<%s>", s, 250); |
| 232 | } |
| 233 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 234 | int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 235 | int sendmail_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 236 | { |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 237 | #if ENABLE_FEATURE_SENDMAIL_MAILX |
Denis Vlasenko | 3dee8e2 | 2008-06-27 21:24:08 +0000 | [diff] [blame] | 238 | llist_t *opt_attachments = NULL; |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 239 | const char *opt_subject; |
| 240 | const char *opt_charset = CONFIG_FEATURE_SENDMAIL_CHARSET; |
| 241 | #if ENABLE_FEATURE_SENDMAIL_MAILXX |
| 242 | llist_t *opt_carboncopies = NULL; |
| 243 | char *opt_errors_to; |
| 244 | #endif |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 245 | #endif |
| 246 | char *opt_from, *opt_fullname; |
Denis Vlasenko | 8d07560 | 2008-08-10 20:46:39 +0000 | [diff] [blame] | 247 | const char *opt_user; |
| 248 | const char *opt_pass; |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 249 | int code; |
| 250 | char *boundary; |
| 251 | llist_t *l; |
| 252 | llist_t *headers = NULL; |
| 253 | char *domain = sane(safe_getdomainname()); |
| 254 | unsigned opts; |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 255 | |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 256 | enum { |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 257 | OPT_w = 1 << 0, // network timeout |
Denis Vlasenko | 6ea75e2 | 2008-06-28 21:46:41 +0000 | [diff] [blame] | 258 | OPT_H = 1 << 1, // [user:password@]server[:port] |
| 259 | OPT_S = 1 << 2, // connect using openssl s_client helper |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 260 | OPT_t = 1 << 3, // read message for recipients |
| 261 | OPT_N = 1 << 4, // request notification |
| 262 | OPT_f = 1 << 5, // sender address |
| 263 | OPT_F = 1 << 6, // sender name, overrides $NAME |
| 264 | OPT_s = 1 << 7, // subject |
| 265 | OPT_j = 1 << 8, // assumed charset |
| 266 | OPT_a = 1 << 9, // attachment(s) |
| 267 | OPT_c = 1 << 10, // carbon copy |
| 268 | OPT_e = 1 << 11, // errors-to address |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 269 | }; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 270 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 271 | // init global variables |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 272 | INIT_G(); |
| 273 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 274 | // save initial stdin since body is piped! |
| 275 | xdup2(STDIN_FILENO, 3); |
| 276 | fp0 = fdopen(3, "r"); |
| 277 | |
| 278 | // parse options |
| 279 | opt_complementary = "w+:a::" USE_FEATURE_SENDMAIL_MAILXX("c::"); |
| 280 | opts = getopt32(argv, |
| 281 | "w:H:St" "N:f:F:" USE_FEATURE_SENDMAIL_MAILX("s:j:a:") USE_FEATURE_SENDMAIL_MAILXX("c:e:") |
| 282 | "X:V:vq:R:O:o:nmL:Iih:GC:B:b:A:" // postfix compat only, ignored |
| 283 | // r:Q:p:M:Dd are candidates from another man page. TODO? |
| 284 | "46E", // ssmtp introduces another quirks. TODO?: -a[upm] (user, pass, method) to be supported |
Denis Vlasenko | 6ea75e2 | 2008-06-28 21:46:41 +0000 | [diff] [blame] | 285 | &timeout /* -w */, &opt_connect /* -H */, |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 286 | NULL, &opt_from, &opt_fullname, |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 287 | USE_FEATURE_SENDMAIL_MAILX(&opt_subject, &opt_charset, &opt_attachments,) |
| 288 | USE_FEATURE_SENDMAIL_MAILXX(&opt_carboncopies, &opt_errors_to,) |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 289 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 290 | ); |
| 291 | //argc -= optind; |
| 292 | argv += optind; |
| 293 | |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 294 | // connect to server |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 295 | // host[:port] not specified ? -> use $SMTPHOST. no $SMTPHOST ? -> use localhost |
Denis Vlasenko | 3dee8e2 | 2008-06-27 21:24:08 +0000 | [diff] [blame] | 296 | if (!(opts & OPT_H)) { |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 297 | opt_connect = getenv("SMTPHOST"); |
Denis Vlasenko | 3dee8e2 | 2008-06-27 21:24:08 +0000 | [diff] [blame] | 298 | if (!opt_connect) |
| 299 | opt_connect = "127.0.0.1"; |
| 300 | } |
Denis Vlasenko | 6ea75e2 | 2008-06-28 21:46:41 +0000 | [diff] [blame] | 301 | // fetch username and password, if any |
| 302 | // NB: parse_url modifies opt_connect[] ONLY if '@' is there. |
| 303 | // Thus "127.0.0.1" won't be modified, an is ok that it is RO. |
Denis Vlasenko | 8d07560 | 2008-08-10 20:46:39 +0000 | [diff] [blame] | 304 | opt_connect = parse_url(opt_connect, &opt_user, &opt_pass); |
Denis Vlasenko | 6ea75e2 | 2008-06-28 21:46:41 +0000 | [diff] [blame] | 305 | |
Denis Vlasenko | c94d356 | 2008-06-30 13:30:21 +0000 | [diff] [blame] | 306 | // username must be defined! |
| 307 | if (!opt_user) { |
| 308 | // N.B. IMHO getenv("USER") can be way easily spoofed! |
| 309 | opt_user = bb_getpwuid(NULL, -1, getuid()); |
| 310 | } |
| 311 | |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 312 | // SSL ordered? -> |
Denis Vlasenko | 6ea75e2 | 2008-06-28 21:46:41 +0000 | [diff] [blame] | 313 | if (opts & OPT_S) { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 314 | // ... use openssl helper |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 315 | launch_helper(xargs); |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 316 | // no SSL ordered? -> |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 317 | } else { |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 318 | // ... make plain connect |
| 319 | int fd = create_and_connect_stream_or_die(opt_connect, 25); |
| 320 | // make ourselves a simple IO filter |
| 321 | // from now we know nothing about network :) |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 322 | xmove_fd(fd, STDIN_FILENO); |
| 323 | xdup2(STDIN_FILENO, STDOUT_FILENO); |
| 324 | } |
| 325 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 326 | // got no sender address? -> use username as a resort |
| 327 | if (!(opts & OPT_f)) { |
| 328 | opt_from = xasprintf("%s@%s", opt_user, domain); |
| 329 | } |
Denis Vlasenko | 3dee8e2 | 2008-06-27 21:24:08 +0000 | [diff] [blame] | 330 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 331 | // introduce to server |
| 332 | |
| 333 | // we didn't use SSL helper? -> |
| 334 | if (!(opts & OPT_S)) { |
| 335 | // ... wait for initial server OK |
| 336 | smtp_check(NULL, 220); |
| 337 | } |
| 338 | |
| 339 | // we should start with modern EHLO |
| 340 | if (250 != smtp_checkp("EHLO %s", domain, -1)) { |
| 341 | smtp_checkp("HELO %s", domain, 250); |
| 342 | } |
| 343 | if (ENABLE_FEATURE_CLEAN_UP) |
| 344 | free(domain); |
| 345 | |
| 346 | // set sender |
| 347 | // NOTE: if password has not been specified |
| 348 | // then no authentication is possible |
| 349 | code = (opt_pass ? -1 : 250); |
| 350 | // first try softly without authentication |
| 351 | while (250 != smtp_checkp("MAIL FROM:<%s>", opt_from, code)) { |
| 352 | // MAIL FROM failed -> authentication needed |
| 353 | if (334 == smtp_check("AUTH LOGIN", -1)) { |
| 354 | uuencode(NULL, opt_user); // opt_user != NULL |
| 355 | smtp_check("", 334); |
| 356 | uuencode(NULL, opt_pass); |
| 357 | smtp_check("", 235); |
Denis Vlasenko | c94d356 | 2008-06-30 13:30:21 +0000 | [diff] [blame] | 358 | } |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 359 | // authenticated OK? -> retry to set sender |
| 360 | // but this time die on failure! |
| 361 | code = 250; |
| 362 | } |
Denis Vlasenko | c94d356 | 2008-06-30 13:30:21 +0000 | [diff] [blame] | 363 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 364 | // recipients specified as arguments |
| 365 | while (*argv) { |
| 366 | char *s = sane(*argv); |
| 367 | // loose test on email address validity |
| 368 | if (strchr(s, '@')) { |
| 369 | rcptto(s); |
| 370 | llist_add_to_end(&headers, xasprintf("To: %s", s)); |
Denis Vlasenko | c94d356 | 2008-06-30 13:30:21 +0000 | [diff] [blame] | 371 | } |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 372 | argv++; |
| 373 | } |
Denis Vlasenko | c94d356 | 2008-06-30 13:30:21 +0000 | [diff] [blame] | 374 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 375 | #if ENABLE_FEATURE_SENDMAIL_MAILXX |
| 376 | // carbon copies recipients specified as -c options |
| 377 | for (l = opt_carboncopies; l; l = l->link) { |
| 378 | char *s = sane(l->data); |
| 379 | // loose test on email address validity |
| 380 | if (strchr(s, '@')) { |
| 381 | rcptto(s); |
| 382 | // TODO: do we ever need to mangle the message? |
| 383 | //llist_add_to_end(&headers, xasprintf("Cc: %s", s)); |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 384 | } |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 385 | } |
| 386 | #endif |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 387 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 388 | // if -t specified or no recipients specified -> read recipients from message |
| 389 | // i.e. scan stdin for To:, Cc:, Bcc: lines ... |
| 390 | // ... and then use the rest of stdin as message body |
| 391 | // N.B. subject read from body can be further overrided with one specified on command line. |
| 392 | // recipients are merged. Bcc: lines are deleted |
| 393 | // N.B. other headers are collected and will be dumped verbatim |
| 394 | if (opts & OPT_t || !headers) { |
| 395 | // fetch recipients and (optionally) subject |
| 396 | char *s; |
| 397 | while ((s = xmalloc_fgetline(fp0)) != NULL) { |
| 398 | if (0 == strncasecmp("To: ", s, 4) || 0 == strncasecmp("Cc: ", s, 4)) { |
| 399 | rcptto(sane(s+4)); |
| 400 | llist_add_to_end(&headers, s); |
| 401 | } else if (0 == strncasecmp("Bcc: ", s, 5)) { |
| 402 | rcptto(sane(s+5)); |
| 403 | free(s); |
| 404 | // N.B. Bcc vanishes from headers! |
| 405 | } else if (0 == strncmp("Subject: ", s, 9)) { |
| 406 | // we read subject -> use it verbatim unless it is specified |
| 407 | // on command line |
| 408 | if (!(opts & OPT_s)) |
Denis Vlasenko | 7354244 | 2008-07-17 19:37:09 +0000 | [diff] [blame] | 409 | llist_add_to_end(&headers, s); |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 410 | else |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 411 | free(s); |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 412 | } else if (s[0]) { |
| 413 | // misc header |
| 414 | llist_add_to_end(&headers, s); |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 415 | } else { |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 416 | free(s); |
| 417 | break; // stop on the first empty line |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 418 | } |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 419 | } |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 420 | } |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 421 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 422 | // enter "put message" mode |
| 423 | smtp_check("DATA", 354); |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 424 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 425 | // put headers we could have preread with -t |
| 426 | for (l = headers; l; l = l->link) { |
| 427 | printf("%s\r\n", l->data); |
| 428 | if (ENABLE_FEATURE_CLEAN_UP) |
| 429 | free(l->data); |
| 430 | } |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 431 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 432 | // put (possibly encoded) subject |
| 433 | if (opts & OPT_j) |
| 434 | sane((char *)opt_charset); |
| 435 | if (opts & OPT_s) { |
| 436 | printf("Subject: "); |
| 437 | if (opts & OPT_j) { |
| 438 | printf("=?%s?B?", opt_charset); |
| 439 | uuencode(NULL, opt_subject); |
| 440 | printf("?="); |
| 441 | } else { |
| 442 | printf("%s", opt_subject); |
| 443 | } |
| 444 | printf("\r\n"); |
| 445 | } |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 446 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 447 | // put sender name, $NAME is the default |
| 448 | if (!(opts & OPT_F)) |
| 449 | opt_fullname = getenv("NAME"); |
| 450 | if (opt_fullname) |
| 451 | printf("From: \"%s\" <%s>\r\n", opt_fullname, opt_from); |
| 452 | |
| 453 | // put notification |
| 454 | if (opts & OPT_N) |
| 455 | printf("Disposition-Notification-To: %s\r\n", opt_from); |
| 456 | |
| 457 | #if ENABLE_FEATURE_SENDMAIL_MAILXX |
| 458 | // put errors recipient |
| 459 | if (opts & OPT_e) |
| 460 | printf("Errors-To: %s\r\n", opt_errors_to); |
| 461 | #endif |
| 462 | |
| 463 | // make a random string -- it will delimit message parts |
| 464 | srand(monotonic_us()); |
| 465 | boundary = xasprintf("%d-%d-%d", rand(), rand(), rand()); |
| 466 | |
| 467 | // put common headers |
| 468 | // TODO: do we really need this? |
| 469 | // printf("Message-ID: <%s>\r\n", boundary); |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 470 | |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 471 | #if ENABLE_FEATURE_SENDMAIL_MAILX |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 472 | // have attachments? -> compose multipart MIME |
| 473 | if (opt_attachments) { |
| 474 | const char *fmt; |
| 475 | const char *p; |
| 476 | char *q; |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 477 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 478 | printf( |
| 479 | "Mime-Version: 1.0\r\n" |
| 480 | "%smultipart/mixed; boundary=\"%s\"\r\n" |
| 481 | , "Content-Type: " |
| 482 | , boundary |
| 483 | ); |
| 484 | |
| 485 | // body is pseudo attachment read from stdin in first turn |
| 486 | llist_add_to(&opt_attachments, (char *)"-"); |
| 487 | |
| 488 | // put body + attachment(s) |
| 489 | // N.B. all these weird things just to be tiny |
| 490 | // by reusing string patterns! |
| 491 | fmt = |
| 492 | "\r\n--%s\r\n" |
| 493 | "%stext/plain; charset=%s\r\n" |
| 494 | "%s%s\r\n" |
| 495 | "%s" |
| 496 | ; |
| 497 | p = opt_charset; |
| 498 | q = (char *)""; |
| 499 | l = opt_attachments; |
| 500 | while (l) { |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 501 | printf( |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 502 | fmt |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 503 | , boundary |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 504 | , "Content-Type: " |
| 505 | , p |
| 506 | , "Content-Disposition: inline" |
| 507 | , q |
| 508 | , "Content-Transfer-Encoding: base64\r\n" |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 509 | ); |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 510 | p = ""; |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 511 | fmt = |
| 512 | "\r\n--%s\r\n" |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 513 | "%sapplication/octet-stream%s\r\n" |
| 514 | "%s; filename=\"%s\"\r\n" |
Denis Vlasenko | 6d52c1e | 2008-02-08 18:24:54 +0000 | [diff] [blame] | 515 | "%s" |
| 516 | ; |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 517 | uuencode(l->data, NULL); |
| 518 | l = l->link; |
| 519 | if (l) |
| 520 | q = bb_get_last_path_component_strip(l->data); |
| 521 | } |
Denis Vlasenko | 17db1a9 | 2008-02-26 21:13:17 +0000 | [diff] [blame] | 522 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 523 | // put message terminator |
| 524 | printf("\r\n--%s--\r\n" "\r\n", boundary); |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 525 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 526 | // no attachments? -> just dump message |
| 527 | } else |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 528 | #endif |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 529 | { |
| 530 | char *s; |
| 531 | // terminate headers |
| 532 | printf("\r\n"); |
| 533 | // put plain text respecting leading dots |
| 534 | while ((s = xmalloc_fgetline(fp0)) != NULL) { |
| 535 | // escape leading dots |
| 536 | // N.B. this feature is implied even if no -i (-oi) switch given |
| 537 | // N.B. we need to escape the leading dot regardless of |
| 538 | // whether it is single or not character on the line |
| 539 | if ('.' == s[0] /*&& '\0' == s[1] */) |
| 540 | printf("."); |
| 541 | // dump read line |
| 542 | printf("%s\r\n", s); |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 543 | } |
Denis Vlasenko | 3dee8e2 | 2008-06-27 21:24:08 +0000 | [diff] [blame] | 544 | } |
Denis Vlasenko | 8195d20 | 2008-02-27 09:39:04 +0000 | [diff] [blame] | 545 | |
Denis Vlasenko | 2b2d977 | 2008-09-28 13:50:57 +0000 | [diff] [blame] | 546 | // leave "put message" mode |
| 547 | smtp_check(".", 250); |
| 548 | // ... and say goodbye |
| 549 | smtp_check("QUIT", 221); |
| 550 | // cleanup |
| 551 | if (ENABLE_FEATURE_CLEAN_UP) |
| 552 | fclose(fp0); |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 553 | |
Denis Vlasenko | bed22a0 | 2008-09-27 14:01:22 +0000 | [diff] [blame] | 554 | return EXIT_SUCCESS; |
Denis Vlasenko | ddd42cb | 2008-01-29 00:59:15 +0000 | [diff] [blame] | 555 | } |