rexec.c 5.1 KB

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