xref: /freebsd/lib/libc/gen/getutxent.c (revision 955c8cbb4960e6cf3602de144b1b9154a5092968)
1 /*-
2  * Copyright (c) 2010 Ed Schouten <ed@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "namespace.h"
31 #include <sys/endian.h>
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <utmpx.h>
38 #include "utxdb.h"
39 #include "un-namespace.h"
40 
41 #ifdef __NO_TLS
42 static FILE *uf = NULL;
43 static int udb;
44 #else
45 static _Thread_local FILE *uf = NULL;
46 static _Thread_local int udb;
47 #endif
48 
49 int
50 setutxdb(int db, const char *file)
51 {
52 	struct stat sb;
53 
54 	switch (db) {
55 	case UTXDB_ACTIVE:
56 		if (file == NULL)
57 			file = _PATH_UTX_ACTIVE;
58 		break;
59 	case UTXDB_LASTLOGIN:
60 		if (file == NULL)
61 			file = _PATH_UTX_LASTLOGIN;
62 		break;
63 	case UTXDB_LOG:
64 		if (file == NULL)
65 			file = _PATH_UTX_LOG;
66 		break;
67 	default:
68 		errno = EINVAL;
69 		return (-1);
70 	}
71 
72 	if (uf != NULL)
73 		fclose(uf);
74 	uf = fopen(file, "re");
75 	if (uf == NULL)
76 		return (-1);
77 
78 	if (db != UTXDB_LOG) {
79 		/* Safety check: never use broken files. */
80 		if (_fstat(fileno(uf), &sb) != -1 &&
81 		    sb.st_size % sizeof(struct futx) != 0) {
82 			fclose(uf);
83 			uf = NULL;
84 			errno = EFTYPE;
85 			return (-1);
86 		}
87 		/* Prevent reading of partial records. */
88 		(void)setvbuf(uf, NULL, _IOFBF,
89 		    rounddown(BUFSIZ, sizeof(struct futx)));
90 	}
91 
92 	udb = db;
93 	return (0);
94 }
95 
96 void
97 setutxent(void)
98 {
99 
100 	setutxdb(UTXDB_ACTIVE, NULL);
101 }
102 
103 void
104 endutxent(void)
105 {
106 
107 	if (uf != NULL) {
108 		fclose(uf);
109 		uf = NULL;
110 	}
111 }
112 
113 static int
114 getfutxent(struct futx *fu)
115 {
116 
117 	if (uf == NULL)
118 		setutxent();
119 	if (uf == NULL)
120 		return (-1);
121 
122 	if (udb == UTXDB_LOG) {
123 		uint16_t len;
124 
125 		if (fread(&len, sizeof(len), 1, uf) != 1)
126 			return (-1);
127 		len = be16toh(len);
128 		if (len > sizeof *fu) {
129 			/* Forward compatibility. */
130 			if (fread(fu, sizeof(*fu), 1, uf) != 1)
131 				return (-1);
132 			fseek(uf, len - sizeof(*fu), SEEK_CUR);
133 		} else {
134 			/* Partial record. */
135 			memset(fu, 0, sizeof(*fu));
136 			if (fread(fu, len, 1, uf) != 1)
137 				return (-1);
138 		}
139 	} else {
140 		if (fread(fu, sizeof(*fu), 1, uf) != 1)
141 			return (-1);
142 	}
143 	return (0);
144 }
145 
146 struct utmpx *
147 getutxent(void)
148 {
149 	struct futx fu;
150 
151 	if (getfutxent(&fu) != 0)
152 		return (NULL);
153 	return (futx_to_utx(&fu));
154 }
155 
156 struct utmpx *
157 getutxid(const struct utmpx *id)
158 {
159 	struct futx fu;
160 
161 	for (;;) {
162 		if (getfutxent(&fu) != 0)
163 			return (NULL);
164 
165 		switch (fu.fu_type) {
166 		case USER_PROCESS:
167 		case INIT_PROCESS:
168 		case LOGIN_PROCESS:
169 		case DEAD_PROCESS:
170 			switch (id->ut_type) {
171 			case USER_PROCESS:
172 			case INIT_PROCESS:
173 			case LOGIN_PROCESS:
174 			case DEAD_PROCESS:
175 				if (memcmp(fu.fu_id, id->ut_id,
176 				    MIN(sizeof(fu.fu_id), sizeof(id->ut_id))) ==
177 				    0)
178 					goto found;
179 			}
180 			break;
181 		default:
182 			if (fu.fu_type == id->ut_type)
183 				goto found;
184 			break;
185 		}
186 	}
187 
188 found:
189 	return (futx_to_utx(&fu));
190 }
191 
192 struct utmpx *
193 getutxline(const struct utmpx *line)
194 {
195 	struct futx fu;
196 
197 	for (;;) {
198 		if (getfutxent(&fu) != 0)
199 			return (NULL);
200 
201 		switch (fu.fu_type) {
202 		case USER_PROCESS:
203 		case LOGIN_PROCESS:
204 			if (strncmp(fu.fu_line, line->ut_line,
205 			    MIN(sizeof(fu.fu_line), sizeof(line->ut_line))) ==
206 			    0)
207 				goto found;
208 			break;
209 		}
210 	}
211 
212 found:
213 	return (futx_to_utx(&fu));
214 }
215 
216 struct utmpx *
217 getutxuser(const char *user)
218 {
219 	struct futx fu;
220 
221 	for (;;) {
222 		if (getfutxent(&fu) != 0)
223 			return (NULL);
224 
225 		switch (fu.fu_type) {
226 		case USER_PROCESS:
227 			if (strncmp(fu.fu_user, user, sizeof(fu.fu_user)) == 0)
228 				goto found;
229 			break;
230 		}
231 	}
232 
233 found:
234 	return (futx_to_utx(&fu));
235 }
236