summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2000-05-31 13:57:18 +1000
committerDamien Miller <djm@mindrot.org>2000-05-31 13:57:18 +1000
commit1c77392bce06d3f354fe1a3efa4e336c68933807 (patch)
tree8dc7ee3a8a0ea73552c1b752528111b45fbef4cd
parent1ea8ac7b90c2002ee6041fb90e1f94c7f8a94608 (diff)
- Rewrote bsd-login to use proper utmp API if available. Major cleanup
of fallback DIY code.
-rw-r--r--ChangeLog2
-rw-r--r--bsd-login.c142
-rw-r--r--configure.in2
-rw-r--r--login.c4
4 files changed, 72 insertions, 78 deletions
diff --git a/ChangeLog b/ChangeLog
index a87f32b74..fa1f65be7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,8 @@
2 - Cleanup of auth.c, login.c and fake-* 2 - Cleanup of auth.c, login.c and fake-*
3 - Cleanup of auth-pam.c, save and print "account expired" error messages 3 - Cleanup of auth-pam.c, save and print "account expired" error messages
4 - Fix EGD read bug by IWAMURO Motonori <iwa@mmp.fujitsu.co.jp> 4 - Fix EGD read bug by IWAMURO Motonori <iwa@mmp.fujitsu.co.jp>
5 - Rewrote bsd-login to use proper utmp API if available. Major cleanup
6 of fallback DIY code.
5 7
620000530 820000530
7 - Define atexit for old Solaris 9 - Define atexit for old Solaris
diff --git a/bsd-login.c b/bsd-login.c
index ecd5e05af..a6f4acca3 100644
--- a/bsd-login.c
+++ b/bsd-login.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * This file has been modified from the original OpenBSD version 2 * This file has been heavily modified from the original OpenBSD version
3 */ 3 */
4 4
5/* $OpenBSD: login.c,v 1.5 1998/07/13 02:11:12 millert Exp $ */ 5/* $OpenBSD: login.c,v 1.5 1998/07/13 02:11:12 millert Exp $ */
@@ -74,7 +74,7 @@ struct utmp * utp;
74 int t = 0; 74 int t = 0;
75 struct utmp * u; 75 struct utmp * u;
76 76
77#if defined(HAVE_TYPE_IN_UTMP) || defined(HAVE_TYPE_IN_UTMPX) 77# if defined(HAVE_TYPE_IN_UTMP) || defined(HAVE_TYPE_IN_UTMPX)
78 setutent(); 78 setutent();
79 79
80 while((u = getutent()) != NULL) { 80 while((u = getutent()) != NULL) {
@@ -93,35 +93,47 @@ struct utmp * utp;
93 } 93 }
94 94
95 endutent(); 95 endutent();
96#endif 96# endif /* defined(HAVE_TYPE_IN_UTMP) || defined(HAVE_TYPE_IN_UTMPX) */
97 return(-1); 97 return(-1);
98} 98}
99#else 99#else /* USER_PROCESS */
100int find_tty_slot( utp ) 100int find_tty_slot(struct utmp *utp)
101struct utmp * utp;
102{ 101{
103 return(ttyslot()); 102 return(ttyslot());
104} 103}
105#endif 104#endif /* USER_PROCESS */
106 105
107#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) 106#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
108void 107void login(struct utmpx *utx)
109login(utp,utx)
110 struct utmp *utp;
111 struct utmpx *utx;
112#else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */ 108#else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
113void 109void login(struct utmp *utp)
114login(utp)
115 struct utmp *utp;
116#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */ 110#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
117{ 111{
118#if defined(HAVE_HOST_IN_UTMP) 112 /* Use proper API if we have it */
119 struct utmp old_ut; 113#if defined(USE_UTMPX)
120#endif 114# if defined(HAVE_PUTUTXLINE)
121#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) 115 setutxent();
122 struct utmpx *old_utx; 116 pututxline(utx);
123#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */ 117 endutxent();
124 register int fd; 118# endif /* defined(HAVE_PUTUTXLINE) */
119# if defined(HAVE_UPDWTMPX)
120 updwtmpx(_PATH_WTMPX, utx);
121# endif /* defined(HAVE_UPDWTMPX) */
122#else /* defined(USE_UTMPX) */
123# if defined(HAVE_PUTUTLINE)
124 setutent();
125 pututline(utp);
126 endutent();
127# endif /* defined(HAVE_PUTUTLINE) */
128# if defined(HAVE_UPDWTMPX)
129 updwtmp(_PATH_WTMP, utp);
130# endif /* defined(HAVE_UPDWTMP) */
131#endif /* defined(USE_UTMPX) */
132
133 /* Otherwise DIY */
134#if (defined(USE_UTMPX) && !defined(HAVE_PUTUTXLINE)) || \
135 (!defined(USE_UTMPX) && !defined(HAVE_PUTUTLINE))
136 int fd;
125 int tty; 137 int tty;
126 138
127 /* can't use ttyslot here, as that will not work for logout 139 /* can't use ttyslot here, as that will not work for logout
@@ -132,72 +144,52 @@ login(utp)
132 tty = find_tty_slot(utp); 144 tty = find_tty_slot(utp);
133 145
134#ifdef USE_UTMPX 146#ifdef USE_UTMPX
147 /* If no tty was found, append it to utmpx */
148 if (tty == -1) {
149 if ((fd = open(_PATH_UTMPX, O_WRONLY|O_APPEND, 0)) >= 0) {
150 (void)write(fd, utp, sizeof(struct utmp));
151 (void)close(fd);
152 return;
153 }
154 }
155 /* Otherwise, tty was found - update at its location */
135 fd = open(_PATH_UTMPX, O_RDWR|O_CREAT, 0644); 156 fd = open(_PATH_UTMPX, O_RDWR|O_CREAT, 0644);
136 if (fd == -1) { 157 if (fd == -1) {
137 log("Couldn't open %s: %s", _PATH_UTMPX, strerror(errno)); 158 log("Couldn't open %s: %s", _PATH_UTMPX, strerror(errno));
159 return;
160 }
161 lseek(fd, (off_t)(tty * sizeof(struct utmpx)), SEEK_SET);
162 write(fd, utx, sizeof(struct utmpx));
163 close(fd);
164 if ((fd = open(_PATH_WTMPX, O_WRONLY|O_APPEND, 0)) >= 0) {
165 (void)write(fd, utx, sizeof(struct utmpx));
166 (void)close(fd);
167 }
138#else /* USE_UTMPX */ 168#else /* USE_UTMPX */
139 fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644); 169 /* If no tty was found, append it to utmp */
140 if (fd == -1) { 170 if (tty == -1) {
141 log("Couldn't open %s: %s", _PATH_UTMP, strerror(errno)); 171 if ((fd = open(_PATH_UTMP, O_WRONLY|O_APPEND, 0)) >= 0) {
142#endif /* USE_UTMPX */
143 } else {
144 /* If no tty was found... */
145 if (tty == -1) {
146 /* ... append it to utmp on login */
147#if defined(HAVE_TYPE_IN_UTMP) || defined(HAVE_TYPE_IN_UTMPX)
148 if (utp->ut_type == USER_PROCESS) {
149#ifdef USE_UTMPX
150 if ((fd = open(_PATH_UTMPX, O_WRONLY|O_APPEND, 0)) >= 0) {
151#else /* USE_UTMPX */
152 if ((fd = open(_PATH_UTMP, O_WRONLY|O_APPEND, 0)) >= 0) {
153#endif /* USE_UTMPX */
154 (void)write(fd, utp, sizeof(struct utmp));
155 (void)close(fd);
156 }
157 } else {
158 /* Shouldn't get to here unless somthing happened to utmp */
159 /* Between login and logout */
160 log("No tty slot found at logout");
161 }
162#endif
163 } else {
164 /* Otherwise, tty was found - update at its location */
165#if defined(HAVE_HOST_IN_UTMP)
166# ifndef UT_LINESIZE
167# define UT_LINESIZE (sizeof(old_ut.ut_line))
168# define UT_NAMESIZE (sizeof(old_ut.ut_name))
169# define UT_HOSTSIZE (sizeof(old_ut.ut_host))
170# endif
171 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
172 /*
173 * Prevent luser from zero'ing out ut_host.
174 * If the new ut_line is empty but the old one is not
175 * and ut_line and ut_name match, preserve the old ut_line.
176 */
177 if (read(fd, &old_ut, sizeof(struct utmp)) ==
178 sizeof(struct utmp) && utp->ut_host[0] == '\0' &&
179 old_ut.ut_host[0] != '\0' &&
180 strncmp(old_ut.ut_line, utp->ut_line, UT_LINESIZE) == 0 &&
181 strncmp(old_ut.ut_name, utp->ut_name, UT_NAMESIZE) == 0)
182 (void)memcpy(utp->ut_host, old_ut.ut_host, UT_HOSTSIZE);
183#endif /* defined(HAVE_HOST_IN_UTMP) */
184 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
185 (void)write(fd, utp, sizeof(struct utmp)); 172 (void)write(fd, utp, sizeof(struct utmp));
186 (void)close(fd); 173 (void)close(fd);
174 return;
187 } 175 }
188 } 176 }
189 177 /* Otherwise, tty was found - update at its location */
178 fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644);
179 if (fd == -1) {
180 log("Couldn't open %s: %s", _PATH_UTMP, strerror(errno));
181 return;
182 }
183 lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
184 write(fd, utp, sizeof(struct utmp));
185 close(fd);
190 if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) { 186 if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) {
191 (void)write(fd, utp, sizeof(struct utmp)); 187 (void)write(fd, utp, sizeof(struct utmp));
192 (void)close(fd); 188 (void)close(fd);
193 } 189 }
194#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) 190#endif /* USE_UTMPX */
195 old_utx = pututxline(utx); 191#endif /* (defined(USE_UTMPX) && !defined(HAVE_PUTUTXLINE)) || \
196# ifdef HAVE_UPDWTMPX 192 (!defined(USE_UTMPX) && !defined(HAVE_PUTUTLINE)) */
197 updwtmpx(_PATH_WTMPX, utx);
198# endif /* HAVE_UPDWTMPX */
199 endutxent();
200#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
201} 193}
202 194
203#endif /* HAVE_LOGIN */ 195#endif /* HAVE_LOGIN */
diff --git a/configure.in b/configure.in
index 3c6694dba..86284aa22 100644
--- a/configure.in
+++ b/configure.in
@@ -135,7 +135,7 @@ fi
135AC_CHECK_HEADERS(bstring.h endian.h lastlog.h login.h maillock.h netdb.h netgroup.h netinet/in_systm.h paths.h poll.h pty.h shadow.h security/pam_appl.h sys/bitypes.h sys/bsdtty.h sys/cdefs.h sys/poll.h sys/select.h sys/stropts.h sys/sysmacros.h sys/time.h sys/ttcompat.h stddef.h util.h utmp.h utmpx.h) 135AC_CHECK_HEADERS(bstring.h endian.h lastlog.h login.h maillock.h netdb.h netgroup.h netinet/in_systm.h paths.h poll.h pty.h shadow.h security/pam_appl.h sys/bitypes.h sys/bsdtty.h sys/cdefs.h sys/poll.h sys/select.h sys/stropts.h sys/sysmacros.h sys/time.h sys/ttcompat.h stddef.h util.h utmp.h utmpx.h)
136 136
137# Checks for library functions. 137# Checks for library functions.
138AC_CHECK_FUNCS(arc4random atexit b64_ntop bcopy bindresvport_af clock freeaddrinfo gai_strerror getaddrinfo getnameinfo getrusage innetgr md5_crypt memmove mkdtemp on_exit openpty rresvport_af setenv seteuid setlogin setproctitle setreuid snprintf strlcat strlcpy updwtmpx vsnprintf vhangup _getpty __b64_ntop) 138AC_CHECK_FUNCS(arc4random atexit b64_ntop bcopy bindresvport_af clock freeaddrinfo gai_strerror getaddrinfo getnameinfo getrusage innetgr md5_crypt memmove mkdtemp on_exit openpty pututline pututxline rresvport_af setenv seteuid setlogin setproctitle setreuid snprintf strlcat strlcpy updwtmp updwtmpx vsnprintf vhangup _getpty __b64_ntop)
139 139
140AC_CHECK_FUNC(login, 140AC_CHECK_FUNC(login,
141 [AC_DEFINE(HAVE_LOGIN)], 141 [AC_DEFINE(HAVE_LOGIN)],
diff --git a/login.c b/login.c
index 6fdc71e68..09e73b3de 100644
--- a/login.c
+++ b/login.c
@@ -18,7 +18,7 @@
18 */ 18 */
19 19
20#include "includes.h" 20#include "includes.h"
21RCSID("$Id: login.c,v 1.29 2000/05/31 01:20:12 damien Exp $"); 21RCSID("$Id: login.c,v 1.30 2000/05/31 03:57:19 damien Exp $");
22 22
23#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) 23#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
24# include <utmpx.h> 24# include <utmpx.h>
@@ -253,7 +253,7 @@ record_login(pid_t pid, const char *ttyname, const char *user, uid_t uid,
253#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */ 253#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
254 254
255#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) 255#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
256 login(&u, &utx); 256 login(&utx);
257#else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */ 257#else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
258 login(&u); 258 login(&u);
259#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */ 259#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */