2008-11-06 23:42:42 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* helper routines
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
|
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2008-11-06 23:42:42 +00:00
|
|
|
*/
|
|
|
|
#include "libbb.h"
|
|
|
|
#include "mail.h"
|
|
|
|
|
|
|
|
static void kill_helper(void)
|
|
|
|
{
|
2009-03-04 17:56:00 +00:00
|
|
|
if (G.helper_pid > 0) {
|
2008-11-06 23:42:42 +00:00
|
|
|
kill(G.helper_pid, SIGTERM);
|
2009-03-04 17:56:00 +00:00
|
|
|
G.helper_pid = 0;
|
|
|
|
}
|
2008-11-06 23:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// generic signal handler
|
|
|
|
static void signal_handler(int signo)
|
|
|
|
{
|
|
|
|
#define err signo
|
|
|
|
if (SIGALRM == signo) {
|
|
|
|
kill_helper();
|
|
|
|
bb_error_msg_and_die("timed out");
|
|
|
|
}
|
|
|
|
|
|
|
|
// SIGCHLD. reap zombies
|
2009-03-04 17:56:00 +00:00
|
|
|
if (safe_waitpid(G.helper_pid, &err, WNOHANG) > 0) {
|
|
|
|
if (WIFSIGNALED(err))
|
|
|
|
bb_error_msg_and_die("helper killed by signal %u", WTERMSIG(err));
|
2008-11-06 23:42:42 +00:00
|
|
|
if (WIFEXITED(err)) {
|
|
|
|
G.helper_pid = 0;
|
|
|
|
if (WEXITSTATUS(err))
|
2009-03-04 17:56:00 +00:00
|
|
|
bb_error_msg_and_die("helper exited (%u)", WEXITSTATUS(err));
|
2008-11-06 23:42:42 +00:00
|
|
|
}
|
2009-03-04 17:56:00 +00:00
|
|
|
}
|
2008-11-06 23:42:42 +00:00
|
|
|
#undef err
|
|
|
|
}
|
|
|
|
|
|
|
|
void FAST_FUNC launch_helper(const char **argv)
|
|
|
|
{
|
|
|
|
// setup vanilla unidirectional pipes interchange
|
2009-03-04 17:56:00 +00:00
|
|
|
int i;
|
2008-11-06 23:42:42 +00:00
|
|
|
int pipes[4];
|
|
|
|
|
|
|
|
xpipe(pipes);
|
2009-03-04 17:56:00 +00:00
|
|
|
xpipe(pipes + 2);
|
|
|
|
|
2009-03-10 16:01:57 +00:00
|
|
|
// NB: handler must be installed before vfork
|
|
|
|
bb_signals(0
|
|
|
|
+ (1 << SIGCHLD)
|
|
|
|
+ (1 << SIGALRM)
|
|
|
|
, signal_handler);
|
|
|
|
|
2010-07-04 13:32:38 +00:00
|
|
|
G.helper_pid = xvfork();
|
2009-03-04 17:56:00 +00:00
|
|
|
|
|
|
|
i = (!G.helper_pid) * 2; // for parent:0, for child:2
|
2011-09-18 01:01:49 +00:00
|
|
|
close(pipes[i + 1]); // 1 or 3 - closing one write end
|
|
|
|
close(pipes[2 - i]); // 2 or 0 - closing one read end
|
|
|
|
xmove_fd(pipes[i], STDIN_FILENO); // 0 or 2 - using other read end
|
|
|
|
xmove_fd(pipes[3 - i], STDOUT_FILENO); // 3 or 1 - using other write end
|
|
|
|
// End result:
|
|
|
|
// parent stdout [3] -> child stdin [2]
|
|
|
|
// child stdout [1] -> parent stdin [0]
|
2009-03-04 17:56:00 +00:00
|
|
|
|
2008-11-06 23:42:42 +00:00
|
|
|
if (!G.helper_pid) {
|
|
|
|
// child: try to execute connection helper
|
2009-03-10 16:01:57 +00:00
|
|
|
// NB: SIGCHLD & SIGALRM revert to SIG_DFL on exec
|
2010-07-03 22:57:03 +00:00
|
|
|
BB_EXECVP_or_die((char**)argv);
|
2008-11-06 23:42:42 +00:00
|
|
|
}
|
2009-03-04 17:56:00 +00:00
|
|
|
|
|
|
|
// parent
|
|
|
|
// check whether child is alive
|
|
|
|
//redundant:signal_handler(SIGCHLD);
|
2008-11-06 23:42:42 +00:00
|
|
|
// child seems OK -> parent goes on
|
|
|
|
atexit(kill_helper);
|
|
|
|
}
|
|
|
|
|
2010-12-20 04:12:39 +00:00
|
|
|
char* FAST_FUNC send_mail_command(const char *fmt, const char *param)
|
2008-11-06 23:42:42 +00:00
|
|
|
{
|
2010-12-20 04:12:39 +00:00
|
|
|
char *msg;
|
2008-11-06 23:42:42 +00:00
|
|
|
if (timeout)
|
|
|
|
alarm(timeout);
|
2010-12-20 04:12:39 +00:00
|
|
|
msg = (char*)fmt;
|
|
|
|
if (fmt) {
|
2008-11-06 23:42:42 +00:00
|
|
|
msg = xasprintf(fmt, param);
|
2010-12-20 04:12:39 +00:00
|
|
|
if (verbose)
|
|
|
|
bb_error_msg("send:'%s'", msg);
|
2008-11-06 23:42:42 +00:00
|
|
|
printf("%s\r\n", msg);
|
|
|
|
}
|
2009-11-02 13:19:51 +00:00
|
|
|
fflush_all();
|
2008-11-06 23:42:42 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: parse_url can modify url[] (despite const), but only if '@' is there
|
|
|
|
/*
|
2010-12-20 04:12:39 +00:00
|
|
|
static char* FAST_FUNC parse_url(char *url, char **user, char **pass)
|
2008-11-06 23:42:42 +00:00
|
|
|
{
|
|
|
|
// parse [user[:pass]@]host
|
|
|
|
// return host
|
|
|
|
char *s = strchr(url, '@');
|
|
|
|
*user = *pass = NULL;
|
|
|
|
if (s) {
|
|
|
|
*s++ = '\0';
|
|
|
|
*user = url;
|
|
|
|
url = s;
|
|
|
|
s = strchr(*user, ':');
|
|
|
|
if (s) {
|
|
|
|
*s++ = '\0';
|
|
|
|
*pass = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol)
|
|
|
|
{
|
|
|
|
enum {
|
2011-11-18 21:25:35 +00:00
|
|
|
SRC_BUF_SIZE = 57, /* This *MUST* be a multiple of 3 */
|
2008-11-06 23:42:42 +00:00
|
|
|
DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
|
|
|
|
};
|
|
|
|
#define src_buf text
|
2010-09-16 16:10:04 +00:00
|
|
|
char src[SRC_BUF_SIZE];
|
2008-11-06 23:42:42 +00:00
|
|
|
FILE *fp = fp;
|
|
|
|
ssize_t len = len;
|
|
|
|
char dst_buf[DST_BUF_SIZE + 1];
|
|
|
|
|
|
|
|
if (fname) {
|
|
|
|
fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : (FILE *)text;
|
2010-09-16 16:10:04 +00:00
|
|
|
src_buf = src;
|
2008-11-06 23:42:42 +00:00
|
|
|
} else if (text) {
|
|
|
|
// though we do not call uuencode(NULL, NULL) explicitly
|
|
|
|
// still we do not want to break things suddenly
|
|
|
|
len = strlen(text);
|
|
|
|
} else
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
size_t size;
|
|
|
|
if (fname) {
|
|
|
|
size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp);
|
|
|
|
if ((ssize_t)size < 0)
|
|
|
|
bb_perror_msg_and_die(bb_msg_read_error);
|
|
|
|
} else {
|
|
|
|
size = len;
|
|
|
|
if (len > SRC_BUF_SIZE)
|
|
|
|
size = SRC_BUF_SIZE;
|
|
|
|
}
|
|
|
|
if (!size)
|
|
|
|
break;
|
|
|
|
// encode the buffer we just read in
|
|
|
|
bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
|
|
|
|
if (fname) {
|
2015-10-07 20:42:45 +00:00
|
|
|
puts(eol);
|
2008-11-06 23:42:42 +00:00
|
|
|
} else {
|
|
|
|
src_buf += size;
|
|
|
|
len -= size;
|
|
|
|
}
|
|
|
|
fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout);
|
|
|
|
}
|
|
|
|
if (fname && NOT_LONE_DASH(fname))
|
|
|
|
fclose(fp);
|
|
|
|
#undef src_buf
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get username and password from a file descriptor
|
|
|
|
*/
|
|
|
|
void FAST_FUNC get_cred_or_die(int fd)
|
|
|
|
{
|
|
|
|
if (isatty(fd)) {
|
2009-03-04 17:56:00 +00:00
|
|
|
G.user = xstrdup(bb_ask(fd, /* timeout: */ 0, "User: "));
|
|
|
|
G.pass = xstrdup(bb_ask(fd, /* timeout: */ 0, "Password: "));
|
2008-11-06 23:42:42 +00:00
|
|
|
} else {
|
2011-05-08 19:21:10 +00:00
|
|
|
G.user = xmalloc_reads(fd, /* maxsize: */ NULL);
|
|
|
|
G.pass = xmalloc_reads(fd, /* maxsize: */ NULL);
|
2008-11-06 23:42:42 +00:00
|
|
|
}
|
2009-03-04 17:56:00 +00:00
|
|
|
if (!G.user || !*G.user || !G.pass)
|
2008-11-06 23:42:42 +00:00
|
|
|
bb_error_msg_and_die("no username or password");
|
|
|
|
}
|