rexec.c 5.0 KB

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