]>
Commit | Line | Data |
---|---|---|
0d5f7abc | 1 | /* |
2 | * This file has been modified from the original OpenBSD version | |
3 | */ | |
4 | ||
beb43d31 | 5 | /* $OpenBSD: login.c,v 1.5 1998/07/13 02:11:12 millert Exp $ */ |
6 | /* | |
7 | * Copyright (c) 1988, 1993 | |
8 | * The Regents of the University of California. All rights reserved. | |
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 University of | |
21 | * California, Berkeley and its contributors. | |
22 | * 4. Neither the name of the University nor the names of its contributors | |
23 | * may be used to endorse or promote products derived from this software | |
24 | * without specific prior written permission. | |
25 | * | |
26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
36 | * SUCH DAMAGE. | |
37 | */ | |
38 | ||
39 | #include "config.h" | |
40 | #ifndef HAVE_LOGIN | |
41 | ||
0d5f7abc | 42 | #include <errno.h> |
43 | ||
beb43d31 | 44 | #if defined(LIBC_SCCS) && !defined(lint) |
45 | /* from: static char sccsid[] = "@(#)login.c 8.1 (Berkeley) 6/4/93"; */ | |
b09a984b | 46 | static char *rcsid = "$OpenBSD: login.c,v 1.5 1998/07/13 02:11:12 millert Exp $"; |
beb43d31 | 47 | #endif /* LIBC_SCCS and not lint */ |
48 | ||
49 | #include <sys/types.h> | |
50 | ||
51 | #include <fcntl.h> | |
52 | #include <unistd.h> | |
53 | #include <stdlib.h> | |
92f90c57 | 54 | #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) |
8946db53 | 55 | # include <utmpx.h> |
56 | #endif | |
57 | #ifdef HAVE_UTMP_H | |
58 | # include <utmp.h> | |
59 | #endif | |
beb43d31 | 60 | #include <stdio.h> |
847e8865 | 61 | #include <string.h> |
beb43d31 | 62 | |
a423beaf | 63 | #ifdef USER_PROCESS |
0d5f7abc | 64 | /* |
65 | * find first matching slot in utmp, or "-1" for none | |
66 | * | |
67 | * algorithm: for USER_PROCESS, check tty name | |
68 | * for DEAD_PROCESS, check PID and tty name | |
69 | * | |
70 | */ | |
71 | int find_tty_slot( utp ) | |
72 | struct utmp * utp; | |
73 | { | |
74 | int t = 0; | |
75 | struct utmp * u; | |
76 | ||
25422c70 | 77 | #if defined(HAVE_TYPE_IN_UTMP) || defined(HAVE_TYPE_IN_UTMPX) |
0d5f7abc | 78 | setutent(); |
79 | ||
80 | while((u = getutent()) != NULL) { | |
81 | if (utp->ut_type == USER_PROCESS && | |
82 | (strncmp(utp->ut_line, u->ut_line, sizeof(utp->ut_line)) == 0)) { | |
83 | endutent(); | |
84 | return(t); | |
85 | } | |
86 | ||
87 | if ((utp->ut_type == DEAD_PROCESS) && (utp->ut_pid == u->ut_pid) && | |
88 | (strncmp(utp->ut_line, u->ut_line, sizeof(utp->ut_line)) == 0 )) { | |
89 | endutent(); | |
90 | return(t); | |
91 | } | |
92 | t++; | |
93 | } | |
94 | ||
95 | endutent(); | |
c04f75f1 | 96 | #endif |
0d5f7abc | 97 | return(-1); |
98 | } | |
a423beaf | 99 | #else |
100 | int find_tty_slot( utp ) | |
101 | struct utmp * utp; | |
102 | { | |
103 | return(ttyslot()); | |
104 | } | |
105 | #endif | |
0d5f7abc | 106 | |
f498ed15 | 107 | #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) |
108 | void | |
109 | login(utp,utx) | |
110 | struct utmp *utp; | |
111 | struct utmpx *utx; | |
112 | #else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */ | |
beb43d31 | 113 | void |
114 | login(utp) | |
f498ed15 | 115 | struct utmp *utp; |
116 | #endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */ | |
beb43d31 | 117 | { |
f74efc8d | 118 | #if defined(HAVE_HOST_IN_UTMP) |
f498ed15 | 119 | struct utmp old_ut; |
f74efc8d | 120 | #endif |
f498ed15 | 121 | #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) |
122 | struct utmpx *old_utx; | |
123 | #endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */ | |
beb43d31 | 124 | register int fd; |
125 | int tty; | |
126 | ||
0d5f7abc | 127 | /* can't use ttyslot here, as that will not work for logout |
128 | * (record_logout() is called from the master sshd, which does | |
129 | * not have the correct tty on stdin/out, so ttyslot will return | |
130 | * "-1" or (worse) a wrong number | |
131 | */ | |
132 | tty = find_tty_slot(utp); | |
f74efc8d | 133 | |
25422c70 | 134 | #ifdef USE_UTMPX |
135 | fd = open(_PATH_UTMPX, O_RDWR|O_CREAT, 0644); | |
136 | if (fd == -1) { | |
137 | log("Couldn't open %s: %s", _PATH_UTMPX, strerror(errno)); | |
138 | #else /* USE_UTMPX */ | |
0d5f7abc | 139 | fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644); |
140 | if (fd == -1) { | |
141 | log("Couldn't open %s: %s", _PATH_UTMP, strerror(errno)); | |
25422c70 | 142 | #endif /* USE_UTMPX */ |
0d5f7abc | 143 | } else { |
144 | /* If no tty was found... */ | |
145 | if (tty == -1) { | |
146 | /* ... append it to utmp on login */ | |
25422c70 | 147 | #if defined(HAVE_TYPE_IN_UTMP) || defined(HAVE_TYPE_IN_UTMPX) |
0d5f7abc | 148 | if (utp->ut_type == USER_PROCESS) { |
25422c70 | 149 | #ifdef USE_UTMPX |
150 | if ((fd = open(_PATH_UTMPX, O_WRONLY|O_APPEND, 0)) >= 0) { | |
151 | #else /* USE_UTMPX */ | |
0d5f7abc | 152 | if ((fd = open(_PATH_UTMP, O_WRONLY|O_APPEND, 0)) >= 0) { |
25422c70 | 153 | #endif /* USE_UTMPX */ |
0d5f7abc | 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 | } | |
c04f75f1 | 162 | #endif |
0d5f7abc | 163 | } else { |
164 | /* Otherwise, tty was found - update at its location */ | |
f498ed15 | 165 | #if defined(HAVE_HOST_IN_UTMP) |
f74efc8d | 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 | |
0d5f7abc | 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); | |
f498ed15 | 183 | #endif /* defined(HAVE_HOST_IN_UTMP) */ |
0d5f7abc | 184 | (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET); |
185 | (void)write(fd, utp, sizeof(struct utmp)); | |
186 | (void)close(fd); | |
187 | } | |
beb43d31 | 188 | } |
0d5f7abc | 189 | |
beb43d31 | 190 | if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) { |
f498ed15 | 191 | (void)write(fd, utp, sizeof(struct utmp)); |
beb43d31 | 192 | (void)close(fd); |
193 | } | |
f498ed15 | 194 | #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) |
195 | old_utx = pututxline(utx); | |
196 | # ifdef HAVE_UPDWTMPX | |
197 | updwtmpx(_PATH_WTMPX, utx); | |
198 | # endif /* HAVE_UPDWTMPX */ | |
199 | endutxent(); | |
200 | #endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */ | |
beb43d31 | 201 | } |
202 | ||
203 | #endif /* HAVE_LOGIN */ |