blob: ef6e03be6119e5903d9b9c5990ac5bcdea75488a [file] [log] [blame]
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko2b2d9772008-09-28 13:50:57 +00003 * bare bones sendmail
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +00004 *
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 Vlasenko17db1a92008-02-26 21:13:17 +000011struct globals {
12 pid_t helper_pid;
13 unsigned timeout;
Denis Vlasenkobed22a02008-09-27 14:01:22 +000014 FILE *fp0; // initial stdin
Denis Vlasenko17db1a92008-02-26 21:13:17 +000015 // arguments for SSL connection helper
16 const char *xargs[9];
Denis Vlasenko17db1a92008-02-26 21:13:17 +000017};
18#define G (*ptr_to_globals)
19#define helper_pid (G.helper_pid)
20#define timeout (G.timeout )
Denis Vlasenkobed22a02008-09-27 14:01:22 +000021#define fp0 (G.fp0 )
Denis Vlasenko17db1a92008-02-26 21:13:17 +000022#define xargs (G.xargs )
Denis Vlasenko17db1a92008-02-26 21:13:17 +000023#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +000024 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenko8195d202008-02-27 09:39:04 +000025 xargs[0] = "openssl"; \
26 xargs[1] = "s_client"; \
27 xargs[2] = "-quiet"; \
28 xargs[3] = "-connect"; \
Denis Vlasenko3dee8e22008-06-27 21:24:08 +000029 /*xargs[4] = "localhost";*/ \
Denis Vlasenko8195d202008-02-27 09:39:04 +000030 xargs[5] = "-tls1"; \
31 xargs[6] = "-starttls"; \
32 xargs[7] = "smtp"; \
Denis Vlasenko17db1a92008-02-26 21:13:17 +000033} while (0)
34
Denis Vlasenko2b2d9772008-09-28 13:50:57 +000035#define opt_connect (xargs[4])
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +000036
Denis Vlasenkobed22a02008-09-27 14:01:22 +000037static 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 Vlasenko6d52c1e2008-02-08 18:24:54 +000090static 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 Vlasenkoddd42cb2008-01-29 00:59:15 +000097// generic signal handler
98static void signal_handler(int signo)
99{
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000100#define err signo
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000101 if (SIGALRM == signo) {
102 kill_helper();
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000103 bb_error_msg_and_die("timed out");
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000104 }
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000105
106 // SIGCHLD. reap zombies
107 if (wait_any_nohang(&err) > 0)
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000108 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 Vlasenko3dee8e22008-06-27 21:24:08 +0000114#undef err
115}
116
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000117static void launch_helper(const char **argv)
118{
119 // setup vanilla unidirectional pipes interchange
120 int idx;
121 int pipes[4];
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000122
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000123 xpipe(pipes);
124 xpipe(pipes+2);
Denis Vlasenkofa0b56d2008-07-01 16:09:07 +0000125 helper_pid = vfork();
126 if (helper_pid < 0)
127 bb_perror_msg_and_die("vfork");
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000128 idx = (!helper_pid) * 2;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000129 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 Vlasenko8195d202008-02-27 09:39:04 +0000136 // child: try to execute connection helper
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000137 BB_EXECVP(*argv, (char **)argv);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000138 _exit(127);
139 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000140 // parent: check whether child is alive
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000141 bb_signals(0
142 + (1 << SIGCHLD)
143 + (1 << SIGALRM)
144 , signal_handler);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000145 signal_handler(SIGCHLD);
146 // child seems OK -> parent goes on
147}
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000148#else
149#define kill_helper() ((void)0)
150#define launch_helper(x) bb_error_msg_and_die("no SSL support")
151#endif
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000152
Denis Vlasenko8195d202008-02-27 09:39:04 +0000153static const char *command(const char *fmt, const char *param)
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000154{
Denis Vlasenko8195d202008-02-27 09:39:04 +0000155 const char *msg = fmt;
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000156 alarm(timeout);
157 if (msg) {
Denis Vlasenko8195d202008-02-27 09:39:04 +0000158 msg = xasprintf(fmt, param);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000159 printf("%s\r\n", msg);
160 }
161 fflush(stdout);
162 return msg;
163}
164
165static int smtp_checkp(const char *fmt, const char *param, int code)
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000166{
167 char *answer;
Denis Vlasenko8195d202008-02-27 09:39:04 +0000168 const char *msg = command(fmt, param);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000169 // 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 Vlasenko8ee649a2008-03-26 20:04:27 +0000175 while ((answer = xmalloc_fgetline(stdin)) != NULL)
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000176 if (strlen(answer) <= 3 || '-' != answer[3])
177 break;
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000178 if (answer) {
179 int n = atoi(answer);
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000180 alarm(0);
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000181 free(answer);
182 if (-1 == code || n == code)
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000183 return n;
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000184 }
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000185 kill_helper();
186 bb_error_msg_and_die("%s failed", msg);
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000187}
188
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000189static inline int smtp_check(const char *fmt, int code)
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000190{
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000191 return smtp_checkp(fmt, NULL, code);
192}
193
Denis Vlasenkoa2980c62008-02-02 17:54:35 +0000194// strip argument of bad chars
195static 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 Vlasenkoddd42cb2008-01-29 00:59:15 +0000208
Denis Vlasenko8d075602008-08-10 20:46:39 +0000209// NB: parse_url can modify url[] (despite const), but only if '@' is there
210static const char *parse_url(const char *url, const char **user, const char **pass)
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000211{
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 Vlasenko73542442008-07-17 19:37:09 +0000229static void rcptto(const char *s)
230{
231 smtp_checkp("RCPT TO:<%s>", s, 250);
232}
233
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000234int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
235int sendmail_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000236{
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000237#if ENABLE_FEATURE_SENDMAIL_MAILX
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000238 llist_t *opt_attachments = NULL;
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000239 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 Vlasenkobed22a02008-09-27 14:01:22 +0000245#endif
246 char *opt_from, *opt_fullname;
Denis Vlasenko8d075602008-08-10 20:46:39 +0000247 const char *opt_user;
248 const char *opt_pass;
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000249 int code;
250 char *boundary;
251 llist_t *l;
252 llist_t *headers = NULL;
253 char *domain = sane(safe_getdomainname());
254 unsigned opts;
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000255
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000256 enum {
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000257 OPT_w = 1 << 0, // network timeout
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000258 OPT_H = 1 << 1, // [user:password@]server[:port]
259 OPT_S = 1 << 2, // connect using openssl s_client helper
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000260 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 Vlasenkoddd42cb2008-01-29 00:59:15 +0000269 };
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000270
Denis Vlasenko8195d202008-02-27 09:39:04 +0000271 // init global variables
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000272 INIT_G();
273
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000274 // 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 Vlasenko6ea75e22008-06-28 21:46:41 +0000285 &timeout /* -w */, &opt_connect /* -H */,
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000286 NULL, &opt_from, &opt_fullname,
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000287 USE_FEATURE_SENDMAIL_MAILX(&opt_subject, &opt_charset, &opt_attachments,)
288 USE_FEATURE_SENDMAIL_MAILXX(&opt_carboncopies, &opt_errors_to,)
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000289 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000290 );
291 //argc -= optind;
292 argv += optind;
293
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000294 // connect to server
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000295 // host[:port] not specified ? -> use $SMTPHOST. no $SMTPHOST ? -> use localhost
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000296 if (!(opts & OPT_H)) {
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000297 opt_connect = getenv("SMTPHOST");
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000298 if (!opt_connect)
299 opt_connect = "127.0.0.1";
300 }
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000301 // 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 Vlasenko8d075602008-08-10 20:46:39 +0000304 opt_connect = parse_url(opt_connect, &opt_user, &opt_pass);
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000305
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000306 // 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 Vlasenko8195d202008-02-27 09:39:04 +0000312 // SSL ordered? ->
Denis Vlasenko6ea75e22008-06-28 21:46:41 +0000313 if (opts & OPT_S) {
Denis Vlasenko8195d202008-02-27 09:39:04 +0000314 // ... use openssl helper
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000315 launch_helper(xargs);
Denis Vlasenko8195d202008-02-27 09:39:04 +0000316 // no SSL ordered? ->
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000317 } else {
Denis Vlasenko8195d202008-02-27 09:39:04 +0000318 // ... 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 Vlasenko6d52c1e2008-02-08 18:24:54 +0000322 xmove_fd(fd, STDIN_FILENO);
323 xdup2(STDIN_FILENO, STDOUT_FILENO);
324 }
325
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000326 // 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 Vlasenko3dee8e22008-06-27 21:24:08 +0000330
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000331 // 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 Vlasenkoc94d3562008-06-30 13:30:21 +0000358 }
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000359 // authenticated OK? -> retry to set sender
360 // but this time die on failure!
361 code = 250;
362 }
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000363
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000364 // 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 Vlasenkoc94d3562008-06-30 13:30:21 +0000371 }
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000372 argv++;
373 }
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000374
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000375#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 Vlasenkoddd42cb2008-01-29 00:59:15 +0000384 }
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000385 }
386#endif
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000387
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000388 // 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 Vlasenko73542442008-07-17 19:37:09 +0000409 llist_add_to_end(&headers, s);
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000410 else
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000411 free(s);
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000412 } else if (s[0]) {
413 // misc header
414 llist_add_to_end(&headers, s);
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000415 } else {
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000416 free(s);
417 break; // stop on the first empty line
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000418 }
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000419 }
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000420 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000421
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000422 // enter "put message" mode
423 smtp_check("DATA", 354);
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000424
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000425 // 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 Vlasenko8195d202008-02-27 09:39:04 +0000431
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000432 // 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 Vlasenko8195d202008-02-27 09:39:04 +0000446
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000447 // 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 Vlasenko17db1a92008-02-26 21:13:17 +0000470
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000471#if ENABLE_FEATURE_SENDMAIL_MAILX
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000472 // have attachments? -> compose multipart MIME
473 if (opt_attachments) {
474 const char *fmt;
475 const char *p;
476 char *q;
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000477
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000478 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 Vlasenko6d52c1e2008-02-08 18:24:54 +0000501 printf(
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000502 fmt
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000503 , boundary
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000504 , "Content-Type: "
505 , p
506 , "Content-Disposition: inline"
507 , q
508 , "Content-Transfer-Encoding: base64\r\n"
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000509 );
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000510 p = "";
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000511 fmt =
512 "\r\n--%s\r\n"
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000513 "%sapplication/octet-stream%s\r\n"
514 "%s; filename=\"%s\"\r\n"
Denis Vlasenko6d52c1e2008-02-08 18:24:54 +0000515 "%s"
516 ;
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000517 uuencode(l->data, NULL);
518 l = l->link;
519 if (l)
520 q = bb_get_last_path_component_strip(l->data);
521 }
Denis Vlasenko17db1a92008-02-26 21:13:17 +0000522
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000523 // put message terminator
524 printf("\r\n--%s--\r\n" "\r\n", boundary);
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000525
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000526 // no attachments? -> just dump message
527 } else
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000528#endif
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000529 {
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 Vlasenkobed22a02008-09-27 14:01:22 +0000543 }
Denis Vlasenko3dee8e22008-06-27 21:24:08 +0000544 }
Denis Vlasenko8195d202008-02-27 09:39:04 +0000545
Denis Vlasenko2b2d9772008-09-28 13:50:57 +0000546 // 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 Vlasenkoddd42cb2008-01-29 00:59:15 +0000553
Denis Vlasenkobed22a02008-09-27 14:01:22 +0000554 return EXIT_SUCCESS;
Denis Vlasenkoddd42cb2008-01-29 00:59:15 +0000555}