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