3
0

mail.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // generic signal handler
  13. static void signal_handler(int signo)
  14. {
  15. #define err signo
  16. if (SIGALRM == signo) {
  17. bb_simple_error_msg_and_die("timed out");
  18. }
  19. // SIGCHLD. reap zombies
  20. if (safe_waitpid(G.helper_pid, &err, WNOHANG) > 0) {
  21. if (WIFSIGNALED(err))
  22. bb_error_msg_and_die("helper killed by signal %u", WTERMSIG(err));
  23. if (WIFEXITED(err)) {
  24. G.helper_pid = 0;
  25. if (WEXITSTATUS(err))
  26. bb_error_msg_and_die("helper exited (%u)", WEXITSTATUS(err));
  27. }
  28. }
  29. #undef err
  30. }
  31. void FAST_FUNC launch_helper(const char **argv)
  32. {
  33. // setup vanilla unidirectional pipes interchange
  34. int i;
  35. int pipes[4];
  36. xpipe(pipes);
  37. xpipe(pipes + 2);
  38. // NB: handler must be installed before vfork
  39. bb_signals(0
  40. + (1 << SIGCHLD)
  41. + (1 << SIGALRM)
  42. , signal_handler);
  43. G.helper_pid = xvfork();
  44. i = (!G.helper_pid) * 2; // for parent:0, for child:2
  45. close(pipes[i + 1]); // 1 or 3 - closing one write end
  46. close(pipes[2 - i]); // 2 or 0 - closing one read end
  47. xmove_fd(pipes[i], STDIN_FILENO); // 0 or 2 - using other read end
  48. xmove_fd(pipes[3 - i], STDOUT_FILENO); // 3 or 1 - using other write end
  49. // End result:
  50. // parent stdout [3] -> child stdin [2]
  51. // child stdout [1] -> parent stdin [0]
  52. if (!G.helper_pid) {
  53. // child
  54. // if parent dies, get SIGTERM
  55. prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
  56. // try to execute connection helper
  57. // NB: SIGCHLD & SIGALRM revert to SIG_DFL on exec
  58. BB_EXECVP_or_die((char**)argv);
  59. }
  60. // parent goes on
  61. }
  62. char* FAST_FUNC send_mail_command(const char *fmt, const char *param)
  63. {
  64. char *msg;
  65. if (timeout)
  66. alarm(timeout);
  67. msg = (char*)fmt;
  68. if (fmt) {
  69. msg = xasprintf(fmt, param);
  70. if (verbose)
  71. bb_error_msg("send:'%s'", msg);
  72. printf("%s\r\n", msg);
  73. }
  74. fflush_all();
  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. static void encode_n_base64(const char *fname, const char *text, size_t len)
  99. {
  100. enum {
  101. SRC_BUF_SIZE = 57, /* This *MUST* be a multiple of 3 */
  102. DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
  103. };
  104. #define src_buf text
  105. char src[SRC_BUF_SIZE];
  106. FILE *fp = fp;
  107. char dst_buf[DST_BUF_SIZE + 1];
  108. if (fname) {
  109. fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : stdin;
  110. src_buf = src;
  111. }
  112. while (1) {
  113. size_t size;
  114. if (fname) {
  115. size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp);
  116. if ((ssize_t)size < 0)
  117. bb_simple_perror_msg_and_die(bb_msg_read_error);
  118. } else {
  119. size = len;
  120. if (len > SRC_BUF_SIZE)
  121. size = SRC_BUF_SIZE;
  122. }
  123. if (!size)
  124. break;
  125. // encode the buffer we just read in
  126. bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
  127. if (fname) {
  128. puts("");
  129. } else {
  130. src_buf += size;
  131. len -= size;
  132. }
  133. fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout);
  134. }
  135. if (fname && NOT_LONE_DASH(fname))
  136. fclose(fp);
  137. #undef src_buf
  138. }
  139. void FAST_FUNC printstr_base64(const char *text)
  140. {
  141. encode_n_base64(NULL, text, strlen(text));
  142. }
  143. void FAST_FUNC printbuf_base64(const char *text, unsigned len)
  144. {
  145. encode_n_base64(NULL, text, len);
  146. }
  147. void FAST_FUNC printfile_base64(const char *fname)
  148. {
  149. encode_n_base64(fname, NULL, 0);
  150. }
  151. /*
  152. * get username and password from a file descriptor
  153. */
  154. void FAST_FUNC get_cred_or_die(int fd)
  155. {
  156. if (isatty(fd)) {
  157. G.user = bb_ask_noecho(fd, /* timeout: */ 0, "User: ");
  158. G.pass = bb_ask_noecho(fd, /* timeout: */ 0, "Password: ");
  159. } else {
  160. G.user = xmalloc_reads(fd, /* maxsize: */ NULL);
  161. G.pass = xmalloc_reads(fd, /* maxsize: */ NULL);
  162. }
  163. if (!G.user || !*G.user || !G.pass)
  164. bb_simple_error_msg_and_die("no username or password");
  165. }