rexec.c 5.7 KB

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