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

Annotation of src/usr.bin/ssh/login.c, Revision 1.12

1.1       deraadt     1: /*
1.12    ! markus      2:  *
1.9       deraadt     3:  * login.c
1.12    ! markus      4:  *
1.9       deraadt     5:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
1.12    ! markus      6:  *
1.9       deraadt     7:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      8:  *                    All rights reserved
1.12    ! markus      9:  *
1.9       deraadt    10:  * Created: Fri Mar 24 14:51:08 1995 ylo
1.12    ! markus     11:  *
1.9       deraadt    12:  * This file performs some of the things login(1) normally does.  We cannot
                     13:  * easily use something like login -p -h host -f user, because there are
                     14:  * several different logins around, and it is hard to determined what kind of
                     15:  * login the current system has.  Also, we want to be able to execute commands
                     16:  * on a tty.
1.12    ! markus     17:  *
1.9       deraadt    18:  */
1.1       deraadt    19:
                     20: #include "includes.h"
1.12    ! markus     21: RCSID("$Id: login.c,v 1.11 2000/01/04 00:07:59 markus Exp $");
1.1       deraadt    22:
1.2       dugsong    23: #include <util.h>
1.1       deraadt    24: #include <utmp.h>
                     25: #include "ssh.h"
                     26:
1.10      markus     27: /*
                     28:  * Returns the time when the user last logged in.  Returns 0 if the
                     29:  * information is not available.  This must be called before record_login.
                     30:  * The host the user logged in from will be returned in buf.
                     31:  */
                     32:
                     33: /*
                     34:  * Returns the time when the user last logged in (or 0 if no previous login
                     35:  * is found).  The name of the host used last time is returned in buf.
                     36:  */
1.1       deraadt    37:
1.12    ! markus     38: unsigned long
1.8       markus     39: get_last_login_time(uid_t uid, const char *logname,
                     40:                    char *buf, unsigned int bufsize)
1.1       deraadt    41: {
1.8       markus     42:        struct lastlog ll;
                     43:        char *lastlog;
                     44:        int fd;
                     45:
                     46:        lastlog = _PATH_LASTLOG;
                     47:        buf[0] = '\0';
                     48:
                     49:        fd = open(lastlog, O_RDONLY);
                     50:        if (fd < 0)
                     51:                return 0;
                     52:        lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
                     53:        if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) {
                     54:                close(fd);
                     55:                return 0;
                     56:        }
                     57:        close(fd);
                     58:        if (bufsize > sizeof(ll.ll_host) + 1)
                     59:                bufsize = sizeof(ll.ll_host) + 1;
                     60:        strncpy(buf, ll.ll_host, bufsize - 1);
                     61:        buf[bufsize - 1] = 0;
                     62:        return ll.ll_time;
1.1       deraadt    63: }
                     64:
1.10      markus     65: /*
                     66:  * Records that the user has logged in.  I these parts of operating systems
                     67:  * were more standardized.
                     68:  */
1.1       deraadt    69:
1.12    ! markus     70: void
1.8       markus     71: record_login(int pid, const char *ttyname, const char *user, uid_t uid,
1.11      markus     72:             const char *host, struct sockaddr * addr)
1.1       deraadt    73: {
1.8       markus     74:        int fd;
                     75:        struct lastlog ll;
                     76:        char *lastlog;
                     77:        struct utmp u;
                     78:        const char *utmp, *wtmp;
                     79:
                     80:        /* Construct an utmp/wtmp entry. */
                     81:        memset(&u, 0, sizeof(u));
                     82:        strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));
                     83:        u.ut_time = time(NULL);
                     84:        strncpy(u.ut_name, user, sizeof(u.ut_name));
                     85:        strncpy(u.ut_host, host, sizeof(u.ut_host));
                     86:
                     87:        /* Figure out the file names. */
                     88:        utmp = _PATH_UTMP;
                     89:        wtmp = _PATH_WTMP;
                     90:
                     91:        login(&u);
                     92:        lastlog = _PATH_LASTLOG;
                     93:
                     94:        /* Update lastlog unless actually recording a logout. */
                     95:        if (strcmp(user, "") != 0) {
1.10      markus     96:                /*
                     97:                 * It is safer to bzero the lastlog structure first because
                     98:                 * some systems might have some extra fields in it (e.g. SGI)
                     99:                 */
1.8       markus    100:                memset(&ll, 0, sizeof(ll));
                    101:
                    102:                /* Update lastlog. */
                    103:                ll.ll_time = time(NULL);
                    104:                strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));
                    105:                strncpy(ll.ll_host, host, sizeof(ll.ll_host));
                    106:                fd = open(lastlog, O_RDWR);
                    107:                if (fd >= 0) {
                    108:                        lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
                    109:                        if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
                    110:                                log("Could not write %.100s: %.100s", lastlog, strerror(errno));
                    111:                        close(fd);
                    112:                }
1.1       deraadt   113:        }
                    114: }
1.8       markus    115:
1.1       deraadt   116: /* Records that the user has logged out. */
                    117:
1.12    ! markus    118: void
1.8       markus    119: record_logout(int pid, const char *ttyname)
1.1       deraadt   120: {
1.8       markus    121:        const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */
                    122:        if (logout(line))
                    123:                logwtmp(line, "", "");
1.1       deraadt   124: }