xref: /illumos-gate/usr/src/lib/libutempter/common/utempter.c (revision 8124b8119d8ab7f80f1bd8b094cd96356f531b54)
1 /*
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 
31 #include <pwd.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <utmpx.h>
36 
37 static int last_fd = -1;
38 
39 static void
utempter_update_utmpx(int type,int fd,const char * host)40 utempter_update_utmpx(int type, int fd, const char *host)
41 {
42 	struct utmpx ut;
43 	struct passwd *pw;
44 	uid_t uid;
45 
46 	(void) memset(&ut, 0, sizeof (ut));
47 	ut.ut_type = type;
48 	ut.ut_pid = getpid();
49 	ut.ut_session = getsid(0);
50 	(void) gettimeofday(&ut.ut_tv, NULL);
51 	if (snprintf(ut.ut_id, sizeof (ut.ut_id), "f%d", fd) >=
52 	    sizeof (ut.ut_id))
53 		return;
54 	uid = getuid();
55 	if ((pw = getpwuid(uid)) == NULL)
56 		return;
57 	(void) strlcpy(ut.ut_user, pw->pw_name, sizeof (ut.ut_user));
58 
59 	if (type == DEAD_PROCESS) {
60 		struct utmpx *ut1;
61 		struct utmpx ut2;
62 
63 		(void) memset(&ut2, 0, sizeof (ut2));
64 		ut2.ut_type = USER_PROCESS;
65 		if (snprintf(ut2.ut_id, sizeof (ut2.ut_id), "f%d", fd) >=
66 		    sizeof (ut2.ut_id))
67 			return;
68 		if ((ut1 = getutxid(&ut2)) == NULL)
69 			return;
70 		(void) strlcpy(ut.ut_line, ut1->ut_line, sizeof (ut.ut_line));
71 	} else {
72 		char *line = ptsname(fd);
73 		if (line == NULL)
74 			return;
75 		(void) strlcpy(ut.ut_line, line + strlen("/dev/"),
76 		    sizeof (ut.ut_line));
77 	}
78 
79 	if (host != NULL) {
80 		(void) strlcpy(ut.ut_host, host, sizeof (ut.ut_host));
81 		ut.ut_syslen = strlen(ut.ut_host) + 1;
82 	}
83 
84 	setutxent();
85 	(void) pututxline(&ut);
86 	endutxent();
87 }
88 
89 int
utempter_add_record(int fd,const char * host)90 utempter_add_record(int fd, const char *host)
91 {
92 	utempter_update_utmpx(USER_PROCESS, fd, host);
93 	last_fd = fd;
94 	return (0);
95 }
96 
97 int
utempter_remove_record(int fd)98 utempter_remove_record(int fd)
99 {
100 	utempter_update_utmpx(DEAD_PROCESS, fd, NULL);
101 	if (last_fd == fd)
102 		last_fd = -1;
103 	return (0);
104 }
105 
106 int
utempter_remove_added_record(void)107 utempter_remove_added_record(void)
108 {
109 	if (last_fd < 0)
110 		return (0);
111 	utempter_update_utmpx(DEAD_PROCESS, last_fd, NULL);
112 	last_fd = -1;
113 	return (0);
114 }
115 
116 void
addToUtmp(const char * pty __unused,const char * host,int fd)117 addToUtmp(const char *pty __unused, const char *host, int fd)
118 {
119 	(void) utempter_add_record(fd, host);
120 }
121 
122 void
removeFromUtmp(void)123 removeFromUtmp(void)
124 {
125 	(void) utempter_remove_added_record();
126 }
127 
128 void
removeLineFromUtmp(const char * pty __unused,int fd)129 removeLineFromUtmp(const char *pty __unused, int fd)
130 {
131 	(void) utempter_remove_record(fd);
132 }
133