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
5*06fc3f99Sas145665 * Common Development and Distribution License (the "License").
6*06fc3f99Sas145665 * 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 /*
22*06fc3f99Sas145665 * 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 #pragma ident "%Z%%M% %I% %E% SMI"
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include <unistd.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <fcntl.h>
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/wait.h>
477c478bd9Sstevel@tonic-gate #include <string.h>
487c478bd9Sstevel@tonic-gate #include <memory.h>
497c478bd9Sstevel@tonic-gate #include <utmpx.h>
507c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate #include "sac.h"
537c478bd9Sstevel@tonic-gate #include "tmextern.h"
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate extern char *lastname();
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate * account - create a utmpx record for service
597c478bd9Sstevel@tonic-gate *
607c478bd9Sstevel@tonic-gate */
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate int
account(line)637c478bd9Sstevel@tonic-gate account(line)
647c478bd9Sstevel@tonic-gate char *line;
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate struct utmpx utmpx; /* prototype utmpx entry */
677c478bd9Sstevel@tonic-gate struct utmpx *up = &utmpx; /* and a pointer to it */
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate (void) memset(up, '\0', sizeof (utmpx));
707c478bd9Sstevel@tonic-gate up->ut_user[0] = '.';
717c478bd9Sstevel@tonic-gate (void) strncpy(&up->ut_user[1], Tag, sizeof (up->ut_user)-1);
727c478bd9Sstevel@tonic-gate (void) strncpy(up->ut_line, lastname(line), sizeof (up->ut_line));
737c478bd9Sstevel@tonic-gate up->ut_pid = getpid();
747c478bd9Sstevel@tonic-gate up->ut_type = USER_PROCESS;
757c478bd9Sstevel@tonic-gate up->ut_id[0] = 't';
767c478bd9Sstevel@tonic-gate up->ut_id[1] = 'm';
777c478bd9Sstevel@tonic-gate up->ut_id[2] = SC_WILDC;
787c478bd9Sstevel@tonic-gate up->ut_id[3] = SC_WILDC;
797c478bd9Sstevel@tonic-gate up->ut_exit.e_termination = 0;
807c478bd9Sstevel@tonic-gate up->ut_exit.e_exit = 0;
817c478bd9Sstevel@tonic-gate (void) time(&up->ut_tv.tv_sec);
827c478bd9Sstevel@tonic-gate if (makeutx(up) == NULL) {
837c478bd9Sstevel@tonic-gate log("makeutx for pid %d failed", up->ut_pid);
847c478bd9Sstevel@tonic-gate return (-1);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate return (0);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate * checkut_line - check if a login is active on the requested device
917c478bd9Sstevel@tonic-gate */
927c478bd9Sstevel@tonic-gate int
checkut_line(char * line)937c478bd9Sstevel@tonic-gate checkut_line(char *line)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate struct utmpx *u;
967c478bd9Sstevel@tonic-gate char buf[33], ttyn[33];
977c478bd9Sstevel@tonic-gate int rvalue = 0;
98*06fc3f99Sas145665 pid_t ownpid = getpid();
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate (void) strncpy(buf, lastname(line), sizeof (u->ut_line));
1017c478bd9Sstevel@tonic-gate buf[sizeof (u->ut_line)] = '\0';
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate setutxent();
1047c478bd9Sstevel@tonic-gate while ((u = getutxent()) != NULL) {
105*06fc3f99Sas145665 if (u->ut_pid == ownpid) {
1067c478bd9Sstevel@tonic-gate if (u->ut_type == USER_PROCESS) {
1077c478bd9Sstevel@tonic-gate strncpy(ttyn, u->ut_line, sizeof (u->ut_line));
1087c478bd9Sstevel@tonic-gate ttyn[sizeof (u->ut_line)] = '\0';
1097c478bd9Sstevel@tonic-gate if (strcmp(buf, ttyn) == 0) {
1107c478bd9Sstevel@tonic-gate rvalue = 1;
1117c478bd9Sstevel@tonic-gate break;
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate }
115*06fc3f99Sas145665 }
1167c478bd9Sstevel@tonic-gate endutxent();
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate return (rvalue);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate void
cleanut(pid,status)1237c478bd9Sstevel@tonic-gate cleanut(pid, status)
1247c478bd9Sstevel@tonic-gate pid_t pid;
1257c478bd9Sstevel@tonic-gate int status;
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate pam_handle_t *pamh;
1287c478bd9Sstevel@tonic-gate struct utmpx *up;
1297c478bd9Sstevel@tonic-gate struct utmpx ut;
1307c478bd9Sstevel@tonic-gate char user[33], ttyn[33], rhost[258];
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate setutxent();
1337c478bd9Sstevel@tonic-gate while (up = getutxent()) {
1347c478bd9Sstevel@tonic-gate if (up->ut_pid == pid) {
1357c478bd9Sstevel@tonic-gate if (up->ut_type == DEAD_PROCESS) {
1367c478bd9Sstevel@tonic-gate /* Cleaned up elsewhere. */
1377c478bd9Sstevel@tonic-gate break;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate strncpy(user, up->ut_user, sizeof (up->ut_user));
1417c478bd9Sstevel@tonic-gate user[sizeof (up->ut_user)] = '\0';
1427c478bd9Sstevel@tonic-gate strncpy(ttyn, up->ut_line, sizeof (up->ut_line));
1437c478bd9Sstevel@tonic-gate ttyn[sizeof (up->ut_line)] = '\0';
1447c478bd9Sstevel@tonic-gate strncpy(rhost, up->ut_host, sizeof (up->ut_host));
1457c478bd9Sstevel@tonic-gate rhost[sizeof (up->ut_host)] = '\0';
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate if (pam_start("ttymon", user, NULL, &pamh)
1487c478bd9Sstevel@tonic-gate == PAM_SUCCESS) {
1497c478bd9Sstevel@tonic-gate (void) pam_set_item(pamh, PAM_TTY, ttyn);
1507c478bd9Sstevel@tonic-gate (void) pam_set_item(pamh, PAM_RHOST, rhost);
1517c478bd9Sstevel@tonic-gate (void) pam_close_session(pamh, 0);
1527c478bd9Sstevel@tonic-gate (void) pam_end(pamh, PAM_SUCCESS);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate up->ut_type = DEAD_PROCESS;
1577c478bd9Sstevel@tonic-gate up->ut_exit.e_termination = WTERMSIG(status);
1587c478bd9Sstevel@tonic-gate up->ut_exit.e_exit = WEXITSTATUS(status);
1597c478bd9Sstevel@tonic-gate (void) time(&up->ut_tv.tv_sec);
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate if (modutx(up) == NULL) {
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate * Since modutx failed we'll
1647c478bd9Sstevel@tonic-gate * write out the new entry
1657c478bd9Sstevel@tonic-gate * ourselves.
1667c478bd9Sstevel@tonic-gate */
1677c478bd9Sstevel@tonic-gate (void) pututxline(up);
1687c478bd9Sstevel@tonic-gate updwtmpx("wtmpx", up);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate break;
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate endutxent();
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate * getty_account - This is a copy of old getty account routine.
1787c478bd9Sstevel@tonic-gate * - This is only called if ttymon is invoked as getty.
1797c478bd9Sstevel@tonic-gate * - It tries to find its own INIT_PROCESS entry in utmpx
1807c478bd9Sstevel@tonic-gate * - and change it to LOGIN_PROCESS
1817c478bd9Sstevel@tonic-gate */
1827c478bd9Sstevel@tonic-gate void
getty_account(line)1837c478bd9Sstevel@tonic-gate getty_account(line)
1847c478bd9Sstevel@tonic-gate char *line;
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate pid_t ownpid;
1877c478bd9Sstevel@tonic-gate struct utmpx *u;
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate ownpid = getpid();
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate setutxent();
1927c478bd9Sstevel@tonic-gate while ((u = getutxent()) != NULL) {
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate if (u->ut_type == INIT_PROCESS && u->ut_pid == ownpid) {
1957c478bd9Sstevel@tonic-gate (void) strncpy(u->ut_line, lastname(line),
1967c478bd9Sstevel@tonic-gate sizeof (u->ut_line));
1977c478bd9Sstevel@tonic-gate (void) strncpy(u->ut_user, "LOGIN",
1987c478bd9Sstevel@tonic-gate sizeof (u->ut_user));
1997c478bd9Sstevel@tonic-gate u->ut_type = LOGIN_PROCESS;
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate /* Write out the updated entry. */
2027c478bd9Sstevel@tonic-gate (void) pututxline(u);
2037c478bd9Sstevel@tonic-gate break;
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate /* create wtmpx entry also */
2087c478bd9Sstevel@tonic-gate if (u != NULL)
2097c478bd9Sstevel@tonic-gate updwtmpx("/etc/wtmpx", u);
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate endutxent();
2127c478bd9Sstevel@tonic-gate }
213