blob: 7adc4f22cacc04c9fcc3b422ffb8d6c5a467b5ad [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 Miller6b85a7f2000-01-02 11:45:33 +110021RCSID("$Id: login.c,v 1.17 2000/01/02 00:45:33 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022
Damien Miller368cf641999-12-21 09:51:36 +110023#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
Damien Millerbf1c9b21999-12-09 10:16:54 +110024# include <utmpx.h>
25#endif
26#ifdef HAVE_UTMP_H
27# include <utmp.h>
28#endif
Damien Miller95def091999-11-25 00:26:21 +110029#include "ssh.h"
Damien Millerab18c411999-11-11 10:40:23 +110030
Damien Miller95def091999-11-25 00:26:21 +110031#ifdef HAVE_UTIL_H
32# include <util.h>
33#endif
Damien Millerab18c411999-11-11 10:40:23 +110034#ifdef HAVE_LASTLOG_H
35# include <lastlog.h>
36#endif
Damien Miller063fdf81999-11-25 13:08:31 +110037#ifdef HAVE_LOGIN_H
38# include <login.h>
39#endif
Damien Millerab18c411999-11-11 10:40:23 +110040
Damien Miller5428f641999-11-25 11:54:57 +110041/*
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 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046
Damien Miller5428f641999-11-25 11:54:57 +110047/*
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 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
Damien Miller95def091999-11-25 00:26:21 +110052unsigned long
53get_last_login_time(uid_t uid, const char *logname,
54 char *buf, unsigned int bufsize)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055{
Damien Miller1b0c2281999-12-22 16:09:48 +110056#if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
Damien Miller95def091999-11-25 00:26:21 +110057 struct lastlog ll;
58 char *lastlog;
59 int fd;
Damien Miller6b85a7f2000-01-02 11:45:33 +110060#ifdef LASTLOG_IS_DIR
61 char buf[1024];
62#endif /* LASTLOG_IS_DIR */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
Damien Miller95def091999-11-25 00:26:21 +110064 lastlog = _PATH_LASTLOG;
65 buf[0] = '\0';
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Damien Miller6b85a7f2000-01-02 11:45:33 +110067#ifdef LASTLOG_IS_DIR
Damien Miller95def091999-11-25 00:26:21 +110068 fd = open(lastlog, O_RDONLY);
69 if (fd < 0)
70 return 0;
71 lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
Damien Miller6b85a7f2000-01-02 11:45:33 +110072#else /* LASTLOG_IS_DIR */
73 snprintf(buf, sizeof(buf), "%s/%s", lastlog, logname);
74 fd = open(buf, O_RDONLY);
75 if (fd < 0)
76 return 0;
77#endif /* LASTLOG_IS_DIR */
Damien Miller95def091999-11-25 00:26:21 +110078 if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) {
79 close(fd);
80 return 0;
81 }
82 close(fd);
83 if (bufsize > sizeof(ll.ll_host) + 1)
84 bufsize = sizeof(ll.ll_host) + 1;
85 strncpy(buf, ll.ll_host, bufsize - 1);
86 buf[bufsize - 1] = 0;
87 return ll.ll_time;
Damien Miller76112de1999-12-21 11:18:08 +110088
Damien Miller1b0c2281999-12-22 16:09:48 +110089#else /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
Damien Miller76112de1999-12-21 11:18:08 +110090 /* Look in wtmp for the last login */
91 struct utmp wt;
92 char *wt_file = _PATH_WTMP;
93 int fd1;
94 unsigned long t = 0;
95
96 if ( (fd1 = open(wt_file, O_RDONLY)) < 0 ) {
97 error("Couldn't open %.100s to find last login time.", wt_file);
98 return 0;
99 }
100
101 /* seek to last record of file */
102 lseek(fd1, (off_t)(0-sizeof(struct utmp)), SEEK_END);
103
104 /* loop through wtmp for our last user login record */
105 do {
106 if (read(fd1, &wt, sizeof(wt)) != sizeof(wt)) {
107 close(fd1);
108 return 0;
109 }
110
111 if ( wt.ut_type == USER_PROCESS) {
112 if ( !strncmp(logname, wt.ut_user, 8) ) {
113 t = (unsigned long) wt.ut_time;
Damien Miller1b0c2281999-12-22 16:09:48 +1100114#ifdef HAVE_HOST_IN_UTMP
Damien Miller76112de1999-12-21 11:18:08 +1100115 if (bufsize > sizeof(wt.ut_host) + 1)
116 bufsize = sizeof(wt.ut_host) + 1;
117 strncpy(buf, wt.ut_host, bufsize - 1);
118 buf[bufsize - 1] = 0;
Damien Miller1b0c2281999-12-22 16:09:48 +1100119#else /* HAVE_HOST_IN_UTMP */
120 buf[0] = 0;
121#endif /* HAVE_HOST_IN_UTMP */
Damien Miller76112de1999-12-21 11:18:08 +1100122 }
123 }
124
125 if (lseek(fd1, (off_t)(0-2*sizeof(struct utmp)), SEEK_CUR) == -1)
126 break;
127 } while (t == 0);
128
129 return t;
Damien Miller1b0c2281999-12-22 16:09:48 +1100130#endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131}
132
Damien Miller5428f641999-11-25 11:54:57 +1100133/*
134 * Records that the user has logged in. I these parts of operating systems
135 * were more standardized.
136 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137
Damien Miller95def091999-11-25 00:26:21 +1100138void
139record_login(int pid, const char *ttyname, const char *user, uid_t uid,
140 const char *host, struct sockaddr_in * addr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141{
Damien Miller1b0c2281999-12-22 16:09:48 +1100142#if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
Damien Miller95def091999-11-25 00:26:21 +1100143 struct lastlog ll;
144 char *lastlog;
Damien Miller6b85a7f2000-01-02 11:45:33 +1100145#ifdef LASTLOG_IS_DIR
146 char buf[1024];
147#endif /* LASTLOG_IS_DIR */
Damien Miller1b0c2281999-12-22 16:09:48 +1100148#endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
Damien Miller2e1b0821999-12-25 10:11:29 +1100149 struct utmp u;
150#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
151 struct utmpx utx;
152#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153
Damien Miller95def091999-11-25 00:26:21 +1100154 /* Construct an utmp/wtmp entry. */
155 memset(&u, 0, sizeof(u));
156 strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));
Damien Miller9550a761999-12-29 02:32:22 +1100157#if defined(HAVE_ID_IN_UTMP)
Damien Millere1276241999-12-27 11:33:56 +1100158 strncpy(u.ut_id, ttyname + 8, sizeof(u.ut_id));
Damien Miller9550a761999-12-29 02:32:22 +1100159#endif /* defined(HAVE_ID_IN_UTMP) */
Damien Miller95def091999-11-25 00:26:21 +1100160 strncpy(u.ut_name, user, sizeof(u.ut_name));
Damien Miller4ff2b9b1999-12-28 10:41:12 +1100161#if defined(HAVE_TV_IN_UTMP)
162 (void)gettimeofday(&u.ut_tv, NULL);
163#else /* defined(HAVE_TV_IN_UTMP) */
164 u.ut_time = time(NULL);
165#endif /* defined(HAVE_TV_IN_UTMP) */
166#if defined(HAVE_PID_IN_UTMP)
167 u.ut_pid = (pid_t)pid;
168#endif /* HAVE_PID_IN_UTMP */
169#if defined(HAVE_TYPE_IN_UTMP)
Damien Miller2e1b0821999-12-25 10:11:29 +1100170 u.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS;
Damien Miller4ff2b9b1999-12-28 10:41:12 +1100171#endif /* HAVE_TYPE_IN_UTMP */
Damien Miller2e1b0821999-12-25 10:11:29 +1100172#if defined(HAVE_HOST_IN_UTMP)
Damien Miller95def091999-11-25 00:26:21 +1100173 strncpy(u.ut_host, host, sizeof(u.ut_host));
Damien Millerab18c411999-11-11 10:40:23 +1100174#endif
Damien Miller3131d8b1999-12-31 09:42:24 +1100175#if defined(HAVE_ADDR_IN_UTMP)
176 u.ut_addr = addr->sin_addr.s_addr;
177#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178
Damien Miller2e1b0821999-12-25 10:11:29 +1100179#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
180 memset(&utx, 0, sizeof(utx));
181 strncpy(utx.ut_user, user, sizeof(utx.ut_name));
182 strncpy(utx.ut_line, ttyname + 5, sizeof(utx.ut_line));
Damien Millere1276241999-12-27 11:33:56 +1100183 strncpy(utx.ut_id, ttyname + 8, sizeof(utx.ut_id));
Damien Miller2e1b0821999-12-25 10:11:29 +1100184 utx.ut_pid = (pid_t)pid;
Damien Miller4ff2b9b1999-12-28 10:41:12 +1100185 (void)gettimeofday(&utx.ut_tv, NULL);
Damien Miller32b3cf21999-12-26 10:21:48 +1100186 utx.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS;
187# ifdef HAVE_HOST_IN_UTMPX
188# ifdef HAVE_SYSLEN_IN_UTMPX
Damien Miller2e1b0821999-12-25 10:11:29 +1100189 utx.ut_syslen = strlen(host);
Damien Miller32b3cf21999-12-26 10:21:48 +1100190 strncpy(utx.ut_host, host, utx.ut_syslen);
191# else
Damien Miller2e1b0821999-12-25 10:11:29 +1100192 strncpy(utx.ut_host, host, sizeof(utx.ut_host));
Damien Miller32b3cf21999-12-26 10:21:48 +1100193# endif /* HAVE_SYSLEN_IN_UTMPX */
194# endif
Damien Miller3131d8b1999-12-31 09:42:24 +1100195# if defined(HAVE_ADDR_IN_UTMPX)
196 utx.ut_addr = addr->sin_addr.s_addr;
197# endif
Damien Miller2e1b0821999-12-25 10:11:29 +1100198#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199
Damien Miller32b3cf21999-12-26 10:21:48 +1100200/*#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) && !defined(HAVE_LOGIN)*/
201#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
Damien Miller2e1b0821999-12-25 10:11:29 +1100202 login(&u, &utx);
203#else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
Damien Miller95def091999-11-25 00:26:21 +1100204 login(&u);
Damien Miller2e1b0821999-12-25 10:11:29 +1100205#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
Damien Miller76112de1999-12-21 11:18:08 +1100206
Damien Miller1b0c2281999-12-22 16:09:48 +1100207#if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
Damien Miller95def091999-11-25 00:26:21 +1100208 lastlog = _PATH_LASTLOG;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209
Damien Miller95def091999-11-25 00:26:21 +1100210 /* Update lastlog unless actually recording a logout. */
211 if (strcmp(user, "") != 0) {
Damien Miller76112de1999-12-21 11:18:08 +1100212 int fd;
Damien Miller5428f641999-11-25 11:54:57 +1100213 /*
214 * It is safer to bzero the lastlog structure first because
215 * some systems might have some extra fields in it (e.g. SGI)
216 */
Damien Miller95def091999-11-25 00:26:21 +1100217 memset(&ll, 0, sizeof(ll));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218
Damien Miller95def091999-11-25 00:26:21 +1100219 /* Update lastlog. */
220 ll.ll_time = time(NULL);
221 strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));
222 strncpy(ll.ll_host, host, sizeof(ll.ll_host));
Damien Miller6b85a7f2000-01-02 11:45:33 +1100223#ifdef LASTLOG_IS_DIR
224 snprintf(buf, sizeof(buf), "%s/%s", lastlog, logname);
225 fd = open(buf, O_RDWR);
226 if (fd >= 0) {
227#else /* LASTLOG_IS_DIR */
Damien Miller95def091999-11-25 00:26:21 +1100228 fd = open(lastlog, O_RDWR);
229 if (fd >= 0) {
230 lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
Damien Miller6b85a7f2000-01-02 11:45:33 +1100231#endif /* LASTLOG_IS_DIR */
Damien Miller95def091999-11-25 00:26:21 +1100232 if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
233 log("Could not write %.100s: %.100s", lastlog, strerror(errno));
234 close(fd);
235 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236 }
Damien Miller1b0c2281999-12-22 16:09:48 +1100237#endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238}
Damien Miller95def091999-11-25 00:26:21 +1100239
240/* Records that the user has logged out. */
241
242void
243record_logout(int pid, const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244{
Damien Millerab18c411999-11-11 10:40:23 +1100245#ifdef HAVE_LIBUTIL_LOGIN
Damien Miller95def091999-11-25 00:26:21 +1100246 const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */
247 if (logout(line))
248 logwtmp(line, "", "");
Damien Millerab18c411999-11-11 10:40:23 +1100249#else /* HAVE_LIBUTIL_LOGIN */
Damien Miller95def091999-11-25 00:26:21 +1100250 record_login(pid, ttyname, "", -1, "", NULL);
Damien Millerab18c411999-11-11 10:40:23 +1100251#endif /* HAVE_LIBUTIL_LOGIN */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252}