3
0

mail.c 4.1 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 "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 = xvfork();
  51. i = (!G.helper_pid) * 2; // for parent:0, for child:2
  52. close(pipes[i + 1]); // 1 or 3 - closing one write end
  53. close(pipes[2 - i]); // 2 or 0 - closing one read end
  54. xmove_fd(pipes[i], STDIN_FILENO); // 0 or 2 - using other read end
  55. xmove_fd(pipes[3 - i], STDOUT_FILENO); // 3 or 1 - using other write end
  56. // End result:
  57. // parent stdout [3] -> child stdin [2]
  58. // child stdout [1] -> parent stdin [0]
  59. if (!G.helper_pid) {
  60. // child: try to execute connection helper
  61. // NB: SIGCHLD & SIGALRM revert to SIG_DFL on exec
  62. BB_EXECVP_or_die((char**)argv);
  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. char* FAST_FUNC send_mail_command(const char *fmt, const char *param)
  71. {
  72. char *msg;
  73. if (timeout)
  74. alarm(timeout);
  75. msg = (char*)fmt;
  76. if (fmt) {
  77. msg = xasprintf(fmt, param);
  78. if (verbose)
  79. bb_error_msg("send:'%s'", msg);
  80. printf("%s\r\n", msg);
  81. }
  82. fflush_all();
  83. return msg;
  84. }
  85. // NB: parse_url can modify url[] (despite const), but only if '@' is there
  86. /*
  87. static char* FAST_FUNC parse_url(char *url, char **user, char **pass)
  88. {
  89. // parse [user[:pass]@]host
  90. // return host
  91. char *s = strchr(url, '@');
  92. *user = *pass = NULL;
  93. if (s) {
  94. *s++ = '\0';
  95. *user = url;
  96. url = s;
  97. s = strchr(*user, ':');
  98. if (s) {
  99. *s++ = '\0';
  100. *pass = s;
  101. }
  102. }
  103. return url;
  104. }
  105. */
  106. void FAST_FUNC encode_base64(char *fname, const char *text, const char *eol)
  107. {
  108. enum {
  109. SRC_BUF_SIZE = 57, /* This *MUST* be a multiple of 3 */
  110. DST_BUF_SIZE = 4 * ((SRC_BUF_SIZE + 2) / 3),
  111. };
  112. #define src_buf text
  113. char src[SRC_BUF_SIZE];
  114. FILE *fp = fp;
  115. ssize_t len = len;
  116. char dst_buf[DST_BUF_SIZE + 1];
  117. if (fname) {
  118. fp = (NOT_LONE_DASH(fname)) ? xfopen_for_read(fname) : (FILE *)text;
  119. src_buf = src;
  120. } else if (text) {
  121. // though we do not call uuencode(NULL, NULL) explicitly
  122. // still we do not want to break things suddenly
  123. len = strlen(text);
  124. } else
  125. return;
  126. while (1) {
  127. size_t size;
  128. if (fname) {
  129. size = fread((char *)src_buf, 1, SRC_BUF_SIZE, fp);
  130. if ((ssize_t)size < 0)
  131. bb_perror_msg_and_die(bb_msg_read_error);
  132. } else {
  133. size = len;
  134. if (len > SRC_BUF_SIZE)
  135. size = SRC_BUF_SIZE;
  136. }
  137. if (!size)
  138. break;
  139. // encode the buffer we just read in
  140. bb_uuencode(dst_buf, src_buf, size, bb_uuenc_tbl_base64);
  141. if (fname) {
  142. printf("%s\n", eol);
  143. } else {
  144. src_buf += size;
  145. len -= size;
  146. }
  147. fwrite(dst_buf, 1, 4 * ((size + 2) / 3), stdout);
  148. }
  149. if (fname && NOT_LONE_DASH(fname))
  150. fclose(fp);
  151. #undef src_buf
  152. }
  153. /*
  154. * get username and password from a file descriptor
  155. */
  156. void FAST_FUNC get_cred_or_die(int fd)
  157. {
  158. if (isatty(fd)) {
  159. G.user = xstrdup(bb_ask(fd, /* timeout: */ 0, "User: "));
  160. G.pass = xstrdup(bb_ask(fd, /* timeout: */ 0, "Password: "));
  161. } else {
  162. G.user = xmalloc_reads(fd, /* maxsize: */ NULL);
  163. G.pass = xmalloc_reads(fd, /* maxsize: */ NULL);
  164. }
  165. if (!G.user || !*G.user || !G.pass)
  166. bb_error_msg_and_die("no username or password");
  167. }