146677Sbostic /*-
2*66728Spendry * Copyright (c) 1988, 1989, 1992, 1993, 1994
361447Sbostic * The Regents of the University of California. All rights reserved.
435485Sbostic *
542672Sbostic * %sccs.include.redist.c%
621176Sdist */
721176Sdist
86454Swnj #ifndef lint
961447Sbostic static char copyright[] =
10*66728Spendry "@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
1161447Sbostic The Regents of the University of California. All rights reserved.\n";
1235485Sbostic #endif /* not lint */
136454Swnj
1421176Sdist #ifndef lint
15*66728Spendry static char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 04/06/94";
1635485Sbostic #endif /* not lint */
1721176Sdist
1846677Sbostic /*
1916370Skarels * remote shell server:
2036629Skarels * [port]\0
2116370Skarels * remuser\0
2216370Skarels * locuser\0
2316370Skarels * command\0
2416370Skarels * data
2516370Skarels */
2636629Skarels #include <sys/param.h>
276454Swnj #include <sys/ioctl.h>
2827901Slepreau #include <sys/time.h>
2955260Sandrew #include <sys/socket.h>
309212Ssam
319212Ssam #include <netinet/in.h>
3224903Sbloom #include <arpa/inet.h>
3346677Sbostic #include <netdb.h>
3424903Sbloom
35*66728Spendry #include <errno.h>
3655260Sandrew #include <fcntl.h>
37*66728Spendry #include <paths.h>
38*66728Spendry #include <pwd.h>
3955260Sandrew #include <signal.h>
4046677Sbostic #include <stdio.h>
4146677Sbostic #include <stdlib.h>
4246677Sbostic #include <string.h>
43*66728Spendry #include <syslog.h>
44*66728Spendry #include <unistd.h>
456454Swnj
4636600Sbostic int keepalive = 1;
4755260Sandrew int check_all;
4855260Sandrew int log_success; /* If TRUE, log all successful accesses */
4939122Skarels int sent_null;
5016370Skarels
5155260Sandrew void doit __P((struct sockaddr_in *));
5255260Sandrew void error __P((const char *, ...));
5355260Sandrew void getstr __P((char *, int, char *));
5455260Sandrew int local_domain __P((char *));
5555260Sandrew char *topdomain __P((char *));
5655260Sandrew void usage __P((void));
5755260Sandrew
5839793Sbostic #ifdef KERBEROS
5941763Skfall #include <kerberosIV/des.h>
6040679Sbostic #include <kerberosIV/krb.h>
6139793Sbostic #define VERSION_SIZE 9
6239793Sbostic #define SECURE_MESSAGE "This rsh session is using DES encryption for all transmissions.\r\n"
6355260Sandrew #define OPTIONS "alnkvxL"
6439793Sbostic char authbuf[sizeof(AUTH_DAT)];
6539793Sbostic char tickbuf[sizeof(KTEXT_ST)];
6646677Sbostic int doencrypt, use_kerberos, vacuous;
6739793Sbostic Key_schedule schedule;
6839793Sbostic #else
6955260Sandrew #define OPTIONS "alnL"
7039793Sbostic #endif
7139793Sbostic
7255260Sandrew int
main(argc,argv)736454Swnj main(argc, argv)
746454Swnj int argc;
7555260Sandrew char *argv[];
766454Swnj {
7755260Sandrew extern int __check_rhosts_file;
7817156Ssam struct linger linger;
7936318Sbostic int ch, on = 1, fromlen;
806454Swnj struct sockaddr_in from;
816454Swnj
8236712Skfall openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
8336318Sbostic
8436318Sbostic opterr = 0;
8539793Sbostic while ((ch = getopt(argc, argv, OPTIONS)) != EOF)
8639122Skarels switch (ch) {
8739122Skarels case 'a':
8839122Skarels check_all = 1;
8939122Skarels break;
9036318Sbostic case 'l':
9155260Sandrew __check_rhosts_file = 0;
9236318Sbostic break;
9336600Sbostic case 'n':
9436600Sbostic keepalive = 0;
9536600Sbostic break;
9639793Sbostic #ifdef KERBEROS
9739793Sbostic case 'k':
9839793Sbostic use_kerberos = 1;
9939793Sbostic break;
10039793Sbostic
10139793Sbostic case 'v':
10239793Sbostic vacuous = 1;
10339793Sbostic break;
10439793Sbostic
10555260Sandrew #ifdef CRYPT
10655260Sandrew case 'x':
10755260Sandrew doencrypt = 1;
10855260Sandrew break;
10939793Sbostic #endif
11055260Sandrew #endif
11155260Sandrew case 'L':
11255260Sandrew log_success = 1;
11355260Sandrew break;
11436318Sbostic case '?':
11536318Sbostic default:
11639793Sbostic usage();
117*66728Spendry break;
11836318Sbostic }
11936611Skfall
12036318Sbostic argc -= optind;
12136318Sbostic argv += optind;
12236318Sbostic
12339793Sbostic #ifdef KERBEROS
12439793Sbostic if (use_kerberos && vacuous) {
12539793Sbostic syslog(LOG_ERR, "only one of -k and -v allowed");
12640657Skarels exit(2);
12739793Sbostic }
12855260Sandrew #ifdef CRYPT
12955260Sandrew if (doencrypt && !use_kerberos) {
13055260Sandrew syslog(LOG_ERR, "-k is required for -x");
13155260Sandrew exit(2);
13255260Sandrew }
13339793Sbostic #endif
13455260Sandrew #endif
13539793Sbostic
13616370Skarels fromlen = sizeof (from);
13746677Sbostic if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
13839122Skarels syslog(LOG_ERR, "getpeername: %m");
13916370Skarels _exit(1);
1408380Ssam }
14136600Sbostic if (keepalive &&
14236600Sbostic setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
14336600Sbostic sizeof(on)) < 0)
14417187Sralph syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
14517156Ssam linger.l_onoff = 1;
14617156Ssam linger.l_linger = 60; /* XXX */
14727901Slepreau if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
14827901Slepreau sizeof (linger)) < 0)
14917187Sralph syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
15036700Skarels doit(&from);
15155260Sandrew /* NOTREACHED */
1526454Swnj }
1536454Swnj
1546454Swnj char username[20] = "USER=";
1556454Swnj char homedir[64] = "HOME=";
1566454Swnj char shell[64] = "SHELL=";
15746862Sbostic char path[100] = "PATH=";
1586454Swnj char *envinit[] =
15946862Sbostic {homedir, shell, path, username, 0};
1606454Swnj char **environ;
1616454Swnj
16255260Sandrew void
doit(fromp)16336700Skarels doit(fromp)
1646454Swnj struct sockaddr_in *fromp;
1656454Swnj {
16655260Sandrew extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
16755260Sandrew struct hostent *hp;
1686454Swnj struct passwd *pwd;
16943253Sbostic u_short port;
17036629Skarels fd_set ready, readfrom;
17155260Sandrew int cc, nfd, pv[2], pid, s;
1726454Swnj int one = 1;
17355260Sandrew char *hostname, *errorstr, *errorhost;
17455260Sandrew char *cp, sig, buf[BUFSIZ];
17555260Sandrew char cmdbuf[NCARGS+1], locuser[16], remuser[16];
17636629Skarels char remotehost[2 * MAXHOSTNAMELEN + 1];
1776454Swnj
17839793Sbostic #ifdef KERBEROS
17939793Sbostic AUTH_DAT *kdata = (AUTH_DAT *) NULL;
18039793Sbostic KTEXT ticket = (KTEXT) NULL;
18139793Sbostic char instance[INST_SZ], version[VERSION_SIZE];
18239793Sbostic struct sockaddr_in fromaddr;
18339793Sbostic int rc;
18439793Sbostic long authopts;
18539793Sbostic int pv1[2], pv2[2];
18639793Sbostic fd_set wready, writeto;
18739793Sbostic
18839793Sbostic fromaddr = *fromp;
18939793Sbostic #endif
19039793Sbostic
1916454Swnj (void) signal(SIGINT, SIG_DFL);
1926454Swnj (void) signal(SIGQUIT, SIG_DFL);
1936454Swnj (void) signal(SIGTERM, SIG_DFL);
19410883Ssam #ifdef DEBUG
19537697Sbostic { int t = open(_PATH_TTY, 2);
1966454Swnj if (t >= 0) {
1976454Swnj ioctl(t, TIOCNOTTY, (char *)0);
1986454Swnj (void) close(t);
1996454Swnj }
2006454Swnj }
2016454Swnj #endif
2026454Swnj fromp->sin_port = ntohs((u_short)fromp->sin_port);
20332108Skarels if (fromp->sin_family != AF_INET) {
20440657Skarels syslog(LOG_ERR, "malformed \"from\" address (af %d)\n",
20540657Skarels fromp->sin_family);
2066454Swnj exit(1);
20710596Ssam }
20836700Skarels #ifdef IP_OPTIONS
20936700Skarels {
21036700Skarels u_char optbuf[BUFSIZ/3], *cp;
21136700Skarels char lbuf[BUFSIZ], *lp;
21236700Skarels int optsize = sizeof(optbuf), ipproto;
21336700Skarels struct protoent *ip;
21436611Skfall
21536700Skarels if ((ip = getprotobyname("ip")) != NULL)
21636700Skarels ipproto = ip->p_proto;
21736700Skarels else
21836700Skarels ipproto = IPPROTO_IP;
21939793Sbostic if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
22036700Skarels optsize != 0) {
22136700Skarels lp = lbuf;
22236700Skarels for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
22336700Skarels sprintf(lp, " %2.2x", *cp);
22436700Skarels syslog(LOG_NOTICE,
22540657Skarels "Connection received from %s using IP options (ignored):%s",
22640657Skarels inet_ntoa(fromp->sin_addr), lbuf);
22736700Skarels if (setsockopt(0, ipproto, IP_OPTIONS,
22846677Sbostic (char *)NULL, optsize) != 0) {
22936700Skarels syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
23036700Skarels exit(1);
23136700Skarels }
23236700Skarels }
23336700Skarels }
23436700Skarels #endif
23536700Skarels
23639793Sbostic #ifdef KERBEROS
23739793Sbostic if (!use_kerberos)
23839793Sbostic #endif
23939793Sbostic if (fromp->sin_port >= IPPORT_RESERVED ||
24043253Sbostic fromp->sin_port < IPPORT_RESERVED/2) {
24139793Sbostic syslog(LOG_NOTICE|LOG_AUTH,
24255260Sandrew "Connection from %s on illegal port %u",
24355260Sandrew inet_ntoa(fromp->sin_addr),
24455260Sandrew fromp->sin_port);
24539793Sbostic exit(1);
24639793Sbostic }
24736611Skfall
2486454Swnj (void) alarm(60);
2496454Swnj port = 0;
2506454Swnj for (;;) {
2516454Swnj char c;
25255770Sbostic if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
25332108Skarels if (cc < 0)
25432108Skarels syslog(LOG_NOTICE, "read: %m");
25536700Skarels shutdown(0, 1+1);
2566454Swnj exit(1);
25710596Ssam }
25839793Sbostic if (c== 0)
2596454Swnj break;
2606454Swnj port = port * 10 + c - '0';
2616454Swnj }
26236611Skfall
2636454Swnj (void) alarm(0);
2646454Swnj if (port != 0) {
26527901Slepreau int lport = IPPORT_RESERVED - 1;
26610273Ssam s = rresvport(&lport);
26710596Ssam if (s < 0) {
26817187Sralph syslog(LOG_ERR, "can't get stderr port: %m");
2696454Swnj exit(1);
27010596Ssam }
27139793Sbostic #ifdef KERBEROS
27239793Sbostic if (!use_kerberos)
27339793Sbostic #endif
27439793Sbostic if (port >= IPPORT_RESERVED) {
27539793Sbostic syslog(LOG_ERR, "2nd port not reserved\n");
27639793Sbostic exit(1);
27739793Sbostic }
27843253Sbostic fromp->sin_port = htons(port);
27946677Sbostic if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) {
28055260Sandrew syslog(LOG_INFO, "connect second port %d: %m", port);
2816454Swnj exit(1);
28210596Ssam }
2836454Swnj }
28436611Skfall
28539793Sbostic #ifdef KERBEROS
28639793Sbostic if (vacuous) {
28739793Sbostic error("rshd: remote host requires Kerberos authentication\n");
28839793Sbostic exit(1);
28939793Sbostic }
29039793Sbostic #endif
29139793Sbostic
29236629Skarels #ifdef notdef
29336700Skarels /* from inetd, socket is already on 0, 1, 2 */
29410596Ssam dup2(f, 0);
29510596Ssam dup2(f, 1);
29610596Ssam dup2(f, 2);
29736629Skarels #endif
29855260Sandrew errorstr = NULL;
29927901Slepreau hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (struct in_addr),
3008380Ssam fromp->sin_family);
30136611Skfall if (hp) {
30236629Skarels /*
30336629Skarels * If name returned by gethostbyaddr is in our domain,
30436629Skarels * attempt to verify that we haven't been fooled by someone
30536629Skarels * in a remote net; look up the name and check that this
30636629Skarels * address corresponds to the name.
30736629Skarels */
30844662Skarels hostname = hp->h_name;
30940657Skarels #ifdef KERBEROS
31040657Skarels if (!use_kerberos)
31140657Skarels #endif
31240657Skarels if (check_all || local_domain(hp->h_name)) {
31336629Skarels strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
31436629Skarels remotehost[sizeof(remotehost) - 1] = 0;
31540657Skarels errorhost = remotehost;
31636611Skfall hp = gethostbyname(remotehost);
31736629Skarels if (hp == NULL) {
31836629Skarels syslog(LOG_INFO,
31936629Skarels "Couldn't look up address for %s",
32036629Skarels remotehost);
32143253Sbostic errorstr =
32240657Skarels "Couldn't look up address for your host (%s)\n";
32340657Skarels hostname = inet_ntoa(fromp->sin_addr);
32439793Sbostic } else for (; ; hp->h_addr_list++) {
32536629Skarels if (hp->h_addr_list[0] == NULL) {
32636629Skarels syslog(LOG_NOTICE,
32736629Skarels "Host addr %s not listed for host %s",
32836629Skarels inet_ntoa(fromp->sin_addr),
32936629Skarels hp->h_name);
33040657Skarels errorstr =
33140657Skarels "Host address mismatch for %s\n";
33240657Skarels hostname = inet_ntoa(fromp->sin_addr);
33340657Skarels break;
33436611Skfall }
33539793Sbostic if (!bcmp(hp->h_addr_list[0],
33639793Sbostic (caddr_t)&fromp->sin_addr,
33740657Skarels sizeof(fromp->sin_addr))) {
33840657Skarels hostname = hp->h_name;
33939793Sbostic break;
34040657Skarels }
34136611Skfall }
34236611Skfall }
34336629Skarels } else
34440657Skarels errorhost = hostname = inet_ntoa(fromp->sin_addr);
34536611Skfall
34639793Sbostic #ifdef KERBEROS
34739793Sbostic if (use_kerberos) {
34839793Sbostic kdata = (AUTH_DAT *) authbuf;
34939793Sbostic ticket = (KTEXT) tickbuf;
35039793Sbostic authopts = 0L;
35139793Sbostic strcpy(instance, "*");
35239793Sbostic version[VERSION_SIZE - 1] = '\0';
35355260Sandrew #ifdef CRYPT
35455260Sandrew if (doencrypt) {
35555260Sandrew struct sockaddr_in local_addr;
35655260Sandrew rc = sizeof(local_addr);
35755260Sandrew if (getsockname(0, (struct sockaddr *)&local_addr,
35855260Sandrew &rc) < 0) {
35955260Sandrew syslog(LOG_ERR, "getsockname: %m");
36055260Sandrew error("rlogind: getsockname: %m");
36155260Sandrew exit(1);
36255260Sandrew }
36355260Sandrew authopts = KOPT_DO_MUTUAL;
36455260Sandrew rc = krb_recvauth(authopts, 0, ticket,
36555260Sandrew "rcmd", instance, &fromaddr,
36655260Sandrew &local_addr, kdata, "", schedule,
36755260Sandrew version);
36855260Sandrew des_set_key(kdata->session, schedule);
36955260Sandrew } else
37055260Sandrew #endif
37139793Sbostic rc = krb_recvauth(authopts, 0, ticket, "rcmd",
37239793Sbostic instance, &fromaddr,
37339793Sbostic (struct sockaddr_in *) 0,
37439793Sbostic kdata, "", (bit_64 *) 0, version);
37539793Sbostic if (rc != KSUCCESS) {
37644662Skarels error("Kerberos authentication failure: %s\n",
37739793Sbostic krb_err_txt[rc]);
37839793Sbostic exit(1);
37939793Sbostic }
38039793Sbostic } else
38139793Sbostic #endif
38239793Sbostic getstr(remuser, sizeof(remuser), "remuser");
38339793Sbostic
3846454Swnj getstr(locuser, sizeof(locuser), "locuser");
3856454Swnj getstr(cmdbuf, sizeof(cmdbuf), "command");
3866454Swnj setpwent();
3876454Swnj pwd = getpwnam(locuser);
3886454Swnj if (pwd == NULL) {
38955260Sandrew syslog(LOG_INFO|LOG_AUTH,
39055260Sandrew "%s@%s as %s: unknown login. cmd='%.80s'",
39155260Sandrew remuser, hostname, locuser, cmdbuf);
39240657Skarels if (errorstr == NULL)
39340657Skarels errorstr = "Login incorrect.\n";
39440657Skarels goto fail;
3956454Swnj }
3966454Swnj if (chdir(pwd->pw_dir) < 0) {
39727901Slepreau (void) chdir("/");
39816370Skarels #ifdef notdef
39955260Sandrew syslog(LOG_INFO|LOG_AUTH,
40055260Sandrew "%s@%s as %s: no home directory. cmd='%.80s'",
40155260Sandrew remuser, hostname, locuser, cmdbuf);
4026454Swnj error("No remote directory.\n");
4036454Swnj exit(1);
40416370Skarels #endif
4056454Swnj }
40636611Skfall
40739793Sbostic #ifdef KERBEROS
40839793Sbostic if (use_kerberos) {
40939793Sbostic if (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0') {
41039793Sbostic if (kuserok(kdata, locuser) != 0) {
41155260Sandrew syslog(LOG_INFO|LOG_AUTH,
41240657Skarels "Kerberos rsh denied to %s.%s@%s",
41340657Skarels kdata->pname, kdata->pinst, kdata->prealm);
41439793Sbostic error("Permission denied.\n");
41539793Sbostic exit(1);
41639793Sbostic }
41739793Sbostic }
41839793Sbostic } else
41939793Sbostic #endif
42036611Skfall
42143253Sbostic if (errorstr ||
42240657Skarels pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
42355260Sandrew iruserok(fromp->sin_addr.s_addr, pwd->pw_uid == 0,
42455260Sandrew remuser, locuser) < 0) {
42555260Sandrew if (__rcmd_errstr)
42655260Sandrew syslog(LOG_INFO|LOG_AUTH,
42755260Sandrew "%s@%s as %s: permission denied (%s). cmd='%.80s'",
42855260Sandrew remuser, hostname, locuser, __rcmd_errstr,
42955260Sandrew cmdbuf);
43055260Sandrew else
43155260Sandrew syslog(LOG_INFO|LOG_AUTH,
43255260Sandrew "%s@%s as %s: permission denied. cmd='%.80s'",
43355260Sandrew remuser, hostname, locuser, cmdbuf);
43440657Skarels fail:
43540657Skarels if (errorstr == NULL)
43640657Skarels errorstr = "Permission denied.\n";
43740657Skarels error(errorstr, errorhost);
43839793Sbostic exit(1);
43939793Sbostic }
44039793Sbostic
44137294Sbostic if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
44231025Sbostic error("Logins currently disabled.\n");
44331025Sbostic exit(1);
44431025Sbostic }
44536712Skfall
44655770Sbostic (void) write(STDERR_FILENO, "\0", 1);
44739122Skarels sent_null = 1;
44836611Skfall
4496454Swnj if (port) {
4506454Swnj if (pipe(pv) < 0) {
4516454Swnj error("Can't make pipe.\n");
4526454Swnj exit(1);
4536454Swnj }
45455260Sandrew #ifdef CRYPT
45555260Sandrew #ifdef KERBEROS
45655260Sandrew if (doencrypt) {
45755260Sandrew if (pipe(pv1) < 0) {
45855260Sandrew error("Can't make 2nd pipe.\n");
45955260Sandrew exit(1);
46055260Sandrew }
46155260Sandrew if (pipe(pv2) < 0) {
46255260Sandrew error("Can't make 3rd pipe.\n");
46355260Sandrew exit(1);
46455260Sandrew }
46555260Sandrew }
46655260Sandrew #endif
46755260Sandrew #endif
4686454Swnj pid = fork();
4696454Swnj if (pid == -1) {
47039122Skarels error("Can't fork; try again.\n");
4716454Swnj exit(1);
4726454Swnj }
4736454Swnj if (pid) {
47455260Sandrew #ifdef CRYPT
47555260Sandrew #ifdef KERBEROS
47655260Sandrew if (doencrypt) {
47755260Sandrew static char msg[] = SECURE_MESSAGE;
47855260Sandrew (void) close(pv1[1]);
47955260Sandrew (void) close(pv2[1]);
48056423Sbostic des_write(s, msg, sizeof(msg) - 1);
48155260Sandrew
48255260Sandrew } else
48355260Sandrew #endif
48455260Sandrew #endif
48539793Sbostic {
48655260Sandrew (void) close(0);
48755260Sandrew (void) close(1);
48839793Sbostic }
48955260Sandrew (void) close(2);
49055260Sandrew (void) close(pv[1]);
49139793Sbostic
49236629Skarels FD_ZERO(&readfrom);
49336629Skarels FD_SET(s, &readfrom);
49436629Skarels FD_SET(pv[0], &readfrom);
49539793Sbostic if (pv[0] > s)
49639793Sbostic nfd = pv[0];
49739793Sbostic else
49839793Sbostic nfd = s;
49955260Sandrew #ifdef CRYPT
50055260Sandrew #ifdef KERBEROS
50155260Sandrew if (doencrypt) {
50255260Sandrew FD_ZERO(&writeto);
50355260Sandrew FD_SET(pv2[0], &writeto);
50455260Sandrew FD_SET(pv1[0], &readfrom);
50555260Sandrew
50655260Sandrew nfd = MAX(nfd, pv2[0]);
50755260Sandrew nfd = MAX(nfd, pv1[0]);
50855260Sandrew } else
50955260Sandrew #endif
51055260Sandrew #endif
51139793Sbostic ioctl(pv[0], FIONBIO, (char *)&one);
51239793Sbostic
5136454Swnj /* should set s nbio! */
51439793Sbostic nfd++;
5156454Swnj do {
5166454Swnj ready = readfrom;
51755260Sandrew #ifdef CRYPT
51855260Sandrew #ifdef KERBEROS
51955260Sandrew if (doencrypt) {
52055260Sandrew wready = writeto;
52155260Sandrew if (select(nfd, &ready,
52255260Sandrew &wready, (fd_set *) 0,
52355260Sandrew (struct timeval *) 0) < 0)
52455260Sandrew break;
52555260Sandrew } else
52655260Sandrew #endif
52755260Sandrew #endif
52839793Sbostic if (select(nfd, &ready, (fd_set *)0,
52943253Sbostic (fd_set *)0, (struct timeval *)0) < 0)
53039793Sbostic break;
53136629Skarels if (FD_ISSET(s, &ready)) {
53239793Sbostic int ret;
53355260Sandrew #ifdef CRYPT
53455260Sandrew #ifdef KERBEROS
53555260Sandrew if (doencrypt)
53655260Sandrew ret = des_read(s, &sig, 1);
53755260Sandrew else
53855260Sandrew #endif
53955260Sandrew #endif
54039793Sbostic ret = read(s, &sig, 1);
54139793Sbostic if (ret <= 0)
54236629Skarels FD_CLR(s, &readfrom);
5436454Swnj else
5446454Swnj killpg(pid, sig);
5456454Swnj }
54636629Skarels if (FD_ISSET(pv[0], &ready)) {
54711238Ssam errno = 0;
54839793Sbostic cc = read(pv[0], buf, sizeof(buf));
5496454Swnj if (cc <= 0) {
55010194Ssam shutdown(s, 1+1);
55136629Skarels FD_CLR(pv[0], &readfrom);
55239793Sbostic } else {
55355260Sandrew #ifdef CRYPT
55455260Sandrew #ifdef KERBEROS
55555260Sandrew if (doencrypt)
55639793Sbostic (void)
55755260Sandrew des_write(s, buf, cc);
55855260Sandrew else
55955260Sandrew #endif
56055260Sandrew #endif
56155260Sandrew (void)
56239793Sbostic write(s, buf, cc);
56339793Sbostic }
56439793Sbostic }
56555260Sandrew #ifdef CRYPT
56655260Sandrew #ifdef KERBEROS
56755260Sandrew if (doencrypt && FD_ISSET(pv1[0], &ready)) {
56855260Sandrew errno = 0;
56955260Sandrew cc = read(pv1[0], buf, sizeof(buf));
57055260Sandrew if (cc <= 0) {
57155260Sandrew shutdown(pv1[0], 1+1);
57255260Sandrew FD_CLR(pv1[0], &readfrom);
57355260Sandrew } else
57455770Sbostic (void) des_write(STDOUT_FILENO,
57555770Sbostic buf, cc);
57655260Sandrew }
57739793Sbostic
57855260Sandrew if (doencrypt && FD_ISSET(pv2[0], &wready)) {
57955260Sandrew errno = 0;
58055770Sbostic cc = des_read(STDIN_FILENO,
58155770Sbostic buf, sizeof(buf));
58255260Sandrew if (cc <= 0) {
58355260Sandrew shutdown(pv2[0], 1+1);
58455260Sandrew FD_CLR(pv2[0], &writeto);
58555260Sandrew } else
58655260Sandrew (void) write(pv2[0], buf, cc);
58755260Sandrew }
58855260Sandrew #endif
58955260Sandrew #endif
59055260Sandrew
59136629Skarels } while (FD_ISSET(s, &readfrom) ||
59255260Sandrew #ifdef CRYPT
59355260Sandrew #ifdef KERBEROS
59455260Sandrew (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
59555260Sandrew #endif
59655260Sandrew #endif
59736629Skarels FD_ISSET(pv[0], &readfrom));
5986454Swnj exit(0);
5996454Swnj }
6006454Swnj setpgrp(0, getpid());
60155260Sandrew (void) close(s);
60255260Sandrew (void) close(pv[0]);
60355260Sandrew #ifdef CRYPT
60455260Sandrew #ifdef KERBEROS
60555260Sandrew if (doencrypt) {
60655260Sandrew close(pv1[0]); close(pv2[0]);
60755260Sandrew dup2(pv1[1], 1);
60855260Sandrew dup2(pv2[1], 0);
60955260Sandrew close(pv1[1]);
61055260Sandrew close(pv2[1]);
61155260Sandrew }
61255260Sandrew #endif
61355260Sandrew #endif
6146454Swnj dup2(pv[1], 2);
61536712Skfall close(pv[1]);
6166454Swnj }
6176454Swnj if (*pwd->pw_shell == '\0')
61837294Sbostic pwd->pw_shell = _PATH_BSHELL;
61939122Skarels #if BSD > 43
62039122Skarels if (setlogin(pwd->pw_name) < 0)
62139122Skarels syslog(LOG_ERR, "setlogin() failed: %m");
62239122Skarels #endif
62327901Slepreau (void) setgid((gid_t)pwd->pw_gid);
6249212Ssam initgroups(pwd->pw_name, pwd->pw_gid);
62527901Slepreau (void) setuid((uid_t)pwd->pw_uid);
6266454Swnj environ = envinit;
6276454Swnj strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
62846862Sbostic strcat(path, _PATH_DEFPATH);
6296454Swnj strncat(shell, pwd->pw_shell, sizeof(shell)-7);
6306454Swnj strncat(username, pwd->pw_name, sizeof(username)-6);
631*66728Spendry cp = strrchr(pwd->pw_shell, '/');
6326454Swnj if (cp)
6336454Swnj cp++;
6346454Swnj else
6356454Swnj cp = pwd->pw_shell;
63638060Skfall endpwent();
63755260Sandrew if (log_success || pwd->pw_uid == 0) {
63839793Sbostic #ifdef KERBEROS
63939793Sbostic if (use_kerberos)
64055260Sandrew syslog(LOG_INFO|LOG_AUTH,
64155260Sandrew "Kerberos shell from %s.%s@%s on %s as %s, cmd='%.80s'",
64255260Sandrew kdata->pname, kdata->pinst, kdata->prealm,
64355260Sandrew hostname, locuser, cmdbuf);
64439793Sbostic else
64539793Sbostic #endif
64655260Sandrew syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
64755260Sandrew remuser, hostname, locuser, cmdbuf);
64839793Sbostic }
6496454Swnj execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
6506454Swnj perror(pwd->pw_shell);
6516454Swnj exit(1);
6526454Swnj }
6536454Swnj
65439122Skarels /*
65555260Sandrew * Report error to client. Note: can't be used until second socket has
65655260Sandrew * connected to client, or older clients will hang waiting for that
65755260Sandrew * connection first.
65839122Skarels */
65955260Sandrew #if __STDC__
66055260Sandrew #include <stdarg.h>
66155260Sandrew #else
66255260Sandrew #include <varargs.h>
66355260Sandrew #endif
66455260Sandrew
66555260Sandrew void
66655260Sandrew #if __STDC__
error(const char * fmt,...)66755260Sandrew error(const char *fmt, ...)
66855260Sandrew #else
66955260Sandrew error(fmt, va_alist)
6706454Swnj char *fmt;
67155260Sandrew va_dcl
67255260Sandrew #endif
6736454Swnj {
67455260Sandrew va_list ap;
67555260Sandrew int len;
67655260Sandrew char *bp, buf[BUFSIZ];
67755260Sandrew #if __STDC__
67855260Sandrew va_start(ap, fmt);
67955260Sandrew #else
68055260Sandrew va_start(ap);
68155260Sandrew #endif
68255260Sandrew bp = buf;
68355260Sandrew if (sent_null == 0) {
68439122Skarels *bp++ = 1;
68555260Sandrew len = 1;
68655260Sandrew } else
68755260Sandrew len = 0;
68856592Sbostic (void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap);
68956592Sbostic (void)write(STDERR_FILENO, buf, len + strlen(bp));
6906454Swnj }
6916454Swnj
69255260Sandrew void
getstr(buf,cnt,err)6936454Swnj getstr(buf, cnt, err)
69455260Sandrew char *buf, *err;
6956454Swnj int cnt;
6966454Swnj {
6976454Swnj char c;
6986454Swnj
6996454Swnj do {
70055770Sbostic if (read(STDIN_FILENO, &c, 1) != 1)
7016454Swnj exit(1);
7026454Swnj *buf++ = c;
7036454Swnj if (--cnt == 0) {
7046454Swnj error("%s too long\n", err);
7056454Swnj exit(1);
7066454Swnj }
7076454Swnj } while (c != 0);
7086454Swnj }
70936611Skfall
71036629Skarels /*
71136629Skarels * Check whether host h is in our local domain,
71239122Skarels * defined as sharing the last two components of the domain part,
71339122Skarels * or the entire domain part if the local domain has only one component.
71436629Skarels * If either name is unqualified (contains no '.'),
71536629Skarels * assume that the host is local, as it will be
71636629Skarels * interpreted as such.
71736629Skarels */
71855260Sandrew int
local_domain(h)71936629Skarels local_domain(h)
72036629Skarels char *h;
72136611Skfall {
72236629Skarels char localhost[MAXHOSTNAMELEN];
72355260Sandrew char *p1, *p2;
72436629Skarels
72539122Skarels localhost[0] = 0;
72636629Skarels (void) gethostname(localhost, sizeof(localhost));
72739122Skarels p1 = topdomain(localhost);
72839122Skarels p2 = topdomain(h);
72936629Skarels if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
73055260Sandrew return (1);
73155260Sandrew return (0);
73236611Skfall }
73336712Skfall
73439122Skarels char *
topdomain(h)73539122Skarels topdomain(h)
73639122Skarels char *h;
73739122Skarels {
738*66728Spendry char *p, *maybe = NULL;
73939122Skarels int dots = 0;
74039122Skarels
74139122Skarels for (p = h + strlen(h); p >= h; p--) {
74239122Skarels if (*p == '.') {
74339122Skarels if (++dots == 2)
74439122Skarels return (p);
74539122Skarels maybe = p;
74639122Skarels }
74739122Skarels }
74839122Skarels return (maybe);
74939122Skarels }
75039793Sbostic
75155260Sandrew void
usage()75239793Sbostic usage()
75339793Sbostic {
754*66728Spendry
75543250Sbostic syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
756*66728Spendry exit(2);
75739793Sbostic }
758