1
2
3
4
5
6
7
8
9#include <sys/prctl.h>
10#include "libbb.h"
11#include "mail.h"
12
13
14static void signal_handler(int signo)
15{
16#define err signo
17 if (SIGALRM == signo) {
18 bb_error_msg_and_die("timed out");
19 }
20
21
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));
25 if (WIFEXITED(err)) {
26 G.helper_pid = 0;
27 if (WEXITSTATUS(err))
28 bb_error_msg_and_die("helper exited (%u)", WEXITSTATUS(err));
29 }
30 }
31#undef err
32}
33
34void FAST_FUNC launch_helper(const char **argv)
35{
36
37 int i;
38 int pipes[4];
39
40 xpipe(pipes);
41 xpipe(pipes + 2);
42
43
44 bb_signals(0
45 + (1 << SIGCHLD)
46 + (1 << SIGALRM)
47 , signal_handler);
48
49 G.helper_pid = xvfork();
50
51 i = (!G.helper_pid) * 2;
52 close(pipes[i + 1]);
53 close(pipes[2 - i]);
54 xmove_fd(pipes[i], STDIN_FILENO);
55 xmove_fd(pipes[3 - i], STDOUT_FILENO);
56
57
58
59
60 if (!G.helper_pid) {
61
62
63 prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
64
65
66 BB_EXECVP_or_die((char**)argv);
67 }
68
69
70}
71
72char* FAST_FUNC send_mail_command(const char *fmt, const char *param)
73{
74 char *msg;
75 if (timeout)
76 alarm(timeout);
77 msg = (char*)fmt;
78 if (fmt) {
79 msg = xasprintf(fmt, param);
80 if (verbose)
81 bb_error_msg("send:'%s'", msg);
82 printf("%s\r\n", msg);
83 }
84 fflush_all();
85 return msg;
86}
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110static void encode_n_base64(const char *fname, const char *text, size_t len)
111{
112 enum {
113 SRC_BUF_SIZE = 57,
114 DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
115 };
116#define src_buf text
117 char src[SRC_BUF_SIZE];
118 FILE *fp = fp;
119 char dst_buf[DST_BUF_SIZE + 1];
120
121 if (fname) {
122 fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : stdin;
123 src_buf = src;
124 }
125
126 while (1) {
127 size_t size;
128 if (fname) {
129 size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp);
130 if ((ssize_t)size < 0)
131 bb_perror_msg_and_die(bb_msg_read_error);
132 } else {
133 size = len;
134 if (len > SRC_BUF_SIZE)
135 size = SRC_BUF_SIZE;
136 }
137 if (!size)
138 break;
139
140 bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
141 if (fname) {
142 puts("");
143 } else {
144 src_buf += size;
145 len -= size;
146 }
147 fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout);
148 }
149 if (fname && NOT_LONE_DASH(fname))
150 fclose(fp);
151#undef src_buf
152}
153
154void FAST_FUNC printstr_base64(const char *text)
155{
156 encode_n_base64(NULL, text, strlen(text));
157}
158
159void FAST_FUNC printbuf_base64(const char *text, unsigned len)
160{
161 encode_n_base64(NULL, text, len);
162}
163
164void FAST_FUNC printfile_base64(const char *fname)
165{
166 encode_n_base64(fname, NULL, 0);
167}
168
169
170
171
172void FAST_FUNC get_cred_or_die(int fd)
173{
174 if (isatty(fd)) {
175 G.user = bb_ask_noecho(fd, 0, "User: ");
176 G.pass = bb_ask_noecho(fd, 0, "Password: ");
177 } else {
178 G.user = xmalloc_reads(fd, NULL);
179 G.pass = xmalloc_reads(fd, NULL);
180 }
181 if (!G.user || !*G.user || !G.pass)
182 bb_error_msg_and_die("no username or password");
183}
184