blob: 7850312b62b757806f0e18ecfa619b1ed439cf31 [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 "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +1000163#include "key.h"
164#include "hostfile.h"
165#include "ssh.h"
andre2ff7b5d2000-06-03 14:57:40 +0000166#include "loginrec.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000167#include "log.h"
168#include "atomicio.h"
Darren Tucker2fba9932005-02-02 23:30:24 +1100169#include "packet.h"
170#include "canohost.h"
Darren Tucker269a1ea2005-02-03 00:20:53 +1100171#include "auth.h"
Darren Tuckera39f83e2005-02-15 22:19:28 +1100172#include "buffer.h"
andre2ff7b5d2000-06-03 14:57:40 +0000173
Ben Lindstromdcca9812000-11-10 03:28:31 +0000174#ifdef HAVE_UTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000175# include <util.h>
Ben Lindstromdcca9812000-11-10 03:28:31 +0000176#endif
andre2ff7b5d2000-06-03 14:57:40 +0000177
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000178#ifdef HAVE_LIBUTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000179# include <libutil.h>
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000180#endif
181
andre2ff7b5d2000-06-03 14:57:40 +0000182/**
183 ** prototypes for helper functions in this file
184 **/
185
186#if HAVE_UTMP_H
andre2ff7b5d2000-06-03 14:57:40 +0000187void set_utmp_time(struct logininfo *li, struct utmp *ut);
188void construct_utmp(struct logininfo *li, struct utmp *ut);
189#endif
190
191#ifdef HAVE_UTMPX_H
andre2ff7b5d2000-06-03 14:57:40 +0000192void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
193void construct_utmpx(struct logininfo *li, struct utmpx *ut);
194#endif
195
196int utmp_write_entry(struct logininfo *li);
197int utmpx_write_entry(struct logininfo *li);
198int wtmp_write_entry(struct logininfo *li);
199int wtmpx_write_entry(struct logininfo *li);
200int lastlog_write_entry(struct logininfo *li);
201int syslogin_write_entry(struct logininfo *li);
202
203int getlast_entry(struct logininfo *li);
204int lastlog_get_entry(struct logininfo *li);
205int wtmp_get_entry(struct logininfo *li);
206int wtmpx_get_entry(struct logininfo *li);
207
Darren Tucker691d5232005-02-15 21:45:57 +1100208extern Buffer loginmsg;
209
andre6bb92372000-06-19 08:20:03 +0000210/* pick the shortest string */
Damien Miller8899ed32004-09-12 15:18:55 +1000211#define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2))
andre6bb92372000-06-19 08:20:03 +0000212
andre2ff7b5d2000-06-03 14:57:40 +0000213/**
214 ** platform-independent login functions
215 **/
216
Damien Miller8899ed32004-09-12 15:18:55 +1000217/*
218 * login_login(struct logininfo *) - Record a login
Kevin Stevesef4eea92001-02-05 12:42:17 +0000219 *
andre6bb92372000-06-19 08:20:03 +0000220 * Call with a pointer to a struct logininfo initialised with
221 * login_init_entry() or login_alloc_entry()
222 *
223 * Returns:
224 * >0 if successful
225 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
226 */
andre61e67252000-06-04 17:07:49 +0000227int
Damien Miller8899ed32004-09-12 15:18:55 +1000228login_login(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000229{
230 li->type = LTYPE_LOGIN;
Damien Miller8899ed32004-09-12 15:18:55 +1000231 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000232}
233
234
Damien Miller8899ed32004-09-12 15:18:55 +1000235/*
236 * login_logout(struct logininfo *) - Record a logout
andre6bb92372000-06-19 08:20:03 +0000237 *
238 * Call as with login_login()
239 *
240 * Returns:
241 * >0 if successful
242 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
243 */
andre61e67252000-06-04 17:07:49 +0000244int
245login_logout(struct logininfo *li)
246{
247 li->type = LTYPE_LOGOUT;
Damien Miller8899ed32004-09-12 15:18:55 +1000248 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000249}
250
Damien Miller8899ed32004-09-12 15:18:55 +1000251/*
252 * login_get_lastlog_time(int) - Retrieve the last login time
andre6bb92372000-06-19 08:20:03 +0000253 *
254 * Retrieve the last login time for the given uid. Will try to use the
255 * system lastlog facilities if they are available, but will fall back
256 * to looking in wtmp/wtmpx if necessary
257 *
258 * Returns:
259 * 0 on failure, or if user has never logged in
260 * Time in seconds from the epoch if successful
261 *
262 * Useful preprocessor symbols:
263 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
264 * info
265 * USE_LASTLOG: If set, indicates the presence of system lastlog
266 * facilities. If this and DISABLE_LASTLOG are not set,
267 * try to retrieve lastlog information from wtmp/wtmpx.
268 */
andre61e67252000-06-04 17:07:49 +0000269unsigned int
270login_get_lastlog_time(const int uid)
271{
272 struct logininfo li;
273
andre6bb92372000-06-19 08:20:03 +0000274 if (login_get_lastlog(&li, uid))
Damien Miller8899ed32004-09-12 15:18:55 +1000275 return (li.tv_sec);
andre6bb92372000-06-19 08:20:03 +0000276 else
Damien Miller8899ed32004-09-12 15:18:55 +1000277 return (0);
andre61e67252000-06-04 17:07:49 +0000278}
279
Damien Miller8899ed32004-09-12 15:18:55 +1000280/*
281 * login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
andre6bb92372000-06-19 08:20:03 +0000282 *
283 * Retrieve a logininfo structure populated (only partially) with
284 * information from the system lastlog data, or from wtmp/wtmpx if no
285 * system lastlog information exists.
286 *
287 * Note this routine must be given a pre-allocated logininfo.
288 *
289 * Returns:
290 * >0: A pointer to your struct logininfo if successful
291 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
andre6bb92372000-06-19 08:20:03 +0000292 */
andre61e67252000-06-04 17:07:49 +0000293struct logininfo *
294login_get_lastlog(struct logininfo *li, const int uid)
295{
andre6bb92372000-06-19 08:20:03 +0000296 struct passwd *pw;
andre6bb92372000-06-19 08:20:03 +0000297
Damien Miller348c9b72000-08-15 10:01:22 +1000298 memset(li, '\0', sizeof(*li));
andre61e67252000-06-04 17:07:49 +0000299 li->uid = uid;
andre6bb92372000-06-19 08:20:03 +0000300
Kevin Stevesef4eea92001-02-05 12:42:17 +0000301 /*
Damien Miller53c5d462000-06-28 00:50:50 +1000302 * If we don't have a 'real' lastlog, we need the username to
andre6bb92372000-06-19 08:20:03 +0000303 * reliably search wtmp(x) for the last login (see
Kevin Stevesef4eea92001-02-05 12:42:17 +0000304 * wtmp_get_entry().)
Damien Miller53c5d462000-06-28 00:50:50 +1000305 */
andre6bb92372000-06-19 08:20:03 +0000306 pw = getpwuid(uid);
Damien Millerdd47aa22000-06-27 11:18:27 +1000307 if (pw == NULL)
Damien Miller6b0279c2004-09-12 15:25:17 +1000308 fatal("%s: Cannot find account for uid %i", __func__, uid);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000309
andre98cabe02000-06-19 09:11:30 +0000310 /* No MIN_SIZEOF here - we absolutely *must not* truncate the
Damien Miller8899ed32004-09-12 15:18:55 +1000311 * username (XXX - so check for trunc!) */
Damien Millerf8af08d2000-06-27 09:40:06 +1000312 strlcpy(li->username, pw->pw_name, sizeof(li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000313
andre61e67252000-06-04 17:07:49 +0000314 if (getlast_entry(li))
Damien Miller8899ed32004-09-12 15:18:55 +1000315 return (li);
andre61e67252000-06-04 17:07:49 +0000316 else
Damien Miller8899ed32004-09-12 15:18:55 +1000317 return (NULL);
andre61e67252000-06-04 17:07:49 +0000318}
319
320
Damien Miller8899ed32004-09-12 15:18:55 +1000321/*
322 * login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
Kevin Stevesef4eea92001-02-05 12:42:17 +0000323 * a logininfo structure
324 *
andre6bb92372000-06-19 08:20:03 +0000325 * This function creates a new struct logininfo, a data structure
326 * meant to carry the information required to portably record login info.
327 *
328 * Returns a pointer to a newly created struct logininfo. If memory
329 * allocation fails, the program halts.
330 */
andre61e67252000-06-04 17:07:49 +0000331struct
332logininfo *login_alloc_entry(int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000333 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000334{
andre2ff7b5d2000-06-03 14:57:40 +0000335 struct logininfo *newli;
336
Damien Miller8899ed32004-09-12 15:18:55 +1000337 newli = xmalloc(sizeof(*newli));
338 login_init_entry(newli, pid, username, hostname, line);
339 return (newli);
andre61e67252000-06-04 17:07:49 +0000340}
andre2ff7b5d2000-06-03 14:57:40 +0000341
342
andre6bb92372000-06-19 08:20:03 +0000343/* login_free_entry(struct logininfo *) - free struct memory */
andre61e67252000-06-04 17:07:49 +0000344void
345login_free_entry(struct logininfo *li)
346{
347 xfree(li);
348}
349
andre2ff7b5d2000-06-03 14:57:40 +0000350
andre6bb92372000-06-19 08:20:03 +0000351/* login_init_entry(struct logininfo *, int, char*, char*, char*)
352 * - initialise a struct logininfo
Kevin Stevesef4eea92001-02-05 12:42:17 +0000353 *
andre6bb92372000-06-19 08:20:03 +0000354 * Populates a new struct logininfo, a data structure meant to carry
355 * the information required to portably record login info.
356 *
357 * Returns: 1
358 */
andre61e67252000-06-04 17:07:49 +0000359int
Kevin Stevesef4eea92001-02-05 12:42:17 +0000360login_init_entry(struct logininfo *li, int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000361 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000362{
Damien Millerf8af08d2000-06-27 09:40:06 +1000363 struct passwd *pw;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000364
Damien Miller348c9b72000-08-15 10:01:22 +1000365 memset(li, 0, sizeof(*li));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000366
andre61e67252000-06-04 17:07:49 +0000367 li->pid = pid;
Damien Millerf8af08d2000-06-27 09:40:06 +1000368
andre2ff7b5d2000-06-03 14:57:40 +0000369 /* set the line information */
andre61e67252000-06-04 17:07:49 +0000370 if (line)
andre2ff7b5d2000-06-03 14:57:40 +0000371 line_fullname(li->line, line, sizeof(li->line));
andre2ff7b5d2000-06-03 14:57:40 +0000372
Damien Millerf8af08d2000-06-27 09:40:06 +1000373 if (username) {
andre2ff7b5d2000-06-03 14:57:40 +0000374 strlcpy(li->username, username, sizeof(li->username));
Damien Millerf8af08d2000-06-27 09:40:06 +1000375 pw = getpwnam(li->username);
Damien Miller8899ed32004-09-12 15:18:55 +1000376 if (pw == NULL) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000377 fatal("%s: Cannot find user \"%s\"", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +1000378 li->username);
379 }
Damien Millerf8af08d2000-06-27 09:40:06 +1000380 li->uid = pw->pw_uid;
381 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000382
andre61e67252000-06-04 17:07:49 +0000383 if (hostname)
andre2ff7b5d2000-06-03 14:57:40 +0000384 strlcpy(li->hostname, hostname, sizeof(li->hostname));
Damien Millerf8af08d2000-06-27 09:40:06 +1000385
Damien Miller8899ed32004-09-12 15:18:55 +1000386 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000387}
388
Damien Miller94cf4c82005-07-17 17:04:47 +1000389/*
Damien Miller8899ed32004-09-12 15:18:55 +1000390 * login_set_current_time(struct logininfo *) - set the current time
andre6bb92372000-06-19 08:20:03 +0000391 *
392 * Set the current time in a logininfo structure. This function is
393 * meant to eliminate the need to deal with system dependencies for
394 * time handling.
395 */
andre2ff7b5d2000-06-03 14:57:40 +0000396void
andre61e67252000-06-04 17:07:49 +0000397login_set_current_time(struct logininfo *li)
398{
andre2ff7b5d2000-06-03 14:57:40 +0000399 struct timeval tv;
400
401 gettimeofday(&tv, NULL);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000402
Damien Millerf8af08d2000-06-27 09:40:06 +1000403 li->tv_sec = tv.tv_sec;
404 li->tv_usec = tv.tv_usec;
andre2ff7b5d2000-06-03 14:57:40 +0000405}
406
andre61e67252000-06-04 17:07:49 +0000407/* copy a sockaddr_* into our logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000408void
andre61e67252000-06-04 17:07:49 +0000409login_set_addr(struct logininfo *li, const struct sockaddr *sa,
Damien Miller8899ed32004-09-12 15:18:55 +1000410 const unsigned int sa_size)
andre61e67252000-06-04 17:07:49 +0000411{
412 unsigned int bufsize = sa_size;
413
414 /* make sure we don't overrun our union */
415 if (sizeof(li->hostaddr) < sa_size)
416 bufsize = sizeof(li->hostaddr);
417
Damien Miller8899ed32004-09-12 15:18:55 +1000418 memcpy(&li->hostaddr.sa, sa, bufsize);
andre2ff7b5d2000-06-03 14:57:40 +0000419}
420
andre2ff7b5d2000-06-03 14:57:40 +0000421
andre61e67252000-06-04 17:07:49 +0000422/**
423 ** login_write: Call low-level recording functions based on autoconf
424 ** results
425 **/
andre2ff7b5d2000-06-03 14:57:40 +0000426int
Damien Miller8899ed32004-09-12 15:18:55 +1000427login_write(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000428{
Damien Millerbac2d8a2000-09-05 16:13:06 +1100429#ifndef HAVE_CYGWIN
Damien Miller8899ed32004-09-12 15:18:55 +1000430 if (geteuid() != 0) {
431 logit("Attempt to write login records by non-root user (aborting)");
432 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000433 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100434#endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000435
andre2ff7b5d2000-06-03 14:57:40 +0000436 /* set the timestamp */
437 login_set_current_time(li);
438#ifdef USE_LOGIN
439 syslogin_write_entry(li);
440#endif
441#ifdef USE_LASTLOG
Damien Miller8899ed32004-09-12 15:18:55 +1000442 if (li->type == LTYPE_LOGIN)
andre2ff7b5d2000-06-03 14:57:40 +0000443 lastlog_write_entry(li);
andre2ff7b5d2000-06-03 14:57:40 +0000444#endif
445#ifdef USE_UTMP
446 utmp_write_entry(li);
447#endif
448#ifdef USE_WTMP
449 wtmp_write_entry(li);
450#endif
451#ifdef USE_UTMPX
452 utmpx_write_entry(li);
453#endif
454#ifdef USE_WTMPX
455 wtmpx_write_entry(li);
456#endif
Darren Tucker397a2f22004-08-15 00:09:11 +1000457#ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN
Damien Miller94cf4c82005-07-17 17:04:47 +1000458 if (li->type == LTYPE_LOGIN &&
Damien Millerb6f72f52005-07-17 17:26:43 +1000459 !sys_auth_record_login(li->username,li->hostname,li->line,
460 &loginmsg))
Darren Tucker397a2f22004-08-15 00:09:11 +1000461 logit("Writing login record failed for %s", li->username);
462#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100463#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100464 if (li->type == LTYPE_LOGIN)
465 audit_session_open(li->line);
466 else if (li->type == LTYPE_LOGOUT)
467 audit_session_close(li->line);
468#endif
Damien Miller8899ed32004-09-12 15:18:55 +1000469 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000470}
471
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000472#ifdef LOGIN_NEEDS_UTMPX
473int
474login_utmp_only(struct logininfo *li)
475{
Damien Millera8e06ce2003-11-21 23:48:55 +1100476 li->type = LTYPE_LOGIN;
Ben Lindstrom9197c592001-10-26 15:56:55 +0000477 login_set_current_time(li);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000478# ifdef USE_UTMP
479 utmp_write_entry(li);
480# endif
481# ifdef USE_WTMP
482 wtmp_write_entry(li);
483# endif
484# ifdef USE_UTMPX
485 utmpx_write_entry(li);
486# endif
487# ifdef USE_WTMPX
488 wtmpx_write_entry(li);
489# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000490 return (0);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000491}
492#endif
493
andre2ff7b5d2000-06-03 14:57:40 +0000494/**
andre61e67252000-06-04 17:07:49 +0000495 ** getlast_entry: Call low-level functions to retrieve the last login
496 ** time.
andre2ff7b5d2000-06-03 14:57:40 +0000497 **/
498
andre61e67252000-06-04 17:07:49 +0000499/* take the uid in li and return the last login time */
500int
501getlast_entry(struct logininfo *li)
502{
503#ifdef USE_LASTLOG
Damien Miller53c5d462000-06-28 00:50:50 +1000504 return(lastlog_get_entry(li));
Damien Millerdd47aa22000-06-27 11:18:27 +1000505#else /* !USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000506
Damien Miller8899ed32004-09-12 15:18:55 +1000507#if defined(DISABLE_LASTLOG)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000508 /* On some systems we shouldn't even try to obtain last login
andreecaabf12000-06-12 22:21:44 +0000509 * time, e.g. AIX */
Damien Miller8899ed32004-09-12 15:18:55 +1000510 return (0);
511# elif defined(USE_WTMP) && \
512 (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
andre61e67252000-06-04 17:07:49 +0000513 /* retrieve last login time from utmp */
Damien Millerdd47aa22000-06-27 11:18:27 +1000514 return (wtmp_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000515# elif defined(USE_WTMPX) && \
516 (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
andre61e67252000-06-04 17:07:49 +0000517 /* If wtmp isn't available, try wtmpx */
Damien Millerdd47aa22000-06-27 11:18:27 +1000518 return (wtmpx_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000519# else
andre61e67252000-06-04 17:07:49 +0000520 /* Give up: No means of retrieving last login time */
Damien Miller8899ed32004-09-12 15:18:55 +1000521 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000522# endif /* DISABLE_LASTLOG */
Damien Millerdd47aa22000-06-27 11:18:27 +1000523#endif /* USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000524}
525
526
527
andre2ff7b5d2000-06-03 14:57:40 +0000528/*
andre61e67252000-06-04 17:07:49 +0000529 * 'line' string utility functions
530 *
531 * These functions process the 'line' string into one of three forms:
532 *
andre2ff7b5d2000-06-03 14:57:40 +0000533 * 1. The full filename (including '/dev')
534 * 2. The stripped name (excluding '/dev')
andre61e67252000-06-04 17:07:49 +0000535 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
536 * /dev/pts/1 -> ts/1 )
andre2ff7b5d2000-06-03 14:57:40 +0000537 *
538 * Form 3 is used on some systems to identify a .tmp.? entry when
539 * attempting to remove it. Typically both addition and removal is
andre61e67252000-06-04 17:07:49 +0000540 * performed by one application - say, sshd - so as long as the choice
541 * uniquely identifies a terminal it's ok.
andre2ff7b5d2000-06-03 14:57:40 +0000542 */
543
544
Damien Miller8899ed32004-09-12 15:18:55 +1000545/*
546 * line_fullname(): add the leading '/dev/' if it doesn't exist make
547 * sure dst has enough space, if not just copy src (ugh)
548 */
andre2ff7b5d2000-06-03 14:57:40 +0000549char *
Damien Miller52c8afe2005-06-19 10:19:43 +1000550line_fullname(char *dst, const char *src, u_int dstsize)
andre61e67252000-06-04 17:07:49 +0000551{
andre2ff7b5d2000-06-03 14:57:40 +0000552 memset(dst, '\0', dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000553 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
andre2ff7b5d2000-06-03 14:57:40 +0000554 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000555 else {
Damien Miller1a132252000-06-13 21:23:17 +1000556 strlcpy(dst, "/dev/", dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000557 strlcat(dst, src, dstsize);
558 }
Damien Miller8899ed32004-09-12 15:18:55 +1000559 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000560}
561
andre61e67252000-06-04 17:07:49 +0000562/* line_stripname(): strip the leading '/dev' if it exists, return dst */
andre2ff7b5d2000-06-03 14:57:40 +0000563char *
andre61e67252000-06-04 17:07:49 +0000564line_stripname(char *dst, const char *src, int dstsize)
565{
andre2ff7b5d2000-06-03 14:57:40 +0000566 memset(dst, '\0', dstsize);
567 if (strncmp(src, "/dev/", 5) == 0)
Damien Millerf5a81472000-09-30 21:34:44 +1100568 strlcpy(dst, src + 5, dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000569 else
570 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000571 return (dst);
andre61e67252000-06-04 17:07:49 +0000572}
573
Damien Miller94cf4c82005-07-17 17:04:47 +1000574/*
Damien Miller8899ed32004-09-12 15:18:55 +1000575 * line_abbrevname(): Return the abbreviated (usually four-character)
andre61e67252000-06-04 17:07:49 +0000576 * form of the line (Just use the last <dstsize> characters of the
577 * full name.)
578 *
579 * NOTE: use strncpy because we do NOT necessarily want zero
Damien Miller8899ed32004-09-12 15:18:55 +1000580 * termination
581 */
andre2ff7b5d2000-06-03 14:57:40 +0000582char *
Kevin Stevesef4eea92001-02-05 12:42:17 +0000583line_abbrevname(char *dst, const char *src, int dstsize)
Damien Millerdd47aa22000-06-27 11:18:27 +1000584{
585 size_t len;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000586
andre2ff7b5d2000-06-03 14:57:40 +0000587 memset(dst, '\0', dstsize);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000588
Damien Miller8e81ed32000-07-01 13:17:42 +1000589 /* Always skip prefix if present */
590 if (strncmp(src, "/dev/", 5) == 0)
591 src += 5;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000592
Damien Millerf1b9d112002-04-23 23:09:19 +1000593#ifdef WITH_ABBREV_NO_TTY
594 if (strncmp(src, "tty", 3) == 0)
595 src += 3;
596#endif
597
Damien Millerdd47aa22000-06-27 11:18:27 +1000598 len = strlen(src);
599
Damien Miller8e81ed32000-07-01 13:17:42 +1000600 if (len > 0) {
601 if (((int)len - dstsize) > 0)
602 src += ((int)len - dstsize);
603
604 /* note: _don't_ change this to strlcpy */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000605 strncpy(dst, src, (size_t)dstsize);
Damien Millerdd47aa22000-06-27 11:18:27 +1000606 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000607
Damien Miller8899ed32004-09-12 15:18:55 +1000608 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000609}
610
andre2ff7b5d2000-06-03 14:57:40 +0000611/**
612 ** utmp utility functions
andre61e67252000-06-04 17:07:49 +0000613 **
614 ** These functions manipulate struct utmp, taking system differences
615 ** into account.
andre2ff7b5d2000-06-03 14:57:40 +0000616 **/
617
618#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
619
andre2ff7b5d2000-06-03 14:57:40 +0000620/* build the utmp structure */
621void
andre61e67252000-06-04 17:07:49 +0000622set_utmp_time(struct logininfo *li, struct utmp *ut)
623{
Damien Miller8899ed32004-09-12 15:18:55 +1000624# if defined(HAVE_TV_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000625 ut->ut_tv.tv_sec = li->tv_sec;
626 ut->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000627# elif defined(HAVE_TIME_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000628 ut->ut_time = li->tv_sec;
Damien Millerdd47aa22000-06-27 11:18:27 +1000629# endif
andre2ff7b5d2000-06-03 14:57:40 +0000630}
631
632void
633construct_utmp(struct logininfo *li,
andre61e67252000-06-04 17:07:49 +0000634 struct utmp *ut)
635{
Damien Miller02e16ad2003-01-03 14:42:27 +1100636# ifdef HAVE_ADDR_V6_IN_UTMP
637 struct sockaddr_in6 *sa6;
Damien Miller8899ed32004-09-12 15:18:55 +1000638# endif
639
Damien Miller348c9b72000-08-15 10:01:22 +1000640 memset(ut, '\0', sizeof(*ut));
andre6bb92372000-06-19 08:20:03 +0000641
642 /* First fill out fields used for both logins and logouts */
643
Damien Millerdd47aa22000-06-27 11:18:27 +1000644# ifdef HAVE_ID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000645 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +1000646# endif
andre2ff7b5d2000-06-03 14:57:40 +0000647
Damien Millerdd47aa22000-06-27 11:18:27 +1000648# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +0000649 /* This is done here to keep utmp constants out of struct logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000650 switch (li->type) {
651 case LTYPE_LOGIN:
652 ut->ut_type = USER_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700653#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000654 cray_set_tmpdir(ut);
655#endif
andre2ff7b5d2000-06-03 14:57:40 +0000656 break;
657 case LTYPE_LOGOUT:
658 ut->ut_type = DEAD_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700659#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000660 cray_retain_utmp(ut, li->pid);
661#endif
andre2ff7b5d2000-06-03 14:57:40 +0000662 break;
663 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000664# endif
andre6bb92372000-06-19 08:20:03 +0000665 set_utmp_time(li, ut);
andre2ff7b5d2000-06-03 14:57:40 +0000666
andre6bb92372000-06-19 08:20:03 +0000667 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +1000668
669# ifdef HAVE_PID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000670 ut->ut_pid = li->pid;
Damien Millerdd47aa22000-06-27 11:18:27 +1000671# endif
andre6bb92372000-06-19 08:20:03 +0000672
673 /* If we're logging out, leave all other fields blank */
674 if (li->type == LTYPE_LOGOUT)
Damien Miller8899ed32004-09-12 15:18:55 +1000675 return;
andre6bb92372000-06-19 08:20:03 +0000676
Damien Millerdd47aa22000-06-27 11:18:27 +1000677 /*
678 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000679 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000680 */
andre6bb92372000-06-19 08:20:03 +0000681
682 /* Use strncpy because we don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000683 strncpy(ut->ut_name, li->username,
684 MIN_SIZEOF(ut->ut_name, li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000685# ifdef HAVE_HOST_IN_UTMP
Damien Miller8899ed32004-09-12 15:18:55 +1000686 strncpy(ut->ut_host, li->hostname,
687 MIN_SIZEOF(ut->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000688# endif
689# ifdef HAVE_ADDR_IN_UTMP
andre61e67252000-06-04 17:07:49 +0000690 /* this is just a 32-bit IP address */
691 if (li->hostaddr.sa.sa_family == AF_INET)
692 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000693# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100694# ifdef HAVE_ADDR_V6_IN_UTMP
695 /* this is just a 128-bit IPv6 address */
696 if (li->hostaddr.sa.sa_family == AF_INET6) {
697 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
698 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
699 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
700 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
701 ut->ut_addr_v6[1] = 0;
702 ut->ut_addr_v6[2] = 0;
703 ut->ut_addr_v6[3] = 0;
704 }
705 }
706# endif
andre61e67252000-06-04 17:07:49 +0000707}
Damien Millerdd47aa22000-06-27 11:18:27 +1000708#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
andre61e67252000-06-04 17:07:49 +0000709
andre2ff7b5d2000-06-03 14:57:40 +0000710/**
711 ** utmpx utility functions
andre61e67252000-06-04 17:07:49 +0000712 **
713 ** These functions manipulate struct utmpx, accounting for system
714 ** variations.
andre2ff7b5d2000-06-03 14:57:40 +0000715 **/
716
717#if defined(USE_UTMPX) || defined (USE_WTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000718/* build the utmpx structure */
719void
andre61e67252000-06-04 17:07:49 +0000720set_utmpx_time(struct logininfo *li, struct utmpx *utx)
721{
Damien Miller8899ed32004-09-12 15:18:55 +1000722# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000723 utx->ut_tv.tv_sec = li->tv_sec;
724 utx->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000725# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000726 utx->ut_time = li->tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +1000727# endif
andre2ff7b5d2000-06-03 14:57:40 +0000728}
729
andre61e67252000-06-04 17:07:49 +0000730void
731construct_utmpx(struct logininfo *li, struct utmpx *utx)
732{
Damien Miller02e16ad2003-01-03 14:42:27 +1100733# ifdef HAVE_ADDR_V6_IN_UTMP
734 struct sockaddr_in6 *sa6;
735# endif
Damien Miller348c9b72000-08-15 10:01:22 +1000736 memset(utx, '\0', sizeof(*utx));
Damien Miller8899ed32004-09-12 15:18:55 +1000737
Damien Miller8e81ed32000-07-01 13:17:42 +1000738# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000739 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
Damien Miller8e81ed32000-07-01 13:17:42 +1000740# endif
andre2ff7b5d2000-06-03 14:57:40 +0000741
742 /* this is done here to keep utmp constants out of loginrec.h */
743 switch (li->type) {
744 case LTYPE_LOGIN:
745 utx->ut_type = USER_PROCESS;
746 break;
747 case LTYPE_LOGOUT:
748 utx->ut_type = DEAD_PROCESS;
749 break;
750 }
andre2ff7b5d2000-06-03 14:57:40 +0000751 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
andre2ff7b5d2000-06-03 14:57:40 +0000752 set_utmpx_time(li, utx);
andre6bb92372000-06-19 08:20:03 +0000753 utx->ut_pid = li->pid;
Damien Miller8899ed32004-09-12 15:18:55 +1000754
Tim Ricee06ae4a2002-02-24 17:56:46 -0800755 /* strncpy(): Don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000756 strncpy(utx->ut_name, li->username,
757 MIN_SIZEOF(utx->ut_name, li->username));
andre6bb92372000-06-19 08:20:03 +0000758
759 if (li->type == LTYPE_LOGOUT)
760 return;
761
Damien Millerdd47aa22000-06-27 11:18:27 +1000762 /*
763 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000764 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000765 */
andre6bb92372000-06-19 08:20:03 +0000766
Damien Millerdd47aa22000-06-27 11:18:27 +1000767# ifdef HAVE_HOST_IN_UTMPX
Damien Miller8899ed32004-09-12 15:18:55 +1000768 strncpy(utx->ut_host, li->hostname,
769 MIN_SIZEOF(utx->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000770# endif
771# ifdef HAVE_ADDR_IN_UTMPX
Damien Millerd6f204d2000-09-23 13:57:27 +1100772 /* this is just a 32-bit IP address */
773 if (li->hostaddr.sa.sa_family == AF_INET)
774 utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Damien Millerdd47aa22000-06-27 11:18:27 +1000775# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100776# ifdef HAVE_ADDR_V6_IN_UTMP
777 /* this is just a 128-bit IPv6 address */
778 if (li->hostaddr.sa.sa_family == AF_INET6) {
779 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
780 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
781 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
782 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
783 ut->ut_addr_v6[1] = 0;
784 ut->ut_addr_v6[2] = 0;
785 ut->ut_addr_v6[3] = 0;
786 }
787 }
788# endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000789# ifdef HAVE_SYSLEN_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +0000790 /* ut_syslen is the length of the utx_host string */
791 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +1000792# endif
andre61e67252000-06-04 17:07:49 +0000793}
Damien Millerdd47aa22000-06-27 11:18:27 +1000794#endif /* USE_UTMPX || USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +0000795
796/**
andre61e67252000-06-04 17:07:49 +0000797 ** Low-level utmp functions
andre2ff7b5d2000-06-03 14:57:40 +0000798 **/
799
800/* FIXME: (ATL) utmp_write_direct needs testing */
andre2ff7b5d2000-06-03 14:57:40 +0000801#ifdef USE_UTMP
802
andre2ff7b5d2000-06-03 14:57:40 +0000803/* if we can, use pututline() etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000804# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
805 defined(HAVE_PUTUTLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000806# define UTMP_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000807# endif
andre2ff7b5d2000-06-03 14:57:40 +0000808
809
810/* write a utmp entry with the system's help (pututline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000811# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000812static int
andre61e67252000-06-04 17:07:49 +0000813utmp_write_library(struct logininfo *li, struct utmp *ut)
814{
andre2ff7b5d2000-06-03 14:57:40 +0000815 setutent();
816 pututline(ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000817# ifdef HAVE_ENDUTENT
andre2ff7b5d2000-06-03 14:57:40 +0000818 endutent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000819# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000820 return (1);
andre61e67252000-06-04 17:07:49 +0000821}
Damien Millerdd47aa22000-06-27 11:18:27 +1000822# else /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000823
Damien Miller94cf4c82005-07-17 17:04:47 +1000824/*
Damien Miller8899ed32004-09-12 15:18:55 +1000825 * Write a utmp entry direct to the file
826 * This is a slightly modification of code in OpenBSD's login.c
827 */
andre2ff7b5d2000-06-03 14:57:40 +0000828static int
andre61e67252000-06-04 17:07:49 +0000829utmp_write_direct(struct logininfo *li, struct utmp *ut)
830{
andre2ff7b5d2000-06-03 14:57:40 +0000831 struct utmp old_ut;
832 register int fd;
833 int tty;
834
andre6bb92372000-06-19 08:20:03 +0000835 /* FIXME: (ATL) ttyslot() needs local implementation */
Damien Miller348c9b72000-08-15 10:01:22 +1000836
Damien Millere5192fa2000-08-29 14:30:37 +1100837#if defined(HAVE_GETTTYENT)
Damien Miller8899ed32004-09-12 15:18:55 +1000838 struct ttyent *ty;
Damien Miller348c9b72000-08-15 10:01:22 +1000839
840 tty=0;
Damien Miller348c9b72000-08-15 10:01:22 +1000841 setttyent();
Damien Miller8899ed32004-09-12 15:18:55 +1000842 while (NULL != (ty = getttyent())) {
Damien Miller348c9b72000-08-15 10:01:22 +1000843 tty++;
844 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
845 break;
846 }
847 endttyent();
848
Damien Miller8899ed32004-09-12 15:18:55 +1000849 if (NULL == ty) {
Damien Miller81409592004-08-15 19:12:52 +1000850 logit("%s: tty not found", __func__);
851 return (0);
Damien Miller348c9b72000-08-15 10:01:22 +1000852 }
853#else /* FIXME */
854
andre2ff7b5d2000-06-03 14:57:40 +0000855 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
856
Damien Millere5192fa2000-08-29 14:30:37 +1100857#endif /* HAVE_GETTTYENT */
Damien Miller348c9b72000-08-15 10:01:22 +1000858
andre2ff7b5d2000-06-03 14:57:40 +0000859 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
Damien Miller81409592004-08-15 19:12:52 +1000860 off_t pos, ret;
861
862 pos = (off_t)tty * sizeof(struct utmp);
863 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000864 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000865 return (0);
866 }
867 if (ret != pos) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000868 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Millerb0419f22004-08-23 21:53:28 +1000869 __func__, tty, UTMP_FILE);
Damien Miller81409592004-08-15 19:12:52 +1000870 return (0);
871 }
andre2ff7b5d2000-06-03 14:57:40 +0000872 /*
873 * Prevent luser from zero'ing out ut_host.
874 * If the new ut_line is empty but the old one is not
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000875 * and ut_line and ut_name match, preserve the old ut_line.
andre2ff7b5d2000-06-03 14:57:40 +0000876 */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000877 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
Damien Miller8899ed32004-09-12 15:18:55 +1000878 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
879 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
880 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0))
881 memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000882
Damien Miller81409592004-08-15 19:12:52 +1000883 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000884 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000885 return (0);
886 }
887 if (ret != pos) {
Damien Millerb0419f22004-08-23 21:53:28 +1000888 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Miller81409592004-08-15 19:12:52 +1000889 __func__, tty, UTMP_FILE);
890 return (0);
891 }
Damien Miller8899ed32004-09-12 15:18:55 +1000892 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
Damien Miller81409592004-08-15 19:12:52 +1000893 logit("%s: error writing %s: %s", __func__,
andre6bb92372000-06-19 08:20:03 +0000894 UTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +1000895 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000896
Damien Miller8899ed32004-09-12 15:18:55 +1000897 close(fd);
898 return (1);
Damien Miller53c5d462000-06-28 00:50:50 +1000899 } else {
Damien Miller8899ed32004-09-12 15:18:55 +1000900 return (0);
Damien Miller53c5d462000-06-28 00:50:50 +1000901 }
andre61e67252000-06-04 17:07:49 +0000902}
Damien Millerdd47aa22000-06-27 11:18:27 +1000903# endif /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000904
905static int
andre61e67252000-06-04 17:07:49 +0000906utmp_perform_login(struct logininfo *li)
907{
andre2ff7b5d2000-06-03 14:57:40 +0000908 struct utmp ut;
909
910 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000911# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000912 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000913 logit("%s: utmp_write_library() 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# else
andre2ff7b5d2000-06-03 14:57:40 +0000917 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000918 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000919 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000920 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000921# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000922 return (1);
andre61e67252000-06-04 17:07:49 +0000923}
andre2ff7b5d2000-06-03 14:57:40 +0000924
925
926static int
andre61e67252000-06-04 17:07:49 +0000927utmp_perform_logout(struct logininfo *li)
928{
andre2ff7b5d2000-06-03 14:57:40 +0000929 struct utmp ut;
930
andre6bb92372000-06-19 08:20:03 +0000931 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000932# ifdef UTMP_USE_LIBRARY
andre6bb92372000-06-19 08:20:03 +0000933 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000934 logit("%s: utmp_write_library() 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# else
andre6bb92372000-06-19 08:20:03 +0000938 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000939 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000940 return (0);
andre6bb92372000-06-19 08:20:03 +0000941 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000942# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000943 return (1);
andre61e67252000-06-04 17:07:49 +0000944}
andre2ff7b5d2000-06-03 14:57:40 +0000945
946
947int
andre61e67252000-06-04 17:07:49 +0000948utmp_write_entry(struct logininfo *li)
949{
andre2ff7b5d2000-06-03 14:57:40 +0000950 switch(li->type) {
951 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +1000952 return (utmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +0000953
954 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +1000955 return (utmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +0000956
957 default:
Damien Miller6b0279c2004-09-12 15:25:17 +1000958 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000959 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000960 }
andre61e67252000-06-04 17:07:49 +0000961}
Damien Millerdd47aa22000-06-27 11:18:27 +1000962#endif /* USE_UTMP */
andre2ff7b5d2000-06-03 14:57:40 +0000963
964
965/**
andre61e67252000-06-04 17:07:49 +0000966 ** Low-level utmpx functions
andre2ff7b5d2000-06-03 14:57:40 +0000967 **/
968
969/* not much point if we don't want utmpx entries */
970#ifdef USE_UTMPX
971
andre2ff7b5d2000-06-03 14:57:40 +0000972/* if we have the wherewithall, use pututxline etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000973# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
974 defined(HAVE_PUTUTXLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000975# define UTMPX_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000976# endif
andre2ff7b5d2000-06-03 14:57:40 +0000977
978
979/* write a utmpx entry with the system's help (pututxline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000980# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000981static int
andre61e67252000-06-04 17:07:49 +0000982utmpx_write_library(struct logininfo *li, struct utmpx *utx)
983{
andre2ff7b5d2000-06-03 14:57:40 +0000984 setutxent();
985 pututxline(utx);
986
Damien Millerdd47aa22000-06-27 11:18:27 +1000987# ifdef HAVE_ENDUTXENT
andre2ff7b5d2000-06-03 14:57:40 +0000988 endutxent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000989# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000990 return (1);
andre61e67252000-06-04 17:07:49 +0000991}
andre2ff7b5d2000-06-03 14:57:40 +0000992
Damien Millerdd47aa22000-06-27 11:18:27 +1000993# else /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000994
995/* write a utmp entry direct to the file */
996static int
andre61e67252000-06-04 17:07:49 +0000997utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000998{
Damien Miller6b0279c2004-09-12 15:25:17 +1000999 logit("%s: not implemented!", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001000 return (0);
andre61e67252000-06-04 17:07:49 +00001001}
Damien Millerdd47aa22000-06-27 11:18:27 +10001002# endif /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +00001003
1004static int
andre61e67252000-06-04 17:07:49 +00001005utmpx_perform_login(struct logininfo *li)
1006{
andre2ff7b5d2000-06-03 14:57:40 +00001007 struct utmpx utx;
1008
1009 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001010# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001011 if (!utmpx_write_library(li, &utx)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001012 logit("%s: utmp_write_library() 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# else
andre2ff7b5d2000-06-03 14:57:40 +00001016 if (!utmpx_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001017 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001018 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001019 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001020# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001021 return (1);
andre61e67252000-06-04 17:07:49 +00001022}
andre2ff7b5d2000-06-03 14:57:40 +00001023
1024
1025static int
andre61e67252000-06-04 17:07:49 +00001026utmpx_perform_logout(struct logininfo *li)
1027{
andre2ff7b5d2000-06-03 14:57:40 +00001028 struct utmpx utx;
1029
Tim Ricee06ae4a2002-02-24 17:56:46 -08001030 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001031# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001032 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +10001033# endif
1034# ifdef HAVE_TYPE_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001035 utx.ut_type = DEAD_PROCESS;
Damien Millerdd47aa22000-06-27 11:18:27 +10001036# endif
andre2ff7b5d2000-06-03 14:57:40 +00001037
Damien Millerdd47aa22000-06-27 11:18:27 +10001038# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001039 utmpx_write_library(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001040# else
andre2ff7b5d2000-06-03 14:57:40 +00001041 utmpx_write_direct(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001042# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001043 return (1);
andre61e67252000-06-04 17:07:49 +00001044}
andre2ff7b5d2000-06-03 14:57:40 +00001045
andre2ff7b5d2000-06-03 14:57:40 +00001046int
andre61e67252000-06-04 17:07:49 +00001047utmpx_write_entry(struct logininfo *li)
1048{
andre2ff7b5d2000-06-03 14:57:40 +00001049 switch(li->type) {
1050 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001051 return (utmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001052 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001053 return (utmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001054 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001055 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001056 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001057 }
andre61e67252000-06-04 17:07:49 +00001058}
Damien Millerdd47aa22000-06-27 11:18:27 +10001059#endif /* USE_UTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001060
1061
1062/**
andre61e67252000-06-04 17:07:49 +00001063 ** Low-level wtmp functions
andre2ff7b5d2000-06-03 14:57:40 +00001064 **/
1065
Kevin Stevesef4eea92001-02-05 12:42:17 +00001066#ifdef USE_WTMP
andre2ff7b5d2000-06-03 14:57:40 +00001067
Damien Miller94cf4c82005-07-17 17:04:47 +10001068/*
Damien Miller8899ed32004-09-12 15:18:55 +10001069 * Write a wtmp entry direct to the end of the file
1070 * This is a slight modification of code in OpenBSD's logwtmp.c
1071 */
andre2ff7b5d2000-06-03 14:57:40 +00001072static int
andre61e67252000-06-04 17:07:49 +00001073wtmp_write(struct logininfo *li, struct utmp *ut)
1074{
andre2ff7b5d2000-06-03 14:57:40 +00001075 struct stat buf;
1076 int fd, ret = 1;
1077
1078 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001079 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001080 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001081 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001082 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001083 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001084 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
andre2ff7b5d2000-06-03 14:57:40 +00001085 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001086 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001087 WTMP_FILE, strerror(errno));
1088 ret = 0;
1089 }
Damien Miller8899ed32004-09-12 15:18:55 +10001090 close(fd);
1091 return (ret);
andre61e67252000-06-04 17:07:49 +00001092}
andre2ff7b5d2000-06-03 14:57:40 +00001093
andre2ff7b5d2000-06-03 14:57:40 +00001094static int
Damien Millerdd47aa22000-06-27 11:18:27 +10001095wtmp_perform_login(struct logininfo *li)
1096{
andre2ff7b5d2000-06-03 14:57:40 +00001097 struct utmp ut;
1098
1099 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001100 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001101}
andre2ff7b5d2000-06-03 14:57:40 +00001102
1103
1104static int
andre61e67252000-06-04 17:07:49 +00001105wtmp_perform_logout(struct logininfo *li)
1106{
andre2ff7b5d2000-06-03 14:57:40 +00001107 struct utmp ut;
1108
1109 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001110 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001111}
andre2ff7b5d2000-06-03 14:57:40 +00001112
1113
1114int
andre61e67252000-06-04 17:07:49 +00001115wtmp_write_entry(struct logininfo *li)
1116{
andre2ff7b5d2000-06-03 14:57:40 +00001117 switch(li->type) {
1118 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001119 return (wtmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001120 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001121 return (wtmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001122 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001123 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001124 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001125 }
andre61e67252000-06-04 17:07:49 +00001126}
andre2ff7b5d2000-06-03 14:57:40 +00001127
1128
Damien Miller94cf4c82005-07-17 17:04:47 +10001129/*
Damien Miller8899ed32004-09-12 15:18:55 +10001130 * Notes on fetching login data from wtmp/wtmpx
Kevin Stevesef4eea92001-02-05 12:42:17 +00001131 *
andre6bb92372000-06-19 08:20:03 +00001132 * Logouts are usually recorded with (amongst other things) a blank
1133 * username on a given tty line. However, some systems (HP-UX is one)
1134 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1135 *
1136 * Since we're only looking for logins here, we know that the username
1137 * must be set correctly. On systems that leave it in, we check for
1138 * ut_type==USER_PROCESS (indicating a login.)
1139 *
1140 * Portability: Some systems may set something other than USER_PROCESS
1141 * to indicate a login process. I don't know of any as I write. Also,
1142 * it's possible that some systems may both leave the username in
1143 * place and not have ut_type.
1144 */
1145
andre6bb92372000-06-19 08:20:03 +00001146/* return true if this wtmp entry indicates a login */
1147static int
1148wtmp_islogin(struct logininfo *li, struct utmp *ut)
1149{
Kevin Stevesef4eea92001-02-05 12:42:17 +00001150 if (strncmp(li->username, ut->ut_name,
Damien Miller8899ed32004-09-12 15:18:55 +10001151 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001152# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001153 if (ut->ut_type & USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001154 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001155# else
Damien Miller8899ed32004-09-12 15:18:55 +10001156 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001157# endif
andre6bb92372000-06-19 08:20:03 +00001158 }
Damien Miller8899ed32004-09-12 15:18:55 +10001159 return (0);
andre6bb92372000-06-19 08:20:03 +00001160}
1161
andre2ff7b5d2000-06-03 14:57:40 +00001162int
andre61e67252000-06-04 17:07:49 +00001163wtmp_get_entry(struct logininfo *li)
1164{
andre2ff7b5d2000-06-03 14:57:40 +00001165 struct stat st;
1166 struct utmp ut;
Damien Miller8899ed32004-09-12 15:18:55 +10001167 int fd, found = 0;
andre6bb92372000-06-19 08:20:03 +00001168
1169 /* Clear the time entries in our logininfo */
1170 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001171
1172 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001173 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001174 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001175 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001176 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001177 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001178 logit("%s: couldn't stat %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001179 WTMP_FILE, strerror(errno));
1180 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001181 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001182 }
andre2ff7b5d2000-06-03 14:57:40 +00001183
andre6bb92372000-06-19 08:20:03 +00001184 /* Seek to the start of the last struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001185 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
andre6bb92372000-06-19 08:20:03 +00001186 /* Looks like we've got a fresh wtmp file */
1187 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001188 return (0);
andre6bb92372000-06-19 08:20:03 +00001189 }
1190
1191 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001192 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001193 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001194 WTMP_FILE, strerror(errno));
1195 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001196 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001197 }
andre6bb92372000-06-19 08:20:03 +00001198 if ( wtmp_islogin(li, &ut) ) {
1199 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001200 /*
1201 * We've already checked for a time in struct
1202 * utmp, in login_getlast()
1203 */
Damien Millerdd47aa22000-06-27 11:18:27 +10001204# ifdef HAVE_TIME_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +00001205 li->tv_sec = ut.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001206# else
andre2ff7b5d2000-06-03 14:57:40 +00001207# if HAVE_TV_IN_UTMP
1208 li->tv_sec = ut.ut_tv.tv_sec;
1209# endif
Damien Millerdd47aa22000-06-27 11:18:27 +10001210# endif
andre6bb92372000-06-19 08:20:03 +00001211 line_fullname(li->line, ut.ut_line,
Damien Miller8899ed32004-09-12 15:18:55 +10001212 MIN_SIZEOF(li->line, ut.ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +10001213# ifdef HAVE_HOST_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001214 strlcpy(li->hostname, ut.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001215 MIN_SIZEOF(li->hostname, ut.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001216# endif
andre6bb92372000-06-19 08:20:03 +00001217 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001218 }
andre6bb92372000-06-19 08:20:03 +00001219 /* Seek back 2 x struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001220 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
andre6bb92372000-06-19 08:20:03 +00001221 /* We've found the start of the file, so quit */
Damien Miller8899ed32004-09-12 15:18:55 +10001222 close(fd);
1223 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001224 }
andre6bb92372000-06-19 08:20:03 +00001225 }
1226
1227 /* We found an entry. Tidy up and return */
1228 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001229 return (1);
andre61e67252000-06-04 17:07:49 +00001230}
Damien Millerdd47aa22000-06-27 11:18:27 +10001231# endif /* USE_WTMP */
andre2ff7b5d2000-06-03 14:57:40 +00001232
1233
1234/**
andre61e67252000-06-04 17:07:49 +00001235 ** Low-level wtmpx functions
andre2ff7b5d2000-06-03 14:57:40 +00001236 **/
1237
1238#ifdef USE_WTMPX
Damien Miller8899ed32004-09-12 15:18:55 +10001239/*
1240 * Write a wtmpx entry direct to the end of the file
1241 * This is a slight modification of code in OpenBSD's logwtmp.c
1242 */
andre2ff7b5d2000-06-03 14:57:40 +00001243static int
andre61e67252000-06-04 17:07:49 +00001244wtmpx_write(struct logininfo *li, struct utmpx *utx)
1245{
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001246#ifndef HAVE_UPDWTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001247 struct stat buf;
1248 int fd, ret = 1;
1249
1250 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001251 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001252 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001253 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001254 }
1255
Kevin Stevesef4eea92001-02-05 12:42:17 +00001256 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001257 if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
andre2ff7b5d2000-06-03 14:57:40 +00001258 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001259 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001260 WTMPX_FILE, strerror(errno));
1261 ret = 0;
1262 }
Damien Miller8899ed32004-09-12 15:18:55 +10001263 close(fd);
andre2ff7b5d2000-06-03 14:57:40 +00001264
Damien Miller8899ed32004-09-12 15:18:55 +10001265 return (ret);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001266#else
1267 updwtmpx(WTMPX_FILE, utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001268 return (1);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001269#endif
andre61e67252000-06-04 17:07:49 +00001270}
andre2ff7b5d2000-06-03 14:57:40 +00001271
1272
1273static int
andre61e67252000-06-04 17:07:49 +00001274wtmpx_perform_login(struct logininfo *li)
1275{
andre2ff7b5d2000-06-03 14:57:40 +00001276 struct utmpx utx;
1277
1278 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001279 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001280}
andre2ff7b5d2000-06-03 14:57:40 +00001281
1282
1283static int
andre61e67252000-06-04 17:07:49 +00001284wtmpx_perform_logout(struct logininfo *li)
1285{
andre2ff7b5d2000-06-03 14:57:40 +00001286 struct utmpx utx;
1287
1288 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001289 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001290}
andre2ff7b5d2000-06-03 14:57:40 +00001291
1292
1293int
andre61e67252000-06-04 17:07:49 +00001294wtmpx_write_entry(struct logininfo *li)
1295{
andre2ff7b5d2000-06-03 14:57:40 +00001296 switch(li->type) {
1297 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001298 return (wtmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001299 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001300 return (wtmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001301 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001302 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001303 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001304 }
andre61e67252000-06-04 17:07:49 +00001305}
andre2ff7b5d2000-06-03 14:57:40 +00001306
andre6bb92372000-06-19 08:20:03 +00001307/* Please see the notes above wtmp_islogin() for information about the
1308 next two functions */
1309
1310/* Return true if this wtmpx entry indicates a login */
1311static int
1312wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1313{
Damien Miller8899ed32004-09-12 15:18:55 +10001314 if (strncmp(li->username, utx->ut_name,
1315 MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001316# ifdef HAVE_TYPE_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +00001317 if (utx->ut_type == USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001318 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001319# else
Damien Miller8899ed32004-09-12 15:18:55 +10001320 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001321# endif
andre6bb92372000-06-19 08:20:03 +00001322 }
Damien Miller8899ed32004-09-12 15:18:55 +10001323 return (0);
andre6bb92372000-06-19 08:20:03 +00001324}
1325
andre2ff7b5d2000-06-03 14:57:40 +00001326
1327int
andre61e67252000-06-04 17:07:49 +00001328wtmpx_get_entry(struct logininfo *li)
1329{
andre2ff7b5d2000-06-03 14:57:40 +00001330 struct stat st;
1331 struct utmpx utx;
andre6bb92372000-06-19 08:20:03 +00001332 int fd, found=0;
1333
1334 /* Clear the time entries */
1335 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001336
1337 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001338 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001339 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001340 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001341 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001342 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001343 logit("%s: couldn't stat %s: %s", __func__,
Tim Ricecdb82942002-07-14 15:33:20 -07001344 WTMPX_FILE, strerror(errno));
andre2ff7b5d2000-06-03 14:57:40 +00001345 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001346 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001347 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001348
andre6bb92372000-06-19 08:20:03 +00001349 /* Seek to the start of the last struct utmpx */
Kevin Steves52172652001-10-02 00:29:00 +00001350 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
andre6bb92372000-06-19 08:20:03 +00001351 /* probably a newly rotated wtmpx file */
1352 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001353 return (0);
andre6bb92372000-06-19 08:20:03 +00001354 }
andre2ff7b5d2000-06-03 14:57:40 +00001355
andre6bb92372000-06-19 08:20:03 +00001356 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001357 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001358 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001359 WTMPX_FILE, strerror(errno));
1360 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001361 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001362 }
Damien Miller8899ed32004-09-12 15:18:55 +10001363 /*
Damien Miller94cf4c82005-07-17 17:04:47 +10001364 * Logouts are recorded as a blank username on a particular
Damien Miller8899ed32004-09-12 15:18:55 +10001365 * line. So, we just need to find the username in struct utmpx
1366 */
1367 if (wtmpx_islogin(li, &utx)) {
Tim Rice370e0ba2002-07-14 15:50:51 -07001368 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001369# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001370 li->tv_sec = utx.ut_tv.tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +10001371# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001372 li->tv_sec = utx.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001373# endif
Damien Miller1a132252000-06-13 21:23:17 +10001374 line_fullname(li->line, utx.ut_line, sizeof(li->line));
Damien Miller8899ed32004-09-12 15:18:55 +10001375# if defined(HAVE_HOST_IN_UTMPX)
andre6bb92372000-06-19 08:20:03 +00001376 strlcpy(li->hostname, utx.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001377 MIN_SIZEOF(li->hostname, utx.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001378# endif
andre6bb92372000-06-19 08:20:03 +00001379 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001380 }
Kevin Steves52172652001-10-02 00:29:00 +00001381 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
Damien Miller8899ed32004-09-12 15:18:55 +10001382 close(fd);
1383 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001384 }
andre6bb92372000-06-19 08:20:03 +00001385 }
1386
1387 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001388 return (1);
andre61e67252000-06-04 17:07:49 +00001389}
Damien Millerd5bf3072000-06-07 21:32:13 +10001390#endif /* USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001391
andre2ff7b5d2000-06-03 14:57:40 +00001392/**
andre61e67252000-06-04 17:07:49 +00001393 ** Low-level libutil login() functions
andre2ff7b5d2000-06-03 14:57:40 +00001394 **/
1395
1396#ifdef USE_LOGIN
andre2ff7b5d2000-06-03 14:57:40 +00001397static int
andre61e67252000-06-04 17:07:49 +00001398syslogin_perform_login(struct logininfo *li)
1399{
andre2ff7b5d2000-06-03 14:57:40 +00001400 struct utmp *ut;
1401
Damien Millerb0aae332004-09-12 15:26:00 +10001402 ut = xmalloc(sizeof(*ut));
andre2ff7b5d2000-06-03 14:57:40 +00001403 construct_utmp(li, ut);
1404 login(ut);
Damien Millerf211efc2003-03-10 11:23:06 +11001405 free(ut);
andre2ff7b5d2000-06-03 14:57:40 +00001406
Damien Miller8899ed32004-09-12 15:18:55 +10001407 return (1);
andre61e67252000-06-04 17:07:49 +00001408}
1409
andre2ff7b5d2000-06-03 14:57:40 +00001410static int
andre61e67252000-06-04 17:07:49 +00001411syslogin_perform_logout(struct logininfo *li)
1412{
Damien Millerdd47aa22000-06-27 11:18:27 +10001413# ifdef HAVE_LOGOUT
Darren Tucker4d2f3612004-04-08 10:57:05 +10001414 char line[UT_LINESIZE];
Kevin Stevesef4eea92001-02-05 12:42:17 +00001415
andre2ff7b5d2000-06-03 14:57:40 +00001416 (void)line_stripname(line, li->line, sizeof(line));
1417
Damien Miller8899ed32004-09-12 15:18:55 +10001418 if (!logout(line))
Damien Miller6b0279c2004-09-12 15:25:17 +10001419 logit("%s: logout() returned an error", __func__);
Damien Millerdd47aa22000-06-27 11:18:27 +10001420# ifdef HAVE_LOGWTMP
Damien Miller8899ed32004-09-12 15:18:55 +10001421 else
andre2ff7b5d2000-06-03 14:57:40 +00001422 logwtmp(line, "", "");
Damien Millerdd47aa22000-06-27 11:18:27 +10001423# endif
andre6bb92372000-06-19 08:20:03 +00001424 /* FIXME: (ATL - if the need arises) What to do if we have
1425 * login, but no logout? what if logout but no logwtmp? All
1426 * routines are in libutil so they should all be there,
1427 * but... */
Damien Millerdd47aa22000-06-27 11:18:27 +10001428# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001429 return (1);
andre61e67252000-06-04 17:07:49 +00001430}
andre2ff7b5d2000-06-03 14:57:40 +00001431
andre2ff7b5d2000-06-03 14:57:40 +00001432int
andre61e67252000-06-04 17:07:49 +00001433syslogin_write_entry(struct logininfo *li)
1434{
andre2ff7b5d2000-06-03 14:57:40 +00001435 switch (li->type) {
1436 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001437 return (syslogin_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001438 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001439 return (syslogin_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001440 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001441 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001442 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001443 }
andre61e67252000-06-04 17:07:49 +00001444}
Damien Millerd5bf3072000-06-07 21:32:13 +10001445#endif /* USE_LOGIN */
andre2ff7b5d2000-06-03 14:57:40 +00001446
1447/* end of file log-syslogin.c */
1448
andre2ff7b5d2000-06-03 14:57:40 +00001449/**
andre61e67252000-06-04 17:07:49 +00001450 ** Low-level lastlog functions
andre2ff7b5d2000-06-03 14:57:40 +00001451 **/
1452
1453#ifdef USE_LASTLOG
Damien Millerdd47aa22000-06-27 11:18:27 +10001454#define LL_FILE 1
1455#define LL_DIR 2
1456#define LL_OTHER 3
andre2ff7b5d2000-06-03 14:57:40 +00001457
andre2ff7b5d2000-06-03 14:57:40 +00001458static void
andre61e67252000-06-04 17:07:49 +00001459lastlog_construct(struct logininfo *li, struct lastlog *last)
1460{
andre2ff7b5d2000-06-03 14:57:40 +00001461 /* clear the structure */
Damien Miller348c9b72000-08-15 10:01:22 +10001462 memset(last, '\0', sizeof(*last));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001463
Damien Miller8899ed32004-09-12 15:18:55 +10001464 line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
andre6bb92372000-06-19 08:20:03 +00001465 strlcpy(last->ll_host, li->hostname,
1466 MIN_SIZEOF(last->ll_host, li->hostname));
andre2ff7b5d2000-06-03 14:57:40 +00001467 last->ll_time = li->tv_sec;
andre61e67252000-06-04 17:07:49 +00001468}
andre2ff7b5d2000-06-03 14:57:40 +00001469
andre2ff7b5d2000-06-03 14:57:40 +00001470static int
andre61e67252000-06-04 17:07:49 +00001471lastlog_filetype(char *filename)
1472{
andre2ff7b5d2000-06-03 14:57:40 +00001473 struct stat st;
1474
Damien Millerdd47aa22000-06-27 11:18:27 +10001475 if (stat(LASTLOG_FILE, &st) != 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001476 logit("%s: Couldn't stat %s: %s", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001477 LASTLOG_FILE, strerror(errno));
1478 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001479 }
andre2ff7b5d2000-06-03 14:57:40 +00001480 if (S_ISDIR(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001481 return (LL_DIR);
andre2ff7b5d2000-06-03 14:57:40 +00001482 else if (S_ISREG(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001483 return (LL_FILE);
andre2ff7b5d2000-06-03 14:57:40 +00001484 else
Damien Miller8899ed32004-09-12 15:18:55 +10001485 return (LL_OTHER);
andre61e67252000-06-04 17:07:49 +00001486}
andre2ff7b5d2000-06-03 14:57:40 +00001487
1488
1489/* open the file (using filemode) and seek to the login entry */
1490static int
andre61e67252000-06-04 17:07:49 +00001491lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1492{
andre2ff7b5d2000-06-03 14:57:40 +00001493 off_t offset;
1494 int type;
1495 char lastlog_file[1024];
1496
1497 type = lastlog_filetype(LASTLOG_FILE);
1498 switch (type) {
Damien Miller8899ed32004-09-12 15:18:55 +10001499 case LL_FILE:
1500 strlcpy(lastlog_file, LASTLOG_FILE,
1501 sizeof(lastlog_file));
1502 break;
1503 case LL_DIR:
1504 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1505 LASTLOG_FILE, li->username);
1506 break;
1507 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001508 logit("%s: %.100s is not a file or directory!", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001509 LASTLOG_FILE);
1510 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001511 }
andre2ff7b5d2000-06-03 14:57:40 +00001512
Damien Miller405dc602003-04-09 21:12:52 +10001513 *fd = open(lastlog_file, filemode, 0600);
Damien Miller8899ed32004-09-12 15:18:55 +10001514 if (*fd < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001515 debug("%s: Couldn't open %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001516 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001517 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001518 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001519
Damien Millere477ef62000-08-15 10:21:17 +10001520 if (type == LL_FILE) {
1521 /* find this uid's offset in the lastlog file */
Kevin Steves52172652001-10-02 00:29:00 +00001522 offset = (off_t) ((long)li->uid * sizeof(struct lastlog));
andre2ff7b5d2000-06-03 14:57:40 +00001523
Damien Miller8899ed32004-09-12 15:18:55 +10001524 if (lseek(*fd, offset, SEEK_SET) != offset) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001525 logit("%s: %s->lseek(): %s", __func__,
1526 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001527 return (0);
Damien Millere477ef62000-08-15 10:21:17 +10001528 }
andre2ff7b5d2000-06-03 14:57:40 +00001529 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001530
Damien Miller8899ed32004-09-12 15:18:55 +10001531 return (1);
andre61e67252000-06-04 17:07:49 +00001532}
andre2ff7b5d2000-06-03 14:57:40 +00001533
1534static int
andre61e67252000-06-04 17:07:49 +00001535lastlog_perform_login(struct logininfo *li)
1536{
andre2ff7b5d2000-06-03 14:57:40 +00001537 struct lastlog last;
1538 int fd;
1539
1540 /* create our struct lastlog */
1541 lastlog_construct(li, &last);
1542
Damien Miller405dc602003-04-09 21:12:52 +10001543 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
Damien Miller8899ed32004-09-12 15:18:55 +10001544 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001545
andre2ff7b5d2000-06-03 14:57:40 +00001546 /* write the entry */
Darren Tucker8661b562003-07-06 15:20:46 +10001547 if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) {
Damien Millerc1132e72000-08-18 14:08:38 +10001548 close(fd);
Damien Miller6b0279c2004-09-12 15:25:17 +10001549 logit("%s: Error writing to %s: %s", __func__,
Damien Millerc1132e72000-08-18 14:08:38 +10001550 LASTLOG_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001551 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001552 }
Damien Millerc1132e72000-08-18 14:08:38 +10001553
1554 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001555 return (1);
andre61e67252000-06-04 17:07:49 +00001556}
andre2ff7b5d2000-06-03 14:57:40 +00001557
andre2ff7b5d2000-06-03 14:57:40 +00001558int
andre61e67252000-06-04 17:07:49 +00001559lastlog_write_entry(struct logininfo *li)
1560{
andre2ff7b5d2000-06-03 14:57:40 +00001561 switch(li->type) {
1562 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001563 return (lastlog_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001564 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001565 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001566 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001567 }
andre61e67252000-06-04 17:07:49 +00001568}
andre2ff7b5d2000-06-03 14:57:40 +00001569
andre2ff7b5d2000-06-03 14:57:40 +00001570static void
andre61e67252000-06-04 17:07:49 +00001571lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1572{
andre2ff7b5d2000-06-03 14:57:40 +00001573 line_fullname(li->line, last->ll_line, sizeof(li->line));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001574 strlcpy(li->hostname, last->ll_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001575 MIN_SIZEOF(li->hostname, last->ll_host));
andre2ff7b5d2000-06-03 14:57:40 +00001576 li->tv_sec = last->ll_time;
andre61e67252000-06-04 17:07:49 +00001577}
andre2ff7b5d2000-06-03 14:57:40 +00001578
andre2ff7b5d2000-06-03 14:57:40 +00001579int
andre61e67252000-06-04 17:07:49 +00001580lastlog_get_entry(struct logininfo *li)
1581{
andre2ff7b5d2000-06-03 14:57:40 +00001582 struct lastlog last;
Damien Miller7df881d2003-01-07 16:46:58 +11001583 int fd, ret;
andre2ff7b5d2000-06-03 14:57:40 +00001584
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001585 if (!lastlog_openseek(li, &fd, O_RDONLY))
Damien Miller7df881d2003-01-07 16:46:58 +11001586 return (0);
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001587
Damien Miller7df881d2003-01-07 16:46:58 +11001588 ret = atomicio(read, fd, &last, sizeof(last));
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001589 close(fd);
1590
Damien Miller7df881d2003-01-07 16:46:58 +11001591 switch (ret) {
1592 case 0:
1593 memset(&last, '\0', sizeof(last));
1594 /* FALLTHRU */
1595 case sizeof(last):
1596 lastlog_populate_entry(li, &last);
1597 return (1);
1598 case -1:
Damien Millera8e06ce2003-11-21 23:48:55 +11001599 error("%s: Error reading from %s: %s", __func__,
Damien Miller7df881d2003-01-07 16:46:58 +11001600 LASTLOG_FILE, strerror(errno));
1601 return (0);
1602 default:
1603 error("%s: Error reading from %s: Expecting %d, got %d",
Darren Tuckerefc17472005-11-22 19:55:13 +11001604 __func__, LASTLOG_FILE, (int)sizeof(last), ret);
Damien Miller7df881d2003-01-07 16:46:58 +11001605 return (0);
1606 }
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001607
Damien Miller7df881d2003-01-07 16:46:58 +11001608 /* NOTREACHED */
1609 return (0);
andre61e67252000-06-04 17:07:49 +00001610}
Damien Millerd5bf3072000-06-07 21:32:13 +10001611#endif /* USE_LASTLOG */
Darren Tucker2fba9932005-02-02 23:30:24 +11001612
1613#ifdef USE_BTMP
1614 /*
1615 * Logs failed login attempts in _PATH_BTMP if that exists.
1616 * The most common login failure is to give password instead of username.
1617 * So the _PATH_BTMP file checked for the correct permission, so that
1618 * only root can read it.
1619 */
1620
1621void
1622record_failed_login(const char *username, const char *hostname,
1623 const char *ttyn)
1624{
1625 int fd;
1626 struct utmp ut;
1627 struct sockaddr_storage from;
Darren Tuckerefc17472005-11-22 19:55:13 +11001628 socklen_t fromlen = sizeof(from);
Darren Tucker2fba9932005-02-02 23:30:24 +11001629 struct sockaddr_in *a4;
1630 struct sockaddr_in6 *a6;
1631 time_t t;
1632 struct stat fst;
1633
1634 if (geteuid() != 0)
1635 return;
1636 if ((fd = open(_PATH_BTMP, O_WRONLY | O_APPEND)) < 0) {
1637 debug("Unable to open the btmp file %s: %s", _PATH_BTMP,
1638 strerror(errno));
1639 return;
1640 }
1641 if (fstat(fd, &fst) < 0) {
1642 logit("%s: fstat of %s failed: %s", __func__, _PATH_BTMP,
1643 strerror(errno));
1644 goto out;
1645 }
1646 if((fst.st_mode & (S_IRWXG | S_IRWXO)) || (fst.st_uid != 0)){
1647 logit("Excess permission or bad ownership on file %s",
1648 _PATH_BTMP);
1649 goto out;
1650 }
1651
1652 memset(&ut, 0, sizeof(ut));
1653 /* strncpy because we don't necessarily want nul termination */
1654 strncpy(ut.ut_user, username, sizeof(ut.ut_user));
1655 strlcpy(ut.ut_line, "ssh:notty", sizeof(ut.ut_line));
1656
1657 time(&t);
1658 ut.ut_time = t; /* ut_time is not always a time_t */
1659 ut.ut_type = LOGIN_PROCESS;
1660 ut.ut_pid = getpid();
1661
1662 /* strncpy because we don't necessarily want nul termination */
1663 strncpy(ut.ut_host, hostname, sizeof(ut.ut_host));
1664
1665 if (packet_connection_is_on_socket() &&
1666 getpeername(packet_get_connection_in(),
1667 (struct sockaddr *)&from, &fromlen) == 0) {
1668 ipv64_normalise_mapped(&from, &fromlen);
1669 if (from.ss_family == AF_INET) {
1670 a4 = (struct sockaddr_in *)&from;
1671 memcpy(&ut.ut_addr, &(a4->sin_addr),
1672 MIN_SIZEOF(ut.ut_addr, a4->sin_addr));
1673 }
1674#ifdef HAVE_ADDR_V6_IN_UTMP
1675 if (from.ss_family == AF_INET6) {
1676 a6 = (struct sockaddr_in6 *)&from;
1677 memcpy(&ut.ut_addr_v6, &(a6->sin6_addr),
1678 MIN_SIZEOF(ut.ut_addr_v6, a6->sin6_addr));
1679 }
1680#endif
1681 }
1682
1683 if (atomicio(vwrite, fd, &ut, sizeof(ut)) != sizeof(ut))
1684 error("Failed to write to %s: %s", _PATH_BTMP,
1685 strerror(errno));
1686
1687out:
1688 close(fd);
1689}
1690#endif /* USE_BTMP */