mail.c 4.2 KB

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