Annotation of src/usr.bin/ssh/sshlogin.c, Revision 1.23
1.23 ! stevesk 1: /* $OpenBSD: sshlogin.c,v 1.22 2006/07/22 20:48:23 stevesk Exp $ */
1.1 djm 2: /*
3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * This file performs some of the things login(1) normally does. We cannot
7: * easily use something like login -p -h host -f user, because there are
8: * several different logins around, and it is hard to determined what kind of
9: * login the current system has. Also, we want to be able to execute commands
10: * on a tty.
11: *
12: * As far as I am concerned, the code I have written for this software
13: * can be used freely for any purpose. Any derived versions of this
14: * software must be clearly marked as such, and if the derived work is
15: * incompatible with the protocol description in the RFC file, it must be
16: * called by a name other than "ssh" or "Secure Shell".
17: *
18: * Copyright (c) 1999 Theo de Raadt. All rights reserved.
19: * Copyright (c) 1999 Markus Friedl. All rights reserved.
20: *
21: * Redistribution and use in source and binary forms, with or without
22: * modification, are permitted provided that the following conditions
23: * are met:
24: * 1. Redistributions of source code must retain the above copyright
25: * notice, this list of conditions and the following disclaimer.
26: * 2. Redistributions in binary form must reproduce the above copyright
27: * notice, this list of conditions and the following disclaimer in the
28: * documentation and/or other materials provided with the distribution.
29: *
30: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40: */
41:
42: #include "includes.h"
43:
1.20 stevesk 44: #include <sys/types.h>
1.23 ! stevesk 45: #include <sys/param.h>
1.20 stevesk 46:
1.19 stevesk 47: #include <errno.h>
1.18 stevesk 48: #include <fcntl.h>
1.22 stevesk 49: #include <string.h>
1.21 stevesk 50: #include <time.h>
1.20 stevesk 51: #include <unistd.h>
1.1 djm 52: #include <util.h>
53: #include <utmp.h>
1.18 stevesk 54:
1.1 djm 55: #include "sshlogin.h"
56: #include "log.h"
1.10 dtucker 57: #include "buffer.h"
58: #include "servconf.h"
59:
60: extern Buffer loginmsg;
61: extern ServerOptions options;
1.1 djm 62:
63: /*
64: * Returns the time when the user last logged in. Returns 0 if the
65: * information is not available. This must be called before record_login.
66: * The host the user logged in from will be returned in buf.
67: */
1.17 deraadt 68: time_t
1.1 djm 69: get_last_login_time(uid_t uid, const char *logname,
1.17 deraadt 70: char *buf, size_t bufsize)
1.1 djm 71: {
72: struct lastlog ll;
73: char *lastlog;
74: int fd;
1.11 djm 75: off_t pos, r;
1.1 djm 76:
77: lastlog = _PATH_LASTLOG;
78: buf[0] = '\0';
79:
80: fd = open(lastlog, O_RDONLY);
81: if (fd < 0)
82: return 0;
1.11 djm 83:
84: pos = (long) uid * sizeof(ll);
85: r = lseek(fd, pos, SEEK_SET);
86: if (r == -1) {
1.13 djm 87: error("%s: lseek: %s", __func__, strerror(errno));
1.11 djm 88: return (0);
89: }
90: if (r != pos) {
91: debug("%s: truncated lastlog", __func__);
92: return (0);
93: }
1.1 djm 94: if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) {
95: close(fd);
96: return 0;
97: }
98: close(fd);
99: if (bufsize > sizeof(ll.ll_host) + 1)
100: bufsize = sizeof(ll.ll_host) + 1;
101: strncpy(buf, ll.ll_host, bufsize - 1);
1.9 dtucker 102: buf[bufsize - 1] = '\0';
1.17 deraadt 103: return (time_t)ll.ll_time;
1.1 djm 104: }
105:
106: /*
1.10 dtucker 107: * Generate and store last login message. This must be done before
108: * login_login() is called and lastlog is updated.
109: */
1.12 djm 110: static void
1.10 dtucker 111: store_lastlog_message(const char *user, uid_t uid)
112: {
113: char *time_string, hostname[MAXHOSTNAMELEN] = "", buf[512];
114: time_t last_login_time;
115:
116: if (!options.print_lastlog)
117: return;
118:
119: last_login_time = get_last_login_time(uid, user, hostname,
120: sizeof(hostname));
121:
122: if (last_login_time != 0) {
123: time_string = ctime(&last_login_time);
124: if (strchr(time_string, '\n'))
125: *strchr(time_string, '\n') = '\0';
126: if (strcmp(hostname, "") == 0)
127: snprintf(buf, sizeof(buf), "Last login: %s\r\n",
128: time_string);
129: else
130: snprintf(buf, sizeof(buf), "Last login: %s from %s\r\n",
131: time_string, hostname);
132: buffer_append(&loginmsg, buf, strlen(buf));
133: }
134: }
135:
136: /*
1.7 markus 137: * Records that the user has logged in. I wish these parts of operating
138: * systems were more standardized.
1.1 djm 139: */
140: void
1.8 avsm 141: record_login(pid_t pid, const char *tty, const char *user, uid_t uid,
1.15 deraadt 142: const char *host, struct sockaddr *addr, socklen_t addrlen)
1.1 djm 143: {
144: int fd;
145: struct lastlog ll;
146: char *lastlog;
147: struct utmp u;
1.10 dtucker 148:
149: /* save previous login details before writing new */
150: store_lastlog_message(user, uid);
1.1 djm 151:
152: /* Construct an utmp/wtmp entry. */
153: memset(&u, 0, sizeof(u));
1.8 avsm 154: strncpy(u.ut_line, tty + 5, sizeof(u.ut_line));
1.1 djm 155: u.ut_time = time(NULL);
156: strncpy(u.ut_name, user, sizeof(u.ut_name));
157: strncpy(u.ut_host, host, sizeof(u.ut_host));
158:
159: login(&u);
160: lastlog = _PATH_LASTLOG;
161:
162: /* Update lastlog unless actually recording a logout. */
163: if (strcmp(user, "") != 0) {
164: /*
165: * It is safer to bzero the lastlog structure first because
166: * some systems might have some extra fields in it (e.g. SGI)
167: */
168: memset(&ll, 0, sizeof(ll));
169:
170: /* Update lastlog. */
171: ll.ll_time = time(NULL);
1.8 avsm 172: strncpy(ll.ll_line, tty + 5, sizeof(ll.ll_line));
1.1 djm 173: strncpy(ll.ll_host, host, sizeof(ll.ll_host));
174: fd = open(lastlog, O_RDWR);
175: if (fd >= 0) {
176: lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
177: if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
1.6 itojun 178: logit("Could not write %.100s: %.100s", lastlog, strerror(errno));
1.1 djm 179: close(fd);
180: }
181: }
182: }
183:
184: /* Records that the user has logged out. */
185: void
1.8 avsm 186: record_logout(pid_t pid, const char *tty)
1.1 djm 187: {
1.8 avsm 188: const char *line = tty + 5; /* /dev/ttyq8 -> ttyq8 */
1.1 djm 189: if (logout(line))
190: logwtmp(line, "", "");
191: }