Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 19 | |
| 20 | #include "includes.h" |
Damien Miller | 063fdf8 | 1999-11-25 13:08:31 +1100 | [diff] [blame] | 21 | RCSID("$Id: login.c,v 1.5 1999/11/25 02:08:31 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 22 | |
| 23 | #include <utmp.h> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 24 | #include "ssh.h" |
Damien Miller | ab18c41 | 1999-11-11 10:40:23 +1100 | [diff] [blame] | 25 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 26 | #ifdef HAVE_UTIL_H |
| 27 | # include <util.h> |
| 28 | #endif |
Damien Miller | ab18c41 | 1999-11-11 10:40:23 +1100 | [diff] [blame] | 29 | #ifdef HAVE_LASTLOG_H |
| 30 | # include <lastlog.h> |
| 31 | #endif |
Damien Miller | 063fdf8 | 1999-11-25 13:08:31 +1100 | [diff] [blame] | 32 | #ifdef HAVE_LOGIN_H |
| 33 | # include <login.h> |
| 34 | #endif |
Damien Miller | ab18c41 | 1999-11-11 10:40:23 +1100 | [diff] [blame] | 35 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 36 | /* |
| 37 | * Returns the time when the user last logged in. Returns 0 if the |
| 38 | * information is not available. This must be called before record_login. |
| 39 | * The host the user logged in from will be returned in buf. |
| 40 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 42 | /* |
| 43 | * Returns the time when the user last logged in (or 0 if no previous login |
| 44 | * is found). The name of the host used last time is returned in buf. |
| 45 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 46 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 47 | unsigned long |
| 48 | get_last_login_time(uid_t uid, const char *logname, |
| 49 | char *buf, unsigned int bufsize) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 51 | struct lastlog ll; |
| 52 | char *lastlog; |
| 53 | int fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 54 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 55 | lastlog = _PATH_LASTLOG; |
| 56 | buf[0] = '\0'; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 58 | fd = open(lastlog, O_RDONLY); |
| 59 | if (fd < 0) |
| 60 | return 0; |
| 61 | lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET); |
| 62 | if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) { |
| 63 | close(fd); |
| 64 | return 0; |
| 65 | } |
| 66 | close(fd); |
| 67 | if (bufsize > sizeof(ll.ll_host) + 1) |
| 68 | bufsize = sizeof(ll.ll_host) + 1; |
| 69 | strncpy(buf, ll.ll_host, bufsize - 1); |
| 70 | buf[bufsize - 1] = 0; |
| 71 | return ll.ll_time; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | } |
| 73 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 74 | /* |
| 75 | * Records that the user has logged in. I these parts of operating systems |
| 76 | * were more standardized. |
| 77 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 78 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 79 | void |
| 80 | record_login(int pid, const char *ttyname, const char *user, uid_t uid, |
| 81 | const char *host, struct sockaddr_in * addr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 82 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 83 | int fd; |
| 84 | struct lastlog ll; |
| 85 | char *lastlog; |
| 86 | struct utmp u; |
| 87 | const char *utmp, *wtmp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 88 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 89 | /* Construct an utmp/wtmp entry. */ |
| 90 | memset(&u, 0, sizeof(u)); |
| 91 | strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line)); |
| 92 | u.ut_time = time(NULL); |
| 93 | strncpy(u.ut_name, user, sizeof(u.ut_name)); |
Damien Miller | ab18c41 | 1999-11-11 10:40:23 +1100 | [diff] [blame] | 94 | #ifdef HAVE_HOST_IN_UTMP |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 95 | strncpy(u.ut_host, host, sizeof(u.ut_host)); |
Damien Miller | ab18c41 | 1999-11-11 10:40:23 +1100 | [diff] [blame] | 96 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 97 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 98 | /* Figure out the file names. */ |
| 99 | utmp = _PATH_UTMP; |
| 100 | wtmp = _PATH_WTMP; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 101 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 102 | login(&u); |
| 103 | lastlog = _PATH_LASTLOG; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 104 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 105 | /* Update lastlog unless actually recording a logout. */ |
| 106 | if (strcmp(user, "") != 0) { |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 107 | /* |
| 108 | * It is safer to bzero the lastlog structure first because |
| 109 | * some systems might have some extra fields in it (e.g. SGI) |
| 110 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 111 | memset(&ll, 0, sizeof(ll)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 112 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 113 | /* Update lastlog. */ |
| 114 | ll.ll_time = time(NULL); |
| 115 | strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line)); |
| 116 | strncpy(ll.ll_host, host, sizeof(ll.ll_host)); |
| 117 | fd = open(lastlog, O_RDWR); |
| 118 | if (fd >= 0) { |
| 119 | lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET); |
| 120 | if (write(fd, &ll, sizeof(ll)) != sizeof(ll)) |
| 121 | log("Could not write %.100s: %.100s", lastlog, strerror(errno)); |
| 122 | close(fd); |
| 123 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 124 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 125 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 126 | |
| 127 | /* Records that the user has logged out. */ |
| 128 | |
| 129 | void |
| 130 | record_logout(int pid, const char *ttyname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 131 | { |
Damien Miller | ab18c41 | 1999-11-11 10:40:23 +1100 | [diff] [blame] | 132 | #ifdef HAVE_LIBUTIL_LOGIN |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 133 | const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */ |
| 134 | if (logout(line)) |
| 135 | logwtmp(line, "", ""); |
Damien Miller | ab18c41 | 1999-11-11 10:40:23 +1100 | [diff] [blame] | 136 | #else /* HAVE_LIBUTIL_LOGIN */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | record_login(pid, ttyname, "", -1, "", NULL); |
Damien Miller | ab18c41 | 1999-11-11 10:40:23 +1100 | [diff] [blame] | 138 | #endif /* HAVE_LIBUTIL_LOGIN */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 139 | } |