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

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