utmpx.c revision 1.7 1 /* $NetBSD: utmpx.c,v 1.7 2002/07/23 00:03:36 simonb 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.7 2002/07/23 00:03:36 simonb 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 }
87
88
89 struct utmpx *
90 getutxent()
91 {
92
93 if (fp == NULL) {
94 struct stat st;
95
96 if ((fp = fopen(utfile, "r+")) == NULL)
97 if ((fp = fopen(utfile, "w+")) == NULL)
98 if ((fp = fopen(utfile, "r")) == NULL)
99 goto fail;
100
101 /* get file size in order to check if new file */
102 if (fstat(fileno(fp), &st) == -1)
103 goto failclose;
104
105 if (st.st_size == 0) {
106 /* new file, add signature record */
107 (void)memset(&ut, 0, sizeof(ut));
108 ut.ut_type = SIGNATURE;
109 (void)memcpy(ut.ut_user, vers, sizeof(vers));
110 if (fwrite(&ut, sizeof(ut), 1, fp) != 1)
111 goto failclose;
112 } else {
113 /* old file, read signature record */
114 if (fread(&ut, sizeof(ut), 1, fp) != 1)
115 goto failclose;
116 if (memcmp(ut.ut_user, vers, sizeof(vers)) != 0 ||
117 ut.ut_type != SIGNATURE)
118 goto failclose;
119 }
120 }
121
122 if (fread(&ut, sizeof(ut), 1, fp) != 1)
123 goto fail;
124
125 return &ut;
126 failclose:
127 (void)fclose(fp);
128 fail:
129 (void)memset(&ut, 0, sizeof(ut));
130 return NULL;
131 }
132
133
134 struct utmpx *
135 getutxid(const struct utmpx *utx)
136 {
137
138 if (utx->ut_type == EMPTY)
139 return NULL;
140
141 do {
142 if (ut.ut_type == EMPTY)
143 continue;
144 switch (utx->ut_type) {
145 case EMPTY:
146 return NULL;
147 case RUN_LVL:
148 case BOOT_TIME:
149 case OLD_TIME:
150 case NEW_TIME:
151 if (ut.ut_type == utx->ut_type)
152 return &ut;
153 break;
154 case INIT_PROCESS:
155 case LOGIN_PROCESS:
156 case USER_PROCESS:
157 case DEAD_PROCESS:
158 switch (ut.ut_type) {
159 case INIT_PROCESS:
160 case LOGIN_PROCESS:
161 case USER_PROCESS:
162 case DEAD_PROCESS:
163 if (memcmp(ut.ut_id, utx->ut_id,
164 sizeof(ut.ut_id)) == 0)
165 return &ut;
166 break;
167 default:
168 break;
169 }
170 break;
171 default:
172 return NULL;
173 }
174 } while (getutxent() != NULL);
175 return NULL;
176 }
177
178
179 struct utmpx *
180 getutxline(const struct utmpx *utx)
181 {
182
183 do {
184 switch (ut.ut_type) {
185 case EMPTY:
186 break;
187 case LOGIN_PROCESS:
188 case USER_PROCESS:
189 if (strncmp(ut.ut_line, utx->ut_line,
190 sizeof(ut.ut_line)) == 0)
191 return &ut;
192 break;
193 default:
194 break;
195 }
196 } while (getutxent() != NULL);
197 return NULL;
198 }
199
200
201 struct utmpx *
202 pututxline(const struct utmpx *utx)
203 {
204 struct utmpx temp, *u = NULL;
205 int gotlock = 0;
206
207 if (strcmp(_PATH_UTMPX, utfile) == 0 && geteuid() != 0)
208 return utmp_update(utx);
209
210 if (utx == NULL)
211 return NULL;
212
213 (void)memcpy(&temp, utx, sizeof(temp));
214
215 if (fp == NULL) {
216 (void)getutxent();
217 if (fp == NULL)
218 return NULL;
219 }
220
221 if (getutxid(&temp) == NULL) {
222 setutxent();
223 if (getutxid(&temp) == NULL) {
224 if (lockf(fileno(fp), F_LOCK, (off_t)0) == -1)
225 return NULL;
226 gotlock++;
227 if (fseeko(fp, (off_t)0, SEEK_END) == -1)
228 goto fail;
229 }
230 }
231
232 if (!gotlock) {
233 /* we are not appending */
234 if (fseeko(fp, -(off_t)sizeof(ut), SEEK_CUR) == -1)
235 return NULL;
236 }
237
238 if (fwrite(&temp, sizeof (temp), 1, fp) != 1)
239 goto fail;
240
241 if (fflush(fp) == -1)
242 goto fail;
243
244 u = memcpy(&ut, &temp, sizeof(ut));
245 fail:
246 if (gotlock) {
247 if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1)
248 return NULL;
249 }
250 return u;
251 }
252
253
254 static struct utmpx *
255 utmp_update(const struct utmpx *utx)
256 {
257 char buf[sizeof(*utx) * 4 + 1];
258 pid_t pid;
259 int status;
260
261 (void)strvisx(buf, (const char *)(const void *)utx, sizeof(*utx),
262 VIS_WHITE);
263 switch (pid = fork()) {
264 case 0:
265 (void)execl(_PATH_UTMP_UPDATE,
266 strrchr(_PATH_UTMP_UPDATE, '/') + 1, buf);
267 exit(1);
268 /*NOTREACHED*/
269 case -1:
270 return NULL;
271 default:
272 if (waitpid(pid, &status, 0) == -1)
273 return NULL;
274 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
275 return memcpy(&ut, utx, sizeof(ut));
276 return NULL;
277 }
278
279 }
280
281 void
282 updwtmpx(const char *file, const struct utmpx *utx)
283 {
284 int fd = open(file, O_WRONLY | O_APPEND);
285
286 if (fd == -1) {
287 if ((fd = open(file, O_CREAT | O_WRONLY, 0644)) == -1)
288 return;
289 (void)memset(&ut, 0, sizeof(ut));
290 ut.ut_type = SIGNATURE;
291 (void)memcpy(ut.ut_user, vers, sizeof(vers));
292 (void)write(fd, &ut, sizeof(ut));
293 }
294 (void)write(fd, utx, sizeof(*utx));
295 (void)close(fd);
296 }
297
298
299 /*
300 * The following are extensions and not part of the X/Open spec
301 */
302 int
303 utmpxname(const char *fname)
304 {
305 size_t len = strlen(fname);
306
307 if (len >= sizeof(utfile))
308 return 0;
309
310 /* must end in x! */
311 if (fname[len - 1] != 'x')
312 return 0;
313
314 (void)strcpy(utfile, fname);
315 endutxent();
316 return 1;
317 }
318
319
320 void
321 getutmp(const struct utmpx *ux, struct utmp *u)
322 {
323
324 (void)memcpy(u->ut_name, ux->ut_name, sizeof(u->ut_name));
325 (void)memcpy(u->ut_line, ux->ut_line, sizeof(u->ut_line));
326 (void)memcpy(u->ut_host, ux->ut_host, sizeof(u->ut_host));
327 u->ut_time = ux->ut_tv.tv_sec;
328 }
329
330 void
331 getutmpx(const struct utmp *u, struct utmpx *ux)
332 {
333
334 (void)memcpy(ux->ut_name, u->ut_name, sizeof(u->ut_name));
335 (void)memcpy(ux->ut_line, u->ut_line, sizeof(u->ut_line));
336 (void)memcpy(ux->ut_host, u->ut_host, sizeof(u->ut_host));
337 ux->ut_tv.tv_sec = u->ut_time;
338 ux->ut_tv.tv_usec = 0;
339 (void)memset(&ux->ut_ss, 0, sizeof(ux->ut_ss));
340 ux->ut_pid = 0;
341 ux->ut_type = USER_PROCESS;
342 ux->ut_session = 0;
343 ux->ut_exit.e_termination = 0;
344 ux->ut_exit.e_exit = 0;
345 }
346