rexec.c 5.1 KB

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