]> andersk Git - openssh.git/blame - login.c
- OpenBSD CVS updates:
[openssh.git] / login.c
CommitLineData
8efc0c15 1/*
5260325f 2 *
3 * login.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 * Created: Fri Mar 24 14:51:08 1995 ylo
11 *
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.
17 *
18 */
8efc0c15 19
20#include "includes.h"
21RCSID("$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
5260325f 52unsigned long
53get_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
61 char buf[1024];
62#endif /* LASTLOG_IS_DIR */
5260325f 63
64 lastlog = _PATH_LASTLOG;
65 buf[0] = '\0';
66
76a8e733 67#ifdef 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 */
73 snprintf(buf, sizeof(buf), "%s/%s", lastlog, logname);
74 fd = open(buf, O_RDONLY);
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) */
a7effaac 90 /* Look in wtmp for the last login */
91 struct utmp wt;
92 char *wt_file = _PATH_WTMP;
93 int fd1;
94 unsigned long t = 0;
95
96 if ( (fd1 = open(wt_file, O_RDONLY)) < 0 ) {
97 error("Couldn't open %.100s to find last login time.", wt_file);
98 return 0;
99 }
100
101 /* seek to last record of file */
102 lseek(fd1, (off_t)(0-sizeof(struct utmp)), SEEK_END);
103
104 /* loop through wtmp for our last user login record */
105 do {
106 if (read(fd1, &wt, sizeof(wt)) != sizeof(wt)) {
107 close(fd1);
108 return 0;
109 }
110
111 if ( wt.ut_type == USER_PROCESS) {
112 if ( !strncmp(logname, wt.ut_user, 8) ) {
113 t = (unsigned long) wt.ut_time;
ae28776a 114#ifdef HAVE_HOST_IN_UTMP
a7effaac 115 if (bufsize > sizeof(wt.ut_host) + 1)
116 bufsize = sizeof(wt.ut_host) + 1;
117 strncpy(buf, wt.ut_host, bufsize - 1);
118 buf[bufsize - 1] = 0;
ae28776a 119#else /* HAVE_HOST_IN_UTMP */
120 buf[0] = 0;
121#endif /* HAVE_HOST_IN_UTMP */
a7effaac 122 }
123 }
124
125 if (lseek(fd1, (off_t)(0-2*sizeof(struct utmp)), SEEK_CUR) == -1)
126 break;
127 } while (t == 0);
128
129 return t;
ae28776a 130#endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
8efc0c15 131}
132
aa3378df 133/*
134 * Records that the user has logged in. I these parts of operating systems
135 * were more standardized.
136 */
8efc0c15 137
5260325f 138void
139record_login(int pid, const char *ttyname, const char *user, uid_t uid,
48e671d5 140 const char *host, struct sockaddr * addr)
8efc0c15 141{
ae28776a 142#if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
5260325f 143 struct lastlog ll;
144 char *lastlog;
76a8e733 145#ifdef LASTLOG_IS_DIR
146 char buf[1024];
147#endif /* LASTLOG_IS_DIR */
ae28776a 148#endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
f498ed15 149 struct utmp u;
150#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
151 struct utmpx utx;
152#endif
5260325f 153
154 /* Construct an utmp/wtmp entry. */
155 memset(&u, 0, sizeof(u));
156 strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));
c73a0cb5 157#if defined(HAVE_ID_IN_UTMP)
21feb5fa 158 strncpy(u.ut_id, ttyname + 8, sizeof(u.ut_id));
c73a0cb5 159#endif /* defined(HAVE_ID_IN_UTMP) */
5260325f 160 strncpy(u.ut_name, user, sizeof(u.ut_name));
70e0115b 161#if defined(HAVE_TV_IN_UTMP)
162 (void)gettimeofday(&u.ut_tv, NULL);
163#else /* defined(HAVE_TV_IN_UTMP) */
164 u.ut_time = time(NULL);
165#endif /* defined(HAVE_TV_IN_UTMP) */
166#if defined(HAVE_PID_IN_UTMP)
167 u.ut_pid = (pid_t)pid;
168#endif /* HAVE_PID_IN_UTMP */
169#if defined(HAVE_TYPE_IN_UTMP)
f498ed15 170 u.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS;
70e0115b 171#endif /* HAVE_TYPE_IN_UTMP */
f498ed15 172#if defined(HAVE_HOST_IN_UTMP)
5260325f 173 strncpy(u.ut_host, host, sizeof(u.ut_host));
4cca272e 174#endif
4811cc0b 175#if defined(HAVE_ADDR_IN_UTMP)
48e671d5 176 switch (addr->sa_family) {
177 case AF_INET: {
178 struct sockaddr_in *in = (struct sockaddr_in*)addr;
179 memcpy(&(u.ut_addr), &(in->sin_addr), sizeof(&(in->sin_addr)));
180 break;
181 }
182#if defined(HAVE_ADDR_V6_IN_UTMP)
183 case AF_INET6: {
184 struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr;
185 memcpy(u.ut_addr_v6, &(in6->sin6_addr), sizeof(&(in6->sin6_addr)));
186 break;
187 }
188#endif
189 default:
190 break;
191 }
4811cc0b 192#endif
8efc0c15 193
f498ed15 194#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
195 memset(&utx, 0, sizeof(utx));
196 strncpy(utx.ut_user, user, sizeof(utx.ut_name));
197 strncpy(utx.ut_line, ttyname + 5, sizeof(utx.ut_line));
21feb5fa 198 strncpy(utx.ut_id, ttyname + 8, sizeof(utx.ut_id));
f498ed15 199 utx.ut_pid = (pid_t)pid;
70e0115b 200 (void)gettimeofday(&utx.ut_tv, NULL);
f74efc8d 201 utx.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS;
202# ifdef HAVE_HOST_IN_UTMPX
203# ifdef HAVE_SYSLEN_IN_UTMPX
f498ed15 204 utx.ut_syslen = strlen(host);
f74efc8d 205 strncpy(utx.ut_host, host, utx.ut_syslen);
206# else
f498ed15 207 strncpy(utx.ut_host, host, sizeof(utx.ut_host));
f74efc8d 208# endif /* HAVE_SYSLEN_IN_UTMPX */
209# endif
48e671d5 210#if defined(HAVE_ADDR_IN_UTMPX)
211 switch (addr->sa_family) {
212 case AF_INET: {
213 struct sockaddr_in *in = (struct sockaddr_in*)addr;
214 memcpy(&(utx.ut_addr), &(in->sin_addr), sizeof(&(in->sin_addr)));
215 break;
216 }
217#if defined(HAVE_ADDR_V6_IN_UTMPX)
218 case AF_INET6: {
219 struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr;
220 memcpy(utx.ut_addr_v6, &(in6->sin6_addr), sizeof(&(in6->sin6_addr)));
221 break;
222 }
223#endif
224 default:
225 break;
226 }
227#endif
f498ed15 228#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
5260325f 229
f74efc8d 230/*#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) && !defined(HAVE_LOGIN)*/
231#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
f498ed15 232 login(&u, &utx);
233#else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
5260325f 234 login(&u);
f498ed15 235#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
a7effaac 236
ae28776a 237#if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
5260325f 238 lastlog = _PATH_LASTLOG;
239
240 /* Update lastlog unless actually recording a logout. */
241 if (strcmp(user, "") != 0) {
a7effaac 242 int fd;
aa3378df 243 /*
244 * It is safer to bzero the lastlog structure first because
245 * some systems might have some extra fields in it (e.g. SGI)
246 */
5260325f 247 memset(&ll, 0, sizeof(ll));
248
249 /* Update lastlog. */
250 ll.ll_time = time(NULL);
251 strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));
252 strncpy(ll.ll_host, host, sizeof(ll.ll_host));
76a8e733 253#ifdef LASTLOG_IS_DIR
254 snprintf(buf, sizeof(buf), "%s/%s", lastlog, logname);
255 fd = open(buf, O_RDWR);
256 if (fd >= 0) {
257#else /* LASTLOG_IS_DIR */
5260325f 258 fd = open(lastlog, O_RDWR);
259 if (fd >= 0) {
260 lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
76a8e733 261#endif /* LASTLOG_IS_DIR */
5260325f 262 if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
263 log("Could not write %.100s: %.100s", lastlog, strerror(errno));
264 close(fd);
265 }
8efc0c15 266 }
ae28776a 267#endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
8efc0c15 268}
5260325f 269
270/* Records that the user has logged out. */
271
272void
273record_logout(int pid, const char *ttyname)
8efc0c15 274{
4cca272e 275#ifdef HAVE_LIBUTIL_LOGIN
5260325f 276 const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */
277 if (logout(line))
278 logwtmp(line, "", "");
4cca272e 279#else /* HAVE_LIBUTIL_LOGIN */
5260325f 280 record_login(pid, ttyname, "", -1, "", NULL);
4cca272e 281#endif /* HAVE_LIBUTIL_LOGIN */
8efc0c15 282}
This page took 0.281586 seconds and 5 git commands to generate.