Contents of /trunk/mkinitrd-magellan/busybox/mailutils/mail.c
Parent Directory | Revision Log
Revision 984 -
(show annotations)
(download)
Sun May 30 11:32:42 2010 UTC (14 years, 4 months ago) by niro
File MIME type: text/plain
File size: 5558 byte(s)
Sun May 30 11:32:42 2010 UTC (14 years, 4 months ago) by niro
File MIME type: text/plain
File size: 5558 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 | /* 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 | |
12 | static void kill_helper(void) |
13 | { |
14 | if (G.helper_pid > 0) { |
15 | kill(G.helper_pid, SIGTERM); |
16 | G.helper_pid = 0; |
17 | } |
18 | } |
19 | |
20 | // generic signal handler |
21 | static 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 |
30 | 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)); |
33 | if (WIFEXITED(err)) { |
34 | G.helper_pid = 0; |
35 | if (WEXITSTATUS(err)) |
36 | bb_error_msg_and_die("helper exited (%u)", WEXITSTATUS(err)); |
37 | } |
38 | } |
39 | #undef err |
40 | } |
41 | |
42 | void FAST_FUNC launch_helper(const char **argv) |
43 | { |
44 | // setup vanilla unidirectional pipes interchange |
45 | int i; |
46 | int pipes[4]; |
47 | |
48 | xpipe(pipes); |
49 | xpipe(pipes + 2); |
50 | |
51 | // NB: handler must be installed before vfork |
52 | bb_signals(0 |
53 | + (1 << SIGCHLD) |
54 | + (1 << SIGALRM) |
55 | , signal_handler); |
56 | |
57 | G.helper_pid = vfork(); |
58 | if (G.helper_pid < 0) |
59 | bb_perror_msg_and_die("vfork"); |
60 | |
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 | |
67 | if (!G.helper_pid) { |
68 | // child: try to execute connection helper |
69 | // NB: SIGCHLD & SIGALRM revert to SIG_DFL on exec |
70 | BB_EXECVP(*argv, (char **)argv); |
71 | _exit(127); |
72 | } |
73 | |
74 | // parent |
75 | // check whether child is alive |
76 | //redundant:signal_handler(SIGCHLD); |
77 | // child seems OK -> parent goes on |
78 | atexit(kill_helper); |
79 | } |
80 | |
81 | const FAST_FUNC char *command(const char *fmt, const char *param) |
82 | { |
83 | const char *msg = fmt; |
84 | if (timeout) |
85 | alarm(timeout); |
86 | if (msg) { |
87 | msg = xasprintf(fmt, param); |
88 | printf("%s\r\n", msg); |
89 | } |
90 | fflush_all(); |
91 | return msg; |
92 | } |
93 | |
94 | // NB: parse_url can modify url[] (despite const), but only if '@' is there |
95 | /* |
96 | static char FAST_FUNC *parse_url(char *url, char **user, char **pass) |
97 | { |
98 | // parse [user[:pass]@]host |
99 | // return host |
100 | char *s = strchr(url, '@'); |
101 | *user = *pass = NULL; |
102 | if (s) { |
103 | *s++ = '\0'; |
104 | *user = url; |
105 | url = s; |
106 | s = strchr(*user, ':'); |
107 | if (s) { |
108 | *s++ = '\0'; |
109 | *pass = s; |
110 | } |
111 | } |
112 | return url; |
113 | } |
114 | */ |
115 | |
116 | void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol) |
117 | { |
118 | enum { |
119 | SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */ |
120 | DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3), |
121 | }; |
122 | |
123 | #define src_buf text |
124 | FILE *fp = fp; |
125 | ssize_t len = len; |
126 | char dst_buf[DST_BUF_SIZE + 1]; |
127 | |
128 | if (fname) { |
129 | fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : (FILE *)text; |
130 | src_buf = bb_common_bufsiz1; |
131 | // N.B. strlen(NULL) segfaults! |
132 | } else if (text) { |
133 | // though we do not call uuencode(NULL, NULL) explicitly |
134 | // still we do not want to break things suddenly |
135 | len = strlen(text); |
136 | } else |
137 | return; |
138 | |
139 | while (1) { |
140 | size_t size; |
141 | if (fname) { |
142 | size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp); |
143 | if ((ssize_t)size < 0) |
144 | bb_perror_msg_and_die(bb_msg_read_error); |
145 | } else { |
146 | size = len; |
147 | if (len > SRC_BUF_SIZE) |
148 | size = SRC_BUF_SIZE; |
149 | } |
150 | if (!size) |
151 | break; |
152 | // encode the buffer we just read in |
153 | bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64); |
154 | if (fname) { |
155 | printf("%s\n", eol); |
156 | } else { |
157 | src_buf += size; |
158 | len -= size; |
159 | } |
160 | fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout); |
161 | } |
162 | if (fname && NOT_LONE_DASH(fname)) |
163 | fclose(fp); |
164 | #undef src_buf |
165 | } |
166 | |
167 | void FAST_FUNC decode_base64(FILE *src_stream, FILE *dst_stream) |
168 | { |
169 | int term_count = 1; |
170 | |
171 | while (1) { |
172 | char translated[4]; |
173 | int count = 0; |
174 | |
175 | while (count < 4) { |
176 | char *table_ptr; |
177 | int ch; |
178 | |
179 | /* Get next _valid_ character. |
180 | * global vector bb_uuenc_tbl_base64[] contains this string: |
181 | * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n" |
182 | */ |
183 | do { |
184 | ch = fgetc(src_stream); |
185 | if (ch == EOF) { |
186 | bb_error_msg_and_die(bb_msg_read_error); |
187 | } |
188 | // - means end of MIME section |
189 | if ('-' == ch) { |
190 | // push it back |
191 | ungetc(ch, src_stream); |
192 | return; |
193 | } |
194 | table_ptr = strchr(bb_uuenc_tbl_base64, ch); |
195 | } while (table_ptr == NULL); |
196 | |
197 | /* Convert encoded character to decimal */ |
198 | ch = table_ptr - bb_uuenc_tbl_base64; |
199 | |
200 | if (*table_ptr == '=') { |
201 | if (term_count == 0) { |
202 | translated[count] = '\0'; |
203 | break; |
204 | } |
205 | term_count++; |
206 | } else if (*table_ptr == '\n') { |
207 | /* Check for terminating line */ |
208 | if (term_count == 5) { |
209 | return; |
210 | } |
211 | term_count = 1; |
212 | continue; |
213 | } else { |
214 | translated[count] = ch; |
215 | count++; |
216 | term_count = 0; |
217 | } |
218 | } |
219 | |
220 | /* Merge 6 bit chars to 8 bit */ |
221 | if (count > 1) { |
222 | fputc(translated[0] << 2 | translated[1] >> 4, dst_stream); |
223 | } |
224 | if (count > 2) { |
225 | fputc(translated[1] << 4 | translated[2] >> 2, dst_stream); |
226 | } |
227 | if (count > 3) { |
228 | fputc(translated[2] << 6 | translated[3], dst_stream); |
229 | } |
230 | } |
231 | } |
232 | |
233 | |
234 | /* |
235 | * get username and password from a file descriptor |
236 | */ |
237 | void FAST_FUNC get_cred_or_die(int fd) |
238 | { |
239 | if (isatty(fd)) { |
240 | G.user = xstrdup(bb_ask(fd, /* timeout: */ 0, "User: ")); |
241 | G.pass = xstrdup(bb_ask(fd, /* timeout: */ 0, "Password: ")); |
242 | } else { |
243 | G.user = xmalloc_reads(fd, /* pfx: */ NULL, /* maxsize: */ NULL); |
244 | G.pass = xmalloc_reads(fd, /* pfx: */ NULL, /* maxsize: */ NULL); |
245 | } |
246 | if (!G.user || !*G.user || !G.pass) |
247 | bb_error_msg_and_die("no username or password"); |
248 | } |