NetBSD/libexec/rshd/rshd.c

720 lines
18 KiB
C
Raw Normal View History

2001-02-05 01:14:13 +03:00
/* $NetBSD: rshd.c,v 1.23 2001/02/04 22:14:57 christos Exp $ */
2000-01-31 17:20:13 +03:00
/*
* Copyright (C) 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by WIDE Project and
* its contributors.
* 4. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
1997-10-07 14:37:30 +04:00
1993-03-21 12:45:37 +03:00
/*-
1994-06-05 19:33:20 +04:00
* Copyright (c) 1988, 1989, 1992, 1993, 1994
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
1997-10-07 14:37:30 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1997-10-07 14:37:30 +04:00
__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n");
#if 0
static char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94";
#else
2001-02-05 01:14:13 +03:00
__RCSID("$NetBSD: rshd.c,v 1.23 2001/02/04 22:14:57 christos Exp $");
1997-10-07 14:37:30 +04:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
/*
* remote shell server:
* [port]\0
* remuser\0
* locuser\0
* command\0
* data
*/
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/socket.h>
1994-06-05 19:33:20 +04:00
1993-03-21 12:45:37 +03:00
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
1994-06-05 19:33:20 +04:00
#include <fcntl.h>
#include <paths.h>
#include <pwd.h>
#include <signal.h>
1993-03-21 12:45:37 +03:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
1994-06-05 19:33:20 +04:00
#include <syslog.h>
#include <unistd.h>
#ifdef LOGIN_CAP
#include <login_cap.h>
#endif
1993-03-21 12:45:37 +03:00
int keepalive = 1;
1994-06-05 19:33:20 +04:00
int check_all;
int log_success; /* If TRUE, log all successful accesses */
1993-03-21 12:45:37 +03:00
int sent_null;
2000-01-31 17:20:13 +03:00
void doit __P((struct sockaddr *));
2000-10-10 23:54:37 +04:00
void error __P((const char *, ...))
__attribute__((__format__(__printf__, 1, 2)));
1994-06-05 19:33:20 +04:00
void getstr __P((char *, int, char *));
int local_domain __P((char *));
char *topdomain __P((char *));
void usage __P((void));
1997-10-07 14:37:30 +04:00
int main __P((int, char *[]));
1994-06-05 19:33:20 +04:00
#define OPTIONS "alnL"
2001-02-05 01:14:13 +03:00
extern int __check_rhosts_file;
extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
1993-03-21 12:45:37 +03:00
1994-06-05 19:33:20 +04:00
int
1993-03-21 12:45:37 +03:00
main(argc, argv)
int argc;
1994-06-05 19:33:20 +04:00
char *argv[];
1993-03-21 12:45:37 +03:00
{
struct linger linger;
int ch, on = 1, fromlen;
2000-01-31 17:20:13 +03:00
struct sockaddr_storage from;
1993-03-21 12:45:37 +03:00
openlog("rshd", LOG_PID, LOG_DAEMON);
1993-03-21 12:45:37 +03:00
opterr = 0;
while ((ch = getopt(argc, argv, OPTIONS)) != -1)
1993-03-21 12:45:37 +03:00
switch (ch) {
case 'a':
check_all = 1;
break;
case 'l':
1994-05-20 02:54:19 +04:00
__check_rhosts_file = 0;
1993-03-21 12:45:37 +03:00
break;
case 'n':
keepalive = 0;
break;
case 'L':
1994-06-05 19:33:20 +04:00
log_success = 1;
break;
1993-03-21 12:45:37 +03:00
case '?':
default:
usage();
1994-06-05 19:33:20 +04:00
break;
1993-03-21 12:45:37 +03:00
}
argc -= optind;
argv += optind;
2000-01-31 17:20:13 +03:00
fromlen = sizeof (from); /* xxx */
1993-03-21 12:45:37 +03:00
if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
syslog(LOG_ERR, "getpeername: %m");
_exit(1);
}
#if 0
if (((struct sockaddr *)&from)->sa_family == AF_INET6 &&
IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr) &&
sizeof(struct sockaddr_in) <= sizeof(from)) {
struct sockaddr_in sin;
struct sockaddr_in6 *sin6;
const int off = sizeof(struct sockaddr_in6) -
sizeof(struct sockaddr_in);
sin6 = (struct sockaddr_in6 *)&from;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_len = sizeof(struct sockaddr_in);
memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[off],
sizeof(sin.sin_addr));
memcpy(&from, &sin, sizeof(sin));
fromlen = sin.sin_len;
}
#else
if (((struct sockaddr *)&from)->sa_family == AF_INET6 &&
IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) {
char hbuf[NI_MAXHOST];
if (getnameinfo((struct sockaddr *)&from, fromlen, hbuf,
sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) {
strncpy(hbuf, "invalid", sizeof(hbuf));
}
syslog(LOG_ERR, "malformed \"from\" address (v4 mapped, %s)\n",
hbuf);
exit(1);
}
#endif
1993-03-21 12:45:37 +03:00
if (keepalive &&
setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
sizeof(on)) < 0)
syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
linger.l_onoff = 1;
linger.l_linger = 60; /* XXX */
if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
sizeof (linger)) < 0)
syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
2000-01-31 17:20:13 +03:00
doit((struct sockaddr *)&from);
1994-06-05 19:33:20 +04:00
/* NOTREACHED */
1997-10-07 14:37:30 +04:00
#ifdef __GNUC__
exit(0);
#endif
1993-03-21 12:45:37 +03:00
}
char username[20] = "USER=";
char homedir[64] = "HOME=";
char shell[64] = "SHELL=";
char path[100] = "PATH=";
char *envinit[] =
{homedir, shell, path, username, 0};
char **environ;
1994-06-05 19:33:20 +04:00
void
1993-03-21 12:45:37 +03:00
doit(fromp)
2000-01-31 17:20:13 +03:00
struct sockaddr *fromp;
1993-03-21 12:45:37 +03:00
{
1994-06-05 19:33:20 +04:00
struct passwd *pwd;
in_port_t port;
1993-03-21 12:45:37 +03:00
fd_set ready, readfrom;
1997-10-07 14:37:30 +04:00
int cc, nfd, pv[2], pid, s = -1; /* XXX gcc */
1993-03-21 12:45:37 +03:00
int one = 1;
1997-10-07 14:37:30 +04:00
char *hostname, *errorstr, *errorhost = NULL; /* XXX gcc */
1998-07-26 23:44:12 +04:00
const char *cp;
char sig, buf[BUFSIZ];
1994-06-05 19:33:20 +04:00
char cmdbuf[NCARGS+1], locuser[16], remuser[16];
1993-03-21 12:45:37 +03:00
char remotehost[2 * MAXHOSTNAMELEN + 1];
char hostnamebuf[2 * MAXHOSTNAMELEN + 1];
#ifdef LOGIN_CAP
login_cap_t *lc;
#endif
2000-01-31 17:20:13 +03:00
char naddr[NI_MAXHOST];
char saddr[NI_MAXHOST];
char raddr[NI_MAXHOST];
char pbuf[NI_MAXSERV];
int af = fromp->sa_family;
u_int16_t *portp;
struct addrinfo hints, *res, *res0;
int gaierror;
#ifdef NI_WITHSCOPEID
const int niflags = NI_NUMERICHOST | NI_NUMERICSERV | NI_WITHSCOPEID;
#else
const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
#endif
1993-03-21 12:45:37 +03:00
(void) signal(SIGINT, SIG_DFL);
(void) signal(SIGQUIT, SIG_DFL);
(void) signal(SIGTERM, SIG_DFL);
#ifdef DEBUG
{ int t = open(_PATH_TTY, 2);
if (t >= 0) {
ioctl(t, TIOCNOTTY, (char *)0);
(void) close(t);
}
}
#endif
2000-01-31 17:20:13 +03:00
switch (af) {
case AF_INET:
portp = &((struct sockaddr_in *)fromp)->sin_port;
break;
#ifdef INET6
case AF_INET6:
portp = &((struct sockaddr_in6 *)fromp)->sin6_port;
break;
#endif
default:
syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af);
exit(1);
}
if (getnameinfo(fromp, fromp->sa_len, naddr, sizeof(naddr),
pbuf, sizeof(pbuf), niflags) != 0) {
syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af);
1993-03-21 12:45:37 +03:00
exit(1);
}
#ifdef IP_OPTIONS
2000-01-31 17:20:13 +03:00
if (af == AF_INET)
1993-03-21 12:45:37 +03:00
{
u_char optbuf[BUFSIZ/3], *cp;
char lbuf[BUFSIZ], *lp;
int optsize = sizeof(optbuf), ipproto;
struct protoent *ip;
if ((ip = getprotobyname("ip")) != NULL)
ipproto = ip->p_proto;
else
ipproto = IPPROTO_IP;
if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
optsize != 0) {
lp = lbuf;
for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
sprintf(lp, " %2.2x", *cp);
syslog(LOG_NOTICE,
"Connection received from %s using IP options (ignored):%s",
2000-01-31 17:20:13 +03:00
naddr, lbuf);
1993-03-21 12:45:37 +03:00
if (setsockopt(0, ipproto, IP_OPTIONS,
(char *)NULL, optsize) != 0) {
syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
exit(1);
}
}
}
#endif
2000-01-31 17:20:13 +03:00
if (ntohs(*portp) >= IPPORT_RESERVED
|| ntohs(*portp) < IPPORT_RESERVED/2) {
syslog(LOG_NOTICE|LOG_AUTH,
"Connection from %s on illegal port %u",
2000-01-31 17:20:13 +03:00
naddr, ntohs(*portp));
exit(1);
}
1993-03-21 12:45:37 +03:00
(void) alarm(60);
port = 0;
for (;;) {
char c;
1994-06-05 19:33:20 +04:00
if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
1993-03-21 12:45:37 +03:00
if (cc < 0)
syslog(LOG_ERR, "read: %m");
shutdown(0, SHUT_RDWR);
1993-03-21 12:45:37 +03:00
exit(1);
}
if (c == 0)
1993-03-21 12:45:37 +03:00
break;
port = port * 10 + c - '0';
}
(void) alarm(0);
if (port != 0) {
int lport = IPPORT_RESERVED - 1;
2000-01-31 17:20:13 +03:00
s = rresvport_af(&lport, af);
1993-03-21 12:45:37 +03:00
if (s < 0) {
syslog(LOG_ERR, "can't get stderr port: %m");
exit(1);
}
1998-01-08 06:30:49 +03:00
if (port >= IPPORT_RESERVED) {
syslog(LOG_ERR, "2nd port not reserved\n");
exit(1);
}
2000-01-31 17:20:13 +03:00
*portp = htons(port);
if (connect(s, (struct sockaddr *)fromp, fromp->sa_len) < 0) {
syslog(LOG_ERR, "connect second port %d: %m", port);
1993-03-21 12:45:37 +03:00
exit(1);
}
}
#ifdef notdef
/* from inetd, socket is already on 0, 1, 2 */
dup2(f, 0);
dup2(f, 1);
dup2(f, 2);
#endif
1994-06-05 19:33:20 +04:00
errorstr = NULL;
2000-01-31 17:20:13 +03:00
if (getnameinfo(fromp, fromp->sa_len, saddr, sizeof(saddr),
NULL, 0, NI_NAMEREQD) == 0) {
1993-03-21 12:45:37 +03:00
/*
2000-01-31 17:20:13 +03:00
* If name returned by getnameinfo is in our domain,
1993-03-21 12:45:37 +03:00
* attempt to verify that we haven't been fooled by someone
* in a remote net; look up the name and check that this
* address corresponds to the name.
*/
2000-01-31 17:20:13 +03:00
hostname = saddr;
res0 = NULL;
2000-01-31 17:20:13 +03:00
if (check_all || local_domain(saddr)) {
strncpy(remotehost, saddr, sizeof(remotehost) - 1);
1993-03-21 12:45:37 +03:00
remotehost[sizeof(remotehost) - 1] = 0;
errorhost = remotehost;
2000-01-31 17:20:13 +03:00
memset(&hints, 0, sizeof(hints));
hints.ai_family = fromp->sa_family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_CANONNAME;
gaierror = getaddrinfo(remotehost, pbuf, &hints, &res0);
if (gaierror) {
syslog(LOG_NOTICE,
2000-01-31 17:20:13 +03:00
"Couldn't look up address for %s: %s",
remotehost, gai_strerror(gaierror));
1993-03-21 12:45:37 +03:00
errorstr =
"Couldn't look up address for your host (%s)\n";
2000-01-31 17:20:13 +03:00
hostname = naddr;
} else {
for (res = res0; res; res = res->ai_next) {
if (res->ai_family != fromp->sa_family)
continue;
if (res->ai_addrlen != fromp->sa_len)
continue;
if (getnameinfo(res->ai_addr,
res->ai_addrlen,
raddr, sizeof(raddr), NULL, 0,
niflags) == 0
&& strcmp(naddr, raddr) == 0) {
hostname = res->ai_canonname
? res->ai_canonname
: saddr;
break;
}
}
if (res == NULL) {
1993-03-21 12:45:37 +03:00
syslog(LOG_NOTICE,
"Host addr %s not listed for host %s",
2000-01-31 17:20:13 +03:00
naddr, res0->ai_canonname
? res0->ai_canonname
: saddr);
1993-03-21 12:45:37 +03:00
errorstr =
"Host address mismatch for %s\n";
2000-01-31 17:20:13 +03:00
hostname = naddr;
1993-03-21 12:45:37 +03:00
}
}
}
hostname = strncpy(hostnamebuf, hostname,
sizeof(hostnamebuf) - 1);
if (res0)
freeaddrinfo(res0);
2000-01-31 17:20:13 +03:00
} else {
errorhost = hostname = strncpy(hostnamebuf,
2000-01-31 17:20:13 +03:00
naddr, sizeof(hostnamebuf) - 1);
}
1993-03-21 12:45:37 +03:00
hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
1993-03-21 12:45:37 +03:00
getstr(remuser, sizeof(remuser), "remuser");
1993-03-21 12:45:37 +03:00
getstr(locuser, sizeof(locuser), "locuser");
getstr(cmdbuf, sizeof(cmdbuf), "command");
setpwent();
pwd = getpwnam(locuser);
if (pwd == NULL) {
1994-06-05 19:33:20 +04:00
syslog(LOG_INFO|LOG_AUTH,
"%s@%s as %s: unknown login. cmd='%.80s'",
remuser, hostname, locuser, cmdbuf);
1993-03-21 12:45:37 +03:00
if (errorstr == NULL)
errorstr = "Login incorrect.\n";
goto fail;
}
#ifdef LOGIN_CAP
lc = login_getclass(pwd ? pwd->pw_class : NULL);
#endif
1993-03-21 12:45:37 +03:00
if (chdir(pwd->pw_dir) < 0) {
#ifdef LOGIN_CAP
if (chdir("/") < 0 ||
login_getcapbool(lc, "requirehome", pwd->pw_uid ? 1 : 0)) {
syslog(LOG_INFO|LOG_AUTH,
"%s@%s as %s: no home directory. cmd='%.80s'",
remuser, hostname, locuser, cmdbuf);
error("No remote home directory.\n");
exit(0);
}
#else
1993-03-21 12:45:37 +03:00
(void) chdir("/");
#ifdef notdef
1994-06-05 19:33:20 +04:00
syslog(LOG_INFO|LOG_AUTH,
"%s@%s as %s: no home directory. cmd='%.80s'",
remuser, hostname, locuser, cmdbuf);
1993-03-21 12:45:37 +03:00
error("No remote directory.\n");
exit(1);
#endif /* notdef */
#endif /* LOGIN_CAP */
1993-03-21 12:45:37 +03:00
}
if (errorstr ||
(pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
2000-01-31 17:20:13 +03:00
iruserok_sa(fromp, fromp->sa_len, pwd->pw_uid == 0, remuser,
locuser) < 0)) {
if (__rcmd_errstr)
syslog(LOG_INFO|LOG_AUTH,
1994-06-05 19:33:20 +04:00
"%s@%s as %s: permission denied (%s). cmd='%.80s'",
remuser, hostname, locuser, __rcmd_errstr,
cmdbuf);
else
syslog(LOG_INFO|LOG_AUTH,
1994-06-05 19:33:20 +04:00
"%s@%s as %s: permission denied. cmd='%.80s'",
remuser, hostname, locuser, cmdbuf);
1993-03-21 12:45:37 +03:00
fail:
if (errorstr == NULL)
errorstr = "Permission denied.\n";
error(errorstr, errorhost);
exit(1);
}
1993-03-21 12:45:37 +03:00
if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
error("Logins currently disabled.\n");
exit(1);
}
1994-06-05 19:33:20 +04:00
(void) write(STDERR_FILENO, "\0", 1);
1993-03-21 12:45:37 +03:00
sent_null = 1;
if (port) {
if (pipe(pv) < 0) {
error("Can't make pipe.\n");
exit(1);
}
pid = fork();
if (pid == -1) {
error("Can't fork; try again.\n");
exit(1);
}
if (pid) {
{
1994-06-05 19:33:20 +04:00
(void) close(0);
(void) close(1);
1993-03-21 12:45:37 +03:00
}
1994-06-05 19:33:20 +04:00
(void) close(2);
(void) close(pv[1]);
1993-03-21 12:45:37 +03:00
FD_ZERO(&readfrom);
FD_SET(s, &readfrom);
FD_SET(pv[0], &readfrom);
if (pv[0] > s)
nfd = pv[0];
else
nfd = s;
ioctl(pv[0], FIONBIO, (char *)&one);
1993-03-21 12:45:37 +03:00
/* should set s nbio! */
nfd++;
do {
ready = readfrom;
if (select(nfd, &ready, (fd_set *)0,
(fd_set *)0, (struct timeval *)0) < 0)
break;
1993-03-21 12:45:37 +03:00
if (FD_ISSET(s, &ready)) {
int ret;
ret = read(s, &sig, 1);
1993-03-21 12:45:37 +03:00
if (ret <= 0)
FD_CLR(s, &readfrom);
else
killpg(pid, sig);
}
if (FD_ISSET(pv[0], &ready)) {
errno = 0;
cc = read(pv[0], buf, sizeof(buf));
if (cc <= 0) {
shutdown(s, SHUT_RDWR);
1993-03-21 12:45:37 +03:00
FD_CLR(pv[0], &readfrom);
} else {
(void) write(s, buf, cc);
1993-03-21 12:45:37 +03:00
}
}
} while (FD_ISSET(s, &readfrom) ||
FD_ISSET(pv[0], &readfrom));
exit(0);
}
setpgrp(0, getpid());
1994-06-05 19:33:20 +04:00
(void) close(s);
(void) close(pv[0]);
1993-03-21 12:45:37 +03:00
dup2(pv[1], 2);
close(pv[1]);
}
#if BSD > 43
if (setlogin(pwd->pw_name) < 0)
syslog(LOG_ERR, "setlogin() failed: %m");
#endif
if (*pwd->pw_shell == '\0')
pwd->pw_shell = _PATH_BSHELL;
#ifdef LOGIN_CAP
{
char *sh;
if((sh = login_getcapstr(lc, "shell", NULL, NULL))) {
if(!(sh = strdup(sh))) {
syslog(LOG_ERR, "Cannot alloc mem");
exit(1);
}
pwd->pw_shell = sh;
}
}
#endif
1993-03-21 12:45:37 +03:00
environ = envinit;
strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
strcat(path, _PATH_DEFPATH);
strncat(shell, pwd->pw_shell, sizeof(shell)-7);
strncat(username, pwd->pw_name, sizeof(username)-6);
#ifdef LOGIN_CAP
if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
syslog(LOG_ERR, "setusercontext: %m");
exit(1);
}
login_close(lc);
#else
(void) setgid((gid_t)pwd->pw_gid);
initgroups(pwd->pw_name, pwd->pw_gid);
(void) setuid((uid_t)pwd->pw_uid);
#endif
1993-03-21 12:45:37 +03:00
endpwent();
1994-06-05 19:33:20 +04:00
if (log_success || pwd->pw_uid == 0) {
syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
remuser, hostname, locuser, cmdbuf);
1993-03-21 12:45:37 +03:00
}
cp = strrchr(pwd->pw_shell, '/');
if (cp)
cp++;
else
cp = pwd->pw_shell;
1993-03-21 12:45:37 +03:00
execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
perror(pwd->pw_shell);
exit(1);
}
/*
1994-06-05 19:33:20 +04:00
* Report error to client. Note: can't be used until second socket has
* connected to client, or older clients will hang waiting for that
* connection first.
1993-03-21 12:45:37 +03:00
*/
1994-06-05 19:33:20 +04:00
#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
void
#if __STDC__
error(const char *fmt, ...)
#else
error(fmt, va_alist)
1993-03-21 12:45:37 +03:00
char *fmt;
1994-06-05 19:33:20 +04:00
va_dcl
#endif
1993-03-21 12:45:37 +03:00
{
1994-06-05 19:33:20 +04:00
va_list ap;
int len;
char *bp, buf[BUFSIZ];
#if __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
bp = buf;
if (sent_null == 0) {
1993-03-21 12:45:37 +03:00
*bp++ = 1;
1994-06-05 19:33:20 +04:00
len = 1;
} else
len = 0;
(void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap);
(void)write(STDERR_FILENO, buf, len + strlen(bp));
1993-03-21 12:45:37 +03:00
}
1994-06-05 19:33:20 +04:00
void
1993-03-21 12:45:37 +03:00
getstr(buf, cnt, err)
1994-06-05 19:33:20 +04:00
char *buf, *err;
1993-03-21 12:45:37 +03:00
int cnt;
{
char c;
do {
1994-06-05 19:33:20 +04:00
if (read(STDIN_FILENO, &c, 1) != 1)
1993-03-21 12:45:37 +03:00
exit(1);
*buf++ = c;
if (--cnt == 0) {
error("%s too long\n", err);
exit(1);
}
} while (c != 0);
}
/*
* Check whether host h is in our local domain,
* defined as sharing the last two components of the domain part,
* or the entire domain part if the local domain has only one component.
* If either name is unqualified (contains no '.'),
* assume that the host is local, as it will be
* interpreted as such.
*/
1994-06-05 19:33:20 +04:00
int
1993-03-21 12:45:37 +03:00
local_domain(h)
char *h;
{
char localhost[MAXHOSTNAMELEN + 1];
1994-06-05 19:33:20 +04:00
char *p1, *p2;
1993-03-21 12:45:37 +03:00
localhost[0] = 0;
(void)gethostname(localhost, sizeof(localhost));
localhost[sizeof(localhost) - 1] = '\0';
1993-03-21 12:45:37 +03:00
p1 = topdomain(localhost);
p2 = topdomain(h);
if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
1994-06-05 19:33:20 +04:00
return (1);
return (0);
1993-03-21 12:45:37 +03:00
}
char *
topdomain(h)
char *h;
{
1994-06-05 19:33:20 +04:00
char *p, *maybe = NULL;
1993-03-21 12:45:37 +03:00
int dots = 0;
for (p = h + strlen(h); p >= h; p--) {
if (*p == '.') {
if (++dots == 2)
return (p);
maybe = p;
}
}
return (maybe);
}
1994-06-05 19:33:20 +04:00
void
1993-03-21 12:45:37 +03:00
usage()
{
1994-06-05 19:33:20 +04:00
1993-03-21 12:45:37 +03:00
syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
1994-06-05 19:33:20 +04:00
exit(2);
1993-03-21 12:45:37 +03:00
}