Blame inet/rexec.c

Packit 6c4009
/*
Packit 6c4009
 * Copyright (c) 1980, 1993
Packit 6c4009
 *	The Regents of the University of California.  All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions
Packit 6c4009
 * are met:
Packit 6c4009
 * 1. Redistributions of source code must retain the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer.
Packit 6c4009
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer in the
Packit 6c4009
 *    documentation and/or other materials provided with the distribution.
Packit 6c4009
 * 4. Neither the name of the University nor the names of its contributors
Packit 6c4009
 *    may be used to endorse or promote products derived from this software
Packit 6c4009
 *    without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 6c4009
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 6c4009
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 6c4009
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 6c4009
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 6c4009
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 6c4009
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 6c4009
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 6c4009
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 6c4009
 * SUCH DAMAGE.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/socket.h>
Packit 6c4009
Packit 6c4009
#include <netinet/in.h>
Packit 6c4009
Packit 6c4009
#include <alloca.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <netdb.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/uio.h>
Packit 6c4009
Packit 6c4009
int	rexecoptions;
Packit 6c4009
libc_freeres_ptr (static char *ahostbuf);
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
rexec_af (char **ahost, int rport, const char *name, const char *pass,
Packit 6c4009
	  const char *cmd, int *fd2p, sa_family_t af)
Packit 6c4009
{
Packit 6c4009
	struct sockaddr_storage from;
Packit 6c4009
	struct addrinfo hints, *res0;
Packit 6c4009
	const char *orig_name = name;
Packit 6c4009
	const char *orig_pass = pass;
Packit 6c4009
	u_short port = 0;
Packit 6c4009
	int s, timo = 1, s3;
Packit 6c4009
	char c;
Packit 6c4009
	int gai;
Packit 6c4009
	char servbuff[NI_MAXSERV];
Packit 6c4009
Packit 6c4009
	__snprintf(servbuff, sizeof(servbuff), "%d", ntohs(rport));
Packit 6c4009
	servbuff[sizeof(servbuff) - 1] = '\0';
Packit 6c4009
Packit 6c4009
	memset(&hints, '\0', sizeof(hints));
Packit 6c4009
	hints.ai_family = af;
Packit 6c4009
	hints.ai_socktype = SOCK_STREAM;
Packit 6c4009
	hints.ai_flags = AI_CANONNAME;
Packit 6c4009
	gai = getaddrinfo(*ahost, servbuff, &hints, &res0);
Packit 6c4009
	if (gai){
Packit 6c4009
		/* XXX: set errno? */
Packit 6c4009
		return -1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	if (res0->ai_canonname){
Packit 6c4009
		free (ahostbuf);
Packit 6c4009
		ahostbuf = __strdup (res0->ai_canonname);
Packit 6c4009
		if (ahostbuf == NULL) {
Packit 6c4009
			perror ("rexec: strdup");
Packit 6c4009
			return (-1);
Packit 6c4009
		}
Packit 6c4009
		*ahost = ahostbuf;
Packit 6c4009
	} else {
Packit 6c4009
		*ahost = NULL;
Packit 6c4009
		__set_errno (ENOENT);
Packit 6c4009
		return -1;
Packit 6c4009
	}
Packit 6c4009
	ruserpass(res0->ai_canonname, &name, &pass);
Packit 6c4009
retry:
Packit 6c4009
	/* NB: No SOCK_CLOEXEC for backwards compatibility.  */
Packit 6c4009
	s = __socket(res0->ai_family, res0->ai_socktype, 0);
Packit 6c4009
	if (s < 0) {
Packit 6c4009
		perror("rexec: socket");
Packit 6c4009
		return (-1);
Packit 6c4009
	}
Packit 6c4009
	if (__connect(s, res0->ai_addr, res0->ai_addrlen) < 0) {
Packit 6c4009
		if (errno == ECONNREFUSED && timo <= 16) {
Packit 6c4009
			(void) __close(s);
Packit 6c4009
			__sleep(timo);
Packit 6c4009
			timo *= 2;
Packit 6c4009
			goto retry;
Packit 6c4009
		}
Packit 6c4009
		perror(res0->ai_canonname);
Packit 6c4009
		return (-1);
Packit 6c4009
	}
Packit 6c4009
	if (fd2p == 0) {
Packit 6c4009
		(void) __write(s, "", 1);
Packit 6c4009
		port = 0;
Packit 6c4009
	} else {
Packit 6c4009
		char num[32];
Packit 6c4009
		int s2;
Packit 6c4009
		union
Packit 6c4009
		{
Packit 6c4009
		  struct sockaddr_storage ss;
Packit 6c4009
		  struct sockaddr sa;
Packit 6c4009
		} sa2;
Packit 6c4009
		socklen_t sa2len;
Packit 6c4009
Packit 6c4009
		s2 = __socket(res0->ai_family, res0->ai_socktype, 0);
Packit 6c4009
		if (s2 < 0) {
Packit 6c4009
			(void) __close(s);
Packit 6c4009
			return (-1);
Packit 6c4009
		}
Packit 6c4009
		__listen(s2, 1);
Packit 6c4009
		sa2len = sizeof (sa2);
Packit 6c4009
		if (__getsockname(s2, &sa2.sa, &sa2len) < 0) {
Packit 6c4009
			perror("getsockname");
Packit 6c4009
			(void) __close(s2);
Packit 6c4009
			goto bad;
Packit 6c4009
		} else if (sa2len != SA_LEN(&sa2.sa)) {
Packit 6c4009
			__set_errno(EINVAL);
Packit 6c4009
			(void) __close(s2);
Packit 6c4009
			goto bad;
Packit 6c4009
		}
Packit 6c4009
		port = 0;
Packit 6c4009
		if (!getnameinfo(&sa2.sa, sa2len,
Packit 6c4009
				 NULL, 0, servbuff, sizeof(servbuff),
Packit 6c4009
				 NI_NUMERICSERV))
Packit 6c4009
			port = atoi(servbuff);
Packit 6c4009
		(void) sprintf(num, "%u", port);
Packit 6c4009
		(void) __write(s, num, strlen(num)+1);
Packit 6c4009
		{ socklen_t len = sizeof (from);
Packit 6c4009
		  s3 = TEMP_FAILURE_RETRY (accept(s2, (struct sockaddr *)&from,
Packit 6c4009
						  &len));
Packit 6c4009
		  __close(s2);
Packit 6c4009
		  if (s3 < 0) {
Packit 6c4009
			perror("accept");
Packit 6c4009
			port = 0;
Packit 6c4009
			goto bad;
Packit 6c4009
		  }
Packit 6c4009
		}
Packit 6c4009
		*fd2p = s3;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
	struct iovec iov[3] =
Packit 6c4009
	  {
Packit 6c4009
	    [0] = { .iov_base = (void *) name, .iov_len = strlen (name) + 1 },
Packit 6c4009
	    /* should public key encypt the password here */
Packit 6c4009
	    [1] = { .iov_base = (void *) pass, .iov_len = strlen (pass) + 1 },
Packit 6c4009
	    [2] = { .iov_base = (void *) cmd, .iov_len = strlen (cmd) + 1 }
Packit 6c4009
	  };
Packit 6c4009
	(void) TEMP_FAILURE_RETRY (__writev (s, iov, 3));
Packit 6c4009
Packit 6c4009
	/* We don't need the memory allocated for the name and the password
Packit 6c4009
	   in ruserpass anymore.  */
Packit 6c4009
	if (name != orig_name)
Packit 6c4009
	  free ((char *) name);
Packit 6c4009
	if (pass != orig_pass)
Packit 6c4009
	  free ((char *) pass);
Packit 6c4009
Packit 6c4009
	if (__read(s, &c, 1) != 1) {
Packit 6c4009
		perror(*ahost);
Packit 6c4009
		goto bad;
Packit 6c4009
	}
Packit 6c4009
	if (c != 0) {
Packit 6c4009
		while (__read(s, &c, 1) == 1) {
Packit 6c4009
			(void) __write(2, &c, 1);
Packit 6c4009
			if (c == '\n')
Packit 6c4009
				break;
Packit 6c4009
		}
Packit 6c4009
		goto bad;
Packit 6c4009
	}
Packit 6c4009
	freeaddrinfo(res0);
Packit 6c4009
	return (s);
Packit 6c4009
bad:
Packit 6c4009
	if (port)
Packit 6c4009
		(void) __close(*fd2p);
Packit 6c4009
	(void) __close(s);
Packit 6c4009
	freeaddrinfo(res0);
Packit 6c4009
	return (-1);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (rexec_af)
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
rexec (char **ahost, int rport, const char *name, const char *pass,
Packit 6c4009
       const char *cmd, int *fd2p)
Packit 6c4009
{
Packit 6c4009
	return rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET);
Packit 6c4009
}