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