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
31 /* Returns the time when the user last logged in. Returns 0 if the
32 information is not available. This must be called before record_login.
33 The host the user logged in from will be returned in buf. */
35 /* Returns the time when the user last logged in (or 0 if no previous login
36 is found). The name of the host used last time is returned in buf. */
38 unsigned long get_last_login_time(uid_t uid, const char *logname,
39 char *buf, unsigned int bufsize)
45 lastlog = _PATH_LASTLOG;
49 fd = open(lastlog, O_RDONLY);
52 lseek(fd, (off_t)((long)uid * sizeof(ll)), SEEK_SET);
53 if (read(fd, &ll, sizeof(ll)) != sizeof(ll))
59 if (bufsize > sizeof(ll.ll_host) + 1)
60 bufsize = sizeof(ll.ll_host) + 1;
61 strncpy(buf, ll.ll_host, bufsize - 1);
66 /* Records that the user has logged in. I these parts of operating systems
67 were more standardized. */
69 void record_login(int pid, const char *ttyname, const char *user, uid_t uid,
70 const char *host, struct sockaddr_in *addr)
77 const char *utmp, *wtmp;
79 /* Construct an utmp/wtmp entry. */
80 memset(&u, 0, sizeof(u));
81 strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));
82 u.ut_time = time(NULL);
83 strncpy(u.ut_name, user, sizeof(u.ut_name));
84 #ifdef HAVE_HOST_IN_UTMP
85 strncpy(u.ut_host, host, sizeof(u.ut_host));
88 /* Figure out the file names. */
94 lastlog = _PATH_LASTLOG;
96 /* Update lastlog unless actually recording a logout. */
97 if (strcmp(user, "") != 0)
99 /* It is safer to bzero the lastlog structure first because some
100 systems might have some extra fields in it (e.g. SGI) */
101 memset(&ll, 0, sizeof(ll));
103 /* Update lastlog. */
104 ll.ll_time = time(NULL);
105 strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));
106 strncpy(ll.ll_host, host, sizeof(ll.ll_host));
107 fd = open(lastlog, O_RDWR);
110 lseek(fd, (off_t)((long)uid * sizeof(ll)), SEEK_SET);
111 if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
112 log("Could not write %.100s: %.100s", lastlog, strerror(errno));
118 void record_logout(int pid, const char *ttyname)
120 #ifdef HAVE_LIBUTIL_LOGIN
121 const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */
123 logwtmp(line, "", "");
124 #else /* HAVE_LIBUTIL_LOGIN */
125 record_login(pid, ttyname, "", -1, "", NULL);
126 #endif /* HAVE_LIBUTIL_LOGIN */