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
26 /* Returns the time when the user last logged in. Returns 0 if the
27 information is not available. This must be called before record_login.
28 The host the user logged in from will be returned in buf. */
30 /* Returns the time when the user last logged in (or 0 if no previous login
31 is found). The name of the host used last time is returned in buf. */
33 unsigned long get_last_login_time(uid_t uid, const char *logname,
34 char *buf, unsigned int bufsize)
40 lastlog = _PATH_LASTLOG;
44 fd = open(lastlog, O_RDONLY);
47 lseek(fd, (off_t)((long)uid * sizeof(ll)), SEEK_SET);
48 if (read(fd, &ll, sizeof(ll)) != sizeof(ll))
54 if (bufsize > sizeof(ll.ll_host) + 1)
55 bufsize = sizeof(ll.ll_host) + 1;
56 strncpy(buf, ll.ll_host, bufsize - 1);
61 /* Records that the user has logged in. I these parts of operating systems
62 were more standardized. */
64 void record_login(int pid, const char *ttyname, const char *user, uid_t uid,
65 const char *host, struct sockaddr_in *addr)
72 const char *utmp, *wtmp;
74 /* Construct an utmp/wtmp entry. */
75 memset(&u, 0, sizeof(u));
76 strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));
77 u.ut_time = time(NULL);
78 strncpy(u.ut_name, user, sizeof(u.ut_name));
79 strncpy(u.ut_host, host, sizeof(u.ut_host));
81 /* Figure out the file names. */
87 lastlog = _PATH_LASTLOG;
89 /* Update lastlog unless actually recording a logout. */
90 if (strcmp(user, "") != 0)
92 /* It is safer to bzero the lastlog structure first because some
93 systems might have some extra fields in it (e.g. SGI) */
94 memset(&ll, 0, sizeof(ll));
97 ll.ll_time = time(NULL);
98 strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));
99 strncpy(ll.ll_host, host, sizeof(ll.ll_host));
100 fd = open(lastlog, O_RDWR);
103 lseek(fd, (off_t)((long)uid * sizeof(ll)), SEEK_SET);
104 if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
105 log("Could not write %.100s: %.100s", lastlog, strerror(errno));
111 /* Records that the user has logged out. */
113 void record_logout(int pid, const char *ttyname)
115 const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */
117 logwtmp(line, "", "");