]>
Commit | Line | Data |
---|---|---|
1 | /* | |
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 | */ | |
19 | ||
20 | #include "includes.h" | |
21 | RCSID("$Id$"); | |
22 | ||
23 | #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) | |
24 | # include <utmpx.h> | |
25 | #endif | |
26 | #ifdef HAVE_UTMP_H | |
27 | # include <utmp.h> | |
28 | #endif | |
29 | #include "ssh.h" | |
30 | ||
31 | #ifdef HAVE_UTIL_H | |
32 | # include <util.h> | |
33 | #endif | |
34 | #ifdef HAVE_LASTLOG_H | |
35 | # include <lastlog.h> | |
36 | #endif | |
37 | #ifdef HAVE_LOGIN_H | |
38 | # include <login.h> | |
39 | #endif | |
40 | ||
41 | /* | |
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. | |
45 | */ | |
46 | ||
47 | /* | |
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. | |
50 | */ | |
51 | ||
52 | unsigned long | |
53 | get_last_login_time(uid_t uid, const char *logname, | |
54 | char *buf, unsigned int bufsize) | |
55 | { | |
56 | struct lastlog ll; | |
57 | char *lastlog; | |
58 | int fd; | |
59 | ||
60 | lastlog = _PATH_LASTLOG; | |
61 | buf[0] = '\0'; | |
62 | ||
63 | fd = open(lastlog, O_RDONLY); | |
64 | if (fd < 0) | |
65 | return 0; | |
66 | lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET); | |
67 | if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) { | |
68 | close(fd); | |
69 | return 0; | |
70 | } | |
71 | close(fd); | |
72 | if (bufsize > sizeof(ll.ll_host) + 1) | |
73 | bufsize = sizeof(ll.ll_host) + 1; | |
74 | strncpy(buf, ll.ll_host, bufsize - 1); | |
75 | buf[bufsize - 1] = 0; | |
76 | return ll.ll_time; | |
77 | } | |
78 | ||
79 | /* | |
80 | * Records that the user has logged in. I these parts of operating systems | |
81 | * were more standardized. | |
82 | */ | |
83 | ||
84 | void | |
85 | record_login(int pid, const char *ttyname, const char *user, uid_t uid, | |
86 | const char *host, struct sockaddr_in * addr) | |
87 | { | |
88 | int fd; | |
89 | struct lastlog ll; | |
90 | char *lastlog; | |
91 | struct UTMP_STR u; | |
92 | const char *utmp, *wtmp; | |
93 | ||
94 | /* Construct an utmp/wtmp entry. */ | |
95 | memset(&u, 0, sizeof(u)); | |
96 | strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line)); | |
97 | #if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) | |
98 | u.ut_tv.tv_sec = time(NULL); | |
99 | strncpy(u.ut_user, user, sizeof(u.ut_name)); | |
100 | #else | |
101 | u.ut_time = time(NULL); | |
102 | strncpy(u.ut_name, user, sizeof(u.ut_name)); | |
103 | #endif | |
104 | #if defined(HAVE_HOST_IN_UTMP) || defined(HAVE_HOST_IN_UTMPX) | |
105 | strncpy(u.ut_host, host, sizeof(u.ut_host)); | |
106 | #endif | |
107 | ||
108 | /* Figure out the file names. */ | |
109 | utmp = _PATH_UTMP; | |
110 | wtmp = _PATH_WTMP; | |
111 | ||
112 | login(&u); | |
113 | lastlog = _PATH_LASTLOG; | |
114 | ||
115 | /* Update lastlog unless actually recording a logout. */ | |
116 | if (strcmp(user, "") != 0) { | |
117 | /* | |
118 | * It is safer to bzero the lastlog structure first because | |
119 | * some systems might have some extra fields in it (e.g. SGI) | |
120 | */ | |
121 | memset(&ll, 0, sizeof(ll)); | |
122 | ||
123 | /* Update lastlog. */ | |
124 | ll.ll_time = time(NULL); | |
125 | strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line)); | |
126 | strncpy(ll.ll_host, host, sizeof(ll.ll_host)); | |
127 | fd = open(lastlog, O_RDWR); | |
128 | if (fd >= 0) { | |
129 | lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET); | |
130 | if (write(fd, &ll, sizeof(ll)) != sizeof(ll)) | |
131 | log("Could not write %.100s: %.100s", lastlog, strerror(errno)); | |
132 | close(fd); | |
133 | } | |
134 | } | |
135 | } | |
136 | ||
137 | /* Records that the user has logged out. */ | |
138 | ||
139 | void | |
140 | record_logout(int pid, const char *ttyname) | |
141 | { | |
142 | #ifdef HAVE_LIBUTIL_LOGIN | |
143 | const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */ | |
144 | if (logout(line)) | |
145 | logwtmp(line, "", ""); | |
146 | #else /* HAVE_LIBUTIL_LOGIN */ | |
147 | record_login(pid, ttyname, "", -1, "", NULL); | |
148 | #endif /* HAVE_LIBUTIL_LOGIN */ | |
149 | } |