]> andersk Git - openssh.git/blame - login.c
Made utmpx optional
[openssh.git] / login.c
CommitLineData
8efc0c15 1/*
5260325f 2 *
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 */
8efc0c15 19
20#include "includes.h"
21RCSID("$Id$");
22
92f90c57 23#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
8946db53 24# include <utmpx.h>
25#endif
26#ifdef HAVE_UTMP_H
27# include <utmp.h>
28#endif
5260325f 29#include "ssh.h"
4cca272e 30
5260325f 31#ifdef HAVE_UTIL_H
32# include <util.h>
33#endif
4cca272e 34#ifdef HAVE_LASTLOG_H
35# include <lastlog.h>
36#endif
09041313 37#ifdef HAVE_LOGIN_H
38# include <login.h>
39#endif
4cca272e 40
aa3378df 41/*
42 * Returns the time when the user last logged in. Returns 0 if the
43 * information is not available. This must be called before record_login.
44 * The host the user logged in from will be returned in buf.
45 */
8efc0c15 46
aa3378df 47/*
48 * Returns the time when the user last logged in (or 0 if no previous login
49 * is found). The name of the host used last time is returned in buf.
50 */
8efc0c15 51
5260325f 52unsigned long
53get_last_login_time(uid_t uid, const char *logname,
54 char *buf, unsigned int bufsize)
8efc0c15 55{
5260325f 56 struct lastlog ll;
57 char *lastlog;
58 int fd;
59
60 lastlog = _PATH_LASTLOG;
61 buf[0] = '\0';
62
63 fd = open(lastlog, O_RDONLY);
64 if (fd < 0)
65 return 0;
66 lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
67 if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) {
68 close(fd);
69 return 0;
70 }
71 close(fd);
72 if (bufsize > sizeof(ll.ll_host) + 1)
73 bufsize = sizeof(ll.ll_host) + 1;
74 strncpy(buf, ll.ll_host, bufsize - 1);
75 buf[bufsize - 1] = 0;
76 return ll.ll_time;
8efc0c15 77}
78
aa3378df 79/*
80 * Records that the user has logged in. I these parts of operating systems
81 * were more standardized.
82 */
8efc0c15 83
5260325f 84void
85record_login(int pid, const char *ttyname, const char *user, uid_t uid,
86 const char *host, struct sockaddr_in * addr)
8efc0c15 87{
5260325f 88 int fd;
89 struct lastlog ll;
90 char *lastlog;
8946db53 91 struct UTMP_STR u;
5260325f 92 const char *utmp, *wtmp;
93
94 /* Construct an utmp/wtmp entry. */
95 memset(&u, 0, sizeof(u));
96 strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));
92f90c57 97#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
8946db53 98 u.ut_tv.tv_sec = time(NULL);
99 strncpy(u.ut_user, user, sizeof(u.ut_name));
100#else
5260325f 101 u.ut_time = time(NULL);
102 strncpy(u.ut_name, user, sizeof(u.ut_name));
8946db53 103#endif
104#if defined(HAVE_HOST_IN_UTMP) || defined(HAVE_HOST_IN_UTMPX)
5260325f 105 strncpy(u.ut_host, host, sizeof(u.ut_host));
4cca272e 106#endif
8efc0c15 107
5260325f 108 /* Figure out the file names. */
109 utmp = _PATH_UTMP;
110 wtmp = _PATH_WTMP;
111
112 login(&u);
113 lastlog = _PATH_LASTLOG;
114
115 /* Update lastlog unless actually recording a logout. */
116 if (strcmp(user, "") != 0) {
aa3378df 117 /*
118 * It is safer to bzero the lastlog structure first because
119 * some systems might have some extra fields in it (e.g. SGI)
120 */
5260325f 121 memset(&ll, 0, sizeof(ll));
122
123 /* Update lastlog. */
124 ll.ll_time = time(NULL);
125 strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));
126 strncpy(ll.ll_host, host, sizeof(ll.ll_host));
127 fd = open(lastlog, O_RDWR);
128 if (fd >= 0) {
129 lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
130 if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
131 log("Could not write %.100s: %.100s", lastlog, strerror(errno));
132 close(fd);
133 }
8efc0c15 134 }
8efc0c15 135}
5260325f 136
137/* Records that the user has logged out. */
138
139void
140record_logout(int pid, const char *ttyname)
8efc0c15 141{
4cca272e 142#ifdef HAVE_LIBUTIL_LOGIN
5260325f 143 const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */
144 if (logout(line))
145 logwtmp(line, "", "");
4cca272e 146#else /* HAVE_LIBUTIL_LOGIN */
5260325f 147 record_login(pid, ttyname, "", -1, "", NULL);
4cca272e 148#endif /* HAVE_LIBUTIL_LOGIN */
8efc0c15 149}
This page took 0.100798 seconds and 5 git commands to generate.