Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * helper routines |
| 4 | * |
| 5 | * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | e440b39 | 2017-08-16 17:45:32 +0200 | [diff] [blame] | 9 | #include <sys/prctl.h> |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
| 11 | #include "mail.h" |
| 12 | |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 13 | // generic signal handler |
| 14 | static void signal_handler(int signo) |
| 15 | { |
| 16 | #define err signo |
| 17 | if (SIGALRM == signo) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 18 | bb_simple_error_msg_and_die("timed out"); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | // SIGCHLD. reap zombies |
Denis Vlasenko | 021de3f | 2009-03-04 17:56:00 +0000 | [diff] [blame] | 22 | if (safe_waitpid(G.helper_pid, &err, WNOHANG) > 0) { |
| 23 | if (WIFSIGNALED(err)) |
| 24 | bb_error_msg_and_die("helper killed by signal %u", WTERMSIG(err)); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 25 | if (WIFEXITED(err)) { |
| 26 | G.helper_pid = 0; |
| 27 | if (WEXITSTATUS(err)) |
Denis Vlasenko | 021de3f | 2009-03-04 17:56:00 +0000 | [diff] [blame] | 28 | bb_error_msg_and_die("helper exited (%u)", WEXITSTATUS(err)); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 29 | } |
Denis Vlasenko | 021de3f | 2009-03-04 17:56:00 +0000 | [diff] [blame] | 30 | } |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 31 | #undef err |
| 32 | } |
| 33 | |
| 34 | void FAST_FUNC launch_helper(const char **argv) |
| 35 | { |
| 36 | // setup vanilla unidirectional pipes interchange |
Denis Vlasenko | 021de3f | 2009-03-04 17:56:00 +0000 | [diff] [blame] | 37 | int i; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 38 | int pipes[4]; |
| 39 | |
| 40 | xpipe(pipes); |
Denis Vlasenko | 021de3f | 2009-03-04 17:56:00 +0000 | [diff] [blame] | 41 | xpipe(pipes + 2); |
| 42 | |
Denis Vlasenko | 4774179 | 2009-03-10 16:01:57 +0000 | [diff] [blame] | 43 | // NB: handler must be installed before vfork |
| 44 | bb_signals(0 |
| 45 | + (1 << SIGCHLD) |
| 46 | + (1 << SIGALRM) |
| 47 | , signal_handler); |
| 48 | |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 49 | G.helper_pid = xvfork(); |
Denis Vlasenko | 021de3f | 2009-03-04 17:56:00 +0000 | [diff] [blame] | 50 | |
| 51 | i = (!G.helper_pid) * 2; // for parent:0, for child:2 |
Denys Vlasenko | 34c469a | 2011-09-18 03:01:49 +0200 | [diff] [blame] | 52 | close(pipes[i + 1]); // 1 or 3 - closing one write end |
| 53 | close(pipes[2 - i]); // 2 or 0 - closing one read end |
| 54 | xmove_fd(pipes[i], STDIN_FILENO); // 0 or 2 - using other read end |
| 55 | xmove_fd(pipes[3 - i], STDOUT_FILENO); // 3 or 1 - using other write end |
| 56 | // End result: |
| 57 | // parent stdout [3] -> child stdin [2] |
| 58 | // child stdout [1] -> parent stdin [0] |
Denis Vlasenko | 021de3f | 2009-03-04 17:56:00 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 60 | if (!G.helper_pid) { |
Denys Vlasenko | e440b39 | 2017-08-16 17:45:32 +0200 | [diff] [blame] | 61 | // child |
| 62 | // if parent dies, get SIGTERM |
| 63 | prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0); |
| 64 | // try to execute connection helper |
Denis Vlasenko | 4774179 | 2009-03-10 16:01:57 +0000 | [diff] [blame] | 65 | // NB: SIGCHLD & SIGALRM revert to SIG_DFL on exec |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 66 | BB_EXECVP_or_die((char**)argv); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 67 | } |
Denis Vlasenko | 021de3f | 2009-03-04 17:56:00 +0000 | [diff] [blame] | 68 | |
Denys Vlasenko | e440b39 | 2017-08-16 17:45:32 +0200 | [diff] [blame] | 69 | // parent goes on |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Denys Vlasenko | b6237c0 | 2020-12-17 12:34:25 +0100 | [diff] [blame^] | 72 | void FAST_FUNC send_r_n(const char *s) |
| 73 | { |
| 74 | if (G.verbose) |
| 75 | bb_error_msg("send:'%s'", s); |
| 76 | printf("%s\r\n", s); |
| 77 | } |
| 78 | |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 79 | char* FAST_FUNC send_mail_command(const char *fmt, const char *param) |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 80 | { |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 81 | char *msg; |
Denys Vlasenko | 25b2680 | 2020-12-17 12:24:50 +0100 | [diff] [blame] | 82 | if (G.timeout) |
| 83 | alarm(G.timeout); |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 84 | msg = (char*)fmt; |
| 85 | if (fmt) { |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 86 | msg = xasprintf(fmt, param); |
Denys Vlasenko | b6237c0 | 2020-12-17 12:34:25 +0100 | [diff] [blame^] | 87 | send_r_n(msg); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 88 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 89 | fflush_all(); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 90 | return msg; |
| 91 | } |
| 92 | |
| 93 | // NB: parse_url can modify url[] (despite const), but only if '@' is there |
| 94 | /* |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 95 | static char* FAST_FUNC parse_url(char *url, char **user, char **pass) |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 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 | |
Denys Vlasenko | 8d634a0 | 2018-07-31 17:30:08 +0200 | [diff] [blame] | 115 | static void encode_n_base64(const char *fname, const char *text, size_t len) |
Raffaello D. Di Napoli | f28b885 | 2018-06-26 19:17:45 -0400 | [diff] [blame] | 116 | { |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 117 | enum { |
Denys Vlasenko | 41fea01 | 2011-11-18 22:25:35 +0100 | [diff] [blame] | 118 | SRC_BUF_SIZE = 57, /* This *MUST* be a multiple of 3 */ |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 119 | DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), |
| 120 | }; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 121 | #define src_buf text |
Denys Vlasenko | c8f9a8d | 2010-09-16 18:10:04 +0200 | [diff] [blame] | 122 | char src[SRC_BUF_SIZE]; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 123 | FILE *fp = fp; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 124 | char dst_buf[DST_BUF_SIZE + 1]; |
| 125 | |
| 126 | if (fname) { |
Denys Vlasenko | 8d634a0 | 2018-07-31 17:30:08 +0200 | [diff] [blame] | 127 | fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : stdin; |
Denys Vlasenko | c8f9a8d | 2010-09-16 18:10:04 +0200 | [diff] [blame] | 128 | src_buf = src; |
Denys Vlasenko | 8d634a0 | 2018-07-31 17:30:08 +0200 | [diff] [blame] | 129 | } |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 130 | |
| 131 | while (1) { |
| 132 | size_t size; |
| 133 | if (fname) { |
| 134 | size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp); |
| 135 | if ((ssize_t)size < 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 136 | bb_simple_perror_msg_and_die(bb_msg_read_error); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 137 | } else { |
| 138 | size = len; |
| 139 | if (len > SRC_BUF_SIZE) |
| 140 | size = SRC_BUF_SIZE; |
| 141 | } |
| 142 | if (!size) |
| 143 | break; |
| 144 | // encode the buffer we just read in |
| 145 | bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64); |
| 146 | if (fname) { |
Denys Vlasenko | 8d634a0 | 2018-07-31 17:30:08 +0200 | [diff] [blame] | 147 | puts(""); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 148 | } else { |
| 149 | src_buf += size; |
| 150 | len -= size; |
| 151 | } |
| 152 | fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout); |
| 153 | } |
| 154 | if (fname && NOT_LONE_DASH(fname)) |
| 155 | fclose(fp); |
| 156 | #undef src_buf |
| 157 | } |
| 158 | |
Denys Vlasenko | 8d634a0 | 2018-07-31 17:30:08 +0200 | [diff] [blame] | 159 | void FAST_FUNC printstr_base64(const char *text) |
| 160 | { |
| 161 | encode_n_base64(NULL, text, strlen(text)); |
| 162 | } |
| 163 | |
| 164 | void FAST_FUNC printbuf_base64(const char *text, unsigned len) |
| 165 | { |
| 166 | encode_n_base64(NULL, text, len); |
| 167 | } |
| 168 | |
| 169 | void FAST_FUNC printfile_base64(const char *fname) |
| 170 | { |
| 171 | encode_n_base64(fname, NULL, 0); |
| 172 | } |
| 173 | |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 174 | /* |
| 175 | * get username and password from a file descriptor |
| 176 | */ |
| 177 | void FAST_FUNC get_cred_or_die(int fd) |
| 178 | { |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 179 | if (isatty(fd)) { |
Denys Vlasenko | 17058a0 | 2018-04-07 15:50:30 +0200 | [diff] [blame] | 180 | G.user = bb_ask_noecho(fd, /* timeout: */ 0, "User: "); |
| 181 | G.pass = bb_ask_noecho(fd, /* timeout: */ 0, "Password: "); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 182 | } else { |
Denys Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame] | 183 | G.user = xmalloc_reads(fd, /* maxsize: */ NULL); |
| 184 | G.pass = xmalloc_reads(fd, /* maxsize: */ NULL); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 185 | } |
Denis Vlasenko | 021de3f | 2009-03-04 17:56:00 +0000 | [diff] [blame] | 186 | if (!G.user || !*G.user || !G.pass) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 187 | bb_simple_error_msg_and_die("no username or password"); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 188 | } |