mail.c 3.9 KB

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