Home | History | Annotate | Line # | Download | only in gen
utmpx.c revision 1.19
      1 /*	$NetBSD: utmpx.c,v 1.19 2003/08/25 23:09:37 matt 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.19 2003/08/25 23:09:37 matt Exp $");
     42 #endif /* LIBC_SCCS and not lint */
     43 
     44 #include "namespace.h"
     45 #include <sys/types.h>
     46 #include <sys/param.h>
     47 #include <sys/wait.h>
     48 #include <sys/socket.h>
     49 #include <sys/time.h>
     50 #include <sys/stat.h>
     51 
     52 #include <assert.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <vis.h>
     57 #include <utmp.h>
     58 #include <utmpx.h>
     59 #include <unistd.h>
     60 #include <fcntl.h>
     61 #include <errno.h>
     62 #include <db.h>
     63 
     64 static FILE *fp;
     65 static int readonly = 0;
     66 static struct utmpx ut;
     67 static char utfile[MAXPATHLEN] = _PATH_UTMPX;
     68 static char llfile[MAXPATHLEN] = _PATH_LASTLOGX;
     69 
     70 static struct utmpx *utmp_update(const struct utmpx *);
     71 
     72 static const char vers[] = "utmpx-1.00";
     73 
     74 void
     75 setutxent()
     76 {
     77 
     78 	(void)memset(&ut, 0, sizeof(ut));
     79 	if (fp == NULL)
     80 		return;
     81 	(void)fseeko(fp, (off_t)sizeof(ut), SEEK_SET);
     82 }
     83 
     84 
     85 void
     86 endutxent()
     87 {
     88 
     89 	(void)memset(&ut, 0, sizeof(ut));
     90 	if (fp != NULL) {
     91 		(void)fclose(fp);
     92 		fp = NULL;
     93 		readonly = 0;
     94 	}
     95 }
     96 
     97 
     98 struct utmpx *
     99 getutxent()
    100 {
    101 
    102 	if (fp == NULL) {
    103 		struct stat st;
    104 
    105 		if ((fp = fopen(utfile, "r+")) == NULL)
    106 			if ((fp = fopen(utfile, "w+")) == NULL) {
    107 				if ((fp = fopen(utfile, "r")) == NULL)
    108 					goto fail;
    109 				else
    110 					readonly = 1;
    111 			}
    112 
    113 
    114 		/* get file size in order to check if new file */
    115 		if (fstat(fileno(fp), &st) == -1)
    116 			goto failclose;
    117 
    118 		if (st.st_size == 0) {
    119 			/* new file, add signature record */
    120 			(void)memset(&ut, 0, sizeof(ut));
    121 			ut.ut_type = SIGNATURE;
    122 			(void)memcpy(ut.ut_user, vers, sizeof(vers));
    123 			if (fwrite(&ut, sizeof(ut), 1, fp) != 1)
    124 				goto failclose;
    125 		} else {
    126 			/* old file, read signature record */
    127 			if (fread(&ut, sizeof(ut), 1, fp) != 1)
    128 				goto failclose;
    129 			if (memcmp(ut.ut_user, vers, sizeof(vers)) != 0 ||
    130 			    ut.ut_type != SIGNATURE)
    131 				goto failclose;
    132 		}
    133 	}
    134 
    135 	if (fread(&ut, sizeof(ut), 1, fp) != 1)
    136 		goto fail;
    137 
    138 	return &ut;
    139 failclose:
    140 	(void)fclose(fp);
    141 fail:
    142 	(void)memset(&ut, 0, sizeof(ut));
    143 	return NULL;
    144 }
    145 
    146 
    147 struct utmpx *
    148 getutxid(const struct utmpx *utx)
    149 {
    150 
    151 	_DIAGASSERT(utx != NULL);
    152 
    153 	if (utx->ut_type == EMPTY)
    154 		return NULL;
    155 
    156 	do {
    157 		if (ut.ut_type == EMPTY)
    158 			continue;
    159 		switch (utx->ut_type) {
    160 		case EMPTY:
    161 			return NULL;
    162 		case RUN_LVL:
    163 		case BOOT_TIME:
    164 		case OLD_TIME:
    165 		case NEW_TIME:
    166 			if (ut.ut_type == utx->ut_type)
    167 				return &ut;
    168 			break;
    169 		case INIT_PROCESS:
    170 		case LOGIN_PROCESS:
    171 		case USER_PROCESS:
    172 		case DEAD_PROCESS:
    173 			switch (ut.ut_type) {
    174 			case INIT_PROCESS:
    175 			case LOGIN_PROCESS:
    176 			case USER_PROCESS:
    177 			case DEAD_PROCESS:
    178 				if (memcmp(ut.ut_id, utx->ut_id,
    179 				    sizeof(ut.ut_id)) == 0)
    180 					return &ut;
    181 				break;
    182 			default:
    183 				break;
    184 			}
    185 			break;
    186 		default:
    187 			return NULL;
    188 		}
    189 	} while (getutxent() != NULL);
    190 	return NULL;
    191 }
    192 
    193 
    194 struct utmpx *
    195 getutxline(const struct utmpx *utx)
    196 {
    197 
    198 	_DIAGASSERT(utx != NULL);
    199 
    200 	do {
    201 		switch (ut.ut_type) {
    202 		case EMPTY:
    203 			break;
    204 		case LOGIN_PROCESS:
    205 		case USER_PROCESS:
    206 			if (strncmp(ut.ut_line, utx->ut_line,
    207 			    sizeof(ut.ut_line)) == 0)
    208 				return &ut;
    209 			break;
    210 		default:
    211 			break;
    212 		}
    213 	} while (getutxent() != NULL);
    214 	return NULL;
    215 }
    216 
    217 
    218 struct utmpx *
    219 pututxline(const struct utmpx *utx)
    220 {
    221 	struct utmpx temp, *u = NULL;
    222 	int gotlock = 0;
    223 
    224 	_DIAGASSERT(utx != NULL);
    225 
    226 	if (utx == NULL)
    227 		return NULL;
    228 
    229 	if (strcmp(_PATH_UTMPX, utfile) == 0)
    230 		if ((fp != NULL && readonly) || (fp == NULL && geteuid() != 0))
    231 			return utmp_update(utx);
    232 
    233 
    234 	(void)memcpy(&temp, utx, sizeof(temp));
    235 
    236 	if (fp == NULL) {
    237 		(void)getutxent();
    238 		if (fp == NULL || readonly)
    239 			return NULL;
    240 	}
    241 
    242 	if (getutxid(&temp) == NULL) {
    243 		setutxent();
    244 		if (getutxid(&temp) == NULL) {
    245 			if (lockf(fileno(fp), F_LOCK, (off_t)0) == -1)
    246 				return NULL;
    247 			gotlock++;
    248 			if (fseeko(fp, (off_t)0, SEEK_END) == -1)
    249 				goto fail;
    250 		}
    251 	}
    252 
    253 	if (!gotlock) {
    254 		/* we are not appending */
    255 		if (fseeko(fp, -(off_t)sizeof(ut), SEEK_CUR) == -1)
    256 			return NULL;
    257 	}
    258 
    259 	if (fwrite(&temp, sizeof (temp), 1, fp) != 1)
    260 		goto fail;
    261 
    262 	if (fflush(fp) == -1)
    263 		goto fail;
    264 
    265 	u = memcpy(&ut, &temp, sizeof(ut));
    266 fail:
    267 	if (gotlock) {
    268 		if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1)
    269 			return NULL;
    270 	}
    271 	return u;
    272 }
    273 
    274 
    275 static struct utmpx *
    276 utmp_update(const struct utmpx *utx)
    277 {
    278 	char buf[sizeof(*utx) * 4 + 1];
    279 	pid_t pid;
    280 	int status;
    281 
    282 	_DIAGASSERT(utx != NULL);
    283 
    284 	(void)strvisx(buf, (const char *)(const void *)utx, sizeof(*utx),
    285 	    VIS_WHITE);
    286 	switch (pid = fork()) {
    287 	case 0:
    288 		(void)execl(_PATH_UTMP_UPDATE,
    289 		    strrchr(_PATH_UTMP_UPDATE, '/') + 1, buf, NULL);
    290 		exit(1);
    291 		/*NOTREACHED*/
    292 	case -1:
    293 		return NULL;
    294 	default:
    295 		if (waitpid(pid, &status, 0) == -1)
    296 			return NULL;
    297 		if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
    298 			return memcpy(&ut, utx, sizeof(ut));
    299 		return NULL;
    300 	}
    301 
    302 }
    303 
    304 /*
    305  * The following are extensions and not part of the X/Open spec.
    306  */
    307 int
    308 updwtmpx(const char *file, const struct utmpx *utx)
    309 {
    310 	int fd;
    311 	int saved_errno;
    312 
    313 	_DIAGASSERT(file != NULL);
    314 	_DIAGASSERT(utx != NULL);
    315 
    316 	fd = open(file, O_WRONLY|O_APPEND|O_SHLOCK);
    317 
    318 	if (fd == -1) {
    319 		if ((fd = open(file, O_CREAT|O_WRONLY|O_EXLOCK, 0644)) == -1)
    320 			return -1;
    321 		(void)memset(&ut, 0, sizeof(ut));
    322 		ut.ut_type = SIGNATURE;
    323 		(void)memcpy(ut.ut_user, vers, sizeof(vers));
    324 		if (write(fd, &ut, sizeof(ut)) == -1)
    325 			goto failed;
    326 	}
    327 	if (write(fd, utx, sizeof(*utx)) == -1)
    328 		goto failed;
    329 	if (close(fd) == -1)
    330 		return -1;
    331 	return 0;
    332 
    333   failed:
    334 	saved_errno = errno;
    335 	(void) close(fd);
    336 	errno = saved_errno;
    337 	return -1;
    338 }
    339 
    340 
    341 int
    342 utmpxname(const char *fname)
    343 {
    344 	size_t len;
    345 
    346 	_DIAGASSERT(fname != NULL);
    347 
    348 	len = strlen(fname);
    349 
    350 	if (len >= sizeof(utfile))
    351 		return 0;
    352 
    353 	/* must end in x! */
    354 	if (fname[len - 1] != 'x')
    355 		return 0;
    356 
    357 	(void)strlcpy(utfile, fname, sizeof(utfile));
    358 	endutxent();
    359 	return 1;
    360 }
    361 
    362 
    363 void
    364 getutmp(const struct utmpx *ux, struct utmp *u)
    365 {
    366 
    367 	_DIAGASSERT(ux != NULL);
    368 	_DIAGASSERT(u != NULL);
    369 
    370 	(void)memcpy(u->ut_name, ux->ut_name, sizeof(u->ut_name));
    371 	(void)memcpy(u->ut_line, ux->ut_line, sizeof(u->ut_line));
    372 	(void)memcpy(u->ut_host, ux->ut_host, sizeof(u->ut_host));
    373 	u->ut_time = ux->ut_tv.tv_sec;
    374 }
    375 
    376 void
    377 getutmpx(const struct utmp *u, struct utmpx *ux)
    378 {
    379 
    380 	_DIAGASSERT(ux != NULL);
    381 	_DIAGASSERT(u != NULL);
    382 
    383 	(void)memcpy(ux->ut_name, u->ut_name, sizeof(u->ut_name));
    384 	(void)memcpy(ux->ut_line, u->ut_line, sizeof(u->ut_line));
    385 	(void)memcpy(ux->ut_host, u->ut_host, sizeof(u->ut_host));
    386 	ux->ut_tv.tv_sec = u->ut_time;
    387 	ux->ut_tv.tv_usec = 0;
    388 	(void)memset(&ux->ut_ss, 0, sizeof(ux->ut_ss));
    389 	ux->ut_pid = 0;
    390 	ux->ut_type = USER_PROCESS;
    391 	ux->ut_session = 0;
    392 	ux->ut_exit.e_termination = 0;
    393 	ux->ut_exit.e_exit = 0;
    394 }
    395 
    396 int
    397 lastlogxname(const char *fname)
    398 {
    399 	size_t len;
    400 
    401 	_DIAGASSERT(fname != NULL);
    402 
    403 	len = strlen(fname);
    404 
    405 	if (len >= sizeof(llfile))
    406 		return 0;
    407 
    408 	/* must end in x! */
    409 	if (fname[len - 1] != 'x')
    410 		return 0;
    411 
    412 	(void)strlcpy(llfile, fname, sizeof(llfile));
    413 	return 1;
    414 }
    415 
    416 struct lastlogx *
    417 getlastlogx(uid_t uid, struct lastlogx *ll)
    418 {
    419 	DBT key, data;
    420 	DB *db;
    421 
    422 	_DIAGASSERT(ll != NULL);
    423 
    424 	db = dbopen(llfile, O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL);
    425 
    426 	if (db == NULL)
    427 		return NULL;
    428 
    429 	key.data = &uid;
    430 	key.size = sizeof(uid);
    431 
    432 	if ((db->get)(db, &key, &data, 0) != 0)
    433 		goto error;
    434 
    435 	if (data.size != sizeof(*ll)) {
    436 		errno = EFTYPE;
    437 		goto error;
    438 	}
    439 
    440 	if (ll == NULL)
    441 		if ((ll = malloc(sizeof(*ll))) == NULL)
    442 			goto done;
    443 
    444 	(void)memcpy(ll, data.data, sizeof(*ll));
    445 	goto done;
    446 error:
    447 	ll = NULL;
    448 done:
    449 	(db->close)(db);
    450 	return ll;
    451 }
    452 
    453 int
    454 updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll)
    455 {
    456 	DBT key, data;
    457 	int error = 0;
    458 	DB *db;
    459 
    460 	_DIAGASSERT(fname != NULL);
    461 	_DIAGASSERT(ll != NULL);
    462 
    463 	db = dbopen(fname, O_RDWR|O_CREAT|O_EXLOCK, 0, DB_HASH, NULL);
    464 
    465 	if (db == NULL)
    466 		return -1;
    467 
    468 	key.data = &uid;
    469 	key.size = sizeof(uid);
    470 	data.data = ll;
    471 	data.size = sizeof(*ll);
    472 	if ((db->put)(db, &key, &data, 0) != 0)
    473 		error = -1;
    474 
    475 	(db->close)(db);
    476 	return error;
    477 }
    478