5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 * Created: Fri Mar 24 14:51:08 1995 ylo
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
23 #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
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.
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.
53 get_last_login_time(uid_t uid, const char *logname,
54 char *buf, unsigned int bufsize)
56 #if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
62 #endif /* LASTLOG_IS_DIR */
64 lastlog = _PATH_LASTLOG;
67 #ifndef LASTLOG_IS_DIR
68 fd = open(lastlog, O_RDONLY);
71 lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
72 #else /* LASTLOG_IS_DIR */
73 snprintf(lbuf, sizeof(buf), "%s/%s", lastlog, logname);
74 fd = open(lbuf, O_RDONLY);
77 #endif /* LASTLOG_IS_DIR */
78 if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) {
83 if (bufsize > sizeof(ll.ll_host) + 1)
84 bufsize = sizeof(ll.ll_host) + 1;
85 strncpy(buf, ll.ll_host, bufsize - 1);
89 #else /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
90 /* Look in wtmp for the last login */
92 char *wt_file = _PATH_WTMP;
96 if ( (fd1 = open(wt_file, O_RDONLY)) < 0 ) {
97 error("Couldn't open %.100s to find last login time.", wt_file);
101 /* seek to last record of file */
102 lseek(fd1, (off_t)(0-sizeof(struct utmp)), SEEK_END);
104 /* loop through wtmp for our last user login record */
106 if (read(fd1, &wt, sizeof(wt)) != sizeof(wt)) {
111 if ( wt.ut_type == USER_PROCESS) {
112 if ( !strncmp(logname, wt.ut_user, 8) ) {
113 t = (unsigned long) wt.ut_time;
114 #ifdef HAVE_HOST_IN_UTMP
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;
119 #else /* HAVE_HOST_IN_UTMP */
121 #endif /* HAVE_HOST_IN_UTMP */
125 if (lseek(fd1, (off_t)(0-2*sizeof(struct utmp)), SEEK_CUR) == -1)
130 #endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
134 * Records that the user has logged in. I these parts of operating systems
135 * were more standardized.
139 record_login(pid_t pid, const char *ttyname, const char *user, uid_t uid,
140 const char *host, struct sockaddr * addr)
142 #if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
145 #ifdef LASTLOG_IS_DIR
147 #endif /* LASTLOG_IS_DIR */
148 #endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
150 #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
154 /* Construct an utmp/wtmp entry. */
155 memset(&u, 0, sizeof(u));
156 strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));
157 #if defined(HAVE_ID_IN_UTMP)
159 strncpy(u.ut_id, ttyname + 5, sizeof(u.ut_id));
161 strncpy(u.ut_id, ttyname + 8, sizeof(u.ut_id));
163 #endif /* defined(HAVE_ID_IN_UTMP) */
164 strncpy(u.ut_name, user, sizeof(u.ut_name));
165 #if defined(HAVE_TV_IN_UTMP)
166 (void)gettimeofday(&u.ut_tv, NULL);
167 #else /* defined(HAVE_TV_IN_UTMP) */
168 u.ut_time = time(NULL);
169 #endif /* defined(HAVE_TV_IN_UTMP) */
170 #if defined(HAVE_PID_IN_UTMP)
171 u.ut_pid = (pid_t)pid;
172 #endif /* HAVE_PID_IN_UTMP */
173 #if defined(HAVE_TYPE_IN_UTMP)
174 u.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS;
175 #endif /* HAVE_TYPE_IN_UTMP */
176 #if defined(HAVE_HOST_IN_UTMP)
177 strncpy(u.ut_host, host, sizeof(u.ut_host));
179 #if defined(HAVE_ADDR_IN_UTMP)
181 switch (addr->sa_family) {
183 struct sockaddr_in *in = (struct sockaddr_in*)addr;
184 memcpy(&(u.ut_addr), &(in->sin_addr), sizeof(&(in->sin_addr)));
187 #if defined(HAVE_ADDR_V6_IN_UTMP)
189 struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr;
190 memcpy(u.ut_addr_v6, &(in6->sin6_addr), sizeof(&(in6->sin6_addr)));
200 #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
201 memset(&utx, 0, sizeof(utx));
202 strncpy(utx.ut_user, user, sizeof(utx.ut_name));
203 strncpy(utx.ut_line, ttyname + 5, sizeof(utx.ut_line));
204 strncpy(utx.ut_id, ttyname + 8, sizeof(utx.ut_id));
205 utx.ut_pid = (pid_t)pid;
206 (void)gettimeofday(&utx.ut_tv, NULL);
207 utx.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS;
208 # ifdef HAVE_HOST_IN_UTMPX
209 # ifdef HAVE_SYSLEN_IN_UTMPX
210 utx.ut_syslen = strlen(host);
211 strncpy(utx.ut_host, host, utx.ut_syslen);
213 strncpy(utx.ut_host, host, sizeof(utx.ut_host));
214 # endif /* HAVE_SYSLEN_IN_UTMPX */
216 #if defined(HAVE_ADDR_IN_UTMPX)
218 switch (addr->sa_family) {
220 struct sockaddr_in *in = (struct sockaddr_in*)addr;
221 memcpy(&(utx.ut_addr), &(in->sin_addr), sizeof(&(in->sin_addr)));
224 #if defined(HAVE_ADDR_V6_IN_UTMPX)
226 struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr;
227 memcpy(utx.ut_addr_v6, &(in6->sin6_addr), sizeof(&(in6->sin6_addr)));
236 #endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
238 /*#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) && !defined(HAVE_LOGIN)*/
239 #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
241 #else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
243 #endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
245 #if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
246 lastlog = _PATH_LASTLOG;
248 /* Update lastlog unless actually recording a logout. */
249 if (strcmp(user, "") != 0) {
252 * It is safer to bzero the lastlog structure first because
253 * some systems might have some extra fields in it (e.g. SGI)
255 memset(&ll, 0, sizeof(ll));
257 /* Update lastlog. */
258 ll.ll_time = time(NULL);
259 strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));
260 strncpy(ll.ll_host, host, sizeof(ll.ll_host));
261 #ifdef LASTLOG_IS_DIR
262 snprintf(buf, sizeof(buf), "%s/%s", lastlog, user);
263 fd = open(buf, O_RDWR);
265 #else /* LASTLOG_IS_DIR */
266 fd = open(lastlog, O_RDWR);
268 lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
269 #endif /* LASTLOG_IS_DIR */
270 if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
271 log("Could not write %.100s: %.100s", lastlog, strerror(errno));
275 #endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
278 /* Records that the user has logged out. */
281 record_logout(pid_t pid, const char *ttyname)
283 #ifdef HAVE_LIBUTIL_LOGIN
284 const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */
286 logwtmp(line, "", "");
287 #else /* HAVE_LIBUTIL_LOGIN */
288 record_login(pid, ttyname, "", -1, "", NULL);
289 #endif /* HAVE_LIBUTIL_LOGIN */