xref: /openbsd-src/lib/libutil/login.c (revision 27c829c3d914f26143898b6edfe5dda3cfb2812e)
1*27c829c3Sguenther /*	$OpenBSD: login.c,v 1.11 2015/12/28 20:11:36 guenther Exp $	*/
2df930be7Sderaadt /*
3df930be7Sderaadt  * Copyright (c) 1988, 1993
4df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
5df930be7Sderaadt  *
6df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
7df930be7Sderaadt  * modification, are permitted provided that the following conditions
8df930be7Sderaadt  * are met:
9df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
10df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
11df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
13df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
146580fee3Smillert  * 3. Neither the name of the University nor the names of its contributors
15df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
16df930be7Sderaadt  *    without specific prior written permission.
17df930be7Sderaadt  *
18df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28df930be7Sderaadt  * SUCH DAMAGE.
29df930be7Sderaadt  */
30df930be7Sderaadt 
31df930be7Sderaadt #include <sys/types.h>
32df930be7Sderaadt 
33df930be7Sderaadt #include <fcntl.h>
34df930be7Sderaadt #include <unistd.h>
35df930be7Sderaadt #include <stdlib.h>
36df930be7Sderaadt #include <utmp.h>
37df930be7Sderaadt #include <stdio.h>
386f1c7789Sderaadt #include <string.h>
39ad69070aSdownsj 
40ad69070aSdownsj #include "util.h"
41df930be7Sderaadt 
42df930be7Sderaadt void
login(struct utmp * utp)431477552aSderaadt login(struct utmp *utp)
44df930be7Sderaadt {
45bb2dc11bSmillert 	struct utmp old_ut;
46*27c829c3Sguenther 	int fd, tty;
47*27c829c3Sguenther 	off_t pos;
48df930be7Sderaadt 
49df930be7Sderaadt 	tty = ttyslot();
50*27c829c3Sguenther 	if (tty > 0 && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT|O_CLOEXEC, 0644))
51*27c829c3Sguenther 	    >= 0) {
52bb2dc11bSmillert 		/*
53bb2dc11bSmillert 		 * Prevent luser from zero'ing out ut_host.
54bb2dc11bSmillert 		 * If the new ut_line is empty but the old one is not
55bb2dc11bSmillert 		 * and ut_line and ut_name match, preserve the old ut_line.
56bb2dc11bSmillert 		 */
57*27c829c3Sguenther 		pos = (off_t)tty * sizeof(struct utmp);
58*27c829c3Sguenther 		if (utp->ut_host[0] == '\0' &&
59*27c829c3Sguenther 		    pread(fd, &old_ut, sizeof(struct utmp), pos) ==
60*27c829c3Sguenther 		    sizeof(struct utmp) &&
61bb2dc11bSmillert 		    old_ut.ut_host[0] != '\0' &&
62bb2dc11bSmillert 		    strncmp(old_ut.ut_line, utp->ut_line, UT_LINESIZE) == 0 &&
63bb2dc11bSmillert 		    strncmp(old_ut.ut_name, utp->ut_name, UT_NAMESIZE) == 0)
64bb2dc11bSmillert 			(void)memcpy(utp->ut_host, old_ut.ut_host, UT_HOSTSIZE);
65*27c829c3Sguenther 		(void)pwrite(fd, utp, sizeof(struct utmp), pos);
66df930be7Sderaadt 		(void)close(fd);
67df930be7Sderaadt 	}
68*27c829c3Sguenther 	if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND|O_CLOEXEC)) >= 0) {
69bb2dc11bSmillert 		(void)write(fd, utp, sizeof(struct utmp));
70df930be7Sderaadt 		(void)close(fd);
71df930be7Sderaadt 	}
72df930be7Sderaadt }
73