rshd.c revision 41860
1/*-
2 * Copyright (c) 1988, 1989, 1992, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static const char sccsid[] = "@(#)rshd.c	8.2 (Berkeley) 4/6/94";
43#endif
44static const char rcsid[] =
45	"$Id: rshd.c,v 1.22 1998/12/01 23:27:24 dg Exp $";
46#endif /* not lint */
47
48/*
49 * remote shell server:
50 *	[port]\0
51 *	remuser\0
52 *	locuser\0
53 *	command\0
54 *	data
55 */
56#include <sys/param.h>
57#include <sys/ioctl.h>
58#include <sys/time.h>
59#include <sys/socket.h>
60
61#include <netinet/in_systm.h>
62#include <netinet/in.h>
63#include <netinet/ip.h>
64#include <netinet/tcp.h>
65#include <arpa/inet.h>
66#include <netdb.h>
67
68#include <errno.h>
69#include <fcntl.h>
70#include <paths.h>
71#include <pwd.h>
72#include <signal.h>
73#include <stdio.h>
74#include <stdlib.h>
75#include <string.h>
76#include <syslog.h>
77#include <unistd.h>
78#ifdef	LOGIN_CAP
79#include <login_cap.h>
80#endif
81
82int	keepalive = 1;
83int	log_success;		/* If TRUE, log all successful accesses */
84int	sent_null;
85int	no_delay;
86
87void	 doit __P((struct sockaddr_in *));
88void	 error __P((const char *, ...));
89void	 getstr __P((char *, int, char *));
90int	 local_domain __P((char *));
91char	*topdomain __P((char *));
92void	 usage __P((void));
93
94#ifdef	KERBEROS
95#include <des.h>
96#include <krb.h>
97#define	VERSION_SIZE	9
98#define SECURE_MESSAGE  "This rsh session is using DES encryption for all transmissions.\r\n"
99#define	OPTIONS		"alnkvxDL"
100char	authbuf[sizeof(AUTH_DAT)];
101char	tickbuf[sizeof(KTEXT_ST)];
102int	doencrypt, use_kerberos, vacuous;
103Key_schedule	schedule;
104#else
105#define	OPTIONS	"alnDL"
106#endif
107
108int
109main(argc, argv)
110	int argc;
111	char *argv[];
112{
113	extern int __check_rhosts_file;
114	struct linger linger;
115	int ch, on = 1, fromlen;
116	struct sockaddr_in from;
117
118	openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
119
120	opterr = 0;
121	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
122		switch (ch) {
123		case 'a':
124			/* ignored for compatability */
125			break;
126		case 'l':
127			__check_rhosts_file = 0;
128			break;
129		case 'n':
130			keepalive = 0;
131			break;
132#ifdef	KERBEROS
133		case 'k':
134			use_kerberos = 1;
135			break;
136
137		case 'v':
138			vacuous = 1;
139			break;
140
141#ifdef CRYPT
142		case 'x':
143			doencrypt = 1;
144			break;
145#endif
146#endif
147		case 'D':
148			no_delay = 1;
149			break;
150		case 'L':
151			log_success = 1;
152			break;
153		case '?':
154		default:
155			usage();
156			break;
157		}
158
159	argc -= optind;
160	argv += optind;
161
162#ifdef	KERBEROS
163	if (use_kerberos && vacuous) {
164		syslog(LOG_ERR, "only one of -k and -v allowed");
165		exit(2);
166	}
167#ifdef CRYPT
168	if (doencrypt && !use_kerberos) {
169		syslog(LOG_ERR, "-k is required for -x");
170		exit(2);
171	}
172#endif
173#endif
174
175	fromlen = sizeof (from);
176	if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
177		syslog(LOG_ERR, "getpeername: %m");
178		exit(1);
179	}
180	if (keepalive &&
181	    setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
182	    sizeof(on)) < 0)
183		syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
184	linger.l_onoff = 1;
185	linger.l_linger = 60;			/* XXX */
186	if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
187	    sizeof (linger)) < 0)
188		syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
189	if (no_delay &&
190	    setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
191		syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
192	doit(&from);
193	/* NOTREACHED */
194	return(0);
195}
196
197char	username[20] = "USER=";
198char	homedir[64] = "HOME=";
199char	shell[64] = "SHELL=";
200char	path[100] = "PATH=";
201char	*envinit[] =
202	    {homedir, shell, path, username, 0};
203char	**environ;
204
205void
206doit(fromp)
207	struct sockaddr_in *fromp;
208{
209	extern char *__rcmd_errstr;	/* syslog hook from libc/net/rcmd.c. */
210	struct hostent *hp;
211	struct passwd *pwd;
212	u_short port;
213	fd_set ready, readfrom;
214	int cc, nfd, pv[2], pid, s;
215	int one = 1;
216	char *hostname, *errorstr;
217	char *cp, sig, buf[BUFSIZ];
218	char cmdbuf[NCARGS+1], locuser[16], remuser[16];
219	char fromhost[2 * MAXHOSTNAMELEN + 1];
220#ifdef	LOGIN_CAP
221	login_cap_t *lc;
222#endif
223
224#ifdef	KERBEROS
225	AUTH_DAT	*kdata = (AUTH_DAT *) NULL;
226	KTEXT		ticket = (KTEXT) NULL;
227	char		instance[INST_SZ], version[VERSION_SIZE];
228	struct		sockaddr_in	fromaddr;
229	int		rc;
230	long		authopts;
231	int		pv1[2], pv2[2];
232	fd_set		wready, writeto;
233
234	fromaddr = *fromp;
235#endif
236
237	(void) signal(SIGINT, SIG_DFL);
238	(void) signal(SIGQUIT, SIG_DFL);
239	(void) signal(SIGTERM, SIG_DFL);
240#ifdef DEBUG
241	{ int t = open(_PATH_TTY, 2);
242	  if (t >= 0) {
243		ioctl(t, TIOCNOTTY, (char *)0);
244		(void) close(t);
245	  }
246	}
247#endif
248	fromp->sin_port = ntohs((u_short)fromp->sin_port);
249	if (fromp->sin_family != AF_INET) {
250		syslog(LOG_ERR, "malformed \"from\" address (af %d)",
251		    fromp->sin_family);
252		exit(1);
253	}
254#ifdef IP_OPTIONS
255      {
256	u_char optbuf[BUFSIZ/3];
257	int optsize = sizeof(optbuf), ipproto, i;
258	struct protoent *ip;
259
260	if ((ip = getprotobyname("ip")) != NULL)
261		ipproto = ip->p_proto;
262	else
263		ipproto = IPPROTO_IP;
264	if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
265	    optsize != 0) {
266		for (i = 0; i < optsize; ) {
267			u_char c = optbuf[i];
268			if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
269				syslog(LOG_NOTICE,
270					"connection refused from %s with IP option %s",
271					inet_ntoa(fromp->sin_addr),
272					c == IPOPT_LSRR ? "LSRR" : "SSRR");
273				exit(1);
274			}
275			if (c == IPOPT_EOL)
276				break;
277			i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
278		}
279	}
280      }
281#endif
282
283#ifdef	KERBEROS
284	if (!use_kerberos)
285#endif
286		if (fromp->sin_port >= IPPORT_RESERVED ||
287		    fromp->sin_port < IPPORT_RESERVED/2) {
288			syslog(LOG_NOTICE|LOG_AUTH,
289			    "connection from %s on illegal port %u",
290			    inet_ntoa(fromp->sin_addr),
291			    fromp->sin_port);
292			exit(1);
293		}
294
295	(void) alarm(60);
296	port = 0;
297	s = 0;		/* not set or used if port == 0 */
298	for (;;) {
299		char c;
300		if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
301			if (cc < 0)
302				syslog(LOG_NOTICE, "read: %m");
303			shutdown(0, 1+1);
304			exit(1);
305		}
306		if (c == 0)
307			break;
308		port = port * 10 + c - '0';
309	}
310
311	(void) alarm(0);
312	if (port != 0) {
313		int lport = IPPORT_RESERVED - 1;
314		s = rresvport(&lport);
315		if (s < 0) {
316			syslog(LOG_ERR, "can't get stderr port: %m");
317			exit(1);
318		}
319#ifdef	KERBEROS
320		if (!use_kerberos)
321#endif
322			if (port >= IPPORT_RESERVED ||
323			    port < IPPORT_RESERVED/2) {
324				syslog(LOG_NOTICE|LOG_AUTH,
325				    "2nd socket from %s on unreserved port %u",
326				    inet_ntoa(fromp->sin_addr),
327				    port);
328				exit(1);
329			}
330		fromp->sin_port = htons(port);
331		if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) {
332			syslog(LOG_INFO, "connect second port %d: %m", port);
333			exit(1);
334		}
335	}
336
337#ifdef	KERBEROS
338	if (vacuous) {
339		error("rshd: remote host requires Kerberos authentication\n");
340		exit(1);
341	}
342#endif
343
344#ifdef notdef
345	/* from inetd, socket is already on 0, 1, 2 */
346	dup2(f, 0);
347	dup2(f, 1);
348	dup2(f, 2);
349#endif
350	errorstr = NULL;
351	strncpy(fromhost, inet_ntoa(fromp->sin_addr),
352		sizeof(fromhost) - 1);
353	hostname = fromhost;
354	hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (struct in_addr),
355		fromp->sin_family);
356	if (hp) {
357		/*
358		 * OK, it looks like a DNS name is attached.. Lets see if
359		 * it looks like we can use it.  If it doesn't check out,
360		 * ditch it and use the IP address for logging instead.
361		 * Note that iruserok() does it's own hostname checking!!
362		 */
363		strncpy(fromhost, hp->h_name, sizeof(fromhost) - 1);
364		fromhost[sizeof(fromhost) - 1] = 0;
365		hp = gethostbyname(fromhost);
366		if (hp == NULL) {
367			strncpy(fromhost, inet_ntoa(fromp->sin_addr),
368				sizeof(fromhost) - 1);
369		} else for (; ; hp->h_addr_list++) {
370			if (hp->h_addr_list[0] == NULL) {
371				/* End of list - ditch it */
372				strncpy(fromhost, inet_ntoa(fromp->sin_addr),
373					sizeof(fromhost) - 1);
374				break;
375			}
376			if (!bcmp(hp->h_addr_list[0],
377			    (caddr_t)&fromp->sin_addr,
378			    sizeof(fromp->sin_addr)))
379				break;		/* OK! */
380		}
381	}
382	fromhost[sizeof(fromhost) - 1] = 0;
383
384#ifdef	KERBEROS
385	if (use_kerberos) {
386		kdata = (AUTH_DAT *) authbuf;
387		ticket = (KTEXT) tickbuf;
388		authopts = 0L;
389		strcpy(instance, "*");
390		version[VERSION_SIZE - 1] = '\0';
391#ifdef CRYPT
392		if (doencrypt) {
393			struct sockaddr_in local_addr;
394			rc = sizeof(local_addr);
395			if (getsockname(0, (struct sockaddr *)&local_addr,
396			    &rc) < 0) {
397				syslog(LOG_ERR, "getsockname: %m");
398				error("rlogind: getsockname: %m");
399				exit(1);
400			}
401			authopts = KOPT_DO_MUTUAL;
402			rc = krb_recvauth(authopts, 0, ticket,
403				"rcmd", instance, &fromaddr,
404				&local_addr, kdata, "", schedule,
405				version);
406			des_set_key(&kdata->session, schedule);
407		} else
408#endif
409			rc = krb_recvauth(authopts, 0, ticket, "rcmd",
410				instance, &fromaddr,
411				(struct sockaddr_in *) 0,
412				kdata, "", NULL, version);
413		if (rc != KSUCCESS) {
414			error("Kerberos authentication failure: %s\n",
415				  krb_err_txt[rc]);
416			exit(1);
417		}
418	} else
419#endif
420		getstr(remuser, sizeof(remuser), "remuser");
421
422	getstr(locuser, sizeof(locuser), "locuser");
423	getstr(cmdbuf, sizeof(cmdbuf), "command");
424	setpwent();
425	pwd = getpwnam(locuser);
426	if (pwd == NULL) {
427		syslog(LOG_INFO|LOG_AUTH,
428		    "%s@%s as %s: unknown login. cmd='%.80s'",
429		    remuser, hostname, locuser, cmdbuf);
430		if (errorstr == NULL)
431			errorstr = "Login incorrect.\n";
432		goto fail;
433	}
434#ifdef	LOGIN_CAP
435	lc = login_getpwclass(pwd);
436#endif
437	if (chdir(pwd->pw_dir) < 0) {
438#ifdef	LOGIN_CAP
439		if (chdir("/") < 0 ||
440		    login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
441			syslog(LOG_INFO|LOG_AUTH,
442			"%s@%s as %s: no home directory. cmd='%.80s'",
443			remuser, hostname, locuser, cmdbuf);
444			error("No remote home directory.\n");
445			exit(0);
446		}
447#else
448		(void) chdir("/");
449#ifdef notdef
450		syslog(LOG_INFO|LOG_AUTH,
451		    "%s@%s as %s: no home directory. cmd='%.80s'",
452		    remuser, hostname, locuser, cmdbuf);
453		error("No remote directory.\n");
454		exit(1);
455#endif
456#endif
457		pwd->pw_dir = "/";
458	}
459
460#ifdef	KERBEROS
461	if (use_kerberos) {
462		if (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0') {
463			if (kuserok(kdata, locuser) != 0) {
464				syslog(LOG_INFO|LOG_AUTH,
465				    "Kerberos rsh denied to %s.%s@%s",
466				    kdata->pname, kdata->pinst, kdata->prealm);
467				error("Login incorrect.\n");
468				exit(1);
469			}
470		}
471	} else
472#endif
473
474		if (errorstr ||
475		    (pwd->pw_expire && time(NULL) >= pwd->pw_expire) ||
476		    (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
477		    iruserok(fromp->sin_addr.s_addr, pwd->pw_uid == 0,
478		    remuser, locuser) < 0)) {
479			if (__rcmd_errstr)
480				syslog(LOG_INFO|LOG_AUTH,
481			    "%s@%s as %s: permission denied (%s). cmd='%.80s'",
482				    remuser, hostname, locuser, __rcmd_errstr,
483				    cmdbuf);
484			else
485				syslog(LOG_INFO|LOG_AUTH,
486			    "%s@%s as %s: permission denied. cmd='%.80s'",
487				    remuser, hostname, locuser, cmdbuf);
488fail:
489			if (errorstr == NULL)
490				errorstr = "Login incorrect.\n";
491			error(errorstr, hostname);
492			exit(1);
493		}
494
495	if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
496		error("Logins currently disabled.\n");
497		exit(1);
498	}
499#ifdef	LOGIN_CAP
500	if (lc != NULL) {
501		char	remote_ip[MAXHOSTNAMELEN];
502
503		strncpy(remote_ip, inet_ntoa(fromp->sin_addr),
504			sizeof(remote_ip) - 1);
505		remote_ip[sizeof(remote_ip) - 1] = 0;
506		if (!auth_hostok(lc, fromhost, remote_ip)) {
507			syslog(LOG_INFO|LOG_AUTH,
508			    "%s@%s as %s: permission denied (%s). cmd='%.80s'",
509			    remuser, hostname, locuser, __rcmd_errstr,
510			    cmdbuf);
511			error("Login incorrect.\n");
512			exit(1);
513		}
514		if (!auth_timeok(lc, time(NULL))) {
515			error("Logins not available right now\n");
516			exit(1);
517		}
518	}
519#endif	/* !LOGIN_CAP */
520#if	BSD > 43
521	/* before fork, while we're session leader */
522	if (setlogin(pwd->pw_name) < 0)
523		syslog(LOG_ERR, "setlogin() failed: %m");
524#endif
525
526	(void) write(STDERR_FILENO, "\0", 1);
527	sent_null = 1;
528
529	if (port) {
530		if (pipe(pv) < 0) {
531			error("Can't make pipe.\n");
532			exit(1);
533		}
534#ifdef CRYPT
535#ifdef KERBEROS
536		if (doencrypt) {
537			if (pipe(pv1) < 0) {
538				error("Can't make 2nd pipe.\n");
539				exit(1);
540			}
541			if (pipe(pv2) < 0) {
542				error("Can't make 3rd pipe.\n");
543				exit(1);
544			}
545		}
546#endif
547#endif
548		pid = fork();
549		if (pid == -1)  {
550			error("Can't fork; try again.\n");
551			exit(1);
552		}
553		if (pid) {
554#ifdef CRYPT
555#ifdef KERBEROS
556			if (doencrypt) {
557				static char msg[] = SECURE_MESSAGE;
558				(void) close(pv1[1]);
559				(void) close(pv2[1]);
560				des_enc_write(s, msg, sizeof(msg) - 1,
561					schedule, &kdata->session);
562
563			} else
564#endif
565#endif
566			{
567				(void) close(0);
568				(void) close(1);
569			}
570			(void) close(2);
571			(void) close(pv[1]);
572
573			FD_ZERO(&readfrom);
574			FD_SET(s, &readfrom);
575			FD_SET(pv[0], &readfrom);
576			if (pv[0] > s)
577				nfd = pv[0];
578			else
579				nfd = s;
580#ifdef CRYPT
581#ifdef KERBEROS
582			if (doencrypt) {
583				FD_ZERO(&writeto);
584				FD_SET(pv2[0], &writeto);
585				FD_SET(pv1[0], &readfrom);
586
587				nfd = MAX(nfd, pv2[0]);
588				nfd = MAX(nfd, pv1[0]);
589			} else
590#endif
591#endif
592				ioctl(pv[0], FIONBIO, (char *)&one);
593
594			/* should set s nbio! */
595			nfd++;
596			do {
597				ready = readfrom;
598#ifdef CRYPT
599#ifdef KERBEROS
600				if (doencrypt) {
601					wready = writeto;
602					if (select(nfd, &ready,
603					    &wready, (fd_set *) 0,
604					    (struct timeval *) 0) < 0)
605						break;
606				} else
607#endif
608#endif
609					if (select(nfd, &ready, (fd_set *)0,
610					  (fd_set *)0, (struct timeval *)0) < 0)
611						break;
612				if (FD_ISSET(s, &ready)) {
613					int	ret;
614#ifdef CRYPT
615#ifdef KERBEROS
616					if (doencrypt)
617						ret = des_enc_read(s, &sig, 1,
618						schedule, &kdata->session);
619					else
620#endif
621#endif
622						ret = read(s, &sig, 1);
623					if (ret <= 0)
624						FD_CLR(s, &readfrom);
625					else
626						killpg(pid, sig);
627				}
628				if (FD_ISSET(pv[0], &ready)) {
629					errno = 0;
630					cc = read(pv[0], buf, sizeof(buf));
631					if (cc <= 0) {
632						shutdown(s, 1+1);
633						FD_CLR(pv[0], &readfrom);
634					} else {
635#ifdef CRYPT
636#ifdef KERBEROS
637						if (doencrypt)
638							(void)
639							  des_enc_write(s, buf, cc,
640								schedule, &kdata->session);
641						else
642#endif
643#endif
644							(void)
645							  write(s, buf, cc);
646					}
647				}
648#ifdef CRYPT
649#ifdef KERBEROS
650				if (doencrypt && FD_ISSET(pv1[0], &ready)) {
651					errno = 0;
652					cc = read(pv1[0], buf, sizeof(buf));
653					if (cc <= 0) {
654						shutdown(pv1[0], 1+1);
655						FD_CLR(pv1[0], &readfrom);
656					} else
657						(void) des_enc_write(STDOUT_FILENO,
658						    buf, cc,
659							schedule, &kdata->session);
660				}
661
662				if (doencrypt && FD_ISSET(pv2[0], &wready)) {
663					errno = 0;
664					cc = des_enc_read(STDIN_FILENO,
665					    buf, sizeof(buf),
666						schedule, &kdata->session);
667					if (cc <= 0) {
668						shutdown(pv2[0], 1+1);
669						FD_CLR(pv2[0], &writeto);
670					} else
671						(void) write(pv2[0], buf, cc);
672				}
673#endif
674#endif
675
676			} while (FD_ISSET(s, &readfrom) ||
677#ifdef CRYPT
678#ifdef KERBEROS
679			    (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
680#endif
681#endif
682			    FD_ISSET(pv[0], &readfrom));
683			exit(0);
684		}
685		setpgrp(0, getpid());
686		(void) close(s);
687		(void) close(pv[0]);
688#ifdef CRYPT
689#ifdef KERBEROS
690		if (doencrypt) {
691			close(pv1[0]); close(pv2[0]);
692			dup2(pv1[1], 1);
693			dup2(pv2[1], 0);
694			close(pv1[1]);
695			close(pv2[1]);
696		}
697#endif
698#endif
699		dup2(pv[1], 2);
700		close(pv[1]);
701	}
702	if (*pwd->pw_shell == '\0')
703		pwd->pw_shell = _PATH_BSHELL;
704	environ = envinit;
705	strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
706	strcat(path, _PATH_DEFPATH);
707	strncat(shell, pwd->pw_shell, sizeof(shell)-7);
708	strncat(username, pwd->pw_name, sizeof(username)-6);
709	cp = strrchr(pwd->pw_shell, '/');
710	if (cp)
711		cp++;
712	else
713		cp = pwd->pw_shell;
714#ifdef	LOGIN_CAP
715	if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
716                syslog(LOG_ERR, "setusercontext: %m");
717		exit(1);
718	}
719	login_close(lc);
720#else
721	(void) setgid((gid_t)pwd->pw_gid);
722	initgroups(pwd->pw_name, pwd->pw_gid);
723	(void) setuid((uid_t)pwd->pw_uid);
724#endif
725	endpwent();
726	if (log_success || pwd->pw_uid == 0) {
727#ifdef	KERBEROS
728		if (use_kerberos)
729		    syslog(LOG_INFO|LOG_AUTH,
730			"Kerberos shell from %s.%s@%s on %s as %s, cmd='%.80s'",
731			kdata->pname, kdata->pinst, kdata->prealm,
732			hostname, locuser, cmdbuf);
733		else
734#endif
735		    syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
736			remuser, hostname, locuser, cmdbuf);
737	}
738	execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
739	perror(pwd->pw_shell);
740	exit(1);
741}
742
743/*
744 * Report error to client.  Note: can't be used until second socket has
745 * connected to client, or older clients will hang waiting for that
746 * connection first.
747 */
748#if __STDC__
749#include <stdarg.h>
750#else
751#include <varargs.h>
752#endif
753
754void
755#if __STDC__
756error(const char *fmt, ...)
757#else
758error(fmt, va_alist)
759	char *fmt;
760        va_dcl
761#endif
762{
763	va_list ap;
764	int len;
765	char *bp, buf[BUFSIZ];
766#if __STDC__
767	va_start(ap, fmt);
768#else
769	va_start(ap);
770#endif
771	bp = buf;
772	if (sent_null == 0) {
773		*bp++ = 1;
774		len = 1;
775	} else
776		len = 0;
777	(void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap);
778	(void)write(STDERR_FILENO, buf, len + strlen(bp));
779}
780
781void
782getstr(buf, cnt, err)
783	char *buf, *err;
784	int cnt;
785{
786	char c;
787
788	do {
789		if (read(STDIN_FILENO, &c, 1) != 1)
790			exit(1);
791		*buf++ = c;
792		if (--cnt == 0) {
793			error("%s too long\n", err);
794			exit(1);
795		}
796	} while (c != 0);
797}
798
799/*
800 * Check whether host h is in our local domain,
801 * defined as sharing the last two components of the domain part,
802 * or the entire domain part if the local domain has only one component.
803 * If either name is unqualified (contains no '.'),
804 * assume that the host is local, as it will be
805 * interpreted as such.
806 */
807int
808local_domain(h)
809	char *h;
810{
811	char localhost[MAXHOSTNAMELEN];
812	char *p1, *p2;
813
814	localhost[0] = 0;
815	(void) gethostname(localhost, sizeof(localhost));
816	p1 = topdomain(localhost);
817	p2 = topdomain(h);
818	if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
819		return (1);
820	return (0);
821}
822
823char *
824topdomain(h)
825	char *h;
826{
827	char *p, *maybe = NULL;
828	int dots = 0;
829
830	for (p = h + strlen(h); p >= h; p--) {
831		if (*p == '.') {
832			if (++dots == 2)
833				return (p);
834			maybe = p;
835		}
836	}
837	return (maybe);
838}
839
840void
841usage()
842{
843
844	syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
845	exit(2);
846}
847