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