summaryrefslogtreecommitdiff
path: root/bsd-login.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2000-05-01 22:53:53 +1000
committerDamien Miller <djm@mindrot.org>2000-05-01 22:53:53 +1000
commit0e489dc5aed9d54ce1943e6bde26f1d22ac13ded (patch)
treecc401bf2ad4d1b2bd970ea3fdbf8c2459f60f808 /bsd-login.c
parent35dabd0398dc4aa8735d5ec896ead6955b83b2ff (diff)
- Merged bsd-login ttyslot and AIX utmp patch from Gert Doering
<gd@hilb1.medat.de>
Diffstat (limited to 'bsd-login.c')
-rw-r--r--bsd-login.c99
1 files changed, 82 insertions, 17 deletions
diff --git a/bsd-login.c b/bsd-login.c
index eccb29ee4..910f9ff9f 100644
--- a/bsd-login.c
+++ b/bsd-login.c
@@ -1,3 +1,7 @@
1/*
2 * This file has been modified from the original OpenBSD version
3 */
4
1/* $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 $ */
2/* 6/*
3 * Copyright (c) 1988, 1993 7 * Copyright (c) 1988, 1993
@@ -35,6 +39,8 @@
35#include "config.h" 39#include "config.h"
36#ifndef HAVE_LOGIN 40#ifndef HAVE_LOGIN
37 41
42#include <errno.h>
43
38#if defined(LIBC_SCCS) && !defined(lint) 44#if defined(LIBC_SCCS) && !defined(lint)
39/* from: static char sccsid[] = "@(#)login.c 8.1 (Berkeley) 6/4/93"; */ 45/* from: static char sccsid[] = "@(#)login.c 8.1 (Berkeley) 6/4/93"; */
40static char *rcsid = "$OpenBSD: login.c,v 1.5 1998/07/13 02:11:12 millert Exp $"; 46static char *rcsid = "$OpenBSD: login.c,v 1.5 1998/07/13 02:11:12 millert Exp $";
@@ -54,6 +60,40 @@ static char *rcsid = "$OpenBSD: login.c,v 1.5 1998/07/13 02:11:12 millert Exp $"
54#include <stdio.h> 60#include <stdio.h>
55#include <string.h> 61#include <string.h>
56 62
63/*
64 * find first matching slot in utmp, or "-1" for none
65 *
66 * algorithm: for USER_PROCESS, check tty name
67 * for DEAD_PROCESS, check PID and tty name
68 *
69 */
70int find_tty_slot( utp )
71struct utmp * utp;
72{
73 int t = 0;
74 struct utmp * u;
75
76 setutent();
77
78 while((u = getutent()) != NULL) {
79 if (utp->ut_type == USER_PROCESS &&
80 (strncmp(utp->ut_line, u->ut_line, sizeof(utp->ut_line)) == 0)) {
81 endutent();
82 return(t);
83 }
84
85 if ((utp->ut_type == DEAD_PROCESS) && (utp->ut_pid == u->ut_pid) &&
86 (strncmp(utp->ut_line, u->ut_line, sizeof(utp->ut_line)) == 0 )) {
87 endutent();
88 return(t);
89 }
90 t++;
91 }
92
93 endutent();
94 return(-1);
95}
96
57#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) 97#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
58void 98void
59login(utp,utx) 99login(utp,utx)
@@ -74,32 +114,57 @@ login(utp)
74 register int fd; 114 register int fd;
75 int tty; 115 int tty;
76 116
77 tty = ttyslot(); 117 /* can't use ttyslot here, as that will not work for logout
78 if (tty > 0 && (fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644)) >= 0) { 118 * (record_logout() is called from the master sshd, which does
119 * not have the correct tty on stdin/out, so ttyslot will return
120 * "-1" or (worse) a wrong number
121 */
122 tty = find_tty_slot(utp);
79 123
124 fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644);
125 if (fd == -1) {
126 log("Couldn't open %s: %s", _PATH_UTMP, strerror(errno));
127 } else {
128 /* If no tty was found... */
129 if (tty == -1) {
130 /* ... append it to utmp on login */
131 if (utp->ut_type == USER_PROCESS) {
132 if ((fd = open(_PATH_UTMP, O_WRONLY|O_APPEND, 0)) >= 0) {
133 (void)write(fd, utp, sizeof(struct utmp));
134 (void)close(fd);
135 }
136 } else {
137 /* Shouldn't get to here unless somthing happened to utmp */
138 /* Between login and logout */
139 log("No tty slot found at logout");
140 }
141 } else {
142 /* Otherwise, tty was found - update at its location */
80#if defined(HAVE_HOST_IN_UTMP) 143#if defined(HAVE_HOST_IN_UTMP)
81# ifndef UT_LINESIZE 144# ifndef UT_LINESIZE
82# define UT_LINESIZE (sizeof(old_ut.ut_line)) 145# define UT_LINESIZE (sizeof(old_ut.ut_line))
83# define UT_NAMESIZE (sizeof(old_ut.ut_name)) 146# define UT_NAMESIZE (sizeof(old_ut.ut_name))
84# define UT_HOSTSIZE (sizeof(old_ut.ut_host)) 147# define UT_HOSTSIZE (sizeof(old_ut.ut_host))
85# endif 148# endif
86 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET); 149 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
87 /* 150 /*
88 * Prevent luser from zero'ing out ut_host. 151 * Prevent luser from zero'ing out ut_host.
89 * If the new ut_line is empty but the old one is not 152 * If the new ut_line is empty but the old one is not
90 * and ut_line and ut_name match, preserve the old ut_line. 153 * and ut_line and ut_name match, preserve the old ut_line.
91 */ 154 */
92 if (read(fd, &old_ut, sizeof(struct utmp)) == 155 if (read(fd, &old_ut, sizeof(struct utmp)) ==
93 sizeof(struct utmp) && utp->ut_host[0] == '\0' && 156 sizeof(struct utmp) && utp->ut_host[0] == '\0' &&
94 old_ut.ut_host[0] != '\0' && 157 old_ut.ut_host[0] != '\0' &&
95 strncmp(old_ut.ut_line, utp->ut_line, UT_LINESIZE) == 0 && 158 strncmp(old_ut.ut_line, utp->ut_line, UT_LINESIZE) == 0 &&
96 strncmp(old_ut.ut_name, utp->ut_name, UT_NAMESIZE) == 0) 159 strncmp(old_ut.ut_name, utp->ut_name, UT_NAMESIZE) == 0)
97 (void)memcpy(utp->ut_host, old_ut.ut_host, UT_HOSTSIZE); 160 (void)memcpy(utp->ut_host, old_ut.ut_host, UT_HOSTSIZE);
98#endif /* defined(HAVE_HOST_IN_UTMP) */ 161#endif /* defined(HAVE_HOST_IN_UTMP) */
99 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET); 162 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
100 (void)write(fd, utp, sizeof(struct utmp)); 163 (void)write(fd, utp, sizeof(struct utmp));
101 (void)close(fd); 164 (void)close(fd);
165 }
102 } 166 }
167
103 if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) { 168 if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) {
104 (void)write(fd, utp, sizeof(struct utmp)); 169 (void)write(fd, utp, sizeof(struct utmp));
105 (void)close(fd); 170 (void)close(fd);