xref: /freebsd/lib/libc/gen/getutxent.c (revision 675be9115aae86ad6b3d877155d4fd7822892105)
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 static FILE *uf = NULL;
42 static int udb;
43 
44 int
45 setutxdb(int db, const char *file)
46 {
47 	struct stat sb;
48 
49 	switch (db) {
50 	case UTXDB_ACTIVE:
51 		if (file == NULL)
52 			file = _PATH_UTX_ACTIVE;
53 		break;
54 	case UTXDB_LASTLOGIN:
55 		if (file == NULL)
56 			file = _PATH_UTX_LASTLOGIN;
57 		break;
58 	case UTXDB_LOG:
59 		if (file == NULL)
60 			file = _PATH_UTX_LOG;
61 		break;
62 	default:
63 		errno = EINVAL;
64 		return (-1);
65 	}
66 
67 	if (uf != NULL)
68 		fclose(uf);
69 	uf = fopen(file, "r");
70 	if (uf == NULL)
71 		return (-1);
72 
73 	/* Safety check: never use broken files. */
74 	if (db != UTXDB_LOG && _fstat(fileno(uf), &sb) != -1 &&
75 	    sb.st_size % sizeof(struct futx) != 0) {
76 		fclose(uf);
77 		uf = NULL;
78 		errno = EFTYPE;
79 		return (-1);
80 	}
81 
82 	udb = db;
83 	return (0);
84 }
85 
86 void
87 setutxent(void)
88 {
89 
90 	setutxdb(UTXDB_ACTIVE, NULL);
91 }
92 
93 void
94 endutxent(void)
95 {
96 
97 	if (uf != NULL) {
98 		fclose(uf);
99 		uf = NULL;
100 	}
101 }
102 
103 static int
104 getfutxent(struct futx *fu)
105 {
106 
107 	if (uf == NULL)
108 		setutxent();
109 	if (uf == NULL)
110 		return (-1);
111 
112 	if (udb == UTXDB_LOG) {
113 		uint16_t len;
114 
115 		if (fread(&len, sizeof(len), 1, uf) != 1)
116 			return (-1);
117 		len = be16toh(len);
118 		if (len > sizeof *fu) {
119 			/* Forward compatibility. */
120 			if (fread(fu, sizeof(*fu), 1, uf) != 1)
121 				return (-1);
122 			fseek(uf, len - sizeof(*fu), SEEK_CUR);
123 		} else {
124 			/* Partial record. */
125 			memset(fu, 0, sizeof(*fu));
126 			if (fread(fu, len, 1, uf) != 1)
127 				return (-1);
128 		}
129 	} else {
130 		if (fread(fu, sizeof(*fu), 1, uf) != 1)
131 			return (-1);
132 	}
133 	return (0);
134 }
135 
136 struct utmpx *
137 getutxent(void)
138 {
139 	struct futx fu;
140 
141 	if (getfutxent(&fu) != 0)
142 		return (NULL);
143 	return (futx_to_utx(&fu));
144 }
145 
146 struct utmpx *
147 getutxid(const struct utmpx *id)
148 {
149 	struct futx fu;
150 
151 	for (;;) {
152 		if (getfutxent(&fu) != 0)
153 			return (NULL);
154 
155 		switch (fu.fu_type) {
156 		case USER_PROCESS:
157 		case INIT_PROCESS:
158 		case LOGIN_PROCESS:
159 		case DEAD_PROCESS:
160 			switch (id->ut_type) {
161 			case USER_PROCESS:
162 			case INIT_PROCESS:
163 			case LOGIN_PROCESS:
164 			case DEAD_PROCESS:
165 				if (memcmp(fu.fu_id, id->ut_id,
166 				    MIN(sizeof(fu.fu_id), sizeof(id->ut_id))) ==
167 				    0)
168 					goto found;
169 			}
170 			break;
171 		default:
172 			if (fu.fu_type == id->ut_type)
173 				goto found;
174 			break;
175 		}
176 	}
177 
178 found:
179 	return (futx_to_utx(&fu));
180 }
181 
182 struct utmpx *
183 getutxline(const struct utmpx *line)
184 {
185 	struct futx fu;
186 
187 	for (;;) {
188 		if (getfutxent(&fu) != 0)
189 			return (NULL);
190 
191 		switch (fu.fu_type) {
192 		case USER_PROCESS:
193 		case LOGIN_PROCESS:
194 			if (strncmp(fu.fu_line, line->ut_line,
195 			    MIN(sizeof(fu.fu_line), sizeof(line->ut_line))) ==
196 			    0)
197 				goto found;
198 			break;
199 		}
200 	}
201 
202 found:
203 	return (futx_to_utx(&fu));
204 }
205 
206 struct utmpx *
207 getutxuser(const char *user)
208 {
209 	struct futx fu;
210 
211 	for (;;) {
212 		if (getfutxent(&fu) != 0)
213 			return (NULL);
214 
215 		switch (fu.fu_type) {
216 		case USER_PROCESS:
217 			if (strncmp(fu.fu_user, user, sizeof(fu.fu_user)) == 0)
218 				goto found;
219 			break;
220 		}
221 	}
222 
223 found:
224 	return (futx_to_utx(&fu));
225 }
226