blob: 9f9eb69a7be0a167b129167b6db2ffeccfb522ff [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
Darren Tucker2fba9932005-02-02 23:30:24 +110028/*
29 * The btmp logging code is derived from login.c from util-linux and is under
30 * the the following license:
31 *
32 * Copyright (c) 1980, 1987, 1988 The Regents of the University of California.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms are permitted
36 * provided that the above copyright notice and this paragraph are
37 * duplicated in all such forms and that any documentation,
38 * advertising materials, and other materials related to such
39 * distribution and use acknowledge that the software was developed
40 * by the University of California, Berkeley. The name of the
41 * University may not be used to endorse or promote products derived
42 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
44 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
45 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 */
47
48
Kevin Stevesef4eea92001-02-05 12:42:17 +000049/**
andre2ff7b5d2000-06-03 14:57:40 +000050 ** loginrec.c: platform-independent login recording and lastlog retrieval
51 **/
52
andre61e67252000-06-04 17:07:49 +000053/*
Damien Miller8899ed32004-09-12 15:18:55 +100054 * The new login code explained
55 * ============================
56 *
57 * This code attempts to provide a common interface to login recording
58 * (utmp and friends) and last login time retrieval.
59 *
60 * Its primary means of achieving this is to use 'struct logininfo', a
61 * union of all the useful fields in the various different types of
62 * system login record structures one finds on UNIX variants.
63 *
64 * We depend on autoconf to define which recording methods are to be
65 * used, and which fields are contained in the relevant data structures
66 * on the local system. Many C preprocessor symbols affect which code
67 * gets compiled here.
68 *
69 * The code is designed to make it easy to modify a particular
70 * recording method, without affecting other methods nor requiring so
71 * many nested conditional compilation blocks as were commonplace in
72 * the old code.
73 *
74 * For login recording, we try to use the local system's libraries as
75 * these are clearly most likely to work correctly. For utmp systems
76 * this usually means login() and logout() or setutent() etc., probably
77 * in libutil, along with logwtmp() etc. On these systems, we fall back
78 * to writing the files directly if we have to, though this method
79 * requires very thorough testing so we do not corrupt local auditing
80 * information. These files and their access methods are very system
81 * specific indeed.
82 *
83 * For utmpx systems, the corresponding library functions are
84 * setutxent() etc. To the author's knowledge, all utmpx systems have
85 * these library functions and so no direct write is attempted. If such
86 * a system exists and needs support, direct analogues of the [uw]tmp
87 * code should suffice.
88 *
89 * Retrieving the time of last login ('lastlog') is in some ways even
90 * more problemmatic than login recording. Some systems provide a
91 * simple table of all users which we seek based on uid and retrieve a
92 * relatively standard structure. Others record the same information in
93 * a directory with a separate file, and others don't record the
94 * information separately at all. For systems in the latter category,
95 * we look backwards in the wtmp or wtmpx file for the last login entry
96 * for our user. Naturally this is slower and on busy systems could
97 * incur a significant performance penalty.
98 *
99 * Calling the new code
100 * --------------------
101 *
102 * In OpenSSH all login recording and retrieval is performed in
103 * login.c. Here you'll find working examples. Also, in the logintest.c
104 * program there are more examples.
105 *
106 * Internal handler calling method
107 * -------------------------------
108 *
109 * When a call is made to login_login() or login_logout(), both
110 * routines set a struct logininfo flag defining which action (log in,
111 * or log out) is to be taken. They both then call login_write(), which
112 * calls whichever of the many structure-specific handlers autoconf
113 * selects for the local system.
114 *
115 * The handlers themselves handle system data structure specifics. Both
116 * struct utmp and struct utmpx have utility functions (see
117 * construct_utmp*()) to try to make it simpler to add extra systems
118 * that introduce new features to either structure.
119 *
120 * While it may seem terribly wasteful to replicate so much similar
121 * code for each method, experience has shown that maintaining code to
122 * write both struct utmp and utmpx in one function, whilst maintaining
123 * support for all systems whether they have library support or not, is
124 * a difficult and time-consuming task.
125 *
126 * Lastlog support proceeds similarly. Functions login_get_lastlog()
127 * (and its OpenSSH-tuned friend login_get_lastlog_time()) call
128 * getlast_entry(), which tries one of three methods to find the last
129 * login time. It uses local system lastlog support if it can,
130 * otherwise it tries wtmp or wtmpx before giving up and returning 0,
131 * meaning "tilt".
132 *
133 * Maintenance
134 * -----------
135 *
136 * In many cases it's possible to tweak autoconf to select the correct
137 * methods for a particular platform, either by improving the detection
138 * code (best), or by presetting DISABLE_<method> or CONF_<method>_FILE
139 * symbols for the platform.
140 *
141 * Use logintest to check which symbols are defined before modifying
142 * configure.ac and loginrec.c. (You have to build logintest yourself
143 * with 'make logintest' as it's not built by default.)
144 *
145 * Otherwise, patches to the specific method(s) are very helpful!
146 */
andre2ff7b5d2000-06-03 14:57:40 +0000147
148#include "includes.h"
149
Damien Miller62772522006-03-15 14:01:11 +1100150#include <sys/types.h>
151#include <sys/stat.h>
152
andre2ff7b5d2000-06-03 14:57:40 +0000153#include "ssh.h"
154#include "xmalloc.h"
155#include "loginrec.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000156#include "log.h"
157#include "atomicio.h"
Darren Tucker2fba9932005-02-02 23:30:24 +1100158#include "packet.h"
159#include "canohost.h"
Darren Tucker269a1ea2005-02-03 00:20:53 +1100160#include "auth.h"
Darren Tuckera39f83e2005-02-15 22:19:28 +1100161#include "buffer.h"
andre2ff7b5d2000-06-03 14:57:40 +0000162
Ben Lindstromdcca9812000-11-10 03:28:31 +0000163#ifdef HAVE_UTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000164# include <util.h>
Ben Lindstromdcca9812000-11-10 03:28:31 +0000165#endif
andre2ff7b5d2000-06-03 14:57:40 +0000166
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000167#ifdef HAVE_LIBUTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000168# include <libutil.h>
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000169#endif
170
Damien Miller62772522006-03-15 14:01:11 +1100171RCSID("$Id: loginrec.c,v 1.72 2006/03/15 03:01:11 djm Exp $");
Damien Miller8899ed32004-09-12 15:18:55 +1000172
andre2ff7b5d2000-06-03 14:57:40 +0000173/**
174 ** prototypes for helper functions in this file
175 **/
176
177#if HAVE_UTMP_H
andre2ff7b5d2000-06-03 14:57:40 +0000178void set_utmp_time(struct logininfo *li, struct utmp *ut);
179void construct_utmp(struct logininfo *li, struct utmp *ut);
180#endif
181
182#ifdef HAVE_UTMPX_H
andre2ff7b5d2000-06-03 14:57:40 +0000183void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
184void construct_utmpx(struct logininfo *li, struct utmpx *ut);
185#endif
186
187int utmp_write_entry(struct logininfo *li);
188int utmpx_write_entry(struct logininfo *li);
189int wtmp_write_entry(struct logininfo *li);
190int wtmpx_write_entry(struct logininfo *li);
191int lastlog_write_entry(struct logininfo *li);
192int syslogin_write_entry(struct logininfo *li);
193
194int getlast_entry(struct logininfo *li);
195int lastlog_get_entry(struct logininfo *li);
196int wtmp_get_entry(struct logininfo *li);
197int wtmpx_get_entry(struct logininfo *li);
198
Darren Tucker691d5232005-02-15 21:45:57 +1100199extern Buffer loginmsg;
200
andre6bb92372000-06-19 08:20:03 +0000201/* pick the shortest string */
Damien Miller8899ed32004-09-12 15:18:55 +1000202#define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2))
andre6bb92372000-06-19 08:20:03 +0000203
andre2ff7b5d2000-06-03 14:57:40 +0000204/**
205 ** platform-independent login functions
206 **/
207
Damien Miller8899ed32004-09-12 15:18:55 +1000208/*
209 * login_login(struct logininfo *) - Record a login
Kevin Stevesef4eea92001-02-05 12:42:17 +0000210 *
andre6bb92372000-06-19 08:20:03 +0000211 * Call with a pointer to a struct logininfo initialised with
212 * login_init_entry() or login_alloc_entry()
213 *
214 * Returns:
215 * >0 if successful
216 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
217 */
andre61e67252000-06-04 17:07:49 +0000218int
Damien Miller8899ed32004-09-12 15:18:55 +1000219login_login(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000220{
221 li->type = LTYPE_LOGIN;
Damien Miller8899ed32004-09-12 15:18:55 +1000222 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000223}
224
225
Damien Miller8899ed32004-09-12 15:18:55 +1000226/*
227 * login_logout(struct logininfo *) - Record a logout
andre6bb92372000-06-19 08:20:03 +0000228 *
229 * Call as with login_login()
230 *
231 * Returns:
232 * >0 if successful
233 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
234 */
andre61e67252000-06-04 17:07:49 +0000235int
236login_logout(struct logininfo *li)
237{
238 li->type = LTYPE_LOGOUT;
Damien Miller8899ed32004-09-12 15:18:55 +1000239 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000240}
241
Damien Miller8899ed32004-09-12 15:18:55 +1000242/*
243 * login_get_lastlog_time(int) - Retrieve the last login time
andre6bb92372000-06-19 08:20:03 +0000244 *
245 * Retrieve the last login time for the given uid. Will try to use the
246 * system lastlog facilities if they are available, but will fall back
247 * to looking in wtmp/wtmpx if necessary
248 *
249 * Returns:
250 * 0 on failure, or if user has never logged in
251 * Time in seconds from the epoch if successful
252 *
253 * Useful preprocessor symbols:
254 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
255 * info
256 * USE_LASTLOG: If set, indicates the presence of system lastlog
257 * facilities. If this and DISABLE_LASTLOG are not set,
258 * try to retrieve lastlog information from wtmp/wtmpx.
259 */
andre61e67252000-06-04 17:07:49 +0000260unsigned int
261login_get_lastlog_time(const int uid)
262{
263 struct logininfo li;
264
andre6bb92372000-06-19 08:20:03 +0000265 if (login_get_lastlog(&li, uid))
Damien Miller8899ed32004-09-12 15:18:55 +1000266 return (li.tv_sec);
andre6bb92372000-06-19 08:20:03 +0000267 else
Damien Miller8899ed32004-09-12 15:18:55 +1000268 return (0);
andre61e67252000-06-04 17:07:49 +0000269}
270
Damien Miller8899ed32004-09-12 15:18:55 +1000271/*
272 * login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
andre6bb92372000-06-19 08:20:03 +0000273 *
274 * Retrieve a logininfo structure populated (only partially) with
275 * information from the system lastlog data, or from wtmp/wtmpx if no
276 * system lastlog information exists.
277 *
278 * Note this routine must be given a pre-allocated logininfo.
279 *
280 * Returns:
281 * >0: A pointer to your struct logininfo if successful
282 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
andre6bb92372000-06-19 08:20:03 +0000283 */
andre61e67252000-06-04 17:07:49 +0000284struct logininfo *
285login_get_lastlog(struct logininfo *li, const int uid)
286{
andre6bb92372000-06-19 08:20:03 +0000287 struct passwd *pw;
andre6bb92372000-06-19 08:20:03 +0000288
Damien Miller348c9b72000-08-15 10:01:22 +1000289 memset(li, '\0', sizeof(*li));
andre61e67252000-06-04 17:07:49 +0000290 li->uid = uid;
andre6bb92372000-06-19 08:20:03 +0000291
Kevin Stevesef4eea92001-02-05 12:42:17 +0000292 /*
Damien Miller53c5d462000-06-28 00:50:50 +1000293 * If we don't have a 'real' lastlog, we need the username to
andre6bb92372000-06-19 08:20:03 +0000294 * reliably search wtmp(x) for the last login (see
Kevin Stevesef4eea92001-02-05 12:42:17 +0000295 * wtmp_get_entry().)
Damien Miller53c5d462000-06-28 00:50:50 +1000296 */
andre6bb92372000-06-19 08:20:03 +0000297 pw = getpwuid(uid);
Damien Millerdd47aa22000-06-27 11:18:27 +1000298 if (pw == NULL)
Damien Miller6b0279c2004-09-12 15:25:17 +1000299 fatal("%s: Cannot find account for uid %i", __func__, uid);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000300
andre98cabe02000-06-19 09:11:30 +0000301 /* No MIN_SIZEOF here - we absolutely *must not* truncate the
Damien Miller8899ed32004-09-12 15:18:55 +1000302 * username (XXX - so check for trunc!) */
Damien Millerf8af08d2000-06-27 09:40:06 +1000303 strlcpy(li->username, pw->pw_name, sizeof(li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000304
andre61e67252000-06-04 17:07:49 +0000305 if (getlast_entry(li))
Damien Miller8899ed32004-09-12 15:18:55 +1000306 return (li);
andre61e67252000-06-04 17:07:49 +0000307 else
Damien Miller8899ed32004-09-12 15:18:55 +1000308 return (NULL);
andre61e67252000-06-04 17:07:49 +0000309}
310
311
Damien Miller8899ed32004-09-12 15:18:55 +1000312/*
313 * login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
Kevin Stevesef4eea92001-02-05 12:42:17 +0000314 * a logininfo structure
315 *
andre6bb92372000-06-19 08:20:03 +0000316 * This function creates a new struct logininfo, a data structure
317 * meant to carry the information required to portably record login info.
318 *
319 * Returns a pointer to a newly created struct logininfo. If memory
320 * allocation fails, the program halts.
321 */
andre61e67252000-06-04 17:07:49 +0000322struct
323logininfo *login_alloc_entry(int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000324 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000325{
andre2ff7b5d2000-06-03 14:57:40 +0000326 struct logininfo *newli;
327
Damien Miller8899ed32004-09-12 15:18:55 +1000328 newli = xmalloc(sizeof(*newli));
329 login_init_entry(newli, pid, username, hostname, line);
330 return (newli);
andre61e67252000-06-04 17:07:49 +0000331}
andre2ff7b5d2000-06-03 14:57:40 +0000332
333
andre6bb92372000-06-19 08:20:03 +0000334/* login_free_entry(struct logininfo *) - free struct memory */
andre61e67252000-06-04 17:07:49 +0000335void
336login_free_entry(struct logininfo *li)
337{
338 xfree(li);
339}
340
andre2ff7b5d2000-06-03 14:57:40 +0000341
andre6bb92372000-06-19 08:20:03 +0000342/* login_init_entry(struct logininfo *, int, char*, char*, char*)
343 * - initialise a struct logininfo
Kevin Stevesef4eea92001-02-05 12:42:17 +0000344 *
andre6bb92372000-06-19 08:20:03 +0000345 * Populates a new struct logininfo, a data structure meant to carry
346 * the information required to portably record login info.
347 *
348 * Returns: 1
349 */
andre61e67252000-06-04 17:07:49 +0000350int
Kevin Stevesef4eea92001-02-05 12:42:17 +0000351login_init_entry(struct logininfo *li, int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000352 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000353{
Damien Millerf8af08d2000-06-27 09:40:06 +1000354 struct passwd *pw;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000355
Damien Miller348c9b72000-08-15 10:01:22 +1000356 memset(li, 0, sizeof(*li));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000357
andre61e67252000-06-04 17:07:49 +0000358 li->pid = pid;
Damien Millerf8af08d2000-06-27 09:40:06 +1000359
andre2ff7b5d2000-06-03 14:57:40 +0000360 /* set the line information */
andre61e67252000-06-04 17:07:49 +0000361 if (line)
andre2ff7b5d2000-06-03 14:57:40 +0000362 line_fullname(li->line, line, sizeof(li->line));
andre2ff7b5d2000-06-03 14:57:40 +0000363
Damien Millerf8af08d2000-06-27 09:40:06 +1000364 if (username) {
andre2ff7b5d2000-06-03 14:57:40 +0000365 strlcpy(li->username, username, sizeof(li->username));
Damien Millerf8af08d2000-06-27 09:40:06 +1000366 pw = getpwnam(li->username);
Damien Miller8899ed32004-09-12 15:18:55 +1000367 if (pw == NULL) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000368 fatal("%s: Cannot find user \"%s\"", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +1000369 li->username);
370 }
Damien Millerf8af08d2000-06-27 09:40:06 +1000371 li->uid = pw->pw_uid;
372 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000373
andre61e67252000-06-04 17:07:49 +0000374 if (hostname)
andre2ff7b5d2000-06-03 14:57:40 +0000375 strlcpy(li->hostname, hostname, sizeof(li->hostname));
Damien Millerf8af08d2000-06-27 09:40:06 +1000376
Damien Miller8899ed32004-09-12 15:18:55 +1000377 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000378}
379
Damien Miller94cf4c82005-07-17 17:04:47 +1000380/*
Damien Miller8899ed32004-09-12 15:18:55 +1000381 * login_set_current_time(struct logininfo *) - set the current time
andre6bb92372000-06-19 08:20:03 +0000382 *
383 * Set the current time in a logininfo structure. This function is
384 * meant to eliminate the need to deal with system dependencies for
385 * time handling.
386 */
andre2ff7b5d2000-06-03 14:57:40 +0000387void
andre61e67252000-06-04 17:07:49 +0000388login_set_current_time(struct logininfo *li)
389{
andre2ff7b5d2000-06-03 14:57:40 +0000390 struct timeval tv;
391
392 gettimeofday(&tv, NULL);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000393
Damien Millerf8af08d2000-06-27 09:40:06 +1000394 li->tv_sec = tv.tv_sec;
395 li->tv_usec = tv.tv_usec;
andre2ff7b5d2000-06-03 14:57:40 +0000396}
397
andre61e67252000-06-04 17:07:49 +0000398/* copy a sockaddr_* into our logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000399void
andre61e67252000-06-04 17:07:49 +0000400login_set_addr(struct logininfo *li, const struct sockaddr *sa,
Damien Miller8899ed32004-09-12 15:18:55 +1000401 const unsigned int sa_size)
andre61e67252000-06-04 17:07:49 +0000402{
403 unsigned int bufsize = sa_size;
404
405 /* make sure we don't overrun our union */
406 if (sizeof(li->hostaddr) < sa_size)
407 bufsize = sizeof(li->hostaddr);
408
Damien Miller8899ed32004-09-12 15:18:55 +1000409 memcpy(&li->hostaddr.sa, sa, bufsize);
andre2ff7b5d2000-06-03 14:57:40 +0000410}
411
andre2ff7b5d2000-06-03 14:57:40 +0000412
andre61e67252000-06-04 17:07:49 +0000413/**
414 ** login_write: Call low-level recording functions based on autoconf
415 ** results
416 **/
andre2ff7b5d2000-06-03 14:57:40 +0000417int
Damien Miller8899ed32004-09-12 15:18:55 +1000418login_write(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000419{
Damien Millerbac2d8a2000-09-05 16:13:06 +1100420#ifndef HAVE_CYGWIN
Damien Miller8899ed32004-09-12 15:18:55 +1000421 if (geteuid() != 0) {
422 logit("Attempt to write login records by non-root user (aborting)");
423 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000424 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100425#endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000426
andre2ff7b5d2000-06-03 14:57:40 +0000427 /* set the timestamp */
428 login_set_current_time(li);
429#ifdef USE_LOGIN
430 syslogin_write_entry(li);
431#endif
432#ifdef USE_LASTLOG
Damien Miller8899ed32004-09-12 15:18:55 +1000433 if (li->type == LTYPE_LOGIN)
andre2ff7b5d2000-06-03 14:57:40 +0000434 lastlog_write_entry(li);
andre2ff7b5d2000-06-03 14:57:40 +0000435#endif
436#ifdef USE_UTMP
437 utmp_write_entry(li);
438#endif
439#ifdef USE_WTMP
440 wtmp_write_entry(li);
441#endif
442#ifdef USE_UTMPX
443 utmpx_write_entry(li);
444#endif
445#ifdef USE_WTMPX
446 wtmpx_write_entry(li);
447#endif
Darren Tucker397a2f22004-08-15 00:09:11 +1000448#ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN
Damien Miller94cf4c82005-07-17 17:04:47 +1000449 if (li->type == LTYPE_LOGIN &&
Damien Millerb6f72f52005-07-17 17:26:43 +1000450 !sys_auth_record_login(li->username,li->hostname,li->line,
451 &loginmsg))
Darren Tucker397a2f22004-08-15 00:09:11 +1000452 logit("Writing login record failed for %s", li->username);
453#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100454#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100455 if (li->type == LTYPE_LOGIN)
456 audit_session_open(li->line);
457 else if (li->type == LTYPE_LOGOUT)
458 audit_session_close(li->line);
459#endif
Damien Miller8899ed32004-09-12 15:18:55 +1000460 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000461}
462
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000463#ifdef LOGIN_NEEDS_UTMPX
464int
465login_utmp_only(struct logininfo *li)
466{
Damien Millera8e06ce2003-11-21 23:48:55 +1100467 li->type = LTYPE_LOGIN;
Ben Lindstrom9197c592001-10-26 15:56:55 +0000468 login_set_current_time(li);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000469# ifdef USE_UTMP
470 utmp_write_entry(li);
471# endif
472# ifdef USE_WTMP
473 wtmp_write_entry(li);
474# endif
475# ifdef USE_UTMPX
476 utmpx_write_entry(li);
477# endif
478# ifdef USE_WTMPX
479 wtmpx_write_entry(li);
480# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000481 return (0);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000482}
483#endif
484
andre2ff7b5d2000-06-03 14:57:40 +0000485/**
andre61e67252000-06-04 17:07:49 +0000486 ** getlast_entry: Call low-level functions to retrieve the last login
487 ** time.
andre2ff7b5d2000-06-03 14:57:40 +0000488 **/
489
andre61e67252000-06-04 17:07:49 +0000490/* take the uid in li and return the last login time */
491int
492getlast_entry(struct logininfo *li)
493{
494#ifdef USE_LASTLOG
Damien Miller53c5d462000-06-28 00:50:50 +1000495 return(lastlog_get_entry(li));
Damien Millerdd47aa22000-06-27 11:18:27 +1000496#else /* !USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000497
Damien Miller8899ed32004-09-12 15:18:55 +1000498#if defined(DISABLE_LASTLOG)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000499 /* On some systems we shouldn't even try to obtain last login
andreecaabf12000-06-12 22:21:44 +0000500 * time, e.g. AIX */
Damien Miller8899ed32004-09-12 15:18:55 +1000501 return (0);
502# elif defined(USE_WTMP) && \
503 (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
andre61e67252000-06-04 17:07:49 +0000504 /* retrieve last login time from utmp */
Damien Millerdd47aa22000-06-27 11:18:27 +1000505 return (wtmp_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000506# elif defined(USE_WTMPX) && \
507 (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
andre61e67252000-06-04 17:07:49 +0000508 /* If wtmp isn't available, try wtmpx */
Damien Millerdd47aa22000-06-27 11:18:27 +1000509 return (wtmpx_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000510# else
andre61e67252000-06-04 17:07:49 +0000511 /* Give up: No means of retrieving last login time */
Damien Miller8899ed32004-09-12 15:18:55 +1000512 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000513# endif /* DISABLE_LASTLOG */
Damien Millerdd47aa22000-06-27 11:18:27 +1000514#endif /* USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000515}
516
517
518
andre2ff7b5d2000-06-03 14:57:40 +0000519/*
andre61e67252000-06-04 17:07:49 +0000520 * 'line' string utility functions
521 *
522 * These functions process the 'line' string into one of three forms:
523 *
andre2ff7b5d2000-06-03 14:57:40 +0000524 * 1. The full filename (including '/dev')
525 * 2. The stripped name (excluding '/dev')
andre61e67252000-06-04 17:07:49 +0000526 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
527 * /dev/pts/1 -> ts/1 )
andre2ff7b5d2000-06-03 14:57:40 +0000528 *
529 * Form 3 is used on some systems to identify a .tmp.? entry when
530 * attempting to remove it. Typically both addition and removal is
andre61e67252000-06-04 17:07:49 +0000531 * performed by one application - say, sshd - so as long as the choice
532 * uniquely identifies a terminal it's ok.
andre2ff7b5d2000-06-03 14:57:40 +0000533 */
534
535
Damien Miller8899ed32004-09-12 15:18:55 +1000536/*
537 * line_fullname(): add the leading '/dev/' if it doesn't exist make
538 * sure dst has enough space, if not just copy src (ugh)
539 */
andre2ff7b5d2000-06-03 14:57:40 +0000540char *
Damien Miller52c8afe2005-06-19 10:19:43 +1000541line_fullname(char *dst, const char *src, u_int dstsize)
andre61e67252000-06-04 17:07:49 +0000542{
andre2ff7b5d2000-06-03 14:57:40 +0000543 memset(dst, '\0', dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000544 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
andre2ff7b5d2000-06-03 14:57:40 +0000545 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000546 else {
Damien Miller1a132252000-06-13 21:23:17 +1000547 strlcpy(dst, "/dev/", dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000548 strlcat(dst, src, dstsize);
549 }
Damien Miller8899ed32004-09-12 15:18:55 +1000550 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000551}
552
andre61e67252000-06-04 17:07:49 +0000553/* line_stripname(): strip the leading '/dev' if it exists, return dst */
andre2ff7b5d2000-06-03 14:57:40 +0000554char *
andre61e67252000-06-04 17:07:49 +0000555line_stripname(char *dst, const char *src, int dstsize)
556{
andre2ff7b5d2000-06-03 14:57:40 +0000557 memset(dst, '\0', dstsize);
558 if (strncmp(src, "/dev/", 5) == 0)
Damien Millerf5a81472000-09-30 21:34:44 +1100559 strlcpy(dst, src + 5, dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000560 else
561 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000562 return (dst);
andre61e67252000-06-04 17:07:49 +0000563}
564
Damien Miller94cf4c82005-07-17 17:04:47 +1000565/*
Damien Miller8899ed32004-09-12 15:18:55 +1000566 * line_abbrevname(): Return the abbreviated (usually four-character)
andre61e67252000-06-04 17:07:49 +0000567 * form of the line (Just use the last <dstsize> characters of the
568 * full name.)
569 *
570 * NOTE: use strncpy because we do NOT necessarily want zero
Damien Miller8899ed32004-09-12 15:18:55 +1000571 * termination
572 */
andre2ff7b5d2000-06-03 14:57:40 +0000573char *
Kevin Stevesef4eea92001-02-05 12:42:17 +0000574line_abbrevname(char *dst, const char *src, int dstsize)
Damien Millerdd47aa22000-06-27 11:18:27 +1000575{
576 size_t len;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000577
andre2ff7b5d2000-06-03 14:57:40 +0000578 memset(dst, '\0', dstsize);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000579
Damien Miller8e81ed32000-07-01 13:17:42 +1000580 /* Always skip prefix if present */
581 if (strncmp(src, "/dev/", 5) == 0)
582 src += 5;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000583
Damien Millerf1b9d112002-04-23 23:09:19 +1000584#ifdef WITH_ABBREV_NO_TTY
585 if (strncmp(src, "tty", 3) == 0)
586 src += 3;
587#endif
588
Damien Millerdd47aa22000-06-27 11:18:27 +1000589 len = strlen(src);
590
Damien Miller8e81ed32000-07-01 13:17:42 +1000591 if (len > 0) {
592 if (((int)len - dstsize) > 0)
593 src += ((int)len - dstsize);
594
595 /* note: _don't_ change this to strlcpy */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000596 strncpy(dst, src, (size_t)dstsize);
Damien Millerdd47aa22000-06-27 11:18:27 +1000597 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000598
Damien Miller8899ed32004-09-12 15:18:55 +1000599 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000600}
601
andre2ff7b5d2000-06-03 14:57:40 +0000602/**
603 ** utmp utility functions
andre61e67252000-06-04 17:07:49 +0000604 **
605 ** These functions manipulate struct utmp, taking system differences
606 ** into account.
andre2ff7b5d2000-06-03 14:57:40 +0000607 **/
608
609#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
610
andre2ff7b5d2000-06-03 14:57:40 +0000611/* build the utmp structure */
612void
andre61e67252000-06-04 17:07:49 +0000613set_utmp_time(struct logininfo *li, struct utmp *ut)
614{
Damien Miller8899ed32004-09-12 15:18:55 +1000615# if defined(HAVE_TV_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000616 ut->ut_tv.tv_sec = li->tv_sec;
617 ut->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000618# elif defined(HAVE_TIME_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000619 ut->ut_time = li->tv_sec;
Damien Millerdd47aa22000-06-27 11:18:27 +1000620# endif
andre2ff7b5d2000-06-03 14:57:40 +0000621}
622
623void
624construct_utmp(struct logininfo *li,
andre61e67252000-06-04 17:07:49 +0000625 struct utmp *ut)
626{
Damien Miller02e16ad2003-01-03 14:42:27 +1100627# ifdef HAVE_ADDR_V6_IN_UTMP
628 struct sockaddr_in6 *sa6;
Damien Miller8899ed32004-09-12 15:18:55 +1000629# endif
630
Damien Miller348c9b72000-08-15 10:01:22 +1000631 memset(ut, '\0', sizeof(*ut));
andre6bb92372000-06-19 08:20:03 +0000632
633 /* First fill out fields used for both logins and logouts */
634
Damien Millerdd47aa22000-06-27 11:18:27 +1000635# ifdef HAVE_ID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000636 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +1000637# endif
andre2ff7b5d2000-06-03 14:57:40 +0000638
Damien Millerdd47aa22000-06-27 11:18:27 +1000639# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +0000640 /* This is done here to keep utmp constants out of struct logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000641 switch (li->type) {
642 case LTYPE_LOGIN:
643 ut->ut_type = USER_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700644#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000645 cray_set_tmpdir(ut);
646#endif
andre2ff7b5d2000-06-03 14:57:40 +0000647 break;
648 case LTYPE_LOGOUT:
649 ut->ut_type = DEAD_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700650#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000651 cray_retain_utmp(ut, li->pid);
652#endif
andre2ff7b5d2000-06-03 14:57:40 +0000653 break;
654 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000655# endif
andre6bb92372000-06-19 08:20:03 +0000656 set_utmp_time(li, ut);
andre2ff7b5d2000-06-03 14:57:40 +0000657
andre6bb92372000-06-19 08:20:03 +0000658 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +1000659
660# ifdef HAVE_PID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000661 ut->ut_pid = li->pid;
Damien Millerdd47aa22000-06-27 11:18:27 +1000662# endif
andre6bb92372000-06-19 08:20:03 +0000663
664 /* If we're logging out, leave all other fields blank */
665 if (li->type == LTYPE_LOGOUT)
Damien Miller8899ed32004-09-12 15:18:55 +1000666 return;
andre6bb92372000-06-19 08:20:03 +0000667
Damien Millerdd47aa22000-06-27 11:18:27 +1000668 /*
669 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000670 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000671 */
andre6bb92372000-06-19 08:20:03 +0000672
673 /* Use strncpy because we don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000674 strncpy(ut->ut_name, li->username,
675 MIN_SIZEOF(ut->ut_name, li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000676# ifdef HAVE_HOST_IN_UTMP
Damien Miller8899ed32004-09-12 15:18:55 +1000677 strncpy(ut->ut_host, li->hostname,
678 MIN_SIZEOF(ut->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000679# endif
680# ifdef HAVE_ADDR_IN_UTMP
andre61e67252000-06-04 17:07:49 +0000681 /* this is just a 32-bit IP address */
682 if (li->hostaddr.sa.sa_family == AF_INET)
683 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000684# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100685# ifdef HAVE_ADDR_V6_IN_UTMP
686 /* this is just a 128-bit IPv6 address */
687 if (li->hostaddr.sa.sa_family == AF_INET6) {
688 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
689 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
690 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
691 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
692 ut->ut_addr_v6[1] = 0;
693 ut->ut_addr_v6[2] = 0;
694 ut->ut_addr_v6[3] = 0;
695 }
696 }
697# endif
andre61e67252000-06-04 17:07:49 +0000698}
Damien Millerdd47aa22000-06-27 11:18:27 +1000699#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
andre61e67252000-06-04 17:07:49 +0000700
andre2ff7b5d2000-06-03 14:57:40 +0000701/**
702 ** utmpx utility functions
andre61e67252000-06-04 17:07:49 +0000703 **
704 ** These functions manipulate struct utmpx, accounting for system
705 ** variations.
andre2ff7b5d2000-06-03 14:57:40 +0000706 **/
707
708#if defined(USE_UTMPX) || defined (USE_WTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000709/* build the utmpx structure */
710void
andre61e67252000-06-04 17:07:49 +0000711set_utmpx_time(struct logininfo *li, struct utmpx *utx)
712{
Damien Miller8899ed32004-09-12 15:18:55 +1000713# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000714 utx->ut_tv.tv_sec = li->tv_sec;
715 utx->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000716# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000717 utx->ut_time = li->tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +1000718# endif
andre2ff7b5d2000-06-03 14:57:40 +0000719}
720
andre61e67252000-06-04 17:07:49 +0000721void
722construct_utmpx(struct logininfo *li, struct utmpx *utx)
723{
Damien Miller02e16ad2003-01-03 14:42:27 +1100724# ifdef HAVE_ADDR_V6_IN_UTMP
725 struct sockaddr_in6 *sa6;
726# endif
Damien Miller348c9b72000-08-15 10:01:22 +1000727 memset(utx, '\0', sizeof(*utx));
Damien Miller8899ed32004-09-12 15:18:55 +1000728
Damien Miller8e81ed32000-07-01 13:17:42 +1000729# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000730 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
Damien Miller8e81ed32000-07-01 13:17:42 +1000731# endif
andre2ff7b5d2000-06-03 14:57:40 +0000732
733 /* this is done here to keep utmp constants out of loginrec.h */
734 switch (li->type) {
735 case LTYPE_LOGIN:
736 utx->ut_type = USER_PROCESS;
737 break;
738 case LTYPE_LOGOUT:
739 utx->ut_type = DEAD_PROCESS;
740 break;
741 }
andre2ff7b5d2000-06-03 14:57:40 +0000742 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
andre2ff7b5d2000-06-03 14:57:40 +0000743 set_utmpx_time(li, utx);
andre6bb92372000-06-19 08:20:03 +0000744 utx->ut_pid = li->pid;
Damien Miller8899ed32004-09-12 15:18:55 +1000745
Tim Ricee06ae4a2002-02-24 17:56:46 -0800746 /* strncpy(): Don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000747 strncpy(utx->ut_name, li->username,
748 MIN_SIZEOF(utx->ut_name, li->username));
andre6bb92372000-06-19 08:20:03 +0000749
750 if (li->type == LTYPE_LOGOUT)
751 return;
752
Damien Millerdd47aa22000-06-27 11:18:27 +1000753 /*
754 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000755 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000756 */
andre6bb92372000-06-19 08:20:03 +0000757
Damien Millerdd47aa22000-06-27 11:18:27 +1000758# ifdef HAVE_HOST_IN_UTMPX
Damien Miller8899ed32004-09-12 15:18:55 +1000759 strncpy(utx->ut_host, li->hostname,
760 MIN_SIZEOF(utx->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000761# endif
762# ifdef HAVE_ADDR_IN_UTMPX
Damien Millerd6f204d2000-09-23 13:57:27 +1100763 /* this is just a 32-bit IP address */
764 if (li->hostaddr.sa.sa_family == AF_INET)
765 utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Damien Millerdd47aa22000-06-27 11:18:27 +1000766# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100767# ifdef HAVE_ADDR_V6_IN_UTMP
768 /* this is just a 128-bit IPv6 address */
769 if (li->hostaddr.sa.sa_family == AF_INET6) {
770 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
771 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
772 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
773 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
774 ut->ut_addr_v6[1] = 0;
775 ut->ut_addr_v6[2] = 0;
776 ut->ut_addr_v6[3] = 0;
777 }
778 }
779# endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000780# ifdef HAVE_SYSLEN_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +0000781 /* ut_syslen is the length of the utx_host string */
782 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +1000783# endif
andre61e67252000-06-04 17:07:49 +0000784}
Damien Millerdd47aa22000-06-27 11:18:27 +1000785#endif /* USE_UTMPX || USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +0000786
787/**
andre61e67252000-06-04 17:07:49 +0000788 ** Low-level utmp functions
andre2ff7b5d2000-06-03 14:57:40 +0000789 **/
790
791/* FIXME: (ATL) utmp_write_direct needs testing */
andre2ff7b5d2000-06-03 14:57:40 +0000792#ifdef USE_UTMP
793
andre2ff7b5d2000-06-03 14:57:40 +0000794/* if we can, use pututline() etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000795# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
796 defined(HAVE_PUTUTLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000797# define UTMP_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000798# endif
andre2ff7b5d2000-06-03 14:57:40 +0000799
800
801/* write a utmp entry with the system's help (pututline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000802# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000803static int
andre61e67252000-06-04 17:07:49 +0000804utmp_write_library(struct logininfo *li, struct utmp *ut)
805{
andre2ff7b5d2000-06-03 14:57:40 +0000806 setutent();
807 pututline(ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000808# ifdef HAVE_ENDUTENT
andre2ff7b5d2000-06-03 14:57:40 +0000809 endutent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000810# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000811 return (1);
andre61e67252000-06-04 17:07:49 +0000812}
Damien Millerdd47aa22000-06-27 11:18:27 +1000813# else /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000814
Damien Miller94cf4c82005-07-17 17:04:47 +1000815/*
Damien Miller8899ed32004-09-12 15:18:55 +1000816 * Write a utmp entry direct to the file
817 * This is a slightly modification of code in OpenBSD's login.c
818 */
andre2ff7b5d2000-06-03 14:57:40 +0000819static int
andre61e67252000-06-04 17:07:49 +0000820utmp_write_direct(struct logininfo *li, struct utmp *ut)
821{
andre2ff7b5d2000-06-03 14:57:40 +0000822 struct utmp old_ut;
823 register int fd;
824 int tty;
825
andre6bb92372000-06-19 08:20:03 +0000826 /* FIXME: (ATL) ttyslot() needs local implementation */
Damien Miller348c9b72000-08-15 10:01:22 +1000827
Damien Millere5192fa2000-08-29 14:30:37 +1100828#if defined(HAVE_GETTTYENT)
Damien Miller8899ed32004-09-12 15:18:55 +1000829 struct ttyent *ty;
Damien Miller348c9b72000-08-15 10:01:22 +1000830
831 tty=0;
Damien Miller348c9b72000-08-15 10:01:22 +1000832 setttyent();
Damien Miller8899ed32004-09-12 15:18:55 +1000833 while (NULL != (ty = getttyent())) {
Damien Miller348c9b72000-08-15 10:01:22 +1000834 tty++;
835 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
836 break;
837 }
838 endttyent();
839
Damien Miller8899ed32004-09-12 15:18:55 +1000840 if (NULL == ty) {
Damien Miller81409592004-08-15 19:12:52 +1000841 logit("%s: tty not found", __func__);
842 return (0);
Damien Miller348c9b72000-08-15 10:01:22 +1000843 }
844#else /* FIXME */
845
andre2ff7b5d2000-06-03 14:57:40 +0000846 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
847
Damien Millere5192fa2000-08-29 14:30:37 +1100848#endif /* HAVE_GETTTYENT */
Damien Miller348c9b72000-08-15 10:01:22 +1000849
andre2ff7b5d2000-06-03 14:57:40 +0000850 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
Damien Miller81409592004-08-15 19:12:52 +1000851 off_t pos, ret;
852
853 pos = (off_t)tty * sizeof(struct utmp);
854 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000855 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000856 return (0);
857 }
858 if (ret != pos) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000859 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Millerb0419f22004-08-23 21:53:28 +1000860 __func__, tty, UTMP_FILE);
Damien Miller81409592004-08-15 19:12:52 +1000861 return (0);
862 }
andre2ff7b5d2000-06-03 14:57:40 +0000863 /*
864 * Prevent luser from zero'ing out ut_host.
865 * If the new ut_line is empty but the old one is not
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000866 * and ut_line and ut_name match, preserve the old ut_line.
andre2ff7b5d2000-06-03 14:57:40 +0000867 */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000868 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
Damien Miller8899ed32004-09-12 15:18:55 +1000869 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
870 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
871 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0))
872 memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000873
Damien Miller81409592004-08-15 19:12:52 +1000874 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000875 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000876 return (0);
877 }
878 if (ret != pos) {
Damien Millerb0419f22004-08-23 21:53:28 +1000879 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Miller81409592004-08-15 19:12:52 +1000880 __func__, tty, UTMP_FILE);
881 return (0);
882 }
Damien Miller8899ed32004-09-12 15:18:55 +1000883 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
Damien Miller81409592004-08-15 19:12:52 +1000884 logit("%s: error writing %s: %s", __func__,
andre6bb92372000-06-19 08:20:03 +0000885 UTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +1000886 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000887
Damien Miller8899ed32004-09-12 15:18:55 +1000888 close(fd);
889 return (1);
Damien Miller53c5d462000-06-28 00:50:50 +1000890 } else {
Damien Miller8899ed32004-09-12 15:18:55 +1000891 return (0);
Damien Miller53c5d462000-06-28 00:50:50 +1000892 }
andre61e67252000-06-04 17:07:49 +0000893}
Damien Millerdd47aa22000-06-27 11:18:27 +1000894# endif /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000895
896static int
andre61e67252000-06-04 17:07:49 +0000897utmp_perform_login(struct logininfo *li)
898{
andre2ff7b5d2000-06-03 14:57:40 +0000899 struct utmp ut;
900
901 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000902# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000903 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000904 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000905 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000906 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000907# else
andre2ff7b5d2000-06-03 14:57:40 +0000908 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000909 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000910 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000911 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000912# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000913 return (1);
andre61e67252000-06-04 17:07:49 +0000914}
andre2ff7b5d2000-06-03 14:57:40 +0000915
916
917static int
andre61e67252000-06-04 17:07:49 +0000918utmp_perform_logout(struct logininfo *li)
919{
andre2ff7b5d2000-06-03 14:57:40 +0000920 struct utmp ut;
921
andre6bb92372000-06-19 08:20:03 +0000922 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000923# ifdef UTMP_USE_LIBRARY
andre6bb92372000-06-19 08:20:03 +0000924 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000925 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000926 return (0);
andre6bb92372000-06-19 08:20:03 +0000927 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000928# else
andre6bb92372000-06-19 08:20:03 +0000929 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000930 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000931 return (0);
andre6bb92372000-06-19 08:20:03 +0000932 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000933# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000934 return (1);
andre61e67252000-06-04 17:07:49 +0000935}
andre2ff7b5d2000-06-03 14:57:40 +0000936
937
938int
andre61e67252000-06-04 17:07:49 +0000939utmp_write_entry(struct logininfo *li)
940{
andre2ff7b5d2000-06-03 14:57:40 +0000941 switch(li->type) {
942 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +1000943 return (utmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +0000944
945 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +1000946 return (utmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +0000947
948 default:
Damien Miller6b0279c2004-09-12 15:25:17 +1000949 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000950 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000951 }
andre61e67252000-06-04 17:07:49 +0000952}
Damien Millerdd47aa22000-06-27 11:18:27 +1000953#endif /* USE_UTMP */
andre2ff7b5d2000-06-03 14:57:40 +0000954
955
956/**
andre61e67252000-06-04 17:07:49 +0000957 ** Low-level utmpx functions
andre2ff7b5d2000-06-03 14:57:40 +0000958 **/
959
960/* not much point if we don't want utmpx entries */
961#ifdef USE_UTMPX
962
andre2ff7b5d2000-06-03 14:57:40 +0000963/* if we have the wherewithall, use pututxline etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000964# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
965 defined(HAVE_PUTUTXLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000966# define UTMPX_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000967# endif
andre2ff7b5d2000-06-03 14:57:40 +0000968
969
970/* write a utmpx entry with the system's help (pututxline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000971# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000972static int
andre61e67252000-06-04 17:07:49 +0000973utmpx_write_library(struct logininfo *li, struct utmpx *utx)
974{
andre2ff7b5d2000-06-03 14:57:40 +0000975 setutxent();
976 pututxline(utx);
977
Damien Millerdd47aa22000-06-27 11:18:27 +1000978# ifdef HAVE_ENDUTXENT
andre2ff7b5d2000-06-03 14:57:40 +0000979 endutxent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000980# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000981 return (1);
andre61e67252000-06-04 17:07:49 +0000982}
andre2ff7b5d2000-06-03 14:57:40 +0000983
Damien Millerdd47aa22000-06-27 11:18:27 +1000984# else /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000985
986/* write a utmp entry direct to the file */
987static int
andre61e67252000-06-04 17:07:49 +0000988utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000989{
Damien Miller6b0279c2004-09-12 15:25:17 +1000990 logit("%s: not implemented!", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000991 return (0);
andre61e67252000-06-04 17:07:49 +0000992}
Damien Millerdd47aa22000-06-27 11:18:27 +1000993# endif /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000994
995static int
andre61e67252000-06-04 17:07:49 +0000996utmpx_perform_login(struct logininfo *li)
997{
andre2ff7b5d2000-06-03 14:57:40 +0000998 struct utmpx utx;
999
1000 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001001# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001002 if (!utmpx_write_library(li, &utx)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001003 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001004 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001005 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001006# else
andre2ff7b5d2000-06-03 14:57:40 +00001007 if (!utmpx_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001008 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001009 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001010 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001011# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001012 return (1);
andre61e67252000-06-04 17:07:49 +00001013}
andre2ff7b5d2000-06-03 14:57:40 +00001014
1015
1016static int
andre61e67252000-06-04 17:07:49 +00001017utmpx_perform_logout(struct logininfo *li)
1018{
andre2ff7b5d2000-06-03 14:57:40 +00001019 struct utmpx utx;
1020
Tim Ricee06ae4a2002-02-24 17:56:46 -08001021 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001022# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001023 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +10001024# endif
1025# ifdef HAVE_TYPE_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001026 utx.ut_type = DEAD_PROCESS;
Damien Millerdd47aa22000-06-27 11:18:27 +10001027# endif
andre2ff7b5d2000-06-03 14:57:40 +00001028
Damien Millerdd47aa22000-06-27 11:18:27 +10001029# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001030 utmpx_write_library(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001031# else
andre2ff7b5d2000-06-03 14:57:40 +00001032 utmpx_write_direct(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001033# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001034 return (1);
andre61e67252000-06-04 17:07:49 +00001035}
andre2ff7b5d2000-06-03 14:57:40 +00001036
andre2ff7b5d2000-06-03 14:57:40 +00001037int
andre61e67252000-06-04 17:07:49 +00001038utmpx_write_entry(struct logininfo *li)
1039{
andre2ff7b5d2000-06-03 14:57:40 +00001040 switch(li->type) {
1041 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001042 return (utmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001043 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001044 return (utmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001045 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001046 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001047 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001048 }
andre61e67252000-06-04 17:07:49 +00001049}
Damien Millerdd47aa22000-06-27 11:18:27 +10001050#endif /* USE_UTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001051
1052
1053/**
andre61e67252000-06-04 17:07:49 +00001054 ** Low-level wtmp functions
andre2ff7b5d2000-06-03 14:57:40 +00001055 **/
1056
Kevin Stevesef4eea92001-02-05 12:42:17 +00001057#ifdef USE_WTMP
andre2ff7b5d2000-06-03 14:57:40 +00001058
Damien Miller94cf4c82005-07-17 17:04:47 +10001059/*
Damien Miller8899ed32004-09-12 15:18:55 +10001060 * Write a wtmp entry direct to the end of the file
1061 * This is a slight modification of code in OpenBSD's logwtmp.c
1062 */
andre2ff7b5d2000-06-03 14:57:40 +00001063static int
andre61e67252000-06-04 17:07:49 +00001064wtmp_write(struct logininfo *li, struct utmp *ut)
1065{
andre2ff7b5d2000-06-03 14:57:40 +00001066 struct stat buf;
1067 int fd, ret = 1;
1068
1069 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001070 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001071 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001072 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001073 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001074 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001075 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
andre2ff7b5d2000-06-03 14:57:40 +00001076 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001077 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001078 WTMP_FILE, strerror(errno));
1079 ret = 0;
1080 }
Damien Miller8899ed32004-09-12 15:18:55 +10001081 close(fd);
1082 return (ret);
andre61e67252000-06-04 17:07:49 +00001083}
andre2ff7b5d2000-06-03 14:57:40 +00001084
andre2ff7b5d2000-06-03 14:57:40 +00001085static int
Damien Millerdd47aa22000-06-27 11:18:27 +10001086wtmp_perform_login(struct logininfo *li)
1087{
andre2ff7b5d2000-06-03 14:57:40 +00001088 struct utmp ut;
1089
1090 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001091 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001092}
andre2ff7b5d2000-06-03 14:57:40 +00001093
1094
1095static int
andre61e67252000-06-04 17:07:49 +00001096wtmp_perform_logout(struct logininfo *li)
1097{
andre2ff7b5d2000-06-03 14:57:40 +00001098 struct utmp ut;
1099
1100 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001101 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001102}
andre2ff7b5d2000-06-03 14:57:40 +00001103
1104
1105int
andre61e67252000-06-04 17:07:49 +00001106wtmp_write_entry(struct logininfo *li)
1107{
andre2ff7b5d2000-06-03 14:57:40 +00001108 switch(li->type) {
1109 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001110 return (wtmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001111 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001112 return (wtmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001113 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001114 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001115 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001116 }
andre61e67252000-06-04 17:07:49 +00001117}
andre2ff7b5d2000-06-03 14:57:40 +00001118
1119
Damien Miller94cf4c82005-07-17 17:04:47 +10001120/*
Damien Miller8899ed32004-09-12 15:18:55 +10001121 * Notes on fetching login data from wtmp/wtmpx
Kevin Stevesef4eea92001-02-05 12:42:17 +00001122 *
andre6bb92372000-06-19 08:20:03 +00001123 * Logouts are usually recorded with (amongst other things) a blank
1124 * username on a given tty line. However, some systems (HP-UX is one)
1125 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1126 *
1127 * Since we're only looking for logins here, we know that the username
1128 * must be set correctly. On systems that leave it in, we check for
1129 * ut_type==USER_PROCESS (indicating a login.)
1130 *
1131 * Portability: Some systems may set something other than USER_PROCESS
1132 * to indicate a login process. I don't know of any as I write. Also,
1133 * it's possible that some systems may both leave the username in
1134 * place and not have ut_type.
1135 */
1136
andre6bb92372000-06-19 08:20:03 +00001137/* return true if this wtmp entry indicates a login */
1138static int
1139wtmp_islogin(struct logininfo *li, struct utmp *ut)
1140{
Kevin Stevesef4eea92001-02-05 12:42:17 +00001141 if (strncmp(li->username, ut->ut_name,
Damien Miller8899ed32004-09-12 15:18:55 +10001142 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001143# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001144 if (ut->ut_type & USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001145 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001146# else
Damien Miller8899ed32004-09-12 15:18:55 +10001147 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001148# endif
andre6bb92372000-06-19 08:20:03 +00001149 }
Damien Miller8899ed32004-09-12 15:18:55 +10001150 return (0);
andre6bb92372000-06-19 08:20:03 +00001151}
1152
andre2ff7b5d2000-06-03 14:57:40 +00001153int
andre61e67252000-06-04 17:07:49 +00001154wtmp_get_entry(struct logininfo *li)
1155{
andre2ff7b5d2000-06-03 14:57:40 +00001156 struct stat st;
1157 struct utmp ut;
Damien Miller8899ed32004-09-12 15:18:55 +10001158 int fd, found = 0;
andre6bb92372000-06-19 08:20:03 +00001159
1160 /* Clear the time entries in our logininfo */
1161 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001162
1163 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001164 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001165 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001166 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001167 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001168 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001169 logit("%s: couldn't stat %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001170 WTMP_FILE, strerror(errno));
1171 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001172 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001173 }
andre2ff7b5d2000-06-03 14:57:40 +00001174
andre6bb92372000-06-19 08:20:03 +00001175 /* Seek to the start of the last struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001176 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
andre6bb92372000-06-19 08:20:03 +00001177 /* Looks like we've got a fresh wtmp file */
1178 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001179 return (0);
andre6bb92372000-06-19 08:20:03 +00001180 }
1181
1182 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001183 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001184 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001185 WTMP_FILE, strerror(errno));
1186 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001187 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001188 }
andre6bb92372000-06-19 08:20:03 +00001189 if ( wtmp_islogin(li, &ut) ) {
1190 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001191 /*
1192 * We've already checked for a time in struct
1193 * utmp, in login_getlast()
1194 */
Damien Millerdd47aa22000-06-27 11:18:27 +10001195# ifdef HAVE_TIME_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +00001196 li->tv_sec = ut.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001197# else
andre2ff7b5d2000-06-03 14:57:40 +00001198# if HAVE_TV_IN_UTMP
1199 li->tv_sec = ut.ut_tv.tv_sec;
1200# endif
Damien Millerdd47aa22000-06-27 11:18:27 +10001201# endif
andre6bb92372000-06-19 08:20:03 +00001202 line_fullname(li->line, ut.ut_line,
Damien Miller8899ed32004-09-12 15:18:55 +10001203 MIN_SIZEOF(li->line, ut.ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +10001204# ifdef HAVE_HOST_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001205 strlcpy(li->hostname, ut.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001206 MIN_SIZEOF(li->hostname, ut.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001207# endif
andre6bb92372000-06-19 08:20:03 +00001208 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001209 }
andre6bb92372000-06-19 08:20:03 +00001210 /* Seek back 2 x struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001211 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
andre6bb92372000-06-19 08:20:03 +00001212 /* We've found the start of the file, so quit */
Damien Miller8899ed32004-09-12 15:18:55 +10001213 close(fd);
1214 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001215 }
andre6bb92372000-06-19 08:20:03 +00001216 }
1217
1218 /* We found an entry. Tidy up and return */
1219 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001220 return (1);
andre61e67252000-06-04 17:07:49 +00001221}
Damien Millerdd47aa22000-06-27 11:18:27 +10001222# endif /* USE_WTMP */
andre2ff7b5d2000-06-03 14:57:40 +00001223
1224
1225/**
andre61e67252000-06-04 17:07:49 +00001226 ** Low-level wtmpx functions
andre2ff7b5d2000-06-03 14:57:40 +00001227 **/
1228
1229#ifdef USE_WTMPX
Damien Miller8899ed32004-09-12 15:18:55 +10001230/*
1231 * Write a wtmpx entry direct to the end of the file
1232 * This is a slight modification of code in OpenBSD's logwtmp.c
1233 */
andre2ff7b5d2000-06-03 14:57:40 +00001234static int
andre61e67252000-06-04 17:07:49 +00001235wtmpx_write(struct logininfo *li, struct utmpx *utx)
1236{
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001237#ifndef HAVE_UPDWTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001238 struct stat buf;
1239 int fd, ret = 1;
1240
1241 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001242 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001243 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001244 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001245 }
1246
Kevin Stevesef4eea92001-02-05 12:42:17 +00001247 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001248 if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
andre2ff7b5d2000-06-03 14:57:40 +00001249 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001250 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001251 WTMPX_FILE, strerror(errno));
1252 ret = 0;
1253 }
Damien Miller8899ed32004-09-12 15:18:55 +10001254 close(fd);
andre2ff7b5d2000-06-03 14:57:40 +00001255
Damien Miller8899ed32004-09-12 15:18:55 +10001256 return (ret);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001257#else
1258 updwtmpx(WTMPX_FILE, utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001259 return (1);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001260#endif
andre61e67252000-06-04 17:07:49 +00001261}
andre2ff7b5d2000-06-03 14:57:40 +00001262
1263
1264static int
andre61e67252000-06-04 17:07:49 +00001265wtmpx_perform_login(struct logininfo *li)
1266{
andre2ff7b5d2000-06-03 14:57:40 +00001267 struct utmpx utx;
1268
1269 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001270 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001271}
andre2ff7b5d2000-06-03 14:57:40 +00001272
1273
1274static int
andre61e67252000-06-04 17:07:49 +00001275wtmpx_perform_logout(struct logininfo *li)
1276{
andre2ff7b5d2000-06-03 14:57:40 +00001277 struct utmpx utx;
1278
1279 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001280 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001281}
andre2ff7b5d2000-06-03 14:57:40 +00001282
1283
1284int
andre61e67252000-06-04 17:07:49 +00001285wtmpx_write_entry(struct logininfo *li)
1286{
andre2ff7b5d2000-06-03 14:57:40 +00001287 switch(li->type) {
1288 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001289 return (wtmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001290 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001291 return (wtmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001292 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001293 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001294 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001295 }
andre61e67252000-06-04 17:07:49 +00001296}
andre2ff7b5d2000-06-03 14:57:40 +00001297
andre6bb92372000-06-19 08:20:03 +00001298/* Please see the notes above wtmp_islogin() for information about the
1299 next two functions */
1300
1301/* Return true if this wtmpx entry indicates a login */
1302static int
1303wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1304{
Damien Miller8899ed32004-09-12 15:18:55 +10001305 if (strncmp(li->username, utx->ut_name,
1306 MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001307# ifdef HAVE_TYPE_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +00001308 if (utx->ut_type == USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001309 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001310# else
Damien Miller8899ed32004-09-12 15:18:55 +10001311 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001312# endif
andre6bb92372000-06-19 08:20:03 +00001313 }
Damien Miller8899ed32004-09-12 15:18:55 +10001314 return (0);
andre6bb92372000-06-19 08:20:03 +00001315}
1316
andre2ff7b5d2000-06-03 14:57:40 +00001317
1318int
andre61e67252000-06-04 17:07:49 +00001319wtmpx_get_entry(struct logininfo *li)
1320{
andre2ff7b5d2000-06-03 14:57:40 +00001321 struct stat st;
1322 struct utmpx utx;
andre6bb92372000-06-19 08:20:03 +00001323 int fd, found=0;
1324
1325 /* Clear the time entries */
1326 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001327
1328 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001329 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001330 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001331 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001332 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001333 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001334 logit("%s: couldn't stat %s: %s", __func__,
Tim Ricecdb82942002-07-14 15:33:20 -07001335 WTMPX_FILE, strerror(errno));
andre2ff7b5d2000-06-03 14:57:40 +00001336 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001337 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001338 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001339
andre6bb92372000-06-19 08:20:03 +00001340 /* Seek to the start of the last struct utmpx */
Kevin Steves52172652001-10-02 00:29:00 +00001341 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
andre6bb92372000-06-19 08:20:03 +00001342 /* probably a newly rotated wtmpx file */
1343 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001344 return (0);
andre6bb92372000-06-19 08:20:03 +00001345 }
andre2ff7b5d2000-06-03 14:57:40 +00001346
andre6bb92372000-06-19 08:20:03 +00001347 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001348 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001349 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001350 WTMPX_FILE, strerror(errno));
1351 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001352 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001353 }
Damien Miller8899ed32004-09-12 15:18:55 +10001354 /*
Damien Miller94cf4c82005-07-17 17:04:47 +10001355 * Logouts are recorded as a blank username on a particular
Damien Miller8899ed32004-09-12 15:18:55 +10001356 * line. So, we just need to find the username in struct utmpx
1357 */
1358 if (wtmpx_islogin(li, &utx)) {
Tim Rice370e0ba2002-07-14 15:50:51 -07001359 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001360# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001361 li->tv_sec = utx.ut_tv.tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +10001362# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001363 li->tv_sec = utx.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001364# endif
Damien Miller1a132252000-06-13 21:23:17 +10001365 line_fullname(li->line, utx.ut_line, sizeof(li->line));
Damien Miller8899ed32004-09-12 15:18:55 +10001366# if defined(HAVE_HOST_IN_UTMPX)
andre6bb92372000-06-19 08:20:03 +00001367 strlcpy(li->hostname, utx.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001368 MIN_SIZEOF(li->hostname, utx.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001369# endif
andre6bb92372000-06-19 08:20:03 +00001370 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001371 }
Kevin Steves52172652001-10-02 00:29:00 +00001372 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
Damien Miller8899ed32004-09-12 15:18:55 +10001373 close(fd);
1374 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001375 }
andre6bb92372000-06-19 08:20:03 +00001376 }
1377
1378 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001379 return (1);
andre61e67252000-06-04 17:07:49 +00001380}
Damien Millerd5bf3072000-06-07 21:32:13 +10001381#endif /* USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001382
andre2ff7b5d2000-06-03 14:57:40 +00001383/**
andre61e67252000-06-04 17:07:49 +00001384 ** Low-level libutil login() functions
andre2ff7b5d2000-06-03 14:57:40 +00001385 **/
1386
1387#ifdef USE_LOGIN
andre2ff7b5d2000-06-03 14:57:40 +00001388static int
andre61e67252000-06-04 17:07:49 +00001389syslogin_perform_login(struct logininfo *li)
1390{
andre2ff7b5d2000-06-03 14:57:40 +00001391 struct utmp *ut;
1392
Damien Millerb0aae332004-09-12 15:26:00 +10001393 ut = xmalloc(sizeof(*ut));
andre2ff7b5d2000-06-03 14:57:40 +00001394 construct_utmp(li, ut);
1395 login(ut);
Damien Millerf211efc2003-03-10 11:23:06 +11001396 free(ut);
andre2ff7b5d2000-06-03 14:57:40 +00001397
Damien Miller8899ed32004-09-12 15:18:55 +10001398 return (1);
andre61e67252000-06-04 17:07:49 +00001399}
1400
andre2ff7b5d2000-06-03 14:57:40 +00001401static int
andre61e67252000-06-04 17:07:49 +00001402syslogin_perform_logout(struct logininfo *li)
1403{
Damien Millerdd47aa22000-06-27 11:18:27 +10001404# ifdef HAVE_LOGOUT
Darren Tucker4d2f3612004-04-08 10:57:05 +10001405 char line[UT_LINESIZE];
Kevin Stevesef4eea92001-02-05 12:42:17 +00001406
andre2ff7b5d2000-06-03 14:57:40 +00001407 (void)line_stripname(line, li->line, sizeof(line));
1408
Damien Miller8899ed32004-09-12 15:18:55 +10001409 if (!logout(line))
Damien Miller6b0279c2004-09-12 15:25:17 +10001410 logit("%s: logout() returned an error", __func__);
Damien Millerdd47aa22000-06-27 11:18:27 +10001411# ifdef HAVE_LOGWTMP
Damien Miller8899ed32004-09-12 15:18:55 +10001412 else
andre2ff7b5d2000-06-03 14:57:40 +00001413 logwtmp(line, "", "");
Damien Millerdd47aa22000-06-27 11:18:27 +10001414# endif
andre6bb92372000-06-19 08:20:03 +00001415 /* FIXME: (ATL - if the need arises) What to do if we have
1416 * login, but no logout? what if logout but no logwtmp? All
1417 * routines are in libutil so they should all be there,
1418 * but... */
Damien Millerdd47aa22000-06-27 11:18:27 +10001419# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001420 return (1);
andre61e67252000-06-04 17:07:49 +00001421}
andre2ff7b5d2000-06-03 14:57:40 +00001422
andre2ff7b5d2000-06-03 14:57:40 +00001423int
andre61e67252000-06-04 17:07:49 +00001424syslogin_write_entry(struct logininfo *li)
1425{
andre2ff7b5d2000-06-03 14:57:40 +00001426 switch (li->type) {
1427 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001428 return (syslogin_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001429 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001430 return (syslogin_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001431 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001432 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001433 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001434 }
andre61e67252000-06-04 17:07:49 +00001435}
Damien Millerd5bf3072000-06-07 21:32:13 +10001436#endif /* USE_LOGIN */
andre2ff7b5d2000-06-03 14:57:40 +00001437
1438/* end of file log-syslogin.c */
1439
andre2ff7b5d2000-06-03 14:57:40 +00001440/**
andre61e67252000-06-04 17:07:49 +00001441 ** Low-level lastlog functions
andre2ff7b5d2000-06-03 14:57:40 +00001442 **/
1443
1444#ifdef USE_LASTLOG
Damien Millerdd47aa22000-06-27 11:18:27 +10001445#define LL_FILE 1
1446#define LL_DIR 2
1447#define LL_OTHER 3
andre2ff7b5d2000-06-03 14:57:40 +00001448
andre2ff7b5d2000-06-03 14:57:40 +00001449static void
andre61e67252000-06-04 17:07:49 +00001450lastlog_construct(struct logininfo *li, struct lastlog *last)
1451{
andre2ff7b5d2000-06-03 14:57:40 +00001452 /* clear the structure */
Damien Miller348c9b72000-08-15 10:01:22 +10001453 memset(last, '\0', sizeof(*last));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001454
Damien Miller8899ed32004-09-12 15:18:55 +10001455 line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
andre6bb92372000-06-19 08:20:03 +00001456 strlcpy(last->ll_host, li->hostname,
1457 MIN_SIZEOF(last->ll_host, li->hostname));
andre2ff7b5d2000-06-03 14:57:40 +00001458 last->ll_time = li->tv_sec;
andre61e67252000-06-04 17:07:49 +00001459}
andre2ff7b5d2000-06-03 14:57:40 +00001460
andre2ff7b5d2000-06-03 14:57:40 +00001461static int
andre61e67252000-06-04 17:07:49 +00001462lastlog_filetype(char *filename)
1463{
andre2ff7b5d2000-06-03 14:57:40 +00001464 struct stat st;
1465
Damien Millerdd47aa22000-06-27 11:18:27 +10001466 if (stat(LASTLOG_FILE, &st) != 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001467 logit("%s: Couldn't stat %s: %s", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001468 LASTLOG_FILE, strerror(errno));
1469 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001470 }
andre2ff7b5d2000-06-03 14:57:40 +00001471 if (S_ISDIR(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001472 return (LL_DIR);
andre2ff7b5d2000-06-03 14:57:40 +00001473 else if (S_ISREG(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001474 return (LL_FILE);
andre2ff7b5d2000-06-03 14:57:40 +00001475 else
Damien Miller8899ed32004-09-12 15:18:55 +10001476 return (LL_OTHER);
andre61e67252000-06-04 17:07:49 +00001477}
andre2ff7b5d2000-06-03 14:57:40 +00001478
1479
1480/* open the file (using filemode) and seek to the login entry */
1481static int
andre61e67252000-06-04 17:07:49 +00001482lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1483{
andre2ff7b5d2000-06-03 14:57:40 +00001484 off_t offset;
1485 int type;
1486 char lastlog_file[1024];
1487
1488 type = lastlog_filetype(LASTLOG_FILE);
1489 switch (type) {
Damien Miller8899ed32004-09-12 15:18:55 +10001490 case LL_FILE:
1491 strlcpy(lastlog_file, LASTLOG_FILE,
1492 sizeof(lastlog_file));
1493 break;
1494 case LL_DIR:
1495 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1496 LASTLOG_FILE, li->username);
1497 break;
1498 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001499 logit("%s: %.100s is not a file or directory!", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001500 LASTLOG_FILE);
1501 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001502 }
andre2ff7b5d2000-06-03 14:57:40 +00001503
Damien Miller405dc602003-04-09 21:12:52 +10001504 *fd = open(lastlog_file, filemode, 0600);
Damien Miller8899ed32004-09-12 15:18:55 +10001505 if (*fd < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001506 debug("%s: Couldn't open %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001507 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001508 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001509 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001510
Damien Millere477ef62000-08-15 10:21:17 +10001511 if (type == LL_FILE) {
1512 /* find this uid's offset in the lastlog file */
Kevin Steves52172652001-10-02 00:29:00 +00001513 offset = (off_t) ((long)li->uid * sizeof(struct lastlog));
andre2ff7b5d2000-06-03 14:57:40 +00001514
Damien Miller8899ed32004-09-12 15:18:55 +10001515 if (lseek(*fd, offset, SEEK_SET) != offset) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001516 logit("%s: %s->lseek(): %s", __func__,
1517 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001518 return (0);
Damien Millere477ef62000-08-15 10:21:17 +10001519 }
andre2ff7b5d2000-06-03 14:57:40 +00001520 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001521
Damien Miller8899ed32004-09-12 15:18:55 +10001522 return (1);
andre61e67252000-06-04 17:07:49 +00001523}
andre2ff7b5d2000-06-03 14:57:40 +00001524
1525static int
andre61e67252000-06-04 17:07:49 +00001526lastlog_perform_login(struct logininfo *li)
1527{
andre2ff7b5d2000-06-03 14:57:40 +00001528 struct lastlog last;
1529 int fd;
1530
1531 /* create our struct lastlog */
1532 lastlog_construct(li, &last);
1533
Damien Miller405dc602003-04-09 21:12:52 +10001534 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
Damien Miller8899ed32004-09-12 15:18:55 +10001535 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001536
andre2ff7b5d2000-06-03 14:57:40 +00001537 /* write the entry */
Darren Tucker8661b562003-07-06 15:20:46 +10001538 if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) {
Damien Millerc1132e72000-08-18 14:08:38 +10001539 close(fd);
Damien Miller6b0279c2004-09-12 15:25:17 +10001540 logit("%s: Error writing to %s: %s", __func__,
Damien Millerc1132e72000-08-18 14:08:38 +10001541 LASTLOG_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001542 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001543 }
Damien Millerc1132e72000-08-18 14:08:38 +10001544
1545 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001546 return (1);
andre61e67252000-06-04 17:07:49 +00001547}
andre2ff7b5d2000-06-03 14:57:40 +00001548
andre2ff7b5d2000-06-03 14:57:40 +00001549int
andre61e67252000-06-04 17:07:49 +00001550lastlog_write_entry(struct logininfo *li)
1551{
andre2ff7b5d2000-06-03 14:57:40 +00001552 switch(li->type) {
1553 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001554 return (lastlog_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001555 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001556 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001557 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001558 }
andre61e67252000-06-04 17:07:49 +00001559}
andre2ff7b5d2000-06-03 14:57:40 +00001560
andre2ff7b5d2000-06-03 14:57:40 +00001561static void
andre61e67252000-06-04 17:07:49 +00001562lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1563{
andre2ff7b5d2000-06-03 14:57:40 +00001564 line_fullname(li->line, last->ll_line, sizeof(li->line));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001565 strlcpy(li->hostname, last->ll_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001566 MIN_SIZEOF(li->hostname, last->ll_host));
andre2ff7b5d2000-06-03 14:57:40 +00001567 li->tv_sec = last->ll_time;
andre61e67252000-06-04 17:07:49 +00001568}
andre2ff7b5d2000-06-03 14:57:40 +00001569
andre2ff7b5d2000-06-03 14:57:40 +00001570int
andre61e67252000-06-04 17:07:49 +00001571lastlog_get_entry(struct logininfo *li)
1572{
andre2ff7b5d2000-06-03 14:57:40 +00001573 struct lastlog last;
Damien Miller7df881d2003-01-07 16:46:58 +11001574 int fd, ret;
andre2ff7b5d2000-06-03 14:57:40 +00001575
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001576 if (!lastlog_openseek(li, &fd, O_RDONLY))
Damien Miller7df881d2003-01-07 16:46:58 +11001577 return (0);
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001578
Damien Miller7df881d2003-01-07 16:46:58 +11001579 ret = atomicio(read, fd, &last, sizeof(last));
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001580 close(fd);
1581
Damien Miller7df881d2003-01-07 16:46:58 +11001582 switch (ret) {
1583 case 0:
1584 memset(&last, '\0', sizeof(last));
1585 /* FALLTHRU */
1586 case sizeof(last):
1587 lastlog_populate_entry(li, &last);
1588 return (1);
1589 case -1:
Damien Millera8e06ce2003-11-21 23:48:55 +11001590 error("%s: Error reading from %s: %s", __func__,
Damien Miller7df881d2003-01-07 16:46:58 +11001591 LASTLOG_FILE, strerror(errno));
1592 return (0);
1593 default:
1594 error("%s: Error reading from %s: Expecting %d, got %d",
Darren Tuckerefc17472005-11-22 19:55:13 +11001595 __func__, LASTLOG_FILE, (int)sizeof(last), ret);
Damien Miller7df881d2003-01-07 16:46:58 +11001596 return (0);
1597 }
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001598
Damien Miller7df881d2003-01-07 16:46:58 +11001599 /* NOTREACHED */
1600 return (0);
andre61e67252000-06-04 17:07:49 +00001601}
Damien Millerd5bf3072000-06-07 21:32:13 +10001602#endif /* USE_LASTLOG */
Darren Tucker2fba9932005-02-02 23:30:24 +11001603
1604#ifdef USE_BTMP
1605 /*
1606 * Logs failed login attempts in _PATH_BTMP if that exists.
1607 * The most common login failure is to give password instead of username.
1608 * So the _PATH_BTMP file checked for the correct permission, so that
1609 * only root can read it.
1610 */
1611
1612void
1613record_failed_login(const char *username, const char *hostname,
1614 const char *ttyn)
1615{
1616 int fd;
1617 struct utmp ut;
1618 struct sockaddr_storage from;
Darren Tuckerefc17472005-11-22 19:55:13 +11001619 socklen_t fromlen = sizeof(from);
Darren Tucker2fba9932005-02-02 23:30:24 +11001620 struct sockaddr_in *a4;
1621 struct sockaddr_in6 *a6;
1622 time_t t;
1623 struct stat fst;
1624
1625 if (geteuid() != 0)
1626 return;
1627 if ((fd = open(_PATH_BTMP, O_WRONLY | O_APPEND)) < 0) {
1628 debug("Unable to open the btmp file %s: %s", _PATH_BTMP,
1629 strerror(errno));
1630 return;
1631 }
1632 if (fstat(fd, &fst) < 0) {
1633 logit("%s: fstat of %s failed: %s", __func__, _PATH_BTMP,
1634 strerror(errno));
1635 goto out;
1636 }
1637 if((fst.st_mode & (S_IRWXG | S_IRWXO)) || (fst.st_uid != 0)){
1638 logit("Excess permission or bad ownership on file %s",
1639 _PATH_BTMP);
1640 goto out;
1641 }
1642
1643 memset(&ut, 0, sizeof(ut));
1644 /* strncpy because we don't necessarily want nul termination */
1645 strncpy(ut.ut_user, username, sizeof(ut.ut_user));
1646 strlcpy(ut.ut_line, "ssh:notty", sizeof(ut.ut_line));
1647
1648 time(&t);
1649 ut.ut_time = t; /* ut_time is not always a time_t */
1650 ut.ut_type = LOGIN_PROCESS;
1651 ut.ut_pid = getpid();
1652
1653 /* strncpy because we don't necessarily want nul termination */
1654 strncpy(ut.ut_host, hostname, sizeof(ut.ut_host));
1655
1656 if (packet_connection_is_on_socket() &&
1657 getpeername(packet_get_connection_in(),
1658 (struct sockaddr *)&from, &fromlen) == 0) {
1659 ipv64_normalise_mapped(&from, &fromlen);
1660 if (from.ss_family == AF_INET) {
1661 a4 = (struct sockaddr_in *)&from;
1662 memcpy(&ut.ut_addr, &(a4->sin_addr),
1663 MIN_SIZEOF(ut.ut_addr, a4->sin_addr));
1664 }
1665#ifdef HAVE_ADDR_V6_IN_UTMP
1666 if (from.ss_family == AF_INET6) {
1667 a6 = (struct sockaddr_in6 *)&from;
1668 memcpy(&ut.ut_addr_v6, &(a6->sin6_addr),
1669 MIN_SIZEOF(ut.ut_addr_v6, a6->sin6_addr));
1670 }
1671#endif
1672 }
1673
1674 if (atomicio(vwrite, fd, &ut, sizeof(ut)) != sizeof(ut))
1675 error("Failed to write to %s: %s", _PATH_BTMP,
1676 strerror(errno));
1677
1678out:
1679 close(fd);
1680}
1681#endif /* USE_BTMP */