1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1998-1999,2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #define	BSD_COMP
30*0Sstevel@tonic-gate #include <errno.h>
31*0Sstevel@tonic-gate #include <fcntl.h>
32*0Sstevel@tonic-gate #include <pwd.h>
33*0Sstevel@tonic-gate #include <signal.h>
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <unistd.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include <sac.h>		/* for SC_WILDC */
39*0Sstevel@tonic-gate #include <utmpx.h>
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include <rpc/rpc.h>
42*0Sstevel@tonic-gate #include <sys/file.h>
43*0Sstevel@tonic-gate #include <sys/filio.h>
44*0Sstevel@tonic-gate #include <sys/ioctl.h>
45*0Sstevel@tonic-gate #include <sys/signal.h>
46*0Sstevel@tonic-gate #include <sys/stat.h>
47*0Sstevel@tonic-gate #include <sys/types.h>
48*0Sstevel@tonic-gate #include <sys/wait.h>
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /*
51*0Sstevel@tonic-gate  * # include <sys/label.h>
52*0Sstevel@tonic-gate  * # include <sys/audit.h>
53*0Sstevel@tonic-gate  *
54*0Sstevel@tonic-gate  *
55*0Sstevel@tonic-gate  *
56*0Sstevel@tonic-gate  * # include <pwdadj.h>
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <sys/ttold.h>
60*0Sstevel@tonic-gate #include <stropts.h>
61*0Sstevel@tonic-gate #include <sys/stream.h>
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate #include "rex.h"
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate #include <security/pam_appl.h>
68*0Sstevel@tonic-gate pam_handle_t *pamh;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate #define	NTTYDISC	2	/* New ttydiscipline: stolen from ttold.h */
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate  * unix_login - hairy junk to simulate logins for Unix
74*0Sstevel@tonic-gate  */
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate int	Master,	Slave;			/* sides of the pty */
77*0Sstevel@tonic-gate int	Slave_is_closed_on_master_side;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate static char	*slavename;
80*0Sstevel@tonic-gate extern char *ptsname();
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate int	InputSocket,			/* Network sockets */
84*0Sstevel@tonic-gate 	OutputSocket;
85*0Sstevel@tonic-gate int	Helper1,			/* pids of the helpers */
86*0Sstevel@tonic-gate 	Helper2;
87*0Sstevel@tonic-gate char	UserName[256];			/* saves the user name for loging */
88*0Sstevel@tonic-gate char	HostName[256];			/* saves the host name for loging */
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate static	int	TtySlot;		/* slot number in Utmpx */
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate /*
93*0Sstevel@tonic-gate  * pseudo-xprts used to add pty fds to svc_pollfd[]. This allows the
94*0Sstevel@tonic-gate  * polling for all i/o in one poll().
95*0Sstevel@tonic-gate  */
96*0Sstevel@tonic-gate SVCXPRT uxprt[2];
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate #define	INPUTSOCKET	0		/* InputSocket xprt */
99*0Sstevel@tonic-gate #define	MASTER		1		/* Master xprt */
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate extern	int child;		/* pid of the executed process */
103*0Sstevel@tonic-gate extern	int ChildDied;		/* flag */
104*0Sstevel@tonic-gate extern	int HasHelper;		/* flag */
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate extern	void setproctitle(char *user, char *host);
107*0Sstevel@tonic-gate extern int Debug;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate extern void audit_rexd_fail(char *, char *, char *, uid_t, gid_t,
110*0Sstevel@tonic-gate 				char *, char **);
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate #define	bzero(s, n)	memset((s), 0, (n))
113*0Sstevel@tonic-gate #define	bcopy(a, b, c)	memcpy((b), (a), (c))
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate /*
117*0Sstevel@tonic-gate  * Check for user being able to run on this machine.
118*0Sstevel@tonic-gate  * returns 0 if OK, TRUE if problem, error message in "error"
119*0Sstevel@tonic-gate  * copies name of shell and home directory if user is valid.
120*0Sstevel@tonic-gate  */
121*0Sstevel@tonic-gate int
122*0Sstevel@tonic-gate ValidUser(host, uid, gid, error, shell, dir, rst)
123*0Sstevel@tonic-gate 	char *host;		/* passed in */
124*0Sstevel@tonic-gate 	uid_t uid;
125*0Sstevel@tonic-gate 	gid_t gid;
126*0Sstevel@tonic-gate 	char *error;		/* filled in on return */
127*0Sstevel@tonic-gate 	char *shell;		/* filled in on return */
128*0Sstevel@tonic-gate 	char *dir;		/* filled in on return */
129*0Sstevel@tonic-gate 	struct rex_start *rst;	/* passed in */
130*0Sstevel@tonic-gate {
131*0Sstevel@tonic-gate 	struct passwd *pw, *getpwuid();
132*0Sstevel@tonic-gate 	int v;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	pw = getpwuid(uid);
135*0Sstevel@tonic-gate 	if (pw == NULL || pw->pw_name == NULL)
136*0Sstevel@tonic-gate 	{
137*0Sstevel@tonic-gate 		errprintf(error, "rexd: User id %d not valid\n", uid);
138*0Sstevel@tonic-gate 		audit_rexd_fail("user id is not valid",
139*0Sstevel@tonic-gate 				host,
140*0Sstevel@tonic-gate 				NULL,
141*0Sstevel@tonic-gate 				uid,
142*0Sstevel@tonic-gate 				gid,
143*0Sstevel@tonic-gate 				NULL,
144*0Sstevel@tonic-gate 				rst->rst_cmd);	    /* BSM */
145*0Sstevel@tonic-gate 		return (1);
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 	strncpy(UserName, pw->pw_name, sizeof (UserName) - 1);
148*0Sstevel@tonic-gate 	strncpy(HostName, host, sizeof (HostName) - 1);
149*0Sstevel@tonic-gate 	strcpy(shell, pw->pw_shell);
150*0Sstevel@tonic-gate 	strcpy(dir, pw->pw_dir);
151*0Sstevel@tonic-gate 	setproctitle(pw->pw_name, host);
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	if (pam_start("rexd", pw->pw_name, NULL, &pamh) != PAM_SUCCESS ||
154*0Sstevel@tonic-gate 	    pam_set_item(pamh, PAM_RHOST, host) != PAM_SUCCESS) {
155*0Sstevel@tonic-gate 		audit_rexd_fail("user id is not valid",
156*0Sstevel@tonic-gate 				host,
157*0Sstevel@tonic-gate 				pw->pw_name,
158*0Sstevel@tonic-gate 				uid,
159*0Sstevel@tonic-gate 				gid,
160*0Sstevel@tonic-gate 				shell,
161*0Sstevel@tonic-gate 				rst->rst_cmd);	    /* BSM */
162*0Sstevel@tonic-gate 		errprintf(error, "rexd: User id %d not valid\n", uid);
163*0Sstevel@tonic-gate 		if (pamh) {
164*0Sstevel@tonic-gate 			pam_end(pamh, PAM_ABORT);
165*0Sstevel@tonic-gate 			pamh = NULL;
166*0Sstevel@tonic-gate 		}
167*0Sstevel@tonic-gate 		return (1);
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	if ((v = pam_acct_mgmt(pamh, 0)) != PAM_SUCCESS) {
171*0Sstevel@tonic-gate 		switch (v) {
172*0Sstevel@tonic-gate 		case PAM_NEW_AUTHTOK_REQD:
173*0Sstevel@tonic-gate 			errprintf(error,
174*0Sstevel@tonic-gate 				"rexd: User id %d Password Expired\n", uid);
175*0Sstevel@tonic-gate 			break;
176*0Sstevel@tonic-gate 		case PAM_PERM_DENIED:
177*0Sstevel@tonic-gate 			errprintf(error,
178*0Sstevel@tonic-gate 				"rexd: User id %d Account Expired\n", uid);
179*0Sstevel@tonic-gate 			break;
180*0Sstevel@tonic-gate 		case PAM_AUTHTOK_EXPIRED:
181*0Sstevel@tonic-gate 			errprintf(error,
182*0Sstevel@tonic-gate 				"rexd: User id %d Password Expired\n", uid);
183*0Sstevel@tonic-gate 			break;
184*0Sstevel@tonic-gate 		default:
185*0Sstevel@tonic-gate 			errprintf(error,
186*0Sstevel@tonic-gate 				"rexd: User id %d not valid\n", uid);
187*0Sstevel@tonic-gate 			break;
188*0Sstevel@tonic-gate 		}
189*0Sstevel@tonic-gate 		pam_end(pamh, PAM_ABORT);
190*0Sstevel@tonic-gate 		pamh = NULL;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 		audit_rexd_fail("user account expired",
193*0Sstevel@tonic-gate 				host,
194*0Sstevel@tonic-gate 				pw->pw_name,
195*0Sstevel@tonic-gate 				uid,
196*0Sstevel@tonic-gate 				gid,
197*0Sstevel@tonic-gate 				shell,
198*0Sstevel@tonic-gate 				rst->rst_cmd);	    /* BSM */
199*0Sstevel@tonic-gate 		return (1);
200*0Sstevel@tonic-gate 	}
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	return (0);
203*0Sstevel@tonic-gate }
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate /*
206*0Sstevel@tonic-gate  * Add an audit record with argv that was pre-set, plus the given string
207*0Sstevel@tonic-gate  */
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate /*
210*0Sstevel@tonic-gate  * Allocate a pseudo-terminal
211*0Sstevel@tonic-gate  * sets the global variables Master and Slave.
212*0Sstevel@tonic-gate  * returns 1 on error, 0 if OK
213*0Sstevel@tonic-gate  */
214*0Sstevel@tonic-gate int
215*0Sstevel@tonic-gate AllocatePty(socket0, socket1)
216*0Sstevel@tonic-gate 	int socket0, socket1;
217*0Sstevel@tonic-gate {
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	int on = 1;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	sigset(SIGHUP, SIG_IGN);
222*0Sstevel@tonic-gate 	sigset(SIGTTOU, SIG_IGN);
223*0Sstevel@tonic-gate 	sigset(SIGTTIN, SIG_IGN);
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	if ((Master = open("/dev/ptmx", O_RDWR)) == -1) {
226*0Sstevel@tonic-gate 	    if (Debug)
227*0Sstevel@tonic-gate 		    printf("open-ptmx-failure\n");
228*0Sstevel@tonic-gate 	    perror("AloocatePtyMaster fails");
229*0Sstevel@tonic-gate 	    return (1);		/* error could not open /dev/ptmx */
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate 	if (Debug)
232*0Sstevel@tonic-gate 	    printf("open-ptmx success Master =%d\n", Master);
233*0Sstevel@tonic-gate 	if (Debug)
234*0Sstevel@tonic-gate 	    printf("Before grantpt...Master=%d\n", Master);
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	if (grantpt(Master) == -1) {
237*0Sstevel@tonic-gate 	    perror("could not grant slave pty");
238*0Sstevel@tonic-gate 	    exit(1);
239*0Sstevel@tonic-gate 	}
240*0Sstevel@tonic-gate 	if (unlockpt(Master) == -1) {
241*0Sstevel@tonic-gate 	    perror("could not unlock slave pty");
242*0Sstevel@tonic-gate 	    exit(1);
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate 	if ((slavename = ptsname(Master)) == NULL) {
245*0Sstevel@tonic-gate 	    perror("could not enable slave pty");
246*0Sstevel@tonic-gate 	    exit(1);
247*0Sstevel@tonic-gate 	}
248*0Sstevel@tonic-gate 	if ((Slave = open(slavename, O_RDWR)) == -1) {
249*0Sstevel@tonic-gate 	    perror("could not open slave pty");
250*0Sstevel@tonic-gate 	    exit(1);
251*0Sstevel@tonic-gate 	}
252*0Sstevel@tonic-gate 	if (ioctl(Slave, I_PUSH, "ptem") == -1) {
253*0Sstevel@tonic-gate 	    perror("ioctl I_PUSH ptem");
254*0Sstevel@tonic-gate 	    exit(1);
255*0Sstevel@tonic-gate 	}
256*0Sstevel@tonic-gate 	if (ioctl(Slave, I_PUSH, "ldterm") == -1) {
257*0Sstevel@tonic-gate 	    perror("ioctl I_PUSH ldterm");
258*0Sstevel@tonic-gate 	    exit(1);
259*0Sstevel@tonic-gate 	}
260*0Sstevel@tonic-gate 	if (ioctl(Slave, I_PUSH, "ttcompat") == -1) {
261*0Sstevel@tonic-gate 	    perror("ioctl I_PUSH ttcompat");
262*0Sstevel@tonic-gate 	    exit(1);
263*0Sstevel@tonic-gate 	}
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	Slave_is_closed_on_master_side = FALSE;
266*0Sstevel@tonic-gate 	setsid(); /* get rid of controlling terminal */
267*0Sstevel@tonic-gate 	/* LoginUser(); */
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	InputSocket = socket0;
270*0Sstevel@tonic-gate 	OutputSocket = socket1;
271*0Sstevel@tonic-gate 	ioctl(Master, FIONBIO, &on);
272*0Sstevel@tonic-gate 	uxprt[INPUTSOCKET].xp_fd = InputSocket;
273*0Sstevel@tonic-gate 	xprt_register(&uxprt[INPUTSOCKET]);
274*0Sstevel@tonic-gate 	uxprt[MASTER].xp_fd = Master;
275*0Sstevel@tonic-gate 	xprt_register(&uxprt[MASTER]);
276*0Sstevel@tonic-gate 	return (0);
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate }
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate void
281*0Sstevel@tonic-gate OpenPtySlave()
282*0Sstevel@tonic-gate {
283*0Sstevel@tonic-gate 	close(Slave);
284*0Sstevel@tonic-gate 	Slave = open(slavename, O_RDWR);
285*0Sstevel@tonic-gate 	if (Slave < 0) {
286*0Sstevel@tonic-gate 		perror(slavename);
287*0Sstevel@tonic-gate 		exit(1);
288*0Sstevel@tonic-gate 	}
289*0Sstevel@tonic-gate }
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	/*
294*0Sstevel@tonic-gate 	 * Special processing for interactive operation.
295*0Sstevel@tonic-gate 	 * Given pointers to three standard file descriptors,
296*0Sstevel@tonic-gate 	 * which get set to point to the pty.
297*0Sstevel@tonic-gate 	 */
298*0Sstevel@tonic-gate void
299*0Sstevel@tonic-gate DoHelper(pfd0, pfd1, pfd2)
300*0Sstevel@tonic-gate 	int *pfd0, *pfd1, *pfd2;
301*0Sstevel@tonic-gate {
302*0Sstevel@tonic-gate 	int pgrp;
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	sigset(SIGINT, SIG_IGN);
306*0Sstevel@tonic-gate 	close(Master);
307*0Sstevel@tonic-gate 	close(InputSocket);
308*0Sstevel@tonic-gate 	close(OutputSocket);
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	*pfd0 = Slave;
311*0Sstevel@tonic-gate 	*pfd1 = Slave;
312*0Sstevel@tonic-gate 	*pfd2 = Slave;
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate /*
317*0Sstevel@tonic-gate  * destroy the helpers when the executing process dies
318*0Sstevel@tonic-gate  */
319*0Sstevel@tonic-gate KillHelper(grp)
320*0Sstevel@tonic-gate 	int grp;
321*0Sstevel@tonic-gate {
322*0Sstevel@tonic-gate 	if (Debug)
323*0Sstevel@tonic-gate 		printf("Enter KillHelper\n");
324*0Sstevel@tonic-gate 	close(Master);
325*0Sstevel@tonic-gate 	xprt_unregister(&uxprt[MASTER]);
326*0Sstevel@tonic-gate 	close(InputSocket);
327*0Sstevel@tonic-gate 	xprt_unregister(&uxprt[INPUTSOCKET]);
328*0Sstevel@tonic-gate 	close(OutputSocket);
329*0Sstevel@tonic-gate 	LogoutUser();
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	if (grp)
332*0Sstevel@tonic-gate 	    kill((-grp), SIGKILL);
333*0Sstevel@tonic-gate }
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate /*
337*0Sstevel@tonic-gate  * edit the Unix traditional data files that tell who is logged
338*0Sstevel@tonic-gate  * into "the system"
339*0Sstevel@tonic-gate  */
340*0Sstevel@tonic-gate unsigned char	utid[] = {'o', 'n', SC_WILDC, SC_WILDC};
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate LoginUser()
343*0Sstevel@tonic-gate {
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	char *user;
346*0Sstevel@tonic-gate 	char *rhost;
347*0Sstevel@tonic-gate 	/* the next 4 variables are needed for utmpx mgmt */
348*0Sstevel@tonic-gate 	int		tmplen;
349*0Sstevel@tonic-gate 	struct utmpx	*u = NULL;
350*0Sstevel@tonic-gate 	struct utmpx	set_utmp;
351*0Sstevel@tonic-gate 	char		*ttyntail;
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate 	/* We're pretty drastic here, exiting if an error is detected */
354*0Sstevel@tonic-gate 	if (pam_set_item(pamh, PAM_TTY, slavename)	!= PAM_SUCCESS ||
355*0Sstevel@tonic-gate 	    pam_get_item(pamh, PAM_USER, (void **) &user) != PAM_SUCCESS ||
356*0Sstevel@tonic-gate 	    pam_get_item(pamh, PAM_RHOST, (void **) &rhost) != PAM_SUCCESS ||
357*0Sstevel@tonic-gate 	    pam_open_session(pamh, 0)			!= PAM_SUCCESS) {
358*0Sstevel@tonic-gate 		/*
359*0Sstevel@tonic-gate 		 * XXX should print something but for now we exit
360*0Sstevel@tonic-gate 		 */
361*0Sstevel@tonic-gate 		exit(1);
362*0Sstevel@tonic-gate 	}
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 	(void) memset((void *)&set_utmp, 0, sizeof (set_utmp));
365*0Sstevel@tonic-gate 	(void) time(&set_utmp.ut_tv.tv_sec);
366*0Sstevel@tonic-gate 	set_utmp.ut_pid = getpid();
367*0Sstevel@tonic-gate 	if (rhost != NULL && rhost[0] != '\0') {
368*0Sstevel@tonic-gate 		(void) strcpy(set_utmp.ut_host, rhost);
369*0Sstevel@tonic-gate 		tmplen = strlen(rhost) + 1;
370*0Sstevel@tonic-gate 		if (tmplen < sizeof (set_utmp.ut_host))
371*0Sstevel@tonic-gate 			set_utmp.ut_syslen = tmplen;
372*0Sstevel@tonic-gate 		else
373*0Sstevel@tonic-gate 			set_utmp.ut_syslen = sizeof (set_utmp.ut_host);
374*0Sstevel@tonic-gate 	} else {
375*0Sstevel@tonic-gate 		(void) memset(set_utmp.ut_host, 0, sizeof (set_utmp.ut_host));
376*0Sstevel@tonic-gate 		set_utmp.ut_syslen = 0;
377*0Sstevel@tonic-gate 	}
378*0Sstevel@tonic-gate 	(void) strcpy(set_utmp.ut_user, user);
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	/*
381*0Sstevel@tonic-gate 	 * Copy in the name of the tty minus the "/dev/" if a /dev/ is
382*0Sstevel@tonic-gate 	 * in the path name.
383*0Sstevel@tonic-gate 	 */
384*0Sstevel@tonic-gate 	ttyntail = slavename;
385*0Sstevel@tonic-gate 	if (strstr(ttyntail, "/dev/") != 0)
386*0Sstevel@tonic-gate 		ttyntail = ttyntail + strlen("/dev/");
387*0Sstevel@tonic-gate 	(void) strcpy(set_utmp.ut_line, ttyntail);
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	set_utmp.ut_type = USER_PROCESS;
390*0Sstevel@tonic-gate 	if (utid != NULL)
391*0Sstevel@tonic-gate 		(void) memcpy(set_utmp.ut_id, utid, sizeof (set_utmp.ut_id));
392*0Sstevel@tonic-gate 	/*
393*0Sstevel@tonic-gate 	 * Go through each entry one by one, looking only at INIT,
394*0Sstevel@tonic-gate 	 * LOGIN or USER Processes.  Use the entry found if flags == 0
395*0Sstevel@tonic-gate 	 * and the line name matches, or if the process ID matches if
396*0Sstevel@tonic-gate 	 * the UPDATE_ENTRY flag is set.  The UPDATE_ENTRY flag is mainly
397*0Sstevel@tonic-gate 	 * for login which normally only wants to update an entry if
398*0Sstevel@tonic-gate 	 * the pid fields matches.
399*0Sstevel@tonic-gate 	 */
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	if (u == (struct utmpx *)NULL) {
402*0Sstevel@tonic-gate 		(void) makeutx(&set_utmp);
403*0Sstevel@tonic-gate 	} else
404*0Sstevel@tonic-gate 		updwtmpx(WTMPX_FILE, &set_utmp);
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate }
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate /*
409*0Sstevel@tonic-gate  * edit the Unix traditional data files that tell who is logged
410*0Sstevel@tonic-gate  * into "the system".
411*0Sstevel@tonic-gate  */
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate LogoutUser()
414*0Sstevel@tonic-gate {
415*0Sstevel@tonic-gate 	struct utmpx *up;
416*0Sstevel@tonic-gate 	struct utmpx ut;
417*0Sstevel@tonic-gate 	int pid;
418*0Sstevel@tonic-gate 	char user[sizeof (ut.ut_user) + 1];
419*0Sstevel@tonic-gate 	char ttyn[sizeof (ut.ut_line) + 1];
420*0Sstevel@tonic-gate 	char rhost[sizeof (ut.ut_host) + 1];
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 	sighold(SIGCHLD);		/* no disruption during cleanup */
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate 	if (pamh) {
425*0Sstevel@tonic-gate 		pam_end(pamh, PAM_SUCCESS);
426*0Sstevel@tonic-gate 		pamh = NULL;
427*0Sstevel@tonic-gate 	}
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate 	/* BEGIN RESET UTMP */
430*0Sstevel@tonic-gate 	pid = child;
431*0Sstevel@tonic-gate 	setutxent();
432*0Sstevel@tonic-gate 	while (up = getutxent()) {
433*0Sstevel@tonic-gate 		if (up->ut_pid == pid) {
434*0Sstevel@tonic-gate 			if (up->ut_type == DEAD_PROCESS) {
435*0Sstevel@tonic-gate 				/*
436*0Sstevel@tonic-gate 				 * Cleaned up elsewhere.
437*0Sstevel@tonic-gate 				 */
438*0Sstevel@tonic-gate 				break;
439*0Sstevel@tonic-gate 			}
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 			strncpy(user, up->ut_user, sizeof (up->ut_user));
442*0Sstevel@tonic-gate 			user[sizeof (up->ut_user)] = '\0';
443*0Sstevel@tonic-gate 			strncpy(ttyn, up->ut_line, sizeof (up->ut_line));
444*0Sstevel@tonic-gate 			ttyn[sizeof (up->ut_line)] = '\0';
445*0Sstevel@tonic-gate 			strncpy(rhost, up->ut_host, sizeof (up->ut_host));
446*0Sstevel@tonic-gate 			rhost[sizeof (up->ut_host)] = '\0';
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 			if ((pam_start("rexd", user, NULL, &pamh))
449*0Sstevel@tonic-gate 							== PAM_SUCCESS) {
450*0Sstevel@tonic-gate 				(void) pam_set_item(pamh, PAM_TTY, ttyn);
451*0Sstevel@tonic-gate 				(void) pam_set_item(pamh, PAM_RHOST, rhost);
452*0Sstevel@tonic-gate 				(void) pam_close_session(pamh, 0);
453*0Sstevel@tonic-gate 				(void) pam_end(pamh, PAM_SUCCESS);
454*0Sstevel@tonic-gate 				pamh = NULL;
455*0Sstevel@tonic-gate 			}
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 			up->ut_type = DEAD_PROCESS;
458*0Sstevel@tonic-gate 			up->ut_exit.e_termination = WTERMSIG(0);
459*0Sstevel@tonic-gate 			up->ut_exit.e_exit = WEXITSTATUS(0);
460*0Sstevel@tonic-gate 			(void) time(&up->ut_tv.tv_sec);
461*0Sstevel@tonic-gate 			if (modutx(up) == NULL) {
462*0Sstevel@tonic-gate 				/*
463*0Sstevel@tonic-gate 				 * Since modutx failed we'll
464*0Sstevel@tonic-gate 				 * write out the new entry
465*0Sstevel@tonic-gate 				 * ourselves.
466*0Sstevel@tonic-gate 				 */
467*0Sstevel@tonic-gate 				(void) pututxline(up);
468*0Sstevel@tonic-gate 				updwtmpx("wtmpx", up);
469*0Sstevel@tonic-gate 			}
470*0Sstevel@tonic-gate 			break;
471*0Sstevel@tonic-gate 		}
472*0Sstevel@tonic-gate 	}
473*0Sstevel@tonic-gate 	endutxent();
474*0Sstevel@tonic-gate 	/* END RESET UTMP */
475*0Sstevel@tonic-gate 	sigrelse(SIGCHLD);
476*0Sstevel@tonic-gate }
477*0Sstevel@tonic-gate 
478*0Sstevel@tonic-gate /*
479*0Sstevel@tonic-gate  * set the pty modes to the given values
480*0Sstevel@tonic-gate  */
481*0Sstevel@tonic-gate SetPtyMode(mode)
482*0Sstevel@tonic-gate 	struct rex_ttymode *mode;
483*0Sstevel@tonic-gate {
484*0Sstevel@tonic-gate 	struct sgttyb svr4_sgttyb_var;
485*0Sstevel@tonic-gate 	int ldisc = NTTYDISC;
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate 	if (Debug)
488*0Sstevel@tonic-gate 		printf("Enter SetPtyMode\n");
489*0Sstevel@tonic-gate 	if (Debug)
490*0Sstevel@tonic-gate 		printf("SetPtyMode:opened slave\n");
491*0Sstevel@tonic-gate 	ioctl(Slave, TIOCSETD, &ldisc);
492*0Sstevel@tonic-gate 	if (Debug)
493*0Sstevel@tonic-gate 		printf("SetPtyMode:Slave TIOCSETD done\n");
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 	/*
496*0Sstevel@tonic-gate 	 * Copy from over-the-net(bsd) to SVR4 format
497*0Sstevel@tonic-gate 	 */
498*0Sstevel@tonic-gate 	svr4_sgttyb_var.sg_ispeed = mode->basic.sg_ispeed;
499*0Sstevel@tonic-gate 	svr4_sgttyb_var.sg_ospeed = mode->basic.sg_ospeed;
500*0Sstevel@tonic-gate 	svr4_sgttyb_var.sg_erase  = mode->basic.sg_erase;
501*0Sstevel@tonic-gate 	svr4_sgttyb_var.sg_kill = mode->basic.sg_kill;
502*0Sstevel@tonic-gate 	svr4_sgttyb_var.sg_flags = (int)mode->basic.sg_flags;
503*0Sstevel@tonic-gate 	/*
504*0Sstevel@tonic-gate 	 * Clear any possible sign extension caused by (int)
505*0Sstevel@tonic-gate 	 * typecast
506*0Sstevel@tonic-gate 	 */
507*0Sstevel@tonic-gate 	svr4_sgttyb_var.sg_flags &= 0xFFFF;
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 	ioctl(Slave, TIOCSETN, &svr4_sgttyb_var);
510*0Sstevel@tonic-gate 	if (Debug)
511*0Sstevel@tonic-gate 		printf("SetPtyMode:Slave TIOCSETN done\n");
512*0Sstevel@tonic-gate 	ioctl(Slave, TIOCSETC, &mode->more);
513*0Sstevel@tonic-gate 	if (Debug)
514*0Sstevel@tonic-gate 		printf("SetPtyMode:Slave TIOCSETC done\n");
515*0Sstevel@tonic-gate 	ioctl(Slave, TIOCSLTC, &mode->yetmore);
516*0Sstevel@tonic-gate 	if (Debug)
517*0Sstevel@tonic-gate 		printf("SetPtyMode:Slave TIOCSLTC done\n");
518*0Sstevel@tonic-gate 	ioctl(Slave, TIOCLSET, &mode->andmore);
519*0Sstevel@tonic-gate 	if (Debug)
520*0Sstevel@tonic-gate 		printf("SetPtyMode:Slave TIOCSET done\n");
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 	/* Opened in AllocPty for parent, still open in child */
523*0Sstevel@tonic-gate 	if (Slave_is_closed_on_master_side == FALSE) {
524*0Sstevel@tonic-gate 		close(Slave);
525*0Sstevel@tonic-gate 		Slave_is_closed_on_master_side = TRUE;
526*0Sstevel@tonic-gate 	}
527*0Sstevel@tonic-gate }
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate /*
530*0Sstevel@tonic-gate  * set the pty window size to the given value
531*0Sstevel@tonic-gate  */
532*0Sstevel@tonic-gate SetPtySize(sizep)
533*0Sstevel@tonic-gate 	struct rex_ttysize *sizep;
534*0Sstevel@tonic-gate {
535*0Sstevel@tonic-gate 	struct winsize newsize;
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	/* if size has changed, this ioctl changes it */
538*0Sstevel@tonic-gate 	/* *and* sends SIGWINCH to process group */
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	newsize.ws_row = (unsigned short) sizep->ts_lines;
541*0Sstevel@tonic-gate 	newsize.ws_col = (unsigned short) sizep->ts_cols;
542*0Sstevel@tonic-gate 
543*0Sstevel@tonic-gate 	(void) ioctl(Master, TIOCSWINSZ, &newsize);
544*0Sstevel@tonic-gate 	if (Slave_is_closed_on_master_side == FALSE) {
545*0Sstevel@tonic-gate 		close(Slave);
546*0Sstevel@tonic-gate 		Slave_is_closed_on_master_side = TRUE;
547*0Sstevel@tonic-gate 	}
548*0Sstevel@tonic-gate }
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate /*
552*0Sstevel@tonic-gate  * send the given signal to the group controlling the terminal
553*0Sstevel@tonic-gate  */
554*0Sstevel@tonic-gate SendSignal(sig)
555*0Sstevel@tonic-gate 	int sig;
556*0Sstevel@tonic-gate {
557*0Sstevel@tonic-gate 	pid_t pgrp;
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	pgrp = getpgid(child);
560*0Sstevel@tonic-gate 	if (pgrp != (pid_t)-1)
561*0Sstevel@tonic-gate 		(void) kill((-pgrp), sig);
562*0Sstevel@tonic-gate }
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate /*
565*0Sstevel@tonic-gate  * called when the main select loop detects that we might want to
566*0Sstevel@tonic-gate  * read something.
567*0Sstevel@tonic-gate  */
568*0Sstevel@tonic-gate void
569*0Sstevel@tonic-gate HelperRead(pollfd_t *fdp, int nfds, int *pollretval)
570*0Sstevel@tonic-gate {
571*0Sstevel@tonic-gate 	char buf[128];
572*0Sstevel@tonic-gate 	int retval;
573*0Sstevel@tonic-gate 	extern int errno;
574*0Sstevel@tonic-gate 	int mask;
575*0Sstevel@tonic-gate 	int master = -1;
576*0Sstevel@tonic-gate 	int inputsocket = -1;
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate 	/*
579*0Sstevel@tonic-gate 	 * fdp pollset may be compressed. Search for Master and
580*0Sstevel@tonic-gate 	 * InputSocket fds.
581*0Sstevel@tonic-gate 	 */
582*0Sstevel@tonic-gate 	int i;
583*0Sstevel@tonic-gate 	for (i = 0; i < nfds; i++) {
584*0Sstevel@tonic-gate 		if (fdp[i].fd == Master && fdp[i].revents != 0)
585*0Sstevel@tonic-gate 			master = i;
586*0Sstevel@tonic-gate 		if (fdp[i].fd == InputSocket && fdp[i].revents != 0)
587*0Sstevel@tonic-gate 			inputsocket = i;
588*0Sstevel@tonic-gate 	}
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate /*	mask = sigsetmask (sigmask (SIGCHLD));	*/
591*0Sstevel@tonic-gate 	mask = sighold(SIGCHLD);
592*0Sstevel@tonic-gate 	retval = 0;
593*0Sstevel@tonic-gate 	if (master != -1) {
594*0Sstevel@tonic-gate 		if (!(fdp[master].revents & (POLLERR | POLLHUP | POLLNVAL))) {
595*0Sstevel@tonic-gate 			retval = read(Master, buf, sizeof (buf));
596*0Sstevel@tonic-gate 			if (retval > 0) {
597*0Sstevel@tonic-gate 				(void) write(OutputSocket, buf, retval);
598*0Sstevel@tonic-gate 			} else {
599*0Sstevel@tonic-gate 				if (errno != EINTR && errno != EIO &&
600*0Sstevel@tonic-gate 				    errno != EWOULDBLOCK)
601*0Sstevel@tonic-gate 					perror("pty read");
602*0Sstevel@tonic-gate 				/* 1 => further sends disallowed */
603*0Sstevel@tonic-gate 				shutdown(OutputSocket, 1);
604*0Sstevel@tonic-gate 				xprt_unregister(&uxprt[MASTER]);
605*0Sstevel@tonic-gate 			}
606*0Sstevel@tonic-gate 		}
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate 		/* clear this event for svc_getreq_poll */
609*0Sstevel@tonic-gate 		fdp[master].revents = 0;
610*0Sstevel@tonic-gate 		*pollretval = *pollretval - 1;
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 		if (retval <= 0 && ChildDied) {
613*0Sstevel@tonic-gate 			KillHelper(child);
614*0Sstevel@tonic-gate 			HasHelper = 0;
615*0Sstevel@tonic-gate 			if (inputsocket != -1) {
616*0Sstevel@tonic-gate 				fdp[inputsocket].revents = 0;
617*0Sstevel@tonic-gate 				*pollretval = *pollretval - 1;
618*0Sstevel@tonic-gate 			}
619*0Sstevel@tonic-gate 			goto done;
620*0Sstevel@tonic-gate 		}
621*0Sstevel@tonic-gate 	}
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate 	if (inputsocket != -1) {
624*0Sstevel@tonic-gate 		if (!(fdp[inputsocket].revents & (POLLERR | POLLHUP |
625*0Sstevel@tonic-gate 							    POLLNVAL))) {
626*0Sstevel@tonic-gate 			retval = read(InputSocket, buf, sizeof (buf));
627*0Sstevel@tonic-gate 			if (retval > 0) {
628*0Sstevel@tonic-gate 				(void) write(Master, buf, retval);
629*0Sstevel@tonic-gate 			} else {
630*0Sstevel@tonic-gate 				if (errno != EINTR && errno != EWOULDBLOCK)
631*0Sstevel@tonic-gate 					perror("socket read");
632*0Sstevel@tonic-gate 				xprt_unregister(&uxprt[INPUTSOCKET]);
633*0Sstevel@tonic-gate 			}
634*0Sstevel@tonic-gate 		}
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 		/* clear this event for svc_getreq_poll */
637*0Sstevel@tonic-gate 		fdp[inputsocket].revents = 0;
638*0Sstevel@tonic-gate 		*pollretval = *pollretval - 1;
639*0Sstevel@tonic-gate 	}
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate 	done:
642*0Sstevel@tonic-gate /*	sigsetmask (mask);	*/
643*0Sstevel@tonic-gate 	sigrelse(SIGCHLD);
644*0Sstevel@tonic-gate }
645