utmpx.c revision 1.3.2.2 1 1.3.2.2 nathanw /* $NetBSD: utmpx.c,v 1.3.2.2 2002/03/08 21:35:20 nathanw Exp $ */
2 1.3.2.2 nathanw
3 1.3.2.2 nathanw /*-
4 1.3.2.2 nathanw * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.3.2.2 nathanw * All rights reserved.
6 1.3.2.2 nathanw *
7 1.3.2.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 nathanw * by Christos Zoulas.
9 1.3.2.2 nathanw *
10 1.3.2.2 nathanw * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 nathanw * modification, are permitted provided that the following conditions
12 1.3.2.2 nathanw * are met:
13 1.3.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 nathanw * documentation and/or other materials provided with the distribution.
18 1.3.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.3.2.2 nathanw * must display the following acknowledgement:
20 1.3.2.2 nathanw * This product includes software developed by the NetBSD
21 1.3.2.2 nathanw * Foundation, Inc. and its contributors.
22 1.3.2.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3.2.2 nathanw * contributors may be used to endorse or promote products derived
24 1.3.2.2 nathanw * from this software without specific prior written permission.
25 1.3.2.2 nathanw *
26 1.3.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3.2.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3.2.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3.2.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3.2.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3.2.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3.2.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3.2.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3.2.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3.2.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.3.2.2 nathanw */
38 1.3.2.2 nathanw #include <sys/cdefs.h>
39 1.3.2.2 nathanw
40 1.3.2.2 nathanw #if defined(LIBC_SCCS) && !defined(lint)
41 1.3.2.2 nathanw __RCSID("$NetBSD: utmpx.c,v 1.3.2.2 2002/03/08 21:35:20 nathanw Exp $");
42 1.3.2.2 nathanw #endif /* LIBC_SCCS and not lint */
43 1.3.2.2 nathanw
44 1.3.2.2 nathanw #include <sys/types.h>
45 1.3.2.2 nathanw #include <sys/param.h>
46 1.3.2.2 nathanw #include <sys/wait.h>
47 1.3.2.2 nathanw #include <sys/socket.h>
48 1.3.2.2 nathanw #include <sys/time.h>
49 1.3.2.2 nathanw #include <sys/stat.h>
50 1.3.2.2 nathanw
51 1.3.2.2 nathanw #include <stdio.h>
52 1.3.2.2 nathanw #include <string.h>
53 1.3.2.2 nathanw #include <vis.h>
54 1.3.2.2 nathanw #include <utmpx.h>
55 1.3.2.2 nathanw #include <unistd.h>
56 1.3.2.2 nathanw #include <fcntl.h>
57 1.3.2.2 nathanw
58 1.3.2.2 nathanw static FILE *fp;
59 1.3.2.2 nathanw static struct utmpx ut;
60 1.3.2.2 nathanw static char utfile[MAXPATHLEN] = _PATH_UTMPX;
61 1.3.2.2 nathanw
62 1.3.2.2 nathanw static struct utmpx *utmp_update(const struct utmpx *);
63 1.3.2.2 nathanw
64 1.3.2.2 nathanw static const char vers[] = "utmpx-1.00";
65 1.3.2.2 nathanw
66 1.3.2.2 nathanw void
67 1.3.2.2 nathanw setutxent()
68 1.3.2.2 nathanw {
69 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
70 1.3.2.2 nathanw if (fp == NULL)
71 1.3.2.2 nathanw return;
72 1.3.2.2 nathanw (void)fseeko(fp, (off_t)sizeof(ut), SEEK_SET);
73 1.3.2.2 nathanw }
74 1.3.2.2 nathanw
75 1.3.2.2 nathanw
76 1.3.2.2 nathanw void
77 1.3.2.2 nathanw endutxent()
78 1.3.2.2 nathanw {
79 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
80 1.3.2.2 nathanw if (fp != NULL)
81 1.3.2.2 nathanw (void)fclose(fp);
82 1.3.2.2 nathanw }
83 1.3.2.2 nathanw
84 1.3.2.2 nathanw
85 1.3.2.2 nathanw struct utmpx *
86 1.3.2.2 nathanw getutxent()
87 1.3.2.2 nathanw {
88 1.3.2.2 nathanw if (fp == NULL) {
89 1.3.2.2 nathanw struct stat st;
90 1.3.2.2 nathanw
91 1.3.2.2 nathanw if ((fp = fopen(utfile, "r+")) == NULL)
92 1.3.2.2 nathanw if ((fp = fopen(utfile, "r")) == NULL)
93 1.3.2.2 nathanw goto fail;
94 1.3.2.2 nathanw
95 1.3.2.2 nathanw /* get file size in order to check if new file */
96 1.3.2.2 nathanw if (fstat(fileno(fp), &st) == -1)
97 1.3.2.2 nathanw goto failclose;
98 1.3.2.2 nathanw
99 1.3.2.2 nathanw if (st.st_size == 0) {
100 1.3.2.2 nathanw /* new file, add signature record */
101 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
102 1.3.2.2 nathanw ut.ut_type = SIGNATURE;
103 1.3.2.2 nathanw (void)memcpy(ut.ut_user, vers, sizeof(vers));
104 1.3.2.2 nathanw if (fwrite(&ut, sizeof(ut), 1, fp) != sizeof(ut))
105 1.3.2.2 nathanw goto failclose;
106 1.3.2.2 nathanw } else {
107 1.3.2.2 nathanw /* old file, read signature record */
108 1.3.2.2 nathanw if (fread(&ut, sizeof(ut), 1, fp) != sizeof(ut))
109 1.3.2.2 nathanw goto failclose;
110 1.3.2.2 nathanw if (memcmp(ut.ut_user, vers, sizeof(vers)) != 0 ||
111 1.3.2.2 nathanw ut.ut_type != SIGNATURE)
112 1.3.2.2 nathanw goto failclose;
113 1.3.2.2 nathanw }
114 1.3.2.2 nathanw }
115 1.3.2.2 nathanw
116 1.3.2.2 nathanw if (fread(&ut, sizeof(ut), 1, fp) != sizeof(ut))
117 1.3.2.2 nathanw goto fail;
118 1.3.2.2 nathanw
119 1.3.2.2 nathanw return &ut;
120 1.3.2.2 nathanw failclose:
121 1.3.2.2 nathanw (void)fclose(fp);
122 1.3.2.2 nathanw fail:
123 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
124 1.3.2.2 nathanw return NULL;
125 1.3.2.2 nathanw }
126 1.3.2.2 nathanw
127 1.3.2.2 nathanw
128 1.3.2.2 nathanw struct utmpx *
129 1.3.2.2 nathanw getutxid(const struct utmpx *utx)
130 1.3.2.2 nathanw {
131 1.3.2.2 nathanw if (utx->ut_type == EMPTY)
132 1.3.2.2 nathanw return NULL;
133 1.3.2.2 nathanw
134 1.3.2.2 nathanw do {
135 1.3.2.2 nathanw if (ut.ut_type == EMPTY)
136 1.3.2.2 nathanw continue;
137 1.3.2.2 nathanw switch (utx->ut_type) {
138 1.3.2.2 nathanw case EMPTY:
139 1.3.2.2 nathanw return NULL;
140 1.3.2.2 nathanw case RUN_LVL:
141 1.3.2.2 nathanw case BOOT_TIME:
142 1.3.2.2 nathanw case OLD_TIME:
143 1.3.2.2 nathanw case NEW_TIME:
144 1.3.2.2 nathanw if (ut.ut_type == utx->ut_type)
145 1.3.2.2 nathanw return &ut;
146 1.3.2.2 nathanw break;
147 1.3.2.2 nathanw case INIT_PROCESS:
148 1.3.2.2 nathanw case LOGIN_PROCESS:
149 1.3.2.2 nathanw case USER_PROCESS:
150 1.3.2.2 nathanw case DEAD_PROCESS:
151 1.3.2.2 nathanw switch (ut.ut_type) {
152 1.3.2.2 nathanw case INIT_PROCESS:
153 1.3.2.2 nathanw case LOGIN_PROCESS:
154 1.3.2.2 nathanw case USER_PROCESS:
155 1.3.2.2 nathanw case DEAD_PROCESS:
156 1.3.2.2 nathanw if (memcmp(ut.ut_id, utx->ut_id,
157 1.3.2.2 nathanw sizeof(ut.ut_id)) == 0)
158 1.3.2.2 nathanw return &ut;
159 1.3.2.2 nathanw break;
160 1.3.2.2 nathanw default:
161 1.3.2.2 nathanw break;
162 1.3.2.2 nathanw }
163 1.3.2.2 nathanw break;
164 1.3.2.2 nathanw default:
165 1.3.2.2 nathanw return NULL;
166 1.3.2.2 nathanw }
167 1.3.2.2 nathanw }
168 1.3.2.2 nathanw while (getutxent() != NULL);
169 1.3.2.2 nathanw return NULL;
170 1.3.2.2 nathanw }
171 1.3.2.2 nathanw
172 1.3.2.2 nathanw
173 1.3.2.2 nathanw struct utmpx *
174 1.3.2.2 nathanw getutxline(const struct utmpx *utx)
175 1.3.2.2 nathanw {
176 1.3.2.2 nathanw do {
177 1.3.2.2 nathanw switch (ut.ut_type) {
178 1.3.2.2 nathanw case EMPTY:
179 1.3.2.2 nathanw break;
180 1.3.2.2 nathanw case LOGIN_PROCESS:
181 1.3.2.2 nathanw case USER_PROCESS:
182 1.3.2.2 nathanw if (strncmp(ut.ut_line, utx->ut_line,
183 1.3.2.2 nathanw sizeof(ut.ut_line)) == 0)
184 1.3.2.2 nathanw return &ut;
185 1.3.2.2 nathanw break;
186 1.3.2.2 nathanw default:
187 1.3.2.2 nathanw break;
188 1.3.2.2 nathanw }
189 1.3.2.2 nathanw }
190 1.3.2.2 nathanw while (getutxent() != NULL);
191 1.3.2.2 nathanw return NULL;
192 1.3.2.2 nathanw }
193 1.3.2.2 nathanw
194 1.3.2.2 nathanw
195 1.3.2.2 nathanw struct utmpx *
196 1.3.2.2 nathanw pututxline(const struct utmpx *utx)
197 1.3.2.2 nathanw {
198 1.3.2.2 nathanw struct utmpx temp, *u = NULL;
199 1.3.2.2 nathanw int gotlock = 0;
200 1.3.2.2 nathanw
201 1.3.2.2 nathanw if (strcmp(_PATH_UTMPX, utfile) == 0 && geteuid() != 0)
202 1.3.2.2 nathanw return utmp_update(utx);
203 1.3.2.2 nathanw
204 1.3.2.2 nathanw if (utx == NULL)
205 1.3.2.2 nathanw return NULL;
206 1.3.2.2 nathanw
207 1.3.2.2 nathanw (void)memcpy(&temp, utx, sizeof(temp));
208 1.3.2.2 nathanw
209 1.3.2.2 nathanw if (fp == NULL) {
210 1.3.2.2 nathanw (void)getutxent();
211 1.3.2.2 nathanw if (fp == NULL)
212 1.3.2.2 nathanw return NULL;
213 1.3.2.2 nathanw }
214 1.3.2.2 nathanw
215 1.3.2.2 nathanw if (getutxid(&temp) == NULL) {
216 1.3.2.2 nathanw setutxent();
217 1.3.2.2 nathanw if (getutxid(&temp) == NULL) {
218 1.3.2.2 nathanw if (lockf(fileno(fp), F_LOCK, (off_t)0) == -1)
219 1.3.2.2 nathanw return NULL;
220 1.3.2.2 nathanw gotlock++;
221 1.3.2.2 nathanw if (fseeko(fp, (off_t)0, SEEK_END) == -1)
222 1.3.2.2 nathanw goto fail;
223 1.3.2.2 nathanw }
224 1.3.2.2 nathanw }
225 1.3.2.2 nathanw
226 1.3.2.2 nathanw if (!gotlock) {
227 1.3.2.2 nathanw /* we are not appending */
228 1.3.2.2 nathanw if (fseeko(fp, -(off_t)sizeof(ut), SEEK_CUR) == -1)
229 1.3.2.2 nathanw return NULL;
230 1.3.2.2 nathanw }
231 1.3.2.2 nathanw
232 1.3.2.2 nathanw if (fwrite(&temp, sizeof (temp), 1, fp) != 1)
233 1.3.2.2 nathanw goto fail;
234 1.3.2.2 nathanw
235 1.3.2.2 nathanw if (fflush(fp) == -1)
236 1.3.2.2 nathanw goto fail;
237 1.3.2.2 nathanw
238 1.3.2.2 nathanw u = memcpy(&ut, &temp, sizeof(ut));
239 1.3.2.2 nathanw fail:
240 1.3.2.2 nathanw if (gotlock) {
241 1.3.2.2 nathanw if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1)
242 1.3.2.2 nathanw return NULL;
243 1.3.2.2 nathanw }
244 1.3.2.2 nathanw return u;
245 1.3.2.2 nathanw }
246 1.3.2.2 nathanw
247 1.3.2.2 nathanw
248 1.3.2.2 nathanw static struct utmpx *
249 1.3.2.2 nathanw utmp_update(const struct utmpx *utx)
250 1.3.2.2 nathanw {
251 1.3.2.2 nathanw char buf[sizeof(*utx) * 4 + 1];
252 1.3.2.2 nathanw pid_t pid;
253 1.3.2.2 nathanw int status;
254 1.3.2.2 nathanw
255 1.3.2.2 nathanw (void)strvisx(buf, (const char *)(const void *)utx, sizeof(*utx),
256 1.3.2.2 nathanw VIS_WHITE);
257 1.3.2.2 nathanw switch (pid = fork()) {
258 1.3.2.2 nathanw case 0:
259 1.3.2.2 nathanw (void)execl(_PATH_UTMP_UPDATE,
260 1.3.2.2 nathanw strrchr(_PATH_UTMP_UPDATE, '/') + 1, buf);
261 1.3.2.2 nathanw exit(1);
262 1.3.2.2 nathanw /*NOTREACHED*/
263 1.3.2.2 nathanw case -1:
264 1.3.2.2 nathanw return NULL;
265 1.3.2.2 nathanw default:
266 1.3.2.2 nathanw if (waitpid(pid, &status, 0) == -1)
267 1.3.2.2 nathanw return NULL;
268 1.3.2.2 nathanw if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
269 1.3.2.2 nathanw return memcpy(&ut, utx, sizeof(ut));
270 1.3.2.2 nathanw return NULL;
271 1.3.2.2 nathanw }
272 1.3.2.2 nathanw
273 1.3.2.2 nathanw }
274 1.3.2.2 nathanw
275 1.3.2.2 nathanw void
276 1.3.2.2 nathanw updwtmpx(const char *file, const struct utmpx *utx)
277 1.3.2.2 nathanw {
278 1.3.2.2 nathanw int fd = open(file, O_WRONLY | O_APPEND);
279 1.3.2.2 nathanw if (fd == -1) {
280 1.3.2.2 nathanw if ((fd = open(file, O_CREAT | O_WRONLY, 0644)) == -1)
281 1.3.2.2 nathanw return;
282 1.3.2.2 nathanw (void)memset(&ut, 0, sizeof(ut));
283 1.3.2.2 nathanw ut.ut_type = SIGNATURE;
284 1.3.2.2 nathanw (void)memcpy(ut.ut_user, vers, sizeof(vers));
285 1.3.2.2 nathanw (void)write(fd, &ut, sizeof(ut));
286 1.3.2.2 nathanw }
287 1.3.2.2 nathanw (void)write(fd, utx, sizeof(*utx));
288 1.3.2.2 nathanw (void)close(fd);
289 1.3.2.2 nathanw }
290 1.3.2.2 nathanw
291 1.3.2.2 nathanw
292 1.3.2.2 nathanw /*
293 1.3.2.2 nathanw * The following are extensions and not part of the X/Open spec
294 1.3.2.2 nathanw */
295 1.3.2.2 nathanw int
296 1.3.2.2 nathanw utmpxname(const char *fname)
297 1.3.2.2 nathanw {
298 1.3.2.2 nathanw size_t len = strlen(fname);
299 1.3.2.2 nathanw
300 1.3.2.2 nathanw if (len >= sizeof(utfile))
301 1.3.2.2 nathanw return 0;
302 1.3.2.2 nathanw
303 1.3.2.2 nathanw /* must end in x! */
304 1.3.2.2 nathanw if (fname[len - 1] != 'x')
305 1.3.2.2 nathanw return 0;
306 1.3.2.2 nathanw
307 1.3.2.2 nathanw (void)strcpy(utfile, fname);
308 1.3.2.2 nathanw endutxent();
309 1.3.2.2 nathanw return 1;
310 1.3.2.2 nathanw }
311