blob: c2498e1e3da642637554446706517051b643f734 [file] [log] [blame]
andre2ff7b5d2000-06-03 14:57:40 +00001/*
2 * Copyright (c) 2000 Andre Lucas. All rights reserved.
andre61e67252000-06-04 17:07:49 +00003 * Portions copyright (c) 1998 Todd C. Miller
4 * Portions copyright (c) 1996 Jason Downs
5 * Portions copyright (c) 1996 Theo de Raadt
andre2ff7b5d2000-06-03 14:57:40 +00006 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
andre2ff7b5d2000-06-03 14:57:40 +000015 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
Darren Tucker2fba9932005-02-02 23:30:24 +110028/*
29 * The btmp logging code is derived from login.c from util-linux and is under
30 * the the following license:
31 *
32 * Copyright (c) 1980, 1987, 1988 The Regents of the University of California.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms are permitted
36 * provided that the above copyright notice and this paragraph are
37 * duplicated in all such forms and that any documentation,
38 * advertising materials, and other materials related to such
39 * distribution and use acknowledge that the software was developed
40 * by the University of California, Berkeley. The name of the
41 * University may not be used to endorse or promote products derived
42 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
44 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
45 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 */
47
48
Kevin Stevesef4eea92001-02-05 12:42:17 +000049/**
andre2ff7b5d2000-06-03 14:57:40 +000050 ** loginrec.c: platform-independent login recording and lastlog retrieval
51 **/
52
andre61e67252000-06-04 17:07:49 +000053/*
Damien Miller8899ed32004-09-12 15:18:55 +100054 * The new login code explained
55 * ============================
56 *
57 * This code attempts to provide a common interface to login recording
58 * (utmp and friends) and last login time retrieval.
59 *
60 * Its primary means of achieving this is to use 'struct logininfo', a
61 * union of all the useful fields in the various different types of
62 * system login record structures one finds on UNIX variants.
63 *
64 * We depend on autoconf to define which recording methods are to be
65 * used, and which fields are contained in the relevant data structures
66 * on the local system. Many C preprocessor symbols affect which code
67 * gets compiled here.
68 *
69 * The code is designed to make it easy to modify a particular
70 * recording method, without affecting other methods nor requiring so
71 * many nested conditional compilation blocks as were commonplace in
72 * the old code.
73 *
74 * For login recording, we try to use the local system's libraries as
75 * these are clearly most likely to work correctly. For utmp systems
76 * this usually means login() and logout() or setutent() etc., probably
77 * in libutil, along with logwtmp() etc. On these systems, we fall back
78 * to writing the files directly if we have to, though this method
79 * requires very thorough testing so we do not corrupt local auditing
80 * information. These files and their access methods are very system
81 * specific indeed.
82 *
83 * For utmpx systems, the corresponding library functions are
84 * setutxent() etc. To the author's knowledge, all utmpx systems have
85 * these library functions and so no direct write is attempted. If such
86 * a system exists and needs support, direct analogues of the [uw]tmp
87 * code should suffice.
88 *
89 * Retrieving the time of last login ('lastlog') is in some ways even
90 * more problemmatic than login recording. Some systems provide a
91 * simple table of all users which we seek based on uid and retrieve a
92 * relatively standard structure. Others record the same information in
93 * a directory with a separate file, and others don't record the
94 * information separately at all. For systems in the latter category,
95 * we look backwards in the wtmp or wtmpx file for the last login entry
96 * for our user. Naturally this is slower and on busy systems could
97 * incur a significant performance penalty.
98 *
99 * Calling the new code
100 * --------------------
101 *
102 * In OpenSSH all login recording and retrieval is performed in
103 * login.c. Here you'll find working examples. Also, in the logintest.c
104 * program there are more examples.
105 *
106 * Internal handler calling method
107 * -------------------------------
108 *
109 * When a call is made to login_login() or login_logout(), both
110 * routines set a struct logininfo flag defining which action (log in,
111 * or log out) is to be taken. They both then call login_write(), which
112 * calls whichever of the many structure-specific handlers autoconf
113 * selects for the local system.
114 *
115 * The handlers themselves handle system data structure specifics. Both
116 * struct utmp and struct utmpx have utility functions (see
117 * construct_utmp*()) to try to make it simpler to add extra systems
118 * that introduce new features to either structure.
119 *
120 * While it may seem terribly wasteful to replicate so much similar
121 * code for each method, experience has shown that maintaining code to
122 * write both struct utmp and utmpx in one function, whilst maintaining
123 * support for all systems whether they have library support or not, is
124 * a difficult and time-consuming task.
125 *
126 * Lastlog support proceeds similarly. Functions login_get_lastlog()
127 * (and its OpenSSH-tuned friend login_get_lastlog_time()) call
128 * getlast_entry(), which tries one of three methods to find the last
129 * login time. It uses local system lastlog support if it can,
130 * otherwise it tries wtmp or wtmpx before giving up and returning 0,
131 * meaning "tilt".
132 *
133 * Maintenance
134 * -----------
135 *
136 * In many cases it's possible to tweak autoconf to select the correct
137 * methods for a particular platform, either by improving the detection
138 * code (best), or by presetting DISABLE_<method> or CONF_<method>_FILE
139 * symbols for the platform.
140 *
141 * Use logintest to check which symbols are defined before modifying
142 * configure.ac and loginrec.c. (You have to build logintest yourself
143 * with 'make logintest' as it's not built by default.)
144 *
145 * Otherwise, patches to the specific method(s) are very helpful!
146 */
andre2ff7b5d2000-06-03 14:57:40 +0000147
148#include "includes.h"
149
Damien Miller62772522006-03-15 14:01:11 +1100150#include <sys/types.h>
151#include <sys/stat.h>
152
andre2ff7b5d2000-06-03 14:57:40 +0000153#include "ssh.h"
154#include "xmalloc.h"
155#include "loginrec.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000156#include "log.h"
157#include "atomicio.h"
Darren Tucker2fba9932005-02-02 23:30:24 +1100158#include "packet.h"
159#include "canohost.h"
Darren Tucker269a1ea2005-02-03 00:20:53 +1100160#include "auth.h"
Darren Tuckera39f83e2005-02-15 22:19:28 +1100161#include "buffer.h"
andre2ff7b5d2000-06-03 14:57:40 +0000162
Ben Lindstromdcca9812000-11-10 03:28:31 +0000163#ifdef HAVE_UTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000164# include <util.h>
Ben Lindstromdcca9812000-11-10 03:28:31 +0000165#endif
andre2ff7b5d2000-06-03 14:57:40 +0000166
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000167#ifdef HAVE_LIBUTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000168# include <libutil.h>
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000169#endif
170
andre2ff7b5d2000-06-03 14:57:40 +0000171/**
172 ** prototypes for helper functions in this file
173 **/
174
175#if HAVE_UTMP_H
andre2ff7b5d2000-06-03 14:57:40 +0000176void set_utmp_time(struct logininfo *li, struct utmp *ut);
177void construct_utmp(struct logininfo *li, struct utmp *ut);
178#endif
179
180#ifdef HAVE_UTMPX_H
andre2ff7b5d2000-06-03 14:57:40 +0000181void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
182void construct_utmpx(struct logininfo *li, struct utmpx *ut);
183#endif
184
185int utmp_write_entry(struct logininfo *li);
186int utmpx_write_entry(struct logininfo *li);
187int wtmp_write_entry(struct logininfo *li);
188int wtmpx_write_entry(struct logininfo *li);
189int lastlog_write_entry(struct logininfo *li);
190int syslogin_write_entry(struct logininfo *li);
191
192int getlast_entry(struct logininfo *li);
193int lastlog_get_entry(struct logininfo *li);
194int wtmp_get_entry(struct logininfo *li);
195int wtmpx_get_entry(struct logininfo *li);
196
Darren Tucker691d5232005-02-15 21:45:57 +1100197extern Buffer loginmsg;
198
andre6bb92372000-06-19 08:20:03 +0000199/* pick the shortest string */
Damien Miller8899ed32004-09-12 15:18:55 +1000200#define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2))
andre6bb92372000-06-19 08:20:03 +0000201
andre2ff7b5d2000-06-03 14:57:40 +0000202/**
203 ** platform-independent login functions
204 **/
205
Damien Miller8899ed32004-09-12 15:18:55 +1000206/*
207 * login_login(struct logininfo *) - Record a login
Kevin Stevesef4eea92001-02-05 12:42:17 +0000208 *
andre6bb92372000-06-19 08:20:03 +0000209 * Call with a pointer to a struct logininfo initialised with
210 * login_init_entry() or login_alloc_entry()
211 *
212 * Returns:
213 * >0 if successful
214 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
215 */
andre61e67252000-06-04 17:07:49 +0000216int
Damien Miller8899ed32004-09-12 15:18:55 +1000217login_login(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000218{
219 li->type = LTYPE_LOGIN;
Damien Miller8899ed32004-09-12 15:18:55 +1000220 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000221}
222
223
Damien Miller8899ed32004-09-12 15:18:55 +1000224/*
225 * login_logout(struct logininfo *) - Record a logout
andre6bb92372000-06-19 08:20:03 +0000226 *
227 * Call as with login_login()
228 *
229 * Returns:
230 * >0 if successful
231 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
232 */
andre61e67252000-06-04 17:07:49 +0000233int
234login_logout(struct logininfo *li)
235{
236 li->type = LTYPE_LOGOUT;
Damien Miller8899ed32004-09-12 15:18:55 +1000237 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000238}
239
Damien Miller8899ed32004-09-12 15:18:55 +1000240/*
241 * login_get_lastlog_time(int) - Retrieve the last login time
andre6bb92372000-06-19 08:20:03 +0000242 *
243 * Retrieve the last login time for the given uid. Will try to use the
244 * system lastlog facilities if they are available, but will fall back
245 * to looking in wtmp/wtmpx if necessary
246 *
247 * Returns:
248 * 0 on failure, or if user has never logged in
249 * Time in seconds from the epoch if successful
250 *
251 * Useful preprocessor symbols:
252 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
253 * info
254 * USE_LASTLOG: If set, indicates the presence of system lastlog
255 * facilities. If this and DISABLE_LASTLOG are not set,
256 * try to retrieve lastlog information from wtmp/wtmpx.
257 */
andre61e67252000-06-04 17:07:49 +0000258unsigned int
259login_get_lastlog_time(const int uid)
260{
261 struct logininfo li;
262
andre6bb92372000-06-19 08:20:03 +0000263 if (login_get_lastlog(&li, uid))
Damien Miller8899ed32004-09-12 15:18:55 +1000264 return (li.tv_sec);
andre6bb92372000-06-19 08:20:03 +0000265 else
Damien Miller8899ed32004-09-12 15:18:55 +1000266 return (0);
andre61e67252000-06-04 17:07:49 +0000267}
268
Damien Miller8899ed32004-09-12 15:18:55 +1000269/*
270 * login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
andre6bb92372000-06-19 08:20:03 +0000271 *
272 * Retrieve a logininfo structure populated (only partially) with
273 * information from the system lastlog data, or from wtmp/wtmpx if no
274 * system lastlog information exists.
275 *
276 * Note this routine must be given a pre-allocated logininfo.
277 *
278 * Returns:
279 * >0: A pointer to your struct logininfo if successful
280 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
andre6bb92372000-06-19 08:20:03 +0000281 */
andre61e67252000-06-04 17:07:49 +0000282struct logininfo *
283login_get_lastlog(struct logininfo *li, const int uid)
284{
andre6bb92372000-06-19 08:20:03 +0000285 struct passwd *pw;
andre6bb92372000-06-19 08:20:03 +0000286
Damien Miller348c9b72000-08-15 10:01:22 +1000287 memset(li, '\0', sizeof(*li));
andre61e67252000-06-04 17:07:49 +0000288 li->uid = uid;
andre6bb92372000-06-19 08:20:03 +0000289
Kevin Stevesef4eea92001-02-05 12:42:17 +0000290 /*
Damien Miller53c5d462000-06-28 00:50:50 +1000291 * If we don't have a 'real' lastlog, we need the username to
andre6bb92372000-06-19 08:20:03 +0000292 * reliably search wtmp(x) for the last login (see
Kevin Stevesef4eea92001-02-05 12:42:17 +0000293 * wtmp_get_entry().)
Damien Miller53c5d462000-06-28 00:50:50 +1000294 */
andre6bb92372000-06-19 08:20:03 +0000295 pw = getpwuid(uid);
Damien Millerdd47aa22000-06-27 11:18:27 +1000296 if (pw == NULL)
Damien Miller6b0279c2004-09-12 15:25:17 +1000297 fatal("%s: Cannot find account for uid %i", __func__, uid);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000298
andre98cabe02000-06-19 09:11:30 +0000299 /* No MIN_SIZEOF here - we absolutely *must not* truncate the
Damien Miller8899ed32004-09-12 15:18:55 +1000300 * username (XXX - so check for trunc!) */
Damien Millerf8af08d2000-06-27 09:40:06 +1000301 strlcpy(li->username, pw->pw_name, sizeof(li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000302
andre61e67252000-06-04 17:07:49 +0000303 if (getlast_entry(li))
Damien Miller8899ed32004-09-12 15:18:55 +1000304 return (li);
andre61e67252000-06-04 17:07:49 +0000305 else
Damien Miller8899ed32004-09-12 15:18:55 +1000306 return (NULL);
andre61e67252000-06-04 17:07:49 +0000307}
308
309
Damien Miller8899ed32004-09-12 15:18:55 +1000310/*
311 * login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
Kevin Stevesef4eea92001-02-05 12:42:17 +0000312 * a logininfo structure
313 *
andre6bb92372000-06-19 08:20:03 +0000314 * This function creates a new struct logininfo, a data structure
315 * meant to carry the information required to portably record login info.
316 *
317 * Returns a pointer to a newly created struct logininfo. If memory
318 * allocation fails, the program halts.
319 */
andre61e67252000-06-04 17:07:49 +0000320struct
321logininfo *login_alloc_entry(int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000322 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000323{
andre2ff7b5d2000-06-03 14:57:40 +0000324 struct logininfo *newli;
325
Damien Miller8899ed32004-09-12 15:18:55 +1000326 newli = xmalloc(sizeof(*newli));
327 login_init_entry(newli, pid, username, hostname, line);
328 return (newli);
andre61e67252000-06-04 17:07:49 +0000329}
andre2ff7b5d2000-06-03 14:57:40 +0000330
331
andre6bb92372000-06-19 08:20:03 +0000332/* login_free_entry(struct logininfo *) - free struct memory */
andre61e67252000-06-04 17:07:49 +0000333void
334login_free_entry(struct logininfo *li)
335{
336 xfree(li);
337}
338
andre2ff7b5d2000-06-03 14:57:40 +0000339
andre6bb92372000-06-19 08:20:03 +0000340/* login_init_entry(struct logininfo *, int, char*, char*, char*)
341 * - initialise a struct logininfo
Kevin Stevesef4eea92001-02-05 12:42:17 +0000342 *
andre6bb92372000-06-19 08:20:03 +0000343 * Populates a new struct logininfo, a data structure meant to carry
344 * the information required to portably record login info.
345 *
346 * Returns: 1
347 */
andre61e67252000-06-04 17:07:49 +0000348int
Kevin Stevesef4eea92001-02-05 12:42:17 +0000349login_init_entry(struct logininfo *li, int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000350 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000351{
Damien Millerf8af08d2000-06-27 09:40:06 +1000352 struct passwd *pw;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000353
Damien Miller348c9b72000-08-15 10:01:22 +1000354 memset(li, 0, sizeof(*li));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000355
andre61e67252000-06-04 17:07:49 +0000356 li->pid = pid;
Damien Millerf8af08d2000-06-27 09:40:06 +1000357
andre2ff7b5d2000-06-03 14:57:40 +0000358 /* set the line information */
andre61e67252000-06-04 17:07:49 +0000359 if (line)
andre2ff7b5d2000-06-03 14:57:40 +0000360 line_fullname(li->line, line, sizeof(li->line));
andre2ff7b5d2000-06-03 14:57:40 +0000361
Damien Millerf8af08d2000-06-27 09:40:06 +1000362 if (username) {
andre2ff7b5d2000-06-03 14:57:40 +0000363 strlcpy(li->username, username, sizeof(li->username));
Damien Millerf8af08d2000-06-27 09:40:06 +1000364 pw = getpwnam(li->username);
Damien Miller8899ed32004-09-12 15:18:55 +1000365 if (pw == NULL) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000366 fatal("%s: Cannot find user \"%s\"", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +1000367 li->username);
368 }
Damien Millerf8af08d2000-06-27 09:40:06 +1000369 li->uid = pw->pw_uid;
370 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000371
andre61e67252000-06-04 17:07:49 +0000372 if (hostname)
andre2ff7b5d2000-06-03 14:57:40 +0000373 strlcpy(li->hostname, hostname, sizeof(li->hostname));
Damien Millerf8af08d2000-06-27 09:40:06 +1000374
Damien Miller8899ed32004-09-12 15:18:55 +1000375 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000376}
377
Damien Miller94cf4c82005-07-17 17:04:47 +1000378/*
Damien Miller8899ed32004-09-12 15:18:55 +1000379 * login_set_current_time(struct logininfo *) - set the current time
andre6bb92372000-06-19 08:20:03 +0000380 *
381 * Set the current time in a logininfo structure. This function is
382 * meant to eliminate the need to deal with system dependencies for
383 * time handling.
384 */
andre2ff7b5d2000-06-03 14:57:40 +0000385void
andre61e67252000-06-04 17:07:49 +0000386login_set_current_time(struct logininfo *li)
387{
andre2ff7b5d2000-06-03 14:57:40 +0000388 struct timeval tv;
389
390 gettimeofday(&tv, NULL);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000391
Damien Millerf8af08d2000-06-27 09:40:06 +1000392 li->tv_sec = tv.tv_sec;
393 li->tv_usec = tv.tv_usec;
andre2ff7b5d2000-06-03 14:57:40 +0000394}
395
andre61e67252000-06-04 17:07:49 +0000396/* copy a sockaddr_* into our logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000397void
andre61e67252000-06-04 17:07:49 +0000398login_set_addr(struct logininfo *li, const struct sockaddr *sa,
Damien Miller8899ed32004-09-12 15:18:55 +1000399 const unsigned int sa_size)
andre61e67252000-06-04 17:07:49 +0000400{
401 unsigned int bufsize = sa_size;
402
403 /* make sure we don't overrun our union */
404 if (sizeof(li->hostaddr) < sa_size)
405 bufsize = sizeof(li->hostaddr);
406
Damien Miller8899ed32004-09-12 15:18:55 +1000407 memcpy(&li->hostaddr.sa, sa, bufsize);
andre2ff7b5d2000-06-03 14:57:40 +0000408}
409
andre2ff7b5d2000-06-03 14:57:40 +0000410
andre61e67252000-06-04 17:07:49 +0000411/**
412 ** login_write: Call low-level recording functions based on autoconf
413 ** results
414 **/
andre2ff7b5d2000-06-03 14:57:40 +0000415int
Damien Miller8899ed32004-09-12 15:18:55 +1000416login_write(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000417{
Damien Millerbac2d8a2000-09-05 16:13:06 +1100418#ifndef HAVE_CYGWIN
Damien Miller8899ed32004-09-12 15:18:55 +1000419 if (geteuid() != 0) {
420 logit("Attempt to write login records by non-root user (aborting)");
421 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000422 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100423#endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000424
andre2ff7b5d2000-06-03 14:57:40 +0000425 /* set the timestamp */
426 login_set_current_time(li);
427#ifdef USE_LOGIN
428 syslogin_write_entry(li);
429#endif
430#ifdef USE_LASTLOG
Damien Miller8899ed32004-09-12 15:18:55 +1000431 if (li->type == LTYPE_LOGIN)
andre2ff7b5d2000-06-03 14:57:40 +0000432 lastlog_write_entry(li);
andre2ff7b5d2000-06-03 14:57:40 +0000433#endif
434#ifdef USE_UTMP
435 utmp_write_entry(li);
436#endif
437#ifdef USE_WTMP
438 wtmp_write_entry(li);
439#endif
440#ifdef USE_UTMPX
441 utmpx_write_entry(li);
442#endif
443#ifdef USE_WTMPX
444 wtmpx_write_entry(li);
445#endif
Darren Tucker397a2f22004-08-15 00:09:11 +1000446#ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN
Damien Miller94cf4c82005-07-17 17:04:47 +1000447 if (li->type == LTYPE_LOGIN &&
Damien Millerb6f72f52005-07-17 17:26:43 +1000448 !sys_auth_record_login(li->username,li->hostname,li->line,
449 &loginmsg))
Darren Tucker397a2f22004-08-15 00:09:11 +1000450 logit("Writing login record failed for %s", li->username);
451#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100452#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100453 if (li->type == LTYPE_LOGIN)
454 audit_session_open(li->line);
455 else if (li->type == LTYPE_LOGOUT)
456 audit_session_close(li->line);
457#endif
Damien Miller8899ed32004-09-12 15:18:55 +1000458 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000459}
460
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000461#ifdef LOGIN_NEEDS_UTMPX
462int
463login_utmp_only(struct logininfo *li)
464{
Damien Millera8e06ce2003-11-21 23:48:55 +1100465 li->type = LTYPE_LOGIN;
Ben Lindstrom9197c592001-10-26 15:56:55 +0000466 login_set_current_time(li);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000467# ifdef USE_UTMP
468 utmp_write_entry(li);
469# endif
470# ifdef USE_WTMP
471 wtmp_write_entry(li);
472# endif
473# ifdef USE_UTMPX
474 utmpx_write_entry(li);
475# endif
476# ifdef USE_WTMPX
477 wtmpx_write_entry(li);
478# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000479 return (0);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000480}
481#endif
482
andre2ff7b5d2000-06-03 14:57:40 +0000483/**
andre61e67252000-06-04 17:07:49 +0000484 ** getlast_entry: Call low-level functions to retrieve the last login
485 ** time.
andre2ff7b5d2000-06-03 14:57:40 +0000486 **/
487
andre61e67252000-06-04 17:07:49 +0000488/* take the uid in li and return the last login time */
489int
490getlast_entry(struct logininfo *li)
491{
492#ifdef USE_LASTLOG
Damien Miller53c5d462000-06-28 00:50:50 +1000493 return(lastlog_get_entry(li));
Damien Millerdd47aa22000-06-27 11:18:27 +1000494#else /* !USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000495
Damien Miller8899ed32004-09-12 15:18:55 +1000496#if defined(DISABLE_LASTLOG)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000497 /* On some systems we shouldn't even try to obtain last login
andreecaabf12000-06-12 22:21:44 +0000498 * time, e.g. AIX */
Damien Miller8899ed32004-09-12 15:18:55 +1000499 return (0);
500# elif defined(USE_WTMP) && \
501 (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
andre61e67252000-06-04 17:07:49 +0000502 /* retrieve last login time from utmp */
Damien Millerdd47aa22000-06-27 11:18:27 +1000503 return (wtmp_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000504# elif defined(USE_WTMPX) && \
505 (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
andre61e67252000-06-04 17:07:49 +0000506 /* If wtmp isn't available, try wtmpx */
Damien Millerdd47aa22000-06-27 11:18:27 +1000507 return (wtmpx_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000508# else
andre61e67252000-06-04 17:07:49 +0000509 /* Give up: No means of retrieving last login time */
Damien Miller8899ed32004-09-12 15:18:55 +1000510 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000511# endif /* DISABLE_LASTLOG */
Damien Millerdd47aa22000-06-27 11:18:27 +1000512#endif /* USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000513}
514
515
516
andre2ff7b5d2000-06-03 14:57:40 +0000517/*
andre61e67252000-06-04 17:07:49 +0000518 * 'line' string utility functions
519 *
520 * These functions process the 'line' string into one of three forms:
521 *
andre2ff7b5d2000-06-03 14:57:40 +0000522 * 1. The full filename (including '/dev')
523 * 2. The stripped name (excluding '/dev')
andre61e67252000-06-04 17:07:49 +0000524 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
525 * /dev/pts/1 -> ts/1 )
andre2ff7b5d2000-06-03 14:57:40 +0000526 *
527 * Form 3 is used on some systems to identify a .tmp.? entry when
528 * attempting to remove it. Typically both addition and removal is
andre61e67252000-06-04 17:07:49 +0000529 * performed by one application - say, sshd - so as long as the choice
530 * uniquely identifies a terminal it's ok.
andre2ff7b5d2000-06-03 14:57:40 +0000531 */
532
533
Damien Miller8899ed32004-09-12 15:18:55 +1000534/*
535 * line_fullname(): add the leading '/dev/' if it doesn't exist make
536 * sure dst has enough space, if not just copy src (ugh)
537 */
andre2ff7b5d2000-06-03 14:57:40 +0000538char *
Damien Miller52c8afe2005-06-19 10:19:43 +1000539line_fullname(char *dst, const char *src, u_int dstsize)
andre61e67252000-06-04 17:07:49 +0000540{
andre2ff7b5d2000-06-03 14:57:40 +0000541 memset(dst, '\0', dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000542 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
andre2ff7b5d2000-06-03 14:57:40 +0000543 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000544 else {
Damien Miller1a132252000-06-13 21:23:17 +1000545 strlcpy(dst, "/dev/", dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000546 strlcat(dst, src, dstsize);
547 }
Damien Miller8899ed32004-09-12 15:18:55 +1000548 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000549}
550
andre61e67252000-06-04 17:07:49 +0000551/* line_stripname(): strip the leading '/dev' if it exists, return dst */
andre2ff7b5d2000-06-03 14:57:40 +0000552char *
andre61e67252000-06-04 17:07:49 +0000553line_stripname(char *dst, const char *src, int dstsize)
554{
andre2ff7b5d2000-06-03 14:57:40 +0000555 memset(dst, '\0', dstsize);
556 if (strncmp(src, "/dev/", 5) == 0)
Damien Millerf5a81472000-09-30 21:34:44 +1100557 strlcpy(dst, src + 5, dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000558 else
559 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000560 return (dst);
andre61e67252000-06-04 17:07:49 +0000561}
562
Damien Miller94cf4c82005-07-17 17:04:47 +1000563/*
Damien Miller8899ed32004-09-12 15:18:55 +1000564 * line_abbrevname(): Return the abbreviated (usually four-character)
andre61e67252000-06-04 17:07:49 +0000565 * form of the line (Just use the last <dstsize> characters of the
566 * full name.)
567 *
568 * NOTE: use strncpy because we do NOT necessarily want zero
Damien Miller8899ed32004-09-12 15:18:55 +1000569 * termination
570 */
andre2ff7b5d2000-06-03 14:57:40 +0000571char *
Kevin Stevesef4eea92001-02-05 12:42:17 +0000572line_abbrevname(char *dst, const char *src, int dstsize)
Damien Millerdd47aa22000-06-27 11:18:27 +1000573{
574 size_t len;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000575
andre2ff7b5d2000-06-03 14:57:40 +0000576 memset(dst, '\0', dstsize);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000577
Damien Miller8e81ed32000-07-01 13:17:42 +1000578 /* Always skip prefix if present */
579 if (strncmp(src, "/dev/", 5) == 0)
580 src += 5;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000581
Damien Millerf1b9d112002-04-23 23:09:19 +1000582#ifdef WITH_ABBREV_NO_TTY
583 if (strncmp(src, "tty", 3) == 0)
584 src += 3;
585#endif
586
Damien Millerdd47aa22000-06-27 11:18:27 +1000587 len = strlen(src);
588
Damien Miller8e81ed32000-07-01 13:17:42 +1000589 if (len > 0) {
590 if (((int)len - dstsize) > 0)
591 src += ((int)len - dstsize);
592
593 /* note: _don't_ change this to strlcpy */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000594 strncpy(dst, src, (size_t)dstsize);
Damien Millerdd47aa22000-06-27 11:18:27 +1000595 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000596
Damien Miller8899ed32004-09-12 15:18:55 +1000597 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000598}
599
andre2ff7b5d2000-06-03 14:57:40 +0000600/**
601 ** utmp utility functions
andre61e67252000-06-04 17:07:49 +0000602 **
603 ** These functions manipulate struct utmp, taking system differences
604 ** into account.
andre2ff7b5d2000-06-03 14:57:40 +0000605 **/
606
607#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
608
andre2ff7b5d2000-06-03 14:57:40 +0000609/* build the utmp structure */
610void
andre61e67252000-06-04 17:07:49 +0000611set_utmp_time(struct logininfo *li, struct utmp *ut)
612{
Damien Miller8899ed32004-09-12 15:18:55 +1000613# if defined(HAVE_TV_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000614 ut->ut_tv.tv_sec = li->tv_sec;
615 ut->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000616# elif defined(HAVE_TIME_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000617 ut->ut_time = li->tv_sec;
Damien Millerdd47aa22000-06-27 11:18:27 +1000618# endif
andre2ff7b5d2000-06-03 14:57:40 +0000619}
620
621void
622construct_utmp(struct logininfo *li,
andre61e67252000-06-04 17:07:49 +0000623 struct utmp *ut)
624{
Damien Miller02e16ad2003-01-03 14:42:27 +1100625# ifdef HAVE_ADDR_V6_IN_UTMP
626 struct sockaddr_in6 *sa6;
Damien Miller8899ed32004-09-12 15:18:55 +1000627# endif
628
Damien Miller348c9b72000-08-15 10:01:22 +1000629 memset(ut, '\0', sizeof(*ut));
andre6bb92372000-06-19 08:20:03 +0000630
631 /* First fill out fields used for both logins and logouts */
632
Damien Millerdd47aa22000-06-27 11:18:27 +1000633# ifdef HAVE_ID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000634 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +1000635# endif
andre2ff7b5d2000-06-03 14:57:40 +0000636
Damien Millerdd47aa22000-06-27 11:18:27 +1000637# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +0000638 /* This is done here to keep utmp constants out of struct logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000639 switch (li->type) {
640 case LTYPE_LOGIN:
641 ut->ut_type = USER_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700642#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000643 cray_set_tmpdir(ut);
644#endif
andre2ff7b5d2000-06-03 14:57:40 +0000645 break;
646 case LTYPE_LOGOUT:
647 ut->ut_type = DEAD_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700648#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000649 cray_retain_utmp(ut, li->pid);
650#endif
andre2ff7b5d2000-06-03 14:57:40 +0000651 break;
652 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000653# endif
andre6bb92372000-06-19 08:20:03 +0000654 set_utmp_time(li, ut);
andre2ff7b5d2000-06-03 14:57:40 +0000655
andre6bb92372000-06-19 08:20:03 +0000656 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +1000657
658# ifdef HAVE_PID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000659 ut->ut_pid = li->pid;
Damien Millerdd47aa22000-06-27 11:18:27 +1000660# endif
andre6bb92372000-06-19 08:20:03 +0000661
662 /* If we're logging out, leave all other fields blank */
663 if (li->type == LTYPE_LOGOUT)
Damien Miller8899ed32004-09-12 15:18:55 +1000664 return;
andre6bb92372000-06-19 08:20:03 +0000665
Damien Millerdd47aa22000-06-27 11:18:27 +1000666 /*
667 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000668 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000669 */
andre6bb92372000-06-19 08:20:03 +0000670
671 /* Use strncpy because we don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000672 strncpy(ut->ut_name, li->username,
673 MIN_SIZEOF(ut->ut_name, li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000674# ifdef HAVE_HOST_IN_UTMP
Damien Miller8899ed32004-09-12 15:18:55 +1000675 strncpy(ut->ut_host, li->hostname,
676 MIN_SIZEOF(ut->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000677# endif
678# ifdef HAVE_ADDR_IN_UTMP
andre61e67252000-06-04 17:07:49 +0000679 /* this is just a 32-bit IP address */
680 if (li->hostaddr.sa.sa_family == AF_INET)
681 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000682# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100683# ifdef HAVE_ADDR_V6_IN_UTMP
684 /* this is just a 128-bit IPv6 address */
685 if (li->hostaddr.sa.sa_family == AF_INET6) {
686 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
687 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
688 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
689 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
690 ut->ut_addr_v6[1] = 0;
691 ut->ut_addr_v6[2] = 0;
692 ut->ut_addr_v6[3] = 0;
693 }
694 }
695# endif
andre61e67252000-06-04 17:07:49 +0000696}
Damien Millerdd47aa22000-06-27 11:18:27 +1000697#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
andre61e67252000-06-04 17:07:49 +0000698
andre2ff7b5d2000-06-03 14:57:40 +0000699/**
700 ** utmpx utility functions
andre61e67252000-06-04 17:07:49 +0000701 **
702 ** These functions manipulate struct utmpx, accounting for system
703 ** variations.
andre2ff7b5d2000-06-03 14:57:40 +0000704 **/
705
706#if defined(USE_UTMPX) || defined (USE_WTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000707/* build the utmpx structure */
708void
andre61e67252000-06-04 17:07:49 +0000709set_utmpx_time(struct logininfo *li, struct utmpx *utx)
710{
Damien Miller8899ed32004-09-12 15:18:55 +1000711# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000712 utx->ut_tv.tv_sec = li->tv_sec;
713 utx->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000714# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000715 utx->ut_time = li->tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +1000716# endif
andre2ff7b5d2000-06-03 14:57:40 +0000717}
718
andre61e67252000-06-04 17:07:49 +0000719void
720construct_utmpx(struct logininfo *li, struct utmpx *utx)
721{
Damien Miller02e16ad2003-01-03 14:42:27 +1100722# ifdef HAVE_ADDR_V6_IN_UTMP
723 struct sockaddr_in6 *sa6;
724# endif
Damien Miller348c9b72000-08-15 10:01:22 +1000725 memset(utx, '\0', sizeof(*utx));
Damien Miller8899ed32004-09-12 15:18:55 +1000726
Damien Miller8e81ed32000-07-01 13:17:42 +1000727# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000728 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
Damien Miller8e81ed32000-07-01 13:17:42 +1000729# endif
andre2ff7b5d2000-06-03 14:57:40 +0000730
731 /* this is done here to keep utmp constants out of loginrec.h */
732 switch (li->type) {
733 case LTYPE_LOGIN:
734 utx->ut_type = USER_PROCESS;
735 break;
736 case LTYPE_LOGOUT:
737 utx->ut_type = DEAD_PROCESS;
738 break;
739 }
andre2ff7b5d2000-06-03 14:57:40 +0000740 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
andre2ff7b5d2000-06-03 14:57:40 +0000741 set_utmpx_time(li, utx);
andre6bb92372000-06-19 08:20:03 +0000742 utx->ut_pid = li->pid;
Damien Miller8899ed32004-09-12 15:18:55 +1000743
Tim Ricee06ae4a2002-02-24 17:56:46 -0800744 /* strncpy(): Don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000745 strncpy(utx->ut_name, li->username,
746 MIN_SIZEOF(utx->ut_name, li->username));
andre6bb92372000-06-19 08:20:03 +0000747
748 if (li->type == LTYPE_LOGOUT)
749 return;
750
Damien Millerdd47aa22000-06-27 11:18:27 +1000751 /*
752 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000753 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000754 */
andre6bb92372000-06-19 08:20:03 +0000755
Damien Millerdd47aa22000-06-27 11:18:27 +1000756# ifdef HAVE_HOST_IN_UTMPX
Damien Miller8899ed32004-09-12 15:18:55 +1000757 strncpy(utx->ut_host, li->hostname,
758 MIN_SIZEOF(utx->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000759# endif
760# ifdef HAVE_ADDR_IN_UTMPX
Damien Millerd6f204d2000-09-23 13:57:27 +1100761 /* this is just a 32-bit IP address */
762 if (li->hostaddr.sa.sa_family == AF_INET)
763 utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Damien Millerdd47aa22000-06-27 11:18:27 +1000764# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100765# ifdef HAVE_ADDR_V6_IN_UTMP
766 /* this is just a 128-bit IPv6 address */
767 if (li->hostaddr.sa.sa_family == AF_INET6) {
768 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
769 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
770 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
771 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
772 ut->ut_addr_v6[1] = 0;
773 ut->ut_addr_v6[2] = 0;
774 ut->ut_addr_v6[3] = 0;
775 }
776 }
777# endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000778# ifdef HAVE_SYSLEN_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +0000779 /* ut_syslen is the length of the utx_host string */
780 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +1000781# endif
andre61e67252000-06-04 17:07:49 +0000782}
Damien Millerdd47aa22000-06-27 11:18:27 +1000783#endif /* USE_UTMPX || USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +0000784
785/**
andre61e67252000-06-04 17:07:49 +0000786 ** Low-level utmp functions
andre2ff7b5d2000-06-03 14:57:40 +0000787 **/
788
789/* FIXME: (ATL) utmp_write_direct needs testing */
andre2ff7b5d2000-06-03 14:57:40 +0000790#ifdef USE_UTMP
791
andre2ff7b5d2000-06-03 14:57:40 +0000792/* if we can, use pututline() etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000793# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
794 defined(HAVE_PUTUTLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000795# define UTMP_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000796# endif
andre2ff7b5d2000-06-03 14:57:40 +0000797
798
799/* write a utmp entry with the system's help (pututline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000800# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000801static int
andre61e67252000-06-04 17:07:49 +0000802utmp_write_library(struct logininfo *li, struct utmp *ut)
803{
andre2ff7b5d2000-06-03 14:57:40 +0000804 setutent();
805 pututline(ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000806# ifdef HAVE_ENDUTENT
andre2ff7b5d2000-06-03 14:57:40 +0000807 endutent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000808# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000809 return (1);
andre61e67252000-06-04 17:07:49 +0000810}
Damien Millerdd47aa22000-06-27 11:18:27 +1000811# else /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000812
Damien Miller94cf4c82005-07-17 17:04:47 +1000813/*
Damien Miller8899ed32004-09-12 15:18:55 +1000814 * Write a utmp entry direct to the file
815 * This is a slightly modification of code in OpenBSD's login.c
816 */
andre2ff7b5d2000-06-03 14:57:40 +0000817static int
andre61e67252000-06-04 17:07:49 +0000818utmp_write_direct(struct logininfo *li, struct utmp *ut)
819{
andre2ff7b5d2000-06-03 14:57:40 +0000820 struct utmp old_ut;
821 register int fd;
822 int tty;
823
andre6bb92372000-06-19 08:20:03 +0000824 /* FIXME: (ATL) ttyslot() needs local implementation */
Damien Miller348c9b72000-08-15 10:01:22 +1000825
Damien Millere5192fa2000-08-29 14:30:37 +1100826#if defined(HAVE_GETTTYENT)
Damien Miller8899ed32004-09-12 15:18:55 +1000827 struct ttyent *ty;
Damien Miller348c9b72000-08-15 10:01:22 +1000828
829 tty=0;
Damien Miller348c9b72000-08-15 10:01:22 +1000830 setttyent();
Damien Miller8899ed32004-09-12 15:18:55 +1000831 while (NULL != (ty = getttyent())) {
Damien Miller348c9b72000-08-15 10:01:22 +1000832 tty++;
833 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
834 break;
835 }
836 endttyent();
837
Damien Miller8899ed32004-09-12 15:18:55 +1000838 if (NULL == ty) {
Damien Miller81409592004-08-15 19:12:52 +1000839 logit("%s: tty not found", __func__);
840 return (0);
Damien Miller348c9b72000-08-15 10:01:22 +1000841 }
842#else /* FIXME */
843
andre2ff7b5d2000-06-03 14:57:40 +0000844 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
845
Damien Millere5192fa2000-08-29 14:30:37 +1100846#endif /* HAVE_GETTTYENT */
Damien Miller348c9b72000-08-15 10:01:22 +1000847
andre2ff7b5d2000-06-03 14:57:40 +0000848 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
Damien Miller81409592004-08-15 19:12:52 +1000849 off_t pos, ret;
850
851 pos = (off_t)tty * sizeof(struct utmp);
852 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000853 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000854 return (0);
855 }
856 if (ret != pos) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000857 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Millerb0419f22004-08-23 21:53:28 +1000858 __func__, tty, UTMP_FILE);
Damien Miller81409592004-08-15 19:12:52 +1000859 return (0);
860 }
andre2ff7b5d2000-06-03 14:57:40 +0000861 /*
862 * Prevent luser from zero'ing out ut_host.
863 * If the new ut_line is empty but the old one is not
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000864 * and ut_line and ut_name match, preserve the old ut_line.
andre2ff7b5d2000-06-03 14:57:40 +0000865 */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000866 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
Damien Miller8899ed32004-09-12 15:18:55 +1000867 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
868 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
869 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0))
870 memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000871
Damien Miller81409592004-08-15 19:12:52 +1000872 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000873 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000874 return (0);
875 }
876 if (ret != pos) {
Damien Millerb0419f22004-08-23 21:53:28 +1000877 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Miller81409592004-08-15 19:12:52 +1000878 __func__, tty, UTMP_FILE);
879 return (0);
880 }
Damien Miller8899ed32004-09-12 15:18:55 +1000881 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
Damien Miller81409592004-08-15 19:12:52 +1000882 logit("%s: error writing %s: %s", __func__,
andre6bb92372000-06-19 08:20:03 +0000883 UTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +1000884 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000885
Damien Miller8899ed32004-09-12 15:18:55 +1000886 close(fd);
887 return (1);
Damien Miller53c5d462000-06-28 00:50:50 +1000888 } else {
Damien Miller8899ed32004-09-12 15:18:55 +1000889 return (0);
Damien Miller53c5d462000-06-28 00:50:50 +1000890 }
andre61e67252000-06-04 17:07:49 +0000891}
Damien Millerdd47aa22000-06-27 11:18:27 +1000892# endif /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000893
894static int
andre61e67252000-06-04 17:07:49 +0000895utmp_perform_login(struct logininfo *li)
896{
andre2ff7b5d2000-06-03 14:57:40 +0000897 struct utmp ut;
898
899 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000900# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000901 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000902 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000903 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000904 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000905# else
andre2ff7b5d2000-06-03 14:57:40 +0000906 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000907 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000908 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000909 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000910# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000911 return (1);
andre61e67252000-06-04 17:07:49 +0000912}
andre2ff7b5d2000-06-03 14:57:40 +0000913
914
915static int
andre61e67252000-06-04 17:07:49 +0000916utmp_perform_logout(struct logininfo *li)
917{
andre2ff7b5d2000-06-03 14:57:40 +0000918 struct utmp ut;
919
andre6bb92372000-06-19 08:20:03 +0000920 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000921# ifdef UTMP_USE_LIBRARY
andre6bb92372000-06-19 08:20:03 +0000922 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000923 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000924 return (0);
andre6bb92372000-06-19 08:20:03 +0000925 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000926# else
andre6bb92372000-06-19 08:20:03 +0000927 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000928 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000929 return (0);
andre6bb92372000-06-19 08:20:03 +0000930 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000931# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000932 return (1);
andre61e67252000-06-04 17:07:49 +0000933}
andre2ff7b5d2000-06-03 14:57:40 +0000934
935
936int
andre61e67252000-06-04 17:07:49 +0000937utmp_write_entry(struct logininfo *li)
938{
andre2ff7b5d2000-06-03 14:57:40 +0000939 switch(li->type) {
940 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +1000941 return (utmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +0000942
943 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +1000944 return (utmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +0000945
946 default:
Damien Miller6b0279c2004-09-12 15:25:17 +1000947 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000948 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000949 }
andre61e67252000-06-04 17:07:49 +0000950}
Damien Millerdd47aa22000-06-27 11:18:27 +1000951#endif /* USE_UTMP */
andre2ff7b5d2000-06-03 14:57:40 +0000952
953
954/**
andre61e67252000-06-04 17:07:49 +0000955 ** Low-level utmpx functions
andre2ff7b5d2000-06-03 14:57:40 +0000956 **/
957
958/* not much point if we don't want utmpx entries */
959#ifdef USE_UTMPX
960
andre2ff7b5d2000-06-03 14:57:40 +0000961/* if we have the wherewithall, use pututxline etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000962# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
963 defined(HAVE_PUTUTXLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000964# define UTMPX_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000965# endif
andre2ff7b5d2000-06-03 14:57:40 +0000966
967
968/* write a utmpx entry with the system's help (pututxline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000969# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000970static int
andre61e67252000-06-04 17:07:49 +0000971utmpx_write_library(struct logininfo *li, struct utmpx *utx)
972{
andre2ff7b5d2000-06-03 14:57:40 +0000973 setutxent();
974 pututxline(utx);
975
Damien Millerdd47aa22000-06-27 11:18:27 +1000976# ifdef HAVE_ENDUTXENT
andre2ff7b5d2000-06-03 14:57:40 +0000977 endutxent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000978# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000979 return (1);
andre61e67252000-06-04 17:07:49 +0000980}
andre2ff7b5d2000-06-03 14:57:40 +0000981
Damien Millerdd47aa22000-06-27 11:18:27 +1000982# else /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000983
984/* write a utmp entry direct to the file */
985static int
andre61e67252000-06-04 17:07:49 +0000986utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000987{
Damien Miller6b0279c2004-09-12 15:25:17 +1000988 logit("%s: not implemented!", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000989 return (0);
andre61e67252000-06-04 17:07:49 +0000990}
Damien Millerdd47aa22000-06-27 11:18:27 +1000991# endif /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000992
993static int
andre61e67252000-06-04 17:07:49 +0000994utmpx_perform_login(struct logininfo *li)
995{
andre2ff7b5d2000-06-03 14:57:40 +0000996 struct utmpx utx;
997
998 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +1000999# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001000 if (!utmpx_write_library(li, &utx)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001001 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001002 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001003 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001004# else
andre2ff7b5d2000-06-03 14:57:40 +00001005 if (!utmpx_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001006 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001007 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001008 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001009# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001010 return (1);
andre61e67252000-06-04 17:07:49 +00001011}
andre2ff7b5d2000-06-03 14:57:40 +00001012
1013
1014static int
andre61e67252000-06-04 17:07:49 +00001015utmpx_perform_logout(struct logininfo *li)
1016{
andre2ff7b5d2000-06-03 14:57:40 +00001017 struct utmpx utx;
1018
Tim Ricee06ae4a2002-02-24 17:56:46 -08001019 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001020# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001021 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +10001022# endif
1023# ifdef HAVE_TYPE_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001024 utx.ut_type = DEAD_PROCESS;
Damien Millerdd47aa22000-06-27 11:18:27 +10001025# endif
andre2ff7b5d2000-06-03 14:57:40 +00001026
Damien Millerdd47aa22000-06-27 11:18:27 +10001027# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001028 utmpx_write_library(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001029# else
andre2ff7b5d2000-06-03 14:57:40 +00001030 utmpx_write_direct(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001031# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001032 return (1);
andre61e67252000-06-04 17:07:49 +00001033}
andre2ff7b5d2000-06-03 14:57:40 +00001034
andre2ff7b5d2000-06-03 14:57:40 +00001035int
andre61e67252000-06-04 17:07:49 +00001036utmpx_write_entry(struct logininfo *li)
1037{
andre2ff7b5d2000-06-03 14:57:40 +00001038 switch(li->type) {
1039 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001040 return (utmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001041 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001042 return (utmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001043 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001044 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001045 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001046 }
andre61e67252000-06-04 17:07:49 +00001047}
Damien Millerdd47aa22000-06-27 11:18:27 +10001048#endif /* USE_UTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001049
1050
1051/**
andre61e67252000-06-04 17:07:49 +00001052 ** Low-level wtmp functions
andre2ff7b5d2000-06-03 14:57:40 +00001053 **/
1054
Kevin Stevesef4eea92001-02-05 12:42:17 +00001055#ifdef USE_WTMP
andre2ff7b5d2000-06-03 14:57:40 +00001056
Damien Miller94cf4c82005-07-17 17:04:47 +10001057/*
Damien Miller8899ed32004-09-12 15:18:55 +10001058 * Write a wtmp entry direct to the end of the file
1059 * This is a slight modification of code in OpenBSD's logwtmp.c
1060 */
andre2ff7b5d2000-06-03 14:57:40 +00001061static int
andre61e67252000-06-04 17:07:49 +00001062wtmp_write(struct logininfo *li, struct utmp *ut)
1063{
andre2ff7b5d2000-06-03 14:57:40 +00001064 struct stat buf;
1065 int fd, ret = 1;
1066
1067 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001068 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001069 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001070 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001071 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001072 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001073 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
andre2ff7b5d2000-06-03 14:57:40 +00001074 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001075 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001076 WTMP_FILE, strerror(errno));
1077 ret = 0;
1078 }
Damien Miller8899ed32004-09-12 15:18:55 +10001079 close(fd);
1080 return (ret);
andre61e67252000-06-04 17:07:49 +00001081}
andre2ff7b5d2000-06-03 14:57:40 +00001082
andre2ff7b5d2000-06-03 14:57:40 +00001083static int
Damien Millerdd47aa22000-06-27 11:18:27 +10001084wtmp_perform_login(struct logininfo *li)
1085{
andre2ff7b5d2000-06-03 14:57:40 +00001086 struct utmp ut;
1087
1088 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001089 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001090}
andre2ff7b5d2000-06-03 14:57:40 +00001091
1092
1093static int
andre61e67252000-06-04 17:07:49 +00001094wtmp_perform_logout(struct logininfo *li)
1095{
andre2ff7b5d2000-06-03 14:57:40 +00001096 struct utmp ut;
1097
1098 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001099 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001100}
andre2ff7b5d2000-06-03 14:57:40 +00001101
1102
1103int
andre61e67252000-06-04 17:07:49 +00001104wtmp_write_entry(struct logininfo *li)
1105{
andre2ff7b5d2000-06-03 14:57:40 +00001106 switch(li->type) {
1107 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001108 return (wtmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001109 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001110 return (wtmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001111 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001112 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001113 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001114 }
andre61e67252000-06-04 17:07:49 +00001115}
andre2ff7b5d2000-06-03 14:57:40 +00001116
1117
Damien Miller94cf4c82005-07-17 17:04:47 +10001118/*
Damien Miller8899ed32004-09-12 15:18:55 +10001119 * Notes on fetching login data from wtmp/wtmpx
Kevin Stevesef4eea92001-02-05 12:42:17 +00001120 *
andre6bb92372000-06-19 08:20:03 +00001121 * Logouts are usually recorded with (amongst other things) a blank
1122 * username on a given tty line. However, some systems (HP-UX is one)
1123 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1124 *
1125 * Since we're only looking for logins here, we know that the username
1126 * must be set correctly. On systems that leave it in, we check for
1127 * ut_type==USER_PROCESS (indicating a login.)
1128 *
1129 * Portability: Some systems may set something other than USER_PROCESS
1130 * to indicate a login process. I don't know of any as I write. Also,
1131 * it's possible that some systems may both leave the username in
1132 * place and not have ut_type.
1133 */
1134
andre6bb92372000-06-19 08:20:03 +00001135/* return true if this wtmp entry indicates a login */
1136static int
1137wtmp_islogin(struct logininfo *li, struct utmp *ut)
1138{
Kevin Stevesef4eea92001-02-05 12:42:17 +00001139 if (strncmp(li->username, ut->ut_name,
Damien Miller8899ed32004-09-12 15:18:55 +10001140 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001141# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001142 if (ut->ut_type & USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001143 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001144# else
Damien Miller8899ed32004-09-12 15:18:55 +10001145 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001146# endif
andre6bb92372000-06-19 08:20:03 +00001147 }
Damien Miller8899ed32004-09-12 15:18:55 +10001148 return (0);
andre6bb92372000-06-19 08:20:03 +00001149}
1150
andre2ff7b5d2000-06-03 14:57:40 +00001151int
andre61e67252000-06-04 17:07:49 +00001152wtmp_get_entry(struct logininfo *li)
1153{
andre2ff7b5d2000-06-03 14:57:40 +00001154 struct stat st;
1155 struct utmp ut;
Damien Miller8899ed32004-09-12 15:18:55 +10001156 int fd, found = 0;
andre6bb92372000-06-19 08:20:03 +00001157
1158 /* Clear the time entries in our logininfo */
1159 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001160
1161 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001162 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001163 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001164 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001165 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001166 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001167 logit("%s: couldn't stat %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001168 WTMP_FILE, strerror(errno));
1169 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001170 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001171 }
andre2ff7b5d2000-06-03 14:57:40 +00001172
andre6bb92372000-06-19 08:20:03 +00001173 /* Seek to the start of the last struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001174 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
andre6bb92372000-06-19 08:20:03 +00001175 /* Looks like we've got a fresh wtmp file */
1176 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001177 return (0);
andre6bb92372000-06-19 08:20:03 +00001178 }
1179
1180 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001181 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001182 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001183 WTMP_FILE, strerror(errno));
1184 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001185 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001186 }
andre6bb92372000-06-19 08:20:03 +00001187 if ( wtmp_islogin(li, &ut) ) {
1188 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001189 /*
1190 * We've already checked for a time in struct
1191 * utmp, in login_getlast()
1192 */
Damien Millerdd47aa22000-06-27 11:18:27 +10001193# ifdef HAVE_TIME_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +00001194 li->tv_sec = ut.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001195# else
andre2ff7b5d2000-06-03 14:57:40 +00001196# if HAVE_TV_IN_UTMP
1197 li->tv_sec = ut.ut_tv.tv_sec;
1198# endif
Damien Millerdd47aa22000-06-27 11:18:27 +10001199# endif
andre6bb92372000-06-19 08:20:03 +00001200 line_fullname(li->line, ut.ut_line,
Damien Miller8899ed32004-09-12 15:18:55 +10001201 MIN_SIZEOF(li->line, ut.ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +10001202# ifdef HAVE_HOST_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001203 strlcpy(li->hostname, ut.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001204 MIN_SIZEOF(li->hostname, ut.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001205# endif
andre6bb92372000-06-19 08:20:03 +00001206 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001207 }
andre6bb92372000-06-19 08:20:03 +00001208 /* Seek back 2 x struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001209 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
andre6bb92372000-06-19 08:20:03 +00001210 /* We've found the start of the file, so quit */
Damien Miller8899ed32004-09-12 15:18:55 +10001211 close(fd);
1212 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001213 }
andre6bb92372000-06-19 08:20:03 +00001214 }
1215
1216 /* We found an entry. Tidy up and return */
1217 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001218 return (1);
andre61e67252000-06-04 17:07:49 +00001219}
Damien Millerdd47aa22000-06-27 11:18:27 +10001220# endif /* USE_WTMP */
andre2ff7b5d2000-06-03 14:57:40 +00001221
1222
1223/**
andre61e67252000-06-04 17:07:49 +00001224 ** Low-level wtmpx functions
andre2ff7b5d2000-06-03 14:57:40 +00001225 **/
1226
1227#ifdef USE_WTMPX
Damien Miller8899ed32004-09-12 15:18:55 +10001228/*
1229 * Write a wtmpx entry direct to the end of the file
1230 * This is a slight modification of code in OpenBSD's logwtmp.c
1231 */
andre2ff7b5d2000-06-03 14:57:40 +00001232static int
andre61e67252000-06-04 17:07:49 +00001233wtmpx_write(struct logininfo *li, struct utmpx *utx)
1234{
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001235#ifndef HAVE_UPDWTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001236 struct stat buf;
1237 int fd, ret = 1;
1238
1239 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001240 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001241 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001242 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001243 }
1244
Kevin Stevesef4eea92001-02-05 12:42:17 +00001245 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001246 if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
andre2ff7b5d2000-06-03 14:57:40 +00001247 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001248 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001249 WTMPX_FILE, strerror(errno));
1250 ret = 0;
1251 }
Damien Miller8899ed32004-09-12 15:18:55 +10001252 close(fd);
andre2ff7b5d2000-06-03 14:57:40 +00001253
Damien Miller8899ed32004-09-12 15:18:55 +10001254 return (ret);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001255#else
1256 updwtmpx(WTMPX_FILE, utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001257 return (1);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001258#endif
andre61e67252000-06-04 17:07:49 +00001259}
andre2ff7b5d2000-06-03 14:57:40 +00001260
1261
1262static int
andre61e67252000-06-04 17:07:49 +00001263wtmpx_perform_login(struct logininfo *li)
1264{
andre2ff7b5d2000-06-03 14:57:40 +00001265 struct utmpx utx;
1266
1267 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001268 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001269}
andre2ff7b5d2000-06-03 14:57:40 +00001270
1271
1272static int
andre61e67252000-06-04 17:07:49 +00001273wtmpx_perform_logout(struct logininfo *li)
1274{
andre2ff7b5d2000-06-03 14:57:40 +00001275 struct utmpx utx;
1276
1277 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001278 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001279}
andre2ff7b5d2000-06-03 14:57:40 +00001280
1281
1282int
andre61e67252000-06-04 17:07:49 +00001283wtmpx_write_entry(struct logininfo *li)
1284{
andre2ff7b5d2000-06-03 14:57:40 +00001285 switch(li->type) {
1286 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001287 return (wtmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001288 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001289 return (wtmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001290 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001291 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001292 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001293 }
andre61e67252000-06-04 17:07:49 +00001294}
andre2ff7b5d2000-06-03 14:57:40 +00001295
andre6bb92372000-06-19 08:20:03 +00001296/* Please see the notes above wtmp_islogin() for information about the
1297 next two functions */
1298
1299/* Return true if this wtmpx entry indicates a login */
1300static int
1301wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1302{
Damien Miller8899ed32004-09-12 15:18:55 +10001303 if (strncmp(li->username, utx->ut_name,
1304 MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001305# ifdef HAVE_TYPE_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +00001306 if (utx->ut_type == USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001307 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001308# else
Damien Miller8899ed32004-09-12 15:18:55 +10001309 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001310# endif
andre6bb92372000-06-19 08:20:03 +00001311 }
Damien Miller8899ed32004-09-12 15:18:55 +10001312 return (0);
andre6bb92372000-06-19 08:20:03 +00001313}
1314
andre2ff7b5d2000-06-03 14:57:40 +00001315
1316int
andre61e67252000-06-04 17:07:49 +00001317wtmpx_get_entry(struct logininfo *li)
1318{
andre2ff7b5d2000-06-03 14:57:40 +00001319 struct stat st;
1320 struct utmpx utx;
andre6bb92372000-06-19 08:20:03 +00001321 int fd, found=0;
1322
1323 /* Clear the time entries */
1324 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001325
1326 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001327 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001328 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001329 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001330 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001331 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001332 logit("%s: couldn't stat %s: %s", __func__,
Tim Ricecdb82942002-07-14 15:33:20 -07001333 WTMPX_FILE, strerror(errno));
andre2ff7b5d2000-06-03 14:57:40 +00001334 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001335 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001336 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001337
andre6bb92372000-06-19 08:20:03 +00001338 /* Seek to the start of the last struct utmpx */
Kevin Steves52172652001-10-02 00:29:00 +00001339 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
andre6bb92372000-06-19 08:20:03 +00001340 /* probably a newly rotated wtmpx file */
1341 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001342 return (0);
andre6bb92372000-06-19 08:20:03 +00001343 }
andre2ff7b5d2000-06-03 14:57:40 +00001344
andre6bb92372000-06-19 08:20:03 +00001345 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001346 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001347 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001348 WTMPX_FILE, strerror(errno));
1349 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001350 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001351 }
Damien Miller8899ed32004-09-12 15:18:55 +10001352 /*
Damien Miller94cf4c82005-07-17 17:04:47 +10001353 * Logouts are recorded as a blank username on a particular
Damien Miller8899ed32004-09-12 15:18:55 +10001354 * line. So, we just need to find the username in struct utmpx
1355 */
1356 if (wtmpx_islogin(li, &utx)) {
Tim Rice370e0ba2002-07-14 15:50:51 -07001357 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001358# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001359 li->tv_sec = utx.ut_tv.tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +10001360# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001361 li->tv_sec = utx.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001362# endif
Damien Miller1a132252000-06-13 21:23:17 +10001363 line_fullname(li->line, utx.ut_line, sizeof(li->line));
Damien Miller8899ed32004-09-12 15:18:55 +10001364# if defined(HAVE_HOST_IN_UTMPX)
andre6bb92372000-06-19 08:20:03 +00001365 strlcpy(li->hostname, utx.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001366 MIN_SIZEOF(li->hostname, utx.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001367# endif
andre6bb92372000-06-19 08:20:03 +00001368 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001369 }
Kevin Steves52172652001-10-02 00:29:00 +00001370 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
Damien Miller8899ed32004-09-12 15:18:55 +10001371 close(fd);
1372 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001373 }
andre6bb92372000-06-19 08:20:03 +00001374 }
1375
1376 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001377 return (1);
andre61e67252000-06-04 17:07:49 +00001378}
Damien Millerd5bf3072000-06-07 21:32:13 +10001379#endif /* USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001380
andre2ff7b5d2000-06-03 14:57:40 +00001381/**
andre61e67252000-06-04 17:07:49 +00001382 ** Low-level libutil login() functions
andre2ff7b5d2000-06-03 14:57:40 +00001383 **/
1384
1385#ifdef USE_LOGIN
andre2ff7b5d2000-06-03 14:57:40 +00001386static int
andre61e67252000-06-04 17:07:49 +00001387syslogin_perform_login(struct logininfo *li)
1388{
andre2ff7b5d2000-06-03 14:57:40 +00001389 struct utmp *ut;
1390
Damien Millerb0aae332004-09-12 15:26:00 +10001391 ut = xmalloc(sizeof(*ut));
andre2ff7b5d2000-06-03 14:57:40 +00001392 construct_utmp(li, ut);
1393 login(ut);
Damien Millerf211efc2003-03-10 11:23:06 +11001394 free(ut);
andre2ff7b5d2000-06-03 14:57:40 +00001395
Damien Miller8899ed32004-09-12 15:18:55 +10001396 return (1);
andre61e67252000-06-04 17:07:49 +00001397}
1398
andre2ff7b5d2000-06-03 14:57:40 +00001399static int
andre61e67252000-06-04 17:07:49 +00001400syslogin_perform_logout(struct logininfo *li)
1401{
Damien Millerdd47aa22000-06-27 11:18:27 +10001402# ifdef HAVE_LOGOUT
Darren Tucker4d2f3612004-04-08 10:57:05 +10001403 char line[UT_LINESIZE];
Kevin Stevesef4eea92001-02-05 12:42:17 +00001404
andre2ff7b5d2000-06-03 14:57:40 +00001405 (void)line_stripname(line, li->line, sizeof(line));
1406
Damien Miller8899ed32004-09-12 15:18:55 +10001407 if (!logout(line))
Damien Miller6b0279c2004-09-12 15:25:17 +10001408 logit("%s: logout() returned an error", __func__);
Damien Millerdd47aa22000-06-27 11:18:27 +10001409# ifdef HAVE_LOGWTMP
Damien Miller8899ed32004-09-12 15:18:55 +10001410 else
andre2ff7b5d2000-06-03 14:57:40 +00001411 logwtmp(line, "", "");
Damien Millerdd47aa22000-06-27 11:18:27 +10001412# endif
andre6bb92372000-06-19 08:20:03 +00001413 /* FIXME: (ATL - if the need arises) What to do if we have
1414 * login, but no logout? what if logout but no logwtmp? All
1415 * routines are in libutil so they should all be there,
1416 * but... */
Damien Millerdd47aa22000-06-27 11:18:27 +10001417# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001418 return (1);
andre61e67252000-06-04 17:07:49 +00001419}
andre2ff7b5d2000-06-03 14:57:40 +00001420
andre2ff7b5d2000-06-03 14:57:40 +00001421int
andre61e67252000-06-04 17:07:49 +00001422syslogin_write_entry(struct logininfo *li)
1423{
andre2ff7b5d2000-06-03 14:57:40 +00001424 switch (li->type) {
1425 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001426 return (syslogin_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001427 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001428 return (syslogin_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001429 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001430 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001431 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001432 }
andre61e67252000-06-04 17:07:49 +00001433}
Damien Millerd5bf3072000-06-07 21:32:13 +10001434#endif /* USE_LOGIN */
andre2ff7b5d2000-06-03 14:57:40 +00001435
1436/* end of file log-syslogin.c */
1437
andre2ff7b5d2000-06-03 14:57:40 +00001438/**
andre61e67252000-06-04 17:07:49 +00001439 ** Low-level lastlog functions
andre2ff7b5d2000-06-03 14:57:40 +00001440 **/
1441
1442#ifdef USE_LASTLOG
Damien Millerdd47aa22000-06-27 11:18:27 +10001443#define LL_FILE 1
1444#define LL_DIR 2
1445#define LL_OTHER 3
andre2ff7b5d2000-06-03 14:57:40 +00001446
andre2ff7b5d2000-06-03 14:57:40 +00001447static void
andre61e67252000-06-04 17:07:49 +00001448lastlog_construct(struct logininfo *li, struct lastlog *last)
1449{
andre2ff7b5d2000-06-03 14:57:40 +00001450 /* clear the structure */
Damien Miller348c9b72000-08-15 10:01:22 +10001451 memset(last, '\0', sizeof(*last));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001452
Damien Miller8899ed32004-09-12 15:18:55 +10001453 line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
andre6bb92372000-06-19 08:20:03 +00001454 strlcpy(last->ll_host, li->hostname,
1455 MIN_SIZEOF(last->ll_host, li->hostname));
andre2ff7b5d2000-06-03 14:57:40 +00001456 last->ll_time = li->tv_sec;
andre61e67252000-06-04 17:07:49 +00001457}
andre2ff7b5d2000-06-03 14:57:40 +00001458
andre2ff7b5d2000-06-03 14:57:40 +00001459static int
andre61e67252000-06-04 17:07:49 +00001460lastlog_filetype(char *filename)
1461{
andre2ff7b5d2000-06-03 14:57:40 +00001462 struct stat st;
1463
Damien Millerdd47aa22000-06-27 11:18:27 +10001464 if (stat(LASTLOG_FILE, &st) != 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001465 logit("%s: Couldn't stat %s: %s", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001466 LASTLOG_FILE, strerror(errno));
1467 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001468 }
andre2ff7b5d2000-06-03 14:57:40 +00001469 if (S_ISDIR(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001470 return (LL_DIR);
andre2ff7b5d2000-06-03 14:57:40 +00001471 else if (S_ISREG(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001472 return (LL_FILE);
andre2ff7b5d2000-06-03 14:57:40 +00001473 else
Damien Miller8899ed32004-09-12 15:18:55 +10001474 return (LL_OTHER);
andre61e67252000-06-04 17:07:49 +00001475}
andre2ff7b5d2000-06-03 14:57:40 +00001476
1477
1478/* open the file (using filemode) and seek to the login entry */
1479static int
andre61e67252000-06-04 17:07:49 +00001480lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1481{
andre2ff7b5d2000-06-03 14:57:40 +00001482 off_t offset;
1483 int type;
1484 char lastlog_file[1024];
1485
1486 type = lastlog_filetype(LASTLOG_FILE);
1487 switch (type) {
Damien Miller8899ed32004-09-12 15:18:55 +10001488 case LL_FILE:
1489 strlcpy(lastlog_file, LASTLOG_FILE,
1490 sizeof(lastlog_file));
1491 break;
1492 case LL_DIR:
1493 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1494 LASTLOG_FILE, li->username);
1495 break;
1496 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001497 logit("%s: %.100s is not a file or directory!", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001498 LASTLOG_FILE);
1499 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001500 }
andre2ff7b5d2000-06-03 14:57:40 +00001501
Damien Miller405dc602003-04-09 21:12:52 +10001502 *fd = open(lastlog_file, filemode, 0600);
Damien Miller8899ed32004-09-12 15:18:55 +10001503 if (*fd < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001504 debug("%s: Couldn't open %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001505 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001506 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001507 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001508
Damien Millere477ef62000-08-15 10:21:17 +10001509 if (type == LL_FILE) {
1510 /* find this uid's offset in the lastlog file */
Kevin Steves52172652001-10-02 00:29:00 +00001511 offset = (off_t) ((long)li->uid * sizeof(struct lastlog));
andre2ff7b5d2000-06-03 14:57:40 +00001512
Damien Miller8899ed32004-09-12 15:18:55 +10001513 if (lseek(*fd, offset, SEEK_SET) != offset) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001514 logit("%s: %s->lseek(): %s", __func__,
1515 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001516 return (0);
Damien Millere477ef62000-08-15 10:21:17 +10001517 }
andre2ff7b5d2000-06-03 14:57:40 +00001518 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001519
Damien Miller8899ed32004-09-12 15:18:55 +10001520 return (1);
andre61e67252000-06-04 17:07:49 +00001521}
andre2ff7b5d2000-06-03 14:57:40 +00001522
1523static int
andre61e67252000-06-04 17:07:49 +00001524lastlog_perform_login(struct logininfo *li)
1525{
andre2ff7b5d2000-06-03 14:57:40 +00001526 struct lastlog last;
1527 int fd;
1528
1529 /* create our struct lastlog */
1530 lastlog_construct(li, &last);
1531
Damien Miller405dc602003-04-09 21:12:52 +10001532 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
Damien Miller8899ed32004-09-12 15:18:55 +10001533 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001534
andre2ff7b5d2000-06-03 14:57:40 +00001535 /* write the entry */
Darren Tucker8661b562003-07-06 15:20:46 +10001536 if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) {
Damien Millerc1132e72000-08-18 14:08:38 +10001537 close(fd);
Damien Miller6b0279c2004-09-12 15:25:17 +10001538 logit("%s: Error writing to %s: %s", __func__,
Damien Millerc1132e72000-08-18 14:08:38 +10001539 LASTLOG_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001540 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001541 }
Damien Millerc1132e72000-08-18 14:08:38 +10001542
1543 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001544 return (1);
andre61e67252000-06-04 17:07:49 +00001545}
andre2ff7b5d2000-06-03 14:57:40 +00001546
andre2ff7b5d2000-06-03 14:57:40 +00001547int
andre61e67252000-06-04 17:07:49 +00001548lastlog_write_entry(struct logininfo *li)
1549{
andre2ff7b5d2000-06-03 14:57:40 +00001550 switch(li->type) {
1551 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001552 return (lastlog_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001553 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001554 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001555 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001556 }
andre61e67252000-06-04 17:07:49 +00001557}
andre2ff7b5d2000-06-03 14:57:40 +00001558
andre2ff7b5d2000-06-03 14:57:40 +00001559static void
andre61e67252000-06-04 17:07:49 +00001560lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1561{
andre2ff7b5d2000-06-03 14:57:40 +00001562 line_fullname(li->line, last->ll_line, sizeof(li->line));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001563 strlcpy(li->hostname, last->ll_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001564 MIN_SIZEOF(li->hostname, last->ll_host));
andre2ff7b5d2000-06-03 14:57:40 +00001565 li->tv_sec = last->ll_time;
andre61e67252000-06-04 17:07:49 +00001566}
andre2ff7b5d2000-06-03 14:57:40 +00001567
andre2ff7b5d2000-06-03 14:57:40 +00001568int
andre61e67252000-06-04 17:07:49 +00001569lastlog_get_entry(struct logininfo *li)
1570{
andre2ff7b5d2000-06-03 14:57:40 +00001571 struct lastlog last;
Damien Miller7df881d2003-01-07 16:46:58 +11001572 int fd, ret;
andre2ff7b5d2000-06-03 14:57:40 +00001573
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001574 if (!lastlog_openseek(li, &fd, O_RDONLY))
Damien Miller7df881d2003-01-07 16:46:58 +11001575 return (0);
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001576
Damien Miller7df881d2003-01-07 16:46:58 +11001577 ret = atomicio(read, fd, &last, sizeof(last));
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001578 close(fd);
1579
Damien Miller7df881d2003-01-07 16:46:58 +11001580 switch (ret) {
1581 case 0:
1582 memset(&last, '\0', sizeof(last));
1583 /* FALLTHRU */
1584 case sizeof(last):
1585 lastlog_populate_entry(li, &last);
1586 return (1);
1587 case -1:
Damien Millera8e06ce2003-11-21 23:48:55 +11001588 error("%s: Error reading from %s: %s", __func__,
Damien Miller7df881d2003-01-07 16:46:58 +11001589 LASTLOG_FILE, strerror(errno));
1590 return (0);
1591 default:
1592 error("%s: Error reading from %s: Expecting %d, got %d",
Darren Tuckerefc17472005-11-22 19:55:13 +11001593 __func__, LASTLOG_FILE, (int)sizeof(last), ret);
Damien Miller7df881d2003-01-07 16:46:58 +11001594 return (0);
1595 }
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001596
Damien Miller7df881d2003-01-07 16:46:58 +11001597 /* NOTREACHED */
1598 return (0);
andre61e67252000-06-04 17:07:49 +00001599}
Damien Millerd5bf3072000-06-07 21:32:13 +10001600#endif /* USE_LASTLOG */
Darren Tucker2fba9932005-02-02 23:30:24 +11001601
1602#ifdef USE_BTMP
1603 /*
1604 * Logs failed login attempts in _PATH_BTMP if that exists.
1605 * The most common login failure is to give password instead of username.
1606 * So the _PATH_BTMP file checked for the correct permission, so that
1607 * only root can read it.
1608 */
1609
1610void
1611record_failed_login(const char *username, const char *hostname,
1612 const char *ttyn)
1613{
1614 int fd;
1615 struct utmp ut;
1616 struct sockaddr_storage from;
Darren Tuckerefc17472005-11-22 19:55:13 +11001617 socklen_t fromlen = sizeof(from);
Darren Tucker2fba9932005-02-02 23:30:24 +11001618 struct sockaddr_in *a4;
1619 struct sockaddr_in6 *a6;
1620 time_t t;
1621 struct stat fst;
1622
1623 if (geteuid() != 0)
1624 return;
1625 if ((fd = open(_PATH_BTMP, O_WRONLY | O_APPEND)) < 0) {
1626 debug("Unable to open the btmp file %s: %s", _PATH_BTMP,
1627 strerror(errno));
1628 return;
1629 }
1630 if (fstat(fd, &fst) < 0) {
1631 logit("%s: fstat of %s failed: %s", __func__, _PATH_BTMP,
1632 strerror(errno));
1633 goto out;
1634 }
1635 if((fst.st_mode & (S_IRWXG | S_IRWXO)) || (fst.st_uid != 0)){
1636 logit("Excess permission or bad ownership on file %s",
1637 _PATH_BTMP);
1638 goto out;
1639 }
1640
1641 memset(&ut, 0, sizeof(ut));
1642 /* strncpy because we don't necessarily want nul termination */
1643 strncpy(ut.ut_user, username, sizeof(ut.ut_user));
1644 strlcpy(ut.ut_line, "ssh:notty", sizeof(ut.ut_line));
1645
1646 time(&t);
1647 ut.ut_time = t; /* ut_time is not always a time_t */
1648 ut.ut_type = LOGIN_PROCESS;
1649 ut.ut_pid = getpid();
1650
1651 /* strncpy because we don't necessarily want nul termination */
1652 strncpy(ut.ut_host, hostname, sizeof(ut.ut_host));
1653
1654 if (packet_connection_is_on_socket() &&
1655 getpeername(packet_get_connection_in(),
1656 (struct sockaddr *)&from, &fromlen) == 0) {
1657 ipv64_normalise_mapped(&from, &fromlen);
1658 if (from.ss_family == AF_INET) {
1659 a4 = (struct sockaddr_in *)&from;
1660 memcpy(&ut.ut_addr, &(a4->sin_addr),
1661 MIN_SIZEOF(ut.ut_addr, a4->sin_addr));
1662 }
1663#ifdef HAVE_ADDR_V6_IN_UTMP
1664 if (from.ss_family == AF_INET6) {
1665 a6 = (struct sockaddr_in6 *)&from;
1666 memcpy(&ut.ut_addr_v6, &(a6->sin6_addr),
1667 MIN_SIZEOF(ut.ut_addr_v6, a6->sin6_addr));
1668 }
1669#endif
1670 }
1671
1672 if (atomicio(vwrite, fd, &ut, sizeof(ut)) != sizeof(ut))
1673 error("Failed to write to %s: %s", _PATH_BTMP,
1674 strerror(errno));
1675
1676out:
1677 close(fd);
1678}
1679#endif /* USE_BTMP */