summaryrefslogtreecommitdiff
path: root/bsd-login.c
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 /bsd-login.c
parent1ea8ac7b90c2002ee6041fb90e1f94c7f8a94608 (diff)
- Rewrote bsd-login to use proper utmp API if available. Major cleanup
of fallback DIY code.
Diffstat (limited to 'bsd-login.c')
-rw-r--r--bsd-login.c142
1 files changed, 67 insertions, 75 deletions
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 */