blob: 9b4bebce53c0bde7cae74a2c900d9b62b407d158 [file] [log] [blame]
Denis Vlasenko239d06b2008-11-06 23:42:42 +00001/* vi: set sw=4 ts=4: */
2/*
3 * helper routines
4 *
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko239d06b2008-11-06 23:42:42 +00008 */
9#include "libbb.h"
10#include "mail.h"
11
12static void kill_helper(void)
13{
Denis Vlasenko021de3f2009-03-04 17:56:00 +000014 if (G.helper_pid > 0) {
Denis Vlasenko239d06b2008-11-06 23:42:42 +000015 kill(G.helper_pid, SIGTERM);
Denis Vlasenko021de3f2009-03-04 17:56:00 +000016 G.helper_pid = 0;
17 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +000018}
19
20// generic signal handler
21static void signal_handler(int signo)
22{
23#define err signo
24 if (SIGALRM == signo) {
25 kill_helper();
26 bb_error_msg_and_die("timed out");
27 }
28
29 // SIGCHLD. reap zombies
Denis Vlasenko021de3f2009-03-04 17:56:00 +000030 if (safe_waitpid(G.helper_pid, &err, WNOHANG) > 0) {
31 if (WIFSIGNALED(err))
32 bb_error_msg_and_die("helper killed by signal %u", WTERMSIG(err));
Denis Vlasenko239d06b2008-11-06 23:42:42 +000033 if (WIFEXITED(err)) {
34 G.helper_pid = 0;
35 if (WEXITSTATUS(err))
Denis Vlasenko021de3f2009-03-04 17:56:00 +000036 bb_error_msg_and_die("helper exited (%u)", WEXITSTATUS(err));
Denis Vlasenko239d06b2008-11-06 23:42:42 +000037 }
Denis Vlasenko021de3f2009-03-04 17:56:00 +000038 }
Denis Vlasenko239d06b2008-11-06 23:42:42 +000039#undef err
40}
41
42void FAST_FUNC launch_helper(const char **argv)
43{
44 // setup vanilla unidirectional pipes interchange
Denis Vlasenko021de3f2009-03-04 17:56:00 +000045 int i;
Denis Vlasenko239d06b2008-11-06 23:42:42 +000046 int pipes[4];
47
48 xpipe(pipes);
Denis Vlasenko021de3f2009-03-04 17:56:00 +000049 xpipe(pipes + 2);
50
Denis Vlasenko47741792009-03-10 16:01:57 +000051 // NB: handler must be installed before vfork
52 bb_signals(0
53 + (1 << SIGCHLD)
54 + (1 << SIGALRM)
55 , signal_handler);
56
Pascal Bellard926031b2010-07-04 15:32:38 +020057 G.helper_pid = xvfork();
Denis Vlasenko021de3f2009-03-04 17:56:00 +000058
59 i = (!G.helper_pid) * 2; // for parent:0, for child:2
60 close(pipes[i + 1]); // 1 or 3 - closing one write end
61 close(pipes[2 - i]); // 2 or 0 - closing one read end
62 xmove_fd(pipes[i], STDIN_FILENO); // 0 or 2 - using other read end
63 xmove_fd(pipes[3 - i], STDOUT_FILENO); // 3 or 1 - other write end
64
Denis Vlasenko239d06b2008-11-06 23:42:42 +000065 if (!G.helper_pid) {
66 // child: try to execute connection helper
Denis Vlasenko47741792009-03-10 16:01:57 +000067 // NB: SIGCHLD & SIGALRM revert to SIG_DFL on exec
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020068 BB_EXECVP_or_die((char**)argv);
Denis Vlasenko239d06b2008-11-06 23:42:42 +000069 }
Denis Vlasenko021de3f2009-03-04 17:56:00 +000070
71 // parent
Denis Vlasenko021de3f2009-03-04 17:56:00 +000072 // check whether child is alive
73 //redundant:signal_handler(SIGCHLD);
Denis Vlasenko239d06b2008-11-06 23:42:42 +000074 // child seems OK -> parent goes on
75 atexit(kill_helper);
76}
77
78const FAST_FUNC char *command(const char *fmt, const char *param)
79{
80 const char *msg = fmt;
81 if (timeout)
82 alarm(timeout);
83 if (msg) {
84 msg = xasprintf(fmt, param);
85 printf("%s\r\n", msg);
86 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +010087 fflush_all();
Denis Vlasenko239d06b2008-11-06 23:42:42 +000088 return msg;
89}
90
91// NB: parse_url can modify url[] (despite const), but only if '@' is there
92/*
93static char FAST_FUNC *parse_url(char *url, char **user, char **pass)
94{
95 // parse [user[:pass]@]host
96 // return host
97 char *s = strchr(url, '@');
98 *user = *pass = NULL;
99 if (s) {
100 *s++ = '\0';
101 *user = url;
102 url = s;
103 s = strchr(*user, ':');
104 if (s) {
105 *s++ = '\0';
106 *pass = s;
107 }
108 }
109 return url;
110}
111*/
112
113void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol)
114{
115 enum {
116 SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */
117 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
118 };
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000119#define src_buf text
Denys Vlasenkoc8f9a8d2010-09-16 18:10:04 +0200120 char src[SRC_BUF_SIZE];
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000121 FILE *fp = fp;
122 ssize_t len = len;
123 char dst_buf[DST_BUF_SIZE + 1];
124
125 if (fname) {
126 fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : (FILE *)text;
Denys Vlasenkoc8f9a8d2010-09-16 18:10:04 +0200127 src_buf = src;
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000128 } else if (text) {
129 // though we do not call uuencode(NULL, NULL) explicitly
130 // still we do not want to break things suddenly
131 len = strlen(text);
132 } else
133 return;
134
135 while (1) {
136 size_t size;
137 if (fname) {
138 size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp);
139 if ((ssize_t)size < 0)
140 bb_perror_msg_and_die(bb_msg_read_error);
141 } else {
142 size = len;
143 if (len > SRC_BUF_SIZE)
144 size = SRC_BUF_SIZE;
145 }
146 if (!size)
147 break;
148 // encode the buffer we just read in
149 bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
150 if (fname) {
151 printf("%s\n", eol);
152 } else {
153 src_buf += size;
154 len -= size;
155 }
156 fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout);
157 }
158 if (fname && NOT_LONE_DASH(fname))
159 fclose(fp);
160#undef src_buf
161}
162
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000163/*
164 * get username and password from a file descriptor
165 */
166void FAST_FUNC get_cred_or_die(int fd)
167{
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000168 if (isatty(fd)) {
Denis Vlasenko021de3f2009-03-04 17:56:00 +0000169 G.user = xstrdup(bb_ask(fd, /* timeout: */ 0, "User: "));
170 G.pass = xstrdup(bb_ask(fd, /* timeout: */ 0, "Password: "));
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000171 } else {
Denis Vlasenko021de3f2009-03-04 17:56:00 +0000172 G.user = xmalloc_reads(fd, /* pfx: */ NULL, /* maxsize: */ NULL);
173 G.pass = xmalloc_reads(fd, /* pfx: */ NULL, /* maxsize: */ NULL);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000174 }
Denis Vlasenko021de3f2009-03-04 17:56:00 +0000175 if (!G.user || !*G.user || !G.pass)
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000176 bb_error_msg_and_die("no username or password");
177}