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