xref: /illumos-gate/usr/src/cmd/ttymon/tmutmp.c (revision 3bb2c156)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
506fc3f99Sas145665  * Common Development and Distribution License (the "License").
606fc3f99Sas145665  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2206fc3f99Sas145665  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include	<unistd.h>
407c478bd9Sstevel@tonic-gate #include	<stdlib.h>
417c478bd9Sstevel@tonic-gate #include	<stdio.h>
427c478bd9Sstevel@tonic-gate #include	<fcntl.h>
437c478bd9Sstevel@tonic-gate #include	<sys/types.h>
447c478bd9Sstevel@tonic-gate #include	<sys/wait.h>
457c478bd9Sstevel@tonic-gate #include	<string.h>
467c478bd9Sstevel@tonic-gate #include	<memory.h>
477c478bd9Sstevel@tonic-gate #include	<utmpx.h>
487c478bd9Sstevel@tonic-gate #include	<security/pam_appl.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include	"sac.h"
517c478bd9Sstevel@tonic-gate #include	"tmextern.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * account - create a utmpx record for service
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate int
account(char * line)59*3bb2c156SToomas Soome account(char *line)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	struct utmpx utmpx;			/* prototype utmpx entry */
627c478bd9Sstevel@tonic-gate 	struct utmpx *up = &utmpx;		/* and a pointer to it */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	(void) memset(up, '\0', sizeof (utmpx));
657c478bd9Sstevel@tonic-gate 	up->ut_user[0] = '.';
667c478bd9Sstevel@tonic-gate 	(void) strncpy(&up->ut_user[1], Tag, sizeof (up->ut_user)-1);
677c478bd9Sstevel@tonic-gate 	(void) strncpy(up->ut_line, lastname(line), sizeof (up->ut_line));
687c478bd9Sstevel@tonic-gate 	up->ut_pid = getpid();
697c478bd9Sstevel@tonic-gate 	up->ut_type = USER_PROCESS;
707c478bd9Sstevel@tonic-gate 	up->ut_id[0] = 't';
717c478bd9Sstevel@tonic-gate 	up->ut_id[1] = 'm';
727c478bd9Sstevel@tonic-gate 	up->ut_id[2] = SC_WILDC;
737c478bd9Sstevel@tonic-gate 	up->ut_id[3] = SC_WILDC;
747c478bd9Sstevel@tonic-gate 	up->ut_exit.e_termination = 0;
757c478bd9Sstevel@tonic-gate 	up->ut_exit.e_exit = 0;
767c478bd9Sstevel@tonic-gate 	(void) time(&up->ut_tv.tv_sec);
777c478bd9Sstevel@tonic-gate 	if (makeutx(up) == NULL) {
787c478bd9Sstevel@tonic-gate 		log("makeutx for pid %d failed", up->ut_pid);
797c478bd9Sstevel@tonic-gate 		return (-1);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 	return (0);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * checkut_line	- check if a login is active on the requested device
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate int
checkut_line(char * line)887c478bd9Sstevel@tonic-gate checkut_line(char *line)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	struct utmpx *u;
917c478bd9Sstevel@tonic-gate 	char buf[33], ttyn[33];
927c478bd9Sstevel@tonic-gate 	int rvalue = 0;
9306fc3f99Sas145665 	pid_t ownpid = getpid();
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	(void) strncpy(buf, lastname(line), sizeof (u->ut_line));
967c478bd9Sstevel@tonic-gate 	buf[sizeof (u->ut_line)] = '\0';
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	setutxent();
997c478bd9Sstevel@tonic-gate 	while ((u = getutxent()) != NULL) {
10006fc3f99Sas145665 		if (u->ut_pid == ownpid) {
1017c478bd9Sstevel@tonic-gate 			if (u->ut_type == USER_PROCESS) {
102*3bb2c156SToomas Soome 				(void) strncpy(ttyn, u->ut_line,
103*3bb2c156SToomas Soome 				    sizeof (u->ut_line));
1047c478bd9Sstevel@tonic-gate 				ttyn[sizeof (u->ut_line)] = '\0';
1057c478bd9Sstevel@tonic-gate 				if (strcmp(buf, ttyn) == 0) {
1067c478bd9Sstevel@tonic-gate 					rvalue = 1;
1077c478bd9Sstevel@tonic-gate 					break;
1087c478bd9Sstevel@tonic-gate 				}
1097c478bd9Sstevel@tonic-gate 			}
1107c478bd9Sstevel@tonic-gate 		}
11106fc3f99Sas145665 	}
1127c478bd9Sstevel@tonic-gate 	endutxent();
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	return (rvalue);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate void
cleanut(pid_t pid,int status)119*3bb2c156SToomas Soome cleanut(pid_t pid, int status)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	pam_handle_t *pamh;
1227c478bd9Sstevel@tonic-gate 	struct utmpx *up;
1237c478bd9Sstevel@tonic-gate 	char user[33], ttyn[33], rhost[258];
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	setutxent();
126*3bb2c156SToomas Soome 	while ((up = getutxent())) {
1277c478bd9Sstevel@tonic-gate 		if (up->ut_pid == pid) {
1287c478bd9Sstevel@tonic-gate 			if (up->ut_type == DEAD_PROCESS) {
1297c478bd9Sstevel@tonic-gate 				/* Cleaned up elsewhere. */
1307c478bd9Sstevel@tonic-gate 				break;
1317c478bd9Sstevel@tonic-gate 			}
1327c478bd9Sstevel@tonic-gate 
133*3bb2c156SToomas Soome 			(void) strncpy(user, up->ut_user, sizeof (up->ut_user));
1347c478bd9Sstevel@tonic-gate 			user[sizeof (up->ut_user)] = '\0';
135*3bb2c156SToomas Soome 			(void) strncpy(ttyn, up->ut_line, sizeof (up->ut_line));
1367c478bd9Sstevel@tonic-gate 			ttyn[sizeof (up->ut_line)] = '\0';
137*3bb2c156SToomas Soome 			(void) strncpy(rhost, up->ut_host,
138*3bb2c156SToomas Soome 			    sizeof (up->ut_host));
1397c478bd9Sstevel@tonic-gate 			rhost[sizeof (up->ut_host)] = '\0';
1407c478bd9Sstevel@tonic-gate 
141*3bb2c156SToomas Soome 			if (pam_start("ttymon", user, NULL, &pamh) ==
142*3bb2c156SToomas Soome 			    PAM_SUCCESS) {
1437c478bd9Sstevel@tonic-gate 				(void) pam_set_item(pamh, PAM_TTY, ttyn);
1447c478bd9Sstevel@tonic-gate 				(void) pam_set_item(pamh, PAM_RHOST, rhost);
1457c478bd9Sstevel@tonic-gate 				(void) pam_close_session(pamh, 0);
1467c478bd9Sstevel@tonic-gate 				(void) pam_end(pamh, PAM_SUCCESS);
1477c478bd9Sstevel@tonic-gate 			}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 			up->ut_type = DEAD_PROCESS;
1517c478bd9Sstevel@tonic-gate 			up->ut_exit.e_termination = WTERMSIG(status);
1527c478bd9Sstevel@tonic-gate 			up->ut_exit.e_exit = WEXITSTATUS(status);
1537c478bd9Sstevel@tonic-gate 			(void) time(&up->ut_tv.tv_sec);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 			if (modutx(up) == NULL) {
1567c478bd9Sstevel@tonic-gate 				/*
1577c478bd9Sstevel@tonic-gate 				 * Since modutx failed we'll
1587c478bd9Sstevel@tonic-gate 				 * write out the new entry
1597c478bd9Sstevel@tonic-gate 				 * ourselves.
1607c478bd9Sstevel@tonic-gate 				 */
1617c478bd9Sstevel@tonic-gate 				(void) pututxline(up);
1627c478bd9Sstevel@tonic-gate 				updwtmpx("wtmpx", up);
1637c478bd9Sstevel@tonic-gate 			}
1647c478bd9Sstevel@tonic-gate 			break;
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	endutxent();
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * getty_account	- This is a copy of old getty account routine.
1727c478bd9Sstevel@tonic-gate  *			- This is only called if ttymon is invoked as getty.
1737c478bd9Sstevel@tonic-gate  *			- It tries to find its own INIT_PROCESS entry in utmpx
1747c478bd9Sstevel@tonic-gate  *			- and change it to LOGIN_PROCESS
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate void
getty_account(char * line)177*3bb2c156SToomas Soome getty_account(char *line)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	pid_t ownpid;
1807c478bd9Sstevel@tonic-gate 	struct utmpx *u;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	ownpid = getpid();
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	setutxent();
1857c478bd9Sstevel@tonic-gate 	while ((u = getutxent()) != NULL) {
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 		if (u->ut_type == INIT_PROCESS && u->ut_pid == ownpid) {
1887c478bd9Sstevel@tonic-gate 			(void) strncpy(u->ut_line, lastname(line),
1897c478bd9Sstevel@tonic-gate 			    sizeof (u->ut_line));
1907c478bd9Sstevel@tonic-gate 			(void) strncpy(u->ut_user, "LOGIN",
1917c478bd9Sstevel@tonic-gate 			    sizeof (u->ut_user));
1927c478bd9Sstevel@tonic-gate 			u->ut_type = LOGIN_PROCESS;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 			/* Write out the updated entry. */
1957c478bd9Sstevel@tonic-gate 			(void) pututxline(u);
1967c478bd9Sstevel@tonic-gate 			break;
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/* create wtmpx entry also */
2017c478bd9Sstevel@tonic-gate 	if (u != NULL)
2027c478bd9Sstevel@tonic-gate 		updwtmpx("/etc/wtmpx", u);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	endutxent();
2057c478bd9Sstevel@tonic-gate }
206