blob: 2f99df6c06fb04ee95c695d11fe85fa64bbe6c54 [file] [log] [blame]
Denis Vlasenko239d06b2008-11-06 23:42:42 +00001/* vi: set sw=4 ts=4: */
2/*
3 * bare bones sendmail
4 *
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
6 *
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
8 */
9#include "libbb.h"
10#include "mail.h"
11
Vladimir Dronnikov944d2752009-10-15 23:50:48 +020012// limit maximum allowed number of headers to prevent overflows.
13// set to 0 to not limit
14#define MAX_HEADERS 256
15
Denis Vlasenko239d06b2008-11-06 23:42:42 +000016static 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
40static int smtp_check(const char *fmt, int code)
41{
42 return smtp_checkp(fmt, NULL, code);
43}
44
45// strip argument of bad chars
46static 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
60static void rcptto(const char *s)
61{
Vladimir Dronnikov944d2752009-10-15 23:50:48 +020062 // 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 Vlasenko239d06b2008-11-06 23:42:42 +000065}
66
67int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
68int sendmail_main(int argc UNUSED_PARAM, char **argv)
69{
Denis Vlasenko239d06b2008-11-06 23:42:42 +000070 char *opt_connect = opt_connect;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +000071 char *opt_from;
72 char *s;
73 llist_t *list = NULL;
Denis Vlasenko239d06b2008-11-06 23:42:42 +000074 char *domain = sane_address(safe_getdomainname());
Vladimir Dronnikov944d2752009-10-15 23:50:48 +020075 unsigned nheaders = 0;
Denis Vlasenko239d06b2008-11-06 23:42:42 +000076 int code;
77
78 enum {
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +000079 //--- 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 Dronnikovb618dba2009-10-04 01:34:54 +020083 OPT_i = 1 << 3, // IMPLIED!
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +000084 //--- BB specific options
Vladimir Dronnikovb618dba2009-10-04 01:34:54 +020085 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 Vlasenko239d06b2008-11-06 23:42:42 +000089 };
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 Vlasenko88b8f0a2009-03-31 23:41:53 +000099 // -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 Dronnikovb618dba2009-10-04 01:34:54 +0200104 opts = getopt32(argv, "tf:o:iw:H:S:a::", &opt_from, NULL, &timeout, &opt_connect, &opt_connect, &list);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000105 //argc -= optind;
106 argv += optind;
107
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000108 // 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 Vlasenko239d06b2008-11-06 23:42:42 +0000124 // connect to server
125
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000126 // 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 Vlasenko88b8f0a2009-03-31 23:41:53 +0000132 } else {
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000133 int fd;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000134 // host[:port] not explicitly specified? -> use $SMTPHOST
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000135 // 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 Vlasenko88b8f0a2009-03-31 23:41:53 +0000151 // so we need to kick the server to see whether we are ok
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000152 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 Vlasenko88b8f0a2009-03-31 23:41:53 +0000163 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 Vlasenko239d06b2008-11-06 23:42:42 +0000177
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 Vlasenko88b8f0a2009-03-31 23:41:53 +0000190 // 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 Vlasenko239d06b2008-11-06 23:42:42 +0000199
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000200 // process message
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000201
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000202 // 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 Dronnikov944d2752009-10-15 23:50:48 +0200207 dump:
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000208 // put message lines doubling leading dots
209 if (code) {
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000210 // 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 Vlasenko88b8f0a2009-03-31 23:41:53 +0000218 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 Dronnikov944d2752009-10-15 23:50:48 +0200226 goto addheader;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000227 // 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 Dronnikov8dbe9bb2009-10-17 03:35:10 +0200232
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000233 // other headers go verbatim
Vladimir Dronnikov8dbe9bb2009-10-17 03:35:10 +0200234
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 Dronnikov944d2752009-10-15 23:50:48 +0200239 addheader:
Vladimir Dronnikov8dbe9bb2009-10-17 03:35:10 +0200240 // N.B. we allow MAX_HEADERS generic headers at most to prevent attacks
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200241 if (MAX_HEADERS && ++nheaders >= MAX_HEADERS)
242 goto bail;
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000243 llist_add_to_end(&list, s);
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200244 // a line without ":" (an empty line too, by definition) doesn't look like a valid header
245 // so stop "analyze headers" mode
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000246 } else {
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200247 reenter:
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000248 // put recipients specified on cmdline
249 while (*argv) {
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200250 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 Vlasenko88b8f0a2009-03-31 23:41:53 +0000255 argv++;
256 }
257 // enter "put message" mode
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200258 // 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 Vlasenko88b8f0a2009-03-31 23:41:53 +0000262 // dump the headers
263 while (list) {
264 printf("%s\r\n", (char *) llist_pop(&list));
265 }
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000266 // stop analyzing headers
267 code++;
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200268 // 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 Vlasenko239d06b2008-11-06 23:42:42 +0000277 }
278 }
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200279 // 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 Vlasenko239d06b2008-11-06 23:42:42 +0000283
Denis Vlasenko88b8f0a2009-03-31 23:41:53 +0000284 // finalize the message
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000285 smtp_check(".", 250);
Vladimir Dronnikov944d2752009-10-15 23:50:48 +0200286 bail:
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000287 // ... 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}