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)
41 #ifdef WITH_AIXAUTHENTICATE
42 /* This is done in do_authentication */
43 # define DISABLE_LASTLOG
44 #endif /* WITH_AIXAUTHENTICATE */
47 * Returns the time when the user last logged in. Returns 0 if the
48 * information is not available. This must be called before record_login.
49 * The host the user logged in from will be returned in buf.
53 * Returns the time when the user last logged in (or 0 if no previous login
54 * is found). The name of the host used last time is returned in buf.
58 get_last_login_time(uid_t uid, const char *logname,
59 char *buf, unsigned int bufsize)
61 #if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
64 # ifdef LASTLOG_IS_DIR
67 snprintf(lbuf, sizeof(buf), "%s/%s", _PATH_LASTLOG, logname);
68 if ((fd = open(lbuf, O_RDONLY)) < 0)
70 # else /* LASTLOG_IS_DIR */
73 if ((fd = open(_PATH_LASTLOG, O_RDONLY)) < 0)
76 lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
77 # endif /* LASTLOG_IS_DIR */
78 if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) {
85 if (bufsize > sizeof(ll.ll_host) + 1)
86 bufsize = sizeof(ll.ll_host) + 1;
87 strncpy(buf, ll.ll_host, bufsize - 1);
91 #else /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
92 # ifdef HAVE_TYPE_IN_UTMP
93 /* Look in wtmp for the last login */
98 if ((fd1 = open(_PATH_WTMP, O_RDONLY)) < 0) {
99 error("Couldn't open %.100s to find last login time.", _PATH_WTMP);
103 /* seek to last record of file */
104 lseek(fd1, (off_t)(0 - sizeof(struct utmp)), SEEK_END);
106 /* loop through wtmp for our last user login record */
108 if (read(fd1, &wt, sizeof(wt)) != sizeof(wt)) {
113 if (wt.ut_type == USER_PROCESS) {
114 if (!strncmp(logname, wt.ut_user, 8)) {
115 t = (unsigned long)wt.ut_time;
116 # ifdef HAVE_HOST_IN_UTMP
117 if (bufsize > sizeof(wt.ut_host) + 1)
118 bufsize = sizeof(wt.ut_host) + 1;
119 strncpy(buf, wt.ut_host, bufsize - 1);
120 buf[bufsize - 1] = 0;
121 # else /* HAVE_HOST_IN_UTMP */
123 # endif /* HAVE_HOST_IN_UTMP */
127 if (lseek(fd1, (off_t)(0 - (2 * sizeof(struct utmp))), SEEK_CUR) < 0)
132 # else /* HAVE_TYPE_IN_UTMP */
134 # endif /* HAVE_TYPE_IN_UTMP */
135 #endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
139 * Records that the user has logged in. I wish these parts of operating
140 * systems were more standardized.
143 record_login(pid_t pid, const char *ttyname, const char *user, uid_t uid,
144 const char *host, struct sockaddr * addr)
146 #if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
148 # ifdef LASTLOG_IS_DIR
150 # endif /* LASTLOG_IS_DIR */
151 #endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
153 #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
157 /* Construct an utmp/wtmp entry. */
158 memset(&u, 0, sizeof(u));
159 strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));
161 #if defined(HAVE_ID_IN_UTMP)
163 strncpy(u.ut_id, ttyname + 5, sizeof(u.ut_id));
165 strncpy(u.ut_id, ttyname + 8, sizeof(u.ut_id));
167 #endif /* defined(HAVE_ID_IN_UTMP) */
169 strncpy(u.ut_name, user, sizeof(u.ut_name));
171 #if defined(HAVE_TV_IN_UTMP)
172 (void)gettimeofday(&u.ut_tv, NULL);
173 #else /* defined(HAVE_TV_IN_UTMP) */
174 u.ut_time = time(NULL);
175 #endif /* defined(HAVE_TV_IN_UTMP) */
177 #if defined(HAVE_PID_IN_UTMP)
178 u.ut_pid = (pid_t)pid;
179 #endif /* HAVE_PID_IN_UTMP */
181 #if defined(HAVE_TYPE_IN_UTMP)
182 u.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS;
183 #endif /* HAVE_TYPE_IN_UTMP */
185 #if defined(HAVE_HOST_IN_UTMP)
186 strncpy(u.ut_host, host, sizeof(u.ut_host));
189 #if defined(HAVE_ADDR_IN_UTMP)
191 switch (addr->sa_family) {
193 struct sockaddr_in *in = (struct sockaddr_in*)addr;
194 memcpy(&(u.ut_addr), &(in->sin_addr), sizeof(&(in->sin_addr)));
197 # if defined(HAVE_ADDR_V6_IN_UTMP)
199 struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr;
200 memcpy(u.ut_addr_v6, &(in6->sin6_addr), sizeof(&(in6->sin6_addr)));
203 # endif /* defined(HAVE_ADDR_V6_IN_UTMP) */
208 #endif /* defined(HAVE_ADDR_IN_UTMP) */
210 #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
211 memset(&utx, 0, sizeof(utx));
213 strncpy(utx.ut_user, user, sizeof(utx.ut_name));
214 strncpy(utx.ut_line, ttyname + 5, sizeof(utx.ut_line));
215 strncpy(utx.ut_id, ttyname + 8, sizeof(utx.ut_id));
217 utx.ut_pid = (pid_t)pid;
218 (void)gettimeofday(&utx.ut_tv, NULL);
220 utx.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS;
221 # ifdef HAVE_HOST_IN_UTMPX
222 # ifdef HAVE_SYSLEN_IN_UTMPX
223 utx.ut_syslen = strlen(host);
224 if (utx.ut_syslen + 1 > sizeof(utx.ut_host))
225 utx.ut_syslen = sizeof(utx.ut_host);
226 strncpy(utx.ut_host, host, utx.ut_syslen);
228 strncpy(utx.ut_host, host, sizeof(utx.ut_host));
229 # endif /* HAVE_SYSLEN_IN_UTMPX */
230 utx.ut_host[sizeof(utx.ut_host)-1] = '\0';
233 # if defined(HAVE_ADDR_IN_UTMPX)
235 switch (addr->sa_family) {
237 struct sockaddr_in *in = (struct sockaddr_in*)addr;
238 memcpy(&(utx.ut_addr), &(in->sin_addr), sizeof(&(in->sin_addr)));
241 # if defined(HAVE_ADDR_V6_IN_UTMPX)
243 struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr;
244 memcpy(utx.ut_addr_v6, &(in6->sin6_addr), sizeof(&(in6->sin6_addr)));
247 # endif /* defined(HAVE_ADDR_V6_IN_UTMPX) */
252 # endif /* defined(HAVE_ADDR_IN_UTMPX) */
253 #endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
255 #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
257 #else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
259 #endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
261 #if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
262 /* Update lastlog unless actually recording a logout. */
263 if (strcmp(user, "") != 0) {
266 * It is safer to bzero the lastlog structure first because
267 * some systems might have some extra fields in it (e.g. SGI)
269 memset(&ll, 0, sizeof(ll));
271 /* Update lastlog. */
272 ll.ll_time = time(NULL);
273 strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));
274 strncpy(ll.ll_host, host, sizeof(ll.ll_host));
275 # ifdef LASTLOG_IS_DIR
276 snprintf(buf, sizeof(buf), "%s/%s", _PATH_LASTLOG, user);
277 if ((fd = open(buf, O_RDWR)) >= 0) {
278 if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
279 log("Could not write %.100s: %.100s", buf, strerror(errno));
282 # else /* LASTLOG_IS_DIR */
283 if ((fd = open(_PATH_LASTLOG, O_RDWR)) >= 0) {
284 lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
285 if (write(fd, &ll, sizeof(ll)) != sizeof(ll)) {
286 log("Could not write %.100s: %.100s", _PATH_LASTLOG,
291 # endif /* LASTLOG_IS_DIR */
293 #endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
296 /* Records that the user has logged out. */
299 record_logout(pid_t pid, const char *ttyname)
301 #ifdef HAVE_LIBUTIL_LOGIN
302 const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */
304 logwtmp(line, "", "");
305 #else /* HAVE_LIBUTIL_LOGIN */
306 record_login(pid, ttyname, "", -1, "", NULL);
307 #endif /* HAVE_LIBUTIL_LOGIN */