]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
6ae2364d | 2 | * |
5260325f | 3 | * login.c |
6ae2364d | 4 | * |
5260325f | 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
6ae2364d | 6 | * |
5260325f | 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
8 | * All rights reserved | |
6ae2364d | 9 | * |
5260325f | 10 | * Created: Fri Mar 24 14:51:08 1995 ylo |
6ae2364d | 11 | * |
5260325f | 12 | * This file performs some of the things login(1) normally does. We cannot |
13 | * easily use something like login -p -h host -f user, because there are | |
14 | * several different logins around, and it is hard to determined what kind of | |
15 | * login the current system has. Also, we want to be able to execute commands | |
16 | * on a tty. | |
6ae2364d | 17 | * |
5260325f | 18 | */ |
8efc0c15 | 19 | |
20 | #include "includes.h" | |
21 | RCSID("$Id$"); | |
22 | ||
92f90c57 | 23 | #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) |
8946db53 | 24 | # include <utmpx.h> |
25 | #endif | |
26 | #ifdef HAVE_UTMP_H | |
27 | # include <utmp.h> | |
28 | #endif | |
5260325f | 29 | #include "ssh.h" |
4cca272e | 30 | |
5260325f | 31 | #ifdef HAVE_UTIL_H |
32 | # include <util.h> | |
33 | #endif | |
4cca272e | 34 | #ifdef HAVE_LASTLOG_H |
35 | # include <lastlog.h> | |
36 | #endif | |
09041313 | 37 | #ifdef HAVE_LOGIN_H |
38 | # include <login.h> | |
39 | #endif | |
4cca272e | 40 | |
aa3378df | 41 | /* |
42 | * Returns the time when the user last logged in. Returns 0 if the | |
43 | * information is not available. This must be called before record_login. | |
44 | * The host the user logged in from will be returned in buf. | |
45 | */ | |
8efc0c15 | 46 | |
aa3378df | 47 | /* |
48 | * Returns the time when the user last logged in (or 0 if no previous login | |
49 | * is found). The name of the host used last time is returned in buf. | |
50 | */ | |
8efc0c15 | 51 | |
6ae2364d | 52 | unsigned long |
5260325f | 53 | get_last_login_time(uid_t uid, const char *logname, |
54 | char *buf, unsigned int bufsize) | |
8efc0c15 | 55 | { |
ae28776a | 56 | #if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) |
5260325f | 57 | struct lastlog ll; |
58 | char *lastlog; | |
59 | int fd; | |
76a8e733 | 60 | #ifdef LASTLOG_IS_DIR |
12aa90af | 61 | char lbuf[1024]; |
76a8e733 | 62 | #endif /* LASTLOG_IS_DIR */ |
5260325f | 63 | |
64 | lastlog = _PATH_LASTLOG; | |
65 | buf[0] = '\0'; | |
66 | ||
12aa90af | 67 | #ifndef LASTLOG_IS_DIR |
5260325f | 68 | fd = open(lastlog, O_RDONLY); |
69 | if (fd < 0) | |
70 | return 0; | |
71 | lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET); | |
76a8e733 | 72 | #else /* LASTLOG_IS_DIR */ |
12aa90af | 73 | snprintf(lbuf, sizeof(buf), "%s/%s", lastlog, logname); |
74 | fd = open(lbuf, O_RDONLY); | |
76a8e733 | 75 | if (fd < 0) |
76 | return 0; | |
77 | #endif /* LASTLOG_IS_DIR */ | |
5260325f | 78 | if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) { |
79 | close(fd); | |
80 | return 0; | |
81 | } | |
82 | close(fd); | |
83 | if (bufsize > sizeof(ll.ll_host) + 1) | |
84 | bufsize = sizeof(ll.ll_host) + 1; | |
85 | strncpy(buf, ll.ll_host, bufsize - 1); | |
86 | buf[bufsize - 1] = 0; | |
87 | return ll.ll_time; | |
a7effaac | 88 | |
ae28776a | 89 | #else /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */ |
ad85db64 | 90 | # ifdef HAVE_TYPE_IN_UTMP |
a7effaac | 91 | /* Look in wtmp for the last login */ |
92 | struct utmp wt; | |
93 | char *wt_file = _PATH_WTMP; | |
94 | int fd1; | |
95 | unsigned long t = 0; | |
96 | ||
97 | if ( (fd1 = open(wt_file, O_RDONLY)) < 0 ) { | |
98 | error("Couldn't open %.100s to find last login time.", wt_file); | |
99 | return 0; | |
100 | } | |
101 | ||
102 | /* seek to last record of file */ | |
103 | lseek(fd1, (off_t)(0-sizeof(struct utmp)), SEEK_END); | |
104 | ||
105 | /* loop through wtmp for our last user login record */ | |
106 | do { | |
107 | if (read(fd1, &wt, sizeof(wt)) != sizeof(wt)) { | |
108 | close(fd1); | |
109 | return 0; | |
110 | } | |
111 | ||
112 | if ( wt.ut_type == USER_PROCESS) { | |
113 | if ( !strncmp(logname, wt.ut_user, 8) ) { | |
114 | t = (unsigned long) wt.ut_time; | |
ad85db64 | 115 | # ifdef HAVE_HOST_IN_UTMP |
a7effaac | 116 | if (bufsize > sizeof(wt.ut_host) + 1) |
117 | bufsize = sizeof(wt.ut_host) + 1; | |
118 | strncpy(buf, wt.ut_host, bufsize - 1); | |
119 | buf[bufsize - 1] = 0; | |
ad85db64 | 120 | # else /* HAVE_HOST_IN_UTMP */ |
ae28776a | 121 | buf[0] = 0; |
ad85db64 | 122 | # endif /* HAVE_HOST_IN_UTMP */ |
a7effaac | 123 | } |
124 | } | |
125 | ||
126 | if (lseek(fd1, (off_t)(0-2*sizeof(struct utmp)), SEEK_CUR) == -1) | |
127 | break; | |
128 | } while (t == 0); | |
129 | ||
130 | return t; | |
ad85db64 | 131 | # else |
132 | return 0; | |
133 | # endif /* HAVE_TYPE_IN_UTMP */ | |
ae28776a | 134 | #endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */ |
8efc0c15 | 135 | } |
136 | ||
aa3378df | 137 | /* |
138 | * Records that the user has logged in. I these parts of operating systems | |
139 | * were more standardized. | |
140 | */ | |
8efc0c15 | 141 | |
6ae2364d | 142 | void |
9da5c3c9 | 143 | record_login(pid_t pid, const char *ttyname, const char *user, uid_t uid, |
48e671d5 | 144 | const char *host, struct sockaddr * addr) |
8efc0c15 | 145 | { |
ae28776a | 146 | #if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) |
5260325f | 147 | struct lastlog ll; |
148 | char *lastlog; | |
76a8e733 | 149 | #ifdef LASTLOG_IS_DIR |
150 | char buf[1024]; | |
151 | #endif /* LASTLOG_IS_DIR */ | |
ae28776a | 152 | #endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */ |
f498ed15 | 153 | struct utmp u; |
154 | #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) | |
155 | struct utmpx utx; | |
156 | #endif | |
5260325f | 157 | |
158 | /* Construct an utmp/wtmp entry. */ | |
159 | memset(&u, 0, sizeof(u)); | |
160 | strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line)); | |
c73a0cb5 | 161 | #if defined(HAVE_ID_IN_UTMP) |
0d5f7abc | 162 | #ifdef _AIX |
163 | strncpy(u.ut_id, ttyname + 5, sizeof(u.ut_id)); | |
164 | #else /* !AIX */ | |
21feb5fa | 165 | strncpy(u.ut_id, ttyname + 8, sizeof(u.ut_id)); |
0d5f7abc | 166 | #endif |
c73a0cb5 | 167 | #endif /* defined(HAVE_ID_IN_UTMP) */ |
5260325f | 168 | strncpy(u.ut_name, user, sizeof(u.ut_name)); |
70e0115b | 169 | #if defined(HAVE_TV_IN_UTMP) |
170 | (void)gettimeofday(&u.ut_tv, NULL); | |
171 | #else /* defined(HAVE_TV_IN_UTMP) */ | |
172 | u.ut_time = time(NULL); | |
173 | #endif /* defined(HAVE_TV_IN_UTMP) */ | |
174 | #if defined(HAVE_PID_IN_UTMP) | |
175 | u.ut_pid = (pid_t)pid; | |
176 | #endif /* HAVE_PID_IN_UTMP */ | |
177 | #if defined(HAVE_TYPE_IN_UTMP) | |
f498ed15 | 178 | u.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS; |
70e0115b | 179 | #endif /* HAVE_TYPE_IN_UTMP */ |
f498ed15 | 180 | #if defined(HAVE_HOST_IN_UTMP) |
5260325f | 181 | strncpy(u.ut_host, host, sizeof(u.ut_host)); |
4cca272e | 182 | #endif |
4811cc0b | 183 | #if defined(HAVE_ADDR_IN_UTMP) |
bfae20ad | 184 | if (addr) { |
185 | switch (addr->sa_family) { | |
186 | case AF_INET: { | |
187 | struct sockaddr_in *in = (struct sockaddr_in*)addr; | |
188 | memcpy(&(u.ut_addr), &(in->sin_addr), sizeof(&(in->sin_addr))); | |
189 | break; | |
190 | } | |
48e671d5 | 191 | #if defined(HAVE_ADDR_V6_IN_UTMP) |
bfae20ad | 192 | case AF_INET6: { |
193 | struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr; | |
194 | memcpy(u.ut_addr_v6, &(in6->sin6_addr), sizeof(&(in6->sin6_addr))); | |
195 | break; | |
196 | } | |
48e671d5 | 197 | #endif |
bfae20ad | 198 | default: |
199 | break; | |
200 | } | |
48e671d5 | 201 | } |
4811cc0b | 202 | #endif |
8efc0c15 | 203 | |
f498ed15 | 204 | #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) |
205 | memset(&utx, 0, sizeof(utx)); | |
206 | strncpy(utx.ut_user, user, sizeof(utx.ut_name)); | |
207 | strncpy(utx.ut_line, ttyname + 5, sizeof(utx.ut_line)); | |
21feb5fa | 208 | strncpy(utx.ut_id, ttyname + 8, sizeof(utx.ut_id)); |
f498ed15 | 209 | utx.ut_pid = (pid_t)pid; |
70e0115b | 210 | (void)gettimeofday(&utx.ut_tv, NULL); |
f74efc8d | 211 | utx.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS; |
212 | # ifdef HAVE_HOST_IN_UTMPX | |
213 | # ifdef HAVE_SYSLEN_IN_UTMPX | |
f498ed15 | 214 | utx.ut_syslen = strlen(host); |
f74efc8d | 215 | strncpy(utx.ut_host, host, utx.ut_syslen); |
216 | # else | |
f498ed15 | 217 | strncpy(utx.ut_host, host, sizeof(utx.ut_host)); |
f74efc8d | 218 | # endif /* HAVE_SYSLEN_IN_UTMPX */ |
219 | # endif | |
48e671d5 | 220 | #if defined(HAVE_ADDR_IN_UTMPX) |
aaf2abd7 | 221 | if (addr) { |
bfae20ad | 222 | switch (addr->sa_family) { |
223 | case AF_INET: { | |
224 | struct sockaddr_in *in = (struct sockaddr_in*)addr; | |
225 | memcpy(&(utx.ut_addr), &(in->sin_addr), sizeof(&(in->sin_addr))); | |
226 | break; | |
227 | } | |
48e671d5 | 228 | #if defined(HAVE_ADDR_V6_IN_UTMPX) |
bfae20ad | 229 | case AF_INET6: { |
230 | struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr; | |
231 | memcpy(utx.ut_addr_v6, &(in6->sin6_addr), sizeof(&(in6->sin6_addr))); | |
232 | break; | |
233 | } | |
48e671d5 | 234 | #endif |
bfae20ad | 235 | default: |
236 | break; | |
237 | } | |
48e671d5 | 238 | } |
239 | #endif | |
f498ed15 | 240 | #endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */ |
5260325f | 241 | |
f74efc8d | 242 | /*#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) && !defined(HAVE_LOGIN)*/ |
243 | #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) | |
f498ed15 | 244 | login(&u, &utx); |
245 | #else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */ | |
5260325f | 246 | login(&u); |
f498ed15 | 247 | #endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */ |
a7effaac | 248 | |
ae28776a | 249 | #if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) |
5260325f | 250 | lastlog = _PATH_LASTLOG; |
251 | ||
252 | /* Update lastlog unless actually recording a logout. */ | |
253 | if (strcmp(user, "") != 0) { | |
a7effaac | 254 | int fd; |
aa3378df | 255 | /* |
256 | * It is safer to bzero the lastlog structure first because | |
257 | * some systems might have some extra fields in it (e.g. SGI) | |
258 | */ | |
5260325f | 259 | memset(&ll, 0, sizeof(ll)); |
260 | ||
261 | /* Update lastlog. */ | |
262 | ll.ll_time = time(NULL); | |
263 | strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line)); | |
264 | strncpy(ll.ll_host, host, sizeof(ll.ll_host)); | |
76a8e733 | 265 | #ifdef LASTLOG_IS_DIR |
9d5f374b | 266 | snprintf(buf, sizeof(buf), "%s/%s", lastlog, user); |
76a8e733 | 267 | fd = open(buf, O_RDWR); |
268 | if (fd >= 0) { | |
269 | #else /* LASTLOG_IS_DIR */ | |
5260325f | 270 | fd = open(lastlog, O_RDWR); |
271 | if (fd >= 0) { | |
272 | lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET); | |
76a8e733 | 273 | #endif /* LASTLOG_IS_DIR */ |
5260325f | 274 | if (write(fd, &ll, sizeof(ll)) != sizeof(ll)) |
275 | log("Could not write %.100s: %.100s", lastlog, strerror(errno)); | |
276 | close(fd); | |
277 | } | |
8efc0c15 | 278 | } |
ae28776a | 279 | #endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */ |
8efc0c15 | 280 | } |
5260325f | 281 | |
282 | /* Records that the user has logged out. */ | |
283 | ||
6ae2364d | 284 | void |
9da5c3c9 | 285 | record_logout(pid_t pid, const char *ttyname) |
8efc0c15 | 286 | { |
4cca272e | 287 | #ifdef HAVE_LIBUTIL_LOGIN |
5260325f | 288 | const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */ |
289 | if (logout(line)) | |
290 | logwtmp(line, "", ""); | |
4cca272e | 291 | #else /* HAVE_LIBUTIL_LOGIN */ |
5260325f | 292 | record_login(pid, ttyname, "", -1, "", NULL); |
4cca272e | 293 | #endif /* HAVE_LIBUTIL_LOGIN */ |
8efc0c15 | 294 | } |