blob: 8d48fb99f54f8d4d3b3968ca96c9737594d925b6 [file] [log] [blame]
andre2ff7b5d2000-06-03 14:57:40 +00001/*
2 * Copyright (c) 2000 Andre Lucas. All rights reserved.
andre61e67252000-06-04 17:07:49 +00003 * Portions copyright (c) 1998 Todd C. Miller
4 * Portions copyright (c) 1996 Jason Downs
5 * Portions copyright (c) 1996 Theo de Raadt
andre2ff7b5d2000-06-03 14:57:40 +00006 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
andre2ff7b5d2000-06-03 14:57:40 +000015 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
Kevin Stevesef4eea92001-02-05 12:42:17 +000028/**
andre2ff7b5d2000-06-03 14:57:40 +000029 ** loginrec.c: platform-independent login recording and lastlog retrieval
30 **/
31
andre61e67252000-06-04 17:07:49 +000032/*
Damien Miller8899ed32004-09-12 15:18:55 +100033 * The new login code explained
34 * ============================
35 *
36 * This code attempts to provide a common interface to login recording
37 * (utmp and friends) and last login time retrieval.
38 *
39 * Its primary means of achieving this is to use 'struct logininfo', a
40 * union of all the useful fields in the various different types of
41 * system login record structures one finds on UNIX variants.
42 *
43 * We depend on autoconf to define which recording methods are to be
44 * used, and which fields are contained in the relevant data structures
45 * on the local system. Many C preprocessor symbols affect which code
46 * gets compiled here.
47 *
48 * The code is designed to make it easy to modify a particular
49 * recording method, without affecting other methods nor requiring so
50 * many nested conditional compilation blocks as were commonplace in
51 * the old code.
52 *
53 * For login recording, we try to use the local system's libraries as
54 * these are clearly most likely to work correctly. For utmp systems
55 * this usually means login() and logout() or setutent() etc., probably
56 * in libutil, along with logwtmp() etc. On these systems, we fall back
57 * to writing the files directly if we have to, though this method
58 * requires very thorough testing so we do not corrupt local auditing
59 * information. These files and their access methods are very system
60 * specific indeed.
61 *
62 * For utmpx systems, the corresponding library functions are
63 * setutxent() etc. To the author's knowledge, all utmpx systems have
64 * these library functions and so no direct write is attempted. If such
65 * a system exists and needs support, direct analogues of the [uw]tmp
66 * code should suffice.
67 *
68 * Retrieving the time of last login ('lastlog') is in some ways even
69 * more problemmatic than login recording. Some systems provide a
70 * simple table of all users which we seek based on uid and retrieve a
71 * relatively standard structure. Others record the same information in
72 * a directory with a separate file, and others don't record the
73 * information separately at all. For systems in the latter category,
74 * we look backwards in the wtmp or wtmpx file for the last login entry
75 * for our user. Naturally this is slower and on busy systems could
76 * incur a significant performance penalty.
77 *
78 * Calling the new code
79 * --------------------
80 *
81 * In OpenSSH all login recording and retrieval is performed in
82 * login.c. Here you'll find working examples. Also, in the logintest.c
83 * program there are more examples.
84 *
85 * Internal handler calling method
86 * -------------------------------
87 *
88 * When a call is made to login_login() or login_logout(), both
89 * routines set a struct logininfo flag defining which action (log in,
90 * or log out) is to be taken. They both then call login_write(), which
91 * calls whichever of the many structure-specific handlers autoconf
92 * selects for the local system.
93 *
94 * The handlers themselves handle system data structure specifics. Both
95 * struct utmp and struct utmpx have utility functions (see
96 * construct_utmp*()) to try to make it simpler to add extra systems
97 * that introduce new features to either structure.
98 *
99 * While it may seem terribly wasteful to replicate so much similar
100 * code for each method, experience has shown that maintaining code to
101 * write both struct utmp and utmpx in one function, whilst maintaining
102 * support for all systems whether they have library support or not, is
103 * a difficult and time-consuming task.
104 *
105 * Lastlog support proceeds similarly. Functions login_get_lastlog()
106 * (and its OpenSSH-tuned friend login_get_lastlog_time()) call
107 * getlast_entry(), which tries one of three methods to find the last
108 * login time. It uses local system lastlog support if it can,
109 * otherwise it tries wtmp or wtmpx before giving up and returning 0,
110 * meaning "tilt".
111 *
112 * Maintenance
113 * -----------
114 *
115 * In many cases it's possible to tweak autoconf to select the correct
116 * methods for a particular platform, either by improving the detection
117 * code (best), or by presetting DISABLE_<method> or CONF_<method>_FILE
118 * symbols for the platform.
119 *
120 * Use logintest to check which symbols are defined before modifying
121 * configure.ac and loginrec.c. (You have to build logintest yourself
122 * with 'make logintest' as it's not built by default.)
123 *
124 * Otherwise, patches to the specific method(s) are very helpful!
125 */
andre2ff7b5d2000-06-03 14:57:40 +0000126
127#include "includes.h"
128
andre2ff7b5d2000-06-03 14:57:40 +0000129#include "ssh.h"
130#include "xmalloc.h"
131#include "loginrec.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000132#include "log.h"
133#include "atomicio.h"
andre2ff7b5d2000-06-03 14:57:40 +0000134
Ben Lindstromdcca9812000-11-10 03:28:31 +0000135#ifdef HAVE_UTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000136# include <util.h>
Ben Lindstromdcca9812000-11-10 03:28:31 +0000137#endif
andre2ff7b5d2000-06-03 14:57:40 +0000138
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000139#ifdef HAVE_LIBUTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000140# include <libutil.h>
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000141#endif
142
Damien Miller8899ed32004-09-12 15:18:55 +1000143RCSID("$Id: loginrec.c,v 1.60 2004/09/12 05:18:55 djm Exp $");
144
andre2ff7b5d2000-06-03 14:57:40 +0000145/**
146 ** prototypes for helper functions in this file
147 **/
148
149#if HAVE_UTMP_H
andre2ff7b5d2000-06-03 14:57:40 +0000150void set_utmp_time(struct logininfo *li, struct utmp *ut);
151void construct_utmp(struct logininfo *li, struct utmp *ut);
152#endif
153
154#ifdef HAVE_UTMPX_H
andre2ff7b5d2000-06-03 14:57:40 +0000155void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
156void construct_utmpx(struct logininfo *li, struct utmpx *ut);
157#endif
158
159int utmp_write_entry(struct logininfo *li);
160int utmpx_write_entry(struct logininfo *li);
161int wtmp_write_entry(struct logininfo *li);
162int wtmpx_write_entry(struct logininfo *li);
163int lastlog_write_entry(struct logininfo *li);
164int syslogin_write_entry(struct logininfo *li);
165
166int getlast_entry(struct logininfo *li);
167int lastlog_get_entry(struct logininfo *li);
168int wtmp_get_entry(struct logininfo *li);
169int wtmpx_get_entry(struct logininfo *li);
170
andre6bb92372000-06-19 08:20:03 +0000171/* pick the shortest string */
Damien Miller8899ed32004-09-12 15:18:55 +1000172#define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2))
andre6bb92372000-06-19 08:20:03 +0000173
andre2ff7b5d2000-06-03 14:57:40 +0000174/**
175 ** platform-independent login functions
176 **/
177
Damien Miller8899ed32004-09-12 15:18:55 +1000178/*
179 * login_login(struct logininfo *) - Record a login
Kevin Stevesef4eea92001-02-05 12:42:17 +0000180 *
andre6bb92372000-06-19 08:20:03 +0000181 * Call with a pointer to a struct logininfo initialised with
182 * login_init_entry() or login_alloc_entry()
183 *
184 * Returns:
185 * >0 if successful
186 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
187 */
andre61e67252000-06-04 17:07:49 +0000188int
Damien Miller8899ed32004-09-12 15:18:55 +1000189login_login(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000190{
191 li->type = LTYPE_LOGIN;
Damien Miller8899ed32004-09-12 15:18:55 +1000192 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000193}
194
195
Damien Miller8899ed32004-09-12 15:18:55 +1000196/*
197 * login_logout(struct logininfo *) - Record a logout
andre6bb92372000-06-19 08:20:03 +0000198 *
199 * Call as with login_login()
200 *
201 * Returns:
202 * >0 if successful
203 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
204 */
andre61e67252000-06-04 17:07:49 +0000205int
206login_logout(struct logininfo *li)
207{
208 li->type = LTYPE_LOGOUT;
Damien Miller8899ed32004-09-12 15:18:55 +1000209 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000210}
211
Damien Miller8899ed32004-09-12 15:18:55 +1000212/*
213 * login_get_lastlog_time(int) - Retrieve the last login time
andre6bb92372000-06-19 08:20:03 +0000214 *
215 * Retrieve the last login time for the given uid. Will try to use the
216 * system lastlog facilities if they are available, but will fall back
217 * to looking in wtmp/wtmpx if necessary
218 *
219 * Returns:
220 * 0 on failure, or if user has never logged in
221 * Time in seconds from the epoch if successful
222 *
223 * Useful preprocessor symbols:
224 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
225 * info
226 * USE_LASTLOG: If set, indicates the presence of system lastlog
227 * facilities. If this and DISABLE_LASTLOG are not set,
228 * try to retrieve lastlog information from wtmp/wtmpx.
229 */
andre61e67252000-06-04 17:07:49 +0000230unsigned int
231login_get_lastlog_time(const int uid)
232{
233 struct logininfo li;
234
andre6bb92372000-06-19 08:20:03 +0000235 if (login_get_lastlog(&li, uid))
Damien Miller8899ed32004-09-12 15:18:55 +1000236 return (li.tv_sec);
andre6bb92372000-06-19 08:20:03 +0000237 else
Damien Miller8899ed32004-09-12 15:18:55 +1000238 return (0);
andre61e67252000-06-04 17:07:49 +0000239}
240
Damien Miller8899ed32004-09-12 15:18:55 +1000241/*
242 * login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
andre6bb92372000-06-19 08:20:03 +0000243 *
244 * Retrieve a logininfo structure populated (only partially) with
245 * information from the system lastlog data, or from wtmp/wtmpx if no
246 * system lastlog information exists.
247 *
248 * Note this routine must be given a pre-allocated logininfo.
249 *
250 * Returns:
251 * >0: A pointer to your struct logininfo if successful
252 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
andre6bb92372000-06-19 08:20:03 +0000253 */
andre61e67252000-06-04 17:07:49 +0000254struct logininfo *
255login_get_lastlog(struct logininfo *li, const int uid)
256{
andre6bb92372000-06-19 08:20:03 +0000257 struct passwd *pw;
andre6bb92372000-06-19 08:20:03 +0000258
Damien Miller348c9b72000-08-15 10:01:22 +1000259 memset(li, '\0', sizeof(*li));
andre61e67252000-06-04 17:07:49 +0000260 li->uid = uid;
andre6bb92372000-06-19 08:20:03 +0000261
Kevin Stevesef4eea92001-02-05 12:42:17 +0000262 /*
Damien Miller53c5d462000-06-28 00:50:50 +1000263 * If we don't have a 'real' lastlog, we need the username to
andre6bb92372000-06-19 08:20:03 +0000264 * reliably search wtmp(x) for the last login (see
Kevin Stevesef4eea92001-02-05 12:42:17 +0000265 * wtmp_get_entry().)
Damien Miller53c5d462000-06-28 00:50:50 +1000266 */
andre6bb92372000-06-19 08:20:03 +0000267 pw = getpwuid(uid);
Damien Millerdd47aa22000-06-27 11:18:27 +1000268 if (pw == NULL)
269 fatal("login_get_lastlog: Cannot find account for uid %i", uid);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000270
andre98cabe02000-06-19 09:11:30 +0000271 /* No MIN_SIZEOF here - we absolutely *must not* truncate the
Damien Miller8899ed32004-09-12 15:18:55 +1000272 * username (XXX - so check for trunc!) */
Damien Millerf8af08d2000-06-27 09:40:06 +1000273 strlcpy(li->username, pw->pw_name, sizeof(li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000274
andre61e67252000-06-04 17:07:49 +0000275 if (getlast_entry(li))
Damien Miller8899ed32004-09-12 15:18:55 +1000276 return (li);
andre61e67252000-06-04 17:07:49 +0000277 else
Damien Miller8899ed32004-09-12 15:18:55 +1000278 return (NULL);
andre61e67252000-06-04 17:07:49 +0000279}
280
281
Damien Miller8899ed32004-09-12 15:18:55 +1000282/*
283 * login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
Kevin Stevesef4eea92001-02-05 12:42:17 +0000284 * a logininfo structure
285 *
andre6bb92372000-06-19 08:20:03 +0000286 * This function creates a new struct logininfo, a data structure
287 * meant to carry the information required to portably record login info.
288 *
289 * Returns a pointer to a newly created struct logininfo. If memory
290 * allocation fails, the program halts.
291 */
andre61e67252000-06-04 17:07:49 +0000292struct
293logininfo *login_alloc_entry(int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000294 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000295{
andre2ff7b5d2000-06-03 14:57:40 +0000296 struct logininfo *newli;
297
Damien Miller8899ed32004-09-12 15:18:55 +1000298 newli = xmalloc(sizeof(*newli));
299 login_init_entry(newli, pid, username, hostname, line);
300 return (newli);
andre61e67252000-06-04 17:07:49 +0000301}
andre2ff7b5d2000-06-03 14:57:40 +0000302
303
andre6bb92372000-06-19 08:20:03 +0000304/* login_free_entry(struct logininfo *) - free struct memory */
andre61e67252000-06-04 17:07:49 +0000305void
306login_free_entry(struct logininfo *li)
307{
308 xfree(li);
309}
310
andre2ff7b5d2000-06-03 14:57:40 +0000311
andre6bb92372000-06-19 08:20:03 +0000312/* login_init_entry(struct logininfo *, int, char*, char*, char*)
313 * - initialise a struct logininfo
Kevin Stevesef4eea92001-02-05 12:42:17 +0000314 *
andre6bb92372000-06-19 08:20:03 +0000315 * Populates a new struct logininfo, a data structure meant to carry
316 * the information required to portably record login info.
317 *
318 * Returns: 1
319 */
andre61e67252000-06-04 17:07:49 +0000320int
Kevin Stevesef4eea92001-02-05 12:42:17 +0000321login_init_entry(struct logininfo *li, int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000322 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000323{
Damien Millerf8af08d2000-06-27 09:40:06 +1000324 struct passwd *pw;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000325
Damien Miller348c9b72000-08-15 10:01:22 +1000326 memset(li, 0, sizeof(*li));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000327
andre61e67252000-06-04 17:07:49 +0000328 li->pid = pid;
Damien Millerf8af08d2000-06-27 09:40:06 +1000329
andre2ff7b5d2000-06-03 14:57:40 +0000330 /* set the line information */
andre61e67252000-06-04 17:07:49 +0000331 if (line)
andre2ff7b5d2000-06-03 14:57:40 +0000332 line_fullname(li->line, line, sizeof(li->line));
andre2ff7b5d2000-06-03 14:57:40 +0000333
Damien Millerf8af08d2000-06-27 09:40:06 +1000334 if (username) {
andre2ff7b5d2000-06-03 14:57:40 +0000335 strlcpy(li->username, username, sizeof(li->username));
Damien Millerf8af08d2000-06-27 09:40:06 +1000336 pw = getpwnam(li->username);
Damien Miller8899ed32004-09-12 15:18:55 +1000337 if (pw == NULL) {
338 fatal("login_init_entry: Cannot find user \"%s\"",
339 li->username);
340 }
Damien Millerf8af08d2000-06-27 09:40:06 +1000341 li->uid = pw->pw_uid;
342 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000343
andre61e67252000-06-04 17:07:49 +0000344 if (hostname)
andre2ff7b5d2000-06-03 14:57:40 +0000345 strlcpy(li->hostname, hostname, sizeof(li->hostname));
Damien Millerf8af08d2000-06-27 09:40:06 +1000346
Damien Miller8899ed32004-09-12 15:18:55 +1000347 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000348}
349
Damien Miller8899ed32004-09-12 15:18:55 +1000350/*
351 * login_set_current_time(struct logininfo *) - set the current time
andre6bb92372000-06-19 08:20:03 +0000352 *
353 * Set the current time in a logininfo structure. This function is
354 * meant to eliminate the need to deal with system dependencies for
355 * time handling.
356 */
andre2ff7b5d2000-06-03 14:57:40 +0000357void
andre61e67252000-06-04 17:07:49 +0000358login_set_current_time(struct logininfo *li)
359{
andre2ff7b5d2000-06-03 14:57:40 +0000360 struct timeval tv;
361
362 gettimeofday(&tv, NULL);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000363
Damien Millerf8af08d2000-06-27 09:40:06 +1000364 li->tv_sec = tv.tv_sec;
365 li->tv_usec = tv.tv_usec;
andre2ff7b5d2000-06-03 14:57:40 +0000366}
367
andre61e67252000-06-04 17:07:49 +0000368/* copy a sockaddr_* into our logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000369void
andre61e67252000-06-04 17:07:49 +0000370login_set_addr(struct logininfo *li, const struct sockaddr *sa,
Damien Miller8899ed32004-09-12 15:18:55 +1000371 const unsigned int sa_size)
andre61e67252000-06-04 17:07:49 +0000372{
373 unsigned int bufsize = sa_size;
374
375 /* make sure we don't overrun our union */
376 if (sizeof(li->hostaddr) < sa_size)
377 bufsize = sizeof(li->hostaddr);
378
Damien Miller8899ed32004-09-12 15:18:55 +1000379 memcpy(&li->hostaddr.sa, sa, bufsize);
andre2ff7b5d2000-06-03 14:57:40 +0000380}
381
andre2ff7b5d2000-06-03 14:57:40 +0000382
andre61e67252000-06-04 17:07:49 +0000383/**
384 ** login_write: Call low-level recording functions based on autoconf
385 ** results
386 **/
andre2ff7b5d2000-06-03 14:57:40 +0000387int
Damien Miller8899ed32004-09-12 15:18:55 +1000388login_write(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000389{
Damien Millerbac2d8a2000-09-05 16:13:06 +1100390#ifndef HAVE_CYGWIN
Damien Miller8899ed32004-09-12 15:18:55 +1000391 if (geteuid() != 0) {
392 logit("Attempt to write login records by non-root user (aborting)");
393 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000394 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100395#endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000396
andre2ff7b5d2000-06-03 14:57:40 +0000397 /* set the timestamp */
398 login_set_current_time(li);
399#ifdef USE_LOGIN
400 syslogin_write_entry(li);
401#endif
402#ifdef USE_LASTLOG
Damien Miller8899ed32004-09-12 15:18:55 +1000403 if (li->type == LTYPE_LOGIN)
andre2ff7b5d2000-06-03 14:57:40 +0000404 lastlog_write_entry(li);
andre2ff7b5d2000-06-03 14:57:40 +0000405#endif
406#ifdef USE_UTMP
407 utmp_write_entry(li);
408#endif
409#ifdef USE_WTMP
410 wtmp_write_entry(li);
411#endif
412#ifdef USE_UTMPX
413 utmpx_write_entry(li);
414#endif
415#ifdef USE_WTMPX
416 wtmpx_write_entry(li);
417#endif
Darren Tucker397a2f22004-08-15 00:09:11 +1000418#ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN
419 if (li->type == LTYPE_LOGIN &&
420 !sys_auth_record_login(li->username,li->hostname,li->line))
421 logit("Writing login record failed for %s", li->username);
422#endif
Damien Miller8899ed32004-09-12 15:18:55 +1000423 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000424}
425
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000426#ifdef LOGIN_NEEDS_UTMPX
427int
428login_utmp_only(struct logininfo *li)
429{
Damien Millera8e06ce2003-11-21 23:48:55 +1100430 li->type = LTYPE_LOGIN;
Ben Lindstrom9197c592001-10-26 15:56:55 +0000431 login_set_current_time(li);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000432# ifdef USE_UTMP
433 utmp_write_entry(li);
434# endif
435# ifdef USE_WTMP
436 wtmp_write_entry(li);
437# endif
438# ifdef USE_UTMPX
439 utmpx_write_entry(li);
440# endif
441# ifdef USE_WTMPX
442 wtmpx_write_entry(li);
443# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000444 return (0);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000445}
446#endif
447
andre2ff7b5d2000-06-03 14:57:40 +0000448/**
andre61e67252000-06-04 17:07:49 +0000449 ** getlast_entry: Call low-level functions to retrieve the last login
450 ** time.
andre2ff7b5d2000-06-03 14:57:40 +0000451 **/
452
andre61e67252000-06-04 17:07:49 +0000453/* take the uid in li and return the last login time */
454int
455getlast_entry(struct logininfo *li)
456{
457#ifdef USE_LASTLOG
Damien Miller53c5d462000-06-28 00:50:50 +1000458 return(lastlog_get_entry(li));
Damien Millerdd47aa22000-06-27 11:18:27 +1000459#else /* !USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000460
Damien Miller8899ed32004-09-12 15:18:55 +1000461#if defined(DISABLE_LASTLOG)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000462 /* On some systems we shouldn't even try to obtain last login
andreecaabf12000-06-12 22:21:44 +0000463 * time, e.g. AIX */
Damien Miller8899ed32004-09-12 15:18:55 +1000464 return (0);
465# elif defined(USE_WTMP) && \
466 (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
andre61e67252000-06-04 17:07:49 +0000467 /* retrieve last login time from utmp */
Damien Millerdd47aa22000-06-27 11:18:27 +1000468 return (wtmp_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000469# elif defined(USE_WTMPX) && \
470 (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
andre61e67252000-06-04 17:07:49 +0000471 /* If wtmp isn't available, try wtmpx */
Damien Millerdd47aa22000-06-27 11:18:27 +1000472 return (wtmpx_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000473# else
andre61e67252000-06-04 17:07:49 +0000474 /* Give up: No means of retrieving last login time */
Damien Miller8899ed32004-09-12 15:18:55 +1000475 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000476# endif /* DISABLE_LASTLOG */
Damien Millerdd47aa22000-06-27 11:18:27 +1000477#endif /* USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000478}
479
480
481
andre2ff7b5d2000-06-03 14:57:40 +0000482/*
andre61e67252000-06-04 17:07:49 +0000483 * 'line' string utility functions
484 *
485 * These functions process the 'line' string into one of three forms:
486 *
andre2ff7b5d2000-06-03 14:57:40 +0000487 * 1. The full filename (including '/dev')
488 * 2. The stripped name (excluding '/dev')
andre61e67252000-06-04 17:07:49 +0000489 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
490 * /dev/pts/1 -> ts/1 )
andre2ff7b5d2000-06-03 14:57:40 +0000491 *
492 * Form 3 is used on some systems to identify a .tmp.? entry when
493 * attempting to remove it. Typically both addition and removal is
andre61e67252000-06-04 17:07:49 +0000494 * performed by one application - say, sshd - so as long as the choice
495 * uniquely identifies a terminal it's ok.
andre2ff7b5d2000-06-03 14:57:40 +0000496 */
497
498
Damien Miller8899ed32004-09-12 15:18:55 +1000499/*
500 * line_fullname(): add the leading '/dev/' if it doesn't exist make
501 * sure dst has enough space, if not just copy src (ugh)
502 */
andre2ff7b5d2000-06-03 14:57:40 +0000503char *
andre61e67252000-06-04 17:07:49 +0000504line_fullname(char *dst, const char *src, int dstsize)
505{
andre2ff7b5d2000-06-03 14:57:40 +0000506 memset(dst, '\0', dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000507 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
andre2ff7b5d2000-06-03 14:57:40 +0000508 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000509 else {
Damien Miller1a132252000-06-13 21:23:17 +1000510 strlcpy(dst, "/dev/", dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000511 strlcat(dst, src, dstsize);
512 }
Damien Miller8899ed32004-09-12 15:18:55 +1000513 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000514}
515
andre61e67252000-06-04 17:07:49 +0000516/* line_stripname(): strip the leading '/dev' if it exists, return dst */
andre2ff7b5d2000-06-03 14:57:40 +0000517char *
andre61e67252000-06-04 17:07:49 +0000518line_stripname(char *dst, const char *src, int dstsize)
519{
andre2ff7b5d2000-06-03 14:57:40 +0000520 memset(dst, '\0', dstsize);
521 if (strncmp(src, "/dev/", 5) == 0)
Damien Millerf5a81472000-09-30 21:34:44 +1100522 strlcpy(dst, src + 5, dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000523 else
524 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000525 return (dst);
andre61e67252000-06-04 17:07:49 +0000526}
527
Damien Miller8899ed32004-09-12 15:18:55 +1000528/*
529 * line_abbrevname(): Return the abbreviated (usually four-character)
andre61e67252000-06-04 17:07:49 +0000530 * form of the line (Just use the last <dstsize> characters of the
531 * full name.)
532 *
533 * NOTE: use strncpy because we do NOT necessarily want zero
Damien Miller8899ed32004-09-12 15:18:55 +1000534 * termination
535 */
andre2ff7b5d2000-06-03 14:57:40 +0000536char *
Kevin Stevesef4eea92001-02-05 12:42:17 +0000537line_abbrevname(char *dst, const char *src, int dstsize)
Damien Millerdd47aa22000-06-27 11:18:27 +1000538{
539 size_t len;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000540
andre2ff7b5d2000-06-03 14:57:40 +0000541 memset(dst, '\0', dstsize);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000542
Damien Miller8e81ed32000-07-01 13:17:42 +1000543 /* Always skip prefix if present */
544 if (strncmp(src, "/dev/", 5) == 0)
545 src += 5;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000546
Damien Millerf1b9d112002-04-23 23:09:19 +1000547#ifdef WITH_ABBREV_NO_TTY
548 if (strncmp(src, "tty", 3) == 0)
549 src += 3;
550#endif
551
Damien Millerdd47aa22000-06-27 11:18:27 +1000552 len = strlen(src);
553
Damien Miller8e81ed32000-07-01 13:17:42 +1000554 if (len > 0) {
555 if (((int)len - dstsize) > 0)
556 src += ((int)len - dstsize);
557
558 /* note: _don't_ change this to strlcpy */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000559 strncpy(dst, src, (size_t)dstsize);
Damien Millerdd47aa22000-06-27 11:18:27 +1000560 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000561
Damien Miller8899ed32004-09-12 15:18:55 +1000562 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000563}
564
andre2ff7b5d2000-06-03 14:57:40 +0000565/**
566 ** utmp utility functions
andre61e67252000-06-04 17:07:49 +0000567 **
568 ** These functions manipulate struct utmp, taking system differences
569 ** into account.
andre2ff7b5d2000-06-03 14:57:40 +0000570 **/
571
572#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
573
andre2ff7b5d2000-06-03 14:57:40 +0000574/* build the utmp structure */
575void
andre61e67252000-06-04 17:07:49 +0000576set_utmp_time(struct logininfo *li, struct utmp *ut)
577{
Damien Miller8899ed32004-09-12 15:18:55 +1000578# if defined(HAVE_TV_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000579 ut->ut_tv.tv_sec = li->tv_sec;
580 ut->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000581# elif defined(HAVE_TIME_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000582 ut->ut_time = li->tv_sec;
Damien Millerdd47aa22000-06-27 11:18:27 +1000583# endif
andre2ff7b5d2000-06-03 14:57:40 +0000584}
585
586void
587construct_utmp(struct logininfo *li,
andre61e67252000-06-04 17:07:49 +0000588 struct utmp *ut)
589{
Damien Miller02e16ad2003-01-03 14:42:27 +1100590# ifdef HAVE_ADDR_V6_IN_UTMP
591 struct sockaddr_in6 *sa6;
Damien Miller8899ed32004-09-12 15:18:55 +1000592# endif
593
Damien Miller348c9b72000-08-15 10:01:22 +1000594 memset(ut, '\0', sizeof(*ut));
andre6bb92372000-06-19 08:20:03 +0000595
596 /* First fill out fields used for both logins and logouts */
597
Damien Millerdd47aa22000-06-27 11:18:27 +1000598# ifdef HAVE_ID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000599 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +1000600# endif
andre2ff7b5d2000-06-03 14:57:40 +0000601
Damien Millerdd47aa22000-06-27 11:18:27 +1000602# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +0000603 /* This is done here to keep utmp constants out of struct logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000604 switch (li->type) {
605 case LTYPE_LOGIN:
606 ut->ut_type = USER_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700607#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000608 cray_set_tmpdir(ut);
609#endif
andre2ff7b5d2000-06-03 14:57:40 +0000610 break;
611 case LTYPE_LOGOUT:
612 ut->ut_type = DEAD_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700613#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000614 cray_retain_utmp(ut, li->pid);
615#endif
andre2ff7b5d2000-06-03 14:57:40 +0000616 break;
617 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000618# endif
andre6bb92372000-06-19 08:20:03 +0000619 set_utmp_time(li, ut);
andre2ff7b5d2000-06-03 14:57:40 +0000620
andre6bb92372000-06-19 08:20:03 +0000621 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +1000622
623# ifdef HAVE_PID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000624 ut->ut_pid = li->pid;
Damien Millerdd47aa22000-06-27 11:18:27 +1000625# endif
andre6bb92372000-06-19 08:20:03 +0000626
627 /* If we're logging out, leave all other fields blank */
628 if (li->type == LTYPE_LOGOUT)
Damien Miller8899ed32004-09-12 15:18:55 +1000629 return;
andre6bb92372000-06-19 08:20:03 +0000630
Damien Millerdd47aa22000-06-27 11:18:27 +1000631 /*
632 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000633 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000634 */
andre6bb92372000-06-19 08:20:03 +0000635
636 /* Use strncpy because we don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000637 strncpy(ut->ut_name, li->username,
638 MIN_SIZEOF(ut->ut_name, li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000639# ifdef HAVE_HOST_IN_UTMP
Damien Miller8899ed32004-09-12 15:18:55 +1000640 strncpy(ut->ut_host, li->hostname,
641 MIN_SIZEOF(ut->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000642# endif
643# ifdef HAVE_ADDR_IN_UTMP
andre61e67252000-06-04 17:07:49 +0000644 /* this is just a 32-bit IP address */
645 if (li->hostaddr.sa.sa_family == AF_INET)
646 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000647# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100648# ifdef HAVE_ADDR_V6_IN_UTMP
649 /* this is just a 128-bit IPv6 address */
650 if (li->hostaddr.sa.sa_family == AF_INET6) {
651 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
652 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
653 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
654 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
655 ut->ut_addr_v6[1] = 0;
656 ut->ut_addr_v6[2] = 0;
657 ut->ut_addr_v6[3] = 0;
658 }
659 }
660# endif
andre61e67252000-06-04 17:07:49 +0000661}
Damien Millerdd47aa22000-06-27 11:18:27 +1000662#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
andre61e67252000-06-04 17:07:49 +0000663
andre2ff7b5d2000-06-03 14:57:40 +0000664/**
665 ** utmpx utility functions
andre61e67252000-06-04 17:07:49 +0000666 **
667 ** These functions manipulate struct utmpx, accounting for system
668 ** variations.
andre2ff7b5d2000-06-03 14:57:40 +0000669 **/
670
671#if defined(USE_UTMPX) || defined (USE_WTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000672/* build the utmpx structure */
673void
andre61e67252000-06-04 17:07:49 +0000674set_utmpx_time(struct logininfo *li, struct utmpx *utx)
675{
Damien Miller8899ed32004-09-12 15:18:55 +1000676# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000677 utx->ut_tv.tv_sec = li->tv_sec;
678 utx->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000679# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000680 utx->ut_time = li->tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +1000681# endif
andre2ff7b5d2000-06-03 14:57:40 +0000682}
683
andre61e67252000-06-04 17:07:49 +0000684void
685construct_utmpx(struct logininfo *li, struct utmpx *utx)
686{
Damien Miller02e16ad2003-01-03 14:42:27 +1100687# ifdef HAVE_ADDR_V6_IN_UTMP
688 struct sockaddr_in6 *sa6;
689# endif
Damien Miller348c9b72000-08-15 10:01:22 +1000690 memset(utx, '\0', sizeof(*utx));
Damien Miller8899ed32004-09-12 15:18:55 +1000691
Damien Miller8e81ed32000-07-01 13:17:42 +1000692# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000693 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
Damien Miller8e81ed32000-07-01 13:17:42 +1000694# endif
andre2ff7b5d2000-06-03 14:57:40 +0000695
696 /* this is done here to keep utmp constants out of loginrec.h */
697 switch (li->type) {
698 case LTYPE_LOGIN:
699 utx->ut_type = USER_PROCESS;
700 break;
701 case LTYPE_LOGOUT:
702 utx->ut_type = DEAD_PROCESS;
703 break;
704 }
andre2ff7b5d2000-06-03 14:57:40 +0000705 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
andre2ff7b5d2000-06-03 14:57:40 +0000706 set_utmpx_time(li, utx);
andre6bb92372000-06-19 08:20:03 +0000707 utx->ut_pid = li->pid;
Damien Miller8899ed32004-09-12 15:18:55 +1000708
Tim Ricee06ae4a2002-02-24 17:56:46 -0800709 /* strncpy(): Don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000710 strncpy(utx->ut_name, li->username,
711 MIN_SIZEOF(utx->ut_name, li->username));
andre6bb92372000-06-19 08:20:03 +0000712
713 if (li->type == LTYPE_LOGOUT)
714 return;
715
Damien Millerdd47aa22000-06-27 11:18:27 +1000716 /*
717 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000718 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000719 */
andre6bb92372000-06-19 08:20:03 +0000720
Damien Millerdd47aa22000-06-27 11:18:27 +1000721# ifdef HAVE_HOST_IN_UTMPX
Damien Miller8899ed32004-09-12 15:18:55 +1000722 strncpy(utx->ut_host, li->hostname,
723 MIN_SIZEOF(utx->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000724# endif
725# ifdef HAVE_ADDR_IN_UTMPX
Damien Millerd6f204d2000-09-23 13:57:27 +1100726 /* this is just a 32-bit IP address */
727 if (li->hostaddr.sa.sa_family == AF_INET)
728 utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Damien Millerdd47aa22000-06-27 11:18:27 +1000729# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100730# ifdef HAVE_ADDR_V6_IN_UTMP
731 /* this is just a 128-bit IPv6 address */
732 if (li->hostaddr.sa.sa_family == AF_INET6) {
733 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
734 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
735 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
736 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
737 ut->ut_addr_v6[1] = 0;
738 ut->ut_addr_v6[2] = 0;
739 ut->ut_addr_v6[3] = 0;
740 }
741 }
742# endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000743# ifdef HAVE_SYSLEN_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +0000744 /* ut_syslen is the length of the utx_host string */
745 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +1000746# endif
andre61e67252000-06-04 17:07:49 +0000747}
Damien Millerdd47aa22000-06-27 11:18:27 +1000748#endif /* USE_UTMPX || USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +0000749
750/**
andre61e67252000-06-04 17:07:49 +0000751 ** Low-level utmp functions
andre2ff7b5d2000-06-03 14:57:40 +0000752 **/
753
754/* FIXME: (ATL) utmp_write_direct needs testing */
andre2ff7b5d2000-06-03 14:57:40 +0000755#ifdef USE_UTMP
756
andre2ff7b5d2000-06-03 14:57:40 +0000757/* if we can, use pututline() etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000758# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
759 defined(HAVE_PUTUTLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000760# define UTMP_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000761# endif
andre2ff7b5d2000-06-03 14:57:40 +0000762
763
764/* write a utmp entry with the system's help (pututline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000765# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000766static int
andre61e67252000-06-04 17:07:49 +0000767utmp_write_library(struct logininfo *li, struct utmp *ut)
768{
andre2ff7b5d2000-06-03 14:57:40 +0000769 setutent();
770 pututline(ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000771# ifdef HAVE_ENDUTENT
andre2ff7b5d2000-06-03 14:57:40 +0000772 endutent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000773# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000774 return (1);
andre61e67252000-06-04 17:07:49 +0000775}
Damien Millerdd47aa22000-06-27 11:18:27 +1000776# else /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000777
Damien Miller8899ed32004-09-12 15:18:55 +1000778/*
779 * Write a utmp entry direct to the file
780 * This is a slightly modification of code in OpenBSD's login.c
781 */
andre2ff7b5d2000-06-03 14:57:40 +0000782static int
andre61e67252000-06-04 17:07:49 +0000783utmp_write_direct(struct logininfo *li, struct utmp *ut)
784{
andre2ff7b5d2000-06-03 14:57:40 +0000785 struct utmp old_ut;
786 register int fd;
787 int tty;
788
andre6bb92372000-06-19 08:20:03 +0000789 /* FIXME: (ATL) ttyslot() needs local implementation */
Damien Miller348c9b72000-08-15 10:01:22 +1000790
Damien Millere5192fa2000-08-29 14:30:37 +1100791#if defined(HAVE_GETTTYENT)
Damien Miller8899ed32004-09-12 15:18:55 +1000792 struct ttyent *ty;
Damien Miller348c9b72000-08-15 10:01:22 +1000793
794 tty=0;
Damien Miller348c9b72000-08-15 10:01:22 +1000795 setttyent();
Damien Miller8899ed32004-09-12 15:18:55 +1000796 while (NULL != (ty = getttyent())) {
Damien Miller348c9b72000-08-15 10:01:22 +1000797 tty++;
798 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
799 break;
800 }
801 endttyent();
802
Damien Miller8899ed32004-09-12 15:18:55 +1000803 if (NULL == ty) {
Damien Miller81409592004-08-15 19:12:52 +1000804 logit("%s: tty not found", __func__);
805 return (0);
Damien Miller348c9b72000-08-15 10:01:22 +1000806 }
807#else /* FIXME */
808
andre2ff7b5d2000-06-03 14:57:40 +0000809 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
810
Damien Millere5192fa2000-08-29 14:30:37 +1100811#endif /* HAVE_GETTTYENT */
Damien Miller348c9b72000-08-15 10:01:22 +1000812
andre2ff7b5d2000-06-03 14:57:40 +0000813 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
Damien Miller81409592004-08-15 19:12:52 +1000814 off_t pos, ret;
815
816 pos = (off_t)tty * sizeof(struct utmp);
817 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000818 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000819 return (0);
820 }
821 if (ret != pos) {
Damien Millerb0419f22004-08-23 21:53:28 +1000822 logit("%s: Couldn't seek to tty %d slot in %s",
823 __func__, tty, UTMP_FILE);
Damien Miller81409592004-08-15 19:12:52 +1000824 return (0);
825 }
andre2ff7b5d2000-06-03 14:57:40 +0000826 /*
827 * Prevent luser from zero'ing out ut_host.
828 * If the new ut_line is empty but the old one is not
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000829 * and ut_line and ut_name match, preserve the old ut_line.
andre2ff7b5d2000-06-03 14:57:40 +0000830 */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000831 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
Damien Miller8899ed32004-09-12 15:18:55 +1000832 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
833 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
834 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0))
835 memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000836
Damien Miller81409592004-08-15 19:12:52 +1000837 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000838 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000839 return (0);
840 }
841 if (ret != pos) {
Damien Millerb0419f22004-08-23 21:53:28 +1000842 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Miller81409592004-08-15 19:12:52 +1000843 __func__, tty, UTMP_FILE);
844 return (0);
845 }
Damien Miller8899ed32004-09-12 15:18:55 +1000846 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
Damien Miller81409592004-08-15 19:12:52 +1000847 logit("%s: error writing %s: %s", __func__,
andre6bb92372000-06-19 08:20:03 +0000848 UTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +1000849 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000850
Damien Miller8899ed32004-09-12 15:18:55 +1000851 close(fd);
852 return (1);
Damien Miller53c5d462000-06-28 00:50:50 +1000853 } else {
Damien Miller8899ed32004-09-12 15:18:55 +1000854 return (0);
Damien Miller53c5d462000-06-28 00:50:50 +1000855 }
andre61e67252000-06-04 17:07:49 +0000856}
Damien Millerdd47aa22000-06-27 11:18:27 +1000857# endif /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000858
859static int
andre61e67252000-06-04 17:07:49 +0000860utmp_perform_login(struct logininfo *li)
861{
andre2ff7b5d2000-06-03 14:57:40 +0000862 struct utmp ut;
863
864 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000865# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000866 if (!utmp_write_library(li, &ut)) {
Damien Miller996acd22003-04-09 20:59:48 +1000867 logit("utmp_perform_login: utmp_write_library() failed");
Damien Miller8899ed32004-09-12 15:18:55 +1000868 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000869 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000870# else
andre2ff7b5d2000-06-03 14:57:40 +0000871 if (!utmp_write_direct(li, &ut)) {
Damien Miller996acd22003-04-09 20:59:48 +1000872 logit("utmp_perform_login: utmp_write_direct() failed");
Damien Miller8899ed32004-09-12 15:18:55 +1000873 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000874 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000875# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000876 return (1);
andre61e67252000-06-04 17:07:49 +0000877}
andre2ff7b5d2000-06-03 14:57:40 +0000878
879
880static int
andre61e67252000-06-04 17:07:49 +0000881utmp_perform_logout(struct logininfo *li)
882{
andre2ff7b5d2000-06-03 14:57:40 +0000883 struct utmp ut;
884
andre6bb92372000-06-19 08:20:03 +0000885 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000886# ifdef UTMP_USE_LIBRARY
andre6bb92372000-06-19 08:20:03 +0000887 if (!utmp_write_library(li, &ut)) {
Damien Miller996acd22003-04-09 20:59:48 +1000888 logit("utmp_perform_logout: utmp_write_library() failed");
Damien Miller8899ed32004-09-12 15:18:55 +1000889 return (0);
andre6bb92372000-06-19 08:20:03 +0000890 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000891# else
andre6bb92372000-06-19 08:20:03 +0000892 if (!utmp_write_direct(li, &ut)) {
Damien Miller996acd22003-04-09 20:59:48 +1000893 logit("utmp_perform_logout: utmp_write_direct() failed");
Damien Miller8899ed32004-09-12 15:18:55 +1000894 return (0);
andre6bb92372000-06-19 08:20:03 +0000895 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000896# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000897 return (1);
andre61e67252000-06-04 17:07:49 +0000898}
andre2ff7b5d2000-06-03 14:57:40 +0000899
900
901int
andre61e67252000-06-04 17:07:49 +0000902utmp_write_entry(struct logininfo *li)
903{
andre2ff7b5d2000-06-03 14:57:40 +0000904 switch(li->type) {
905 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +1000906 return (utmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +0000907
908 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +1000909 return (utmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +0000910
911 default:
Damien Miller996acd22003-04-09 20:59:48 +1000912 logit("utmp_write_entry: invalid type field");
Damien Miller8899ed32004-09-12 15:18:55 +1000913 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000914 }
andre61e67252000-06-04 17:07:49 +0000915}
Damien Millerdd47aa22000-06-27 11:18:27 +1000916#endif /* USE_UTMP */
andre2ff7b5d2000-06-03 14:57:40 +0000917
918
919/**
andre61e67252000-06-04 17:07:49 +0000920 ** Low-level utmpx functions
andre2ff7b5d2000-06-03 14:57:40 +0000921 **/
922
923/* not much point if we don't want utmpx entries */
924#ifdef USE_UTMPX
925
andre2ff7b5d2000-06-03 14:57:40 +0000926/* if we have the wherewithall, use pututxline etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000927# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
928 defined(HAVE_PUTUTXLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000929# define UTMPX_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000930# endif
andre2ff7b5d2000-06-03 14:57:40 +0000931
932
933/* write a utmpx entry with the system's help (pututxline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000934# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000935static int
andre61e67252000-06-04 17:07:49 +0000936utmpx_write_library(struct logininfo *li, struct utmpx *utx)
937{
andre2ff7b5d2000-06-03 14:57:40 +0000938 setutxent();
939 pututxline(utx);
940
Damien Millerdd47aa22000-06-27 11:18:27 +1000941# ifdef HAVE_ENDUTXENT
andre2ff7b5d2000-06-03 14:57:40 +0000942 endutxent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000943# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000944 return (1);
andre61e67252000-06-04 17:07:49 +0000945}
andre2ff7b5d2000-06-03 14:57:40 +0000946
Damien Millerdd47aa22000-06-27 11:18:27 +1000947# else /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000948
949/* write a utmp entry direct to the file */
950static int
andre61e67252000-06-04 17:07:49 +0000951utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000952{
Damien Miller996acd22003-04-09 20:59:48 +1000953 logit("utmpx_write_direct: not implemented!");
Damien Miller8899ed32004-09-12 15:18:55 +1000954 return (0);
andre61e67252000-06-04 17:07:49 +0000955}
Damien Millerdd47aa22000-06-27 11:18:27 +1000956# endif /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000957
958static int
andre61e67252000-06-04 17:07:49 +0000959utmpx_perform_login(struct logininfo *li)
960{
andre2ff7b5d2000-06-03 14:57:40 +0000961 struct utmpx utx;
962
963 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +1000964# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000965 if (!utmpx_write_library(li, &utx)) {
Damien Miller996acd22003-04-09 20:59:48 +1000966 logit("utmpx_perform_login: utmp_write_library() failed");
Damien Miller8899ed32004-09-12 15:18:55 +1000967 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000968 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000969# else
andre2ff7b5d2000-06-03 14:57:40 +0000970 if (!utmpx_write_direct(li, &ut)) {
Damien Miller996acd22003-04-09 20:59:48 +1000971 logit("utmpx_perform_login: utmp_write_direct() failed");
Damien Miller8899ed32004-09-12 15:18:55 +1000972 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000973 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000974# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000975 return (1);
andre61e67252000-06-04 17:07:49 +0000976}
andre2ff7b5d2000-06-03 14:57:40 +0000977
978
979static int
andre61e67252000-06-04 17:07:49 +0000980utmpx_perform_logout(struct logininfo *li)
981{
andre2ff7b5d2000-06-03 14:57:40 +0000982 struct utmpx utx;
983
Tim Ricee06ae4a2002-02-24 17:56:46 -0800984 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +1000985# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000986 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +1000987# endif
988# ifdef HAVE_TYPE_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000989 utx.ut_type = DEAD_PROCESS;
Damien Millerdd47aa22000-06-27 11:18:27 +1000990# endif
andre2ff7b5d2000-06-03 14:57:40 +0000991
Damien Millerdd47aa22000-06-27 11:18:27 +1000992# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000993 utmpx_write_library(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +1000994# else
andre2ff7b5d2000-06-03 14:57:40 +0000995 utmpx_write_direct(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +1000996# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000997 return (1);
andre61e67252000-06-04 17:07:49 +0000998}
andre2ff7b5d2000-06-03 14:57:40 +0000999
andre2ff7b5d2000-06-03 14:57:40 +00001000int
andre61e67252000-06-04 17:07:49 +00001001utmpx_write_entry(struct logininfo *li)
1002{
andre2ff7b5d2000-06-03 14:57:40 +00001003 switch(li->type) {
1004 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001005 return (utmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001006 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001007 return (utmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001008 default:
Damien Miller996acd22003-04-09 20:59:48 +10001009 logit("utmpx_write_entry: invalid type field");
Damien Miller8899ed32004-09-12 15:18:55 +10001010 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001011 }
andre61e67252000-06-04 17:07:49 +00001012}
Damien Millerdd47aa22000-06-27 11:18:27 +10001013#endif /* USE_UTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001014
1015
1016/**
andre61e67252000-06-04 17:07:49 +00001017 ** Low-level wtmp functions
andre2ff7b5d2000-06-03 14:57:40 +00001018 **/
1019
Kevin Stevesef4eea92001-02-05 12:42:17 +00001020#ifdef USE_WTMP
andre2ff7b5d2000-06-03 14:57:40 +00001021
Damien Miller8899ed32004-09-12 15:18:55 +10001022/*
1023 * Write a wtmp entry direct to the end of the file
1024 * This is a slight modification of code in OpenBSD's logwtmp.c
1025 */
andre2ff7b5d2000-06-03 14:57:40 +00001026static int
andre61e67252000-06-04 17:07:49 +00001027wtmp_write(struct logininfo *li, struct utmp *ut)
1028{
andre2ff7b5d2000-06-03 14:57:40 +00001029 struct stat buf;
1030 int fd, ret = 1;
1031
1032 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001033 logit("wtmp_write: problem writing %s: %s",
andre2ff7b5d2000-06-03 14:57:40 +00001034 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001035 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001036 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001037 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001038 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
andre2ff7b5d2000-06-03 14:57:40 +00001039 ftruncate(fd, buf.st_size);
Damien Miller996acd22003-04-09 20:59:48 +10001040 logit("wtmp_write: problem writing %s: %s",
andre2ff7b5d2000-06-03 14:57:40 +00001041 WTMP_FILE, strerror(errno));
1042 ret = 0;
1043 }
Damien Miller8899ed32004-09-12 15:18:55 +10001044 close(fd);
1045 return (ret);
andre61e67252000-06-04 17:07:49 +00001046}
andre2ff7b5d2000-06-03 14:57:40 +00001047
andre2ff7b5d2000-06-03 14:57:40 +00001048static int
Damien Millerdd47aa22000-06-27 11:18:27 +10001049wtmp_perform_login(struct logininfo *li)
1050{
andre2ff7b5d2000-06-03 14:57:40 +00001051 struct utmp ut;
1052
1053 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001054 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001055}
andre2ff7b5d2000-06-03 14:57:40 +00001056
1057
1058static int
andre61e67252000-06-04 17:07:49 +00001059wtmp_perform_logout(struct logininfo *li)
1060{
andre2ff7b5d2000-06-03 14:57:40 +00001061 struct utmp ut;
1062
1063 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001064 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001065}
andre2ff7b5d2000-06-03 14:57:40 +00001066
1067
1068int
andre61e67252000-06-04 17:07:49 +00001069wtmp_write_entry(struct logininfo *li)
1070{
andre2ff7b5d2000-06-03 14:57:40 +00001071 switch(li->type) {
1072 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001073 return (wtmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001074 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001075 return (wtmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001076 default:
Damien Miller996acd22003-04-09 20:59:48 +10001077 logit("wtmp_write_entry: invalid type field");
Damien Miller8899ed32004-09-12 15:18:55 +10001078 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001079 }
andre61e67252000-06-04 17:07:49 +00001080}
andre2ff7b5d2000-06-03 14:57:40 +00001081
1082
Damien Miller8899ed32004-09-12 15:18:55 +10001083/*
1084 * Notes on fetching login data from wtmp/wtmpx
Kevin Stevesef4eea92001-02-05 12:42:17 +00001085 *
andre6bb92372000-06-19 08:20:03 +00001086 * Logouts are usually recorded with (amongst other things) a blank
1087 * username on a given tty line. However, some systems (HP-UX is one)
1088 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1089 *
1090 * Since we're only looking for logins here, we know that the username
1091 * must be set correctly. On systems that leave it in, we check for
1092 * ut_type==USER_PROCESS (indicating a login.)
1093 *
1094 * Portability: Some systems may set something other than USER_PROCESS
1095 * to indicate a login process. I don't know of any as I write. Also,
1096 * it's possible that some systems may both leave the username in
1097 * place and not have ut_type.
1098 */
1099
andre6bb92372000-06-19 08:20:03 +00001100/* return true if this wtmp entry indicates a login */
1101static int
1102wtmp_islogin(struct logininfo *li, struct utmp *ut)
1103{
Kevin Stevesef4eea92001-02-05 12:42:17 +00001104 if (strncmp(li->username, ut->ut_name,
Damien Miller8899ed32004-09-12 15:18:55 +10001105 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001106# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001107 if (ut->ut_type & USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001108 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001109# else
Damien Miller8899ed32004-09-12 15:18:55 +10001110 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001111# endif
andre6bb92372000-06-19 08:20:03 +00001112 }
Damien Miller8899ed32004-09-12 15:18:55 +10001113 return (0);
andre6bb92372000-06-19 08:20:03 +00001114}
1115
andre2ff7b5d2000-06-03 14:57:40 +00001116int
andre61e67252000-06-04 17:07:49 +00001117wtmp_get_entry(struct logininfo *li)
1118{
andre2ff7b5d2000-06-03 14:57:40 +00001119 struct stat st;
1120 struct utmp ut;
Damien Miller8899ed32004-09-12 15:18:55 +10001121 int fd, found = 0;
andre6bb92372000-06-19 08:20:03 +00001122
1123 /* Clear the time entries in our logininfo */
1124 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001125
1126 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001127 logit("wtmp_get_entry: problem opening %s: %s",
andre2ff7b5d2000-06-03 14:57:40 +00001128 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001129 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001130 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001131 if (fstat(fd, &st) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001132 logit("wtmp_get_entry: couldn't stat %s: %s",
andre2ff7b5d2000-06-03 14:57:40 +00001133 WTMP_FILE, strerror(errno));
1134 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001135 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001136 }
andre2ff7b5d2000-06-03 14:57:40 +00001137
andre6bb92372000-06-19 08:20:03 +00001138 /* Seek to the start of the last struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001139 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
andre6bb92372000-06-19 08:20:03 +00001140 /* Looks like we've got a fresh wtmp file */
1141 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001142 return (0);
andre6bb92372000-06-19 08:20:03 +00001143 }
1144
1145 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001146 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
Damien Miller996acd22003-04-09 20:59:48 +10001147 logit("wtmp_get_entry: read of %s failed: %s",
andre2ff7b5d2000-06-03 14:57:40 +00001148 WTMP_FILE, strerror(errno));
1149 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001150 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001151 }
andre6bb92372000-06-19 08:20:03 +00001152 if ( wtmp_islogin(li, &ut) ) {
1153 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001154 /*
1155 * We've already checked for a time in struct
1156 * utmp, in login_getlast()
1157 */
Damien Millerdd47aa22000-06-27 11:18:27 +10001158# ifdef HAVE_TIME_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +00001159 li->tv_sec = ut.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001160# else
andre2ff7b5d2000-06-03 14:57:40 +00001161# if HAVE_TV_IN_UTMP
1162 li->tv_sec = ut.ut_tv.tv_sec;
1163# endif
Damien Millerdd47aa22000-06-27 11:18:27 +10001164# endif
andre6bb92372000-06-19 08:20:03 +00001165 line_fullname(li->line, ut.ut_line,
Damien Miller8899ed32004-09-12 15:18:55 +10001166 MIN_SIZEOF(li->line, ut.ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +10001167# ifdef HAVE_HOST_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001168 strlcpy(li->hostname, ut.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001169 MIN_SIZEOF(li->hostname, ut.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001170# endif
andre6bb92372000-06-19 08:20:03 +00001171 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001172 }
andre6bb92372000-06-19 08:20:03 +00001173 /* Seek back 2 x struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001174 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
andre6bb92372000-06-19 08:20:03 +00001175 /* We've found the start of the file, so quit */
Damien Miller8899ed32004-09-12 15:18:55 +10001176 close(fd);
1177 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001178 }
andre6bb92372000-06-19 08:20:03 +00001179 }
1180
1181 /* We found an entry. Tidy up and return */
1182 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001183 return (1);
andre61e67252000-06-04 17:07:49 +00001184}
Damien Millerdd47aa22000-06-27 11:18:27 +10001185# endif /* USE_WTMP */
andre2ff7b5d2000-06-03 14:57:40 +00001186
1187
1188/**
andre61e67252000-06-04 17:07:49 +00001189 ** Low-level wtmpx functions
andre2ff7b5d2000-06-03 14:57:40 +00001190 **/
1191
1192#ifdef USE_WTMPX
Damien Miller8899ed32004-09-12 15:18:55 +10001193/*
1194 * Write a wtmpx entry direct to the end of the file
1195 * This is a slight modification of code in OpenBSD's logwtmp.c
1196 */
andre2ff7b5d2000-06-03 14:57:40 +00001197static int
andre61e67252000-06-04 17:07:49 +00001198wtmpx_write(struct logininfo *li, struct utmpx *utx)
1199{
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001200#ifndef HAVE_UPDWTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001201 struct stat buf;
1202 int fd, ret = 1;
1203
1204 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001205 logit("wtmpx_write: problem opening %s: %s",
andre2ff7b5d2000-06-03 14:57:40 +00001206 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001207 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001208 }
1209
Kevin Stevesef4eea92001-02-05 12:42:17 +00001210 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001211 if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
andre2ff7b5d2000-06-03 14:57:40 +00001212 ftruncate(fd, buf.st_size);
Damien Miller996acd22003-04-09 20:59:48 +10001213 logit("wtmpx_write: problem writing %s: %s",
andre2ff7b5d2000-06-03 14:57:40 +00001214 WTMPX_FILE, strerror(errno));
1215 ret = 0;
1216 }
Damien Miller8899ed32004-09-12 15:18:55 +10001217 close(fd);
andre2ff7b5d2000-06-03 14:57:40 +00001218
Damien Miller8899ed32004-09-12 15:18:55 +10001219 return (ret);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001220#else
1221 updwtmpx(WTMPX_FILE, utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001222 return (1);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001223#endif
andre61e67252000-06-04 17:07:49 +00001224}
andre2ff7b5d2000-06-03 14:57:40 +00001225
1226
1227static int
andre61e67252000-06-04 17:07:49 +00001228wtmpx_perform_login(struct logininfo *li)
1229{
andre2ff7b5d2000-06-03 14:57:40 +00001230 struct utmpx utx;
1231
1232 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001233 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001234}
andre2ff7b5d2000-06-03 14:57:40 +00001235
1236
1237static int
andre61e67252000-06-04 17:07:49 +00001238wtmpx_perform_logout(struct logininfo *li)
1239{
andre2ff7b5d2000-06-03 14:57:40 +00001240 struct utmpx utx;
1241
1242 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001243 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001244}
andre2ff7b5d2000-06-03 14:57:40 +00001245
1246
1247int
andre61e67252000-06-04 17:07:49 +00001248wtmpx_write_entry(struct logininfo *li)
1249{
andre2ff7b5d2000-06-03 14:57:40 +00001250 switch(li->type) {
1251 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001252 return (wtmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001253 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001254 return (wtmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001255 default:
Damien Miller996acd22003-04-09 20:59:48 +10001256 logit("wtmpx_write_entry: invalid type field");
Damien Miller8899ed32004-09-12 15:18:55 +10001257 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001258 }
andre61e67252000-06-04 17:07:49 +00001259}
andre2ff7b5d2000-06-03 14:57:40 +00001260
andre6bb92372000-06-19 08:20:03 +00001261/* Please see the notes above wtmp_islogin() for information about the
1262 next two functions */
1263
1264/* Return true if this wtmpx entry indicates a login */
1265static int
1266wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1267{
Damien Miller8899ed32004-09-12 15:18:55 +10001268 if (strncmp(li->username, utx->ut_name,
1269 MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001270# ifdef HAVE_TYPE_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +00001271 if (utx->ut_type == USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001272 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001273# else
Damien Miller8899ed32004-09-12 15:18:55 +10001274 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001275# endif
andre6bb92372000-06-19 08:20:03 +00001276 }
Damien Miller8899ed32004-09-12 15:18:55 +10001277 return (0);
andre6bb92372000-06-19 08:20:03 +00001278}
1279
andre2ff7b5d2000-06-03 14:57:40 +00001280
1281int
andre61e67252000-06-04 17:07:49 +00001282wtmpx_get_entry(struct logininfo *li)
1283{
andre2ff7b5d2000-06-03 14:57:40 +00001284 struct stat st;
1285 struct utmpx utx;
andre6bb92372000-06-19 08:20:03 +00001286 int fd, found=0;
1287
1288 /* Clear the time entries */
1289 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001290
1291 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001292 logit("wtmpx_get_entry: problem opening %s: %s",
andre2ff7b5d2000-06-03 14:57:40 +00001293 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001294 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001295 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001296 if (fstat(fd, &st) != 0) {
Damien Miller996acd22003-04-09 20:59:48 +10001297 logit("wtmpx_get_entry: couldn't stat %s: %s",
Tim Ricecdb82942002-07-14 15:33:20 -07001298 WTMPX_FILE, strerror(errno));
andre2ff7b5d2000-06-03 14:57:40 +00001299 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001300 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001301 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001302
andre6bb92372000-06-19 08:20:03 +00001303 /* Seek to the start of the last struct utmpx */
Kevin Steves52172652001-10-02 00:29:00 +00001304 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
andre6bb92372000-06-19 08:20:03 +00001305 /* probably a newly rotated wtmpx file */
1306 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001307 return (0);
andre6bb92372000-06-19 08:20:03 +00001308 }
andre2ff7b5d2000-06-03 14:57:40 +00001309
andre6bb92372000-06-19 08:20:03 +00001310 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001311 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
Damien Miller996acd22003-04-09 20:59:48 +10001312 logit("wtmpx_get_entry: read of %s failed: %s",
andre2ff7b5d2000-06-03 14:57:40 +00001313 WTMPX_FILE, strerror(errno));
1314 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001315 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001316 }
Damien Miller8899ed32004-09-12 15:18:55 +10001317 /*
1318 * Logouts are recorded as a blank username on a particular
1319 * line. So, we just need to find the username in struct utmpx
1320 */
1321 if (wtmpx_islogin(li, &utx)) {
Tim Rice370e0ba2002-07-14 15:50:51 -07001322 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001323# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001324 li->tv_sec = utx.ut_tv.tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +10001325# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001326 li->tv_sec = utx.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001327# endif
Damien Miller1a132252000-06-13 21:23:17 +10001328 line_fullname(li->line, utx.ut_line, sizeof(li->line));
Damien Miller8899ed32004-09-12 15:18:55 +10001329# if defined(HAVE_HOST_IN_UTMPX)
andre6bb92372000-06-19 08:20:03 +00001330 strlcpy(li->hostname, utx.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001331 MIN_SIZEOF(li->hostname, utx.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001332# endif
andre6bb92372000-06-19 08:20:03 +00001333 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001334 }
Kevin Steves52172652001-10-02 00:29:00 +00001335 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
Damien Miller8899ed32004-09-12 15:18:55 +10001336 close(fd);
1337 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001338 }
andre6bb92372000-06-19 08:20:03 +00001339 }
1340
1341 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001342 return (1);
andre61e67252000-06-04 17:07:49 +00001343}
Damien Millerd5bf3072000-06-07 21:32:13 +10001344#endif /* USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001345
andre2ff7b5d2000-06-03 14:57:40 +00001346/**
andre61e67252000-06-04 17:07:49 +00001347 ** Low-level libutil login() functions
andre2ff7b5d2000-06-03 14:57:40 +00001348 **/
1349
1350#ifdef USE_LOGIN
andre2ff7b5d2000-06-03 14:57:40 +00001351static int
andre61e67252000-06-04 17:07:49 +00001352syslogin_perform_login(struct logininfo *li)
1353{
andre2ff7b5d2000-06-03 14:57:40 +00001354 struct utmp *ut;
1355
Damien Miller8899ed32004-09-12 15:18:55 +10001356 if ((ut = (struct utmp *)malloc(sizeof(*ut))) == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +10001357 logit("syslogin_perform_login: couldn't malloc()");
Damien Miller8899ed32004-09-12 15:18:55 +10001358 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001359 }
1360 construct_utmp(li, ut);
1361 login(ut);
Damien Millerf211efc2003-03-10 11:23:06 +11001362 free(ut);
andre2ff7b5d2000-06-03 14:57:40 +00001363
Damien Miller8899ed32004-09-12 15:18:55 +10001364 return (1);
andre61e67252000-06-04 17:07:49 +00001365}
1366
andre2ff7b5d2000-06-03 14:57:40 +00001367static int
andre61e67252000-06-04 17:07:49 +00001368syslogin_perform_logout(struct logininfo *li)
1369{
Damien Millerdd47aa22000-06-27 11:18:27 +10001370# ifdef HAVE_LOGOUT
Darren Tucker4d2f3612004-04-08 10:57:05 +10001371 char line[UT_LINESIZE];
Kevin Stevesef4eea92001-02-05 12:42:17 +00001372
andre2ff7b5d2000-06-03 14:57:40 +00001373 (void)line_stripname(line, li->line, sizeof(line));
1374
Damien Miller8899ed32004-09-12 15:18:55 +10001375 if (!logout(line))
Damien Miller996acd22003-04-09 20:59:48 +10001376 logit("syslogin_perform_logout: logout() returned an error");
Damien Millerdd47aa22000-06-27 11:18:27 +10001377# ifdef HAVE_LOGWTMP
Damien Miller8899ed32004-09-12 15:18:55 +10001378 else
andre2ff7b5d2000-06-03 14:57:40 +00001379 logwtmp(line, "", "");
Damien Millerdd47aa22000-06-27 11:18:27 +10001380# endif
andre6bb92372000-06-19 08:20:03 +00001381 /* FIXME: (ATL - if the need arises) What to do if we have
1382 * login, but no logout? what if logout but no logwtmp? All
1383 * routines are in libutil so they should all be there,
1384 * but... */
Damien Millerdd47aa22000-06-27 11:18:27 +10001385# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001386 return (1);
andre61e67252000-06-04 17:07:49 +00001387}
andre2ff7b5d2000-06-03 14:57:40 +00001388
andre2ff7b5d2000-06-03 14:57:40 +00001389int
andre61e67252000-06-04 17:07:49 +00001390syslogin_write_entry(struct logininfo *li)
1391{
andre2ff7b5d2000-06-03 14:57:40 +00001392 switch (li->type) {
1393 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001394 return (syslogin_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001395 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001396 return (syslogin_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001397 default:
Damien Miller996acd22003-04-09 20:59:48 +10001398 logit("syslogin_write_entry: Invalid type field");
Damien Miller8899ed32004-09-12 15:18:55 +10001399 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001400 }
andre61e67252000-06-04 17:07:49 +00001401}
Damien Millerd5bf3072000-06-07 21:32:13 +10001402#endif /* USE_LOGIN */
andre2ff7b5d2000-06-03 14:57:40 +00001403
1404/* end of file log-syslogin.c */
1405
andre2ff7b5d2000-06-03 14:57:40 +00001406/**
andre61e67252000-06-04 17:07:49 +00001407 ** Low-level lastlog functions
andre2ff7b5d2000-06-03 14:57:40 +00001408 **/
1409
1410#ifdef USE_LASTLOG
Damien Millerdd47aa22000-06-27 11:18:27 +10001411#define LL_FILE 1
1412#define LL_DIR 2
1413#define LL_OTHER 3
andre2ff7b5d2000-06-03 14:57:40 +00001414
andre2ff7b5d2000-06-03 14:57:40 +00001415static void
andre61e67252000-06-04 17:07:49 +00001416lastlog_construct(struct logininfo *li, struct lastlog *last)
1417{
andre2ff7b5d2000-06-03 14:57:40 +00001418 /* clear the structure */
Damien Miller348c9b72000-08-15 10:01:22 +10001419 memset(last, '\0', sizeof(*last));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001420
Damien Miller8899ed32004-09-12 15:18:55 +10001421 line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
andre6bb92372000-06-19 08:20:03 +00001422 strlcpy(last->ll_host, li->hostname,
1423 MIN_SIZEOF(last->ll_host, li->hostname));
andre2ff7b5d2000-06-03 14:57:40 +00001424 last->ll_time = li->tv_sec;
andre61e67252000-06-04 17:07:49 +00001425}
andre2ff7b5d2000-06-03 14:57:40 +00001426
andre2ff7b5d2000-06-03 14:57:40 +00001427static int
andre61e67252000-06-04 17:07:49 +00001428lastlog_filetype(char *filename)
1429{
andre2ff7b5d2000-06-03 14:57:40 +00001430 struct stat st;
1431
Damien Millerdd47aa22000-06-27 11:18:27 +10001432 if (stat(LASTLOG_FILE, &st) != 0) {
Damien Miller8899ed32004-09-12 15:18:55 +10001433 logit("lastlog_perform_login: Couldn't stat %s: %s",
1434 LASTLOG_FILE, strerror(errno));
1435 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001436 }
andre2ff7b5d2000-06-03 14:57:40 +00001437 if (S_ISDIR(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001438 return (LL_DIR);
andre2ff7b5d2000-06-03 14:57:40 +00001439 else if (S_ISREG(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001440 return (LL_FILE);
andre2ff7b5d2000-06-03 14:57:40 +00001441 else
Damien Miller8899ed32004-09-12 15:18:55 +10001442 return (LL_OTHER);
andre61e67252000-06-04 17:07:49 +00001443}
andre2ff7b5d2000-06-03 14:57:40 +00001444
1445
1446/* open the file (using filemode) and seek to the login entry */
1447static int
andre61e67252000-06-04 17:07:49 +00001448lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1449{
andre2ff7b5d2000-06-03 14:57:40 +00001450 off_t offset;
1451 int type;
1452 char lastlog_file[1024];
1453
1454 type = lastlog_filetype(LASTLOG_FILE);
1455 switch (type) {
Damien Miller8899ed32004-09-12 15:18:55 +10001456 case LL_FILE:
1457 strlcpy(lastlog_file, LASTLOG_FILE,
1458 sizeof(lastlog_file));
1459 break;
1460 case LL_DIR:
1461 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1462 LASTLOG_FILE, li->username);
1463 break;
1464 default:
1465 logit("lastlog_openseek: %.100s is not a file or directory!",
1466 LASTLOG_FILE);
1467 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001468 }
andre2ff7b5d2000-06-03 14:57:40 +00001469
Damien Miller405dc602003-04-09 21:12:52 +10001470 *fd = open(lastlog_file, filemode, 0600);
Damien Miller8899ed32004-09-12 15:18:55 +10001471 if (*fd < 0) {
Damien Miller53c5d462000-06-28 00:50:50 +10001472 debug("lastlog_openseek: Couldn't open %s: %s",
andre2ff7b5d2000-06-03 14:57:40 +00001473 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001474 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001475 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001476
Damien Millere477ef62000-08-15 10:21:17 +10001477 if (type == LL_FILE) {
1478 /* find this uid's offset in the lastlog file */
Kevin Steves52172652001-10-02 00:29:00 +00001479 offset = (off_t) ((long)li->uid * sizeof(struct lastlog));
andre2ff7b5d2000-06-03 14:57:40 +00001480
Damien Miller8899ed32004-09-12 15:18:55 +10001481 if (lseek(*fd, offset, SEEK_SET) != offset) {
Damien Miller996acd22003-04-09 20:59:48 +10001482 logit("lastlog_openseek: %s->lseek(): %s",
Kevin Stevesef4eea92001-02-05 12:42:17 +00001483 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001484 return (0);
Damien Millere477ef62000-08-15 10:21:17 +10001485 }
andre2ff7b5d2000-06-03 14:57:40 +00001486 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001487
Damien Miller8899ed32004-09-12 15:18:55 +10001488 return (1);
andre61e67252000-06-04 17:07:49 +00001489}
andre2ff7b5d2000-06-03 14:57:40 +00001490
1491static int
andre61e67252000-06-04 17:07:49 +00001492lastlog_perform_login(struct logininfo *li)
1493{
andre2ff7b5d2000-06-03 14:57:40 +00001494 struct lastlog last;
1495 int fd;
1496
1497 /* create our struct lastlog */
1498 lastlog_construct(li, &last);
1499
Damien Miller405dc602003-04-09 21:12:52 +10001500 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
Damien Miller8899ed32004-09-12 15:18:55 +10001501 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001502
andre2ff7b5d2000-06-03 14:57:40 +00001503 /* write the entry */
Darren Tucker8661b562003-07-06 15:20:46 +10001504 if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) {
Damien Millerc1132e72000-08-18 14:08:38 +10001505 close(fd);
Damien Miller996acd22003-04-09 20:59:48 +10001506 logit("lastlog_write_filemode: Error writing to %s: %s",
Damien Millerc1132e72000-08-18 14:08:38 +10001507 LASTLOG_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001508 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001509 }
Damien Millerc1132e72000-08-18 14:08:38 +10001510
1511 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001512 return (1);
andre61e67252000-06-04 17:07:49 +00001513}
andre2ff7b5d2000-06-03 14:57:40 +00001514
andre2ff7b5d2000-06-03 14:57:40 +00001515int
andre61e67252000-06-04 17:07:49 +00001516lastlog_write_entry(struct logininfo *li)
1517{
andre2ff7b5d2000-06-03 14:57:40 +00001518 switch(li->type) {
1519 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001520 return (lastlog_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001521 default:
Damien Miller996acd22003-04-09 20:59:48 +10001522 logit("lastlog_write_entry: Invalid type field");
Damien Miller8899ed32004-09-12 15:18:55 +10001523 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001524 }
andre61e67252000-06-04 17:07:49 +00001525}
andre2ff7b5d2000-06-03 14:57:40 +00001526
andre2ff7b5d2000-06-03 14:57:40 +00001527static void
andre61e67252000-06-04 17:07:49 +00001528lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1529{
andre2ff7b5d2000-06-03 14:57:40 +00001530 line_fullname(li->line, last->ll_line, sizeof(li->line));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001531 strlcpy(li->hostname, last->ll_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001532 MIN_SIZEOF(li->hostname, last->ll_host));
andre2ff7b5d2000-06-03 14:57:40 +00001533 li->tv_sec = last->ll_time;
andre61e67252000-06-04 17:07:49 +00001534}
andre2ff7b5d2000-06-03 14:57:40 +00001535
andre2ff7b5d2000-06-03 14:57:40 +00001536int
andre61e67252000-06-04 17:07:49 +00001537lastlog_get_entry(struct logininfo *li)
1538{
andre2ff7b5d2000-06-03 14:57:40 +00001539 struct lastlog last;
Damien Miller7df881d2003-01-07 16:46:58 +11001540 int fd, ret;
andre2ff7b5d2000-06-03 14:57:40 +00001541
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001542 if (!lastlog_openseek(li, &fd, O_RDONLY))
Damien Miller7df881d2003-01-07 16:46:58 +11001543 return (0);
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001544
Damien Miller7df881d2003-01-07 16:46:58 +11001545 ret = atomicio(read, fd, &last, sizeof(last));
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001546 close(fd);
1547
Damien Miller7df881d2003-01-07 16:46:58 +11001548 switch (ret) {
1549 case 0:
1550 memset(&last, '\0', sizeof(last));
1551 /* FALLTHRU */
1552 case sizeof(last):
1553 lastlog_populate_entry(li, &last);
1554 return (1);
1555 case -1:
Damien Millera8e06ce2003-11-21 23:48:55 +11001556 error("%s: Error reading from %s: %s", __func__,
Damien Miller7df881d2003-01-07 16:46:58 +11001557 LASTLOG_FILE, strerror(errno));
1558 return (0);
1559 default:
1560 error("%s: Error reading from %s: Expecting %d, got %d",
1561 __func__, LASTLOG_FILE, sizeof(last), ret);
1562 return (0);
1563 }
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001564
Damien Miller7df881d2003-01-07 16:46:58 +11001565 /* NOTREACHED */
1566 return (0);
andre61e67252000-06-04 17:07:49 +00001567}
Damien Millerd5bf3072000-06-07 21:32:13 +10001568#endif /* USE_LASTLOG */