2008-07-20 05:09:06 +04:00
|
|
|
/* $NetBSD: rshd.c,v 1.46 2008/07/20 01:09:07 lukem 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.
|
2003-08-07 13:46:37 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03: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.
|
|
|
|
*/
|
|
|
|
|
1997-10-07 14:37:30 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
2008-07-20 05:09:06 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\
|
|
|
|
The Regents of the University of California. All rights reserved.");
|
1997-10-07 14:37:30 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94";
|
|
|
|
#else
|
2008-07-20 05:09:06 +04:00
|
|
|
__RCSID("$NetBSD: rshd.c,v 1.46 2008/07/20 01:09:07 lukem 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
|
|
|
|
2005-03-08 07:35:19 +03:00
|
|
|
#include <netinet/in_systm.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <netinet/in.h>
|
2005-03-08 07:35:19 +03:00
|
|
|
#include <netinet/ip.h>
|
2003-06-15 02:43:31 +04:00
|
|
|
#include <netinet/tcp.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#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>
|
2002-09-23 07:32:34 +04:00
|
|
|
#include <poll.h>
|
2000-01-22 13:22:55 +03:00
|
|
|
#ifdef LOGIN_CAP
|
|
|
|
#include <login_cap.h>
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-02-20 08:45:55 +03:00
|
|
|
#ifdef USE_PAM
|
|
|
|
#include <security/pam_appl.h>
|
|
|
|
#include <security/openpam.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
static struct pam_conv pamc = { openpam_nullconv, NULL };
|
|
|
|
static pam_handle_t *pamh;
|
|
|
|
static int pam_err;
|
|
|
|
|
2005-03-12 21:23:30 +03:00
|
|
|
#define PAM_END do { \
|
2005-02-20 08:45:55 +03: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)); \
|
2005-03-12 21:23:30 +03:00
|
|
|
} while (/*CONSTCOND*/0)
|
2005-02-20 08:45:55 +03:00
|
|
|
#else
|
|
|
|
#define PAM_END
|
|
|
|
#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;
|
|
|
|
|
2007-12-15 22:44:37 +03:00
|
|
|
void doit(struct sockaddr *) __dead;
|
2005-02-20 08:45:55 +03:00
|
|
|
void rshd_errx(int, const char *, ...)
|
|
|
|
__attribute__((__noreturn__, __format__(__printf__, 2, 3)));
|
2005-03-28 01:00:58 +04:00
|
|
|
void getstr(char *, int, const char *);
|
2005-02-20 08:45:55 +03:00
|
|
|
int local_domain(char *);
|
|
|
|
char *topdomain(char *);
|
|
|
|
void usage(void);
|
|
|
|
int main(int, char *[]);
|
1994-06-05 19:33:20 +04:00
|
|
|
|
2005-03-09 19:43:37 +03:00
|
|
|
#define OPTIONS "aLln"
|
2001-02-05 01:14:13 +03:00
|
|
|
extern int __check_rhosts_file;
|
|
|
|
extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
|
2005-02-20 08:45:55 +03:00
|
|
|
static const char incorrect[] = "Login incorrect.";
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-05 19:33:20 +04:00
|
|
|
int
|
2002-03-19 02:59:57 +03:00
|
|
|
main(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct linger linger;
|
2006-05-10 00:18:05 +04:00
|
|
|
int ch, on = 1;
|
|
|
|
socklen_t fromlen;
|
2000-01-31 17:20:13 +03:00
|
|
|
struct sockaddr_storage from;
|
2003-06-15 02:43:31 +04:00
|
|
|
struct protoent *proto;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-01-11 04:32:34 +03:00
|
|
|
openlog("rshd", LOG_PID, LOG_DAEMON);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
opterr = 0;
|
1997-10-08 05:04:31 +04:00
|
|
|
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;
|
1993-12-23 12:05:45 +03:00
|
|
|
case 'L':
|
1994-06-05 19:33:20 +04:00
|
|
|
log_success = 1;
|
1993-12-23 12:05:45 +03:00
|
|
|
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;
|
|
|
|
|
2005-03-28 01:00:58 +04:00
|
|
|
fromlen = sizeof(from); /* xxx */
|
|
|
|
if (getpeername(STDIN_FILENO, (struct sockaddr *)&from, &fromlen) < 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
syslog(LOG_ERR, "getpeername: %m");
|
2005-03-28 01:00:58 +04:00
|
|
|
return EXIT_FAILURE;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2000-04-14 16:28:51 +04:00
|
|
|
#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;
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)memset(&sin, 0, sizeof(sin));
|
2000-04-14 16:28:51 +04:00
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_len = sizeof(struct sockaddr_in);
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[off],
|
2000-04-14 16:28:51 +04:00
|
|
|
sizeof(sin.sin_addr));
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)memcpy(&from, &sin, sizeof(sin));
|
2000-04-14 16:28:51 +04:00
|
|
|
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) {
|
2003-05-18 01:26:47 +04:00
|
|
|
strlcpy(hbuf, "invalid", sizeof(hbuf));
|
2000-04-14 16:28:51 +04:00
|
|
|
}
|
2002-03-19 02:59:57 +03:00
|
|
|
syslog(LOG_ERR, "malformed \"from\" address (v4 mapped, %s)",
|
2000-04-14 16:28:51 +04:00
|
|
|
hbuf);
|
2005-03-28 01:00:58 +04:00
|
|
|
return EXIT_FAILURE;
|
2000-04-14 16:28:51 +04:00
|
|
|
}
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
if (keepalive &&
|
2005-03-28 01:00:58 +04:00
|
|
|
setsockopt(STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
|
1993-03-21 12:45:37 +03:00
|
|
|
sizeof(on)) < 0)
|
|
|
|
syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
|
|
|
|
linger.l_onoff = 1;
|
|
|
|
linger.l_linger = 60; /* XXX */
|
2005-03-28 01:00:58 +04:00
|
|
|
if (setsockopt(STDIN_FILENO, SOL_SOCKET, SO_LINGER, (char *)&linger,
|
1993-03-21 12:45:37 +03:00
|
|
|
sizeof (linger)) < 0)
|
|
|
|
syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
|
2003-06-15 02:43:31 +04:00
|
|
|
proto = getprotobyname("tcp");
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)setsockopt(STDIN_FILENO, proto->p_proto, TCP_NODELAY, &on,
|
|
|
|
sizeof(on));
|
2000-01-31 17:20:13 +03:00
|
|
|
doit((struct sockaddr *)&from);
|
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
|
2002-03-19 02:59:57 +03:00
|
|
|
doit(struct sockaddr *fromp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-03-30 05:33:30 +04:00
|
|
|
struct passwd *pwd, pwres;
|
1998-07-06 10:45:41 +04:00
|
|
|
in_port_t port;
|
2002-09-19 00:37:11 +04:00
|
|
|
struct pollfd set[2];
|
|
|
|
int cc, pv[2], pid, s = -1; /* XXX gcc */
|
1993-03-21 12:45:37 +03:00
|
|
|
int one = 1;
|
2005-02-20 08:45:55 +03:00
|
|
|
char *hostname, *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];
|
1995-01-20 21:48:50 +03:00
|
|
|
char hostnamebuf[2 * MAXHOSTNAMELEN + 1];
|
2000-01-22 13:22:55 +03:00
|
|
|
#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;
|
|
|
|
const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
|
2005-02-20 08:45:55 +03:00
|
|
|
const char *errormsg = NULL, *errorstr = NULL;
|
2005-03-30 05:33:30 +04:00
|
|
|
char pwbuf[1024];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)signal(SIGINT, SIG_DFL);
|
|
|
|
(void)signal(SIGQUIT, SIG_DFL);
|
|
|
|
(void)signal(SIGTERM, SIG_DFL);
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifdef DEBUG
|
2005-03-08 07:35:19 +03:00
|
|
|
{
|
2005-03-28 01:00:58 +04:00
|
|
|
int t = open(_PATH_TTY, O_RDWR);
|
2005-03-08 07:35:19 +03:00
|
|
|
if (t >= 0) {
|
2005-03-28 01:00:58 +04:00
|
|
|
ioctl(t, TIOCNOTTY, NULL);
|
2005-03-08 07:35:19 +03:00
|
|
|
(void)close(t);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
#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:
|
2002-03-19 02:59:57 +03:00
|
|
|
syslog(LOG_ERR, "malformed \"from\" address (af %d)", af);
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2000-01-31 17:20:13 +03:00
|
|
|
}
|
|
|
|
if (getnameinfo(fromp, fromp->sa_len, naddr, sizeof(naddr),
|
|
|
|
pbuf, sizeof(pbuf), niflags) != 0) {
|
2002-03-19 02:59:57 +03:00
|
|
|
syslog(LOG_ERR, "malformed \"from\" address (af %d)", af);
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
#ifdef IP_OPTIONS
|
2005-03-08 07:35:19 +03:00
|
|
|
if (af == AF_INET) {
|
|
|
|
|
|
|
|
u_char optbuf[BUFSIZ/3];
|
2006-05-10 00:18:05 +04:00
|
|
|
socklen_t optsize = sizeof(optbuf);
|
|
|
|
int ipproto, i;
|
1993-03-21 12:45:37 +03: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) {
|
2005-03-08 07:35:19 +03:00
|
|
|
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",
|
|
|
|
inet_ntoa((
|
|
|
|
(struct sockaddr_in *)fromp)->sin_addr),
|
|
|
|
c == IPOPT_LSRR ? "LSRR" : "SSRR");
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-03-08 07:35:19 +03:00
|
|
|
}
|
|
|
|
if (c == IPOPT_EOL)
|
|
|
|
break;
|
|
|
|
i += (c == IPOPT_NOP) ? 1 : optbuf[i + 1];
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
2005-03-08 07:35:19 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
2000-01-31 17:20:13 +03:00
|
|
|
if (ntohs(*portp) >= IPPORT_RESERVED
|
2005-03-28 01:00:58 +04:00
|
|
|
|| ntohs(*portp) < IPPORT_RESERVED / 2) {
|
1998-02-28 16:29:03 +03:00
|
|
|
syslog(LOG_NOTICE|LOG_AUTH,
|
|
|
|
"Connection from %s on illegal port %u",
|
2000-01-31 17:20:13 +03:00
|
|
|
naddr, ntohs(*portp));
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1998-02-28 16:29:03 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
(void) alarm(60);
|
|
|
|
port = 0;
|
|
|
|
for (;;) {
|
|
|
|
char c;
|
1998-02-28 16:29:03 +03:00
|
|
|
|
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)
|
2001-01-11 04:32:34 +03:00
|
|
|
syslog(LOG_ERR, "read: %m");
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)shutdown(0, SHUT_RDWR);
|
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1998-02-28 16:29:03 +03:00
|
|
|
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");
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1998-01-08 06:30:49 +03:00
|
|
|
if (port >= IPPORT_RESERVED) {
|
2002-03-19 02:59:57 +03:00
|
|
|
syslog(LOG_ERR, "2nd port not reserved");
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1998-01-08 06:30:49 +03:00
|
|
|
}
|
2000-01-31 17:20:13 +03:00
|
|
|
*portp = htons(port);
|
2005-03-11 19:04:09 +03:00
|
|
|
if (connect(s, fromp, fromp->sa_len) < 0) {
|
2001-01-11 04:32:34 +03:00
|
|
|
syslog(LOG_ERR, "connect second port %d: %m", port);
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef notdef
|
|
|
|
/* from inetd, socket is already on 0, 1, 2 */
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)dup2(f, STDIN_FILENO);
|
|
|
|
(void)dup2(f, STDOUT_FILENO);
|
|
|
|
(void)dup2(f, STDERR_FILENO);
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
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;
|
2000-11-09 04:04:14 +03:00
|
|
|
res0 = NULL;
|
2000-01-31 17:20:13 +03:00
|
|
|
if (check_all || local_domain(saddr)) {
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)strlcpy(remotehost, saddr, sizeof(remotehost));
|
1993-03-21 12:45:37 +03:00
|
|
|
errorhost = remotehost;
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)memset(&hints, 0, sizeof(hints));
|
2000-01-31 17:20:13 +03:00
|
|
|
hints.ai_family = fromp->sa_family;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = AI_CANONNAME;
|
|
|
|
gaierror = getaddrinfo(remotehost, pbuf, &hints, &res0);
|
|
|
|
if (gaierror) {
|
2001-01-11 04:32:34 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)strlcpy(hostnamebuf, hostname, sizeof(hostnamebuf));
|
2002-03-19 02:59:57 +03:00
|
|
|
hostname = hostnamebuf;
|
2000-11-09 04:04:14 +03:00
|
|
|
if (res0)
|
|
|
|
freeaddrinfo(res0);
|
2000-01-31 17:20:13 +03:00
|
|
|
} else {
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)strlcpy(hostnamebuf, naddr, sizeof(hostnamebuf));
|
2002-03-19 02:59:57 +03:00
|
|
|
errorhost = hostname = hostnamebuf;
|
2000-01-31 17:20:13 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-02-20 08:45:55 +03:00
|
|
|
(void)alarm(60);
|
1995-01-20 21:48:50 +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");
|
2005-02-20 08:45:55 +03:00
|
|
|
(void)alarm(0);
|
|
|
|
|
|
|
|
#ifdef USE_PAM
|
|
|
|
pam_err = pam_start("rsh", locuser, &pamc, &pamh);
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
syslog(LOG_ERR|LOG_AUTH, "pam_start(): %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
2005-03-28 01:00:58 +04:00
|
|
|
rshd_errx(EXIT_FAILURE, incorrect);
|
2005-02-20 08:45:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((pam_err = pam_set_item(pamh, PAM_RUSER, remuser)) != PAM_SUCCESS ||
|
|
|
|
(pam_err = pam_set_item(pamh, PAM_RHOST, hostname) != PAM_SUCCESS)){
|
|
|
|
syslog(LOG_ERR|LOG_AUTH, "pam_set_item(): %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
2005-03-28 01:00:58 +04:00
|
|
|
rshd_errx(EXIT_FAILURE, incorrect);
|
2005-02-20 08:45:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pam_err = pam_authenticate(pamh, 0);
|
|
|
|
if (pam_err == PAM_SUCCESS) {
|
|
|
|
if ((pam_err = pam_get_user(pamh, &cp, NULL)) == PAM_SUCCESS) {
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)strlcpy(locuser, cp, sizeof(locuser));
|
2005-02-20 08:45:55 +03:00
|
|
|
/* XXX truncation! */
|
|
|
|
}
|
|
|
|
pam_err = pam_acct_mgmt(pamh, 0);
|
|
|
|
}
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
errorstr = incorrect;
|
|
|
|
errormsg = pam_strerror(pamh, pam_err);
|
|
|
|
goto badlogin;
|
|
|
|
}
|
|
|
|
#endif /* USE_PAM */
|
1993-03-21 12:45:37 +03:00
|
|
|
setpwent();
|
2005-04-19 07:22:54 +04:00
|
|
|
if (getpwnam_r(locuser, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
|
|
|
|
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)
|
2005-02-20 08:45:55 +03:00
|
|
|
errorstr = "Permission denied.";
|
2005-03-28 01:00:58 +04:00
|
|
|
rshd_errx(EXIT_FAILURE, errorstr, errorhost);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2000-01-22 13:22:55 +03:00
|
|
|
#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) {
|
2005-02-20 08:45:55 +03:00
|
|
|
if (chdir("/") < 0
|
2000-01-22 13:22:55 +03:00
|
|
|
#ifdef LOGIN_CAP
|
2005-02-20 08:45:55 +03:00
|
|
|
|| login_getcapbool(lc, "requirehome", pwd->pw_uid ? 1 : 0)
|
|
|
|
#endif
|
|
|
|
) {
|
2000-01-22 13:22:55 +03:00
|
|
|
syslog(LOG_INFO|LOG_AUTH,
|
|
|
|
"%s@%s as %s: no home directory. cmd='%.80s'",
|
|
|
|
remuser, hostname, locuser, cmdbuf);
|
2005-03-28 01:00:58 +04:00
|
|
|
rshd_errx(EXIT_SUCCESS, "No remote home directory.");
|
2000-01-22 13:22:55 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2005-02-20 08:45:55 +03:00
|
|
|
#ifndef USE_PAM
|
1998-02-28 16:29:03 +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)) {
|
2005-02-20 08:45:55 +03:00
|
|
|
errormsg = __rcmd_errstr ? __rcmd_errstr : "unknown error";
|
1998-02-28 16:29:03 +03:00
|
|
|
if (errorstr == NULL)
|
2005-02-20 08:45:55 +03:00
|
|
|
errorstr = "Permission denied.";
|
2005-02-20 09:11:51 +03:00
|
|
|
goto badlogin;
|
1998-02-28 16:29:03 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-02-20 08:45:55 +03:00
|
|
|
if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK))
|
2005-03-28 01:00:58 +04:00
|
|
|
rshd_errx(EXIT_FAILURE, "Logins currently disabled.");
|
2005-03-12 21:23:30 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LOGIN_CAP
|
|
|
|
/*
|
|
|
|
* 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) {
|
|
|
|
syslog(LOG_ERR, "setusercontext: %m");
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-03-12 21:23:30 +03:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
initgroups(pwd->pw_name, pwd->pw_gid);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_PAM
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)write(STDERR_FILENO, "\0", 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
sent_null = 1;
|
|
|
|
|
|
|
|
if (port) {
|
2005-02-20 08:45:55 +03:00
|
|
|
if (pipe(pv) < 0)
|
2005-03-28 01:00:58 +04:00
|
|
|
rshd_errx(EXIT_FAILURE, "Can't make pipe. (%s)",
|
|
|
|
strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
pid = fork();
|
2005-02-20 08:45:55 +03:00
|
|
|
if (pid == -1)
|
2005-03-28 01:00:58 +04:00
|
|
|
rshd_errx(EXIT_FAILURE, "Can't fork. (%s)",
|
|
|
|
strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
if (pid) {
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)close(STDIN_FILENO);
|
|
|
|
(void)close(STDOUT_FILENO);
|
|
|
|
(void)close(STDERR_FILENO);
|
|
|
|
(void)close(pv[1]);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-09-19 00:37:11 +04:00
|
|
|
set[0].fd = s;
|
|
|
|
set[0].events = POLLIN;
|
|
|
|
set[1].fd = pv[0];
|
|
|
|
set[1].events = POLLIN;
|
1998-02-28 16:29:03 +03:00
|
|
|
ioctl(pv[0], FIONBIO, (char *)&one);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/* should set s nbio! */
|
|
|
|
do {
|
2002-09-19 00:37:11 +04:00
|
|
|
if (poll(set, 2, INFTIM) < 0)
|
1998-02-28 16:29:03 +03:00
|
|
|
break;
|
2002-09-19 00:37:11 +04:00
|
|
|
if (set[0].revents & POLLIN) {
|
1993-03-21 12:45:37 +03:00
|
|
|
int ret;
|
1998-02-28 16:29:03 +03:00
|
|
|
|
|
|
|
ret = read(s, &sig, 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (ret <= 0)
|
2002-09-19 00:37:11 +04:00
|
|
|
set[0].events = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
|
|
|
killpg(pid, sig);
|
|
|
|
}
|
2002-09-19 00:37:11 +04:00
|
|
|
if (set[1].revents & POLLIN) {
|
1993-03-21 12:45:37 +03:00
|
|
|
errno = 0;
|
|
|
|
cc = read(pv[0], buf, sizeof(buf));
|
|
|
|
if (cc <= 0) {
|
2001-01-11 04:32:34 +03:00
|
|
|
shutdown(s, SHUT_RDWR);
|
2002-09-19 00:37:11 +04:00
|
|
|
set[1].events = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)write(s, buf, cc);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-19 00:37:11 +04:00
|
|
|
} while ((set[0].revents | set[1].revents) & POLLIN);
|
2005-03-12 21:23:30 +03:00
|
|
|
PAM_END;
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_SUCCESS);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)close(s);
|
|
|
|
(void)close(pv[0]);
|
|
|
|
(void)dup2(pv[1], STDERR_FILENO);
|
1993-03-21 12:45:37 +03:00
|
|
|
close(pv[1]);
|
|
|
|
}
|
2005-02-20 08:45:55 +03:00
|
|
|
#ifdef USE_PAM
|
|
|
|
else {
|
|
|
|
pid = fork();
|
|
|
|
if (pid == -1)
|
2005-03-28 01:00:58 +04:00
|
|
|
rshd_errx(EXIT_FAILURE, "Can't fork. (%s)",
|
|
|
|
strerror(errno));
|
2005-02-20 08:45:55 +03:00
|
|
|
if (pid) {
|
2005-03-12 21:23:30 +03:00
|
|
|
pid_t xpid;
|
|
|
|
int status;
|
|
|
|
if ((xpid = waitpid(pid, &status, 0)) != pid) {
|
|
|
|
pam_err = pam_close_session(pamh, 0);
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"pam_close_session: %s",
|
|
|
|
pam_strerror(pamh, pam_err));
|
|
|
|
}
|
|
|
|
PAM_END;
|
|
|
|
if (xpid != -1)
|
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"wrong PID: %d != %d", pid, xpid);
|
|
|
|
else
|
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"wait pid=%d failed %m", pid);
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-03-12 21:23:30 +03:00
|
|
|
}
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_SUCCESS);
|
2005-03-12 21:23:30 +03:00
|
|
|
}
|
2005-02-20 08:45:55 +03:00
|
|
|
}
|
|
|
|
#endif
|
2000-01-22 13:22:55 +03:00
|
|
|
|
2005-02-20 08:45:55 +03:00
|
|
|
#ifdef F_CLOSEM
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)fcntl(STDERR_FILENO + 1, F_CLOSEM, 0);
|
2005-02-20 08:45:55 +03:00
|
|
|
#else
|
2005-03-28 01:00:58 +04:00
|
|
|
for (fd = getdtablesize(); fd > STDERR_FILENO; fd--)
|
2005-02-20 08:45:55 +03:00
|
|
|
(void)close(fd);
|
|
|
|
#endif
|
|
|
|
if (setsid() == -1)
|
|
|
|
syslog(LOG_ERR, "setsid() failed: %m");
|
|
|
|
#ifdef USE_PAM
|
|
|
|
if (setlogin(pwd->pw_name) < 0)
|
|
|
|
syslog(LOG_ERR, "setlogin() failed: %m");
|
|
|
|
|
2005-03-12 21:23:30 +03:00
|
|
|
if (*pwd->pw_shell == '\0')
|
2005-03-28 01:00:58 +04:00
|
|
|
pwd->pw_shell = __UNCONST(_PATH_BSHELL);
|
2005-03-12 21:23:30 +03:00
|
|
|
|
2005-02-20 08:45:55 +03: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);
|
2005-03-12 21:23:30 +03:00
|
|
|
#else
|
2000-01-22 13:22:55 +03:00
|
|
|
#ifdef LOGIN_CAP
|
2005-03-12 21:23:30 +03:00
|
|
|
{
|
|
|
|
char *sh;
|
|
|
|
if ((sh = login_getcapstr(lc, "shell", NULL, NULL))) {
|
|
|
|
if(!(sh = strdup(sh))) {
|
|
|
|
syslog(LOG_ERR, "Cannot alloc mem");
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-03-12 21:23:30 +03:00
|
|
|
}
|
|
|
|
pwd->pw_shell = sh;
|
2000-01-22 13:22:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
environ = envinit;
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)strlcat(homedir, pwd->pw_dir, sizeof(homedir));
|
|
|
|
(void)strlcat(path, _PATH_DEFPATH, sizeof(path));
|
|
|
|
(void)strlcat(shell, pwd->pw_shell, sizeof(shell));
|
|
|
|
(void)strlcat(username, pwd->pw_name, sizeof(username));
|
2005-02-20 08:45:55 +03:00
|
|
|
#endif
|
|
|
|
|
2000-01-22 13:22:55 +03:00
|
|
|
cp = strrchr(pwd->pw_shell, '/');
|
|
|
|
if (cp)
|
|
|
|
cp++;
|
|
|
|
else
|
|
|
|
cp = pwd->pw_shell;
|
2005-02-20 08:45:55 +03:00
|
|
|
|
|
|
|
#ifdef LOGIN_CAP
|
|
|
|
if (setusercontext(lc, pwd, pwd->pw_uid,
|
|
|
|
LOGIN_SETALL & ~LOGIN_SETGROUP) < 0) {
|
|
|
|
syslog(LOG_ERR, "setusercontext(): %m");
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-02-20 08:45:55 +03:00
|
|
|
}
|
|
|
|
login_close(lc);
|
|
|
|
#else
|
|
|
|
(void)setgid((gid_t)pwd->pw_gid);
|
|
|
|
(void)setuid((uid_t)pwd->pw_uid);
|
|
|
|
#endif
|
|
|
|
endpwent();
|
|
|
|
if (log_success || pwd->pw_uid == 0) {
|
|
|
|
syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
|
|
|
|
remuser, hostname, locuser, cmdbuf);
|
|
|
|
}
|
2005-03-28 01:00:58 +04:00
|
|
|
(void)execl(pwd->pw_shell, cp, "-c", cmdbuf, NULL);
|
|
|
|
rshd_errx(EXIT_FAILURE, "%s: %s", pwd->pw_shell, strerror(errno));
|
2005-02-20 08:45:55 +03:00
|
|
|
badlogin:
|
|
|
|
syslog(LOG_INFO|LOG_AUTH,
|
|
|
|
"%s@%s as %s: permission denied (%s). cmd='%.80s'",
|
|
|
|
remuser, hostname, locuser, errormsg, cmdbuf);
|
2005-03-28 01:00:58 +04:00
|
|
|
rshd_errx(EXIT_FAILURE, errorstr, errorhost);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
*/
|
2002-03-19 02:59:57 +03:00
|
|
|
|
1994-06-05 19:33:20 +04:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2005-02-20 08:45:55 +03:00
|
|
|
void
|
|
|
|
rshd_errx(int error, const char *fmt, ...)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-06-05 19:33:20 +04:00
|
|
|
va_list ap;
|
2005-02-20 08:45:55 +03:00
|
|
|
int len, rv;
|
1994-06-05 19:33:20 +04:00
|
|
|
char *bp, buf[BUFSIZ];
|
|
|
|
va_start(ap, fmt);
|
|
|
|
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;
|
2005-02-20 08:45:55 +03:00
|
|
|
rv = vsnprintf(bp, sizeof(buf) - 2, fmt, ap);
|
|
|
|
bp[rv++] = '\n';
|
|
|
|
(void)write(STDERR_FILENO, buf, len + rv);
|
2001-09-24 17:22:25 +04:00
|
|
|
va_end(ap);
|
2005-02-20 08:45:55 +03:00
|
|
|
exit(error);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-06-05 19:33:20 +04:00
|
|
|
void
|
2005-03-28 01:00:58 +04:00
|
|
|
getstr(char *buf, int cnt, const char *err)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
do {
|
1994-06-05 19:33:20 +04:00
|
|
|
if (read(STDIN_FILENO, &c, 1) != 1)
|
2005-03-28 01:00:58 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 12:45:37 +03:00
|
|
|
*buf++ = c;
|
2005-02-20 08:45:55 +03:00
|
|
|
if (--cnt == 0)
|
2005-03-28 01:00:58 +04:00
|
|
|
rshd_errx(EXIT_FAILURE, "%s too long", err);
|
1993-03-21 12:45:37 +03: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.
|
|
|
|
*/
|
1994-06-05 19:33:20 +04:00
|
|
|
int
|
2002-03-19 02:59:57 +03:00
|
|
|
local_domain(char *h)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-07-06 10:45:41 +04:00
|
|
|
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;
|
1998-07-06 10:45:41 +04:00
|
|
|
(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 *
|
2002-03-19 02:59:57 +03:00
|
|
|
topdomain(char *h)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
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
|
2002-03-19 02:59:57 +03:00
|
|
|
usage(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-06-05 19:33:20 +04:00
|
|
|
|
2005-03-28 01:00:58 +04:00
|
|
|
syslog(LOG_ERR, "Usage: %s [-%s]", getprogname(), OPTIONS);
|
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|