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.
|
2002-05-08 00:43:46 +00:00
|
|
|
* Copyright (c) 2002 Networks Associates Technology, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Portions of this software were developed for the FreeBSD Project by
|
|
|
|
* ThinkSec AS and NAI Labs, the Security Research Division of Network
|
|
|
|
* Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
|
|
|
|
* ("CBOSS"), as part of the DARPA CHATS research program.
|
1994-05-27 12:39:25 +00: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.
|
2014-02-17 22:27:32 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-27 12:39:25 +00:00
|
|
|
* 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
|
1994-05-27 12:39:25 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
2002-06-26 17:09:08 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
/*
|
|
|
|
* remote shell server:
|
|
|
|
* [port]\0
|
2002-05-08 00:43:46 +00:00
|
|
|
* ruser\0
|
|
|
|
* luser\0
|
1994-05-27 12:39:25 +00:00
|
|
|
* 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>
|
|
|
|
|
2001-04-28 07:43:01 +00:00
|
|
|
#include <err.h>
|
1994-05-27 12:39:25 +00:00
|
|
|
#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>
|
2001-04-29 17:50:29 +00:00
|
|
|
#include <stdarg.h>
|
1994-05-27 12:39:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
1997-04-23 03:06:47 +00:00
|
|
|
#include <login_cap.h>
|
2001-03-27 19:40:51 +00:00
|
|
|
|
|
|
|
#include <security/pam_appl.h>
|
2002-05-08 00:43:46 +00:00
|
|
|
#include <security/openpam.h>
|
2001-03-27 19:40:51 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2002-05-08 00:43:46 +00:00
|
|
|
static struct pam_conv pamc = { openpam_nullconv, NULL };
|
2001-03-27 19:40:51 +00:00
|
|
|
static pam_handle_t *pamh;
|
2002-05-08 00:43:46 +00:00
|
|
|
static int pam_err;
|
2001-03-27 19:40:51 +00:00
|
|
|
|
|
|
|
#define PAM_END { \
|
2002-05-08 00:43:46 +00:00
|
|
|
if ((pam_err = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) \
|
|
|
|
syslog(LOG_ERR|LOG_AUTH, "pam_setcred(): %s", pam_strerror(pamh, pam_err)); \
|
|
|
|
if ((pam_err = pam_close_session(pamh,0)) != PAM_SUCCESS) \
|
|
|
|
syslog(LOG_ERR|LOG_AUTH, "pam_close_session(): %s", pam_strerror(pamh, pam_err)); \
|
|
|
|
if ((pam_err = pam_end(pamh, pam_err)) != PAM_SUCCESS) \
|
|
|
|
syslog(LOG_ERR|LOG_AUTH, "pam_end(): %s", pam_strerror(pamh, pam_err)); \
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
2000-01-25 14:52:10 +00:00
|
|
|
|
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;
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2002-05-08 00:43:46 +00:00
|
|
|
void doit(struct sockaddr *);
|
2002-02-07 04:58:29 +00:00
|
|
|
static void rshd_errx(int, const char *, ...) __printf0like(2, 3);
|
2002-02-07 05:07:04 +00:00
|
|
|
void getstr(char *, int, const char *);
|
2002-02-07 04:58:29 +00:00
|
|
|
int local_domain(char *);
|
|
|
|
char *topdomain(char *);
|
|
|
|
void usage(void);
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2002-06-26 17:09:08 +00:00
|
|
|
char slash[] = "/";
|
|
|
|
char bshell[] = _PATH_BSHELL;
|
|
|
|
|
2005-02-09 20:36:12 +00:00
|
|
|
#define OPTIONS "aDLln"
|
1999-09-19 22:05:32 +00:00
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
int
|
2002-02-07 04:58:29 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
|
|
|
extern int __check_rhosts_file;
|
|
|
|
struct linger linger;
|
2005-02-14 17:42:58 +00:00
|
|
|
socklen_t fromlen;
|
|
|
|
int ch, on = 1;
|
2000-01-25 14:52:10 +00:00
|
|
|
struct sockaddr_storage from;
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2014-08-17 19:06:26 +00:00
|
|
|
openlog("rshd", LOG_PID, LOG_DAEMON);
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
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':
|
2001-02-06 12:05:58 +00:00
|
|
|
/* ignored for compatibility */
|
1994-05-27 12:39:25 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
__check_rhosts_file = 0;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
keepalive = 0;
|
|
|
|
break;
|
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;
|
|
|
|
|
|
|
|
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");
|
2002-05-08 00:43:46 +00:00
|
|
|
doit((struct sockaddr *)&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
|
|
|
}
|
|
|
|
|
2002-05-08 00:43:46 +00:00
|
|
|
extern char **environ;
|
1994-05-27 12:39:25 +00:00
|
|
|
|
|
|
|
void
|
2002-05-08 00:43:46 +00:00
|
|
|
doit(struct sockaddr *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;
|
2002-05-08 00:43:46 +00:00
|
|
|
int cc, fd, nfd, pv[2], pid, s;
|
1994-05-27 12:39:25 +00:00
|
|
|
int one = 1;
|
2002-05-08 00:43:46 +00:00
|
|
|
const char *cp, *errorstr;
|
|
|
|
char sig, buf[BUFSIZ];
|
2005-03-21 08:01:14 +00:00
|
|
|
char *cmdbuf, luser[16], ruser[16];
|
2002-05-08 00:43:46 +00:00
|
|
|
char rhost[2 * MAXHOSTNAMELEN + 1];
|
2000-01-25 14:52:10 +00:00
|
|
|
char numericname[INET6_ADDRSTRLEN];
|
2002-06-26 17:09:08 +00:00
|
|
|
int af, srcport;
|
2005-03-21 08:01:14 +00:00
|
|
|
int maxcmdlen;
|
1997-04-23 03:06:47 +00:00
|
|
|
login_cap_t *lc;
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2005-03-21 08:01:14 +00:00
|
|
|
maxcmdlen = (int)sysconf(_SC_ARG_MAX);
|
|
|
|
if (maxcmdlen <= 0 || (cmdbuf = malloc(maxcmdlen)) == NULL)
|
|
|
|
exit(1);
|
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
(void) signal(SIGINT, SIG_DFL);
|
|
|
|
(void) signal(SIGQUIT, SIG_DFL);
|
|
|
|
(void) signal(SIGTERM, SIG_DFL);
|
2002-05-08 00:43:46 +00:00
|
|
|
af = fromp->sa_family;
|
|
|
|
srcport = ntohs(*((in_port_t *)&fromp->sa_data));
|
|
|
|
if (af == AF_INET) {
|
|
|
|
inet_ntop(af, &((struct sockaddr_in *)fromp)->sin_addr,
|
|
|
|
numericname, sizeof numericname);
|
|
|
|
} else if (af == AF_INET6) {
|
|
|
|
inet_ntop(af, &((struct sockaddr_in6 *)fromp)->sin6_addr,
|
|
|
|
numericname, sizeof numericname);
|
|
|
|
} else {
|
2001-03-27 19:40:51 +00:00
|
|
|
syslog(LOG_ERR, "malformed \"from\" address (af %d)", af);
|
1994-05-27 12:39:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#ifdef IP_OPTIONS
|
2002-05-08 00:43:46 +00:00
|
|
|
if (af == AF_INET) {
|
|
|
|
u_char optbuf[BUFSIZ/3];
|
2005-02-14 17:42:58 +00:00
|
|
|
socklen_t optsize = sizeof(optbuf), ipproto, i;
|
2002-05-08 00:43:46 +00:00
|
|
|
struct protoent *ip;
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2002-05-08 00:43:46 +00:00
|
|
|
if ((ip = getprotobyname("ip")) != NULL)
|
|
|
|
ipproto = ip->p_proto;
|
|
|
|
else
|
|
|
|
ipproto = IPPROTO_IP;
|
|
|
|
if (!getsockopt(0, ipproto, IP_OPTIONS, optbuf, &optsize) &&
|
|
|
|
optsize != 0) {
|
|
|
|
for (i = 0; i < optsize; ) {
|
|
|
|
u_char c = optbuf[i];
|
|
|
|
if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
|
|
|
|
syslog(LOG_NOTICE,
|
|
|
|
"connection refused from %s with IP option %s",
|
|
|
|
numericname,
|
|
|
|
c == IPOPT_LSRR ? "LSRR" : "SSRR");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (c == IPOPT_EOL)
|
|
|
|
break;
|
|
|
|
i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
|
1997-02-09 04:16:27 +00:00
|
|
|
}
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-05-08 00:43:46 +00:00
|
|
|
if (srcport >= IPPORT_RESERVED ||
|
|
|
|
srcport < 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,
|
2002-05-08 00:43:46 +00:00
|
|
|
srcport);
|
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");
|
2005-05-11 02:41:39 +00:00
|
|
|
shutdown(0, SHUT_RDWR);
|
1994-05-27 12:39:25 +00:00
|
|
|
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);
|
|
|
|
}
|
2002-05-08 00:43:46 +00:00
|
|
|
*((in_port_t *)&fromp->sa_data) = htons(port);
|
|
|
|
if (connect(s, fromp, fromp->sa_len) < 0) {
|
1994-05-27 12:39:25 +00:00
|
|
|
syslog(LOG_INFO, "connect second port %d: %m", port);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errorstr = NULL;
|
2002-05-08 00:43:46 +00:00
|
|
|
realhostname_sa(rhost, sizeof(rhost) - 1, fromp, fromp->sa_len);
|
|
|
|
rhost[sizeof(rhost) - 1] = '\0';
|
|
|
|
/* XXX truncation! */
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2002-05-08 00:43:46 +00:00
|
|
|
(void) alarm(60);
|
|
|
|
getstr(ruser, sizeof(ruser), "ruser");
|
|
|
|
getstr(luser, sizeof(luser), "luser");
|
2005-03-21 08:01:14 +00:00
|
|
|
getstr(cmdbuf, maxcmdlen, "command");
|
2002-05-08 00:43:46 +00:00
|
|
|
(void) alarm(0);
|
2001-03-27 19:40:51 +00:00
|
|
|
|
2002-05-08 00:43:46 +00:00
|
|
|
pam_err = pam_start("rsh", luser, &pamc, &pamh);
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
syslog(LOG_ERR|LOG_AUTH, "pam_start(): %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
2001-04-29 17:50:29 +00:00
|
|
|
rshd_errx(1, "Login incorrect.");
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
|
|
|
|
2002-05-08 00:43:46 +00:00
|
|
|
if ((pam_err = pam_set_item(pamh, PAM_RUSER, ruser)) != PAM_SUCCESS ||
|
2011-10-30 12:33:19 +00:00
|
|
|
(pam_err = pam_set_item(pamh, PAM_RHOST, rhost)) != PAM_SUCCESS) {
|
2002-05-08 00:43:46 +00:00
|
|
|
syslog(LOG_ERR|LOG_AUTH, "pam_set_item(): %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
2001-04-29 17:50:29 +00:00
|
|
|
rshd_errx(1, "Login incorrect.");
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
|
|
|
|
2002-05-08 00:43:46 +00:00
|
|
|
pam_err = pam_authenticate(pamh, 0);
|
|
|
|
if (pam_err == PAM_SUCCESS) {
|
|
|
|
if ((pam_err = pam_get_user(pamh, &cp, NULL)) == PAM_SUCCESS) {
|
|
|
|
strncpy(luser, cp, sizeof(luser));
|
|
|
|
luser[sizeof(luser) - 1] = '\0';
|
|
|
|
/* XXX truncation! */
|
|
|
|
}
|
|
|
|
pam_err = pam_acct_mgmt(pamh, 0);
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
2002-05-08 00:43:46 +00:00
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
syslog(LOG_INFO|LOG_AUTH,
|
|
|
|
"%s@%s as %s: permission denied (%s). cmd='%.80s'",
|
|
|
|
ruser, rhost, luser, pam_strerror(pamh, pam_err), cmdbuf);
|
2001-04-29 17:50:29 +00:00
|
|
|
rshd_errx(1, "Login incorrect.");
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
setpwent();
|
2002-05-08 00:43:46 +00:00
|
|
|
pwd = getpwnam(luser);
|
1994-05-27 12:39:25 +00:00
|
|
|
if (pwd == NULL) {
|
|
|
|
syslog(LOG_INFO|LOG_AUTH,
|
|
|
|
"%s@%s as %s: unknown login. cmd='%.80s'",
|
2002-05-08 00:43:46 +00:00
|
|
|
ruser, rhost, luser, cmdbuf);
|
2001-03-27 19:40:51 +00:00
|
|
|
if (errorstr == NULL)
|
2001-04-28 07:43:01 +00:00
|
|
|
errorstr = "Login incorrect.";
|
2002-05-08 00:43:46 +00:00
|
|
|
rshd_errx(1, errorstr, rhost);
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
|
|
|
|
1997-05-10 19:02:03 +00:00
|
|
|
lc = login_getpwclass(pwd);
|
2001-03-27 19:40:51 +00:00
|
|
|
if (pwd->pw_uid)
|
|
|
|
auth_checknologin(lc);
|
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
if (chdir(pwd->pw_dir) < 0) {
|
1997-04-23 03:06:47 +00:00
|
|
|
if (chdir("/") < 0 ||
|
|
|
|
login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
|
|
|
|
syslog(LOG_INFO|LOG_AUTH,
|
|
|
|
"%s@%s as %s: no home directory. cmd='%.80s'",
|
2002-05-08 00:43:46 +00:00
|
|
|
ruser, rhost, luser, cmdbuf);
|
2001-04-29 17:50:29 +00:00
|
|
|
rshd_errx(0, "No remote home directory.");
|
1997-04-23 03:06:47 +00:00
|
|
|
}
|
2002-06-26 17:09:08 +00:00
|
|
|
pwd->pw_dir = slash;
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
|
|
|
|
2002-05-08 00:43:46 +00:00
|
|
|
if (lc != NULL && fromp->sa_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;
|
2002-05-08 00:43:46 +00:00
|
|
|
/* XXX truncation! */
|
|
|
|
if (!auth_hostok(lc, rhost, remote_ip)) {
|
1997-04-23 03:06:47 +00:00
|
|
|
syslog(LOG_INFO|LOG_AUTH,
|
|
|
|
"%s@%s as %s: permission denied (%s). cmd='%.80s'",
|
2002-05-08 00:43:46 +00:00
|
|
|
ruser, rhost, luser, __rcmd_errstr,
|
1997-04-23 03:06:47 +00:00
|
|
|
cmdbuf);
|
2001-04-29 17:50:29 +00:00
|
|
|
rshd_errx(1, "Login incorrect.");
|
1997-04-23 03:06:47 +00:00
|
|
|
}
|
2001-04-28 07:43:01 +00:00
|
|
|
if (!auth_timeok(lc, time(NULL)))
|
2001-04-29 17:50:29 +00:00
|
|
|
rshd_errx(1, "Logins not available right now");
|
1997-04-23 03:06:47 +00:00
|
|
|
}
|
1994-05-27 12:39:25 +00:00
|
|
|
|
2001-03-27 19:40:51 +00:00
|
|
|
/*
|
|
|
|
* PAM modules might add supplementary groups in
|
|
|
|
* pam_setcred(), so initialize them first.
|
|
|
|
* But we need to open the session as root.
|
|
|
|
*/
|
|
|
|
if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
|
2002-05-08 00:43:46 +00:00
|
|
|
syslog(LOG_ERR, "setusercontext: %m");
|
2001-03-27 19:40:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-05-08 00:43:46 +00:00
|
|
|
if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
|
|
|
|
syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, pam_err));
|
|
|
|
} else if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
|
|
|
|
syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, pam_err));
|
2001-03-27 19:40:51 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
(void) write(STDERR_FILENO, "\0", 1);
|
|
|
|
sent_null = 1;
|
|
|
|
|
|
|
|
if (port) {
|
2001-04-28 07:43:01 +00:00
|
|
|
if (pipe(pv) < 0)
|
2001-04-29 17:50:29 +00:00
|
|
|
rshd_errx(1, "Can't make pipe.");
|
1994-05-27 12:39:25 +00:00
|
|
|
pid = fork();
|
2001-04-28 07:43:01 +00:00
|
|
|
if (pid == -1)
|
2001-04-29 17:50:29 +00:00
|
|
|
rshd_errx(1, "Can't fork; try again.");
|
1994-05-27 12:39:25 +00:00
|
|
|
if (pid) {
|
2002-06-26 17:09:08 +00:00
|
|
|
(void) close(0);
|
|
|
|
(void) close(1);
|
1994-05-27 12:39:25 +00:00
|
|
|
(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;
|
|
|
|
ioctl(pv[0], FIONBIO, (char *)&one);
|
|
|
|
|
|
|
|
/* should set s nbio! */
|
|
|
|
nfd++;
|
|
|
|
do {
|
|
|
|
ready = readfrom;
|
2002-06-26 17:09:08 +00:00
|
|
|
if (select(nfd, &ready, (fd_set *)0,
|
|
|
|
(fd_set *)0, (struct timeval *)0) < 0)
|
|
|
|
break;
|
1994-05-27 12:39:25 +00:00
|
|
|
if (FD_ISSET(s, &ready)) {
|
|
|
|
int ret;
|
|
|
|
ret = read(s, &sig, 1);
|
2002-06-26 17:09:08 +00:00
|
|
|
if (ret <= 0)
|
|
|
|
FD_CLR(s, &readfrom);
|
|
|
|
else
|
|
|
|
killpg(pid, sig);
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
|
|
|
if (FD_ISSET(pv[0], &ready)) {
|
|
|
|
errno = 0;
|
|
|
|
cc = read(pv[0], buf, sizeof(buf));
|
|
|
|
if (cc <= 0) {
|
2005-05-11 02:41:39 +00:00
|
|
|
shutdown(s, SHUT_RDWR);
|
1994-05-27 12:39:25 +00:00
|
|
|
FD_CLR(pv[0], &readfrom);
|
|
|
|
} else {
|
2002-06-26 17:09:08 +00:00
|
|
|
(void)write(s, buf, cc);
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (FD_ISSET(s, &readfrom) ||
|
|
|
|
FD_ISSET(pv[0], &readfrom));
|
2001-03-27 19:40:51 +00:00
|
|
|
PAM_END;
|
1994-05-27 12:39:25 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
(void) close(s);
|
|
|
|
(void) close(pv[0]);
|
|
|
|
dup2(pv[1], 2);
|
|
|
|
close(pv[1]);
|
|
|
|
}
|
2001-03-27 19:40:51 +00:00
|
|
|
else {
|
|
|
|
pid = fork();
|
2001-04-28 07:43:01 +00:00
|
|
|
if (pid == -1)
|
2001-04-29 17:50:29 +00:00
|
|
|
rshd_errx(1, "Can't fork; try again.");
|
2001-03-27 19:40:51 +00:00
|
|
|
if (pid) {
|
|
|
|
/* Parent. */
|
2002-05-08 00:43:46 +00:00
|
|
|
while (wait(NULL) > 0 || errno == EINTR)
|
|
|
|
/* nothing */ ;
|
2001-03-27 19:40:51 +00:00
|
|
|
PAM_END;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
2002-05-08 00:43:46 +00:00
|
|
|
|
|
|
|
for (fd = getdtablesize(); fd > 2; fd--)
|
|
|
|
(void) close(fd);
|
|
|
|
if (setsid() == -1)
|
|
|
|
syslog(LOG_ERR, "setsid() failed: %m");
|
|
|
|
if (setlogin(pwd->pw_name) < 0)
|
|
|
|
syslog(LOG_ERR, "setlogin() failed: %m");
|
2001-03-27 19:40:51 +00:00
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
if (*pwd->pw_shell == '\0')
|
2002-06-26 17:09:08 +00:00
|
|
|
pwd->pw_shell = bshell;
|
2002-05-08 00:43:46 +00:00
|
|
|
(void) pam_setenv(pamh, "HOME", pwd->pw_dir, 1);
|
|
|
|
(void) pam_setenv(pamh, "SHELL", pwd->pw_shell, 1);
|
|
|
|
(void) pam_setenv(pamh, "USER", pwd->pw_name, 1);
|
|
|
|
(void) pam_setenv(pamh, "PATH", _PATH_DEFPATH, 1);
|
|
|
|
environ = pam_getenvlist(pamh);
|
|
|
|
(void) pam_end(pamh, pam_err);
|
1994-05-27 12:39:25 +00:00
|
|
|
cp = strrchr(pwd->pw_shell, '/');
|
|
|
|
if (cp)
|
|
|
|
cp++;
|
|
|
|
else
|
|
|
|
cp = pwd->pw_shell;
|
2001-03-27 19:40:51 +00:00
|
|
|
|
2002-05-08 00:43:46 +00:00
|
|
|
if (setusercontext(lc, pwd, pwd->pw_uid,
|
|
|
|
LOGIN_SETALL & ~LOGIN_SETGROUP) < 0) {
|
|
|
|
syslog(LOG_ERR, "setusercontext(): %m");
|
1997-04-23 03:06:47 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
login_close(lc);
|
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'",
|
2002-05-08 00:43:46 +00:00
|
|
|
ruser, rhost, luser, cmdbuf);
|
1994-05-27 12:39:25 +00:00
|
|
|
}
|
2004-04-04 20:53:23 +00:00
|
|
|
execl(pwd->pw_shell, cp, "-c", cmdbuf, (char *)NULL);
|
2002-05-08 00:43:46 +00:00
|
|
|
err(1, "%s", pwd->pw_shell);
|
1994-05-27 12:39:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2001-04-29 17:50:29 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
rshd_errx(int errcode, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
if (sent_null == 0)
|
|
|
|
write(STDERR_FILENO, "\1", 1);
|
|
|
|
|
|
|
|
verrx(errcode, fmt, ap);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:39:25 +00:00
|
|
|
void
|
2002-02-07 05:07:04 +00:00
|
|
|
getstr(char *buf, int cnt, const char *error)
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (read(STDIN_FILENO, &c, 1) != 1)
|
|
|
|
exit(1);
|
|
|
|
*buf++ = c;
|
2001-04-29 09:03:52 +00:00
|
|
|
if (--cnt == 0)
|
2002-02-07 05:07:04 +00:00
|
|
|
rshd_errx(1, "%s too long", error);
|
1994-05-27 12:39:25 +00:00
|
|
|
} 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
|
2002-02-07 04:58:29 +00:00
|
|
|
local_domain(char *h)
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
|
|
|
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';
|
2002-05-08 00:43:46 +00:00
|
|
|
/* XXX truncation! */
|
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 *
|
2002-02-07 04:58:29 +00:00
|
|
|
topdomain(char *h)
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
|
|
|
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
|
2002-02-07 04:58:29 +00:00
|
|
|
usage(void)
|
1994-05-27 12:39:25 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
|
|
|
|
exit(2);
|
|
|
|
}
|