1994-05-27 12:39:25 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1988, 1989, 1992, 1993, 1994
|
|
|
|
* The Regents of the University of California. 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 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-09-28 08:38:04 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:39:25 +00:00
|
|
|
"@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-12-02 12:30:04 +00:00
|
|
|
#if 0
|
1997-09-28 08:38:04 +00:00
|
|
|
static const char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94";
|
1997-12-02 12:30:04 +00:00
|
|
|
#endif
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1994-05-27 12:39:25 +00: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>
|
|
|
|
|
1997-02-09 04:16:27 +00:00
|
|
|
#include <netinet/in_systm.h>
|
1994-05-27 12:39:25 +00:00
|
|
|
#include <netinet/in.h>
|
1997-02-09 04:16:27 +00:00
|
|
|
#include <netinet/ip.h>
|
1998-12-01 23:27:24 +00:00
|
|
|
#include <netinet/tcp.h>
|
1994-05-27 12:39:25 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
1999-04-06 23:06:00 +00:00
|
|
|
#include <libutil.h>
|
1994-05-27 12:39:25 +00:00
|
|
|
#include <paths.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
1997-04-23 03:06:47 +00:00
|
|
|
#ifdef LOGIN_CAP
|
|
|
|
#include <login_cap.h>
|
|
|
|
#endif
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2000-01-25 14:52:10 +00:00
|
|
|
/* wrapper for KAME-special getnameinfo() */
|
|
|
|
#ifndef NI_WITHSCOPEID
|
|
|
|
#define NI_WITHSCOPEID 0
|
|
|
|
#endif
|
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
int keepalive = 1;
|
|
|
|
int log_success; /* If TRUE, log all successful accesses */
|
|
|
|
int sent_null;
|
1998-12-01 23:27:24 +00:00
|
|
|
int no_delay;
|
1999-09-19 22:05:32 +00:00
|
|
|
#ifdef CRYPT
|
|
|
|
int doencrypt = 0;
|
|
|
|
#endif
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2000-01-25 14:52:10 +00:00
|
|
|
union sockunion {
|
|
|
|
struct sockinet {
|
|
|
|
u_char si_len;
|
|
|
|
u_char si_family;
|
|
|
|
u_short si_port;
|
|
|
|
} su_si;
|
|
|
|
struct sockaddr_in su_sin;
|
|
|
|
struct sockaddr_in6 su_sin6;
|
|
|
|
};
|
|
|
|
#define su_len su_si.si_len
|
|
|
|
#define su_family su_si.si_family
|
|
|
|
#define su_port su_si.si_port
|
|
|
|
|
|
|
|
void doit __P((union sockunion *));
|
1994-05-27 12:39:25 +00:00
|
|
|
void error __P((const char *, ...));
|
|
|
|
void getstr __P((char *, int, char *));
|
|
|
|
int local_domain __P((char *));
|
|
|
|
char *topdomain __P((char *));
|
|
|
|
void usage __P((void));
|
|
|
|
|
1999-09-19 22:05:32 +00:00
|
|
|
#ifndef NO_PAM
|
|
|
|
extern int auth_pam __P((char *));
|
1994-05-27 12:39:25 +00:00
|
|
|
#endif
|
|
|
|
|
1999-09-19 22:05:32 +00:00
|
|
|
#define OPTIONS "alnDL"
|
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
extern int __check_rhosts_file;
|
|
|
|
struct linger linger;
|
|
|
|
int ch, on = 1, fromlen;
|
2000-01-25 14:52:10 +00:00
|
|
|
struct sockaddr_storage from;
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
|
|
|
|
|
|
|
|
opterr = 0;
|
1997-03-28 15:48:21 +00:00
|
|
|
while ((ch = getopt(argc, argv, OPTIONS)) != -1)
|
1994-05-27 12:39:25 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
1998-12-16 07:20:45 +00:00
|
|
|
/* ignored for compatability */
|
1994-05-27 12:39:25 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
__check_rhosts_file = 0;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
keepalive = 0;
|
|
|
|
break;
|
|
|
|
#ifdef CRYPT
|
|
|
|
case 'x':
|
|
|
|
doencrypt = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
1998-12-01 23:27:24 +00:00
|
|
|
case 'D':
|
|
|
|
no_delay = 1;
|
|
|
|
break;
|
1994-05-27 12:39:25 +00:00
|
|
|
case 'L':
|
|
|
|
log_success = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
#ifdef CRYPT
|
1999-09-19 22:05:32 +00:00
|
|
|
if (doencrypt) {
|
1994-05-27 12:39:25 +00:00
|
|
|
syslog(LOG_ERR, "-k is required for -x");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fromlen = sizeof (from);
|
|
|
|
if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
|
|
|
|
syslog(LOG_ERR, "getpeername: %m");
|
1998-05-05 00:28:51 +00:00
|
|
|
exit(1);
|
1994-05-27 12:39:25 +00: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");
|
1998-12-01 23:27:24 +00:00
|
|
|
if (no_delay &&
|
|
|
|
setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
|
|
|
|
syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
|
2000-01-25 14:52:10 +00:00
|
|
|
doit((union sockunion *)&from);
|
1994-05-27 12:39:25 +00:00
|
|
|
/* NOTREACHED */
|
1997-09-28 08:38:04 +00:00
|
|
|
return(0);
|
1994-05-27 12:39:25 +00: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;
|
|
|
|
|
|
|
|
void
|
|
|
|
doit(fromp)
|
2000-01-25 14:52:10 +00:00
|
|
|
union sockunion *fromp;
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
|
|
|
extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
|
|
|
|
struct passwd *pwd;
|
|
|
|
u_short port;
|
|
|
|
fd_set ready, readfrom;
|
|
|
|
int cc, nfd, pv[2], pid, s;
|
|
|
|
int one = 1;
|
1999-04-06 23:06:00 +00:00
|
|
|
char *errorstr;
|
1994-05-27 12:39:25 +00:00
|
|
|
char *cp, sig, buf[BUFSIZ];
|
|
|
|
char cmdbuf[NCARGS+1], locuser[16], remuser[16];
|
2000-01-25 14:52:10 +00:00
|
|
|
char fromhost[2 * MAXHOSTNAMELEN + 1];
|
|
|
|
char numericname[INET6_ADDRSTRLEN];
|
|
|
|
int af = fromp->su_family, err;
|
1999-09-19 22:05:32 +00:00
|
|
|
int retval;
|
|
|
|
#ifdef CRYPT
|
|
|
|
int rc;
|
|
|
|
int pv1[2], pv2[2];
|
|
|
|
#endif
|
1997-04-23 03:06:47 +00:00
|
|
|
#ifdef LOGIN_CAP
|
|
|
|
login_cap_t *lc;
|
|
|
|
#endif
|
1994-05-27 12:39:25 +00: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-25 14:52:10 +00:00
|
|
|
fromp->su_port = ntohs((u_short)fromp->su_port);
|
|
|
|
if (af != AF_INET
|
|
|
|
#ifdef INET6
|
|
|
|
&& af != AF_INET6
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af);
|
1994-05-27 12:39:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2000-01-25 14:52:10 +00:00
|
|
|
err = getnameinfo((struct sockaddr *)fromp, fromp->su_len, numericname,
|
|
|
|
sizeof(numericname), NULL, 0,
|
|
|
|
NI_NUMERICHOST|NI_WITHSCOPEID);
|
|
|
|
/* XXX: do 'err' check */
|
1994-05-27 12:39:25 +00:00
|
|
|
#ifdef IP_OPTIONS
|
2000-01-25 14:52:10 +00:00
|
|
|
if (af == AF_INET) {
|
1997-02-09 04:16:27 +00:00
|
|
|
u_char optbuf[BUFSIZ/3];
|
|
|
|
int optsize = sizeof(optbuf), ipproto, i;
|
1994-05-27 12:39:25 +00:00
|
|
|
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) {
|
1997-02-09 04:16:27 +00:00
|
|
|
for (i = 0; i < optsize; ) {
|
|
|
|
u_char c = optbuf[i];
|
|
|
|
if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
|
|
|
|
syslog(LOG_NOTICE,
|
1997-12-02 12:30:04 +00:00
|
|
|
"connection refused from %s with IP option %s",
|
2000-01-25 14:52:10 +00:00
|
|
|
numericname,
|
1997-02-09 04:16:27 +00:00
|
|
|
c == IPOPT_LSRR ? "LSRR" : "SSRR");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (c == IPOPT_EOL)
|
|
|
|
break;
|
|
|
|
i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-01-25 14:52:10 +00:00
|
|
|
if (fromp->su_port >= IPPORT_RESERVED ||
|
|
|
|
fromp->su_port < IPPORT_RESERVED/2) {
|
1999-09-19 22:05:32 +00:00
|
|
|
syslog(LOG_NOTICE|LOG_AUTH,
|
|
|
|
"connection from %s on illegal port %u",
|
2000-01-25 14:52:10 +00:00
|
|
|
numericname,
|
|
|
|
fromp->su_port);
|
1999-09-19 22:05:32 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
(void) alarm(60);
|
|
|
|
port = 0;
|
1998-12-16 07:20:45 +00:00
|
|
|
s = 0; /* not set or used if port == 0 */
|
1994-05-27 12:39:25 +00:00
|
|
|
for (;;) {
|
|
|
|
char c;
|
|
|
|
if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
|
|
|
|
if (cc < 0)
|
|
|
|
syslog(LOG_NOTICE, "read: %m");
|
|
|
|
shutdown(0, 1+1);
|
|
|
|
exit(1);
|
|
|
|
}
|
1998-12-16 07:20:45 +00:00
|
|
|
if (c == 0)
|
1994-05-27 12:39:25 +00:00
|
|
|
break;
|
|
|
|
port = port * 10 + c - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) alarm(0);
|
|
|
|
if (port != 0) {
|
|
|
|
int lport = IPPORT_RESERVED - 1;
|
2000-01-25 14:52:10 +00:00
|
|
|
s = rresvport_af(&lport, af);
|
1994-05-27 12:39:25 +00:00
|
|
|
if (s < 0) {
|
|
|
|
syslog(LOG_ERR, "can't get stderr port: %m");
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-09-19 22:05:32 +00:00
|
|
|
if (port >= IPPORT_RESERVED ||
|
|
|
|
port < IPPORT_RESERVED/2) {
|
|
|
|
syslog(LOG_NOTICE|LOG_AUTH,
|
|
|
|
"2nd socket from %s on unreserved port %u",
|
2000-01-25 14:52:10 +00:00
|
|
|
numericname,
|
1999-09-19 22:05:32 +00:00
|
|
|
port);
|
|
|
|
exit(1);
|
|
|
|
}
|
2000-01-25 14:52:10 +00:00
|
|
|
fromp->su_port = htons(port);
|
1994-05-27 12:39:25 +00:00
|
|
|
if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) {
|
|
|
|
syslog(LOG_INFO, "connect second port %d: %m", port);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errorstr = NULL;
|
2000-01-25 14:52:10 +00:00
|
|
|
realhostname_sa(fromhost, sizeof(fromhost) - 1,
|
|
|
|
(struct sockaddr *)fromp,
|
|
|
|
fromp->su_len);
|
1999-04-06 23:06:00 +00:00
|
|
|
fromhost[sizeof(fromhost) - 1] = '\0';
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
#ifdef CRYPT
|
2000-01-25 14:52:10 +00:00
|
|
|
if (doencrypt && af == AF_INET) {
|
1999-09-19 22:05:32 +00:00
|
|
|
struct sockaddr_in local_addr;
|
|
|
|
rc = sizeof(local_addr);
|
|
|
|
if (getsockname(0, (struct sockaddr *)&local_addr,
|
|
|
|
&rc) < 0) {
|
|
|
|
syslog(LOG_ERR, "getsockname: %m");
|
|
|
|
error("rlogind: getsockname: %m");
|
1994-05-27 12:39:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1999-09-19 22:05:32 +00:00
|
|
|
authopts = KOPT_DO_MUTUAL;
|
|
|
|
rc = krb_recvauth(authopts, 0, ticket,
|
|
|
|
"rcmd", instance, &fromaddr,
|
|
|
|
&local_addr, kdata, "", schedule,
|
|
|
|
version);
|
|
|
|
des_set_key(&kdata->session, schedule);
|
|
|
|
}
|
1994-05-27 12:39:25 +00:00
|
|
|
#endif
|
1999-09-19 22:05:32 +00:00
|
|
|
getstr(remuser, sizeof(remuser), "remuser");
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
getstr(locuser, sizeof(locuser), "locuser");
|
|
|
|
getstr(cmdbuf, sizeof(cmdbuf), "command");
|
|
|
|
setpwent();
|
|
|
|
pwd = getpwnam(locuser);
|
|
|
|
if (pwd == NULL) {
|
|
|
|
syslog(LOG_INFO|LOG_AUTH,
|
|
|
|
"%s@%s as %s: unknown login. cmd='%.80s'",
|
1999-04-06 23:06:00 +00:00
|
|
|
remuser, fromhost, locuser, cmdbuf);
|
1994-05-27 12:39:25 +00:00
|
|
|
if (errorstr == NULL)
|
|
|
|
errorstr = "Login incorrect.\n";
|
|
|
|
goto fail;
|
|
|
|
}
|
1997-04-23 03:06:47 +00:00
|
|
|
#ifdef LOGIN_CAP
|
1997-05-10 19:02:03 +00:00
|
|
|
lc = login_getpwclass(pwd);
|
1997-04-23 03:06:47 +00:00
|
|
|
#endif
|
1994-05-27 12:39:25 +00:00
|
|
|
if (chdir(pwd->pw_dir) < 0) {
|
1997-04-23 03:06:47 +00:00
|
|
|
#ifdef LOGIN_CAP
|
|
|
|
if (chdir("/") < 0 ||
|
|
|
|
login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
|
|
|
|
syslog(LOG_INFO|LOG_AUTH,
|
|
|
|
"%s@%s as %s: no home directory. cmd='%.80s'",
|
1999-04-06 23:06:00 +00:00
|
|
|
remuser, fromhost, locuser, cmdbuf);
|
1997-04-23 03:06:47 +00:00
|
|
|
error("No remote home directory.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
#else
|
1994-05-27 12:39:25 +00:00
|
|
|
(void) chdir("/");
|
|
|
|
#ifdef notdef
|
|
|
|
syslog(LOG_INFO|LOG_AUTH,
|
|
|
|
"%s@%s as %s: no home directory. cmd='%.80s'",
|
1999-04-06 23:06:00 +00:00
|
|
|
remuser, fromhost, locuser, cmdbuf);
|
1994-05-27 12:39:25 +00:00
|
|
|
error("No remote directory.\n");
|
|
|
|
exit(1);
|
|
|
|
#endif
|
1997-04-23 03:06:47 +00:00
|
|
|
#endif
|
|
|
|
pwd->pw_dir = "/";
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
|
|
|
|
1999-09-19 22:05:32 +00:00
|
|
|
#ifndef NO_PAM
|
|
|
|
retval = auth_pam(locuser);
|
|
|
|
|
|
|
|
if (retval) {
|
|
|
|
if (retval == -1) {
|
|
|
|
syslog(LOG_ERR,"PAM authentication failed");
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
1999-09-19 22:05:32 +00:00
|
|
|
else {
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"User %s failed PAM authentication", locuser);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
if (errorstr ||
|
1995-08-28 21:30:59 +00:00
|
|
|
(pwd->pw_expire && time(NULL) >= pwd->pw_expire) ||
|
|
|
|
(pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
|
2000-01-25 14:52:10 +00:00
|
|
|
iruserok_af(
|
|
|
|
#ifdef INET6
|
|
|
|
(af == AF_INET6)
|
|
|
|
? (void *)&fromp->su_sin6.sin6_addr :
|
|
|
|
#endif
|
|
|
|
(void *)&fromp->su_sin.sin_addr,
|
|
|
|
pwd->pw_uid == 0,
|
|
|
|
remuser, locuser, af) < 0)) {
|
1994-05-27 12:39:25 +00:00
|
|
|
if (__rcmd_errstr)
|
|
|
|
syslog(LOG_INFO|LOG_AUTH,
|
|
|
|
"%s@%s as %s: permission denied (%s). cmd='%.80s'",
|
1999-04-06 23:06:00 +00:00
|
|
|
remuser, fromhost, locuser, __rcmd_errstr,
|
1994-05-27 12:39:25 +00:00
|
|
|
cmdbuf);
|
|
|
|
else
|
|
|
|
syslog(LOG_INFO|LOG_AUTH,
|
|
|
|
"%s@%s as %s: permission denied. cmd='%.80s'",
|
1999-04-06 23:06:00 +00:00
|
|
|
remuser, fromhost, locuser, cmdbuf);
|
1994-05-27 12:39:25 +00:00
|
|
|
fail:
|
|
|
|
if (errorstr == NULL)
|
1997-07-18 21:04:19 +00:00
|
|
|
errorstr = "Login incorrect.\n";
|
1999-04-06 23:06:00 +00:00
|
|
|
error(errorstr, fromhost);
|
1994-05-27 12:39:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
|
|
|
|
error("Logins currently disabled.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
1997-04-23 03:06:47 +00:00
|
|
|
#ifdef LOGIN_CAP
|
2000-01-25 14:52:10 +00:00
|
|
|
if (lc != NULL && fromp->su_family == AF_INET) { /*XXX*/
|
1997-04-23 03:06:47 +00:00
|
|
|
char remote_ip[MAXHOSTNAMELEN];
|
|
|
|
|
2000-01-25 14:52:10 +00:00
|
|
|
strncpy(remote_ip, numericname,
|
1997-04-23 03:06:47 +00:00
|
|
|
sizeof(remote_ip) - 1);
|
|
|
|
remote_ip[sizeof(remote_ip) - 1] = 0;
|
|
|
|
if (!auth_hostok(lc, fromhost, remote_ip)) {
|
|
|
|
syslog(LOG_INFO|LOG_AUTH,
|
|
|
|
"%s@%s as %s: permission denied (%s). cmd='%.80s'",
|
1999-04-06 23:06:00 +00:00
|
|
|
remuser, fromhost, locuser, __rcmd_errstr,
|
1997-04-23 03:06:47 +00:00
|
|
|
cmdbuf);
|
1997-07-18 21:04:19 +00:00
|
|
|
error("Login incorrect.\n");
|
1997-04-23 03:06:47 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!auth_timeok(lc, time(NULL))) {
|
|
|
|
error("Logins not available right now\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* !LOGIN_CAP */
|
1995-11-12 18:31:23 +00:00
|
|
|
#if BSD > 43
|
|
|
|
/* before fork, while we're session leader */
|
|
|
|
if (setlogin(pwd->pw_name) < 0)
|
|
|
|
syslog(LOG_ERR, "setlogin() failed: %m");
|
|
|
|
#endif
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
(void) write(STDERR_FILENO, "\0", 1);
|
|
|
|
sent_null = 1;
|
|
|
|
|
|
|
|
if (port) {
|
|
|
|
if (pipe(pv) < 0) {
|
|
|
|
error("Can't make pipe.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#ifdef CRYPT
|
|
|
|
if (doencrypt) {
|
|
|
|
if (pipe(pv1) < 0) {
|
|
|
|
error("Can't make 2nd pipe.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (pipe(pv2) < 0) {
|
|
|
|
error("Can't make 3rd pipe.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
pid = fork();
|
|
|
|
if (pid == -1) {
|
|
|
|
error("Can't fork; try again.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (pid) {
|
|
|
|
#ifdef CRYPT
|
|
|
|
if (doencrypt) {
|
|
|
|
static char msg[] = SECURE_MESSAGE;
|
|
|
|
(void) close(pv1[1]);
|
|
|
|
(void) close(pv2[1]);
|
1997-09-28 08:38:04 +00:00
|
|
|
des_enc_write(s, msg, sizeof(msg) - 1,
|
|
|
|
schedule, &kdata->session);
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
(void) close(0);
|
|
|
|
(void) close(1);
|
|
|
|
}
|
|
|
|
(void) close(2);
|
|
|
|
(void) close(pv[1]);
|
|
|
|
|
|
|
|
FD_ZERO(&readfrom);
|
|
|
|
FD_SET(s, &readfrom);
|
|
|
|
FD_SET(pv[0], &readfrom);
|
|
|
|
if (pv[0] > s)
|
|
|
|
nfd = pv[0];
|
|
|
|
else
|
|
|
|
nfd = s;
|
|
|
|
#ifdef CRYPT
|
|
|
|
if (doencrypt) {
|
|
|
|
FD_ZERO(&writeto);
|
|
|
|
FD_SET(pv2[0], &writeto);
|
|
|
|
FD_SET(pv1[0], &readfrom);
|
|
|
|
|
|
|
|
nfd = MAX(nfd, pv2[0]);
|
|
|
|
nfd = MAX(nfd, pv1[0]);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
ioctl(pv[0], FIONBIO, (char *)&one);
|
|
|
|
|
|
|
|
/* should set s nbio! */
|
|
|
|
nfd++;
|
|
|
|
do {
|
|
|
|
ready = readfrom;
|
|
|
|
#ifdef CRYPT
|
|
|
|
if (doencrypt) {
|
|
|
|
wready = writeto;
|
|
|
|
if (select(nfd, &ready,
|
|
|
|
&wready, (fd_set *) 0,
|
|
|
|
(struct timeval *) 0) < 0)
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (select(nfd, &ready, (fd_set *)0,
|
|
|
|
(fd_set *)0, (struct timeval *)0) < 0)
|
|
|
|
break;
|
|
|
|
if (FD_ISSET(s, &ready)) {
|
|
|
|
int ret;
|
|
|
|
#ifdef CRYPT
|
|
|
|
if (doencrypt)
|
1997-09-28 08:38:04 +00:00
|
|
|
ret = des_enc_read(s, &sig, 1,
|
|
|
|
schedule, &kdata->session);
|
1994-05-27 12:39:25 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
ret = read(s, &sig, 1);
|
|
|
|
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, 1+1);
|
|
|
|
FD_CLR(pv[0], &readfrom);
|
|
|
|
} else {
|
|
|
|
#ifdef CRYPT
|
|
|
|
if (doencrypt)
|
|
|
|
(void)
|
1997-09-28 08:38:04 +00:00
|
|
|
des_enc_write(s, buf, cc,
|
|
|
|
schedule, &kdata->session);
|
1994-05-27 12:39:25 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
(void)
|
|
|
|
write(s, buf, cc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef CRYPT
|
|
|
|
if (doencrypt && FD_ISSET(pv1[0], &ready)) {
|
|
|
|
errno = 0;
|
|
|
|
cc = read(pv1[0], buf, sizeof(buf));
|
|
|
|
if (cc <= 0) {
|
|
|
|
shutdown(pv1[0], 1+1);
|
|
|
|
FD_CLR(pv1[0], &readfrom);
|
|
|
|
} else
|
1997-09-28 08:38:04 +00:00
|
|
|
(void) des_enc_write(STDOUT_FILENO,
|
|
|
|
buf, cc,
|
|
|
|
schedule, &kdata->session);
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (doencrypt && FD_ISSET(pv2[0], &wready)) {
|
|
|
|
errno = 0;
|
1997-09-28 08:38:04 +00:00
|
|
|
cc = des_enc_read(STDIN_FILENO,
|
|
|
|
buf, sizeof(buf),
|
|
|
|
schedule, &kdata->session);
|
1994-05-27 12:39:25 +00:00
|
|
|
if (cc <= 0) {
|
|
|
|
shutdown(pv2[0], 1+1);
|
|
|
|
FD_CLR(pv2[0], &writeto);
|
|
|
|
} else
|
|
|
|
(void) write(pv2[0], buf, cc);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} while (FD_ISSET(s, &readfrom) ||
|
|
|
|
#ifdef CRYPT
|
|
|
|
(doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
|
|
|
|
#endif
|
|
|
|
FD_ISSET(pv[0], &readfrom));
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
setpgrp(0, getpid());
|
|
|
|
(void) close(s);
|
|
|
|
(void) close(pv[0]);
|
|
|
|
#ifdef CRYPT
|
|
|
|
if (doencrypt) {
|
|
|
|
close(pv1[0]); close(pv2[0]);
|
|
|
|
dup2(pv1[1], 1);
|
|
|
|
dup2(pv2[1], 0);
|
|
|
|
close(pv1[1]);
|
|
|
|
close(pv2[1]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
dup2(pv[1], 2);
|
|
|
|
close(pv[1]);
|
|
|
|
}
|
|
|
|
if (*pwd->pw_shell == '\0')
|
|
|
|
pwd->pw_shell = _PATH_BSHELL;
|
|
|
|
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);
|
|
|
|
cp = strrchr(pwd->pw_shell, '/');
|
|
|
|
if (cp)
|
|
|
|
cp++;
|
|
|
|
else
|
|
|
|
cp = pwd->pw_shell;
|
1997-04-23 03:06:47 +00:00
|
|
|
#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
|
1994-05-27 12:39:25 +00:00
|
|
|
endpwent();
|
|
|
|
if (log_success || pwd->pw_uid == 0) {
|
|
|
|
syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
|
1999-04-06 23:06:00 +00:00
|
|
|
remuser, fromhost, locuser, cmdbuf);
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
|
|
|
execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
|
|
|
|
perror(pwd->pw_shell);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#if __STDC__
|
|
|
|
#include <stdarg.h>
|
|
|
|
#else
|
|
|
|
#include <varargs.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
#if __STDC__
|
|
|
|
error(const char *fmt, ...)
|
|
|
|
#else
|
|
|
|
error(fmt, va_alist)
|
|
|
|
char *fmt;
|
|
|
|
va_dcl
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
*bp++ = 1;
|
|
|
|
len = 1;
|
|
|
|
} else
|
|
|
|
len = 0;
|
|
|
|
(void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap);
|
|
|
|
(void)write(STDERR_FILENO, buf, len + strlen(bp));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
getstr(buf, cnt, err)
|
|
|
|
char *buf, *err;
|
|
|
|
int cnt;
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (read(STDIN_FILENO, &c, 1) != 1)
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
local_domain(h)
|
|
|
|
char *h;
|
|
|
|
{
|
|
|
|
char localhost[MAXHOSTNAMELEN];
|
|
|
|
char *p1, *p2;
|
|
|
|
|
|
|
|
localhost[0] = 0;
|
1999-04-07 08:27:45 +00:00
|
|
|
(void) gethostname(localhost, sizeof(localhost) - 1);
|
|
|
|
localhost[sizeof(localhost) - 1] = '\0';
|
1994-05-27 12:39:25 +00:00
|
|
|
p1 = topdomain(localhost);
|
|
|
|
p2 = topdomain(h);
|
|
|
|
if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
|
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
topdomain(h)
|
|
|
|
char *h;
|
|
|
|
{
|
|
|
|
char *p, *maybe = NULL;
|
|
|
|
int dots = 0;
|
|
|
|
|
|
|
|
for (p = h + strlen(h); p >= h; p--) {
|
|
|
|
if (*p == '.') {
|
|
|
|
if (++dots == 2)
|
|
|
|
return (p);
|
|
|
|
maybe = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (maybe);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
|
|
|
|
syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
|
|
|
|
exit(2);
|
|
|
|
}
|