1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
12 Extracted from BB uuencode.c
15 SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */
16 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
19 static void uuencode(const char *fname)
22 char src_buf[SRC_BUF_SIZE];
23 char dst_buf[1 + DST_BUF_SIZE + 1];
25 fd = xopen(fname, O_RDONLY);
29 size_t size = full_read(fd, src_buf, SRC_BUF_SIZE);
32 if ((ssize_t)size < 0)
33 bb_perror_msg_and_die(bb_msg_read_error);
34 /* Encode the buffer we just read in */
35 bb_uuencode(dst_buf + 1, src_buf, size, bb_uuenc_tbl_base64);
36 xwrite(STDOUT_FILENO, dst_buf, 1 + 4 * ((size + 2) / 3));
42 // encodes content of given buffer instead of fd
43 // used to encode subject and authentication terms
44 static void uuencode_inline(const char *src_buf)
47 char dst_buf[DST_BUF_SIZE + 1];
49 len = strlen(src_buf);
52 size_t chunk = (len <= SRC_BUF_SIZE) ? len : SRC_BUF_SIZE;
53 bb_uuencode(dst_buf, src_buf, chunk, bb_uuenc_tbl_base64);
54 xwrite(STDOUT_FILENO, dst_buf, 4 * ((chunk + 2) / 3));
60 #if ENABLE_FEATURE_SENDMAIL_NETWORK
61 // generic signal handler
62 static void signal_handler(int signo)
67 bb_error_msg_and_die("timed out");
69 // SIGCHLD. reap zombies
70 if (wait_any_nohang(&err) > 0)
71 if (WIFEXITED(err) && WEXITSTATUS(err))
72 bb_error_msg_and_die("child exited (%d)", WEXITSTATUS(err));
75 static pid_t helper_pid = -1;
77 // read stdin, parses first bytes to a number, i.e. server response
78 // if code = -1 then just return this number
79 // if code != -1 then checks whether the number equals the code
80 // if not equal -> die saying errmsg
81 static int check(int code, const char *errmsg)
85 // read a string and match it against the set of available answers
87 answer = xmalloc_getline(stdin);
90 if (-1 == code || n == code) {
95 // TODO!!!: is there more elegant way to terminate child on program failure?
97 kill(helper_pid, SIGTERM);
99 answer = (char*)"EOF";
101 *strchrnul(answer, '\r') = '\0';
102 bb_error_msg_and_die("error at %s: got '%s' instead", errmsg, answer);
105 static int puts_and_check(const char *msg, int code, const char *errmsg)
108 return check(code, errmsg);
112 int sendmail_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
113 int sendmail_main(int argc, char **argv)
115 llist_t *recipients = NULL;
116 llist_t *bodies = NULL;
117 llist_t *attachments = NULL;
121 const char *charset = "utf-8";
122 #if ENABLE_FEATURE_SENDMAIL_NETWORK
123 const char *wsecs = "10";
124 const char *server = "127.0.0.1";
125 const char *port = NULL;
126 const char *opt_user;
127 const char *opt_pass;
133 OPT_f = 1 << 0, // sender
134 OPT_n = 1 << 2, // notification
135 OPT_s = 1 << 3, // subject given
136 OPT_d = 1 << 7, // dry run - no networking
137 OPT_w = 1 << 8, // network timeout
138 OPT_h = 1 << 9, // server
139 OPT_p = 1 << 10, // port
140 OPT_U = 1 << 11, // user specified
141 OPT_P = 1 << 12, // password specified
144 // -f must be specified
145 // -t, -b, -a may be multiple
146 opt_complementary = "f:t::b::a::";
147 opts = getopt32(argv,
148 "f:t:n::s:b:a:c:" USE_FEATURE_SENDMAIL_NETWORK("dw:h:p:U:P:"),
149 &from, &recipients, ¬ify, &subject, &bodies, &attachments, &charset
150 USE_FEATURE_SENDMAIL_NETWORK(, &wsecs, &server, &port, &opt_user, &opt_pass)
155 //printf("OPTS[%4x]\n", opts);
157 // TODO!!!: strip recipients and sender from <>
159 // establish connection
160 #if ENABLE_FEATURE_SENDMAIL_NETWORK
161 timeout = xatou(wsecs);
162 if (!(opts & OPT_d)) {
163 // ask password if we need to and while we're still have terminal
164 // TODO: get password directly from /dev/tty? or from a secret file?
165 if ((opts & (OPT_U+OPT_P)) == OPT_U) {
166 if (!isatty(STDIN_FILENO) || !(opt_pass = bb_askpass(0, "Password: "))) {
167 bb_error_msg_and_die("no password");
170 //printf("OPTS[%4x][%s][%s]\n", opts, opt_user, opt_pass);
176 // if connection helper given
177 // setup vanilla unidirectional pipes interchange
181 helper_pid = vfork();
183 bb_perror_msg_and_die("vfork");
184 xdup2(pipes[(helper_pid)?0:2], STDIN_FILENO);
185 xdup2(pipes[(helper_pid)?3:1], STDOUT_FILENO);
186 if (ENABLE_FEATURE_CLEAN_UP)
187 for (int i = 4; --i >= 0; )
188 if (pipes[i] > STDOUT_FILENO)
190 // replace child with connection helper
192 // child - try to execute connection helper
193 BB_EXECVP(argv[0], argv);
196 // parent - check whether child is alive
197 sig_catch(SIGCHLD, signal_handler);
198 sig_catch(SIGALRM, signal_handler);
199 signal_handler(SIGCHLD);
200 // child seems OK -> parent goes on SMTP chat
202 // no connection helper provided -> make plain connect
203 int fd = create_and_connect_stream_or_die(
205 bb_lookup_port(port, "tcp", 25)
207 xmove_fd(fd, STDIN_FILENO);
208 xdup2(STDIN_FILENO, STDOUT_FILENO);
212 // mail user specified? try modern AUTHentication
213 if (opt_user && (334 == puts_and_check("auth login", -1, "auth login"))) {
214 uuencode_inline(opt_user);
215 puts_and_check("", 334, "AUTH");
216 uuencode_inline(opt_pass);
217 puts_and_check("", 235, "AUTH");
218 // no mail user specified or modern AUTHentication is not supported?
220 // fallback to simple HELO authentication
221 // fetch domain name (defaults to local)
222 const char *domain = strchr(from, '@');
227 printf("helo %s\n", domain);
232 printf("mail from:<%s>\n", from);
233 check(250, "MAIL FROM");
234 for (llist_t *to = recipients; to; to = to->link) {
235 printf("rcpt to:<%s>\n", to->data);
236 check(250, "RCPT TO");
238 puts_and_check("data", 354, "DATA");
239 // no timeout while sending message
245 // put address headers
246 printf("From: %s\n", from);
247 for (llist_t *to = recipients; to; to = to->link) {
248 printf("To: %s\n", to->data);
250 // put encoded subject
252 printf("Subject: =?%s?B?", charset);
253 uuencode_inline(subject);
258 const char *s = notify;
260 s = from; // notify sender by default
261 printf("Disposition-Notification-To: %s\n", s);
263 // put common headers and body start
265 boundary = xasprintf("%d-%d-%d", rand(), rand(), rand());
267 "X-Mailer: busybox " BB_VER " sendmail\n"
270 "Mime-Version: 1.0\n"
271 "Content-Type: multipart/mixed; boundary=\"%s\"\n"
274 "Content-Type: text/plain; charset=%s\n"
276 , boundary, boundary, boundary, charset
277 , "Content-Disposition: inline"
278 , "Content-Transfer-Encoding: base64\n"
281 for (llist_t *f = bodies; f; f = f->link) {
285 for (llist_t *f = attachments; f; f = f->link) {
288 "Content-Type: application/octet-stream\n"
289 "%s; filename=\"%s\"\n"
292 , "Content-Disposition: inline"
293 , bb_get_last_path_component_strip(f->data)
294 , "Content-Transfer-Encoding: base64\n"
299 printf("\n--%s--\n\n", boundary);
300 if (ENABLE_FEATURE_CLEAN_UP)
303 #if ENABLE_FEATURE_SENDMAIL_NETWORK
304 // end message and say goodbye
305 if (!(opts & OPT_d)) {
307 puts_and_check(".", 250, "BODY");
308 puts_and_check("quit", 221, "QUIT");