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