blob: 8299b79e429be03850406f468435ef5c7e03ec23 [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>
159
andre2ff7b5d2000-06-03 14:57:40 +0000160#include "ssh.h"
161#include "xmalloc.h"
162#include "loginrec.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000163#include "log.h"
164#include "atomicio.h"
Darren Tucker2fba9932005-02-02 23:30:24 +1100165#include "packet.h"
166#include "canohost.h"
Darren Tucker269a1ea2005-02-03 00:20:53 +1100167#include "auth.h"
Darren Tuckera39f83e2005-02-15 22:19:28 +1100168#include "buffer.h"
andre2ff7b5d2000-06-03 14:57:40 +0000169
Ben Lindstromdcca9812000-11-10 03:28:31 +0000170#ifdef HAVE_UTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000171# include <util.h>
Ben Lindstromdcca9812000-11-10 03:28:31 +0000172#endif
andre2ff7b5d2000-06-03 14:57:40 +0000173
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000174#ifdef HAVE_LIBUTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000175# include <libutil.h>
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000176#endif
177
andre2ff7b5d2000-06-03 14:57:40 +0000178/**
179 ** prototypes for helper functions in this file
180 **/
181
182#if HAVE_UTMP_H
andre2ff7b5d2000-06-03 14:57:40 +0000183void set_utmp_time(struct logininfo *li, struct utmp *ut);
184void construct_utmp(struct logininfo *li, struct utmp *ut);
185#endif
186
187#ifdef HAVE_UTMPX_H
andre2ff7b5d2000-06-03 14:57:40 +0000188void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
189void construct_utmpx(struct logininfo *li, struct utmpx *ut);
190#endif
191
192int utmp_write_entry(struct logininfo *li);
193int utmpx_write_entry(struct logininfo *li);
194int wtmp_write_entry(struct logininfo *li);
195int wtmpx_write_entry(struct logininfo *li);
196int lastlog_write_entry(struct logininfo *li);
197int syslogin_write_entry(struct logininfo *li);
198
199int getlast_entry(struct logininfo *li);
200int lastlog_get_entry(struct logininfo *li);
201int wtmp_get_entry(struct logininfo *li);
202int wtmpx_get_entry(struct logininfo *li);
203
Darren Tucker691d5232005-02-15 21:45:57 +1100204extern Buffer loginmsg;
205
andre6bb92372000-06-19 08:20:03 +0000206/* pick the shortest string */
Damien Miller8899ed32004-09-12 15:18:55 +1000207#define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2))
andre6bb92372000-06-19 08:20:03 +0000208
andre2ff7b5d2000-06-03 14:57:40 +0000209/**
210 ** platform-independent login functions
211 **/
212
Damien Miller8899ed32004-09-12 15:18:55 +1000213/*
214 * login_login(struct logininfo *) - Record a login
Kevin Stevesef4eea92001-02-05 12:42:17 +0000215 *
andre6bb92372000-06-19 08:20:03 +0000216 * Call with a pointer to a struct logininfo initialised with
217 * login_init_entry() or login_alloc_entry()
218 *
219 * Returns:
220 * >0 if successful
221 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
222 */
andre61e67252000-06-04 17:07:49 +0000223int
Damien Miller8899ed32004-09-12 15:18:55 +1000224login_login(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000225{
226 li->type = LTYPE_LOGIN;
Damien Miller8899ed32004-09-12 15:18:55 +1000227 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000228}
229
230
Damien Miller8899ed32004-09-12 15:18:55 +1000231/*
232 * login_logout(struct logininfo *) - Record a logout
andre6bb92372000-06-19 08:20:03 +0000233 *
234 * Call as with login_login()
235 *
236 * Returns:
237 * >0 if successful
238 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
239 */
andre61e67252000-06-04 17:07:49 +0000240int
241login_logout(struct logininfo *li)
242{
243 li->type = LTYPE_LOGOUT;
Damien Miller8899ed32004-09-12 15:18:55 +1000244 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000245}
246
Damien Miller8899ed32004-09-12 15:18:55 +1000247/*
248 * login_get_lastlog_time(int) - Retrieve the last login time
andre6bb92372000-06-19 08:20:03 +0000249 *
250 * Retrieve the last login time for the given uid. Will try to use the
251 * system lastlog facilities if they are available, but will fall back
252 * to looking in wtmp/wtmpx if necessary
253 *
254 * Returns:
255 * 0 on failure, or if user has never logged in
256 * Time in seconds from the epoch if successful
257 *
258 * Useful preprocessor symbols:
259 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
260 * info
261 * USE_LASTLOG: If set, indicates the presence of system lastlog
262 * facilities. If this and DISABLE_LASTLOG are not set,
263 * try to retrieve lastlog information from wtmp/wtmpx.
264 */
andre61e67252000-06-04 17:07:49 +0000265unsigned int
266login_get_lastlog_time(const int uid)
267{
268 struct logininfo li;
269
andre6bb92372000-06-19 08:20:03 +0000270 if (login_get_lastlog(&li, uid))
Damien Miller8899ed32004-09-12 15:18:55 +1000271 return (li.tv_sec);
andre6bb92372000-06-19 08:20:03 +0000272 else
Damien Miller8899ed32004-09-12 15:18:55 +1000273 return (0);
andre61e67252000-06-04 17:07:49 +0000274}
275
Damien Miller8899ed32004-09-12 15:18:55 +1000276/*
277 * login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
andre6bb92372000-06-19 08:20:03 +0000278 *
279 * Retrieve a logininfo structure populated (only partially) with
280 * information from the system lastlog data, or from wtmp/wtmpx if no
281 * system lastlog information exists.
282 *
283 * Note this routine must be given a pre-allocated logininfo.
284 *
285 * Returns:
286 * >0: A pointer to your struct logininfo if successful
287 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
andre6bb92372000-06-19 08:20:03 +0000288 */
andre61e67252000-06-04 17:07:49 +0000289struct logininfo *
290login_get_lastlog(struct logininfo *li, const int uid)
291{
andre6bb92372000-06-19 08:20:03 +0000292 struct passwd *pw;
andre6bb92372000-06-19 08:20:03 +0000293
Damien Miller348c9b72000-08-15 10:01:22 +1000294 memset(li, '\0', sizeof(*li));
andre61e67252000-06-04 17:07:49 +0000295 li->uid = uid;
andre6bb92372000-06-19 08:20:03 +0000296
Kevin Stevesef4eea92001-02-05 12:42:17 +0000297 /*
Damien Miller53c5d462000-06-28 00:50:50 +1000298 * If we don't have a 'real' lastlog, we need the username to
andre6bb92372000-06-19 08:20:03 +0000299 * reliably search wtmp(x) for the last login (see
Kevin Stevesef4eea92001-02-05 12:42:17 +0000300 * wtmp_get_entry().)
Damien Miller53c5d462000-06-28 00:50:50 +1000301 */
andre6bb92372000-06-19 08:20:03 +0000302 pw = getpwuid(uid);
Damien Millerdd47aa22000-06-27 11:18:27 +1000303 if (pw == NULL)
Damien Miller6b0279c2004-09-12 15:25:17 +1000304 fatal("%s: Cannot find account for uid %i", __func__, uid);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000305
andre98cabe02000-06-19 09:11:30 +0000306 /* No MIN_SIZEOF here - we absolutely *must not* truncate the
Damien Miller8899ed32004-09-12 15:18:55 +1000307 * username (XXX - so check for trunc!) */
Damien Millerf8af08d2000-06-27 09:40:06 +1000308 strlcpy(li->username, pw->pw_name, sizeof(li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000309
andre61e67252000-06-04 17:07:49 +0000310 if (getlast_entry(li))
Damien Miller8899ed32004-09-12 15:18:55 +1000311 return (li);
andre61e67252000-06-04 17:07:49 +0000312 else
Damien Miller8899ed32004-09-12 15:18:55 +1000313 return (NULL);
andre61e67252000-06-04 17:07:49 +0000314}
315
316
Damien Miller8899ed32004-09-12 15:18:55 +1000317/*
318 * login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
Kevin Stevesef4eea92001-02-05 12:42:17 +0000319 * a logininfo structure
320 *
andre6bb92372000-06-19 08:20:03 +0000321 * This function creates a new struct logininfo, a data structure
322 * meant to carry the information required to portably record login info.
323 *
324 * Returns a pointer to a newly created struct logininfo. If memory
325 * allocation fails, the program halts.
326 */
andre61e67252000-06-04 17:07:49 +0000327struct
328logininfo *login_alloc_entry(int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000329 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000330{
andre2ff7b5d2000-06-03 14:57:40 +0000331 struct logininfo *newli;
332
Damien Miller8899ed32004-09-12 15:18:55 +1000333 newli = xmalloc(sizeof(*newli));
334 login_init_entry(newli, pid, username, hostname, line);
335 return (newli);
andre61e67252000-06-04 17:07:49 +0000336}
andre2ff7b5d2000-06-03 14:57:40 +0000337
338
andre6bb92372000-06-19 08:20:03 +0000339/* login_free_entry(struct logininfo *) - free struct memory */
andre61e67252000-06-04 17:07:49 +0000340void
341login_free_entry(struct logininfo *li)
342{
343 xfree(li);
344}
345
andre2ff7b5d2000-06-03 14:57:40 +0000346
andre6bb92372000-06-19 08:20:03 +0000347/* login_init_entry(struct logininfo *, int, char*, char*, char*)
348 * - initialise a struct logininfo
Kevin Stevesef4eea92001-02-05 12:42:17 +0000349 *
andre6bb92372000-06-19 08:20:03 +0000350 * Populates a new struct logininfo, a data structure meant to carry
351 * the information required to portably record login info.
352 *
353 * Returns: 1
354 */
andre61e67252000-06-04 17:07:49 +0000355int
Kevin Stevesef4eea92001-02-05 12:42:17 +0000356login_init_entry(struct logininfo *li, int pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000357 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000358{
Damien Millerf8af08d2000-06-27 09:40:06 +1000359 struct passwd *pw;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000360
Damien Miller348c9b72000-08-15 10:01:22 +1000361 memset(li, 0, sizeof(*li));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000362
andre61e67252000-06-04 17:07:49 +0000363 li->pid = pid;
Damien Millerf8af08d2000-06-27 09:40:06 +1000364
andre2ff7b5d2000-06-03 14:57:40 +0000365 /* set the line information */
andre61e67252000-06-04 17:07:49 +0000366 if (line)
andre2ff7b5d2000-06-03 14:57:40 +0000367 line_fullname(li->line, line, sizeof(li->line));
andre2ff7b5d2000-06-03 14:57:40 +0000368
Damien Millerf8af08d2000-06-27 09:40:06 +1000369 if (username) {
andre2ff7b5d2000-06-03 14:57:40 +0000370 strlcpy(li->username, username, sizeof(li->username));
Damien Millerf8af08d2000-06-27 09:40:06 +1000371 pw = getpwnam(li->username);
Damien Miller8899ed32004-09-12 15:18:55 +1000372 if (pw == NULL) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000373 fatal("%s: Cannot find user \"%s\"", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +1000374 li->username);
375 }
Damien Millerf8af08d2000-06-27 09:40:06 +1000376 li->uid = pw->pw_uid;
377 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000378
andre61e67252000-06-04 17:07:49 +0000379 if (hostname)
andre2ff7b5d2000-06-03 14:57:40 +0000380 strlcpy(li->hostname, hostname, sizeof(li->hostname));
Damien Millerf8af08d2000-06-27 09:40:06 +1000381
Damien Miller8899ed32004-09-12 15:18:55 +1000382 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000383}
384
Damien Miller94cf4c82005-07-17 17:04:47 +1000385/*
Damien Miller8899ed32004-09-12 15:18:55 +1000386 * login_set_current_time(struct logininfo *) - set the current time
andre6bb92372000-06-19 08:20:03 +0000387 *
388 * Set the current time in a logininfo structure. This function is
389 * meant to eliminate the need to deal with system dependencies for
390 * time handling.
391 */
andre2ff7b5d2000-06-03 14:57:40 +0000392void
andre61e67252000-06-04 17:07:49 +0000393login_set_current_time(struct logininfo *li)
394{
andre2ff7b5d2000-06-03 14:57:40 +0000395 struct timeval tv;
396
397 gettimeofday(&tv, NULL);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000398
Damien Millerf8af08d2000-06-27 09:40:06 +1000399 li->tv_sec = tv.tv_sec;
400 li->tv_usec = tv.tv_usec;
andre2ff7b5d2000-06-03 14:57:40 +0000401}
402
andre61e67252000-06-04 17:07:49 +0000403/* copy a sockaddr_* into our logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000404void
andre61e67252000-06-04 17:07:49 +0000405login_set_addr(struct logininfo *li, const struct sockaddr *sa,
Damien Miller8899ed32004-09-12 15:18:55 +1000406 const unsigned int sa_size)
andre61e67252000-06-04 17:07:49 +0000407{
408 unsigned int bufsize = sa_size;
409
410 /* make sure we don't overrun our union */
411 if (sizeof(li->hostaddr) < sa_size)
412 bufsize = sizeof(li->hostaddr);
413
Damien Miller8899ed32004-09-12 15:18:55 +1000414 memcpy(&li->hostaddr.sa, sa, bufsize);
andre2ff7b5d2000-06-03 14:57:40 +0000415}
416
andre2ff7b5d2000-06-03 14:57:40 +0000417
andre61e67252000-06-04 17:07:49 +0000418/**
419 ** login_write: Call low-level recording functions based on autoconf
420 ** results
421 **/
andre2ff7b5d2000-06-03 14:57:40 +0000422int
Damien Miller8899ed32004-09-12 15:18:55 +1000423login_write(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000424{
Damien Millerbac2d8a2000-09-05 16:13:06 +1100425#ifndef HAVE_CYGWIN
Damien Miller8899ed32004-09-12 15:18:55 +1000426 if (geteuid() != 0) {
427 logit("Attempt to write login records by non-root user (aborting)");
428 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000429 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100430#endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000431
andre2ff7b5d2000-06-03 14:57:40 +0000432 /* set the timestamp */
433 login_set_current_time(li);
434#ifdef USE_LOGIN
435 syslogin_write_entry(li);
436#endif
437#ifdef USE_LASTLOG
Damien Miller8899ed32004-09-12 15:18:55 +1000438 if (li->type == LTYPE_LOGIN)
andre2ff7b5d2000-06-03 14:57:40 +0000439 lastlog_write_entry(li);
andre2ff7b5d2000-06-03 14:57:40 +0000440#endif
441#ifdef USE_UTMP
442 utmp_write_entry(li);
443#endif
444#ifdef USE_WTMP
445 wtmp_write_entry(li);
446#endif
447#ifdef USE_UTMPX
448 utmpx_write_entry(li);
449#endif
450#ifdef USE_WTMPX
451 wtmpx_write_entry(li);
452#endif
Darren Tucker397a2f22004-08-15 00:09:11 +1000453#ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN
Damien Miller94cf4c82005-07-17 17:04:47 +1000454 if (li->type == LTYPE_LOGIN &&
Damien Millerb6f72f52005-07-17 17:26:43 +1000455 !sys_auth_record_login(li->username,li->hostname,li->line,
456 &loginmsg))
Darren Tucker397a2f22004-08-15 00:09:11 +1000457 logit("Writing login record failed for %s", li->username);
458#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100459#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100460 if (li->type == LTYPE_LOGIN)
461 audit_session_open(li->line);
462 else if (li->type == LTYPE_LOGOUT)
463 audit_session_close(li->line);
464#endif
Damien Miller8899ed32004-09-12 15:18:55 +1000465 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000466}
467
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000468#ifdef LOGIN_NEEDS_UTMPX
469int
470login_utmp_only(struct logininfo *li)
471{
Damien Millera8e06ce2003-11-21 23:48:55 +1100472 li->type = LTYPE_LOGIN;
Ben Lindstrom9197c592001-10-26 15:56:55 +0000473 login_set_current_time(li);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000474# ifdef USE_UTMP
475 utmp_write_entry(li);
476# endif
477# ifdef USE_WTMP
478 wtmp_write_entry(li);
479# endif
480# ifdef USE_UTMPX
481 utmpx_write_entry(li);
482# endif
483# ifdef USE_WTMPX
484 wtmpx_write_entry(li);
485# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000486 return (0);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000487}
488#endif
489
andre2ff7b5d2000-06-03 14:57:40 +0000490/**
andre61e67252000-06-04 17:07:49 +0000491 ** getlast_entry: Call low-level functions to retrieve the last login
492 ** time.
andre2ff7b5d2000-06-03 14:57:40 +0000493 **/
494
andre61e67252000-06-04 17:07:49 +0000495/* take the uid in li and return the last login time */
496int
497getlast_entry(struct logininfo *li)
498{
499#ifdef USE_LASTLOG
Damien Miller53c5d462000-06-28 00:50:50 +1000500 return(lastlog_get_entry(li));
Damien Millerdd47aa22000-06-27 11:18:27 +1000501#else /* !USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000502
Damien Miller8899ed32004-09-12 15:18:55 +1000503#if defined(DISABLE_LASTLOG)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000504 /* On some systems we shouldn't even try to obtain last login
andreecaabf12000-06-12 22:21:44 +0000505 * time, e.g. AIX */
Damien Miller8899ed32004-09-12 15:18:55 +1000506 return (0);
507# elif defined(USE_WTMP) && \
508 (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
andre61e67252000-06-04 17:07:49 +0000509 /* retrieve last login time from utmp */
Damien Millerdd47aa22000-06-27 11:18:27 +1000510 return (wtmp_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000511# elif defined(USE_WTMPX) && \
512 (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
andre61e67252000-06-04 17:07:49 +0000513 /* If wtmp isn't available, try wtmpx */
Damien Millerdd47aa22000-06-27 11:18:27 +1000514 return (wtmpx_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000515# else
andre61e67252000-06-04 17:07:49 +0000516 /* Give up: No means of retrieving last login time */
Damien Miller8899ed32004-09-12 15:18:55 +1000517 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000518# endif /* DISABLE_LASTLOG */
Damien Millerdd47aa22000-06-27 11:18:27 +1000519#endif /* USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000520}
521
522
523
andre2ff7b5d2000-06-03 14:57:40 +0000524/*
andre61e67252000-06-04 17:07:49 +0000525 * 'line' string utility functions
526 *
527 * These functions process the 'line' string into one of three forms:
528 *
andre2ff7b5d2000-06-03 14:57:40 +0000529 * 1. The full filename (including '/dev')
530 * 2. The stripped name (excluding '/dev')
andre61e67252000-06-04 17:07:49 +0000531 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
532 * /dev/pts/1 -> ts/1 )
andre2ff7b5d2000-06-03 14:57:40 +0000533 *
534 * Form 3 is used on some systems to identify a .tmp.? entry when
535 * attempting to remove it. Typically both addition and removal is
andre61e67252000-06-04 17:07:49 +0000536 * performed by one application - say, sshd - so as long as the choice
537 * uniquely identifies a terminal it's ok.
andre2ff7b5d2000-06-03 14:57:40 +0000538 */
539
540
Damien Miller8899ed32004-09-12 15:18:55 +1000541/*
542 * line_fullname(): add the leading '/dev/' if it doesn't exist make
543 * sure dst has enough space, if not just copy src (ugh)
544 */
andre2ff7b5d2000-06-03 14:57:40 +0000545char *
Damien Miller52c8afe2005-06-19 10:19:43 +1000546line_fullname(char *dst, const char *src, u_int dstsize)
andre61e67252000-06-04 17:07:49 +0000547{
andre2ff7b5d2000-06-03 14:57:40 +0000548 memset(dst, '\0', dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000549 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
andre2ff7b5d2000-06-03 14:57:40 +0000550 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000551 else {
Damien Miller1a132252000-06-13 21:23:17 +1000552 strlcpy(dst, "/dev/", dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000553 strlcat(dst, src, dstsize);
554 }
Damien Miller8899ed32004-09-12 15:18:55 +1000555 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000556}
557
andre61e67252000-06-04 17:07:49 +0000558/* line_stripname(): strip the leading '/dev' if it exists, return dst */
andre2ff7b5d2000-06-03 14:57:40 +0000559char *
andre61e67252000-06-04 17:07:49 +0000560line_stripname(char *dst, const char *src, int dstsize)
561{
andre2ff7b5d2000-06-03 14:57:40 +0000562 memset(dst, '\0', dstsize);
563 if (strncmp(src, "/dev/", 5) == 0)
Damien Millerf5a81472000-09-30 21:34:44 +1100564 strlcpy(dst, src + 5, dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000565 else
566 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000567 return (dst);
andre61e67252000-06-04 17:07:49 +0000568}
569
Damien Miller94cf4c82005-07-17 17:04:47 +1000570/*
Damien Miller8899ed32004-09-12 15:18:55 +1000571 * line_abbrevname(): Return the abbreviated (usually four-character)
andre61e67252000-06-04 17:07:49 +0000572 * form of the line (Just use the last <dstsize> characters of the
573 * full name.)
574 *
575 * NOTE: use strncpy because we do NOT necessarily want zero
Damien Miller8899ed32004-09-12 15:18:55 +1000576 * termination
577 */
andre2ff7b5d2000-06-03 14:57:40 +0000578char *
Kevin Stevesef4eea92001-02-05 12:42:17 +0000579line_abbrevname(char *dst, const char *src, int dstsize)
Damien Millerdd47aa22000-06-27 11:18:27 +1000580{
581 size_t len;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000582
andre2ff7b5d2000-06-03 14:57:40 +0000583 memset(dst, '\0', dstsize);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000584
Damien Miller8e81ed32000-07-01 13:17:42 +1000585 /* Always skip prefix if present */
586 if (strncmp(src, "/dev/", 5) == 0)
587 src += 5;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000588
Damien Millerf1b9d112002-04-23 23:09:19 +1000589#ifdef WITH_ABBREV_NO_TTY
590 if (strncmp(src, "tty", 3) == 0)
591 src += 3;
592#endif
593
Damien Millerdd47aa22000-06-27 11:18:27 +1000594 len = strlen(src);
595
Damien Miller8e81ed32000-07-01 13:17:42 +1000596 if (len > 0) {
597 if (((int)len - dstsize) > 0)
598 src += ((int)len - dstsize);
599
600 /* note: _don't_ change this to strlcpy */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000601 strncpy(dst, src, (size_t)dstsize);
Damien Millerdd47aa22000-06-27 11:18:27 +1000602 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000603
Damien Miller8899ed32004-09-12 15:18:55 +1000604 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000605}
606
andre2ff7b5d2000-06-03 14:57:40 +0000607/**
608 ** utmp utility functions
andre61e67252000-06-04 17:07:49 +0000609 **
610 ** These functions manipulate struct utmp, taking system differences
611 ** into account.
andre2ff7b5d2000-06-03 14:57:40 +0000612 **/
613
614#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
615
andre2ff7b5d2000-06-03 14:57:40 +0000616/* build the utmp structure */
617void
andre61e67252000-06-04 17:07:49 +0000618set_utmp_time(struct logininfo *li, struct utmp *ut)
619{
Damien Miller8899ed32004-09-12 15:18:55 +1000620# if defined(HAVE_TV_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000621 ut->ut_tv.tv_sec = li->tv_sec;
622 ut->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000623# elif defined(HAVE_TIME_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000624 ut->ut_time = li->tv_sec;
Damien Millerdd47aa22000-06-27 11:18:27 +1000625# endif
andre2ff7b5d2000-06-03 14:57:40 +0000626}
627
628void
629construct_utmp(struct logininfo *li,
andre61e67252000-06-04 17:07:49 +0000630 struct utmp *ut)
631{
Damien Miller02e16ad2003-01-03 14:42:27 +1100632# ifdef HAVE_ADDR_V6_IN_UTMP
633 struct sockaddr_in6 *sa6;
Damien Miller8899ed32004-09-12 15:18:55 +1000634# endif
635
Damien Miller348c9b72000-08-15 10:01:22 +1000636 memset(ut, '\0', sizeof(*ut));
andre6bb92372000-06-19 08:20:03 +0000637
638 /* First fill out fields used for both logins and logouts */
639
Damien Millerdd47aa22000-06-27 11:18:27 +1000640# ifdef HAVE_ID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000641 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +1000642# endif
andre2ff7b5d2000-06-03 14:57:40 +0000643
Damien Millerdd47aa22000-06-27 11:18:27 +1000644# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +0000645 /* This is done here to keep utmp constants out of struct logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000646 switch (li->type) {
647 case LTYPE_LOGIN:
648 ut->ut_type = USER_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700649#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000650 cray_set_tmpdir(ut);
651#endif
andre2ff7b5d2000-06-03 14:57:40 +0000652 break;
653 case LTYPE_LOGOUT:
654 ut->ut_type = DEAD_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700655#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000656 cray_retain_utmp(ut, li->pid);
657#endif
andre2ff7b5d2000-06-03 14:57:40 +0000658 break;
659 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000660# endif
andre6bb92372000-06-19 08:20:03 +0000661 set_utmp_time(li, ut);
andre2ff7b5d2000-06-03 14:57:40 +0000662
andre6bb92372000-06-19 08:20:03 +0000663 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +1000664
665# ifdef HAVE_PID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000666 ut->ut_pid = li->pid;
Damien Millerdd47aa22000-06-27 11:18:27 +1000667# endif
andre6bb92372000-06-19 08:20:03 +0000668
669 /* If we're logging out, leave all other fields blank */
670 if (li->type == LTYPE_LOGOUT)
Damien Miller8899ed32004-09-12 15:18:55 +1000671 return;
andre6bb92372000-06-19 08:20:03 +0000672
Damien Millerdd47aa22000-06-27 11:18:27 +1000673 /*
674 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000675 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000676 */
andre6bb92372000-06-19 08:20:03 +0000677
678 /* Use strncpy because we don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000679 strncpy(ut->ut_name, li->username,
680 MIN_SIZEOF(ut->ut_name, li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000681# ifdef HAVE_HOST_IN_UTMP
Damien Miller8899ed32004-09-12 15:18:55 +1000682 strncpy(ut->ut_host, li->hostname,
683 MIN_SIZEOF(ut->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000684# endif
685# ifdef HAVE_ADDR_IN_UTMP
andre61e67252000-06-04 17:07:49 +0000686 /* this is just a 32-bit IP address */
687 if (li->hostaddr.sa.sa_family == AF_INET)
688 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000689# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100690# ifdef HAVE_ADDR_V6_IN_UTMP
691 /* this is just a 128-bit IPv6 address */
692 if (li->hostaddr.sa.sa_family == AF_INET6) {
693 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
694 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
695 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
696 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
697 ut->ut_addr_v6[1] = 0;
698 ut->ut_addr_v6[2] = 0;
699 ut->ut_addr_v6[3] = 0;
700 }
701 }
702# endif
andre61e67252000-06-04 17:07:49 +0000703}
Damien Millerdd47aa22000-06-27 11:18:27 +1000704#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
andre61e67252000-06-04 17:07:49 +0000705
andre2ff7b5d2000-06-03 14:57:40 +0000706/**
707 ** utmpx utility functions
andre61e67252000-06-04 17:07:49 +0000708 **
709 ** These functions manipulate struct utmpx, accounting for system
710 ** variations.
andre2ff7b5d2000-06-03 14:57:40 +0000711 **/
712
713#if defined(USE_UTMPX) || defined (USE_WTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000714/* build the utmpx structure */
715void
andre61e67252000-06-04 17:07:49 +0000716set_utmpx_time(struct logininfo *li, struct utmpx *utx)
717{
Damien Miller8899ed32004-09-12 15:18:55 +1000718# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000719 utx->ut_tv.tv_sec = li->tv_sec;
720 utx->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000721# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000722 utx->ut_time = li->tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +1000723# endif
andre2ff7b5d2000-06-03 14:57:40 +0000724}
725
andre61e67252000-06-04 17:07:49 +0000726void
727construct_utmpx(struct logininfo *li, struct utmpx *utx)
728{
Damien Miller02e16ad2003-01-03 14:42:27 +1100729# ifdef HAVE_ADDR_V6_IN_UTMP
730 struct sockaddr_in6 *sa6;
731# endif
Damien Miller348c9b72000-08-15 10:01:22 +1000732 memset(utx, '\0', sizeof(*utx));
Damien Miller8899ed32004-09-12 15:18:55 +1000733
Damien Miller8e81ed32000-07-01 13:17:42 +1000734# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000735 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
Damien Miller8e81ed32000-07-01 13:17:42 +1000736# endif
andre2ff7b5d2000-06-03 14:57:40 +0000737
738 /* this is done here to keep utmp constants out of loginrec.h */
739 switch (li->type) {
740 case LTYPE_LOGIN:
741 utx->ut_type = USER_PROCESS;
742 break;
743 case LTYPE_LOGOUT:
744 utx->ut_type = DEAD_PROCESS;
745 break;
746 }
andre2ff7b5d2000-06-03 14:57:40 +0000747 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
andre2ff7b5d2000-06-03 14:57:40 +0000748 set_utmpx_time(li, utx);
andre6bb92372000-06-19 08:20:03 +0000749 utx->ut_pid = li->pid;
Damien Miller8899ed32004-09-12 15:18:55 +1000750
Tim Ricee06ae4a2002-02-24 17:56:46 -0800751 /* strncpy(): Don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000752 strncpy(utx->ut_name, li->username,
753 MIN_SIZEOF(utx->ut_name, li->username));
andre6bb92372000-06-19 08:20:03 +0000754
755 if (li->type == LTYPE_LOGOUT)
756 return;
757
Damien Millerdd47aa22000-06-27 11:18:27 +1000758 /*
759 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000760 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000761 */
andre6bb92372000-06-19 08:20:03 +0000762
Damien Millerdd47aa22000-06-27 11:18:27 +1000763# ifdef HAVE_HOST_IN_UTMPX
Damien Miller8899ed32004-09-12 15:18:55 +1000764 strncpy(utx->ut_host, li->hostname,
765 MIN_SIZEOF(utx->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000766# endif
767# ifdef HAVE_ADDR_IN_UTMPX
Damien Millerd6f204d2000-09-23 13:57:27 +1100768 /* this is just a 32-bit IP address */
769 if (li->hostaddr.sa.sa_family == AF_INET)
770 utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Damien Millerdd47aa22000-06-27 11:18:27 +1000771# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100772# ifdef HAVE_ADDR_V6_IN_UTMP
773 /* this is just a 128-bit IPv6 address */
774 if (li->hostaddr.sa.sa_family == AF_INET6) {
775 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
776 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
777 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
778 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
779 ut->ut_addr_v6[1] = 0;
780 ut->ut_addr_v6[2] = 0;
781 ut->ut_addr_v6[3] = 0;
782 }
783 }
784# endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000785# ifdef HAVE_SYSLEN_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +0000786 /* ut_syslen is the length of the utx_host string */
787 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +1000788# endif
andre61e67252000-06-04 17:07:49 +0000789}
Damien Millerdd47aa22000-06-27 11:18:27 +1000790#endif /* USE_UTMPX || USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +0000791
792/**
andre61e67252000-06-04 17:07:49 +0000793 ** Low-level utmp functions
andre2ff7b5d2000-06-03 14:57:40 +0000794 **/
795
796/* FIXME: (ATL) utmp_write_direct needs testing */
andre2ff7b5d2000-06-03 14:57:40 +0000797#ifdef USE_UTMP
798
andre2ff7b5d2000-06-03 14:57:40 +0000799/* if we can, use pututline() etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000800# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
801 defined(HAVE_PUTUTLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000802# define UTMP_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000803# endif
andre2ff7b5d2000-06-03 14:57:40 +0000804
805
806/* write a utmp entry with the system's help (pututline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000807# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000808static int
andre61e67252000-06-04 17:07:49 +0000809utmp_write_library(struct logininfo *li, struct utmp *ut)
810{
andre2ff7b5d2000-06-03 14:57:40 +0000811 setutent();
812 pututline(ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000813# ifdef HAVE_ENDUTENT
andre2ff7b5d2000-06-03 14:57:40 +0000814 endutent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000815# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000816 return (1);
andre61e67252000-06-04 17:07:49 +0000817}
Damien Millerdd47aa22000-06-27 11:18:27 +1000818# else /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000819
Damien Miller94cf4c82005-07-17 17:04:47 +1000820/*
Damien Miller8899ed32004-09-12 15:18:55 +1000821 * Write a utmp entry direct to the file
822 * This is a slightly modification of code in OpenBSD's login.c
823 */
andre2ff7b5d2000-06-03 14:57:40 +0000824static int
andre61e67252000-06-04 17:07:49 +0000825utmp_write_direct(struct logininfo *li, struct utmp *ut)
826{
andre2ff7b5d2000-06-03 14:57:40 +0000827 struct utmp old_ut;
828 register int fd;
829 int tty;
830
andre6bb92372000-06-19 08:20:03 +0000831 /* FIXME: (ATL) ttyslot() needs local implementation */
Damien Miller348c9b72000-08-15 10:01:22 +1000832
Damien Millere5192fa2000-08-29 14:30:37 +1100833#if defined(HAVE_GETTTYENT)
Damien Miller8899ed32004-09-12 15:18:55 +1000834 struct ttyent *ty;
Damien Miller348c9b72000-08-15 10:01:22 +1000835
836 tty=0;
Damien Miller348c9b72000-08-15 10:01:22 +1000837 setttyent();
Damien Miller8899ed32004-09-12 15:18:55 +1000838 while (NULL != (ty = getttyent())) {
Damien Miller348c9b72000-08-15 10:01:22 +1000839 tty++;
840 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
841 break;
842 }
843 endttyent();
844
Damien Miller8899ed32004-09-12 15:18:55 +1000845 if (NULL == ty) {
Damien Miller81409592004-08-15 19:12:52 +1000846 logit("%s: tty not found", __func__);
847 return (0);
Damien Miller348c9b72000-08-15 10:01:22 +1000848 }
849#else /* FIXME */
850
andre2ff7b5d2000-06-03 14:57:40 +0000851 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
852
Damien Millere5192fa2000-08-29 14:30:37 +1100853#endif /* HAVE_GETTTYENT */
Damien Miller348c9b72000-08-15 10:01:22 +1000854
andre2ff7b5d2000-06-03 14:57:40 +0000855 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
Damien Miller81409592004-08-15 19:12:52 +1000856 off_t pos, ret;
857
858 pos = (off_t)tty * sizeof(struct utmp);
859 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000860 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000861 return (0);
862 }
863 if (ret != pos) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000864 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Millerb0419f22004-08-23 21:53:28 +1000865 __func__, tty, UTMP_FILE);
Damien Miller81409592004-08-15 19:12:52 +1000866 return (0);
867 }
andre2ff7b5d2000-06-03 14:57:40 +0000868 /*
869 * Prevent luser from zero'ing out ut_host.
870 * If the new ut_line is empty but the old one is not
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000871 * and ut_line and ut_name match, preserve the old ut_line.
andre2ff7b5d2000-06-03 14:57:40 +0000872 */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000873 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
Damien Miller8899ed32004-09-12 15:18:55 +1000874 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
875 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
876 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0))
877 memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000878
Damien Miller81409592004-08-15 19:12:52 +1000879 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000880 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000881 return (0);
882 }
883 if (ret != pos) {
Damien Millerb0419f22004-08-23 21:53:28 +1000884 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Miller81409592004-08-15 19:12:52 +1000885 __func__, tty, UTMP_FILE);
886 return (0);
887 }
Damien Miller8899ed32004-09-12 15:18:55 +1000888 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
Damien Miller81409592004-08-15 19:12:52 +1000889 logit("%s: error writing %s: %s", __func__,
andre6bb92372000-06-19 08:20:03 +0000890 UTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +1000891 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000892
Damien Miller8899ed32004-09-12 15:18:55 +1000893 close(fd);
894 return (1);
Damien Miller53c5d462000-06-28 00:50:50 +1000895 } else {
Damien Miller8899ed32004-09-12 15:18:55 +1000896 return (0);
Damien Miller53c5d462000-06-28 00:50:50 +1000897 }
andre61e67252000-06-04 17:07:49 +0000898}
Damien Millerdd47aa22000-06-27 11:18:27 +1000899# endif /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000900
901static int
andre61e67252000-06-04 17:07:49 +0000902utmp_perform_login(struct logininfo *li)
903{
andre2ff7b5d2000-06-03 14:57:40 +0000904 struct utmp ut;
905
906 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000907# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000908 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000909 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000910 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000911 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000912# else
andre2ff7b5d2000-06-03 14:57:40 +0000913 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000914 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000915 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000916 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000917# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000918 return (1);
andre61e67252000-06-04 17:07:49 +0000919}
andre2ff7b5d2000-06-03 14:57:40 +0000920
921
922static int
andre61e67252000-06-04 17:07:49 +0000923utmp_perform_logout(struct logininfo *li)
924{
andre2ff7b5d2000-06-03 14:57:40 +0000925 struct utmp ut;
926
andre6bb92372000-06-19 08:20:03 +0000927 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000928# ifdef UTMP_USE_LIBRARY
andre6bb92372000-06-19 08:20:03 +0000929 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000930 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000931 return (0);
andre6bb92372000-06-19 08:20:03 +0000932 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000933# else
andre6bb92372000-06-19 08:20:03 +0000934 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000935 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000936 return (0);
andre6bb92372000-06-19 08:20:03 +0000937 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000938# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000939 return (1);
andre61e67252000-06-04 17:07:49 +0000940}
andre2ff7b5d2000-06-03 14:57:40 +0000941
942
943int
andre61e67252000-06-04 17:07:49 +0000944utmp_write_entry(struct logininfo *li)
945{
andre2ff7b5d2000-06-03 14:57:40 +0000946 switch(li->type) {
947 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +1000948 return (utmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +0000949
950 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +1000951 return (utmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +0000952
953 default:
Damien Miller6b0279c2004-09-12 15:25:17 +1000954 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000955 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000956 }
andre61e67252000-06-04 17:07:49 +0000957}
Damien Millerdd47aa22000-06-27 11:18:27 +1000958#endif /* USE_UTMP */
andre2ff7b5d2000-06-03 14:57:40 +0000959
960
961/**
andre61e67252000-06-04 17:07:49 +0000962 ** Low-level utmpx functions
andre2ff7b5d2000-06-03 14:57:40 +0000963 **/
964
965/* not much point if we don't want utmpx entries */
966#ifdef USE_UTMPX
967
andre2ff7b5d2000-06-03 14:57:40 +0000968/* if we have the wherewithall, use pututxline etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000969# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
970 defined(HAVE_PUTUTXLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000971# define UTMPX_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000972# endif
andre2ff7b5d2000-06-03 14:57:40 +0000973
974
975/* write a utmpx entry with the system's help (pututxline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000976# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000977static int
andre61e67252000-06-04 17:07:49 +0000978utmpx_write_library(struct logininfo *li, struct utmpx *utx)
979{
andre2ff7b5d2000-06-03 14:57:40 +0000980 setutxent();
981 pututxline(utx);
982
Damien Millerdd47aa22000-06-27 11:18:27 +1000983# ifdef HAVE_ENDUTXENT
andre2ff7b5d2000-06-03 14:57:40 +0000984 endutxent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000985# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000986 return (1);
andre61e67252000-06-04 17:07:49 +0000987}
andre2ff7b5d2000-06-03 14:57:40 +0000988
Damien Millerdd47aa22000-06-27 11:18:27 +1000989# else /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000990
991/* write a utmp entry direct to the file */
992static int
andre61e67252000-06-04 17:07:49 +0000993utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000994{
Damien Miller6b0279c2004-09-12 15:25:17 +1000995 logit("%s: not implemented!", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000996 return (0);
andre61e67252000-06-04 17:07:49 +0000997}
Damien Millerdd47aa22000-06-27 11:18:27 +1000998# endif /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000999
1000static int
andre61e67252000-06-04 17:07:49 +00001001utmpx_perform_login(struct logininfo *li)
1002{
andre2ff7b5d2000-06-03 14:57:40 +00001003 struct utmpx utx;
1004
1005 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001006# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001007 if (!utmpx_write_library(li, &utx)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001008 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001009 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001010 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001011# else
andre2ff7b5d2000-06-03 14:57:40 +00001012 if (!utmpx_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001013 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001014 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001015 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001016# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001017 return (1);
andre61e67252000-06-04 17:07:49 +00001018}
andre2ff7b5d2000-06-03 14:57:40 +00001019
1020
1021static int
andre61e67252000-06-04 17:07:49 +00001022utmpx_perform_logout(struct logininfo *li)
1023{
andre2ff7b5d2000-06-03 14:57:40 +00001024 struct utmpx utx;
1025
Tim Ricee06ae4a2002-02-24 17:56:46 -08001026 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001027# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001028 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +10001029# endif
1030# ifdef HAVE_TYPE_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001031 utx.ut_type = DEAD_PROCESS;
Damien Millerdd47aa22000-06-27 11:18:27 +10001032# endif
andre2ff7b5d2000-06-03 14:57:40 +00001033
Damien Millerdd47aa22000-06-27 11:18:27 +10001034# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001035 utmpx_write_library(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001036# else
andre2ff7b5d2000-06-03 14:57:40 +00001037 utmpx_write_direct(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001038# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001039 return (1);
andre61e67252000-06-04 17:07:49 +00001040}
andre2ff7b5d2000-06-03 14:57:40 +00001041
andre2ff7b5d2000-06-03 14:57:40 +00001042int
andre61e67252000-06-04 17:07:49 +00001043utmpx_write_entry(struct logininfo *li)
1044{
andre2ff7b5d2000-06-03 14:57:40 +00001045 switch(li->type) {
1046 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001047 return (utmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001048 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001049 return (utmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001050 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001051 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001052 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001053 }
andre61e67252000-06-04 17:07:49 +00001054}
Damien Millerdd47aa22000-06-27 11:18:27 +10001055#endif /* USE_UTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001056
1057
1058/**
andre61e67252000-06-04 17:07:49 +00001059 ** Low-level wtmp functions
andre2ff7b5d2000-06-03 14:57:40 +00001060 **/
1061
Kevin Stevesef4eea92001-02-05 12:42:17 +00001062#ifdef USE_WTMP
andre2ff7b5d2000-06-03 14:57:40 +00001063
Damien Miller94cf4c82005-07-17 17:04:47 +10001064/*
Damien Miller8899ed32004-09-12 15:18:55 +10001065 * Write a wtmp entry direct to the end of the file
1066 * This is a slight modification of code in OpenBSD's logwtmp.c
1067 */
andre2ff7b5d2000-06-03 14:57:40 +00001068static int
andre61e67252000-06-04 17:07:49 +00001069wtmp_write(struct logininfo *li, struct utmp *ut)
1070{
andre2ff7b5d2000-06-03 14:57:40 +00001071 struct stat buf;
1072 int fd, ret = 1;
1073
1074 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
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));
Damien Miller8899ed32004-09-12 15:18:55 +10001077 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001078 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001079 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001080 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
andre2ff7b5d2000-06-03 14:57:40 +00001081 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001082 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001083 WTMP_FILE, strerror(errno));
1084 ret = 0;
1085 }
Damien Miller8899ed32004-09-12 15:18:55 +10001086 close(fd);
1087 return (ret);
andre61e67252000-06-04 17:07:49 +00001088}
andre2ff7b5d2000-06-03 14:57:40 +00001089
andre2ff7b5d2000-06-03 14:57:40 +00001090static int
Damien Millerdd47aa22000-06-27 11:18:27 +10001091wtmp_perform_login(struct logininfo *li)
1092{
andre2ff7b5d2000-06-03 14:57:40 +00001093 struct utmp ut;
1094
1095 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001096 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001097}
andre2ff7b5d2000-06-03 14:57:40 +00001098
1099
1100static int
andre61e67252000-06-04 17:07:49 +00001101wtmp_perform_logout(struct logininfo *li)
1102{
andre2ff7b5d2000-06-03 14:57:40 +00001103 struct utmp ut;
1104
1105 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001106 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001107}
andre2ff7b5d2000-06-03 14:57:40 +00001108
1109
1110int
andre61e67252000-06-04 17:07:49 +00001111wtmp_write_entry(struct logininfo *li)
1112{
andre2ff7b5d2000-06-03 14:57:40 +00001113 switch(li->type) {
1114 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001115 return (wtmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001116 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001117 return (wtmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001118 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001119 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001120 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001121 }
andre61e67252000-06-04 17:07:49 +00001122}
andre2ff7b5d2000-06-03 14:57:40 +00001123
1124
Damien Miller94cf4c82005-07-17 17:04:47 +10001125/*
Damien Miller8899ed32004-09-12 15:18:55 +10001126 * Notes on fetching login data from wtmp/wtmpx
Kevin Stevesef4eea92001-02-05 12:42:17 +00001127 *
andre6bb92372000-06-19 08:20:03 +00001128 * Logouts are usually recorded with (amongst other things) a blank
1129 * username on a given tty line. However, some systems (HP-UX is one)
1130 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1131 *
1132 * Since we're only looking for logins here, we know that the username
1133 * must be set correctly. On systems that leave it in, we check for
1134 * ut_type==USER_PROCESS (indicating a login.)
1135 *
1136 * Portability: Some systems may set something other than USER_PROCESS
1137 * to indicate a login process. I don't know of any as I write. Also,
1138 * it's possible that some systems may both leave the username in
1139 * place and not have ut_type.
1140 */
1141
andre6bb92372000-06-19 08:20:03 +00001142/* return true if this wtmp entry indicates a login */
1143static int
1144wtmp_islogin(struct logininfo *li, struct utmp *ut)
1145{
Kevin Stevesef4eea92001-02-05 12:42:17 +00001146 if (strncmp(li->username, ut->ut_name,
Damien Miller8899ed32004-09-12 15:18:55 +10001147 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001148# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001149 if (ut->ut_type & USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001150 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001151# else
Damien Miller8899ed32004-09-12 15:18:55 +10001152 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001153# endif
andre6bb92372000-06-19 08:20:03 +00001154 }
Damien Miller8899ed32004-09-12 15:18:55 +10001155 return (0);
andre6bb92372000-06-19 08:20:03 +00001156}
1157
andre2ff7b5d2000-06-03 14:57:40 +00001158int
andre61e67252000-06-04 17:07:49 +00001159wtmp_get_entry(struct logininfo *li)
1160{
andre2ff7b5d2000-06-03 14:57:40 +00001161 struct stat st;
1162 struct utmp ut;
Damien Miller8899ed32004-09-12 15:18:55 +10001163 int fd, found = 0;
andre6bb92372000-06-19 08:20:03 +00001164
1165 /* Clear the time entries in our logininfo */
1166 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001167
1168 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001169 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001170 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001171 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001172 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001173 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001174 logit("%s: couldn't stat %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001175 WTMP_FILE, strerror(errno));
1176 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001177 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001178 }
andre2ff7b5d2000-06-03 14:57:40 +00001179
andre6bb92372000-06-19 08:20:03 +00001180 /* Seek to the start of the last struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001181 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
andre6bb92372000-06-19 08:20:03 +00001182 /* Looks like we've got a fresh wtmp file */
1183 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001184 return (0);
andre6bb92372000-06-19 08:20:03 +00001185 }
1186
1187 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001188 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001189 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001190 WTMP_FILE, strerror(errno));
1191 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001192 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001193 }
andre6bb92372000-06-19 08:20:03 +00001194 if ( wtmp_islogin(li, &ut) ) {
1195 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001196 /*
1197 * We've already checked for a time in struct
1198 * utmp, in login_getlast()
1199 */
Damien Millerdd47aa22000-06-27 11:18:27 +10001200# ifdef HAVE_TIME_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +00001201 li->tv_sec = ut.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001202# else
andre2ff7b5d2000-06-03 14:57:40 +00001203# if HAVE_TV_IN_UTMP
1204 li->tv_sec = ut.ut_tv.tv_sec;
1205# endif
Damien Millerdd47aa22000-06-27 11:18:27 +10001206# endif
andre6bb92372000-06-19 08:20:03 +00001207 line_fullname(li->line, ut.ut_line,
Damien Miller8899ed32004-09-12 15:18:55 +10001208 MIN_SIZEOF(li->line, ut.ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +10001209# ifdef HAVE_HOST_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001210 strlcpy(li->hostname, ut.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001211 MIN_SIZEOF(li->hostname, ut.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001212# endif
andre6bb92372000-06-19 08:20:03 +00001213 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001214 }
andre6bb92372000-06-19 08:20:03 +00001215 /* Seek back 2 x struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001216 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
andre6bb92372000-06-19 08:20:03 +00001217 /* We've found the start of the file, so quit */
Damien Miller8899ed32004-09-12 15:18:55 +10001218 close(fd);
1219 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001220 }
andre6bb92372000-06-19 08:20:03 +00001221 }
1222
1223 /* We found an entry. Tidy up and return */
1224 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001225 return (1);
andre61e67252000-06-04 17:07:49 +00001226}
Damien Millerdd47aa22000-06-27 11:18:27 +10001227# endif /* USE_WTMP */
andre2ff7b5d2000-06-03 14:57:40 +00001228
1229
1230/**
andre61e67252000-06-04 17:07:49 +00001231 ** Low-level wtmpx functions
andre2ff7b5d2000-06-03 14:57:40 +00001232 **/
1233
1234#ifdef USE_WTMPX
Damien Miller8899ed32004-09-12 15:18:55 +10001235/*
1236 * Write a wtmpx entry direct to the end of the file
1237 * This is a slight modification of code in OpenBSD's logwtmp.c
1238 */
andre2ff7b5d2000-06-03 14:57:40 +00001239static int
andre61e67252000-06-04 17:07:49 +00001240wtmpx_write(struct logininfo *li, struct utmpx *utx)
1241{
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001242#ifndef HAVE_UPDWTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001243 struct stat buf;
1244 int fd, ret = 1;
1245
1246 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001247 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001248 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001249 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001250 }
1251
Kevin Stevesef4eea92001-02-05 12:42:17 +00001252 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001253 if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
andre2ff7b5d2000-06-03 14:57:40 +00001254 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001255 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001256 WTMPX_FILE, strerror(errno));
1257 ret = 0;
1258 }
Damien Miller8899ed32004-09-12 15:18:55 +10001259 close(fd);
andre2ff7b5d2000-06-03 14:57:40 +00001260
Damien Miller8899ed32004-09-12 15:18:55 +10001261 return (ret);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001262#else
1263 updwtmpx(WTMPX_FILE, utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001264 return (1);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001265#endif
andre61e67252000-06-04 17:07:49 +00001266}
andre2ff7b5d2000-06-03 14:57:40 +00001267
1268
1269static int
andre61e67252000-06-04 17:07:49 +00001270wtmpx_perform_login(struct logininfo *li)
1271{
andre2ff7b5d2000-06-03 14:57:40 +00001272 struct utmpx utx;
1273
1274 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001275 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001276}
andre2ff7b5d2000-06-03 14:57:40 +00001277
1278
1279static int
andre61e67252000-06-04 17:07:49 +00001280wtmpx_perform_logout(struct logininfo *li)
1281{
andre2ff7b5d2000-06-03 14:57:40 +00001282 struct utmpx utx;
1283
1284 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001285 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001286}
andre2ff7b5d2000-06-03 14:57:40 +00001287
1288
1289int
andre61e67252000-06-04 17:07:49 +00001290wtmpx_write_entry(struct logininfo *li)
1291{
andre2ff7b5d2000-06-03 14:57:40 +00001292 switch(li->type) {
1293 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001294 return (wtmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001295 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001296 return (wtmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001297 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001298 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001299 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001300 }
andre61e67252000-06-04 17:07:49 +00001301}
andre2ff7b5d2000-06-03 14:57:40 +00001302
andre6bb92372000-06-19 08:20:03 +00001303/* Please see the notes above wtmp_islogin() for information about the
1304 next two functions */
1305
1306/* Return true if this wtmpx entry indicates a login */
1307static int
1308wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1309{
Damien Miller8899ed32004-09-12 15:18:55 +10001310 if (strncmp(li->username, utx->ut_name,
1311 MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001312# ifdef HAVE_TYPE_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +00001313 if (utx->ut_type == USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001314 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001315# else
Damien Miller8899ed32004-09-12 15:18:55 +10001316 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001317# endif
andre6bb92372000-06-19 08:20:03 +00001318 }
Damien Miller8899ed32004-09-12 15:18:55 +10001319 return (0);
andre6bb92372000-06-19 08:20:03 +00001320}
1321
andre2ff7b5d2000-06-03 14:57:40 +00001322
1323int
andre61e67252000-06-04 17:07:49 +00001324wtmpx_get_entry(struct logininfo *li)
1325{
andre2ff7b5d2000-06-03 14:57:40 +00001326 struct stat st;
1327 struct utmpx utx;
andre6bb92372000-06-19 08:20:03 +00001328 int fd, found=0;
1329
1330 /* Clear the time entries */
1331 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001332
1333 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001334 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001335 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001336 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001337 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001338 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001339 logit("%s: couldn't stat %s: %s", __func__,
Tim Ricecdb82942002-07-14 15:33:20 -07001340 WTMPX_FILE, strerror(errno));
andre2ff7b5d2000-06-03 14:57:40 +00001341 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001342 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001343 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001344
andre6bb92372000-06-19 08:20:03 +00001345 /* Seek to the start of the last struct utmpx */
Kevin Steves52172652001-10-02 00:29:00 +00001346 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
andre6bb92372000-06-19 08:20:03 +00001347 /* probably a newly rotated wtmpx file */
1348 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001349 return (0);
andre6bb92372000-06-19 08:20:03 +00001350 }
andre2ff7b5d2000-06-03 14:57:40 +00001351
andre6bb92372000-06-19 08:20:03 +00001352 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001353 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001354 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001355 WTMPX_FILE, strerror(errno));
1356 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001357 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001358 }
Damien Miller8899ed32004-09-12 15:18:55 +10001359 /*
Damien Miller94cf4c82005-07-17 17:04:47 +10001360 * Logouts are recorded as a blank username on a particular
Damien Miller8899ed32004-09-12 15:18:55 +10001361 * line. So, we just need to find the username in struct utmpx
1362 */
1363 if (wtmpx_islogin(li, &utx)) {
Tim Rice370e0ba2002-07-14 15:50:51 -07001364 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001365# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001366 li->tv_sec = utx.ut_tv.tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +10001367# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001368 li->tv_sec = utx.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001369# endif
Damien Miller1a132252000-06-13 21:23:17 +10001370 line_fullname(li->line, utx.ut_line, sizeof(li->line));
Damien Miller8899ed32004-09-12 15:18:55 +10001371# if defined(HAVE_HOST_IN_UTMPX)
andre6bb92372000-06-19 08:20:03 +00001372 strlcpy(li->hostname, utx.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001373 MIN_SIZEOF(li->hostname, utx.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001374# endif
andre6bb92372000-06-19 08:20:03 +00001375 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001376 }
Kevin Steves52172652001-10-02 00:29:00 +00001377 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
Damien Miller8899ed32004-09-12 15:18:55 +10001378 close(fd);
1379 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001380 }
andre6bb92372000-06-19 08:20:03 +00001381 }
1382
1383 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001384 return (1);
andre61e67252000-06-04 17:07:49 +00001385}
Damien Millerd5bf3072000-06-07 21:32:13 +10001386#endif /* USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001387
andre2ff7b5d2000-06-03 14:57:40 +00001388/**
andre61e67252000-06-04 17:07:49 +00001389 ** Low-level libutil login() functions
andre2ff7b5d2000-06-03 14:57:40 +00001390 **/
1391
1392#ifdef USE_LOGIN
andre2ff7b5d2000-06-03 14:57:40 +00001393static int
andre61e67252000-06-04 17:07:49 +00001394syslogin_perform_login(struct logininfo *li)
1395{
andre2ff7b5d2000-06-03 14:57:40 +00001396 struct utmp *ut;
1397
Damien Millerb0aae332004-09-12 15:26:00 +10001398 ut = xmalloc(sizeof(*ut));
andre2ff7b5d2000-06-03 14:57:40 +00001399 construct_utmp(li, ut);
1400 login(ut);
Damien Millerf211efc2003-03-10 11:23:06 +11001401 free(ut);
andre2ff7b5d2000-06-03 14:57:40 +00001402
Damien Miller8899ed32004-09-12 15:18:55 +10001403 return (1);
andre61e67252000-06-04 17:07:49 +00001404}
1405
andre2ff7b5d2000-06-03 14:57:40 +00001406static int
andre61e67252000-06-04 17:07:49 +00001407syslogin_perform_logout(struct logininfo *li)
1408{
Damien Millerdd47aa22000-06-27 11:18:27 +10001409# ifdef HAVE_LOGOUT
Darren Tucker4d2f3612004-04-08 10:57:05 +10001410 char line[UT_LINESIZE];
Kevin Stevesef4eea92001-02-05 12:42:17 +00001411
andre2ff7b5d2000-06-03 14:57:40 +00001412 (void)line_stripname(line, li->line, sizeof(line));
1413
Damien Miller8899ed32004-09-12 15:18:55 +10001414 if (!logout(line))
Damien Miller6b0279c2004-09-12 15:25:17 +10001415 logit("%s: logout() returned an error", __func__);
Damien Millerdd47aa22000-06-27 11:18:27 +10001416# ifdef HAVE_LOGWTMP
Damien Miller8899ed32004-09-12 15:18:55 +10001417 else
andre2ff7b5d2000-06-03 14:57:40 +00001418 logwtmp(line, "", "");
Damien Millerdd47aa22000-06-27 11:18:27 +10001419# endif
andre6bb92372000-06-19 08:20:03 +00001420 /* FIXME: (ATL - if the need arises) What to do if we have
1421 * login, but no logout? what if logout but no logwtmp? All
1422 * routines are in libutil so they should all be there,
1423 * but... */
Damien Millerdd47aa22000-06-27 11:18:27 +10001424# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001425 return (1);
andre61e67252000-06-04 17:07:49 +00001426}
andre2ff7b5d2000-06-03 14:57:40 +00001427
andre2ff7b5d2000-06-03 14:57:40 +00001428int
andre61e67252000-06-04 17:07:49 +00001429syslogin_write_entry(struct logininfo *li)
1430{
andre2ff7b5d2000-06-03 14:57:40 +00001431 switch (li->type) {
1432 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001433 return (syslogin_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001434 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001435 return (syslogin_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001436 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001437 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001438 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001439 }
andre61e67252000-06-04 17:07:49 +00001440}
Damien Millerd5bf3072000-06-07 21:32:13 +10001441#endif /* USE_LOGIN */
andre2ff7b5d2000-06-03 14:57:40 +00001442
1443/* end of file log-syslogin.c */
1444
andre2ff7b5d2000-06-03 14:57:40 +00001445/**
andre61e67252000-06-04 17:07:49 +00001446 ** Low-level lastlog functions
andre2ff7b5d2000-06-03 14:57:40 +00001447 **/
1448
1449#ifdef USE_LASTLOG
Damien Millerdd47aa22000-06-27 11:18:27 +10001450#define LL_FILE 1
1451#define LL_DIR 2
1452#define LL_OTHER 3
andre2ff7b5d2000-06-03 14:57:40 +00001453
andre2ff7b5d2000-06-03 14:57:40 +00001454static void
andre61e67252000-06-04 17:07:49 +00001455lastlog_construct(struct logininfo *li, struct lastlog *last)
1456{
andre2ff7b5d2000-06-03 14:57:40 +00001457 /* clear the structure */
Damien Miller348c9b72000-08-15 10:01:22 +10001458 memset(last, '\0', sizeof(*last));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001459
Damien Miller8899ed32004-09-12 15:18:55 +10001460 line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
andre6bb92372000-06-19 08:20:03 +00001461 strlcpy(last->ll_host, li->hostname,
1462 MIN_SIZEOF(last->ll_host, li->hostname));
andre2ff7b5d2000-06-03 14:57:40 +00001463 last->ll_time = li->tv_sec;
andre61e67252000-06-04 17:07:49 +00001464}
andre2ff7b5d2000-06-03 14:57:40 +00001465
andre2ff7b5d2000-06-03 14:57:40 +00001466static int
andre61e67252000-06-04 17:07:49 +00001467lastlog_filetype(char *filename)
1468{
andre2ff7b5d2000-06-03 14:57:40 +00001469 struct stat st;
1470
Damien Millerdd47aa22000-06-27 11:18:27 +10001471 if (stat(LASTLOG_FILE, &st) != 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001472 logit("%s: Couldn't stat %s: %s", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001473 LASTLOG_FILE, strerror(errno));
1474 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001475 }
andre2ff7b5d2000-06-03 14:57:40 +00001476 if (S_ISDIR(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001477 return (LL_DIR);
andre2ff7b5d2000-06-03 14:57:40 +00001478 else if (S_ISREG(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001479 return (LL_FILE);
andre2ff7b5d2000-06-03 14:57:40 +00001480 else
Damien Miller8899ed32004-09-12 15:18:55 +10001481 return (LL_OTHER);
andre61e67252000-06-04 17:07:49 +00001482}
andre2ff7b5d2000-06-03 14:57:40 +00001483
1484
1485/* open the file (using filemode) and seek to the login entry */
1486static int
andre61e67252000-06-04 17:07:49 +00001487lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1488{
andre2ff7b5d2000-06-03 14:57:40 +00001489 off_t offset;
1490 int type;
1491 char lastlog_file[1024];
1492
1493 type = lastlog_filetype(LASTLOG_FILE);
1494 switch (type) {
Damien Miller8899ed32004-09-12 15:18:55 +10001495 case LL_FILE:
1496 strlcpy(lastlog_file, LASTLOG_FILE,
1497 sizeof(lastlog_file));
1498 break;
1499 case LL_DIR:
1500 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1501 LASTLOG_FILE, li->username);
1502 break;
1503 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001504 logit("%s: %.100s is not a file or directory!", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001505 LASTLOG_FILE);
1506 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001507 }
andre2ff7b5d2000-06-03 14:57:40 +00001508
Damien Miller405dc602003-04-09 21:12:52 +10001509 *fd = open(lastlog_file, filemode, 0600);
Damien Miller8899ed32004-09-12 15:18:55 +10001510 if (*fd < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001511 debug("%s: Couldn't open %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001512 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001513 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001514 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001515
Damien Millere477ef62000-08-15 10:21:17 +10001516 if (type == LL_FILE) {
1517 /* find this uid's offset in the lastlog file */
Kevin Steves52172652001-10-02 00:29:00 +00001518 offset = (off_t) ((long)li->uid * sizeof(struct lastlog));
andre2ff7b5d2000-06-03 14:57:40 +00001519
Damien Miller8899ed32004-09-12 15:18:55 +10001520 if (lseek(*fd, offset, SEEK_SET) != offset) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001521 logit("%s: %s->lseek(): %s", __func__,
1522 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001523 return (0);
Damien Millere477ef62000-08-15 10:21:17 +10001524 }
andre2ff7b5d2000-06-03 14:57:40 +00001525 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001526
Damien Miller8899ed32004-09-12 15:18:55 +10001527 return (1);
andre61e67252000-06-04 17:07:49 +00001528}
andre2ff7b5d2000-06-03 14:57:40 +00001529
1530static int
andre61e67252000-06-04 17:07:49 +00001531lastlog_perform_login(struct logininfo *li)
1532{
andre2ff7b5d2000-06-03 14:57:40 +00001533 struct lastlog last;
1534 int fd;
1535
1536 /* create our struct lastlog */
1537 lastlog_construct(li, &last);
1538
Damien Miller405dc602003-04-09 21:12:52 +10001539 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
Damien Miller8899ed32004-09-12 15:18:55 +10001540 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001541
andre2ff7b5d2000-06-03 14:57:40 +00001542 /* write the entry */
Darren Tucker8661b562003-07-06 15:20:46 +10001543 if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) {
Damien Millerc1132e72000-08-18 14:08:38 +10001544 close(fd);
Damien Miller6b0279c2004-09-12 15:25:17 +10001545 logit("%s: Error writing to %s: %s", __func__,
Damien Millerc1132e72000-08-18 14:08:38 +10001546 LASTLOG_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001547 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001548 }
Damien Millerc1132e72000-08-18 14:08:38 +10001549
1550 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001551 return (1);
andre61e67252000-06-04 17:07:49 +00001552}
andre2ff7b5d2000-06-03 14:57:40 +00001553
andre2ff7b5d2000-06-03 14:57:40 +00001554int
andre61e67252000-06-04 17:07:49 +00001555lastlog_write_entry(struct logininfo *li)
1556{
andre2ff7b5d2000-06-03 14:57:40 +00001557 switch(li->type) {
1558 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001559 return (lastlog_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001560 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001561 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001562 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001563 }
andre61e67252000-06-04 17:07:49 +00001564}
andre2ff7b5d2000-06-03 14:57:40 +00001565
andre2ff7b5d2000-06-03 14:57:40 +00001566static void
andre61e67252000-06-04 17:07:49 +00001567lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1568{
andre2ff7b5d2000-06-03 14:57:40 +00001569 line_fullname(li->line, last->ll_line, sizeof(li->line));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001570 strlcpy(li->hostname, last->ll_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001571 MIN_SIZEOF(li->hostname, last->ll_host));
andre2ff7b5d2000-06-03 14:57:40 +00001572 li->tv_sec = last->ll_time;
andre61e67252000-06-04 17:07:49 +00001573}
andre2ff7b5d2000-06-03 14:57:40 +00001574
andre2ff7b5d2000-06-03 14:57:40 +00001575int
andre61e67252000-06-04 17:07:49 +00001576lastlog_get_entry(struct logininfo *li)
1577{
andre2ff7b5d2000-06-03 14:57:40 +00001578 struct lastlog last;
Damien Miller7df881d2003-01-07 16:46:58 +11001579 int fd, ret;
andre2ff7b5d2000-06-03 14:57:40 +00001580
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001581 if (!lastlog_openseek(li, &fd, O_RDONLY))
Damien Miller7df881d2003-01-07 16:46:58 +11001582 return (0);
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001583
Damien Miller7df881d2003-01-07 16:46:58 +11001584 ret = atomicio(read, fd, &last, sizeof(last));
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001585 close(fd);
1586
Damien Miller7df881d2003-01-07 16:46:58 +11001587 switch (ret) {
1588 case 0:
1589 memset(&last, '\0', sizeof(last));
1590 /* FALLTHRU */
1591 case sizeof(last):
1592 lastlog_populate_entry(li, &last);
1593 return (1);
1594 case -1:
Damien Millera8e06ce2003-11-21 23:48:55 +11001595 error("%s: Error reading from %s: %s", __func__,
Damien Miller7df881d2003-01-07 16:46:58 +11001596 LASTLOG_FILE, strerror(errno));
1597 return (0);
1598 default:
1599 error("%s: Error reading from %s: Expecting %d, got %d",
Darren Tuckerefc17472005-11-22 19:55:13 +11001600 __func__, LASTLOG_FILE, (int)sizeof(last), ret);
Damien Miller7df881d2003-01-07 16:46:58 +11001601 return (0);
1602 }
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001603
Damien Miller7df881d2003-01-07 16:46:58 +11001604 /* NOTREACHED */
1605 return (0);
andre61e67252000-06-04 17:07:49 +00001606}
Damien Millerd5bf3072000-06-07 21:32:13 +10001607#endif /* USE_LASTLOG */
Darren Tucker2fba9932005-02-02 23:30:24 +11001608
1609#ifdef USE_BTMP
1610 /*
1611 * Logs failed login attempts in _PATH_BTMP if that exists.
1612 * The most common login failure is to give password instead of username.
1613 * So the _PATH_BTMP file checked for the correct permission, so that
1614 * only root can read it.
1615 */
1616
1617void
1618record_failed_login(const char *username, const char *hostname,
1619 const char *ttyn)
1620{
1621 int fd;
1622 struct utmp ut;
1623 struct sockaddr_storage from;
Darren Tuckerefc17472005-11-22 19:55:13 +11001624 socklen_t fromlen = sizeof(from);
Darren Tucker2fba9932005-02-02 23:30:24 +11001625 struct sockaddr_in *a4;
1626 struct sockaddr_in6 *a6;
1627 time_t t;
1628 struct stat fst;
1629
1630 if (geteuid() != 0)
1631 return;
1632 if ((fd = open(_PATH_BTMP, O_WRONLY | O_APPEND)) < 0) {
1633 debug("Unable to open the btmp file %s: %s", _PATH_BTMP,
1634 strerror(errno));
1635 return;
1636 }
1637 if (fstat(fd, &fst) < 0) {
1638 logit("%s: fstat of %s failed: %s", __func__, _PATH_BTMP,
1639 strerror(errno));
1640 goto out;
1641 }
1642 if((fst.st_mode & (S_IRWXG | S_IRWXO)) || (fst.st_uid != 0)){
1643 logit("Excess permission or bad ownership on file %s",
1644 _PATH_BTMP);
1645 goto out;
1646 }
1647
1648 memset(&ut, 0, sizeof(ut));
1649 /* strncpy because we don't necessarily want nul termination */
1650 strncpy(ut.ut_user, username, sizeof(ut.ut_user));
1651 strlcpy(ut.ut_line, "ssh:notty", sizeof(ut.ut_line));
1652
1653 time(&t);
1654 ut.ut_time = t; /* ut_time is not always a time_t */
1655 ut.ut_type = LOGIN_PROCESS;
1656 ut.ut_pid = getpid();
1657
1658 /* strncpy because we don't necessarily want nul termination */
1659 strncpy(ut.ut_host, hostname, sizeof(ut.ut_host));
1660
1661 if (packet_connection_is_on_socket() &&
1662 getpeername(packet_get_connection_in(),
1663 (struct sockaddr *)&from, &fromlen) == 0) {
1664 ipv64_normalise_mapped(&from, &fromlen);
1665 if (from.ss_family == AF_INET) {
1666 a4 = (struct sockaddr_in *)&from;
1667 memcpy(&ut.ut_addr, &(a4->sin_addr),
1668 MIN_SIZEOF(ut.ut_addr, a4->sin_addr));
1669 }
1670#ifdef HAVE_ADDR_V6_IN_UTMP
1671 if (from.ss_family == AF_INET6) {
1672 a6 = (struct sockaddr_in6 *)&from;
1673 memcpy(&ut.ut_addr_v6, &(a6->sin6_addr),
1674 MIN_SIZEOF(ut.ut_addr_v6, a6->sin6_addr));
1675 }
1676#endif
1677 }
1678
1679 if (atomicio(vwrite, fd, &ut, sizeof(ut)) != sizeof(ut))
1680 error("Failed to write to %s: %s", _PATH_BTMP,
1681 strerror(errno));
1682
1683out:
1684 close(fd);
1685}
1686#endif /* USE_BTMP */