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