rexec.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 1980, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <alloca.h>
  33. #include <stdio.h>
  34. #include <netdb.h>
  35. #include <errno.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include <sys/uio.h>
  40. #include <set-freeres.h>
  41. int rexecoptions;
  42. static char *ahostbuf;
  43. int
  44. rexec_af (char **ahost, int rport, const char *name, const char *pass,
  45. const char *cmd, int *fd2p, sa_family_t af)
  46. {
  47. struct sockaddr_storage from;
  48. struct addrinfo hints, *res0;
  49. const char *orig_name = name;
  50. const char *orig_pass = pass;
  51. u_short port = 0;
  52. int s, timo = 1, s3;
  53. char c;
  54. int gai;
  55. char servbuff[NI_MAXSERV];
  56. __snprintf(servbuff, sizeof(servbuff), "%d", ntohs(rport));
  57. servbuff[sizeof(servbuff) - 1] = '\0';
  58. memset(&hints, '\0', sizeof(hints));
  59. hints.ai_family = af;
  60. hints.ai_socktype = SOCK_STREAM;
  61. hints.ai_flags = AI_CANONNAME;
  62. gai = getaddrinfo(*ahost, servbuff, &hints, &res0);
  63. if (gai){
  64. /* XXX: set errno? */
  65. return -1;
  66. }
  67. if (res0->ai_canonname){
  68. free (ahostbuf);
  69. ahostbuf = __strdup (res0->ai_canonname);
  70. if (ahostbuf == NULL) {
  71. perror ("rexec: strdup");
  72. goto bad2;
  73. }
  74. *ahost = ahostbuf;
  75. } else {
  76. *ahost = NULL;
  77. __set_errno (ENOENT);
  78. goto bad2;
  79. }
  80. ruserpass(res0->ai_canonname, &name, &pass);
  81. retry:
  82. /* NB: No SOCK_CLOEXEC for backwards compatibility. */
  83. s = __socket(res0->ai_family, res0->ai_socktype, 0);
  84. if (s < 0) {
  85. perror("rexec: socket");
  86. goto bad2;
  87. }
  88. if (__connect(s, res0->ai_addr, res0->ai_addrlen) < 0) {
  89. if (errno == ECONNREFUSED && timo <= 16) {
  90. (void) __close(s);
  91. __sleep(timo);
  92. timo *= 2;
  93. goto retry;
  94. }
  95. perror(res0->ai_canonname);
  96. goto bad;
  97. }
  98. if (fd2p == NULL) {
  99. (void) __write(s, "", 1);
  100. port = 0;
  101. } else {
  102. char num[32];
  103. int s2;
  104. union
  105. {
  106. struct sockaddr_storage ss;
  107. struct sockaddr sa;
  108. } sa2;
  109. socklen_t sa2len;
  110. s2 = __socket(res0->ai_family, res0->ai_socktype, 0);
  111. if (s2 < 0)
  112. goto bad;
  113. __listen(s2, 1);
  114. sa2len = sizeof (sa2);
  115. if (__getsockname(s2, &sa2.sa, &sa2len) < 0) {
  116. perror("getsockname");
  117. (void) __close(s2);
  118. goto bad;
  119. } else if (sa2len != SA_LEN(&sa2.sa)) {
  120. __set_errno(EINVAL);
  121. (void) __close(s2);
  122. goto bad;
  123. }
  124. port = 0;
  125. if (!getnameinfo(&sa2.sa, sa2len,
  126. NULL, 0, servbuff, sizeof(servbuff),
  127. NI_NUMERICSERV))
  128. port = strtol(servbuff, NULL, 10);
  129. (void) sprintf(num, "%u", port);
  130. (void) __write(s, num, strlen(num)+1);
  131. { socklen_t len = sizeof (from);
  132. s3 = TEMP_FAILURE_RETRY (accept(s2, (struct sockaddr *)&from,
  133. &len));
  134. __close(s2);
  135. if (s3 < 0) {
  136. perror("accept");
  137. port = 0;
  138. goto bad;
  139. }
  140. }
  141. *fd2p = s3;
  142. }
  143. struct iovec iov[3] =
  144. {
  145. [0] = { .iov_base = (void *) name, .iov_len = strlen (name) + 1 },
  146. /* should public key encrypt the password here */
  147. [1] = { .iov_base = (void *) pass, .iov_len = strlen (pass) + 1 },
  148. [2] = { .iov_base = (void *) cmd, .iov_len = strlen (cmd) + 1 }
  149. };
  150. (void) TEMP_FAILURE_RETRY (__writev (s, iov, 3));
  151. /* We don't need the memory allocated for the name and the password
  152. in ruserpass anymore. */
  153. if (name != orig_name)
  154. free ((char *) name);
  155. if (pass != orig_pass)
  156. free ((char *) pass);
  157. if (__read(s, &c, 1) != 1) {
  158. perror(*ahost);
  159. goto bad;
  160. }
  161. if (c != 0) {
  162. while (__read(s, &c, 1) == 1) {
  163. (void) __write(2, &c, 1);
  164. if (c == '\n')
  165. break;
  166. }
  167. goto bad;
  168. }
  169. freeaddrinfo(res0);
  170. return (s);
  171. bad:
  172. if (port)
  173. (void) __close(*fd2p);
  174. (void) __close(s);
  175. bad2:
  176. freeaddrinfo(res0);
  177. return (-1);
  178. }
  179. libc_hidden_def (rexec_af)
  180. int
  181. rexec (char **ahost, int rport, const char *name, const char *pass,
  182. const char *cmd, int *fd2p)
  183. {
  184. return rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET);
  185. }
  186. weak_alias (ahostbuf, __libc_rexec_freemem_ptr)