rexec.c 5.4 KB

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