blob: 955d42e8f4c5887145c6232ab2f1b450e748ce3b [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
Darren Tucker2c1a02a2006-07-12 22:40:50 +1000156#include <errno.h>
Damien Millera1738e42006-07-10 21:33:04 +1000157#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +1000158#include <pwd.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +1000159#include <string.h>
160#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +1000161
andre2ff7b5d2000-06-03 14:57:40 +0000162#include "ssh.h"
163#include "xmalloc.h"
164#include "loginrec.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000165#include "log.h"
166#include "atomicio.h"
Darren Tucker2fba9932005-02-02 23:30:24 +1100167#include "packet.h"
168#include "canohost.h"
Darren Tucker269a1ea2005-02-03 00:20:53 +1100169#include "auth.h"
Darren Tuckera39f83e2005-02-15 22:19:28 +1100170#include "buffer.h"
andre2ff7b5d2000-06-03 14:57:40 +0000171
Ben Lindstromdcca9812000-11-10 03:28:31 +0000172#ifdef HAVE_UTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000173# include <util.h>
Ben Lindstromdcca9812000-11-10 03:28:31 +0000174#endif
andre2ff7b5d2000-06-03 14:57:40 +0000175
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000176#ifdef HAVE_LIBUTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000177# include <libutil.h>
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000178#endif
179
andre2ff7b5d2000-06-03 14:57:40 +0000180/**
181 ** prototypes for helper functions in this file
182 **/
183
184#if HAVE_UTMP_H
andre2ff7b5d2000-06-03 14:57:40 +0000185void set_utmp_time(struct logininfo *li, struct utmp *ut);
186void construct_utmp(struct logininfo *li, struct utmp *ut);
187#endif
188
189#ifdef HAVE_UTMPX_H
andre2ff7b5d2000-06-03 14:57:40 +0000190void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
191void construct_utmpx(struct logininfo *li, struct utmpx *ut);
192#endif
193
194int utmp_write_entry(struct logininfo *li);
195int utmpx_write_entry(struct logininfo *li);
196int wtmp_write_entry(struct logininfo *li);
197int wtmpx_write_entry(struct logininfo *li);
198int lastlog_write_entry(struct logininfo *li);
199int syslogin_write_entry(struct logininfo *li);
200
201int getlast_entry(struct logininfo *li);
202int lastlog_get_entry(struct logininfo *li);
203int wtmp_get_entry(struct logininfo *li);
204int wtmpx_get_entry(struct logininfo *li);
205
Darren Tucker691d5232005-02-15 21:45:57 +1100206extern Buffer loginmsg;
207
andre6bb92372000-06-19 08:20:03 +0000208/* pick the shortest string */
Damien Miller8899ed32004-09-12 15:18:55 +1000209#define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2))
andre6bb92372000-06-19 08:20:03 +0000210
andre2ff7b5d2000-06-03 14:57:40 +0000211/**
212 ** platform-independent login functions
213 **/
214
Damien Miller8899ed32004-09-12 15:18:55 +1000215/*
216 * login_login(struct logininfo *) - Record a login
Kevin Stevesef4eea92001-02-05 12:42:17 +0000217 *
andre6bb92372000-06-19 08:20:03 +0000218 * Call with a pointer to a struct logininfo initialised with
219 * login_init_entry() or login_alloc_entry()
220 *
221 * Returns:
222 * >0 if successful
223 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
224 */
andre61e67252000-06-04 17:07:49 +0000225int
Damien Miller8899ed32004-09-12 15:18:55 +1000226login_login(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000227{
228 li->type = LTYPE_LOGIN;
Damien Miller8899ed32004-09-12 15:18:55 +1000229 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000230}
231
232
Damien Miller8899ed32004-09-12 15:18:55 +1000233/*
234 * login_logout(struct logininfo *) - Record a logout
andre6bb92372000-06-19 08:20:03 +0000235 *
236 * Call as with login_login()
237 *
238 * Returns:
239 * >0 if successful
240 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
241 */
andre61e67252000-06-04 17:07:49 +0000242int
243login_logout(struct logininfo *li)
244{
245 li->type = LTYPE_LOGOUT;
Damien Miller8899ed32004-09-12 15:18:55 +1000246 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000247}
248
Damien Miller8899ed32004-09-12 15:18:55 +1000249/*
250 * login_get_lastlog_time(int) - Retrieve the last login time
andre6bb92372000-06-19 08:20:03 +0000251 *
252 * Retrieve the last login time for the given uid. Will try to use the
253 * system lastlog facilities if they are available, but will fall back
254 * to looking in wtmp/wtmpx if necessary
255 *
256 * Returns:
257 * 0 on failure, or if user has never logged in
258 * Time in seconds from the epoch if successful
259 *
260 * Useful preprocessor symbols:
261 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
262 * info
263 * USE_LASTLOG: If set, indicates the presence of system lastlog
264 * facilities. If this and DISABLE_LASTLOG are not set,
265 * try to retrieve lastlog information from wtmp/wtmpx.
266 */
andre61e67252000-06-04 17:07:49 +0000267unsigned int
268login_get_lastlog_time(const int uid)
269{
270 struct logininfo li;
271
andre6bb92372000-06-19 08:20:03 +0000272 if (login_get_lastlog(&li, uid))
Damien Miller8899ed32004-09-12 15:18:55 +1000273 return (li.tv_sec);
andre6bb92372000-06-19 08:20:03 +0000274 else
Damien Miller8899ed32004-09-12 15:18:55 +1000275 return (0);
andre61e67252000-06-04 17:07:49 +0000276}
277
Damien Miller8899ed32004-09-12 15:18:55 +1000278/*
279 * login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
andre6bb92372000-06-19 08:20:03 +0000280 *
281 * Retrieve a logininfo structure populated (only partially) with
282 * information from the system lastlog data, or from wtmp/wtmpx if no
283 * system lastlog information exists.
284 *
285 * Note this routine must be given a pre-allocated logininfo.
286 *
287 * Returns:
288 * >0: A pointer to your struct logininfo if successful
289 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
andre6bb92372000-06-19 08:20:03 +0000290 */
andre61e67252000-06-04 17:07:49 +0000291struct logininfo *
292login_get_lastlog(struct logininfo *li, const int uid)
293{
andre6bb92372000-06-19 08:20:03 +0000294 struct passwd *pw;
andre6bb92372000-06-19 08:20:03 +0000295
Damien Miller348c9b72000-08-15 10:01:22 +1000296 memset(li, '\0', sizeof(*li));
andre61e67252000-06-04 17:07:49 +0000297 li->uid = uid;
andre6bb92372000-06-19 08:20:03 +0000298
Kevin Stevesef4eea92001-02-05 12:42:17 +0000299 /*
Damien Miller53c5d462000-06-28 00:50:50 +1000300 * If we don't have a 'real' lastlog, we need the username to
andre6bb92372000-06-19 08:20:03 +0000301 * reliably search wtmp(x) for the last login (see
Kevin Stevesef4eea92001-02-05 12:42:17 +0000302 * wtmp_get_entry().)
Damien Miller53c5d462000-06-28 00:50:50 +1000303 */
andre6bb92372000-06-19 08:20:03 +0000304 pw = getpwuid(uid);
Damien Millerdd47aa22000-06-27 11:18:27 +1000305 if (pw == NULL)
Damien Miller6b0279c2004-09-12 15:25:17 +1000306 fatal("%s: Cannot find account for uid %i", __func__, uid);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000307
andre98cabe02000-06-19 09:11:30 +0000308 /* No MIN_SIZEOF here - we absolutely *must not* truncate the
Damien Miller8899ed32004-09-12 15:18:55 +1000309 * username (XXX - so check for trunc!) */
Damien Millerf8af08d2000-06-27 09:40:06 +1000310 strlcpy(li->username, pw->pw_name, sizeof(li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000311
andre61e67252000-06-04 17:07:49 +0000312 if (getlast_entry(li))
Damien Miller8899ed32004-09-12 15:18:55 +1000313 return (li);
andre61e67252000-06-04 17:07:49 +0000314 else
Damien Miller8899ed32004-09-12 15:18:55 +1000315 return (NULL);
andre61e67252000-06-04 17:07:49 +0000316}
317
318
Damien Miller8899ed32004-09-12 15:18:55 +1000319/*
320 * login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
Kevin Stevesef4eea92001-02-05 12:42:17 +0000321 * a logininfo structure
322 *
andre6bb92372000-06-19 08:20:03 +0000323 * This function creates a new struct logininfo, a data structure
324 * meant to carry the information required to portably record login info.
325 *
326 * Returns a pointer to a newly created struct logininfo. If memory
327 * allocation fails, the program halts.
328 */
andre61e67252000-06-04 17:07:49 +0000329struct
330logininfo *login_alloc_entry(int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000331 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000332{
andre2ff7b5d2000-06-03 14:57:40 +0000333 struct logininfo *newli;
334
Damien Miller8899ed32004-09-12 15:18:55 +1000335 newli = xmalloc(sizeof(*newli));
336 login_init_entry(newli, pid, username, hostname, line);
337 return (newli);
andre61e67252000-06-04 17:07:49 +0000338}
andre2ff7b5d2000-06-03 14:57:40 +0000339
340
andre6bb92372000-06-19 08:20:03 +0000341/* login_free_entry(struct logininfo *) - free struct memory */
andre61e67252000-06-04 17:07:49 +0000342void
343login_free_entry(struct logininfo *li)
344{
345 xfree(li);
346}
347
andre2ff7b5d2000-06-03 14:57:40 +0000348
andre6bb92372000-06-19 08:20:03 +0000349/* login_init_entry(struct logininfo *, int, char*, char*, char*)
350 * - initialise a struct logininfo
Kevin Stevesef4eea92001-02-05 12:42:17 +0000351 *
andre6bb92372000-06-19 08:20:03 +0000352 * Populates a new struct logininfo, a data structure meant to carry
353 * the information required to portably record login info.
354 *
355 * Returns: 1
356 */
andre61e67252000-06-04 17:07:49 +0000357int
Kevin Stevesef4eea92001-02-05 12:42:17 +0000358login_init_entry(struct logininfo *li, int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000359 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000360{
Damien Millerf8af08d2000-06-27 09:40:06 +1000361 struct passwd *pw;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000362
Damien Miller348c9b72000-08-15 10:01:22 +1000363 memset(li, 0, sizeof(*li));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000364
andre61e67252000-06-04 17:07:49 +0000365 li->pid = pid;
Damien Millerf8af08d2000-06-27 09:40:06 +1000366
andre2ff7b5d2000-06-03 14:57:40 +0000367 /* set the line information */
andre61e67252000-06-04 17:07:49 +0000368 if (line)
andre2ff7b5d2000-06-03 14:57:40 +0000369 line_fullname(li->line, line, sizeof(li->line));
andre2ff7b5d2000-06-03 14:57:40 +0000370
Damien Millerf8af08d2000-06-27 09:40:06 +1000371 if (username) {
andre2ff7b5d2000-06-03 14:57:40 +0000372 strlcpy(li->username, username, sizeof(li->username));
Damien Millerf8af08d2000-06-27 09:40:06 +1000373 pw = getpwnam(li->username);
Damien Miller8899ed32004-09-12 15:18:55 +1000374 if (pw == NULL) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000375 fatal("%s: Cannot find user \"%s\"", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +1000376 li->username);
377 }
Damien Millerf8af08d2000-06-27 09:40:06 +1000378 li->uid = pw->pw_uid;
379 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000380
andre61e67252000-06-04 17:07:49 +0000381 if (hostname)
andre2ff7b5d2000-06-03 14:57:40 +0000382 strlcpy(li->hostname, hostname, sizeof(li->hostname));
Damien Millerf8af08d2000-06-27 09:40:06 +1000383
Damien Miller8899ed32004-09-12 15:18:55 +1000384 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000385}
386
Damien Miller94cf4c82005-07-17 17:04:47 +1000387/*
Damien Miller8899ed32004-09-12 15:18:55 +1000388 * login_set_current_time(struct logininfo *) - set the current time
andre6bb92372000-06-19 08:20:03 +0000389 *
390 * Set the current time in a logininfo structure. This function is
391 * meant to eliminate the need to deal with system dependencies for
392 * time handling.
393 */
andre2ff7b5d2000-06-03 14:57:40 +0000394void
andre61e67252000-06-04 17:07:49 +0000395login_set_current_time(struct logininfo *li)
396{
andre2ff7b5d2000-06-03 14:57:40 +0000397 struct timeval tv;
398
399 gettimeofday(&tv, NULL);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000400
Damien Millerf8af08d2000-06-27 09:40:06 +1000401 li->tv_sec = tv.tv_sec;
402 li->tv_usec = tv.tv_usec;
andre2ff7b5d2000-06-03 14:57:40 +0000403}
404
andre61e67252000-06-04 17:07:49 +0000405/* copy a sockaddr_* into our logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000406void
andre61e67252000-06-04 17:07:49 +0000407login_set_addr(struct logininfo *li, const struct sockaddr *sa,
Damien Miller8899ed32004-09-12 15:18:55 +1000408 const unsigned int sa_size)
andre61e67252000-06-04 17:07:49 +0000409{
410 unsigned int bufsize = sa_size;
411
412 /* make sure we don't overrun our union */
413 if (sizeof(li->hostaddr) < sa_size)
414 bufsize = sizeof(li->hostaddr);
415
Damien Miller8899ed32004-09-12 15:18:55 +1000416 memcpy(&li->hostaddr.sa, sa, bufsize);
andre2ff7b5d2000-06-03 14:57:40 +0000417}
418
andre2ff7b5d2000-06-03 14:57:40 +0000419
andre61e67252000-06-04 17:07:49 +0000420/**
421 ** login_write: Call low-level recording functions based on autoconf
422 ** results
423 **/
andre2ff7b5d2000-06-03 14:57:40 +0000424int
Damien Miller8899ed32004-09-12 15:18:55 +1000425login_write(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000426{
Damien Millerbac2d8a2000-09-05 16:13:06 +1100427#ifndef HAVE_CYGWIN
Damien Miller8899ed32004-09-12 15:18:55 +1000428 if (geteuid() != 0) {
429 logit("Attempt to write login records by non-root user (aborting)");
430 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000431 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100432#endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000433
andre2ff7b5d2000-06-03 14:57:40 +0000434 /* set the timestamp */
435 login_set_current_time(li);
436#ifdef USE_LOGIN
437 syslogin_write_entry(li);
438#endif
439#ifdef USE_LASTLOG
Damien Miller8899ed32004-09-12 15:18:55 +1000440 if (li->type == LTYPE_LOGIN)
andre2ff7b5d2000-06-03 14:57:40 +0000441 lastlog_write_entry(li);
andre2ff7b5d2000-06-03 14:57:40 +0000442#endif
443#ifdef USE_UTMP
444 utmp_write_entry(li);
445#endif
446#ifdef USE_WTMP
447 wtmp_write_entry(li);
448#endif
449#ifdef USE_UTMPX
450 utmpx_write_entry(li);
451#endif
452#ifdef USE_WTMPX
453 wtmpx_write_entry(li);
454#endif
Darren Tucker397a2f22004-08-15 00:09:11 +1000455#ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN
Damien Miller94cf4c82005-07-17 17:04:47 +1000456 if (li->type == LTYPE_LOGIN &&
Damien Millerb6f72f52005-07-17 17:26:43 +1000457 !sys_auth_record_login(li->username,li->hostname,li->line,
458 &loginmsg))
Darren Tucker397a2f22004-08-15 00:09:11 +1000459 logit("Writing login record failed for %s", li->username);
460#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100461#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100462 if (li->type == LTYPE_LOGIN)
463 audit_session_open(li->line);
464 else if (li->type == LTYPE_LOGOUT)
465 audit_session_close(li->line);
466#endif
Damien Miller8899ed32004-09-12 15:18:55 +1000467 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000468}
469
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000470#ifdef LOGIN_NEEDS_UTMPX
471int
472login_utmp_only(struct logininfo *li)
473{
Damien Millera8e06ce2003-11-21 23:48:55 +1100474 li->type = LTYPE_LOGIN;
Ben Lindstrom9197c592001-10-26 15:56:55 +0000475 login_set_current_time(li);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000476# ifdef USE_UTMP
477 utmp_write_entry(li);
478# endif
479# ifdef USE_WTMP
480 wtmp_write_entry(li);
481# endif
482# ifdef USE_UTMPX
483 utmpx_write_entry(li);
484# endif
485# ifdef USE_WTMPX
486 wtmpx_write_entry(li);
487# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000488 return (0);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000489}
490#endif
491
andre2ff7b5d2000-06-03 14:57:40 +0000492/**
andre61e67252000-06-04 17:07:49 +0000493 ** getlast_entry: Call low-level functions to retrieve the last login
494 ** time.
andre2ff7b5d2000-06-03 14:57:40 +0000495 **/
496
andre61e67252000-06-04 17:07:49 +0000497/* take the uid in li and return the last login time */
498int
499getlast_entry(struct logininfo *li)
500{
501#ifdef USE_LASTLOG
Damien Miller53c5d462000-06-28 00:50:50 +1000502 return(lastlog_get_entry(li));
Damien Millerdd47aa22000-06-27 11:18:27 +1000503#else /* !USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000504
Damien Miller8899ed32004-09-12 15:18:55 +1000505#if defined(DISABLE_LASTLOG)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000506 /* On some systems we shouldn't even try to obtain last login
andreecaabf12000-06-12 22:21:44 +0000507 * time, e.g. AIX */
Damien Miller8899ed32004-09-12 15:18:55 +1000508 return (0);
509# elif defined(USE_WTMP) && \
510 (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
andre61e67252000-06-04 17:07:49 +0000511 /* retrieve last login time from utmp */
Damien Millerdd47aa22000-06-27 11:18:27 +1000512 return (wtmp_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000513# elif defined(USE_WTMPX) && \
514 (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
andre61e67252000-06-04 17:07:49 +0000515 /* If wtmp isn't available, try wtmpx */
Damien Millerdd47aa22000-06-27 11:18:27 +1000516 return (wtmpx_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000517# else
andre61e67252000-06-04 17:07:49 +0000518 /* Give up: No means of retrieving last login time */
Damien Miller8899ed32004-09-12 15:18:55 +1000519 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000520# endif /* DISABLE_LASTLOG */
Damien Millerdd47aa22000-06-27 11:18:27 +1000521#endif /* USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000522}
523
524
525
andre2ff7b5d2000-06-03 14:57:40 +0000526/*
andre61e67252000-06-04 17:07:49 +0000527 * 'line' string utility functions
528 *
529 * These functions process the 'line' string into one of three forms:
530 *
andre2ff7b5d2000-06-03 14:57:40 +0000531 * 1. The full filename (including '/dev')
532 * 2. The stripped name (excluding '/dev')
andre61e67252000-06-04 17:07:49 +0000533 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
534 * /dev/pts/1 -> ts/1 )
andre2ff7b5d2000-06-03 14:57:40 +0000535 *
536 * Form 3 is used on some systems to identify a .tmp.? entry when
537 * attempting to remove it. Typically both addition and removal is
andre61e67252000-06-04 17:07:49 +0000538 * performed by one application - say, sshd - so as long as the choice
539 * uniquely identifies a terminal it's ok.
andre2ff7b5d2000-06-03 14:57:40 +0000540 */
541
542
Damien Miller8899ed32004-09-12 15:18:55 +1000543/*
544 * line_fullname(): add the leading '/dev/' if it doesn't exist make
545 * sure dst has enough space, if not just copy src (ugh)
546 */
andre2ff7b5d2000-06-03 14:57:40 +0000547char *
Damien Miller52c8afe2005-06-19 10:19:43 +1000548line_fullname(char *dst, const char *src, u_int dstsize)
andre61e67252000-06-04 17:07:49 +0000549{
andre2ff7b5d2000-06-03 14:57:40 +0000550 memset(dst, '\0', dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000551 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
andre2ff7b5d2000-06-03 14:57:40 +0000552 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000553 else {
Damien Miller1a132252000-06-13 21:23:17 +1000554 strlcpy(dst, "/dev/", dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000555 strlcat(dst, src, dstsize);
556 }
Damien Miller8899ed32004-09-12 15:18:55 +1000557 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000558}
559
andre61e67252000-06-04 17:07:49 +0000560/* line_stripname(): strip the leading '/dev' if it exists, return dst */
andre2ff7b5d2000-06-03 14:57:40 +0000561char *
andre61e67252000-06-04 17:07:49 +0000562line_stripname(char *dst, const char *src, int dstsize)
563{
andre2ff7b5d2000-06-03 14:57:40 +0000564 memset(dst, '\0', dstsize);
565 if (strncmp(src, "/dev/", 5) == 0)
Damien Millerf5a81472000-09-30 21:34:44 +1100566 strlcpy(dst, src + 5, dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000567 else
568 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000569 return (dst);
andre61e67252000-06-04 17:07:49 +0000570}
571
Damien Miller94cf4c82005-07-17 17:04:47 +1000572/*
Damien Miller8899ed32004-09-12 15:18:55 +1000573 * line_abbrevname(): Return the abbreviated (usually four-character)
andre61e67252000-06-04 17:07:49 +0000574 * form of the line (Just use the last <dstsize> characters of the
575 * full name.)
576 *
577 * NOTE: use strncpy because we do NOT necessarily want zero
Damien Miller8899ed32004-09-12 15:18:55 +1000578 * termination
579 */
andre2ff7b5d2000-06-03 14:57:40 +0000580char *
Kevin Stevesef4eea92001-02-05 12:42:17 +0000581line_abbrevname(char *dst, const char *src, int dstsize)
Damien Millerdd47aa22000-06-27 11:18:27 +1000582{
583 size_t len;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000584
andre2ff7b5d2000-06-03 14:57:40 +0000585 memset(dst, '\0', dstsize);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000586
Damien Miller8e81ed32000-07-01 13:17:42 +1000587 /* Always skip prefix if present */
588 if (strncmp(src, "/dev/", 5) == 0)
589 src += 5;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000590
Damien Millerf1b9d112002-04-23 23:09:19 +1000591#ifdef WITH_ABBREV_NO_TTY
592 if (strncmp(src, "tty", 3) == 0)
593 src += 3;
594#endif
595
Damien Millerdd47aa22000-06-27 11:18:27 +1000596 len = strlen(src);
597
Damien Miller8e81ed32000-07-01 13:17:42 +1000598 if (len > 0) {
599 if (((int)len - dstsize) > 0)
600 src += ((int)len - dstsize);
601
602 /* note: _don't_ change this to strlcpy */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000603 strncpy(dst, src, (size_t)dstsize);
Damien Millerdd47aa22000-06-27 11:18:27 +1000604 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000605
Damien Miller8899ed32004-09-12 15:18:55 +1000606 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000607}
608
andre2ff7b5d2000-06-03 14:57:40 +0000609/**
610 ** utmp utility functions
andre61e67252000-06-04 17:07:49 +0000611 **
612 ** These functions manipulate struct utmp, taking system differences
613 ** into account.
andre2ff7b5d2000-06-03 14:57:40 +0000614 **/
615
616#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
617
andre2ff7b5d2000-06-03 14:57:40 +0000618/* build the utmp structure */
619void
andre61e67252000-06-04 17:07:49 +0000620set_utmp_time(struct logininfo *li, struct utmp *ut)
621{
Damien Miller8899ed32004-09-12 15:18:55 +1000622# if defined(HAVE_TV_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000623 ut->ut_tv.tv_sec = li->tv_sec;
624 ut->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000625# elif defined(HAVE_TIME_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000626 ut->ut_time = li->tv_sec;
Damien Millerdd47aa22000-06-27 11:18:27 +1000627# endif
andre2ff7b5d2000-06-03 14:57:40 +0000628}
629
630void
631construct_utmp(struct logininfo *li,
andre61e67252000-06-04 17:07:49 +0000632 struct utmp *ut)
633{
Damien Miller02e16ad2003-01-03 14:42:27 +1100634# ifdef HAVE_ADDR_V6_IN_UTMP
635 struct sockaddr_in6 *sa6;
Damien Miller8899ed32004-09-12 15:18:55 +1000636# endif
637
Damien Miller348c9b72000-08-15 10:01:22 +1000638 memset(ut, '\0', sizeof(*ut));
andre6bb92372000-06-19 08:20:03 +0000639
640 /* First fill out fields used for both logins and logouts */
641
Damien Millerdd47aa22000-06-27 11:18:27 +1000642# ifdef HAVE_ID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000643 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +1000644# endif
andre2ff7b5d2000-06-03 14:57:40 +0000645
Damien Millerdd47aa22000-06-27 11:18:27 +1000646# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +0000647 /* This is done here to keep utmp constants out of struct logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000648 switch (li->type) {
649 case LTYPE_LOGIN:
650 ut->ut_type = USER_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700651#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000652 cray_set_tmpdir(ut);
653#endif
andre2ff7b5d2000-06-03 14:57:40 +0000654 break;
655 case LTYPE_LOGOUT:
656 ut->ut_type = DEAD_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700657#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000658 cray_retain_utmp(ut, li->pid);
659#endif
andre2ff7b5d2000-06-03 14:57:40 +0000660 break;
661 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000662# endif
andre6bb92372000-06-19 08:20:03 +0000663 set_utmp_time(li, ut);
andre2ff7b5d2000-06-03 14:57:40 +0000664
andre6bb92372000-06-19 08:20:03 +0000665 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +1000666
667# ifdef HAVE_PID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000668 ut->ut_pid = li->pid;
Damien Millerdd47aa22000-06-27 11:18:27 +1000669# endif
andre6bb92372000-06-19 08:20:03 +0000670
671 /* If we're logging out, leave all other fields blank */
672 if (li->type == LTYPE_LOGOUT)
Damien Miller8899ed32004-09-12 15:18:55 +1000673 return;
andre6bb92372000-06-19 08:20:03 +0000674
Damien Millerdd47aa22000-06-27 11:18:27 +1000675 /*
676 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000677 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000678 */
andre6bb92372000-06-19 08:20:03 +0000679
680 /* Use strncpy because we don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000681 strncpy(ut->ut_name, li->username,
682 MIN_SIZEOF(ut->ut_name, li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000683# ifdef HAVE_HOST_IN_UTMP
Damien Miller8899ed32004-09-12 15:18:55 +1000684 strncpy(ut->ut_host, li->hostname,
685 MIN_SIZEOF(ut->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000686# endif
687# ifdef HAVE_ADDR_IN_UTMP
andre61e67252000-06-04 17:07:49 +0000688 /* this is just a 32-bit IP address */
689 if (li->hostaddr.sa.sa_family == AF_INET)
690 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000691# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100692# ifdef HAVE_ADDR_V6_IN_UTMP
693 /* this is just a 128-bit IPv6 address */
694 if (li->hostaddr.sa.sa_family == AF_INET6) {
695 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
696 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
697 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
698 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
699 ut->ut_addr_v6[1] = 0;
700 ut->ut_addr_v6[2] = 0;
701 ut->ut_addr_v6[3] = 0;
702 }
703 }
704# endif
andre61e67252000-06-04 17:07:49 +0000705}
Damien Millerdd47aa22000-06-27 11:18:27 +1000706#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
andre61e67252000-06-04 17:07:49 +0000707
andre2ff7b5d2000-06-03 14:57:40 +0000708/**
709 ** utmpx utility functions
andre61e67252000-06-04 17:07:49 +0000710 **
711 ** These functions manipulate struct utmpx, accounting for system
712 ** variations.
andre2ff7b5d2000-06-03 14:57:40 +0000713 **/
714
715#if defined(USE_UTMPX) || defined (USE_WTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000716/* build the utmpx structure */
717void
andre61e67252000-06-04 17:07:49 +0000718set_utmpx_time(struct logininfo *li, struct utmpx *utx)
719{
Damien Miller8899ed32004-09-12 15:18:55 +1000720# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000721 utx->ut_tv.tv_sec = li->tv_sec;
722 utx->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000723# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000724 utx->ut_time = li->tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +1000725# endif
andre2ff7b5d2000-06-03 14:57:40 +0000726}
727
andre61e67252000-06-04 17:07:49 +0000728void
729construct_utmpx(struct logininfo *li, struct utmpx *utx)
730{
Damien Miller02e16ad2003-01-03 14:42:27 +1100731# ifdef HAVE_ADDR_V6_IN_UTMP
732 struct sockaddr_in6 *sa6;
733# endif
Damien Miller348c9b72000-08-15 10:01:22 +1000734 memset(utx, '\0', sizeof(*utx));
Damien Miller8899ed32004-09-12 15:18:55 +1000735
Damien Miller8e81ed32000-07-01 13:17:42 +1000736# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000737 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
Damien Miller8e81ed32000-07-01 13:17:42 +1000738# endif
andre2ff7b5d2000-06-03 14:57:40 +0000739
740 /* this is done here to keep utmp constants out of loginrec.h */
741 switch (li->type) {
742 case LTYPE_LOGIN:
743 utx->ut_type = USER_PROCESS;
744 break;
745 case LTYPE_LOGOUT:
746 utx->ut_type = DEAD_PROCESS;
747 break;
748 }
andre2ff7b5d2000-06-03 14:57:40 +0000749 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
andre2ff7b5d2000-06-03 14:57:40 +0000750 set_utmpx_time(li, utx);
andre6bb92372000-06-19 08:20:03 +0000751 utx->ut_pid = li->pid;
Damien Miller8899ed32004-09-12 15:18:55 +1000752
Tim Ricee06ae4a2002-02-24 17:56:46 -0800753 /* strncpy(): Don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000754 strncpy(utx->ut_name, li->username,
755 MIN_SIZEOF(utx->ut_name, li->username));
andre6bb92372000-06-19 08:20:03 +0000756
757 if (li->type == LTYPE_LOGOUT)
758 return;
759
Damien Millerdd47aa22000-06-27 11:18:27 +1000760 /*
761 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000762 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000763 */
andre6bb92372000-06-19 08:20:03 +0000764
Damien Millerdd47aa22000-06-27 11:18:27 +1000765# ifdef HAVE_HOST_IN_UTMPX
Damien Miller8899ed32004-09-12 15:18:55 +1000766 strncpy(utx->ut_host, li->hostname,
767 MIN_SIZEOF(utx->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000768# endif
769# ifdef HAVE_ADDR_IN_UTMPX
Damien Millerd6f204d2000-09-23 13:57:27 +1100770 /* this is just a 32-bit IP address */
771 if (li->hostaddr.sa.sa_family == AF_INET)
772 utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Damien Millerdd47aa22000-06-27 11:18:27 +1000773# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100774# ifdef HAVE_ADDR_V6_IN_UTMP
775 /* this is just a 128-bit IPv6 address */
776 if (li->hostaddr.sa.sa_family == AF_INET6) {
777 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
778 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
779 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
780 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
781 ut->ut_addr_v6[1] = 0;
782 ut->ut_addr_v6[2] = 0;
783 ut->ut_addr_v6[3] = 0;
784 }
785 }
786# endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000787# ifdef HAVE_SYSLEN_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +0000788 /* ut_syslen is the length of the utx_host string */
789 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +1000790# endif
andre61e67252000-06-04 17:07:49 +0000791}
Damien Millerdd47aa22000-06-27 11:18:27 +1000792#endif /* USE_UTMPX || USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +0000793
794/**
andre61e67252000-06-04 17:07:49 +0000795 ** Low-level utmp functions
andre2ff7b5d2000-06-03 14:57:40 +0000796 **/
797
798/* FIXME: (ATL) utmp_write_direct needs testing */
andre2ff7b5d2000-06-03 14:57:40 +0000799#ifdef USE_UTMP
800
andre2ff7b5d2000-06-03 14:57:40 +0000801/* if we can, use pututline() etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000802# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
803 defined(HAVE_PUTUTLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000804# define UTMP_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000805# endif
andre2ff7b5d2000-06-03 14:57:40 +0000806
807
808/* write a utmp entry with the system's help (pututline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000809# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000810static int
andre61e67252000-06-04 17:07:49 +0000811utmp_write_library(struct logininfo *li, struct utmp *ut)
812{
andre2ff7b5d2000-06-03 14:57:40 +0000813 setutent();
814 pututline(ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000815# ifdef HAVE_ENDUTENT
andre2ff7b5d2000-06-03 14:57:40 +0000816 endutent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000817# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000818 return (1);
andre61e67252000-06-04 17:07:49 +0000819}
Damien Millerdd47aa22000-06-27 11:18:27 +1000820# else /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000821
Damien Miller94cf4c82005-07-17 17:04:47 +1000822/*
Damien Miller8899ed32004-09-12 15:18:55 +1000823 * Write a utmp entry direct to the file
824 * This is a slightly modification of code in OpenBSD's login.c
825 */
andre2ff7b5d2000-06-03 14:57:40 +0000826static int
andre61e67252000-06-04 17:07:49 +0000827utmp_write_direct(struct logininfo *li, struct utmp *ut)
828{
andre2ff7b5d2000-06-03 14:57:40 +0000829 struct utmp old_ut;
830 register int fd;
831 int tty;
832
andre6bb92372000-06-19 08:20:03 +0000833 /* FIXME: (ATL) ttyslot() needs local implementation */
Damien Miller348c9b72000-08-15 10:01:22 +1000834
Damien Millere5192fa2000-08-29 14:30:37 +1100835#if defined(HAVE_GETTTYENT)
Damien Miller8899ed32004-09-12 15:18:55 +1000836 struct ttyent *ty;
Damien Miller348c9b72000-08-15 10:01:22 +1000837
838 tty=0;
Damien Miller348c9b72000-08-15 10:01:22 +1000839 setttyent();
Damien Miller8899ed32004-09-12 15:18:55 +1000840 while (NULL != (ty = getttyent())) {
Damien Miller348c9b72000-08-15 10:01:22 +1000841 tty++;
842 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
843 break;
844 }
845 endttyent();
846
Damien Miller8899ed32004-09-12 15:18:55 +1000847 if (NULL == ty) {
Damien Miller81409592004-08-15 19:12:52 +1000848 logit("%s: tty not found", __func__);
849 return (0);
Damien Miller348c9b72000-08-15 10:01:22 +1000850 }
851#else /* FIXME */
852
andre2ff7b5d2000-06-03 14:57:40 +0000853 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
854
Damien Millere5192fa2000-08-29 14:30:37 +1100855#endif /* HAVE_GETTTYENT */
Damien Miller348c9b72000-08-15 10:01:22 +1000856
andre2ff7b5d2000-06-03 14:57:40 +0000857 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
Damien Miller81409592004-08-15 19:12:52 +1000858 off_t pos, ret;
859
860 pos = (off_t)tty * sizeof(struct utmp);
861 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000862 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000863 return (0);
864 }
865 if (ret != pos) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000866 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Millerb0419f22004-08-23 21:53:28 +1000867 __func__, tty, UTMP_FILE);
Damien Miller81409592004-08-15 19:12:52 +1000868 return (0);
869 }
andre2ff7b5d2000-06-03 14:57:40 +0000870 /*
871 * Prevent luser from zero'ing out ut_host.
872 * If the new ut_line is empty but the old one is not
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000873 * and ut_line and ut_name match, preserve the old ut_line.
andre2ff7b5d2000-06-03 14:57:40 +0000874 */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000875 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
Damien Miller8899ed32004-09-12 15:18:55 +1000876 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
877 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
878 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0))
879 memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000880
Damien Miller81409592004-08-15 19:12:52 +1000881 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000882 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000883 return (0);
884 }
885 if (ret != pos) {
Damien Millerb0419f22004-08-23 21:53:28 +1000886 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Miller81409592004-08-15 19:12:52 +1000887 __func__, tty, UTMP_FILE);
888 return (0);
889 }
Damien Miller8899ed32004-09-12 15:18:55 +1000890 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
Damien Miller81409592004-08-15 19:12:52 +1000891 logit("%s: error writing %s: %s", __func__,
andre6bb92372000-06-19 08:20:03 +0000892 UTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +1000893 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000894
Damien Miller8899ed32004-09-12 15:18:55 +1000895 close(fd);
896 return (1);
Damien Miller53c5d462000-06-28 00:50:50 +1000897 } else {
Damien Miller8899ed32004-09-12 15:18:55 +1000898 return (0);
Damien Miller53c5d462000-06-28 00:50:50 +1000899 }
andre61e67252000-06-04 17:07:49 +0000900}
Damien Millerdd47aa22000-06-27 11:18:27 +1000901# endif /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000902
903static int
andre61e67252000-06-04 17:07:49 +0000904utmp_perform_login(struct logininfo *li)
905{
andre2ff7b5d2000-06-03 14:57:40 +0000906 struct utmp ut;
907
908 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000909# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000910 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000911 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000912 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000913 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000914# else
andre2ff7b5d2000-06-03 14:57:40 +0000915 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000916 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000917 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000918 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000919# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000920 return (1);
andre61e67252000-06-04 17:07:49 +0000921}
andre2ff7b5d2000-06-03 14:57:40 +0000922
923
924static int
andre61e67252000-06-04 17:07:49 +0000925utmp_perform_logout(struct logininfo *li)
926{
andre2ff7b5d2000-06-03 14:57:40 +0000927 struct utmp ut;
928
andre6bb92372000-06-19 08:20:03 +0000929 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000930# ifdef UTMP_USE_LIBRARY
andre6bb92372000-06-19 08:20:03 +0000931 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000932 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000933 return (0);
andre6bb92372000-06-19 08:20:03 +0000934 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000935# else
andre6bb92372000-06-19 08:20:03 +0000936 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000937 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000938 return (0);
andre6bb92372000-06-19 08:20:03 +0000939 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000940# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000941 return (1);
andre61e67252000-06-04 17:07:49 +0000942}
andre2ff7b5d2000-06-03 14:57:40 +0000943
944
945int
andre61e67252000-06-04 17:07:49 +0000946utmp_write_entry(struct logininfo *li)
947{
andre2ff7b5d2000-06-03 14:57:40 +0000948 switch(li->type) {
949 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +1000950 return (utmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +0000951
952 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +1000953 return (utmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +0000954
955 default:
Damien Miller6b0279c2004-09-12 15:25:17 +1000956 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000957 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000958 }
andre61e67252000-06-04 17:07:49 +0000959}
Damien Millerdd47aa22000-06-27 11:18:27 +1000960#endif /* USE_UTMP */
andre2ff7b5d2000-06-03 14:57:40 +0000961
962
963/**
andre61e67252000-06-04 17:07:49 +0000964 ** Low-level utmpx functions
andre2ff7b5d2000-06-03 14:57:40 +0000965 **/
966
967/* not much point if we don't want utmpx entries */
968#ifdef USE_UTMPX
969
andre2ff7b5d2000-06-03 14:57:40 +0000970/* if we have the wherewithall, use pututxline etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000971# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
972 defined(HAVE_PUTUTXLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000973# define UTMPX_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000974# endif
andre2ff7b5d2000-06-03 14:57:40 +0000975
976
977/* write a utmpx entry with the system's help (pututxline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000978# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000979static int
andre61e67252000-06-04 17:07:49 +0000980utmpx_write_library(struct logininfo *li, struct utmpx *utx)
981{
andre2ff7b5d2000-06-03 14:57:40 +0000982 setutxent();
983 pututxline(utx);
984
Damien Millerdd47aa22000-06-27 11:18:27 +1000985# ifdef HAVE_ENDUTXENT
andre2ff7b5d2000-06-03 14:57:40 +0000986 endutxent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000987# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000988 return (1);
andre61e67252000-06-04 17:07:49 +0000989}
andre2ff7b5d2000-06-03 14:57:40 +0000990
Damien Millerdd47aa22000-06-27 11:18:27 +1000991# else /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000992
993/* write a utmp entry direct to the file */
994static int
andre61e67252000-06-04 17:07:49 +0000995utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000996{
Damien Miller6b0279c2004-09-12 15:25:17 +1000997 logit("%s: not implemented!", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000998 return (0);
andre61e67252000-06-04 17:07:49 +0000999}
Damien Millerdd47aa22000-06-27 11:18:27 +10001000# endif /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +00001001
1002static int
andre61e67252000-06-04 17:07:49 +00001003utmpx_perform_login(struct logininfo *li)
1004{
andre2ff7b5d2000-06-03 14:57:40 +00001005 struct utmpx utx;
1006
1007 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001008# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001009 if (!utmpx_write_library(li, &utx)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001010 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001011 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001012 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001013# else
andre2ff7b5d2000-06-03 14:57:40 +00001014 if (!utmpx_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001015 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001016 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001017 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001018# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001019 return (1);
andre61e67252000-06-04 17:07:49 +00001020}
andre2ff7b5d2000-06-03 14:57:40 +00001021
1022
1023static int
andre61e67252000-06-04 17:07:49 +00001024utmpx_perform_logout(struct logininfo *li)
1025{
andre2ff7b5d2000-06-03 14:57:40 +00001026 struct utmpx utx;
1027
Tim Ricee06ae4a2002-02-24 17:56:46 -08001028 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001029# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001030 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +10001031# endif
1032# ifdef HAVE_TYPE_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001033 utx.ut_type = DEAD_PROCESS;
Damien Millerdd47aa22000-06-27 11:18:27 +10001034# endif
andre2ff7b5d2000-06-03 14:57:40 +00001035
Damien Millerdd47aa22000-06-27 11:18:27 +10001036# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001037 utmpx_write_library(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001038# else
andre2ff7b5d2000-06-03 14:57:40 +00001039 utmpx_write_direct(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001040# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001041 return (1);
andre61e67252000-06-04 17:07:49 +00001042}
andre2ff7b5d2000-06-03 14:57:40 +00001043
andre2ff7b5d2000-06-03 14:57:40 +00001044int
andre61e67252000-06-04 17:07:49 +00001045utmpx_write_entry(struct logininfo *li)
1046{
andre2ff7b5d2000-06-03 14:57:40 +00001047 switch(li->type) {
1048 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001049 return (utmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001050 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001051 return (utmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001052 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001053 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001054 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001055 }
andre61e67252000-06-04 17:07:49 +00001056}
Damien Millerdd47aa22000-06-27 11:18:27 +10001057#endif /* USE_UTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001058
1059
1060/**
andre61e67252000-06-04 17:07:49 +00001061 ** Low-level wtmp functions
andre2ff7b5d2000-06-03 14:57:40 +00001062 **/
1063
Kevin Stevesef4eea92001-02-05 12:42:17 +00001064#ifdef USE_WTMP
andre2ff7b5d2000-06-03 14:57:40 +00001065
Damien Miller94cf4c82005-07-17 17:04:47 +10001066/*
Damien Miller8899ed32004-09-12 15:18:55 +10001067 * Write a wtmp entry direct to the end of the file
1068 * This is a slight modification of code in OpenBSD's logwtmp.c
1069 */
andre2ff7b5d2000-06-03 14:57:40 +00001070static int
andre61e67252000-06-04 17:07:49 +00001071wtmp_write(struct logininfo *li, struct utmp *ut)
1072{
andre2ff7b5d2000-06-03 14:57:40 +00001073 struct stat buf;
1074 int fd, ret = 1;
1075
1076 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
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));
Damien Miller8899ed32004-09-12 15:18:55 +10001079 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001080 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001081 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001082 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
andre2ff7b5d2000-06-03 14:57:40 +00001083 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001084 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001085 WTMP_FILE, strerror(errno));
1086 ret = 0;
1087 }
Damien Miller8899ed32004-09-12 15:18:55 +10001088 close(fd);
1089 return (ret);
andre61e67252000-06-04 17:07:49 +00001090}
andre2ff7b5d2000-06-03 14:57:40 +00001091
andre2ff7b5d2000-06-03 14:57:40 +00001092static int
Damien Millerdd47aa22000-06-27 11:18:27 +10001093wtmp_perform_login(struct logininfo *li)
1094{
andre2ff7b5d2000-06-03 14:57:40 +00001095 struct utmp ut;
1096
1097 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001098 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001099}
andre2ff7b5d2000-06-03 14:57:40 +00001100
1101
1102static int
andre61e67252000-06-04 17:07:49 +00001103wtmp_perform_logout(struct logininfo *li)
1104{
andre2ff7b5d2000-06-03 14:57:40 +00001105 struct utmp ut;
1106
1107 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001108 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001109}
andre2ff7b5d2000-06-03 14:57:40 +00001110
1111
1112int
andre61e67252000-06-04 17:07:49 +00001113wtmp_write_entry(struct logininfo *li)
1114{
andre2ff7b5d2000-06-03 14:57:40 +00001115 switch(li->type) {
1116 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001117 return (wtmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001118 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001119 return (wtmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001120 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001121 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001122 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001123 }
andre61e67252000-06-04 17:07:49 +00001124}
andre2ff7b5d2000-06-03 14:57:40 +00001125
1126
Damien Miller94cf4c82005-07-17 17:04:47 +10001127/*
Damien Miller8899ed32004-09-12 15:18:55 +10001128 * Notes on fetching login data from wtmp/wtmpx
Kevin Stevesef4eea92001-02-05 12:42:17 +00001129 *
andre6bb92372000-06-19 08:20:03 +00001130 * Logouts are usually recorded with (amongst other things) a blank
1131 * username on a given tty line. However, some systems (HP-UX is one)
1132 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1133 *
1134 * Since we're only looking for logins here, we know that the username
1135 * must be set correctly. On systems that leave it in, we check for
1136 * ut_type==USER_PROCESS (indicating a login.)
1137 *
1138 * Portability: Some systems may set something other than USER_PROCESS
1139 * to indicate a login process. I don't know of any as I write. Also,
1140 * it's possible that some systems may both leave the username in
1141 * place and not have ut_type.
1142 */
1143
andre6bb92372000-06-19 08:20:03 +00001144/* return true if this wtmp entry indicates a login */
1145static int
1146wtmp_islogin(struct logininfo *li, struct utmp *ut)
1147{
Kevin Stevesef4eea92001-02-05 12:42:17 +00001148 if (strncmp(li->username, ut->ut_name,
Damien Miller8899ed32004-09-12 15:18:55 +10001149 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001150# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001151 if (ut->ut_type & USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001152 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001153# else
Damien Miller8899ed32004-09-12 15:18:55 +10001154 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001155# endif
andre6bb92372000-06-19 08:20:03 +00001156 }
Damien Miller8899ed32004-09-12 15:18:55 +10001157 return (0);
andre6bb92372000-06-19 08:20:03 +00001158}
1159
andre2ff7b5d2000-06-03 14:57:40 +00001160int
andre61e67252000-06-04 17:07:49 +00001161wtmp_get_entry(struct logininfo *li)
1162{
andre2ff7b5d2000-06-03 14:57:40 +00001163 struct stat st;
1164 struct utmp ut;
Damien Miller8899ed32004-09-12 15:18:55 +10001165 int fd, found = 0;
andre6bb92372000-06-19 08:20:03 +00001166
1167 /* Clear the time entries in our logininfo */
1168 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001169
1170 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001171 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001172 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001173 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001174 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001175 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001176 logit("%s: couldn't stat %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001177 WTMP_FILE, strerror(errno));
1178 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001179 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001180 }
andre2ff7b5d2000-06-03 14:57:40 +00001181
andre6bb92372000-06-19 08:20:03 +00001182 /* Seek to the start of the last struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001183 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
andre6bb92372000-06-19 08:20:03 +00001184 /* Looks like we've got a fresh wtmp file */
1185 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001186 return (0);
andre6bb92372000-06-19 08:20:03 +00001187 }
1188
1189 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001190 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001191 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001192 WTMP_FILE, strerror(errno));
1193 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001194 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001195 }
andre6bb92372000-06-19 08:20:03 +00001196 if ( wtmp_islogin(li, &ut) ) {
1197 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001198 /*
1199 * We've already checked for a time in struct
1200 * utmp, in login_getlast()
1201 */
Damien Millerdd47aa22000-06-27 11:18:27 +10001202# ifdef HAVE_TIME_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +00001203 li->tv_sec = ut.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001204# else
andre2ff7b5d2000-06-03 14:57:40 +00001205# if HAVE_TV_IN_UTMP
1206 li->tv_sec = ut.ut_tv.tv_sec;
1207# endif
Damien Millerdd47aa22000-06-27 11:18:27 +10001208# endif
andre6bb92372000-06-19 08:20:03 +00001209 line_fullname(li->line, ut.ut_line,
Damien Miller8899ed32004-09-12 15:18:55 +10001210 MIN_SIZEOF(li->line, ut.ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +10001211# ifdef HAVE_HOST_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001212 strlcpy(li->hostname, ut.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001213 MIN_SIZEOF(li->hostname, ut.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001214# endif
andre6bb92372000-06-19 08:20:03 +00001215 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001216 }
andre6bb92372000-06-19 08:20:03 +00001217 /* Seek back 2 x struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001218 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
andre6bb92372000-06-19 08:20:03 +00001219 /* We've found the start of the file, so quit */
Damien Miller8899ed32004-09-12 15:18:55 +10001220 close(fd);
1221 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001222 }
andre6bb92372000-06-19 08:20:03 +00001223 }
1224
1225 /* We found an entry. Tidy up and return */
1226 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001227 return (1);
andre61e67252000-06-04 17:07:49 +00001228}
Damien Millerdd47aa22000-06-27 11:18:27 +10001229# endif /* USE_WTMP */
andre2ff7b5d2000-06-03 14:57:40 +00001230
1231
1232/**
andre61e67252000-06-04 17:07:49 +00001233 ** Low-level wtmpx functions
andre2ff7b5d2000-06-03 14:57:40 +00001234 **/
1235
1236#ifdef USE_WTMPX
Damien Miller8899ed32004-09-12 15:18:55 +10001237/*
1238 * Write a wtmpx entry direct to the end of the file
1239 * This is a slight modification of code in OpenBSD's logwtmp.c
1240 */
andre2ff7b5d2000-06-03 14:57:40 +00001241static int
andre61e67252000-06-04 17:07:49 +00001242wtmpx_write(struct logininfo *li, struct utmpx *utx)
1243{
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001244#ifndef HAVE_UPDWTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001245 struct stat buf;
1246 int fd, ret = 1;
1247
1248 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001249 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001250 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001251 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001252 }
1253
Kevin Stevesef4eea92001-02-05 12:42:17 +00001254 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001255 if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
andre2ff7b5d2000-06-03 14:57:40 +00001256 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001257 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001258 WTMPX_FILE, strerror(errno));
1259 ret = 0;
1260 }
Damien Miller8899ed32004-09-12 15:18:55 +10001261 close(fd);
andre2ff7b5d2000-06-03 14:57:40 +00001262
Damien Miller8899ed32004-09-12 15:18:55 +10001263 return (ret);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001264#else
1265 updwtmpx(WTMPX_FILE, utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001266 return (1);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001267#endif
andre61e67252000-06-04 17:07:49 +00001268}
andre2ff7b5d2000-06-03 14:57:40 +00001269
1270
1271static int
andre61e67252000-06-04 17:07:49 +00001272wtmpx_perform_login(struct logininfo *li)
1273{
andre2ff7b5d2000-06-03 14:57:40 +00001274 struct utmpx utx;
1275
1276 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001277 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001278}
andre2ff7b5d2000-06-03 14:57:40 +00001279
1280
1281static int
andre61e67252000-06-04 17:07:49 +00001282wtmpx_perform_logout(struct logininfo *li)
1283{
andre2ff7b5d2000-06-03 14:57:40 +00001284 struct utmpx utx;
1285
1286 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001287 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001288}
andre2ff7b5d2000-06-03 14:57:40 +00001289
1290
1291int
andre61e67252000-06-04 17:07:49 +00001292wtmpx_write_entry(struct logininfo *li)
1293{
andre2ff7b5d2000-06-03 14:57:40 +00001294 switch(li->type) {
1295 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001296 return (wtmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001297 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001298 return (wtmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001299 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001300 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001301 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001302 }
andre61e67252000-06-04 17:07:49 +00001303}
andre2ff7b5d2000-06-03 14:57:40 +00001304
andre6bb92372000-06-19 08:20:03 +00001305/* Please see the notes above wtmp_islogin() for information about the
1306 next two functions */
1307
1308/* Return true if this wtmpx entry indicates a login */
1309static int
1310wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1311{
Damien Miller8899ed32004-09-12 15:18:55 +10001312 if (strncmp(li->username, utx->ut_name,
1313 MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001314# ifdef HAVE_TYPE_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +00001315 if (utx->ut_type == USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001316 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001317# else
Damien Miller8899ed32004-09-12 15:18:55 +10001318 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001319# endif
andre6bb92372000-06-19 08:20:03 +00001320 }
Damien Miller8899ed32004-09-12 15:18:55 +10001321 return (0);
andre6bb92372000-06-19 08:20:03 +00001322}
1323
andre2ff7b5d2000-06-03 14:57:40 +00001324
1325int
andre61e67252000-06-04 17:07:49 +00001326wtmpx_get_entry(struct logininfo *li)
1327{
andre2ff7b5d2000-06-03 14:57:40 +00001328 struct stat st;
1329 struct utmpx utx;
andre6bb92372000-06-19 08:20:03 +00001330 int fd, found=0;
1331
1332 /* Clear the time entries */
1333 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001334
1335 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001336 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001337 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001338 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001339 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001340 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001341 logit("%s: couldn't stat %s: %s", __func__,
Tim Ricecdb82942002-07-14 15:33:20 -07001342 WTMPX_FILE, strerror(errno));
andre2ff7b5d2000-06-03 14:57:40 +00001343 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001344 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001345 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001346
andre6bb92372000-06-19 08:20:03 +00001347 /* Seek to the start of the last struct utmpx */
Kevin Steves52172652001-10-02 00:29:00 +00001348 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
andre6bb92372000-06-19 08:20:03 +00001349 /* probably a newly rotated wtmpx file */
1350 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001351 return (0);
andre6bb92372000-06-19 08:20:03 +00001352 }
andre2ff7b5d2000-06-03 14:57:40 +00001353
andre6bb92372000-06-19 08:20:03 +00001354 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001355 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001356 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001357 WTMPX_FILE, strerror(errno));
1358 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001359 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001360 }
Damien Miller8899ed32004-09-12 15:18:55 +10001361 /*
Damien Miller94cf4c82005-07-17 17:04:47 +10001362 * Logouts are recorded as a blank username on a particular
Damien Miller8899ed32004-09-12 15:18:55 +10001363 * line. So, we just need to find the username in struct utmpx
1364 */
1365 if (wtmpx_islogin(li, &utx)) {
Tim Rice370e0ba2002-07-14 15:50:51 -07001366 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001367# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001368 li->tv_sec = utx.ut_tv.tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +10001369# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001370 li->tv_sec = utx.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001371# endif
Damien Miller1a132252000-06-13 21:23:17 +10001372 line_fullname(li->line, utx.ut_line, sizeof(li->line));
Damien Miller8899ed32004-09-12 15:18:55 +10001373# if defined(HAVE_HOST_IN_UTMPX)
andre6bb92372000-06-19 08:20:03 +00001374 strlcpy(li->hostname, utx.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001375 MIN_SIZEOF(li->hostname, utx.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001376# endif
andre6bb92372000-06-19 08:20:03 +00001377 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001378 }
Kevin Steves52172652001-10-02 00:29:00 +00001379 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
Damien Miller8899ed32004-09-12 15:18:55 +10001380 close(fd);
1381 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001382 }
andre6bb92372000-06-19 08:20:03 +00001383 }
1384
1385 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001386 return (1);
andre61e67252000-06-04 17:07:49 +00001387}
Damien Millerd5bf3072000-06-07 21:32:13 +10001388#endif /* USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001389
andre2ff7b5d2000-06-03 14:57:40 +00001390/**
andre61e67252000-06-04 17:07:49 +00001391 ** Low-level libutil login() functions
andre2ff7b5d2000-06-03 14:57:40 +00001392 **/
1393
1394#ifdef USE_LOGIN
andre2ff7b5d2000-06-03 14:57:40 +00001395static int
andre61e67252000-06-04 17:07:49 +00001396syslogin_perform_login(struct logininfo *li)
1397{
andre2ff7b5d2000-06-03 14:57:40 +00001398 struct utmp *ut;
1399
Damien Millerb0aae332004-09-12 15:26:00 +10001400 ut = xmalloc(sizeof(*ut));
andre2ff7b5d2000-06-03 14:57:40 +00001401 construct_utmp(li, ut);
1402 login(ut);
Damien Millerf211efc2003-03-10 11:23:06 +11001403 free(ut);
andre2ff7b5d2000-06-03 14:57:40 +00001404
Damien Miller8899ed32004-09-12 15:18:55 +10001405 return (1);
andre61e67252000-06-04 17:07:49 +00001406}
1407
andre2ff7b5d2000-06-03 14:57:40 +00001408static int
andre61e67252000-06-04 17:07:49 +00001409syslogin_perform_logout(struct logininfo *li)
1410{
Damien Millerdd47aa22000-06-27 11:18:27 +10001411# ifdef HAVE_LOGOUT
Darren Tucker4d2f3612004-04-08 10:57:05 +10001412 char line[UT_LINESIZE];
Kevin Stevesef4eea92001-02-05 12:42:17 +00001413
andre2ff7b5d2000-06-03 14:57:40 +00001414 (void)line_stripname(line, li->line, sizeof(line));
1415
Damien Miller8899ed32004-09-12 15:18:55 +10001416 if (!logout(line))
Damien Miller6b0279c2004-09-12 15:25:17 +10001417 logit("%s: logout() returned an error", __func__);
Damien Millerdd47aa22000-06-27 11:18:27 +10001418# ifdef HAVE_LOGWTMP
Damien Miller8899ed32004-09-12 15:18:55 +10001419 else
andre2ff7b5d2000-06-03 14:57:40 +00001420 logwtmp(line, "", "");
Damien Millerdd47aa22000-06-27 11:18:27 +10001421# endif
andre6bb92372000-06-19 08:20:03 +00001422 /* FIXME: (ATL - if the need arises) What to do if we have
1423 * login, but no logout? what if logout but no logwtmp? All
1424 * routines are in libutil so they should all be there,
1425 * but... */
Damien Millerdd47aa22000-06-27 11:18:27 +10001426# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001427 return (1);
andre61e67252000-06-04 17:07:49 +00001428}
andre2ff7b5d2000-06-03 14:57:40 +00001429
andre2ff7b5d2000-06-03 14:57:40 +00001430int
andre61e67252000-06-04 17:07:49 +00001431syslogin_write_entry(struct logininfo *li)
1432{
andre2ff7b5d2000-06-03 14:57:40 +00001433 switch (li->type) {
1434 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001435 return (syslogin_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001436 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001437 return (syslogin_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001438 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001439 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001440 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001441 }
andre61e67252000-06-04 17:07:49 +00001442}
Damien Millerd5bf3072000-06-07 21:32:13 +10001443#endif /* USE_LOGIN */
andre2ff7b5d2000-06-03 14:57:40 +00001444
1445/* end of file log-syslogin.c */
1446
andre2ff7b5d2000-06-03 14:57:40 +00001447/**
andre61e67252000-06-04 17:07:49 +00001448 ** Low-level lastlog functions
andre2ff7b5d2000-06-03 14:57:40 +00001449 **/
1450
1451#ifdef USE_LASTLOG
Damien Millerdd47aa22000-06-27 11:18:27 +10001452#define LL_FILE 1
1453#define LL_DIR 2
1454#define LL_OTHER 3
andre2ff7b5d2000-06-03 14:57:40 +00001455
andre2ff7b5d2000-06-03 14:57:40 +00001456static void
andre61e67252000-06-04 17:07:49 +00001457lastlog_construct(struct logininfo *li, struct lastlog *last)
1458{
andre2ff7b5d2000-06-03 14:57:40 +00001459 /* clear the structure */
Damien Miller348c9b72000-08-15 10:01:22 +10001460 memset(last, '\0', sizeof(*last));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001461
Damien Miller8899ed32004-09-12 15:18:55 +10001462 line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
andre6bb92372000-06-19 08:20:03 +00001463 strlcpy(last->ll_host, li->hostname,
1464 MIN_SIZEOF(last->ll_host, li->hostname));
andre2ff7b5d2000-06-03 14:57:40 +00001465 last->ll_time = li->tv_sec;
andre61e67252000-06-04 17:07:49 +00001466}
andre2ff7b5d2000-06-03 14:57:40 +00001467
andre2ff7b5d2000-06-03 14:57:40 +00001468static int
andre61e67252000-06-04 17:07:49 +00001469lastlog_filetype(char *filename)
1470{
andre2ff7b5d2000-06-03 14:57:40 +00001471 struct stat st;
1472
Damien Millerdd47aa22000-06-27 11:18:27 +10001473 if (stat(LASTLOG_FILE, &st) != 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001474 logit("%s: Couldn't stat %s: %s", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001475 LASTLOG_FILE, strerror(errno));
1476 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001477 }
andre2ff7b5d2000-06-03 14:57:40 +00001478 if (S_ISDIR(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001479 return (LL_DIR);
andre2ff7b5d2000-06-03 14:57:40 +00001480 else if (S_ISREG(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001481 return (LL_FILE);
andre2ff7b5d2000-06-03 14:57:40 +00001482 else
Damien Miller8899ed32004-09-12 15:18:55 +10001483 return (LL_OTHER);
andre61e67252000-06-04 17:07:49 +00001484}
andre2ff7b5d2000-06-03 14:57:40 +00001485
1486
1487/* open the file (using filemode) and seek to the login entry */
1488static int
andre61e67252000-06-04 17:07:49 +00001489lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1490{
andre2ff7b5d2000-06-03 14:57:40 +00001491 off_t offset;
1492 int type;
1493 char lastlog_file[1024];
1494
1495 type = lastlog_filetype(LASTLOG_FILE);
1496 switch (type) {
Damien Miller8899ed32004-09-12 15:18:55 +10001497 case LL_FILE:
1498 strlcpy(lastlog_file, LASTLOG_FILE,
1499 sizeof(lastlog_file));
1500 break;
1501 case LL_DIR:
1502 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1503 LASTLOG_FILE, li->username);
1504 break;
1505 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001506 logit("%s: %.100s is not a file or directory!", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001507 LASTLOG_FILE);
1508 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001509 }
andre2ff7b5d2000-06-03 14:57:40 +00001510
Damien Miller405dc602003-04-09 21:12:52 +10001511 *fd = open(lastlog_file, filemode, 0600);
Damien Miller8899ed32004-09-12 15:18:55 +10001512 if (*fd < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001513 debug("%s: Couldn't open %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001514 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001515 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001516 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001517
Damien Millere477ef62000-08-15 10:21:17 +10001518 if (type == LL_FILE) {
1519 /* find this uid's offset in the lastlog file */
Kevin Steves52172652001-10-02 00:29:00 +00001520 offset = (off_t) ((long)li->uid * sizeof(struct lastlog));
andre2ff7b5d2000-06-03 14:57:40 +00001521
Damien Miller8899ed32004-09-12 15:18:55 +10001522 if (lseek(*fd, offset, SEEK_SET) != offset) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001523 logit("%s: %s->lseek(): %s", __func__,
1524 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001525 return (0);
Damien Millere477ef62000-08-15 10:21:17 +10001526 }
andre2ff7b5d2000-06-03 14:57:40 +00001527 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001528
Damien Miller8899ed32004-09-12 15:18:55 +10001529 return (1);
andre61e67252000-06-04 17:07:49 +00001530}
andre2ff7b5d2000-06-03 14:57:40 +00001531
1532static int
andre61e67252000-06-04 17:07:49 +00001533lastlog_perform_login(struct logininfo *li)
1534{
andre2ff7b5d2000-06-03 14:57:40 +00001535 struct lastlog last;
1536 int fd;
1537
1538 /* create our struct lastlog */
1539 lastlog_construct(li, &last);
1540
Damien Miller405dc602003-04-09 21:12:52 +10001541 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
Damien Miller8899ed32004-09-12 15:18:55 +10001542 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001543
andre2ff7b5d2000-06-03 14:57:40 +00001544 /* write the entry */
Darren Tucker8661b562003-07-06 15:20:46 +10001545 if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) {
Damien Millerc1132e72000-08-18 14:08:38 +10001546 close(fd);
Damien Miller6b0279c2004-09-12 15:25:17 +10001547 logit("%s: Error writing to %s: %s", __func__,
Damien Millerc1132e72000-08-18 14:08:38 +10001548 LASTLOG_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001549 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001550 }
Damien Millerc1132e72000-08-18 14:08:38 +10001551
1552 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001553 return (1);
andre61e67252000-06-04 17:07:49 +00001554}
andre2ff7b5d2000-06-03 14:57:40 +00001555
andre2ff7b5d2000-06-03 14:57:40 +00001556int
andre61e67252000-06-04 17:07:49 +00001557lastlog_write_entry(struct logininfo *li)
1558{
andre2ff7b5d2000-06-03 14:57:40 +00001559 switch(li->type) {
1560 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001561 return (lastlog_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001562 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001563 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001564 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001565 }
andre61e67252000-06-04 17:07:49 +00001566}
andre2ff7b5d2000-06-03 14:57:40 +00001567
andre2ff7b5d2000-06-03 14:57:40 +00001568static void
andre61e67252000-06-04 17:07:49 +00001569lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1570{
andre2ff7b5d2000-06-03 14:57:40 +00001571 line_fullname(li->line, last->ll_line, sizeof(li->line));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001572 strlcpy(li->hostname, last->ll_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001573 MIN_SIZEOF(li->hostname, last->ll_host));
andre2ff7b5d2000-06-03 14:57:40 +00001574 li->tv_sec = last->ll_time;
andre61e67252000-06-04 17:07:49 +00001575}
andre2ff7b5d2000-06-03 14:57:40 +00001576
andre2ff7b5d2000-06-03 14:57:40 +00001577int
andre61e67252000-06-04 17:07:49 +00001578lastlog_get_entry(struct logininfo *li)
1579{
andre2ff7b5d2000-06-03 14:57:40 +00001580 struct lastlog last;
Damien Miller7df881d2003-01-07 16:46:58 +11001581 int fd, ret;
andre2ff7b5d2000-06-03 14:57:40 +00001582
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001583 if (!lastlog_openseek(li, &fd, O_RDONLY))
Damien Miller7df881d2003-01-07 16:46:58 +11001584 return (0);
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001585
Damien Miller7df881d2003-01-07 16:46:58 +11001586 ret = atomicio(read, fd, &last, sizeof(last));
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001587 close(fd);
1588
Damien Miller7df881d2003-01-07 16:46:58 +11001589 switch (ret) {
1590 case 0:
1591 memset(&last, '\0', sizeof(last));
1592 /* FALLTHRU */
1593 case sizeof(last):
1594 lastlog_populate_entry(li, &last);
1595 return (1);
1596 case -1:
Damien Millera8e06ce2003-11-21 23:48:55 +11001597 error("%s: Error reading from %s: %s", __func__,
Damien Miller7df881d2003-01-07 16:46:58 +11001598 LASTLOG_FILE, strerror(errno));
1599 return (0);
1600 default:
1601 error("%s: Error reading from %s: Expecting %d, got %d",
Darren Tuckerefc17472005-11-22 19:55:13 +11001602 __func__, LASTLOG_FILE, (int)sizeof(last), ret);
Damien Miller7df881d2003-01-07 16:46:58 +11001603 return (0);
1604 }
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001605
Damien Miller7df881d2003-01-07 16:46:58 +11001606 /* NOTREACHED */
1607 return (0);
andre61e67252000-06-04 17:07:49 +00001608}
Damien Millerd5bf3072000-06-07 21:32:13 +10001609#endif /* USE_LASTLOG */
Darren Tucker2fba9932005-02-02 23:30:24 +11001610
1611#ifdef USE_BTMP
1612 /*
1613 * Logs failed login attempts in _PATH_BTMP if that exists.
1614 * The most common login failure is to give password instead of username.
1615 * So the _PATH_BTMP file checked for the correct permission, so that
1616 * only root can read it.
1617 */
1618
1619void
1620record_failed_login(const char *username, const char *hostname,
1621 const char *ttyn)
1622{
1623 int fd;
1624 struct utmp ut;
1625 struct sockaddr_storage from;
Darren Tuckerefc17472005-11-22 19:55:13 +11001626 socklen_t fromlen = sizeof(from);
Darren Tucker2fba9932005-02-02 23:30:24 +11001627 struct sockaddr_in *a4;
1628 struct sockaddr_in6 *a6;
1629 time_t t;
1630 struct stat fst;
1631
1632 if (geteuid() != 0)
1633 return;
1634 if ((fd = open(_PATH_BTMP, O_WRONLY | O_APPEND)) < 0) {
1635 debug("Unable to open the btmp file %s: %s", _PATH_BTMP,
1636 strerror(errno));
1637 return;
1638 }
1639 if (fstat(fd, &fst) < 0) {
1640 logit("%s: fstat of %s failed: %s", __func__, _PATH_BTMP,
1641 strerror(errno));
1642 goto out;
1643 }
1644 if((fst.st_mode & (S_IRWXG | S_IRWXO)) || (fst.st_uid != 0)){
1645 logit("Excess permission or bad ownership on file %s",
1646 _PATH_BTMP);
1647 goto out;
1648 }
1649
1650 memset(&ut, 0, sizeof(ut));
1651 /* strncpy because we don't necessarily want nul termination */
1652 strncpy(ut.ut_user, username, sizeof(ut.ut_user));
1653 strlcpy(ut.ut_line, "ssh:notty", sizeof(ut.ut_line));
1654
1655 time(&t);
1656 ut.ut_time = t; /* ut_time is not always a time_t */
1657 ut.ut_type = LOGIN_PROCESS;
1658 ut.ut_pid = getpid();
1659
1660 /* strncpy because we don't necessarily want nul termination */
1661 strncpy(ut.ut_host, hostname, sizeof(ut.ut_host));
1662
1663 if (packet_connection_is_on_socket() &&
1664 getpeername(packet_get_connection_in(),
1665 (struct sockaddr *)&from, &fromlen) == 0) {
1666 ipv64_normalise_mapped(&from, &fromlen);
1667 if (from.ss_family == AF_INET) {
1668 a4 = (struct sockaddr_in *)&from;
1669 memcpy(&ut.ut_addr, &(a4->sin_addr),
1670 MIN_SIZEOF(ut.ut_addr, a4->sin_addr));
1671 }
1672#ifdef HAVE_ADDR_V6_IN_UTMP
1673 if (from.ss_family == AF_INET6) {
1674 a6 = (struct sockaddr_in6 *)&from;
1675 memcpy(&ut.ut_addr_v6, &(a6->sin6_addr),
1676 MIN_SIZEOF(ut.ut_addr_v6, a6->sin6_addr));
1677 }
1678#endif
1679 }
1680
1681 if (atomicio(vwrite, fd, &ut, sizeof(ut)) != sizeof(ut))
1682 error("Failed to write to %s: %s", _PATH_BTMP,
1683 strerror(errno));
1684
1685out:
1686 close(fd);
1687}
1688#endif /* USE_BTMP */