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 static void kill_helper(void)
14 // TODO!!!: is there more elegant way to terminate child on program failure?
16 kill(G.helper_pid, SIGTERM);
19 // generic signal handler
20 static void signal_handler(int signo)
23 if (SIGALRM == signo) {
25 bb_error_msg_and_die("timed out");
28 // SIGCHLD. reap zombies
29 if (safe_waitpid(G.helper_pid, &err, WNOHANG) > 0)
33 bb_error_msg_and_die("child exited (%d)", WEXITSTATUS(err));
38 void FAST_FUNC launch_helper(const char **argv)
40 // setup vanilla unidirectional pipes interchange
46 G.helper_pid = vfork();
48 bb_perror_msg_and_die("vfork");
49 idx = (!G.helper_pid) * 2;
50 xdup2(pipes[idx], STDIN_FILENO);
51 xdup2(pipes[3-idx], STDOUT_FILENO);
52 if (ENABLE_FEATURE_CLEAN_UP)
53 for (int i = 4; --i >= 0; )
54 if (pipes[i] > STDOUT_FILENO)
57 // child: try to execute connection helper
58 BB_EXECVP(*argv, (char **)argv);
61 // parent: check whether child is alive
66 signal_handler(SIGCHLD);
67 // child seems OK -> parent goes on
71 const FAST_FUNC char *command(const char *fmt, const char *param)
73 const char *msg = fmt;
77 msg = xasprintf(fmt, param);
78 printf("%s\r\n", msg);
84 // NB: parse_url can modify url[] (despite const), but only if '@' is there
86 static char FAST_FUNC *parse_url(char *url, char **user, char **pass)
88 // parse [user[:pass]@]host
90 char *s = strchr(url, '@');
96 s = strchr(*user, ':');
106 void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol)
109 SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */
110 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
116 char dst_buf[DST_BUF_SIZE + 1];
119 fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : (FILE *)text;
120 src_buf = bb_common_bufsiz1;
121 // N.B. strlen(NULL) segfaults!
123 // though we do not call uuencode(NULL, NULL) explicitly
124 // still we do not want to break things suddenly
132 size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp);
133 if ((ssize_t)size < 0)
134 bb_perror_msg_and_die(bb_msg_read_error);
137 if (len > SRC_BUF_SIZE)
142 // encode the buffer we just read in
143 bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
150 fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout);
152 if (fname && NOT_LONE_DASH(fname))
157 void FAST_FUNC decode_base64(FILE *src_stream, FILE *dst_stream)
169 /* Get next _valid_ character.
170 * global vector bb_uuenc_tbl_base64[] contains this string:
171 * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"
174 ch = fgetc(src_stream);
176 bb_error_msg_and_die(bb_msg_read_error);
178 // - means end of MIME section
181 ungetc(ch, src_stream);
184 table_ptr = strchr(bb_uuenc_tbl_base64, ch);
185 } while (table_ptr == NULL);
187 /* Convert encoded character to decimal */
188 ch = table_ptr - bb_uuenc_tbl_base64;
190 if (*table_ptr == '=') {
191 if (term_count == 0) {
192 translated[count] = '\0';
196 } else if (*table_ptr == '\n') {
197 /* Check for terminating line */
198 if (term_count == 5) {
204 translated[count] = ch;
210 /* Merge 6 bit chars to 8 bit */
212 fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
215 fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
218 fputc(translated[2] << 6 | translated[3], dst_stream);
225 * get username and password from a file descriptor
227 void FAST_FUNC get_cred_or_die(int fd)
231 G.user = xstrdup(bb_ask_stdin("User: "));
232 G.pass = xstrdup(bb_ask_stdin("Password: "));
235 FILE *fp = fdopen(fd, "r");
236 G.user = xmalloc_fgetline(fp);
237 G.pass = xmalloc_fgetline(fp);
240 if (!G.user || !*G.user || !G.pass || !*G.pass)
241 bb_error_msg_and_die("no username or password");