3
0

mail.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. static void kill_helper(void)
  12. {
  13. // TODO!!!: is there more elegant way to terminate child on program failure?
  14. if (G.helper_pid > 0)
  15. kill(G.helper_pid, SIGTERM);
  16. }
  17. // generic signal handler
  18. static void signal_handler(int signo)
  19. {
  20. #define err signo
  21. if (SIGALRM == signo) {
  22. kill_helper();
  23. bb_error_msg_and_die("timed out");
  24. }
  25. // SIGCHLD. reap zombies
  26. if (safe_waitpid(G.helper_pid, &err, WNOHANG) > 0)
  27. if (WIFEXITED(err)) {
  28. G.helper_pid = 0;
  29. if (WEXITSTATUS(err))
  30. bb_error_msg_and_die("child exited (%d)", WEXITSTATUS(err));
  31. }
  32. #undef err
  33. }
  34. void FAST_FUNC launch_helper(const char **argv)
  35. {
  36. // setup vanilla unidirectional pipes interchange
  37. int idx;
  38. int pipes[4];
  39. xpipe(pipes);
  40. xpipe(pipes+2);
  41. G.helper_pid = vfork();
  42. if (G.helper_pid < 0)
  43. bb_perror_msg_and_die("vfork");
  44. idx = (!G.helper_pid) * 2;
  45. xdup2(pipes[idx], STDIN_FILENO);
  46. xdup2(pipes[3-idx], STDOUT_FILENO);
  47. if (ENABLE_FEATURE_CLEAN_UP)
  48. for (int i = 4; --i >= 0; )
  49. if (pipes[i] > STDOUT_FILENO)
  50. close(pipes[i]);
  51. if (!G.helper_pid) {
  52. // child: try to execute connection helper
  53. BB_EXECVP(*argv, (char **)argv);
  54. _exit(127);
  55. }
  56. // parent: check whether child is alive
  57. bb_signals(0
  58. + (1 << SIGCHLD)
  59. + (1 << SIGALRM)
  60. , signal_handler);
  61. signal_handler(SIGCHLD);
  62. // child seems OK -> parent goes on
  63. atexit(kill_helper);
  64. }
  65. const FAST_FUNC char *command(const char *fmt, const char *param)
  66. {
  67. const char *msg = fmt;
  68. if (timeout)
  69. alarm(timeout);
  70. if (msg) {
  71. msg = xasprintf(fmt, param);
  72. printf("%s\r\n", msg);
  73. }
  74. fflush(stdout);
  75. return msg;
  76. }
  77. // NB: parse_url can modify url[] (despite const), but only if '@' is there
  78. /*
  79. static char FAST_FUNC *parse_url(char *url, char **user, char **pass)
  80. {
  81. // parse [user[:pass]@]host
  82. // return host
  83. char *s = strchr(url, '@');
  84. *user = *pass = NULL;
  85. if (s) {
  86. *s++ = '\0';
  87. *user = url;
  88. url = s;
  89. s = strchr(*user, ':');
  90. if (s) {
  91. *s++ = '\0';
  92. *pass = s;
  93. }
  94. }
  95. return url;
  96. }
  97. */
  98. void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol)
  99. {
  100. enum {
  101. SRC_BUF_SIZE = 45, /* This *MUST* be a multiple of 3 */
  102. DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
  103. };
  104. #define src_buf text
  105. FILE *fp = fp;
  106. ssize_t len = len;
  107. char dst_buf[DST_BUF_SIZE + 1];
  108. if (fname) {
  109. fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : (FILE *)text;
  110. src_buf = bb_common_bufsiz1;
  111. // N.B. strlen(NULL) segfaults!
  112. } else if (text) {
  113. // though we do not call uuencode(NULL, NULL) explicitly
  114. // still we do not want to break things suddenly
  115. len = strlen(text);
  116. } else
  117. return;
  118. while (1) {
  119. size_t size;
  120. if (fname) {
  121. size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp);
  122. if ((ssize_t)size < 0)
  123. bb_perror_msg_and_die(bb_msg_read_error);
  124. } else {
  125. size = len;
  126. if (len > SRC_BUF_SIZE)
  127. size = SRC_BUF_SIZE;
  128. }
  129. if (!size)
  130. break;
  131. // encode the buffer we just read in
  132. bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
  133. if (fname) {
  134. printf("%s\n", eol);
  135. } else {
  136. src_buf += size;
  137. len -= size;
  138. }
  139. fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout);
  140. }
  141. if (fname && NOT_LONE_DASH(fname))
  142. fclose(fp);
  143. #undef src_buf
  144. }
  145. void FAST_FUNC decode_base64(FILE *src_stream, FILE *dst_stream)
  146. {
  147. int term_count = 1;
  148. while (1) {
  149. char translated[4];
  150. int count = 0;
  151. while (count < 4) {
  152. char *table_ptr;
  153. int ch;
  154. /* Get next _valid_ character.
  155. * global vector bb_uuenc_tbl_base64[] contains this string:
  156. * "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n"
  157. */
  158. do {
  159. ch = fgetc(src_stream);
  160. if (ch == EOF) {
  161. bb_error_msg_and_die(bb_msg_read_error);
  162. }
  163. // - means end of MIME section
  164. if ('-' == ch) {
  165. // push it back
  166. ungetc(ch, src_stream);
  167. return;
  168. }
  169. table_ptr = strchr(bb_uuenc_tbl_base64, ch);
  170. } while (table_ptr == NULL);
  171. /* Convert encoded character to decimal */
  172. ch = table_ptr - bb_uuenc_tbl_base64;
  173. if (*table_ptr == '=') {
  174. if (term_count == 0) {
  175. translated[count] = '\0';
  176. break;
  177. }
  178. term_count++;
  179. } else if (*table_ptr == '\n') {
  180. /* Check for terminating line */
  181. if (term_count == 5) {
  182. return;
  183. }
  184. term_count = 1;
  185. continue;
  186. } else {
  187. translated[count] = ch;
  188. count++;
  189. term_count = 0;
  190. }
  191. }
  192. /* Merge 6 bit chars to 8 bit */
  193. if (count > 1) {
  194. fputc(translated[0] << 2 | translated[1] >> 4, dst_stream);
  195. }
  196. if (count > 2) {
  197. fputc(translated[1] << 4 | translated[2] >> 2, dst_stream);
  198. }
  199. if (count > 3) {
  200. fputc(translated[2] << 6 | translated[3], dst_stream);
  201. }
  202. }
  203. }
  204. /*
  205. * get username and password from a file descriptor
  206. */
  207. void FAST_FUNC get_cred_or_die(int fd)
  208. {
  209. // either from TTY
  210. if (isatty(fd)) {
  211. G.user = xstrdup(bb_askpass(0, "User: "));
  212. G.pass = xstrdup(bb_askpass(0, "Password: "));
  213. // or from STDIN
  214. } else {
  215. FILE *fp = fdopen(fd, "r");
  216. G.user = xmalloc_fgetline(fp);
  217. G.pass = xmalloc_fgetline(fp);
  218. fclose(fp);
  219. }
  220. if (!G.user || !*G.user || !G.pass || !*G.pass)
  221. bb_error_msg_and_die("no username or password");
  222. }