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