1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi> 3*7c478bd9Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4*7c478bd9Sstevel@tonic-gate * All rights reserved 5*7c478bd9Sstevel@tonic-gate * This file performs some of the things login(1) normally does. We cannot 6*7c478bd9Sstevel@tonic-gate * easily use something like login -p -h host -f user, because there are 7*7c478bd9Sstevel@tonic-gate * several different logins around, and it is hard to determined what kind of 8*7c478bd9Sstevel@tonic-gate * login the current system has. Also, we want to be able to execute commands 9*7c478bd9Sstevel@tonic-gate * on a tty. 10*7c478bd9Sstevel@tonic-gate * 11*7c478bd9Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software 12*7c478bd9Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this 13*7c478bd9Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is 14*7c478bd9Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be 15*7c478bd9Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell". 16*7c478bd9Sstevel@tonic-gate * 17*7c478bd9Sstevel@tonic-gate * Copyright (c) 1999 Theo de Raadt. All rights reserved. 18*7c478bd9Sstevel@tonic-gate * Copyright (c) 1999 Markus Friedl. All rights reserved. 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 21*7c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions 22*7c478bd9Sstevel@tonic-gate * are met: 23*7c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 24*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 25*7c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 26*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 27*7c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 28*7c478bd9Sstevel@tonic-gate * 29*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 30*7c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 31*7c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 32*7c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 33*7c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 34*7c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35*7c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36*7c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37*7c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 38*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39*7c478bd9Sstevel@tonic-gate */ 40*7c478bd9Sstevel@tonic-gate /* 41*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 42*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 43*7c478bd9Sstevel@tonic-gate */ 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate #include "includes.h" 46*7c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: sshlogin.c,v 1.5 2002/08/29 15:57:25 stevesk Exp $"); 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate #include "loginrec.h" 51*7c478bd9Sstevel@tonic-gate #include "log.h" 52*7c478bd9Sstevel@tonic-gate #include "servconf.h" 53*7c478bd9Sstevel@tonic-gate #include "canohost.h" 54*7c478bd9Sstevel@tonic-gate #include "packet.h" 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate extern u_int utmp_len; 57*7c478bd9Sstevel@tonic-gate extern ServerOptions options; 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate /* 60*7c478bd9Sstevel@tonic-gate * Returns the time when the user last logged in. Returns 0 if the 61*7c478bd9Sstevel@tonic-gate * information is not available. This must be called before record_login. 62*7c478bd9Sstevel@tonic-gate * The host the user logged in from will be returned in buf. 63*7c478bd9Sstevel@tonic-gate */ 64*7c478bd9Sstevel@tonic-gate u_long 65*7c478bd9Sstevel@tonic-gate get_last_login_time(uid_t uid, const char *logname, 66*7c478bd9Sstevel@tonic-gate char *buf, u_int bufsize) 67*7c478bd9Sstevel@tonic-gate { 68*7c478bd9Sstevel@tonic-gate struct logininfo li; 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate (void) login_get_lastlog(&li, uid); 71*7c478bd9Sstevel@tonic-gate (void) strlcpy(buf, li.hostname, bufsize); 72*7c478bd9Sstevel@tonic-gate return li.tv_sec; 73*7c478bd9Sstevel@tonic-gate } 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate /* 76*7c478bd9Sstevel@tonic-gate * Records that the user has logged in. I these parts of operating systems 77*7c478bd9Sstevel@tonic-gate * were more standardized. 78*7c478bd9Sstevel@tonic-gate */ 79*7c478bd9Sstevel@tonic-gate void 80*7c478bd9Sstevel@tonic-gate record_login(pid_t pid, const char *ttyname, const char *progname, 81*7c478bd9Sstevel@tonic-gate const char *user) 82*7c478bd9Sstevel@tonic-gate { 83*7c478bd9Sstevel@tonic-gate struct logininfo *li; 84*7c478bd9Sstevel@tonic-gate static int initialized = 0; 85*7c478bd9Sstevel@tonic-gate static socklen_t fromlen; 86*7c478bd9Sstevel@tonic-gate static struct sockaddr_storage from; 87*7c478bd9Sstevel@tonic-gate static const char *remote_name_or_ip; 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate if (pid == 0) 90*7c478bd9Sstevel@tonic-gate pid = getpid(); 91*7c478bd9Sstevel@tonic-gate /* 92*7c478bd9Sstevel@tonic-gate * Get IP address of client. If the connection is not a socket, let 93*7c478bd9Sstevel@tonic-gate * the address be 0.0.0.0. 94*7c478bd9Sstevel@tonic-gate */ 95*7c478bd9Sstevel@tonic-gate if (!initialized) { 96*7c478bd9Sstevel@tonic-gate (void) memset(&from, 0, sizeof(from)); 97*7c478bd9Sstevel@tonic-gate if (packet_connection_is_on_socket()) { 98*7c478bd9Sstevel@tonic-gate fromlen = sizeof(from); 99*7c478bd9Sstevel@tonic-gate if (getpeername(packet_get_connection_in(), 100*7c478bd9Sstevel@tonic-gate (struct sockaddr *) &from, &fromlen) < 0) { 101*7c478bd9Sstevel@tonic-gate debug("getpeername: %.100s", strerror(errno)); 102*7c478bd9Sstevel@tonic-gate fatal_cleanup(); 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate } 105*7c478bd9Sstevel@tonic-gate remote_name_or_ip = get_remote_name_or_ip(utmp_len, 106*7c478bd9Sstevel@tonic-gate options.verify_reverse_mapping); 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate initialized = 1; 109*7c478bd9Sstevel@tonic-gate } 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate li = login_alloc_entry(pid, user, remote_name_or_ip, ttyname, progname); 112*7c478bd9Sstevel@tonic-gate login_set_addr(li, (struct sockaddr*) &from, sizeof(struct sockaddr)); 113*7c478bd9Sstevel@tonic-gate (void) login_login(li); 114*7c478bd9Sstevel@tonic-gate login_free_entry(li); 115*7c478bd9Sstevel@tonic-gate } 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate #ifdef LOGIN_NEEDS_UTMPX 118*7c478bd9Sstevel@tonic-gate void 119*7c478bd9Sstevel@tonic-gate record_utmp_only(pid_t pid, const char *ttyname, const char *user, 120*7c478bd9Sstevel@tonic-gate const char *host, struct sockaddr * addr) 121*7c478bd9Sstevel@tonic-gate { 122*7c478bd9Sstevel@tonic-gate struct logininfo *li; 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate li = login_alloc_entry(pid, user, host, ttyname, NULL); 125*7c478bd9Sstevel@tonic-gate login_set_addr(li, addr, sizeof(struct sockaddr)); 126*7c478bd9Sstevel@tonic-gate (void) login_utmp_only(li); 127*7c478bd9Sstevel@tonic-gate login_free_entry(li); 128*7c478bd9Sstevel@tonic-gate } 129*7c478bd9Sstevel@tonic-gate #endif 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate /* Records that the user has logged out. */ 132*7c478bd9Sstevel@tonic-gate void 133*7c478bd9Sstevel@tonic-gate record_logout(pid_t pid, const char *ttyname, const char *progname, 134*7c478bd9Sstevel@tonic-gate const char *user) 135*7c478bd9Sstevel@tonic-gate { 136*7c478bd9Sstevel@tonic-gate struct logininfo *li; 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate li = login_alloc_entry(pid, user, NULL, ttyname, progname); 139*7c478bd9Sstevel@tonic-gate (void) login_logout(li); 140*7c478bd9Sstevel@tonic-gate login_free_entry(li); 141*7c478bd9Sstevel@tonic-gate } 142