blob: a27a3ae61cfef8cce78a7aa9d3a579ddd5cc2dd0 [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>
Damien Miller8ec8c3e2006-07-10 20:35:38 +1000152#include <sys/socket.h>
153
154#include <netinet/in.h>
Damien Miller62772522006-03-15 14:01:11 +1100155
Damien Millera1738e42006-07-10 21:33:04 +1000156#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +1000157#include <pwd.h>
158
andre2ff7b5d2000-06-03 14:57:40 +0000159#include "ssh.h"
160#include "xmalloc.h"
161#include "loginrec.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000162#include "log.h"
163#include "atomicio.h"
Darren Tucker2fba9932005-02-02 23:30:24 +1100164#include "packet.h"
165#include "canohost.h"
Darren Tucker269a1ea2005-02-03 00:20:53 +1100166#include "auth.h"
Darren Tuckera39f83e2005-02-15 22:19:28 +1100167#include "buffer.h"
andre2ff7b5d2000-06-03 14:57:40 +0000168
Ben Lindstromdcca9812000-11-10 03:28:31 +0000169#ifdef HAVE_UTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000170# include <util.h>
Ben Lindstromdcca9812000-11-10 03:28:31 +0000171#endif
andre2ff7b5d2000-06-03 14:57:40 +0000172
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000173#ifdef HAVE_LIBUTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000174# include <libutil.h>
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000175#endif
176
andre2ff7b5d2000-06-03 14:57:40 +0000177/**
178 ** prototypes for helper functions in this file
179 **/
180
181#if HAVE_UTMP_H
andre2ff7b5d2000-06-03 14:57:40 +0000182void set_utmp_time(struct logininfo *li, struct utmp *ut);
183void construct_utmp(struct logininfo *li, struct utmp *ut);
184#endif
185
186#ifdef HAVE_UTMPX_H
andre2ff7b5d2000-06-03 14:57:40 +0000187void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
188void construct_utmpx(struct logininfo *li, struct utmpx *ut);
189#endif
190
191int utmp_write_entry(struct logininfo *li);
192int utmpx_write_entry(struct logininfo *li);
193int wtmp_write_entry(struct logininfo *li);
194int wtmpx_write_entry(struct logininfo *li);
195int lastlog_write_entry(struct logininfo *li);
196int syslogin_write_entry(struct logininfo *li);
197
198int getlast_entry(struct logininfo *li);
199int lastlog_get_entry(struct logininfo *li);
200int wtmp_get_entry(struct logininfo *li);
201int wtmpx_get_entry(struct logininfo *li);
202
Darren Tucker691d5232005-02-15 21:45:57 +1100203extern Buffer loginmsg;
204
andre6bb92372000-06-19 08:20:03 +0000205/* pick the shortest string */
Damien Miller8899ed32004-09-12 15:18:55 +1000206#define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2))
andre6bb92372000-06-19 08:20:03 +0000207
andre2ff7b5d2000-06-03 14:57:40 +0000208/**
209 ** platform-independent login functions
210 **/
211
Damien Miller8899ed32004-09-12 15:18:55 +1000212/*
213 * login_login(struct logininfo *) - Record a login
Kevin Stevesef4eea92001-02-05 12:42:17 +0000214 *
andre6bb92372000-06-19 08:20:03 +0000215 * Call with a pointer to a struct logininfo initialised with
216 * login_init_entry() or login_alloc_entry()
217 *
218 * Returns:
219 * >0 if successful
220 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
221 */
andre61e67252000-06-04 17:07:49 +0000222int
Damien Miller8899ed32004-09-12 15:18:55 +1000223login_login(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000224{
225 li->type = LTYPE_LOGIN;
Damien Miller8899ed32004-09-12 15:18:55 +1000226 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000227}
228
229
Damien Miller8899ed32004-09-12 15:18:55 +1000230/*
231 * login_logout(struct logininfo *) - Record a logout
andre6bb92372000-06-19 08:20:03 +0000232 *
233 * Call as with login_login()
234 *
235 * Returns:
236 * >0 if successful
237 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
238 */
andre61e67252000-06-04 17:07:49 +0000239int
240login_logout(struct logininfo *li)
241{
242 li->type = LTYPE_LOGOUT;
Damien Miller8899ed32004-09-12 15:18:55 +1000243 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000244}
245
Damien Miller8899ed32004-09-12 15:18:55 +1000246/*
247 * login_get_lastlog_time(int) - Retrieve the last login time
andre6bb92372000-06-19 08:20:03 +0000248 *
249 * Retrieve the last login time for the given uid. Will try to use the
250 * system lastlog facilities if they are available, but will fall back
251 * to looking in wtmp/wtmpx if necessary
252 *
253 * Returns:
254 * 0 on failure, or if user has never logged in
255 * Time in seconds from the epoch if successful
256 *
257 * Useful preprocessor symbols:
258 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
259 * info
260 * USE_LASTLOG: If set, indicates the presence of system lastlog
261 * facilities. If this and DISABLE_LASTLOG are not set,
262 * try to retrieve lastlog information from wtmp/wtmpx.
263 */
andre61e67252000-06-04 17:07:49 +0000264unsigned int
265login_get_lastlog_time(const int uid)
266{
267 struct logininfo li;
268
andre6bb92372000-06-19 08:20:03 +0000269 if (login_get_lastlog(&li, uid))
Damien Miller8899ed32004-09-12 15:18:55 +1000270 return (li.tv_sec);
andre6bb92372000-06-19 08:20:03 +0000271 else
Damien Miller8899ed32004-09-12 15:18:55 +1000272 return (0);
andre61e67252000-06-04 17:07:49 +0000273}
274
Damien Miller8899ed32004-09-12 15:18:55 +1000275/*
276 * login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
andre6bb92372000-06-19 08:20:03 +0000277 *
278 * Retrieve a logininfo structure populated (only partially) with
279 * information from the system lastlog data, or from wtmp/wtmpx if no
280 * system lastlog information exists.
281 *
282 * Note this routine must be given a pre-allocated logininfo.
283 *
284 * Returns:
285 * >0: A pointer to your struct logininfo if successful
286 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
andre6bb92372000-06-19 08:20:03 +0000287 */
andre61e67252000-06-04 17:07:49 +0000288struct logininfo *
289login_get_lastlog(struct logininfo *li, const int uid)
290{
andre6bb92372000-06-19 08:20:03 +0000291 struct passwd *pw;
andre6bb92372000-06-19 08:20:03 +0000292
Damien Miller348c9b72000-08-15 10:01:22 +1000293 memset(li, '\0', sizeof(*li));
andre61e67252000-06-04 17:07:49 +0000294 li->uid = uid;
andre6bb92372000-06-19 08:20:03 +0000295
Kevin Stevesef4eea92001-02-05 12:42:17 +0000296 /*
Damien Miller53c5d462000-06-28 00:50:50 +1000297 * If we don't have a 'real' lastlog, we need the username to
andre6bb92372000-06-19 08:20:03 +0000298 * reliably search wtmp(x) for the last login (see
Kevin Stevesef4eea92001-02-05 12:42:17 +0000299 * wtmp_get_entry().)
Damien Miller53c5d462000-06-28 00:50:50 +1000300 */
andre6bb92372000-06-19 08:20:03 +0000301 pw = getpwuid(uid);
Damien Millerdd47aa22000-06-27 11:18:27 +1000302 if (pw == NULL)
Damien Miller6b0279c2004-09-12 15:25:17 +1000303 fatal("%s: Cannot find account for uid %i", __func__, uid);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000304
andre98cabe02000-06-19 09:11:30 +0000305 /* No MIN_SIZEOF here - we absolutely *must not* truncate the
Damien Miller8899ed32004-09-12 15:18:55 +1000306 * username (XXX - so check for trunc!) */
Damien Millerf8af08d2000-06-27 09:40:06 +1000307 strlcpy(li->username, pw->pw_name, sizeof(li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000308
andre61e67252000-06-04 17:07:49 +0000309 if (getlast_entry(li))
Damien Miller8899ed32004-09-12 15:18:55 +1000310 return (li);
andre61e67252000-06-04 17:07:49 +0000311 else
Damien Miller8899ed32004-09-12 15:18:55 +1000312 return (NULL);
andre61e67252000-06-04 17:07:49 +0000313}
314
315
Damien Miller8899ed32004-09-12 15:18:55 +1000316/*
317 * login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
Kevin Stevesef4eea92001-02-05 12:42:17 +0000318 * a logininfo structure
319 *
andre6bb92372000-06-19 08:20:03 +0000320 * This function creates a new struct logininfo, a data structure
321 * meant to carry the information required to portably record login info.
322 *
323 * Returns a pointer to a newly created struct logininfo. If memory
324 * allocation fails, the program halts.
325 */
andre61e67252000-06-04 17:07:49 +0000326struct
327logininfo *login_alloc_entry(int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000328 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000329{
andre2ff7b5d2000-06-03 14:57:40 +0000330 struct logininfo *newli;
331
Damien Miller8899ed32004-09-12 15:18:55 +1000332 newli = xmalloc(sizeof(*newli));
333 login_init_entry(newli, pid, username, hostname, line);
334 return (newli);
andre61e67252000-06-04 17:07:49 +0000335}
andre2ff7b5d2000-06-03 14:57:40 +0000336
337
andre6bb92372000-06-19 08:20:03 +0000338/* login_free_entry(struct logininfo *) - free struct memory */
andre61e67252000-06-04 17:07:49 +0000339void
340login_free_entry(struct logininfo *li)
341{
342 xfree(li);
343}
344
andre2ff7b5d2000-06-03 14:57:40 +0000345
andre6bb92372000-06-19 08:20:03 +0000346/* login_init_entry(struct logininfo *, int, char*, char*, char*)
347 * - initialise a struct logininfo
Kevin Stevesef4eea92001-02-05 12:42:17 +0000348 *
andre6bb92372000-06-19 08:20:03 +0000349 * Populates a new struct logininfo, a data structure meant to carry
350 * the information required to portably record login info.
351 *
352 * Returns: 1
353 */
andre61e67252000-06-04 17:07:49 +0000354int
Kevin Stevesef4eea92001-02-05 12:42:17 +0000355login_init_entry(struct logininfo *li, int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000356 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000357{
Damien Millerf8af08d2000-06-27 09:40:06 +1000358 struct passwd *pw;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000359
Damien Miller348c9b72000-08-15 10:01:22 +1000360 memset(li, 0, sizeof(*li));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000361
andre61e67252000-06-04 17:07:49 +0000362 li->pid = pid;
Damien Millerf8af08d2000-06-27 09:40:06 +1000363
andre2ff7b5d2000-06-03 14:57:40 +0000364 /* set the line information */
andre61e67252000-06-04 17:07:49 +0000365 if (line)
andre2ff7b5d2000-06-03 14:57:40 +0000366 line_fullname(li->line, line, sizeof(li->line));
andre2ff7b5d2000-06-03 14:57:40 +0000367
Damien Millerf8af08d2000-06-27 09:40:06 +1000368 if (username) {
andre2ff7b5d2000-06-03 14:57:40 +0000369 strlcpy(li->username, username, sizeof(li->username));
Damien Millerf8af08d2000-06-27 09:40:06 +1000370 pw = getpwnam(li->username);
Damien Miller8899ed32004-09-12 15:18:55 +1000371 if (pw == NULL) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000372 fatal("%s: Cannot find user \"%s\"", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +1000373 li->username);
374 }
Damien Millerf8af08d2000-06-27 09:40:06 +1000375 li->uid = pw->pw_uid;
376 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000377
andre61e67252000-06-04 17:07:49 +0000378 if (hostname)
andre2ff7b5d2000-06-03 14:57:40 +0000379 strlcpy(li->hostname, hostname, sizeof(li->hostname));
Damien Millerf8af08d2000-06-27 09:40:06 +1000380
Damien Miller8899ed32004-09-12 15:18:55 +1000381 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000382}
383
Damien Miller94cf4c82005-07-17 17:04:47 +1000384/*
Damien Miller8899ed32004-09-12 15:18:55 +1000385 * login_set_current_time(struct logininfo *) - set the current time
andre6bb92372000-06-19 08:20:03 +0000386 *
387 * Set the current time in a logininfo structure. This function is
388 * meant to eliminate the need to deal with system dependencies for
389 * time handling.
390 */
andre2ff7b5d2000-06-03 14:57:40 +0000391void
andre61e67252000-06-04 17:07:49 +0000392login_set_current_time(struct logininfo *li)
393{
andre2ff7b5d2000-06-03 14:57:40 +0000394 struct timeval tv;
395
396 gettimeofday(&tv, NULL);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000397
Damien Millerf8af08d2000-06-27 09:40:06 +1000398 li->tv_sec = tv.tv_sec;
399 li->tv_usec = tv.tv_usec;
andre2ff7b5d2000-06-03 14:57:40 +0000400}
401
andre61e67252000-06-04 17:07:49 +0000402/* copy a sockaddr_* into our logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000403void
andre61e67252000-06-04 17:07:49 +0000404login_set_addr(struct logininfo *li, const struct sockaddr *sa,
Damien Miller8899ed32004-09-12 15:18:55 +1000405 const unsigned int sa_size)
andre61e67252000-06-04 17:07:49 +0000406{
407 unsigned int bufsize = sa_size;
408
409 /* make sure we don't overrun our union */
410 if (sizeof(li->hostaddr) < sa_size)
411 bufsize = sizeof(li->hostaddr);
412
Damien Miller8899ed32004-09-12 15:18:55 +1000413 memcpy(&li->hostaddr.sa, sa, bufsize);
andre2ff7b5d2000-06-03 14:57:40 +0000414}
415
andre2ff7b5d2000-06-03 14:57:40 +0000416
andre61e67252000-06-04 17:07:49 +0000417/**
418 ** login_write: Call low-level recording functions based on autoconf
419 ** results
420 **/
andre2ff7b5d2000-06-03 14:57:40 +0000421int
Damien Miller8899ed32004-09-12 15:18:55 +1000422login_write(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000423{
Damien Millerbac2d8a2000-09-05 16:13:06 +1100424#ifndef HAVE_CYGWIN
Damien Miller8899ed32004-09-12 15:18:55 +1000425 if (geteuid() != 0) {
426 logit("Attempt to write login records by non-root user (aborting)");
427 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000428 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100429#endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000430
andre2ff7b5d2000-06-03 14:57:40 +0000431 /* set the timestamp */
432 login_set_current_time(li);
433#ifdef USE_LOGIN
434 syslogin_write_entry(li);
435#endif
436#ifdef USE_LASTLOG
Damien Miller8899ed32004-09-12 15:18:55 +1000437 if (li->type == LTYPE_LOGIN)
andre2ff7b5d2000-06-03 14:57:40 +0000438 lastlog_write_entry(li);
andre2ff7b5d2000-06-03 14:57:40 +0000439#endif
440#ifdef USE_UTMP
441 utmp_write_entry(li);
442#endif
443#ifdef USE_WTMP
444 wtmp_write_entry(li);
445#endif
446#ifdef USE_UTMPX
447 utmpx_write_entry(li);
448#endif
449#ifdef USE_WTMPX
450 wtmpx_write_entry(li);
451#endif
Darren Tucker397a2f22004-08-15 00:09:11 +1000452#ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN
Damien Miller94cf4c82005-07-17 17:04:47 +1000453 if (li->type == LTYPE_LOGIN &&
Damien Millerb6f72f52005-07-17 17:26:43 +1000454 !sys_auth_record_login(li->username,li->hostname,li->line,
455 &loginmsg))
Darren Tucker397a2f22004-08-15 00:09:11 +1000456 logit("Writing login record failed for %s", li->username);
457#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100458#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100459 if (li->type == LTYPE_LOGIN)
460 audit_session_open(li->line);
461 else if (li->type == LTYPE_LOGOUT)
462 audit_session_close(li->line);
463#endif
Damien Miller8899ed32004-09-12 15:18:55 +1000464 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000465}
466
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000467#ifdef LOGIN_NEEDS_UTMPX
468int
469login_utmp_only(struct logininfo *li)
470{
Damien Millera8e06ce2003-11-21 23:48:55 +1100471 li->type = LTYPE_LOGIN;
Ben Lindstrom9197c592001-10-26 15:56:55 +0000472 login_set_current_time(li);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000473# ifdef USE_UTMP
474 utmp_write_entry(li);
475# endif
476# ifdef USE_WTMP
477 wtmp_write_entry(li);
478# endif
479# ifdef USE_UTMPX
480 utmpx_write_entry(li);
481# endif
482# ifdef USE_WTMPX
483 wtmpx_write_entry(li);
484# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000485 return (0);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000486}
487#endif
488
andre2ff7b5d2000-06-03 14:57:40 +0000489/**
andre61e67252000-06-04 17:07:49 +0000490 ** getlast_entry: Call low-level functions to retrieve the last login
491 ** time.
andre2ff7b5d2000-06-03 14:57:40 +0000492 **/
493
andre61e67252000-06-04 17:07:49 +0000494/* take the uid in li and return the last login time */
495int
496getlast_entry(struct logininfo *li)
497{
498#ifdef USE_LASTLOG
Damien Miller53c5d462000-06-28 00:50:50 +1000499 return(lastlog_get_entry(li));
Damien Millerdd47aa22000-06-27 11:18:27 +1000500#else /* !USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000501
Damien Miller8899ed32004-09-12 15:18:55 +1000502#if defined(DISABLE_LASTLOG)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000503 /* On some systems we shouldn't even try to obtain last login
andreecaabf12000-06-12 22:21:44 +0000504 * time, e.g. AIX */
Damien Miller8899ed32004-09-12 15:18:55 +1000505 return (0);
506# elif defined(USE_WTMP) && \
507 (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
andre61e67252000-06-04 17:07:49 +0000508 /* retrieve last login time from utmp */
Damien Millerdd47aa22000-06-27 11:18:27 +1000509 return (wtmp_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000510# elif defined(USE_WTMPX) && \
511 (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
andre61e67252000-06-04 17:07:49 +0000512 /* If wtmp isn't available, try wtmpx */
Damien Millerdd47aa22000-06-27 11:18:27 +1000513 return (wtmpx_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000514# else
andre61e67252000-06-04 17:07:49 +0000515 /* Give up: No means of retrieving last login time */
Damien Miller8899ed32004-09-12 15:18:55 +1000516 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000517# endif /* DISABLE_LASTLOG */
Damien Millerdd47aa22000-06-27 11:18:27 +1000518#endif /* USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000519}
520
521
522
andre2ff7b5d2000-06-03 14:57:40 +0000523/*
andre61e67252000-06-04 17:07:49 +0000524 * 'line' string utility functions
525 *
526 * These functions process the 'line' string into one of three forms:
527 *
andre2ff7b5d2000-06-03 14:57:40 +0000528 * 1. The full filename (including '/dev')
529 * 2. The stripped name (excluding '/dev')
andre61e67252000-06-04 17:07:49 +0000530 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
531 * /dev/pts/1 -> ts/1 )
andre2ff7b5d2000-06-03 14:57:40 +0000532 *
533 * Form 3 is used on some systems to identify a .tmp.? entry when
534 * attempting to remove it. Typically both addition and removal is
andre61e67252000-06-04 17:07:49 +0000535 * performed by one application - say, sshd - so as long as the choice
536 * uniquely identifies a terminal it's ok.
andre2ff7b5d2000-06-03 14:57:40 +0000537 */
538
539
Damien Miller8899ed32004-09-12 15:18:55 +1000540/*
541 * line_fullname(): add the leading '/dev/' if it doesn't exist make
542 * sure dst has enough space, if not just copy src (ugh)
543 */
andre2ff7b5d2000-06-03 14:57:40 +0000544char *
Damien Miller52c8afe2005-06-19 10:19:43 +1000545line_fullname(char *dst, const char *src, u_int dstsize)
andre61e67252000-06-04 17:07:49 +0000546{
andre2ff7b5d2000-06-03 14:57:40 +0000547 memset(dst, '\0', dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000548 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
andre2ff7b5d2000-06-03 14:57:40 +0000549 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000550 else {
Damien Miller1a132252000-06-13 21:23:17 +1000551 strlcpy(dst, "/dev/", dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000552 strlcat(dst, src, dstsize);
553 }
Damien Miller8899ed32004-09-12 15:18:55 +1000554 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000555}
556
andre61e67252000-06-04 17:07:49 +0000557/* line_stripname(): strip the leading '/dev' if it exists, return dst */
andre2ff7b5d2000-06-03 14:57:40 +0000558char *
andre61e67252000-06-04 17:07:49 +0000559line_stripname(char *dst, const char *src, int dstsize)
560{
andre2ff7b5d2000-06-03 14:57:40 +0000561 memset(dst, '\0', dstsize);
562 if (strncmp(src, "/dev/", 5) == 0)
Damien Millerf5a81472000-09-30 21:34:44 +1100563 strlcpy(dst, src + 5, dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000564 else
565 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000566 return (dst);
andre61e67252000-06-04 17:07:49 +0000567}
568
Damien Miller94cf4c82005-07-17 17:04:47 +1000569/*
Damien Miller8899ed32004-09-12 15:18:55 +1000570 * line_abbrevname(): Return the abbreviated (usually four-character)
andre61e67252000-06-04 17:07:49 +0000571 * form of the line (Just use the last <dstsize> characters of the
572 * full name.)
573 *
574 * NOTE: use strncpy because we do NOT necessarily want zero
Damien Miller8899ed32004-09-12 15:18:55 +1000575 * termination
576 */
andre2ff7b5d2000-06-03 14:57:40 +0000577char *
Kevin Stevesef4eea92001-02-05 12:42:17 +0000578line_abbrevname(char *dst, const char *src, int dstsize)
Damien Millerdd47aa22000-06-27 11:18:27 +1000579{
580 size_t len;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000581
andre2ff7b5d2000-06-03 14:57:40 +0000582 memset(dst, '\0', dstsize);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000583
Damien Miller8e81ed32000-07-01 13:17:42 +1000584 /* Always skip prefix if present */
585 if (strncmp(src, "/dev/", 5) == 0)
586 src += 5;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000587
Damien Millerf1b9d112002-04-23 23:09:19 +1000588#ifdef WITH_ABBREV_NO_TTY
589 if (strncmp(src, "tty", 3) == 0)
590 src += 3;
591#endif
592
Damien Millerdd47aa22000-06-27 11:18:27 +1000593 len = strlen(src);
594
Damien Miller8e81ed32000-07-01 13:17:42 +1000595 if (len > 0) {
596 if (((int)len - dstsize) > 0)
597 src += ((int)len - dstsize);
598
599 /* note: _don't_ change this to strlcpy */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000600 strncpy(dst, src, (size_t)dstsize);
Damien Millerdd47aa22000-06-27 11:18:27 +1000601 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000602
Damien Miller8899ed32004-09-12 15:18:55 +1000603 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000604}
605
andre2ff7b5d2000-06-03 14:57:40 +0000606/**
607 ** utmp utility functions
andre61e67252000-06-04 17:07:49 +0000608 **
609 ** These functions manipulate struct utmp, taking system differences
610 ** into account.
andre2ff7b5d2000-06-03 14:57:40 +0000611 **/
612
613#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
614
andre2ff7b5d2000-06-03 14:57:40 +0000615/* build the utmp structure */
616void
andre61e67252000-06-04 17:07:49 +0000617set_utmp_time(struct logininfo *li, struct utmp *ut)
618{
Damien Miller8899ed32004-09-12 15:18:55 +1000619# if defined(HAVE_TV_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000620 ut->ut_tv.tv_sec = li->tv_sec;
621 ut->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000622# elif defined(HAVE_TIME_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000623 ut->ut_time = li->tv_sec;
Damien Millerdd47aa22000-06-27 11:18:27 +1000624# endif
andre2ff7b5d2000-06-03 14:57:40 +0000625}
626
627void
628construct_utmp(struct logininfo *li,
andre61e67252000-06-04 17:07:49 +0000629 struct utmp *ut)
630{
Damien Miller02e16ad2003-01-03 14:42:27 +1100631# ifdef HAVE_ADDR_V6_IN_UTMP
632 struct sockaddr_in6 *sa6;
Damien Miller8899ed32004-09-12 15:18:55 +1000633# endif
634
Damien Miller348c9b72000-08-15 10:01:22 +1000635 memset(ut, '\0', sizeof(*ut));
andre6bb92372000-06-19 08:20:03 +0000636
637 /* First fill out fields used for both logins and logouts */
638
Damien Millerdd47aa22000-06-27 11:18:27 +1000639# ifdef HAVE_ID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000640 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +1000641# endif
andre2ff7b5d2000-06-03 14:57:40 +0000642
Damien Millerdd47aa22000-06-27 11:18:27 +1000643# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +0000644 /* This is done here to keep utmp constants out of struct logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000645 switch (li->type) {
646 case LTYPE_LOGIN:
647 ut->ut_type = USER_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700648#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000649 cray_set_tmpdir(ut);
650#endif
andre2ff7b5d2000-06-03 14:57:40 +0000651 break;
652 case LTYPE_LOGOUT:
653 ut->ut_type = DEAD_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700654#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000655 cray_retain_utmp(ut, li->pid);
656#endif
andre2ff7b5d2000-06-03 14:57:40 +0000657 break;
658 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000659# endif
andre6bb92372000-06-19 08:20:03 +0000660 set_utmp_time(li, ut);
andre2ff7b5d2000-06-03 14:57:40 +0000661
andre6bb92372000-06-19 08:20:03 +0000662 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +1000663
664# ifdef HAVE_PID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000665 ut->ut_pid = li->pid;
Damien Millerdd47aa22000-06-27 11:18:27 +1000666# endif
andre6bb92372000-06-19 08:20:03 +0000667
668 /* If we're logging out, leave all other fields blank */
669 if (li->type == LTYPE_LOGOUT)
Damien Miller8899ed32004-09-12 15:18:55 +1000670 return;
andre6bb92372000-06-19 08:20:03 +0000671
Damien Millerdd47aa22000-06-27 11:18:27 +1000672 /*
673 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000674 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000675 */
andre6bb92372000-06-19 08:20:03 +0000676
677 /* Use strncpy because we don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000678 strncpy(ut->ut_name, li->username,
679 MIN_SIZEOF(ut->ut_name, li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000680# ifdef HAVE_HOST_IN_UTMP
Damien Miller8899ed32004-09-12 15:18:55 +1000681 strncpy(ut->ut_host, li->hostname,
682 MIN_SIZEOF(ut->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000683# endif
684# ifdef HAVE_ADDR_IN_UTMP
andre61e67252000-06-04 17:07:49 +0000685 /* this is just a 32-bit IP address */
686 if (li->hostaddr.sa.sa_family == AF_INET)
687 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000688# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100689# ifdef HAVE_ADDR_V6_IN_UTMP
690 /* this is just a 128-bit IPv6 address */
691 if (li->hostaddr.sa.sa_family == AF_INET6) {
692 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
693 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
694 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
695 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
696 ut->ut_addr_v6[1] = 0;
697 ut->ut_addr_v6[2] = 0;
698 ut->ut_addr_v6[3] = 0;
699 }
700 }
701# endif
andre61e67252000-06-04 17:07:49 +0000702}
Damien Millerdd47aa22000-06-27 11:18:27 +1000703#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
andre61e67252000-06-04 17:07:49 +0000704
andre2ff7b5d2000-06-03 14:57:40 +0000705/**
706 ** utmpx utility functions
andre61e67252000-06-04 17:07:49 +0000707 **
708 ** These functions manipulate struct utmpx, accounting for system
709 ** variations.
andre2ff7b5d2000-06-03 14:57:40 +0000710 **/
711
712#if defined(USE_UTMPX) || defined (USE_WTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000713/* build the utmpx structure */
714void
andre61e67252000-06-04 17:07:49 +0000715set_utmpx_time(struct logininfo *li, struct utmpx *utx)
716{
Damien Miller8899ed32004-09-12 15:18:55 +1000717# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000718 utx->ut_tv.tv_sec = li->tv_sec;
719 utx->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000720# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000721 utx->ut_time = li->tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +1000722# endif
andre2ff7b5d2000-06-03 14:57:40 +0000723}
724
andre61e67252000-06-04 17:07:49 +0000725void
726construct_utmpx(struct logininfo *li, struct utmpx *utx)
727{
Damien Miller02e16ad2003-01-03 14:42:27 +1100728# ifdef HAVE_ADDR_V6_IN_UTMP
729 struct sockaddr_in6 *sa6;
730# endif
Damien Miller348c9b72000-08-15 10:01:22 +1000731 memset(utx, '\0', sizeof(*utx));
Damien Miller8899ed32004-09-12 15:18:55 +1000732
Damien Miller8e81ed32000-07-01 13:17:42 +1000733# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000734 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
Damien Miller8e81ed32000-07-01 13:17:42 +1000735# endif
andre2ff7b5d2000-06-03 14:57:40 +0000736
737 /* this is done here to keep utmp constants out of loginrec.h */
738 switch (li->type) {
739 case LTYPE_LOGIN:
740 utx->ut_type = USER_PROCESS;
741 break;
742 case LTYPE_LOGOUT:
743 utx->ut_type = DEAD_PROCESS;
744 break;
745 }
andre2ff7b5d2000-06-03 14:57:40 +0000746 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
andre2ff7b5d2000-06-03 14:57:40 +0000747 set_utmpx_time(li, utx);
andre6bb92372000-06-19 08:20:03 +0000748 utx->ut_pid = li->pid;
Damien Miller8899ed32004-09-12 15:18:55 +1000749
Tim Ricee06ae4a2002-02-24 17:56:46 -0800750 /* strncpy(): Don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000751 strncpy(utx->ut_name, li->username,
752 MIN_SIZEOF(utx->ut_name, li->username));
andre6bb92372000-06-19 08:20:03 +0000753
754 if (li->type == LTYPE_LOGOUT)
755 return;
756
Damien Millerdd47aa22000-06-27 11:18:27 +1000757 /*
758 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000759 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000760 */
andre6bb92372000-06-19 08:20:03 +0000761
Damien Millerdd47aa22000-06-27 11:18:27 +1000762# ifdef HAVE_HOST_IN_UTMPX
Damien Miller8899ed32004-09-12 15:18:55 +1000763 strncpy(utx->ut_host, li->hostname,
764 MIN_SIZEOF(utx->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000765# endif
766# ifdef HAVE_ADDR_IN_UTMPX
Damien Millerd6f204d2000-09-23 13:57:27 +1100767 /* this is just a 32-bit IP address */
768 if (li->hostaddr.sa.sa_family == AF_INET)
769 utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Damien Millerdd47aa22000-06-27 11:18:27 +1000770# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100771# ifdef HAVE_ADDR_V6_IN_UTMP
772 /* this is just a 128-bit IPv6 address */
773 if (li->hostaddr.sa.sa_family == AF_INET6) {
774 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
775 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
776 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
777 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
778 ut->ut_addr_v6[1] = 0;
779 ut->ut_addr_v6[2] = 0;
780 ut->ut_addr_v6[3] = 0;
781 }
782 }
783# endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000784# ifdef HAVE_SYSLEN_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +0000785 /* ut_syslen is the length of the utx_host string */
786 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +1000787# endif
andre61e67252000-06-04 17:07:49 +0000788}
Damien Millerdd47aa22000-06-27 11:18:27 +1000789#endif /* USE_UTMPX || USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +0000790
791/**
andre61e67252000-06-04 17:07:49 +0000792 ** Low-level utmp functions
andre2ff7b5d2000-06-03 14:57:40 +0000793 **/
794
795/* FIXME: (ATL) utmp_write_direct needs testing */
andre2ff7b5d2000-06-03 14:57:40 +0000796#ifdef USE_UTMP
797
andre2ff7b5d2000-06-03 14:57:40 +0000798/* if we can, use pututline() etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000799# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
800 defined(HAVE_PUTUTLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000801# define UTMP_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000802# endif
andre2ff7b5d2000-06-03 14:57:40 +0000803
804
805/* write a utmp entry with the system's help (pututline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000806# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000807static int
andre61e67252000-06-04 17:07:49 +0000808utmp_write_library(struct logininfo *li, struct utmp *ut)
809{
andre2ff7b5d2000-06-03 14:57:40 +0000810 setutent();
811 pututline(ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000812# ifdef HAVE_ENDUTENT
andre2ff7b5d2000-06-03 14:57:40 +0000813 endutent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000814# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000815 return (1);
andre61e67252000-06-04 17:07:49 +0000816}
Damien Millerdd47aa22000-06-27 11:18:27 +1000817# else /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000818
Damien Miller94cf4c82005-07-17 17:04:47 +1000819/*
Damien Miller8899ed32004-09-12 15:18:55 +1000820 * Write a utmp entry direct to the file
821 * This is a slightly modification of code in OpenBSD's login.c
822 */
andre2ff7b5d2000-06-03 14:57:40 +0000823static int
andre61e67252000-06-04 17:07:49 +0000824utmp_write_direct(struct logininfo *li, struct utmp *ut)
825{
andre2ff7b5d2000-06-03 14:57:40 +0000826 struct utmp old_ut;
827 register int fd;
828 int tty;
829
andre6bb92372000-06-19 08:20:03 +0000830 /* FIXME: (ATL) ttyslot() needs local implementation */
Damien Miller348c9b72000-08-15 10:01:22 +1000831
Damien Millere5192fa2000-08-29 14:30:37 +1100832#if defined(HAVE_GETTTYENT)
Damien Miller8899ed32004-09-12 15:18:55 +1000833 struct ttyent *ty;
Damien Miller348c9b72000-08-15 10:01:22 +1000834
835 tty=0;
Damien Miller348c9b72000-08-15 10:01:22 +1000836 setttyent();
Damien Miller8899ed32004-09-12 15:18:55 +1000837 while (NULL != (ty = getttyent())) {
Damien Miller348c9b72000-08-15 10:01:22 +1000838 tty++;
839 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
840 break;
841 }
842 endttyent();
843
Damien Miller8899ed32004-09-12 15:18:55 +1000844 if (NULL == ty) {
Damien Miller81409592004-08-15 19:12:52 +1000845 logit("%s: tty not found", __func__);
846 return (0);
Damien Miller348c9b72000-08-15 10:01:22 +1000847 }
848#else /* FIXME */
849
andre2ff7b5d2000-06-03 14:57:40 +0000850 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
851
Damien Millere5192fa2000-08-29 14:30:37 +1100852#endif /* HAVE_GETTTYENT */
Damien Miller348c9b72000-08-15 10:01:22 +1000853
andre2ff7b5d2000-06-03 14:57:40 +0000854 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
Damien Miller81409592004-08-15 19:12:52 +1000855 off_t pos, ret;
856
857 pos = (off_t)tty * sizeof(struct utmp);
858 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000859 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000860 return (0);
861 }
862 if (ret != pos) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000863 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Millerb0419f22004-08-23 21:53:28 +1000864 __func__, tty, UTMP_FILE);
Damien Miller81409592004-08-15 19:12:52 +1000865 return (0);
866 }
andre2ff7b5d2000-06-03 14:57:40 +0000867 /*
868 * Prevent luser from zero'ing out ut_host.
869 * If the new ut_line is empty but the old one is not
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000870 * and ut_line and ut_name match, preserve the old ut_line.
andre2ff7b5d2000-06-03 14:57:40 +0000871 */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000872 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
Damien Miller8899ed32004-09-12 15:18:55 +1000873 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
874 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
875 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0))
876 memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000877
Damien Miller81409592004-08-15 19:12:52 +1000878 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000879 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000880 return (0);
881 }
882 if (ret != pos) {
Damien Millerb0419f22004-08-23 21:53:28 +1000883 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Miller81409592004-08-15 19:12:52 +1000884 __func__, tty, UTMP_FILE);
885 return (0);
886 }
Damien Miller8899ed32004-09-12 15:18:55 +1000887 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
Damien Miller81409592004-08-15 19:12:52 +1000888 logit("%s: error writing %s: %s", __func__,
andre6bb92372000-06-19 08:20:03 +0000889 UTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +1000890 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000891
Damien Miller8899ed32004-09-12 15:18:55 +1000892 close(fd);
893 return (1);
Damien Miller53c5d462000-06-28 00:50:50 +1000894 } else {
Damien Miller8899ed32004-09-12 15:18:55 +1000895 return (0);
Damien Miller53c5d462000-06-28 00:50:50 +1000896 }
andre61e67252000-06-04 17:07:49 +0000897}
Damien Millerdd47aa22000-06-27 11:18:27 +1000898# endif /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000899
900static int
andre61e67252000-06-04 17:07:49 +0000901utmp_perform_login(struct logininfo *li)
902{
andre2ff7b5d2000-06-03 14:57:40 +0000903 struct utmp ut;
904
905 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000906# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000907 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000908 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000909 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000910 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000911# else
andre2ff7b5d2000-06-03 14:57:40 +0000912 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000913 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000914 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000915 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000916# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000917 return (1);
andre61e67252000-06-04 17:07:49 +0000918}
andre2ff7b5d2000-06-03 14:57:40 +0000919
920
921static int
andre61e67252000-06-04 17:07:49 +0000922utmp_perform_logout(struct logininfo *li)
923{
andre2ff7b5d2000-06-03 14:57:40 +0000924 struct utmp ut;
925
andre6bb92372000-06-19 08:20:03 +0000926 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000927# ifdef UTMP_USE_LIBRARY
andre6bb92372000-06-19 08:20:03 +0000928 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000929 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000930 return (0);
andre6bb92372000-06-19 08:20:03 +0000931 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000932# else
andre6bb92372000-06-19 08:20:03 +0000933 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000934 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000935 return (0);
andre6bb92372000-06-19 08:20:03 +0000936 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000937# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000938 return (1);
andre61e67252000-06-04 17:07:49 +0000939}
andre2ff7b5d2000-06-03 14:57:40 +0000940
941
942int
andre61e67252000-06-04 17:07:49 +0000943utmp_write_entry(struct logininfo *li)
944{
andre2ff7b5d2000-06-03 14:57:40 +0000945 switch(li->type) {
946 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +1000947 return (utmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +0000948
949 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +1000950 return (utmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +0000951
952 default:
Damien Miller6b0279c2004-09-12 15:25:17 +1000953 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000954 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000955 }
andre61e67252000-06-04 17:07:49 +0000956}
Damien Millerdd47aa22000-06-27 11:18:27 +1000957#endif /* USE_UTMP */
andre2ff7b5d2000-06-03 14:57:40 +0000958
959
960/**
andre61e67252000-06-04 17:07:49 +0000961 ** Low-level utmpx functions
andre2ff7b5d2000-06-03 14:57:40 +0000962 **/
963
964/* not much point if we don't want utmpx entries */
965#ifdef USE_UTMPX
966
andre2ff7b5d2000-06-03 14:57:40 +0000967/* if we have the wherewithall, use pututxline etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000968# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
969 defined(HAVE_PUTUTXLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000970# define UTMPX_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000971# endif
andre2ff7b5d2000-06-03 14:57:40 +0000972
973
974/* write a utmpx entry with the system's help (pututxline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000975# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000976static int
andre61e67252000-06-04 17:07:49 +0000977utmpx_write_library(struct logininfo *li, struct utmpx *utx)
978{
andre2ff7b5d2000-06-03 14:57:40 +0000979 setutxent();
980 pututxline(utx);
981
Damien Millerdd47aa22000-06-27 11:18:27 +1000982# ifdef HAVE_ENDUTXENT
andre2ff7b5d2000-06-03 14:57:40 +0000983 endutxent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000984# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000985 return (1);
andre61e67252000-06-04 17:07:49 +0000986}
andre2ff7b5d2000-06-03 14:57:40 +0000987
Damien Millerdd47aa22000-06-27 11:18:27 +1000988# else /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000989
990/* write a utmp entry direct to the file */
991static int
andre61e67252000-06-04 17:07:49 +0000992utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000993{
Damien Miller6b0279c2004-09-12 15:25:17 +1000994 logit("%s: not implemented!", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000995 return (0);
andre61e67252000-06-04 17:07:49 +0000996}
Damien Millerdd47aa22000-06-27 11:18:27 +1000997# endif /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000998
999static int
andre61e67252000-06-04 17:07:49 +00001000utmpx_perform_login(struct logininfo *li)
1001{
andre2ff7b5d2000-06-03 14:57:40 +00001002 struct utmpx utx;
1003
1004 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001005# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001006 if (!utmpx_write_library(li, &utx)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001007 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001008 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001009 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001010# else
andre2ff7b5d2000-06-03 14:57:40 +00001011 if (!utmpx_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001012 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001013 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001014 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001015# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001016 return (1);
andre61e67252000-06-04 17:07:49 +00001017}
andre2ff7b5d2000-06-03 14:57:40 +00001018
1019
1020static int
andre61e67252000-06-04 17:07:49 +00001021utmpx_perform_logout(struct logininfo *li)
1022{
andre2ff7b5d2000-06-03 14:57:40 +00001023 struct utmpx utx;
1024
Tim Ricee06ae4a2002-02-24 17:56:46 -08001025 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001026# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001027 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +10001028# endif
1029# ifdef HAVE_TYPE_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001030 utx.ut_type = DEAD_PROCESS;
Damien Millerdd47aa22000-06-27 11:18:27 +10001031# endif
andre2ff7b5d2000-06-03 14:57:40 +00001032
Damien Millerdd47aa22000-06-27 11:18:27 +10001033# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001034 utmpx_write_library(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001035# else
andre2ff7b5d2000-06-03 14:57:40 +00001036 utmpx_write_direct(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001037# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001038 return (1);
andre61e67252000-06-04 17:07:49 +00001039}
andre2ff7b5d2000-06-03 14:57:40 +00001040
andre2ff7b5d2000-06-03 14:57:40 +00001041int
andre61e67252000-06-04 17:07:49 +00001042utmpx_write_entry(struct logininfo *li)
1043{
andre2ff7b5d2000-06-03 14:57:40 +00001044 switch(li->type) {
1045 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001046 return (utmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001047 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001048 return (utmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001049 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001050 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001051 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001052 }
andre61e67252000-06-04 17:07:49 +00001053}
Damien Millerdd47aa22000-06-27 11:18:27 +10001054#endif /* USE_UTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001055
1056
1057/**
andre61e67252000-06-04 17:07:49 +00001058 ** Low-level wtmp functions
andre2ff7b5d2000-06-03 14:57:40 +00001059 **/
1060
Kevin Stevesef4eea92001-02-05 12:42:17 +00001061#ifdef USE_WTMP
andre2ff7b5d2000-06-03 14:57:40 +00001062
Damien Miller94cf4c82005-07-17 17:04:47 +10001063/*
Damien Miller8899ed32004-09-12 15:18:55 +10001064 * Write a wtmp entry direct to the end of the file
1065 * This is a slight modification of code in OpenBSD's logwtmp.c
1066 */
andre2ff7b5d2000-06-03 14:57:40 +00001067static int
andre61e67252000-06-04 17:07:49 +00001068wtmp_write(struct logininfo *li, struct utmp *ut)
1069{
andre2ff7b5d2000-06-03 14:57:40 +00001070 struct stat buf;
1071 int fd, ret = 1;
1072
1073 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001074 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001075 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001076 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001077 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001078 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001079 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
andre2ff7b5d2000-06-03 14:57:40 +00001080 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001081 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001082 WTMP_FILE, strerror(errno));
1083 ret = 0;
1084 }
Damien Miller8899ed32004-09-12 15:18:55 +10001085 close(fd);
1086 return (ret);
andre61e67252000-06-04 17:07:49 +00001087}
andre2ff7b5d2000-06-03 14:57:40 +00001088
andre2ff7b5d2000-06-03 14:57:40 +00001089static int
Damien Millerdd47aa22000-06-27 11:18:27 +10001090wtmp_perform_login(struct logininfo *li)
1091{
andre2ff7b5d2000-06-03 14:57:40 +00001092 struct utmp ut;
1093
1094 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001095 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001096}
andre2ff7b5d2000-06-03 14:57:40 +00001097
1098
1099static int
andre61e67252000-06-04 17:07:49 +00001100wtmp_perform_logout(struct logininfo *li)
1101{
andre2ff7b5d2000-06-03 14:57:40 +00001102 struct utmp ut;
1103
1104 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001105 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001106}
andre2ff7b5d2000-06-03 14:57:40 +00001107
1108
1109int
andre61e67252000-06-04 17:07:49 +00001110wtmp_write_entry(struct logininfo *li)
1111{
andre2ff7b5d2000-06-03 14:57:40 +00001112 switch(li->type) {
1113 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001114 return (wtmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001115 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001116 return (wtmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001117 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001118 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001119 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001120 }
andre61e67252000-06-04 17:07:49 +00001121}
andre2ff7b5d2000-06-03 14:57:40 +00001122
1123
Damien Miller94cf4c82005-07-17 17:04:47 +10001124/*
Damien Miller8899ed32004-09-12 15:18:55 +10001125 * Notes on fetching login data from wtmp/wtmpx
Kevin Stevesef4eea92001-02-05 12:42:17 +00001126 *
andre6bb92372000-06-19 08:20:03 +00001127 * Logouts are usually recorded with (amongst other things) a blank
1128 * username on a given tty line. However, some systems (HP-UX is one)
1129 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1130 *
1131 * Since we're only looking for logins here, we know that the username
1132 * must be set correctly. On systems that leave it in, we check for
1133 * ut_type==USER_PROCESS (indicating a login.)
1134 *
1135 * Portability: Some systems may set something other than USER_PROCESS
1136 * to indicate a login process. I don't know of any as I write. Also,
1137 * it's possible that some systems may both leave the username in
1138 * place and not have ut_type.
1139 */
1140
andre6bb92372000-06-19 08:20:03 +00001141/* return true if this wtmp entry indicates a login */
1142static int
1143wtmp_islogin(struct logininfo *li, struct utmp *ut)
1144{
Kevin Stevesef4eea92001-02-05 12:42:17 +00001145 if (strncmp(li->username, ut->ut_name,
Damien Miller8899ed32004-09-12 15:18:55 +10001146 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001147# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001148 if (ut->ut_type & USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001149 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001150# else
Damien Miller8899ed32004-09-12 15:18:55 +10001151 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001152# endif
andre6bb92372000-06-19 08:20:03 +00001153 }
Damien Miller8899ed32004-09-12 15:18:55 +10001154 return (0);
andre6bb92372000-06-19 08:20:03 +00001155}
1156
andre2ff7b5d2000-06-03 14:57:40 +00001157int
andre61e67252000-06-04 17:07:49 +00001158wtmp_get_entry(struct logininfo *li)
1159{
andre2ff7b5d2000-06-03 14:57:40 +00001160 struct stat st;
1161 struct utmp ut;
Damien Miller8899ed32004-09-12 15:18:55 +10001162 int fd, found = 0;
andre6bb92372000-06-19 08:20:03 +00001163
1164 /* Clear the time entries in our logininfo */
1165 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001166
1167 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001168 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001169 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001170 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001171 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001172 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001173 logit("%s: couldn't stat %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001174 WTMP_FILE, strerror(errno));
1175 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001176 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001177 }
andre2ff7b5d2000-06-03 14:57:40 +00001178
andre6bb92372000-06-19 08:20:03 +00001179 /* Seek to the start of the last struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001180 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
andre6bb92372000-06-19 08:20:03 +00001181 /* Looks like we've got a fresh wtmp file */
1182 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001183 return (0);
andre6bb92372000-06-19 08:20:03 +00001184 }
1185
1186 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001187 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001188 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001189 WTMP_FILE, strerror(errno));
1190 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001191 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001192 }
andre6bb92372000-06-19 08:20:03 +00001193 if ( wtmp_islogin(li, &ut) ) {
1194 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001195 /*
1196 * We've already checked for a time in struct
1197 * utmp, in login_getlast()
1198 */
Damien Millerdd47aa22000-06-27 11:18:27 +10001199# ifdef HAVE_TIME_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +00001200 li->tv_sec = ut.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001201# else
andre2ff7b5d2000-06-03 14:57:40 +00001202# if HAVE_TV_IN_UTMP
1203 li->tv_sec = ut.ut_tv.tv_sec;
1204# endif
Damien Millerdd47aa22000-06-27 11:18:27 +10001205# endif
andre6bb92372000-06-19 08:20:03 +00001206 line_fullname(li->line, ut.ut_line,
Damien Miller8899ed32004-09-12 15:18:55 +10001207 MIN_SIZEOF(li->line, ut.ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +10001208# ifdef HAVE_HOST_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001209 strlcpy(li->hostname, ut.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001210 MIN_SIZEOF(li->hostname, ut.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001211# endif
andre6bb92372000-06-19 08:20:03 +00001212 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001213 }
andre6bb92372000-06-19 08:20:03 +00001214 /* Seek back 2 x struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001215 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
andre6bb92372000-06-19 08:20:03 +00001216 /* We've found the start of the file, so quit */
Damien Miller8899ed32004-09-12 15:18:55 +10001217 close(fd);
1218 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001219 }
andre6bb92372000-06-19 08:20:03 +00001220 }
1221
1222 /* We found an entry. Tidy up and return */
1223 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001224 return (1);
andre61e67252000-06-04 17:07:49 +00001225}
Damien Millerdd47aa22000-06-27 11:18:27 +10001226# endif /* USE_WTMP */
andre2ff7b5d2000-06-03 14:57:40 +00001227
1228
1229/**
andre61e67252000-06-04 17:07:49 +00001230 ** Low-level wtmpx functions
andre2ff7b5d2000-06-03 14:57:40 +00001231 **/
1232
1233#ifdef USE_WTMPX
Damien Miller8899ed32004-09-12 15:18:55 +10001234/*
1235 * Write a wtmpx entry direct to the end of the file
1236 * This is a slight modification of code in OpenBSD's logwtmp.c
1237 */
andre2ff7b5d2000-06-03 14:57:40 +00001238static int
andre61e67252000-06-04 17:07:49 +00001239wtmpx_write(struct logininfo *li, struct utmpx *utx)
1240{
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001241#ifndef HAVE_UPDWTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001242 struct stat buf;
1243 int fd, ret = 1;
1244
1245 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001246 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001247 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001248 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001249 }
1250
Kevin Stevesef4eea92001-02-05 12:42:17 +00001251 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001252 if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
andre2ff7b5d2000-06-03 14:57:40 +00001253 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001254 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001255 WTMPX_FILE, strerror(errno));
1256 ret = 0;
1257 }
Damien Miller8899ed32004-09-12 15:18:55 +10001258 close(fd);
andre2ff7b5d2000-06-03 14:57:40 +00001259
Damien Miller8899ed32004-09-12 15:18:55 +10001260 return (ret);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001261#else
1262 updwtmpx(WTMPX_FILE, utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001263 return (1);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001264#endif
andre61e67252000-06-04 17:07:49 +00001265}
andre2ff7b5d2000-06-03 14:57:40 +00001266
1267
1268static int
andre61e67252000-06-04 17:07:49 +00001269wtmpx_perform_login(struct logininfo *li)
1270{
andre2ff7b5d2000-06-03 14:57:40 +00001271 struct utmpx utx;
1272
1273 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001274 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001275}
andre2ff7b5d2000-06-03 14:57:40 +00001276
1277
1278static int
andre61e67252000-06-04 17:07:49 +00001279wtmpx_perform_logout(struct logininfo *li)
1280{
andre2ff7b5d2000-06-03 14:57:40 +00001281 struct utmpx utx;
1282
1283 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001284 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001285}
andre2ff7b5d2000-06-03 14:57:40 +00001286
1287
1288int
andre61e67252000-06-04 17:07:49 +00001289wtmpx_write_entry(struct logininfo *li)
1290{
andre2ff7b5d2000-06-03 14:57:40 +00001291 switch(li->type) {
1292 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001293 return (wtmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001294 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001295 return (wtmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001296 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001297 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001298 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001299 }
andre61e67252000-06-04 17:07:49 +00001300}
andre2ff7b5d2000-06-03 14:57:40 +00001301
andre6bb92372000-06-19 08:20:03 +00001302/* Please see the notes above wtmp_islogin() for information about the
1303 next two functions */
1304
1305/* Return true if this wtmpx entry indicates a login */
1306static int
1307wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1308{
Damien Miller8899ed32004-09-12 15:18:55 +10001309 if (strncmp(li->username, utx->ut_name,
1310 MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001311# ifdef HAVE_TYPE_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +00001312 if (utx->ut_type == USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001313 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001314# else
Damien Miller8899ed32004-09-12 15:18:55 +10001315 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001316# endif
andre6bb92372000-06-19 08:20:03 +00001317 }
Damien Miller8899ed32004-09-12 15:18:55 +10001318 return (0);
andre6bb92372000-06-19 08:20:03 +00001319}
1320
andre2ff7b5d2000-06-03 14:57:40 +00001321
1322int
andre61e67252000-06-04 17:07:49 +00001323wtmpx_get_entry(struct logininfo *li)
1324{
andre2ff7b5d2000-06-03 14:57:40 +00001325 struct stat st;
1326 struct utmpx utx;
andre6bb92372000-06-19 08:20:03 +00001327 int fd, found=0;
1328
1329 /* Clear the time entries */
1330 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001331
1332 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001333 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001334 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001335 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001336 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001337 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001338 logit("%s: couldn't stat %s: %s", __func__,
Tim Ricecdb82942002-07-14 15:33:20 -07001339 WTMPX_FILE, strerror(errno));
andre2ff7b5d2000-06-03 14:57:40 +00001340 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001341 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001342 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001343
andre6bb92372000-06-19 08:20:03 +00001344 /* Seek to the start of the last struct utmpx */
Kevin Steves52172652001-10-02 00:29:00 +00001345 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
andre6bb92372000-06-19 08:20:03 +00001346 /* probably a newly rotated wtmpx file */
1347 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001348 return (0);
andre6bb92372000-06-19 08:20:03 +00001349 }
andre2ff7b5d2000-06-03 14:57:40 +00001350
andre6bb92372000-06-19 08:20:03 +00001351 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001352 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001353 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001354 WTMPX_FILE, strerror(errno));
1355 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001356 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001357 }
Damien Miller8899ed32004-09-12 15:18:55 +10001358 /*
Damien Miller94cf4c82005-07-17 17:04:47 +10001359 * Logouts are recorded as a blank username on a particular
Damien Miller8899ed32004-09-12 15:18:55 +10001360 * line. So, we just need to find the username in struct utmpx
1361 */
1362 if (wtmpx_islogin(li, &utx)) {
Tim Rice370e0ba2002-07-14 15:50:51 -07001363 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001364# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001365 li->tv_sec = utx.ut_tv.tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +10001366# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001367 li->tv_sec = utx.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001368# endif
Damien Miller1a132252000-06-13 21:23:17 +10001369 line_fullname(li->line, utx.ut_line, sizeof(li->line));
Damien Miller8899ed32004-09-12 15:18:55 +10001370# if defined(HAVE_HOST_IN_UTMPX)
andre6bb92372000-06-19 08:20:03 +00001371 strlcpy(li->hostname, utx.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001372 MIN_SIZEOF(li->hostname, utx.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001373# endif
andre6bb92372000-06-19 08:20:03 +00001374 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001375 }
Kevin Steves52172652001-10-02 00:29:00 +00001376 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
Damien Miller8899ed32004-09-12 15:18:55 +10001377 close(fd);
1378 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001379 }
andre6bb92372000-06-19 08:20:03 +00001380 }
1381
1382 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001383 return (1);
andre61e67252000-06-04 17:07:49 +00001384}
Damien Millerd5bf3072000-06-07 21:32:13 +10001385#endif /* USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001386
andre2ff7b5d2000-06-03 14:57:40 +00001387/**
andre61e67252000-06-04 17:07:49 +00001388 ** Low-level libutil login() functions
andre2ff7b5d2000-06-03 14:57:40 +00001389 **/
1390
1391#ifdef USE_LOGIN
andre2ff7b5d2000-06-03 14:57:40 +00001392static int
andre61e67252000-06-04 17:07:49 +00001393syslogin_perform_login(struct logininfo *li)
1394{
andre2ff7b5d2000-06-03 14:57:40 +00001395 struct utmp *ut;
1396
Damien Millerb0aae332004-09-12 15:26:00 +10001397 ut = xmalloc(sizeof(*ut));
andre2ff7b5d2000-06-03 14:57:40 +00001398 construct_utmp(li, ut);
1399 login(ut);
Damien Millerf211efc2003-03-10 11:23:06 +11001400 free(ut);
andre2ff7b5d2000-06-03 14:57:40 +00001401
Damien Miller8899ed32004-09-12 15:18:55 +10001402 return (1);
andre61e67252000-06-04 17:07:49 +00001403}
1404
andre2ff7b5d2000-06-03 14:57:40 +00001405static int
andre61e67252000-06-04 17:07:49 +00001406syslogin_perform_logout(struct logininfo *li)
1407{
Damien Millerdd47aa22000-06-27 11:18:27 +10001408# ifdef HAVE_LOGOUT
Darren Tucker4d2f3612004-04-08 10:57:05 +10001409 char line[UT_LINESIZE];
Kevin Stevesef4eea92001-02-05 12:42:17 +00001410
andre2ff7b5d2000-06-03 14:57:40 +00001411 (void)line_stripname(line, li->line, sizeof(line));
1412
Damien Miller8899ed32004-09-12 15:18:55 +10001413 if (!logout(line))
Damien Miller6b0279c2004-09-12 15:25:17 +10001414 logit("%s: logout() returned an error", __func__);
Damien Millerdd47aa22000-06-27 11:18:27 +10001415# ifdef HAVE_LOGWTMP
Damien Miller8899ed32004-09-12 15:18:55 +10001416 else
andre2ff7b5d2000-06-03 14:57:40 +00001417 logwtmp(line, "", "");
Damien Millerdd47aa22000-06-27 11:18:27 +10001418# endif
andre6bb92372000-06-19 08:20:03 +00001419 /* FIXME: (ATL - if the need arises) What to do if we have
1420 * login, but no logout? what if logout but no logwtmp? All
1421 * routines are in libutil so they should all be there,
1422 * but... */
Damien Millerdd47aa22000-06-27 11:18:27 +10001423# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001424 return (1);
andre61e67252000-06-04 17:07:49 +00001425}
andre2ff7b5d2000-06-03 14:57:40 +00001426
andre2ff7b5d2000-06-03 14:57:40 +00001427int
andre61e67252000-06-04 17:07:49 +00001428syslogin_write_entry(struct logininfo *li)
1429{
andre2ff7b5d2000-06-03 14:57:40 +00001430 switch (li->type) {
1431 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001432 return (syslogin_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001433 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001434 return (syslogin_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001435 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001436 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001437 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001438 }
andre61e67252000-06-04 17:07:49 +00001439}
Damien Millerd5bf3072000-06-07 21:32:13 +10001440#endif /* USE_LOGIN */
andre2ff7b5d2000-06-03 14:57:40 +00001441
1442/* end of file log-syslogin.c */
1443
andre2ff7b5d2000-06-03 14:57:40 +00001444/**
andre61e67252000-06-04 17:07:49 +00001445 ** Low-level lastlog functions
andre2ff7b5d2000-06-03 14:57:40 +00001446 **/
1447
1448#ifdef USE_LASTLOG
Damien Millerdd47aa22000-06-27 11:18:27 +10001449#define LL_FILE 1
1450#define LL_DIR 2
1451#define LL_OTHER 3
andre2ff7b5d2000-06-03 14:57:40 +00001452
andre2ff7b5d2000-06-03 14:57:40 +00001453static void
andre61e67252000-06-04 17:07:49 +00001454lastlog_construct(struct logininfo *li, struct lastlog *last)
1455{
andre2ff7b5d2000-06-03 14:57:40 +00001456 /* clear the structure */
Damien Miller348c9b72000-08-15 10:01:22 +10001457 memset(last, '\0', sizeof(*last));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001458
Damien Miller8899ed32004-09-12 15:18:55 +10001459 line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
andre6bb92372000-06-19 08:20:03 +00001460 strlcpy(last->ll_host, li->hostname,
1461 MIN_SIZEOF(last->ll_host, li->hostname));
andre2ff7b5d2000-06-03 14:57:40 +00001462 last->ll_time = li->tv_sec;
andre61e67252000-06-04 17:07:49 +00001463}
andre2ff7b5d2000-06-03 14:57:40 +00001464
andre2ff7b5d2000-06-03 14:57:40 +00001465static int
andre61e67252000-06-04 17:07:49 +00001466lastlog_filetype(char *filename)
1467{
andre2ff7b5d2000-06-03 14:57:40 +00001468 struct stat st;
1469
Damien Millerdd47aa22000-06-27 11:18:27 +10001470 if (stat(LASTLOG_FILE, &st) != 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001471 logit("%s: Couldn't stat %s: %s", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001472 LASTLOG_FILE, strerror(errno));
1473 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001474 }
andre2ff7b5d2000-06-03 14:57:40 +00001475 if (S_ISDIR(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001476 return (LL_DIR);
andre2ff7b5d2000-06-03 14:57:40 +00001477 else if (S_ISREG(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001478 return (LL_FILE);
andre2ff7b5d2000-06-03 14:57:40 +00001479 else
Damien Miller8899ed32004-09-12 15:18:55 +10001480 return (LL_OTHER);
andre61e67252000-06-04 17:07:49 +00001481}
andre2ff7b5d2000-06-03 14:57:40 +00001482
1483
1484/* open the file (using filemode) and seek to the login entry */
1485static int
andre61e67252000-06-04 17:07:49 +00001486lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1487{
andre2ff7b5d2000-06-03 14:57:40 +00001488 off_t offset;
1489 int type;
1490 char lastlog_file[1024];
1491
1492 type = lastlog_filetype(LASTLOG_FILE);
1493 switch (type) {
Damien Miller8899ed32004-09-12 15:18:55 +10001494 case LL_FILE:
1495 strlcpy(lastlog_file, LASTLOG_FILE,
1496 sizeof(lastlog_file));
1497 break;
1498 case LL_DIR:
1499 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1500 LASTLOG_FILE, li->username);
1501 break;
1502 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001503 logit("%s: %.100s is not a file or directory!", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001504 LASTLOG_FILE);
1505 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001506 }
andre2ff7b5d2000-06-03 14:57:40 +00001507
Damien Miller405dc602003-04-09 21:12:52 +10001508 *fd = open(lastlog_file, filemode, 0600);
Damien Miller8899ed32004-09-12 15:18:55 +10001509 if (*fd < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001510 debug("%s: Couldn't open %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001511 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001512 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001513 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001514
Damien Millere477ef62000-08-15 10:21:17 +10001515 if (type == LL_FILE) {
1516 /* find this uid's offset in the lastlog file */
Kevin Steves52172652001-10-02 00:29:00 +00001517 offset = (off_t) ((long)li->uid * sizeof(struct lastlog));
andre2ff7b5d2000-06-03 14:57:40 +00001518
Damien Miller8899ed32004-09-12 15:18:55 +10001519 if (lseek(*fd, offset, SEEK_SET) != offset) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001520 logit("%s: %s->lseek(): %s", __func__,
1521 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001522 return (0);
Damien Millere477ef62000-08-15 10:21:17 +10001523 }
andre2ff7b5d2000-06-03 14:57:40 +00001524 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001525
Damien Miller8899ed32004-09-12 15:18:55 +10001526 return (1);
andre61e67252000-06-04 17:07:49 +00001527}
andre2ff7b5d2000-06-03 14:57:40 +00001528
1529static int
andre61e67252000-06-04 17:07:49 +00001530lastlog_perform_login(struct logininfo *li)
1531{
andre2ff7b5d2000-06-03 14:57:40 +00001532 struct lastlog last;
1533 int fd;
1534
1535 /* create our struct lastlog */
1536 lastlog_construct(li, &last);
1537
Damien Miller405dc602003-04-09 21:12:52 +10001538 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
Damien Miller8899ed32004-09-12 15:18:55 +10001539 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001540
andre2ff7b5d2000-06-03 14:57:40 +00001541 /* write the entry */
Darren Tucker8661b562003-07-06 15:20:46 +10001542 if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) {
Damien Millerc1132e72000-08-18 14:08:38 +10001543 close(fd);
Damien Miller6b0279c2004-09-12 15:25:17 +10001544 logit("%s: Error writing to %s: %s", __func__,
Damien Millerc1132e72000-08-18 14:08:38 +10001545 LASTLOG_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001546 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001547 }
Damien Millerc1132e72000-08-18 14:08:38 +10001548
1549 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001550 return (1);
andre61e67252000-06-04 17:07:49 +00001551}
andre2ff7b5d2000-06-03 14:57:40 +00001552
andre2ff7b5d2000-06-03 14:57:40 +00001553int
andre61e67252000-06-04 17:07:49 +00001554lastlog_write_entry(struct logininfo *li)
1555{
andre2ff7b5d2000-06-03 14:57:40 +00001556 switch(li->type) {
1557 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001558 return (lastlog_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001559 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001560 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001561 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001562 }
andre61e67252000-06-04 17:07:49 +00001563}
andre2ff7b5d2000-06-03 14:57:40 +00001564
andre2ff7b5d2000-06-03 14:57:40 +00001565static void
andre61e67252000-06-04 17:07:49 +00001566lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1567{
andre2ff7b5d2000-06-03 14:57:40 +00001568 line_fullname(li->line, last->ll_line, sizeof(li->line));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001569 strlcpy(li->hostname, last->ll_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001570 MIN_SIZEOF(li->hostname, last->ll_host));
andre2ff7b5d2000-06-03 14:57:40 +00001571 li->tv_sec = last->ll_time;
andre61e67252000-06-04 17:07:49 +00001572}
andre2ff7b5d2000-06-03 14:57:40 +00001573
andre2ff7b5d2000-06-03 14:57:40 +00001574int
andre61e67252000-06-04 17:07:49 +00001575lastlog_get_entry(struct logininfo *li)
1576{
andre2ff7b5d2000-06-03 14:57:40 +00001577 struct lastlog last;
Damien Miller7df881d2003-01-07 16:46:58 +11001578 int fd, ret;
andre2ff7b5d2000-06-03 14:57:40 +00001579
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001580 if (!lastlog_openseek(li, &fd, O_RDONLY))
Damien Miller7df881d2003-01-07 16:46:58 +11001581 return (0);
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001582
Damien Miller7df881d2003-01-07 16:46:58 +11001583 ret = atomicio(read, fd, &last, sizeof(last));
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001584 close(fd);
1585
Damien Miller7df881d2003-01-07 16:46:58 +11001586 switch (ret) {
1587 case 0:
1588 memset(&last, '\0', sizeof(last));
1589 /* FALLTHRU */
1590 case sizeof(last):
1591 lastlog_populate_entry(li, &last);
1592 return (1);
1593 case -1:
Damien Millera8e06ce2003-11-21 23:48:55 +11001594 error("%s: Error reading from %s: %s", __func__,
Damien Miller7df881d2003-01-07 16:46:58 +11001595 LASTLOG_FILE, strerror(errno));
1596 return (0);
1597 default:
1598 error("%s: Error reading from %s: Expecting %d, got %d",
Darren Tuckerefc17472005-11-22 19:55:13 +11001599 __func__, LASTLOG_FILE, (int)sizeof(last), ret);
Damien Miller7df881d2003-01-07 16:46:58 +11001600 return (0);
1601 }
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001602
Damien Miller7df881d2003-01-07 16:46:58 +11001603 /* NOTREACHED */
1604 return (0);
andre61e67252000-06-04 17:07:49 +00001605}
Damien Millerd5bf3072000-06-07 21:32:13 +10001606#endif /* USE_LASTLOG */
Darren Tucker2fba9932005-02-02 23:30:24 +11001607
1608#ifdef USE_BTMP
1609 /*
1610 * Logs failed login attempts in _PATH_BTMP if that exists.
1611 * The most common login failure is to give password instead of username.
1612 * So the _PATH_BTMP file checked for the correct permission, so that
1613 * only root can read it.
1614 */
1615
1616void
1617record_failed_login(const char *username, const char *hostname,
1618 const char *ttyn)
1619{
1620 int fd;
1621 struct utmp ut;
1622 struct sockaddr_storage from;
Darren Tuckerefc17472005-11-22 19:55:13 +11001623 socklen_t fromlen = sizeof(from);
Darren Tucker2fba9932005-02-02 23:30:24 +11001624 struct sockaddr_in *a4;
1625 struct sockaddr_in6 *a6;
1626 time_t t;
1627 struct stat fst;
1628
1629 if (geteuid() != 0)
1630 return;
1631 if ((fd = open(_PATH_BTMP, O_WRONLY | O_APPEND)) < 0) {
1632 debug("Unable to open the btmp file %s: %s", _PATH_BTMP,
1633 strerror(errno));
1634 return;
1635 }
1636 if (fstat(fd, &fst) < 0) {
1637 logit("%s: fstat of %s failed: %s", __func__, _PATH_BTMP,
1638 strerror(errno));
1639 goto out;
1640 }
1641 if((fst.st_mode & (S_IRWXG | S_IRWXO)) || (fst.st_uid != 0)){
1642 logit("Excess permission or bad ownership on file %s",
1643 _PATH_BTMP);
1644 goto out;
1645 }
1646
1647 memset(&ut, 0, sizeof(ut));
1648 /* strncpy because we don't necessarily want nul termination */
1649 strncpy(ut.ut_user, username, sizeof(ut.ut_user));
1650 strlcpy(ut.ut_line, "ssh:notty", sizeof(ut.ut_line));
1651
1652 time(&t);
1653 ut.ut_time = t; /* ut_time is not always a time_t */
1654 ut.ut_type = LOGIN_PROCESS;
1655 ut.ut_pid = getpid();
1656
1657 /* strncpy because we don't necessarily want nul termination */
1658 strncpy(ut.ut_host, hostname, sizeof(ut.ut_host));
1659
1660 if (packet_connection_is_on_socket() &&
1661 getpeername(packet_get_connection_in(),
1662 (struct sockaddr *)&from, &fromlen) == 0) {
1663 ipv64_normalise_mapped(&from, &fromlen);
1664 if (from.ss_family == AF_INET) {
1665 a4 = (struct sockaddr_in *)&from;
1666 memcpy(&ut.ut_addr, &(a4->sin_addr),
1667 MIN_SIZEOF(ut.ut_addr, a4->sin_addr));
1668 }
1669#ifdef HAVE_ADDR_V6_IN_UTMP
1670 if (from.ss_family == AF_INET6) {
1671 a6 = (struct sockaddr_in6 *)&from;
1672 memcpy(&ut.ut_addr_v6, &(a6->sin6_addr),
1673 MIN_SIZEOF(ut.ut_addr_v6, a6->sin6_addr));
1674 }
1675#endif
1676 }
1677
1678 if (atomicio(vwrite, fd, &ut, sizeof(ut)) != sizeof(ut))
1679 error("Failed to write to %s: %s", _PATH_BTMP,
1680 strerror(errno));
1681
1682out:
1683 close(fd);
1684}
1685#endif /* USE_BTMP */