[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.13.6.1

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