Annotation of src/usr.bin/ssh/sshlogin.c, Revision 1.19
1.19 ! stevesk 1: /* $OpenBSD: sshlogin.c,v 1.18 2006/07/09 15:15:11 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.19 ! stevesk 44: #include <errno.h>
1.18 stevesk 45: #include <fcntl.h>
1.1 djm 46: #include <util.h>
47: #include <utmp.h>
1.18 stevesk 48:
1.1 djm 49: #include "sshlogin.h"
50: #include "log.h"
1.10 dtucker 51: #include "buffer.h"
52: #include "servconf.h"
53:
54: extern Buffer loginmsg;
55: extern ServerOptions options;
1.1 djm 56:
57: /*
58: * Returns the time when the user last logged in. Returns 0 if the
59: * information is not available. This must be called before record_login.
60: * The host the user logged in from will be returned in buf.
61: */
1.17 deraadt 62: time_t
1.1 djm 63: get_last_login_time(uid_t uid, const char *logname,
1.17 deraadt 64: char *buf, size_t bufsize)
1.1 djm 65: {
66: struct lastlog ll;
67: char *lastlog;
68: int fd;
1.11 djm 69: off_t pos, r;
1.1 djm 70:
71: lastlog = _PATH_LASTLOG;
72: buf[0] = '\0';
73:
74: fd = open(lastlog, O_RDONLY);
75: if (fd < 0)
76: return 0;
1.11 djm 77:
78: pos = (long) uid * sizeof(ll);
79: r = lseek(fd, pos, SEEK_SET);
80: if (r == -1) {
1.13 djm 81: error("%s: lseek: %s", __func__, strerror(errno));
1.11 djm 82: return (0);
83: }
84: if (r != pos) {
85: debug("%s: truncated lastlog", __func__);
86: return (0);
87: }
1.1 djm 88: if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) {
89: close(fd);
90: return 0;
91: }
92: close(fd);
93: if (bufsize > sizeof(ll.ll_host) + 1)
94: bufsize = sizeof(ll.ll_host) + 1;
95: strncpy(buf, ll.ll_host, bufsize - 1);
1.9 dtucker 96: buf[bufsize - 1] = '\0';
1.17 deraadt 97: return (time_t)ll.ll_time;
1.1 djm 98: }
99:
100: /*
1.10 dtucker 101: * Generate and store last login message. This must be done before
102: * login_login() is called and lastlog is updated.
103: */
1.12 djm 104: static void
1.10 dtucker 105: store_lastlog_message(const char *user, uid_t uid)
106: {
107: char *time_string, hostname[MAXHOSTNAMELEN] = "", buf[512];
108: time_t last_login_time;
109:
110: if (!options.print_lastlog)
111: return;
112:
113: last_login_time = get_last_login_time(uid, user, hostname,
114: sizeof(hostname));
115:
116: if (last_login_time != 0) {
117: time_string = ctime(&last_login_time);
118: if (strchr(time_string, '\n'))
119: *strchr(time_string, '\n') = '\0';
120: if (strcmp(hostname, "") == 0)
121: snprintf(buf, sizeof(buf), "Last login: %s\r\n",
122: time_string);
123: else
124: snprintf(buf, sizeof(buf), "Last login: %s from %s\r\n",
125: time_string, hostname);
126: buffer_append(&loginmsg, buf, strlen(buf));
127: }
128: }
129:
130: /*
1.7 markus 131: * Records that the user has logged in. I wish these parts of operating
132: * systems were more standardized.
1.1 djm 133: */
134: void
1.8 avsm 135: record_login(pid_t pid, const char *tty, const char *user, uid_t uid,
1.15 deraadt 136: const char *host, struct sockaddr *addr, socklen_t addrlen)
1.1 djm 137: {
138: int fd;
139: struct lastlog ll;
140: char *lastlog;
141: struct utmp u;
1.10 dtucker 142:
143: /* save previous login details before writing new */
144: store_lastlog_message(user, uid);
1.1 djm 145:
146: /* Construct an utmp/wtmp entry. */
147: memset(&u, 0, sizeof(u));
1.8 avsm 148: strncpy(u.ut_line, tty + 5, sizeof(u.ut_line));
1.1 djm 149: u.ut_time = time(NULL);
150: strncpy(u.ut_name, user, sizeof(u.ut_name));
151: strncpy(u.ut_host, host, sizeof(u.ut_host));
152:
153: login(&u);
154: lastlog = _PATH_LASTLOG;
155:
156: /* Update lastlog unless actually recording a logout. */
157: if (strcmp(user, "") != 0) {
158: /*
159: * It is safer to bzero the lastlog structure first because
160: * some systems might have some extra fields in it (e.g. SGI)
161: */
162: memset(&ll, 0, sizeof(ll));
163:
164: /* Update lastlog. */
165: ll.ll_time = time(NULL);
1.8 avsm 166: strncpy(ll.ll_line, tty + 5, sizeof(ll.ll_line));
1.1 djm 167: strncpy(ll.ll_host, host, sizeof(ll.ll_host));
168: fd = open(lastlog, O_RDWR);
169: if (fd >= 0) {
170: lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
171: if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
1.6 itojun 172: logit("Could not write %.100s: %.100s", lastlog, strerror(errno));
1.1 djm 173: close(fd);
174: }
175: }
176: }
177:
178: /* Records that the user has logged out. */
179: void
1.8 avsm 180: record_logout(pid_t pid, const char *tty)
1.1 djm 181: {
1.8 avsm 182: const char *line = tty + 5; /* /dev/ttyq8 -> ttyq8 */
1.1 djm 183: if (logout(line))
184: logwtmp(line, "", "");
185: }