rexec.c 5.4 KB

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