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