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