[BACK]Return to sshlogin.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sshlogin.c, Revision 1.33

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