Home | History | Annotate | Line # | Download | only in gen
utmpx.c revision 1.3
      1  1.3  christos /*	$NetBSD: utmpx.c,v 1.3 2002/03/05 16:16:02 christos Exp $	 */
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Christos Zoulas.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     19  1.1  christos  *    must display the following acknowledgement:
     20  1.1  christos  *        This product includes software developed by the NetBSD
     21  1.1  christos  *        Foundation, Inc. and its contributors.
     22  1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  christos  *    contributors may be used to endorse or promote products derived
     24  1.1  christos  *    from this software without specific prior written permission.
     25  1.1  christos  *
     26  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  christos  */
     38  1.1  christos #include <sys/cdefs.h>
     39  1.1  christos 
     40  1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     41  1.3  christos __RCSID("$NetBSD: utmpx.c,v 1.3 2002/03/05 16:16:02 christos Exp $");
     42  1.1  christos #endif /* LIBC_SCCS and not lint */
     43  1.1  christos 
     44  1.1  christos #include <sys/types.h>
     45  1.1  christos #include <sys/param.h>
     46  1.1  christos #include <sys/wait.h>
     47  1.1  christos #include <sys/socket.h>
     48  1.1  christos #include <sys/time.h>
     49  1.1  christos #include <sys/stat.h>
     50  1.1  christos 
     51  1.1  christos #include <stdio.h>
     52  1.1  christos #include <string.h>
     53  1.1  christos #include <vis.h>
     54  1.1  christos #include <utmpx.h>
     55  1.1  christos #include <unistd.h>
     56  1.3  christos #include <fcntl.h>
     57  1.1  christos 
     58  1.1  christos static FILE *fp;
     59  1.1  christos static struct utmpx ut;
     60  1.1  christos static char utfile[MAXPATHLEN] = _PATH_UTMPX;
     61  1.1  christos 
     62  1.1  christos static struct utmpx *utmp_update(const struct utmpx *);
     63  1.1  christos 
     64  1.1  christos static const char vers[] = "utmpx-1.00";
     65  1.1  christos 
     66  1.1  christos void
     67  1.1  christos setutxent()
     68  1.1  christos {
     69  1.1  christos 	(void)memset(&ut, 0, sizeof(ut));
     70  1.1  christos 	if (fp == NULL)
     71  1.1  christos 		return;
     72  1.1  christos 	(void)fseeko(fp, (off_t)sizeof(ut), SEEK_SET);
     73  1.1  christos }
     74  1.1  christos 
     75  1.1  christos 
     76  1.1  christos void
     77  1.1  christos endutxent()
     78  1.1  christos {
     79  1.1  christos 	(void)memset(&ut, 0, sizeof(ut));
     80  1.1  christos 	if (fp != NULL)
     81  1.1  christos 		(void)fclose(fp);
     82  1.1  christos }
     83  1.1  christos 
     84  1.1  christos 
     85  1.1  christos struct utmpx *
     86  1.1  christos getutxent()
     87  1.1  christos {
     88  1.1  christos 	if (fp == NULL) {
     89  1.1  christos 		struct stat st;
     90  1.1  christos 
     91  1.1  christos 		if ((fp = fopen(utfile, "r+")) == NULL)
     92  1.1  christos 			if ((fp = fopen(utfile, "r")) == NULL)
     93  1.1  christos 				goto fail;
     94  1.1  christos 
     95  1.1  christos 		/* get file size in order to check if new file */
     96  1.1  christos 		if (fstat(fileno(fp), &st) == -1)
     97  1.1  christos 			goto failclose;
     98  1.1  christos 
     99  1.1  christos 		if (st.st_size == 0) {
    100  1.1  christos 			/* new file, add signature record */
    101  1.1  christos 			(void)memset(&ut, 0, sizeof(ut));
    102  1.1  christos 			ut.ut_type = SIGNATURE;
    103  1.2  christos 			(void)memcpy(ut.ut_user, vers, sizeof(vers));
    104  1.1  christos 			if (fwrite(&ut, sizeof(ut), 1, fp) != sizeof(ut))
    105  1.1  christos 				goto failclose;
    106  1.1  christos 		} else {
    107  1.1  christos 			/* old file, read signature record */
    108  1.1  christos 			if (fread(&ut, sizeof(ut), 1, fp) != sizeof(ut))
    109  1.1  christos 				goto failclose;
    110  1.2  christos 			if (memcmp(ut.ut_user, vers, sizeof(vers)) != 0 ||
    111  1.1  christos 			    ut.ut_type != SIGNATURE)
    112  1.1  christos 				goto failclose;
    113  1.1  christos 		}
    114  1.1  christos 	}
    115  1.1  christos 
    116  1.1  christos 	if (fread(&ut, sizeof(ut), 1, fp) != sizeof(ut))
    117  1.1  christos 		goto fail;
    118  1.1  christos 
    119  1.1  christos 	return &ut;
    120  1.1  christos failclose:
    121  1.1  christos 	(void)fclose(fp);
    122  1.1  christos fail:
    123  1.1  christos 	(void)memset(&ut, 0, sizeof(ut));
    124  1.1  christos 	return NULL;
    125  1.1  christos }
    126  1.1  christos 
    127  1.1  christos 
    128  1.1  christos struct utmpx *
    129  1.1  christos getutxid(const struct utmpx *utx)
    130  1.1  christos {
    131  1.1  christos 	if (utx->ut_type == EMPTY)
    132  1.1  christos 		return NULL;
    133  1.1  christos 
    134  1.1  christos 	do {
    135  1.1  christos 		if (ut.ut_type == EMPTY)
    136  1.1  christos 			continue;
    137  1.1  christos 		switch (utx->ut_type) {
    138  1.1  christos 		case EMPTY:
    139  1.1  christos 			return NULL;
    140  1.1  christos 		case RUN_LVL:
    141  1.1  christos 		case BOOT_TIME:
    142  1.1  christos 		case OLD_TIME:
    143  1.1  christos 		case NEW_TIME:
    144  1.1  christos 			if (ut.ut_type == utx->ut_type)
    145  1.1  christos 				return &ut;
    146  1.1  christos 			break;
    147  1.1  christos 		case INIT_PROCESS:
    148  1.1  christos 		case LOGIN_PROCESS:
    149  1.1  christos 		case USER_PROCESS:
    150  1.1  christos 		case DEAD_PROCESS:
    151  1.1  christos 			switch (ut.ut_type) {
    152  1.1  christos 			case INIT_PROCESS:
    153  1.1  christos 			case LOGIN_PROCESS:
    154  1.1  christos 			case USER_PROCESS:
    155  1.1  christos 			case DEAD_PROCESS:
    156  1.1  christos 				if (memcmp(ut.ut_id, utx->ut_id,
    157  1.1  christos 				    sizeof(ut.ut_id)) == 0)
    158  1.1  christos 					return &ut;
    159  1.1  christos 				break;
    160  1.1  christos 			default:
    161  1.1  christos 				break;
    162  1.1  christos 			}
    163  1.1  christos 			break;
    164  1.1  christos 		default:
    165  1.1  christos 			return NULL;
    166  1.1  christos 		}
    167  1.1  christos 	}
    168  1.1  christos 	while (getutxent() != NULL);
    169  1.1  christos 	return NULL;
    170  1.1  christos }
    171  1.1  christos 
    172  1.1  christos 
    173  1.1  christos struct utmpx *
    174  1.1  christos getutxline(const struct utmpx *utx)
    175  1.1  christos {
    176  1.1  christos 	do {
    177  1.1  christos 		switch (ut.ut_type) {
    178  1.1  christos 		case EMPTY:
    179  1.1  christos 			break;
    180  1.1  christos 		case LOGIN_PROCESS:
    181  1.1  christos 		case USER_PROCESS:
    182  1.1  christos 			if (strncmp(ut.ut_line, utx->ut_line,
    183  1.1  christos 			    sizeof(ut.ut_line)) == 0)
    184  1.1  christos 				return &ut;
    185  1.1  christos 			break;
    186  1.1  christos 		default:
    187  1.1  christos 			break;
    188  1.1  christos 		}
    189  1.1  christos 	}
    190  1.1  christos 	while (getutxent() != NULL);
    191  1.1  christos 	return NULL;
    192  1.1  christos }
    193  1.1  christos 
    194  1.1  christos 
    195  1.1  christos struct utmpx *
    196  1.1  christos pututxline(const struct utmpx *utx)
    197  1.1  christos {
    198  1.1  christos 	struct utmpx temp, *u = NULL;
    199  1.1  christos 	int gotlock = 0;
    200  1.1  christos 
    201  1.1  christos 	if (strcmp(_PATH_UTMPX, utfile) == 0 && geteuid() != 0)
    202  1.1  christos 		return utmp_update(utx);
    203  1.1  christos 
    204  1.1  christos 	if (utx == NULL)
    205  1.1  christos 		return NULL;
    206  1.1  christos 
    207  1.1  christos 	(void)memcpy(&temp, utx, sizeof(temp));
    208  1.1  christos 
    209  1.1  christos 	if (fp == NULL) {
    210  1.1  christos 		(void)getutxent();
    211  1.1  christos 		if (fp == NULL)
    212  1.1  christos 			return NULL;
    213  1.1  christos 	}
    214  1.1  christos 
    215  1.1  christos 	if (getutxid(&temp) == NULL) {
    216  1.1  christos 		setutxent();
    217  1.1  christos 		if (getutxid(&temp) == NULL) {
    218  1.1  christos 			if (lockf(fileno(fp), F_LOCK, (off_t)0) == -1)
    219  1.1  christos 				return NULL;
    220  1.1  christos 			gotlock++;
    221  1.1  christos 			if (fseeko(fp, (off_t)0, SEEK_END) == -1)
    222  1.1  christos 				goto fail;
    223  1.1  christos 		}
    224  1.1  christos 	}
    225  1.1  christos 
    226  1.1  christos 	if (!gotlock) {
    227  1.1  christos 		/* we are not appending */
    228  1.1  christos 		if (fseeko(fp, -(off_t)sizeof(ut), SEEK_CUR) == -1)
    229  1.1  christos 			return NULL;
    230  1.1  christos 	}
    231  1.1  christos 
    232  1.1  christos 	if (fwrite(&temp, sizeof (temp), 1, fp) != 1)
    233  1.1  christos 		goto fail;
    234  1.1  christos 
    235  1.1  christos 	if (fflush(fp) == -1)
    236  1.1  christos 		goto fail;
    237  1.1  christos 
    238  1.1  christos 	u = memcpy(&ut, &temp, sizeof(ut));
    239  1.1  christos fail:
    240  1.1  christos 	if (gotlock) {
    241  1.1  christos 		if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1)
    242  1.1  christos 			return NULL;
    243  1.1  christos 	}
    244  1.1  christos 	return u;
    245  1.1  christos }
    246  1.1  christos 
    247  1.1  christos 
    248  1.1  christos static struct utmpx *
    249  1.1  christos utmp_update(const struct utmpx *utx)
    250  1.1  christos {
    251  1.1  christos 	char buf[sizeof(*utx) * 4 + 1];
    252  1.1  christos 	pid_t pid;
    253  1.1  christos 	int status;
    254  1.1  christos 
    255  1.1  christos 	(void)strvisx(buf, (const char *)(const void *)utx, sizeof(*utx),
    256  1.1  christos 	    VIS_WHITE);
    257  1.1  christos 	switch (pid = fork()) {
    258  1.1  christos 	case 0:
    259  1.1  christos 		(void)execl(_PATH_UTMP_UPDATE,
    260  1.1  christos 		    strrchr(_PATH_UTMP_UPDATE, '/') + 1, buf);
    261  1.1  christos 		exit(1);
    262  1.1  christos 		/*NOTREACHED*/
    263  1.1  christos 	case -1:
    264  1.1  christos 		return NULL;
    265  1.1  christos 	default:
    266  1.1  christos 		if (waitpid(pid, &status, 0) == -1)
    267  1.1  christos 			return NULL;
    268  1.1  christos 		if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
    269  1.1  christos 			return memcpy(&ut, utx, sizeof(ut));
    270  1.1  christos 		return NULL;
    271  1.1  christos 	}
    272  1.1  christos 
    273  1.3  christos }
    274  1.3  christos 
    275  1.3  christos void
    276  1.3  christos updwtmpx(const char *file, const struct utmpx *utx)
    277  1.3  christos {
    278  1.3  christos 	int fd = open(file, O_WRONLY | O_APPEND);
    279  1.3  christos 	if (fd == -1) {
    280  1.3  christos 		if ((fd = open(file, O_CREAT | O_WRONLY, 0644)) == -1)
    281  1.3  christos 			return;
    282  1.3  christos 		(void)memset(&ut, 0, sizeof(ut));
    283  1.3  christos 		ut.ut_type = SIGNATURE;
    284  1.3  christos 		(void)memcpy(ut.ut_user, vers, sizeof(vers));
    285  1.3  christos 		(void)write(fd, &ut, sizeof(ut));
    286  1.3  christos 	}
    287  1.3  christos 	(void)write(fd, utx, sizeof(*utx));
    288  1.3  christos 	(void)close(fd);
    289  1.1  christos }
    290  1.1  christos 
    291  1.1  christos 
    292  1.1  christos /*
    293  1.1  christos  * The following are extensions and not part of the X/Open spec
    294  1.1  christos  */
    295  1.1  christos int
    296  1.1  christos utmpxname(const char *fname)
    297  1.1  christos {
    298  1.1  christos 	size_t len = strlen(fname);
    299  1.1  christos 
    300  1.1  christos 	if (len >= sizeof(utfile))
    301  1.1  christos 		return 0;
    302  1.1  christos 
    303  1.1  christos 	/* must end in x! */
    304  1.1  christos 	if (fname[len - 1] != 'x')
    305  1.1  christos 		return 0;
    306  1.1  christos 
    307  1.1  christos 	(void)strcpy(utfile, fname);
    308  1.1  christos 	endutxent();
    309  1.1  christos 	return 1;
    310  1.1  christos }
    311