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