blob: 5eb99e13dae4f757340290eaf765c0d71137fa86 [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 *
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
8 */
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
Denis Vlasenko239d06b2008-11-06 23:42:42 +000057 G.helper_pid = vfork();
58 if (G.helper_pid < 0)
59 bb_perror_msg_and_die("vfork");
Denis Vlasenko021de3f2009-03-04 17:56:00 +000060
61 i = (!G.helper_pid) * 2; // for parent:0, for child:2
62 close(pipes[i + 1]); // 1 or 3 - closing one write end
63 close(pipes[2 - i]); // 2 or 0 - closing one read end
64 xmove_fd(pipes[i], STDIN_FILENO); // 0 or 2 - using other read end
65 xmove_fd(pipes[3 - i], STDOUT_FILENO); // 3 or 1 - other write end
66
Denis Vlasenko239d06b2008-11-06 23:42:42 +000067 if (!G.helper_pid) {
68 // child: try to execute connection helper
Denis Vlasenko47741792009-03-10 16:01:57 +000069 // NB: SIGCHLD & SIGALRM revert to SIG_DFL on exec
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020070 BB_EXECVP_or_die((char**)argv);
Denis Vlasenko239d06b2008-11-06 23:42:42 +000071 }
Denis Vlasenko021de3f2009-03-04 17:56:00 +000072
73 // parent
Denis Vlasenko021de3f2009-03-04 17:56:00 +000074 // check whether child is alive
75 //redundant:signal_handler(SIGCHLD);
Denis Vlasenko239d06b2008-11-06 23:42:42 +000076 // child seems OK -> parent goes on
77 atexit(kill_helper);
78}
79
80const FAST_FUNC char *command(const char *fmt, const char *param)
81{
82 const char *msg = fmt;
83 if (timeout)
84 alarm(timeout);
85 if (msg) {
86 msg = xasprintf(fmt, param);
87 printf("%s\r\n", msg);
88 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +010089 fflush_all();
Denis Vlasenko239d06b2008-11-06 23:42:42 +000090 return msg;
91}
92
93// NB: parse_url can modify url[] (despite const), but only if '@' is there
94/*
95static char FAST_FUNC *parse_url(char *url, char **user, char **pass)
96{
97 // parse [user[:pass]@]host
98 // return host
99 char *s = strchr(url, '@');
100 *user = *pass = NULL;
101 if (s) {
102 *s++ = '\0';
103 *user = url;
104 url = s;
105 s = strchr(*user, ':');
106 if (s) {
107 *s++ = '\0';
108 *pass = s;
109 }
110 }
111 return url;
112}
113*/
114
115void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol)
116{
117 enum {
118 SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */
119 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
120 };
121
122#define src_buf text
123 FILE *fp = fp;
124 ssize_t len = len;
125 char dst_buf[DST_BUF_SIZE + 1];
126
127 if (fname) {
128 fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : (FILE *)text;
129 src_buf = bb_common_bufsiz1;
130 // N.B. strlen(NULL) segfaults!
131 } else if (text) {
132 // though we do not call uuencode(NULL, NULL) explicitly
133 // still we do not want to break things suddenly
134 len = strlen(text);
135 } else
136 return;
137
138 while (1) {
139 size_t size;
140 if (fname) {
141 size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp);
142 if ((ssize_t)size < 0)
143 bb_perror_msg_and_die(bb_msg_read_error);
144 } else {
145 size = len;
146 if (len > SRC_BUF_SIZE)
147 size = SRC_BUF_SIZE;
148 }
149 if (!size)
150 break;
151 // encode the buffer we just read in
152 bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
153 if (fname) {
154 printf("%s\n", eol);
155 } else {
156 src_buf += size;
157 len -= size;
158 }
159 fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout);
160 }
161 if (fname && NOT_LONE_DASH(fname))
162 fclose(fp);
163#undef src_buf
164}
165
166void FAST_FUNC decode_base64(FILE *src_stream, FILE *dst_stream)
167{
168 int term_count = 1;
169
170 while (1) {
171 char translated[4];
172 int count = 0;
173
174 while (count < 4) {
175 char *table_ptr;
176 int ch;
177
178 /* Get next _valid_ character.
179 * global vector bb_uuenc_tbl_base64[] contains this string:
180 * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"
181 */
182 do {
183 ch = fgetc(src_stream);
184 if (ch == EOF) {
185 bb_error_msg_and_die(bb_msg_read_error);
186 }
187 // - means end of MIME section
188 if ('-' == ch) {
189 // push it back
190 ungetc(ch, src_stream);
191 return;
192 }
193 table_ptr = strchr(bb_uuenc_tbl_base64, ch);
194 } while (table_ptr == NULL);
195
196 /* Convert encoded character to decimal */
197 ch = table_ptr - bb_uuenc_tbl_base64;
198
199 if (*table_ptr == '=') {
200 if (term_count == 0) {
201 translated[count] = '\0';
202 break;
203 }
204 term_count++;
205 } else if (*table_ptr == '\n') {
206 /* Check for terminating line */
207 if (term_count == 5) {
208 return;
209 }
210 term_count = 1;
211 continue;
212 } else {
213 translated[count] = ch;
214 count++;
215 term_count = 0;
216 }
217 }
218
219 /* Merge 6 bit chars to 8 bit */
220 if (count > 1) {
221 fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
222 }
223 if (count > 2) {
224 fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
225 }
226 if (count > 3) {
227 fputc(translated[2] << 6 | translated[3], dst_stream);
228 }
229 }
230}
231
232
233/*
234 * get username and password from a file descriptor
235 */
236void FAST_FUNC get_cred_or_die(int fd)
237{
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000238 if (isatty(fd)) {
Denis Vlasenko021de3f2009-03-04 17:56:00 +0000239 G.user = xstrdup(bb_ask(fd, /* timeout: */ 0, "User: "));
240 G.pass = xstrdup(bb_ask(fd, /* timeout: */ 0, "Password: "));
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000241 } else {
Denis Vlasenko021de3f2009-03-04 17:56:00 +0000242 G.user = xmalloc_reads(fd, /* pfx: */ NULL, /* maxsize: */ NULL);
243 G.pass = xmalloc_reads(fd, /* pfx: */ NULL, /* maxsize: */ NULL);
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000244 }
Denis Vlasenko021de3f2009-03-04 17:56:00 +0000245 if (!G.user || !*G.user || !G.pass)
Denis Vlasenko239d06b2008-11-06 23:42:42 +0000246 bb_error_msg_and_die("no username or password");
247}