rexec.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #define SA_LEN(_x) __libc_sa_len((_x)->sa_family)
  40. extern int __libc_sa_len(sa_family_t __af) __THROW attribute_hidden;
  41. /* int rexecoptions; - google does not know it */
  42. static char ahostbuf[NI_MAXHOST];
  43. int
  44. rexec_af(char **ahost, int rport, const char *name, const char *pass, const char *cmd, int *fd2p, sa_family_t af)
  45. {
  46. struct sockaddr_storage sa2, from;
  47. struct addrinfo hints, *res0;
  48. const char *orig_name = name;
  49. const char *orig_pass = pass;
  50. u_short port = 0;
  51. int s, timo = 1, s3;
  52. char c;
  53. int gai;
  54. char servbuff[NI_MAXSERV];
  55. if (sizeof(servbuff) < sizeof(int)*3 + 2) {
  56. snprintf(servbuff, sizeof(servbuff), "%d", ntohs(rport));
  57. servbuff[sizeof(servbuff) - 1] = '\0';
  58. } else {
  59. sprintf(servbuff, "%d", ntohs(rport));
  60. }
  61. memset(&hints, '\0', sizeof(hints));
  62. hints.ai_family = af;
  63. hints.ai_socktype = SOCK_STREAM;
  64. hints.ai_flags = AI_CANONNAME;
  65. gai = getaddrinfo(*ahost, servbuff, &hints, &res0);
  66. if (gai) {
  67. /* XXX: set errno? */
  68. return -1;
  69. }
  70. if (res0->ai_canonname) {
  71. strncpy(ahostbuf, res0->ai_canonname, sizeof(ahostbuf));
  72. ahostbuf[sizeof(ahostbuf)-1] = '\0';
  73. *ahost = ahostbuf;
  74. }
  75. else {
  76. *ahost = NULL;
  77. __set_errno(ENOENT);
  78. return -1;
  79. }
  80. ruserpass(res0->ai_canonname, &name, &pass);
  81. retry:
  82. s = socket(res0->ai_family, res0->ai_socktype, 0);
  83. if (s < 0) {
  84. perror("rexec: socket");
  85. return -1;
  86. }
  87. if (connect(s, res0->ai_addr, res0->ai_addrlen) < 0) {
  88. if (errno == ECONNREFUSED && timo <= 16) {
  89. (void) close(s);
  90. sleep(timo);
  91. timo *= 2;
  92. goto retry;
  93. }
  94. perror(res0->ai_canonname);
  95. return -1;
  96. }
  97. if (fd2p == 0) {
  98. (void) write(s, "", 1);
  99. port = 0;
  100. } else {
  101. char num[32];
  102. int s2;
  103. socklen_t sa2len;
  104. s2 = socket(res0->ai_family, res0->ai_socktype, 0);
  105. if (s2 < 0) {
  106. (void) close(s);
  107. return -1;
  108. }
  109. listen(s2, 1);
  110. sa2len = sizeof(sa2);
  111. if (getsockname(s2, (struct sockaddr *)&sa2, &sa2len) < 0) {
  112. perror("getsockname");
  113. (void) close(s2);
  114. goto bad;
  115. } else if (sa2len != SA_LEN((struct sockaddr *)&sa2)) {
  116. __set_errno(EINVAL);
  117. (void) close(s2);
  118. goto bad;
  119. }
  120. port = 0;
  121. if (!getnameinfo((struct sockaddr *)&sa2, sa2len,
  122. NULL, 0, servbuff, sizeof(servbuff),
  123. NI_NUMERICSERV))
  124. port = atoi(servbuff);
  125. (void) sprintf(num, "%u", port);
  126. (void) write(s, num, strlen(num)+1);
  127. {
  128. socklen_t len = sizeof(from);
  129. s3 = TEMP_FAILURE_RETRY(accept(s2,
  130. (struct sockaddr *)&from, &len));
  131. close(s2);
  132. if (s3 < 0) {
  133. perror("accept");
  134. port = 0;
  135. goto bad;
  136. }
  137. }
  138. *fd2p = s3;
  139. }
  140. (void) write(s, name, strlen(name) + 1);
  141. /* should public key encypt the password here */
  142. (void) write(s, pass, strlen(pass) + 1);
  143. (void) write(s, cmd, strlen(cmd) + 1);
  144. /* We don't need the memory allocated for the name and the password
  145. in ruserpass anymore. */
  146. if (name != orig_name)
  147. free((char *) name);
  148. if (pass != orig_pass)
  149. free((char *) pass);
  150. if (read(s, &c, 1) != 1) {
  151. perror(*ahost);
  152. goto bad;
  153. }
  154. if (c != 0) {
  155. while (read(s, &c, 1) == 1) {
  156. (void) write(2, &c, 1);
  157. if (c == '\n')
  158. break;
  159. }
  160. goto bad;
  161. }
  162. freeaddrinfo(res0);
  163. return s;
  164. bad:
  165. if (port)
  166. (void) close(*fd2p);
  167. (void) close(s);
  168. freeaddrinfo(res0);
  169. return -1;
  170. }
  171. libc_hidden_def(rexec_af)
  172. int
  173. rexec(char **ahost, int rport, const char *name, const char *pass,
  174. const char *cmd, int *fd2p)
  175. {
  176. return rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET);
  177. }