mail.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 source tree.
  8. */
  9. #include <sys/prctl.h>
  10. #include "libbb.h"
  11. #include "mail.h"
  12. // common signal handler
  13. static void signal_handler(int signo)
  14. {
  15. if (SIGALRM == signo) {
  16. bb_simple_error_msg_and_die("timed out");
  17. }
  18. // SIGCHLD. reap the zombie if we expect one
  19. if (G.helper_pid == 0)
  20. return;
  21. #define status signo
  22. if (safe_waitpid(G.helper_pid, &status, WNOHANG) > 0) {
  23. G.helper_pid = 0;
  24. if (WIFSIGNALED(status))
  25. bb_error_msg_and_die("helper killed by signal %u", WTERMSIG(status));
  26. if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
  27. bb_error_msg_and_die("helper exited (%u)", WEXITSTATUS(status));
  28. }
  29. #undef status
  30. }
  31. void FAST_FUNC launch_helper(const char **argv)
  32. {
  33. pid_t pid;
  34. struct fd_pair child_out;
  35. struct fd_pair child_in;
  36. xpiped_pair(child_out);
  37. xpiped_pair(child_in);
  38. // NB: handler must be installed before vfork
  39. bb_signals(0
  40. + (1 << SIGCHLD)
  41. + (1 << SIGALRM)
  42. , signal_handler);
  43. fflush_all();
  44. pid = xvfork();
  45. if (pid == 0) {
  46. // child
  47. close(child_in.wr);
  48. close(child_out.rd);
  49. xmove_fd(child_in.rd, STDIN_FILENO);
  50. xmove_fd(child_out.wr, STDOUT_FILENO);
  51. // if parent dies, get SIGTERM
  52. prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
  53. // try to execute connection helper
  54. // NB: SIGCHLD & SIGALRM revert to SIG_DFL on exec
  55. BB_EXECVP_or_die((char**)argv);
  56. }
  57. G.helper_pid = pid;
  58. close(child_out.wr);
  59. close(child_in.rd);
  60. xmove_fd(child_out.rd, STDIN_FILENO);
  61. xmove_fd(child_in.wr, STDOUT_FILENO);
  62. // parent goes on
  63. }
  64. void FAST_FUNC send_r_n(const char *s)
  65. {
  66. if (G.verbose)
  67. bb_error_msg("send:'%s'", s);
  68. printf("%s\r\n", s);
  69. }
  70. char* FAST_FUNC send_mail_command(const char *fmt, const char *param)
  71. {
  72. char *msg;
  73. if (G.timeout)
  74. alarm(G.timeout);
  75. msg = (char*)fmt;
  76. if (fmt) {
  77. msg = xasprintf(fmt, param);
  78. send_r_n(msg);
  79. }
  80. fflush_all();
  81. return msg;
  82. }
  83. // NB: parse_url can modify url[] (despite const), but only if '@' is there
  84. /*
  85. static char* FAST_FUNC parse_url(char *url, char **user, char **pass)
  86. {
  87. // parse [user[:pass]@]host
  88. // return host
  89. char *s = strchr(url, '@');
  90. *user = *pass = NULL;
  91. if (s) {
  92. *s++ = '\0';
  93. *user = url;
  94. url = s;
  95. s = strchr(*user, ':');
  96. if (s) {
  97. *s++ = '\0';
  98. *pass = s;
  99. }
  100. }
  101. return url;
  102. }
  103. */
  104. static void encode_n_base64(const char *fname, const char *text, size_t len)
  105. {
  106. enum {
  107. SRC_BUF_SIZE = 57, /* This *MUST* be a multiple of 3 */
  108. DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
  109. };
  110. #define src_buf text
  111. char src[SRC_BUF_SIZE];
  112. FILE *fp = fp;
  113. char dst_buf[DST_BUF_SIZE + 1];
  114. if (fname) {
  115. fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : stdin;
  116. src_buf = src;
  117. }
  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_simple_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. puts("");
  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 printstr_base64(const char *text)
  146. {
  147. encode_n_base64(NULL, text, strlen(text));
  148. }
  149. void FAST_FUNC printbuf_base64(const char *text, unsigned len)
  150. {
  151. encode_n_base64(NULL, text, len);
  152. }
  153. void FAST_FUNC printfile_base64(const char *fname)
  154. {
  155. encode_n_base64(fname, NULL, 0);
  156. }
  157. /*
  158. * get username and password from a file descriptor
  159. */
  160. void FAST_FUNC get_cred_or_die(int fd)
  161. {
  162. if (isatty(fd)) {
  163. G.user = bb_ask_noecho(fd, /* timeout: */ 0, "User: ");
  164. G.pass = bb_ask_noecho(fd, /* timeout: */ 0, "Password: ");
  165. } else {
  166. G.user = xmalloc_reads(fd, /* maxsize: */ NULL);
  167. G.pass = xmalloc_reads(fd, /* maxsize: */ NULL);
  168. }
  169. if (!G.user || !*G.user || !G.pass)
  170. bb_simple_error_msg_and_die("no username or password");
  171. }