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 | */ |
Alex Samorukov | 4455cff | 2021-01-04 01:18:44 +0100 | [diff] [blame] | 9 | #if defined(__linux__) |
| 10 | # include <sys/prctl.h> |
| 11 | # define PRCTL |
| 12 | #elif defined(__FreeBSD__) |
| 13 | # include <sys/procctl.h> |
| 14 | # define PROCCTL |
| 15 | #endif |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 16 | #include "libbb.h" |
| 17 | #include "mail.h" |
| 18 | |
Denys Vlasenko | b0e7cb4 | 2020-12-17 12:51:58 +0100 | [diff] [blame] | 19 | // common signal handler |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 20 | static void signal_handler(int signo) |
| 21 | { |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 22 | if (SIGALRM == signo) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 23 | bb_simple_error_msg_and_die("timed out"); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Denys Vlasenko | b0e7cb4 | 2020-12-17 12:51:58 +0100 | [diff] [blame] | 26 | // SIGCHLD. reap the zombie if we expect one |
| 27 | if (G.helper_pid == 0) |
| 28 | return; |
| 29 | #define status signo |
| 30 | if (safe_waitpid(G.helper_pid, &status, WNOHANG) > 0) { |
| 31 | G.helper_pid = 0; |
| 32 | if (WIFSIGNALED(status)) |
| 33 | bb_error_msg_and_die("helper killed by signal %u", WTERMSIG(status)); |
| 34 | if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
| 35 | bb_error_msg_and_die("helper exited (%u)", WEXITSTATUS(status)); |
Denis Vlasenko | 021de3f | 2009-03-04 17:56:00 +0000 | [diff] [blame] | 36 | } |
Denys Vlasenko | b0e7cb4 | 2020-12-17 12:51:58 +0100 | [diff] [blame] | 37 | #undef status |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void FAST_FUNC launch_helper(const char **argv) |
| 41 | { |
Denys Vlasenko | b0e7cb4 | 2020-12-17 12:51:58 +0100 | [diff] [blame] | 42 | pid_t pid; |
| 43 | struct fd_pair child_out; |
| 44 | struct fd_pair child_in; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 45 | |
Denys Vlasenko | b0e7cb4 | 2020-12-17 12:51:58 +0100 | [diff] [blame] | 46 | xpiped_pair(child_out); |
| 47 | xpiped_pair(child_in); |
Denis Vlasenko | 021de3f | 2009-03-04 17:56:00 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | 4774179 | 2009-03-10 16:01:57 +0000 | [diff] [blame] | 49 | // NB: handler must be installed before vfork |
| 50 | bb_signals(0 |
| 51 | + (1 << SIGCHLD) |
| 52 | + (1 << SIGALRM) |
| 53 | , signal_handler); |
| 54 | |
Denys Vlasenko | cd48f07 | 2020-12-17 12:55:22 +0100 | [diff] [blame] | 55 | fflush_all(); |
| 56 | pid = xvfork(); |
Denys Vlasenko | b0e7cb4 | 2020-12-17 12:51:58 +0100 | [diff] [blame] | 57 | if (pid == 0) { |
Denys Vlasenko | e440b39 | 2017-08-16 17:45:32 +0200 | [diff] [blame] | 58 | // child |
Denys Vlasenko | b0e7cb4 | 2020-12-17 12:51:58 +0100 | [diff] [blame] | 59 | close(child_in.wr); |
| 60 | close(child_out.rd); |
| 61 | xmove_fd(child_in.rd, STDIN_FILENO); |
| 62 | xmove_fd(child_out.wr, STDOUT_FILENO); |
Denys Vlasenko | e440b39 | 2017-08-16 17:45:32 +0200 | [diff] [blame] | 63 | // if parent dies, get SIGTERM |
Alex Samorukov | 4455cff | 2021-01-04 01:18:44 +0100 | [diff] [blame] | 64 | #if defined(PRCTL) |
Denys Vlasenko | e440b39 | 2017-08-16 17:45:32 +0200 | [diff] [blame] | 65 | prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0); |
Alex Samorukov | 4455cff | 2021-01-04 01:18:44 +0100 | [diff] [blame] | 66 | #elif defined(PROCCTL) |
| 67 | { |
| 68 | int signum = SIGTERM; |
| 69 | procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signum); |
| 70 | } |
| 71 | #endif |
Denys Vlasenko | e440b39 | 2017-08-16 17:45:32 +0200 | [diff] [blame] | 72 | // try to execute connection helper |
Denis Vlasenko | 4774179 | 2009-03-10 16:01:57 +0000 | [diff] [blame] | 73 | // NB: SIGCHLD & SIGALRM revert to SIG_DFL on exec |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 74 | BB_EXECVP_or_die((char**)argv); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 75 | } |
Denys Vlasenko | cd48f07 | 2020-12-17 12:55:22 +0100 | [diff] [blame] | 76 | G.helper_pid = pid; |
Denys Vlasenko | b0e7cb4 | 2020-12-17 12:51:58 +0100 | [diff] [blame] | 77 | close(child_out.wr); |
| 78 | close(child_in.rd); |
| 79 | xmove_fd(child_out.rd, STDIN_FILENO); |
| 80 | xmove_fd(child_in.wr, STDOUT_FILENO); |
Denis Vlasenko | 021de3f | 2009-03-04 17:56:00 +0000 | [diff] [blame] | 81 | |
Denys Vlasenko | e440b39 | 2017-08-16 17:45:32 +0200 | [diff] [blame] | 82 | // parent goes on |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Denys Vlasenko | b6237c0 | 2020-12-17 12:34:25 +0100 | [diff] [blame] | 85 | void FAST_FUNC send_r_n(const char *s) |
| 86 | { |
| 87 | if (G.verbose) |
| 88 | bb_error_msg("send:'%s'", s); |
| 89 | printf("%s\r\n", s); |
| 90 | } |
| 91 | |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 92 | char* FAST_FUNC send_mail_command(const char *fmt, const char *param) |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 93 | { |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 94 | char *msg; |
Denys Vlasenko | 25b2680 | 2020-12-17 12:24:50 +0100 | [diff] [blame] | 95 | if (G.timeout) |
| 96 | alarm(G.timeout); |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 97 | msg = (char*)fmt; |
| 98 | if (fmt) { |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 99 | msg = xasprintf(fmt, param); |
Denys Vlasenko | b6237c0 | 2020-12-17 12:34:25 +0100 | [diff] [blame] | 100 | send_r_n(msg); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 101 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 102 | fflush_all(); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 103 | return msg; |
| 104 | } |
| 105 | |
| 106 | // NB: parse_url can modify url[] (despite const), but only if '@' is there |
| 107 | /* |
Denys Vlasenko | 5707b52 | 2010-12-20 05:12:39 +0100 | [diff] [blame] | 108 | static char* FAST_FUNC parse_url(char *url, char **user, char **pass) |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 109 | { |
| 110 | // parse [user[:pass]@]host |
| 111 | // return host |
| 112 | char *s = strchr(url, '@'); |
| 113 | *user = *pass = NULL; |
| 114 | if (s) { |
| 115 | *s++ = '\0'; |
| 116 | *user = url; |
| 117 | url = s; |
| 118 | s = strchr(*user, ':'); |
| 119 | if (s) { |
| 120 | *s++ = '\0'; |
| 121 | *pass = s; |
| 122 | } |
| 123 | } |
| 124 | return url; |
| 125 | } |
| 126 | */ |
| 127 | |
Denys Vlasenko | 8d634a0 | 2018-07-31 17:30:08 +0200 | [diff] [blame] | 128 | 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] | 129 | { |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 130 | enum { |
Denys Vlasenko | 41fea01 | 2011-11-18 22:25:35 +0100 | [diff] [blame] | 131 | SRC_BUF_SIZE = 57, /* This *MUST* be a multiple of 3 */ |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 132 | DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), |
| 133 | }; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 134 | #define src_buf text |
Denys Vlasenko | c8f9a8d | 2010-09-16 18:10:04 +0200 | [diff] [blame] | 135 | char src[SRC_BUF_SIZE]; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 136 | FILE *fp = fp; |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 137 | char dst_buf[DST_BUF_SIZE + 1]; |
| 138 | |
| 139 | if (fname) { |
Denys Vlasenko | 8d634a0 | 2018-07-31 17:30:08 +0200 | [diff] [blame] | 140 | fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : stdin; |
Denys Vlasenko | c8f9a8d | 2010-09-16 18:10:04 +0200 | [diff] [blame] | 141 | src_buf = src; |
Denys Vlasenko | 8d634a0 | 2018-07-31 17:30:08 +0200 | [diff] [blame] | 142 | } |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 143 | |
| 144 | while (1) { |
| 145 | size_t size; |
| 146 | if (fname) { |
| 147 | size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp); |
| 148 | if ((ssize_t)size < 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 149 | bb_simple_perror_msg_and_die(bb_msg_read_error); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 150 | } else { |
| 151 | size = len; |
| 152 | if (len > SRC_BUF_SIZE) |
| 153 | size = SRC_BUF_SIZE; |
| 154 | } |
| 155 | if (!size) |
| 156 | break; |
| 157 | // encode the buffer we just read in |
| 158 | bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64); |
| 159 | if (fname) { |
Denys Vlasenko | 8d634a0 | 2018-07-31 17:30:08 +0200 | [diff] [blame] | 160 | puts(""); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 161 | } else { |
| 162 | src_buf += size; |
| 163 | len -= size; |
| 164 | } |
| 165 | fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout); |
| 166 | } |
| 167 | if (fname && NOT_LONE_DASH(fname)) |
| 168 | fclose(fp); |
| 169 | #undef src_buf |
| 170 | } |
| 171 | |
Denys Vlasenko | 8d634a0 | 2018-07-31 17:30:08 +0200 | [diff] [blame] | 172 | void FAST_FUNC printstr_base64(const char *text) |
| 173 | { |
| 174 | encode_n_base64(NULL, text, strlen(text)); |
| 175 | } |
| 176 | |
| 177 | void FAST_FUNC printbuf_base64(const char *text, unsigned len) |
| 178 | { |
| 179 | encode_n_base64(NULL, text, len); |
| 180 | } |
| 181 | |
| 182 | void FAST_FUNC printfile_base64(const char *fname) |
| 183 | { |
| 184 | encode_n_base64(fname, NULL, 0); |
| 185 | } |
| 186 | |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 187 | /* |
| 188 | * get username and password from a file descriptor |
| 189 | */ |
| 190 | void FAST_FUNC get_cred_or_die(int fd) |
| 191 | { |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 192 | if (isatty(fd)) { |
Denys Vlasenko | 17058a0 | 2018-04-07 15:50:30 +0200 | [diff] [blame] | 193 | G.user = bb_ask_noecho(fd, /* timeout: */ 0, "User: "); |
| 194 | G.pass = bb_ask_noecho(fd, /* timeout: */ 0, "Password: "); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 195 | } else { |
Denys Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame] | 196 | G.user = xmalloc_reads(fd, /* maxsize: */ NULL); |
| 197 | G.pass = xmalloc_reads(fd, /* maxsize: */ NULL); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 198 | } |
Denis Vlasenko | 021de3f | 2009-03-04 17:56:00 +0000 | [diff] [blame] | 199 | if (!G.user || !*G.user || !G.pass) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 200 | bb_simple_error_msg_and_die("no username or password"); |
Denis Vlasenko | 239d06b | 2008-11-06 23:42:42 +0000 | [diff] [blame] | 201 | } |