blob: ddd58ff6747edbbaa01f976e24fe3360ec045c1b [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
3 * login.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 * Created: Fri Mar 24 14:51:08 1995 ylo
11 *
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.
17 *
18 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
20#include "includes.h"
Damien Miller5428f641999-11-25 11:54:57 +110021RCSID("$Id: login.c,v 1.4 1999/11/25 00:54:59 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022
23#include <utmp.h>
Damien Miller95def091999-11-25 00:26:21 +110024#include "ssh.h"
Damien Millerab18c411999-11-11 10:40:23 +110025
Damien Miller95def091999-11-25 00:26:21 +110026#ifdef HAVE_UTIL_H
27# include <util.h>
28#endif
Damien Millerab18c411999-11-11 10:40:23 +110029#ifdef HAVE_LASTLOG_H
30# include <lastlog.h>
31#endif
32
Damien Miller5428f641999-11-25 11:54:57 +110033/*
34 * Returns the time when the user last logged in. Returns 0 if the
35 * information is not available. This must be called before record_login.
36 * The host the user logged in from will be returned in buf.
37 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
Damien Miller5428f641999-11-25 11:54:57 +110039/*
40 * Returns the time when the user last logged in (or 0 if no previous login
41 * is found). The name of the host used last time is returned in buf.
42 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
Damien Miller95def091999-11-25 00:26:21 +110044unsigned long
45get_last_login_time(uid_t uid, const char *logname,
46 char *buf, unsigned int bufsize)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047{
Damien Miller95def091999-11-25 00:26:21 +110048 struct lastlog ll;
49 char *lastlog;
50 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
Damien Miller95def091999-11-25 00:26:21 +110052 lastlog = _PATH_LASTLOG;
53 buf[0] = '\0';
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Miller95def091999-11-25 00:26:21 +110055 fd = open(lastlog, O_RDONLY);
56 if (fd < 0)
57 return 0;
58 lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
59 if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) {
60 close(fd);
61 return 0;
62 }
63 close(fd);
64 if (bufsize > sizeof(ll.ll_host) + 1)
65 bufsize = sizeof(ll.ll_host) + 1;
66 strncpy(buf, ll.ll_host, bufsize - 1);
67 buf[bufsize - 1] = 0;
68 return ll.ll_time;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069}
70
Damien Miller5428f641999-11-25 11:54:57 +110071/*
72 * Records that the user has logged in. I these parts of operating systems
73 * were more standardized.
74 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Damien Miller95def091999-11-25 00:26:21 +110076void
77record_login(int pid, const char *ttyname, const char *user, uid_t uid,
78 const char *host, struct sockaddr_in * addr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079{
Damien Miller95def091999-11-25 00:26:21 +110080 int fd;
81 struct lastlog ll;
82 char *lastlog;
83 struct utmp u;
84 const char *utmp, *wtmp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Miller95def091999-11-25 00:26:21 +110086 /* Construct an utmp/wtmp entry. */
87 memset(&u, 0, sizeof(u));
88 strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));
89 u.ut_time = time(NULL);
90 strncpy(u.ut_name, user, sizeof(u.ut_name));
Damien Millerab18c411999-11-11 10:40:23 +110091#ifdef HAVE_HOST_IN_UTMP
Damien Miller95def091999-11-25 00:26:21 +110092 strncpy(u.ut_host, host, sizeof(u.ut_host));
Damien Millerab18c411999-11-11 10:40:23 +110093#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
Damien Miller95def091999-11-25 00:26:21 +110095 /* Figure out the file names. */
96 utmp = _PATH_UTMP;
97 wtmp = _PATH_WTMP;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller95def091999-11-25 00:26:21 +110099 login(&u);
100 lastlog = _PATH_LASTLOG;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
Damien Miller95def091999-11-25 00:26:21 +1100102 /* Update lastlog unless actually recording a logout. */
103 if (strcmp(user, "") != 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100104 /*
105 * It is safer to bzero the lastlog structure first because
106 * some systems might have some extra fields in it (e.g. SGI)
107 */
Damien Miller95def091999-11-25 00:26:21 +1100108 memset(&ll, 0, sizeof(ll));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Damien Miller95def091999-11-25 00:26:21 +1100110 /* Update lastlog. */
111 ll.ll_time = time(NULL);
112 strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));
113 strncpy(ll.ll_host, host, sizeof(ll.ll_host));
114 fd = open(lastlog, O_RDWR);
115 if (fd >= 0) {
116 lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
117 if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
118 log("Could not write %.100s: %.100s", lastlog, strerror(errno));
119 close(fd);
120 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122}
Damien Miller95def091999-11-25 00:26:21 +1100123
124/* Records that the user has logged out. */
125
126void
127record_logout(int pid, const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128{
Damien Millerab18c411999-11-11 10:40:23 +1100129#ifdef HAVE_LIBUTIL_LOGIN
Damien Miller95def091999-11-25 00:26:21 +1100130 const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */
131 if (logout(line))
132 logwtmp(line, "", "");
Damien Millerab18c411999-11-11 10:40:23 +1100133#else /* HAVE_LIBUTIL_LOGIN */
Damien Miller95def091999-11-25 00:26:21 +1100134 record_login(pid, ttyname, "", -1, "", NULL);
Damien Millerab18c411999-11-11 10:40:23 +1100135#endif /* HAVE_LIBUTIL_LOGIN */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136}