17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi>
37c478bd9Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
47c478bd9Sstevel@tonic-gate * All rights reserved
57c478bd9Sstevel@tonic-gate * This file performs some of the things login(1) normally does. We cannot
67c478bd9Sstevel@tonic-gate * easily use something like login -p -h host -f user, because there are
77c478bd9Sstevel@tonic-gate * several different logins around, and it is hard to determined what kind of
87c478bd9Sstevel@tonic-gate * login the current system has. Also, we want to be able to execute commands
97c478bd9Sstevel@tonic-gate * on a tty.
107c478bd9Sstevel@tonic-gate *
117c478bd9Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software
127c478bd9Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this
137c478bd9Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is
147c478bd9Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be
157c478bd9Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell".
167c478bd9Sstevel@tonic-gate *
177c478bd9Sstevel@tonic-gate * Copyright (c) 1999 Theo de Raadt. All rights reserved.
187c478bd9Sstevel@tonic-gate * Copyright (c) 1999 Markus Friedl. All rights reserved.
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
217c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
227c478bd9Sstevel@tonic-gate * are met:
237c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
247c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
257c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
267c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
277c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
287c478bd9Sstevel@tonic-gate *
297c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
307c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
317c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
327c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
337c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
347c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
357c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
367c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
377c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
387c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
397c478bd9Sstevel@tonic-gate */
407c478bd9Sstevel@tonic-gate /*
41b9aa66a7SJan Pechanec * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
427c478bd9Sstevel@tonic-gate * Use is subject to license terms.
437c478bd9Sstevel@tonic-gate */
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate #include "includes.h"
467c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: sshlogin.c,v 1.5 2002/08/29 15:57:25 stevesk Exp $");
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #include "loginrec.h"
497c478bd9Sstevel@tonic-gate #include "log.h"
50*b07b2f5cSHuie-Ying Lee #include "buffer.h"
517c478bd9Sstevel@tonic-gate #include "servconf.h"
527c478bd9Sstevel@tonic-gate #include "canohost.h"
537c478bd9Sstevel@tonic-gate #include "packet.h"
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate extern u_int utmp_len;
567c478bd9Sstevel@tonic-gate extern ServerOptions options;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate * Returns the time when the user last logged in. Returns 0 if the
607c478bd9Sstevel@tonic-gate * information is not available. This must be called before record_login.
617c478bd9Sstevel@tonic-gate * The host the user logged in from will be returned in buf.
627c478bd9Sstevel@tonic-gate */
637c478bd9Sstevel@tonic-gate u_long
get_last_login_time(uid_t uid,const char * logname,char * buf,u_int bufsize)647c478bd9Sstevel@tonic-gate get_last_login_time(uid_t uid, const char *logname,
657c478bd9Sstevel@tonic-gate char *buf, u_int bufsize)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate struct logininfo li;
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate (void) login_get_lastlog(&li, uid);
707c478bd9Sstevel@tonic-gate (void) strlcpy(buf, li.hostname, bufsize);
717c478bd9Sstevel@tonic-gate return li.tv_sec;
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate /*
75b9aa66a7SJan Pechanec * Records that the user has logged in. If only these parts of operating
76b9aa66a7SJan Pechanec * systems were more standardized.
777c478bd9Sstevel@tonic-gate */
787c478bd9Sstevel@tonic-gate void
record_login(pid_t pid,const char * ttyname,const char * progname,const char * user)797c478bd9Sstevel@tonic-gate record_login(pid_t pid, const char *ttyname, const char *progname,
807c478bd9Sstevel@tonic-gate const char *user)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate struct logininfo *li;
837c478bd9Sstevel@tonic-gate static int initialized = 0;
847c478bd9Sstevel@tonic-gate static socklen_t fromlen;
857c478bd9Sstevel@tonic-gate static struct sockaddr_storage from;
867c478bd9Sstevel@tonic-gate static const char *remote_name_or_ip;
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate if (pid == 0)
897c478bd9Sstevel@tonic-gate pid = getpid();
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate * Get IP address of client. If the connection is not a socket, let
927c478bd9Sstevel@tonic-gate * the address be 0.0.0.0.
937c478bd9Sstevel@tonic-gate */
947c478bd9Sstevel@tonic-gate if (!initialized) {
957c478bd9Sstevel@tonic-gate (void) memset(&from, 0, sizeof(from));
967c478bd9Sstevel@tonic-gate if (packet_connection_is_on_socket()) {
977c478bd9Sstevel@tonic-gate fromlen = sizeof(from);
987c478bd9Sstevel@tonic-gate if (getpeername(packet_get_connection_in(),
997c478bd9Sstevel@tonic-gate (struct sockaddr *) &from, &fromlen) < 0) {
1007c478bd9Sstevel@tonic-gate debug("getpeername: %.100s", strerror(errno));
1017c478bd9Sstevel@tonic-gate fatal_cleanup();
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate remote_name_or_ip = get_remote_name_or_ip(utmp_len,
1057c478bd9Sstevel@tonic-gate options.verify_reverse_mapping);
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate initialized = 1;
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate li = login_alloc_entry(pid, user, remote_name_or_ip, ttyname, progname);
1117c478bd9Sstevel@tonic-gate login_set_addr(li, (struct sockaddr*) &from, sizeof(struct sockaddr));
1127c478bd9Sstevel@tonic-gate (void) login_login(li);
1137c478bd9Sstevel@tonic-gate login_free_entry(li);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate /* Records that the user has logged out. */
1177c478bd9Sstevel@tonic-gate void
record_logout(pid_t pid,const char * ttyname,const char * progname,const char * user)1187c478bd9Sstevel@tonic-gate record_logout(pid_t pid, const char *ttyname, const char *progname,
1197c478bd9Sstevel@tonic-gate const char *user)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate struct logininfo *li;
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate li = login_alloc_entry(pid, user, NULL, ttyname, progname);
1247c478bd9Sstevel@tonic-gate (void) login_logout(li);
1257c478bd9Sstevel@tonic-gate login_free_entry(li);
1267c478bd9Sstevel@tonic-gate }
127