mail.c 5.4 KB

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