blob: 8844db5402fe4f02828b17a80a981d7abccfc275 [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>
Darren Tuckerf19bbc32006-09-07 22:57:53 +1000158#ifdef HAVE_PATHS_H
159# include <paths.h>
160#endif
Damien Miller9f2abc42006-07-10 20:53:08 +1000161#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +1000162#include <stdarg.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +1000163#include <string.h>
Darren Tuckerd757e692007-04-29 12:10:57 +1000164#include <time.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +1000165#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +1000166
andre2ff7b5d2000-06-03 14:57:40 +0000167#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +1000168#include "key.h"
169#include "hostfile.h"
170#include "ssh.h"
andre2ff7b5d2000-06-03 14:57:40 +0000171#include "loginrec.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000172#include "log.h"
173#include "atomicio.h"
Darren Tucker2fba9932005-02-02 23:30:24 +1100174#include "packet.h"
175#include "canohost.h"
Darren Tucker269a1ea2005-02-03 00:20:53 +1100176#include "auth.h"
Darren Tuckera39f83e2005-02-15 22:19:28 +1100177#include "buffer.h"
andre2ff7b5d2000-06-03 14:57:40 +0000178
Ben Lindstromdcca9812000-11-10 03:28:31 +0000179#ifdef HAVE_UTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000180# include <util.h>
Ben Lindstromdcca9812000-11-10 03:28:31 +0000181#endif
andre2ff7b5d2000-06-03 14:57:40 +0000182
183/**
184 ** prototypes for helper functions in this file
185 **/
186
187#if HAVE_UTMP_H
andre2ff7b5d2000-06-03 14:57:40 +0000188void set_utmp_time(struct logininfo *li, struct utmp *ut);
189void construct_utmp(struct logininfo *li, struct utmp *ut);
190#endif
191
192#ifdef HAVE_UTMPX_H
andre2ff7b5d2000-06-03 14:57:40 +0000193void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
194void construct_utmpx(struct logininfo *li, struct utmpx *ut);
195#endif
196
197int utmp_write_entry(struct logininfo *li);
198int utmpx_write_entry(struct logininfo *li);
199int wtmp_write_entry(struct logininfo *li);
200int wtmpx_write_entry(struct logininfo *li);
201int lastlog_write_entry(struct logininfo *li);
202int syslogin_write_entry(struct logininfo *li);
203
204int getlast_entry(struct logininfo *li);
205int lastlog_get_entry(struct logininfo *li);
Darren Tucker261d93a2010-04-09 18:13:27 +1000206int utmpx_get_entry(struct logininfo *li);
andre2ff7b5d2000-06-03 14:57:40 +0000207int wtmp_get_entry(struct logininfo *li);
208int wtmpx_get_entry(struct logininfo *li);
209
Darren Tucker691d5232005-02-15 21:45:57 +1100210extern Buffer loginmsg;
211
andre6bb92372000-06-19 08:20:03 +0000212/* pick the shortest string */
Damien Miller8899ed32004-09-12 15:18:55 +1000213#define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2))
andre6bb92372000-06-19 08:20:03 +0000214
andre2ff7b5d2000-06-03 14:57:40 +0000215/**
216 ** platform-independent login functions
217 **/
218
Damien Miller8899ed32004-09-12 15:18:55 +1000219/*
220 * login_login(struct logininfo *) - Record a login
Kevin Stevesef4eea92001-02-05 12:42:17 +0000221 *
andre6bb92372000-06-19 08:20:03 +0000222 * Call with a pointer to a struct logininfo initialised with
223 * login_init_entry() or login_alloc_entry()
224 *
225 * Returns:
226 * >0 if successful
227 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
228 */
andre61e67252000-06-04 17:07:49 +0000229int
Damien Miller8899ed32004-09-12 15:18:55 +1000230login_login(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000231{
232 li->type = LTYPE_LOGIN;
Damien Miller8899ed32004-09-12 15:18:55 +1000233 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000234}
235
236
Damien Miller8899ed32004-09-12 15:18:55 +1000237/*
238 * login_logout(struct logininfo *) - Record a logout
andre6bb92372000-06-19 08:20:03 +0000239 *
240 * Call as with login_login()
241 *
242 * Returns:
243 * >0 if successful
244 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
245 */
andre61e67252000-06-04 17:07:49 +0000246int
247login_logout(struct logininfo *li)
248{
249 li->type = LTYPE_LOGOUT;
Damien Miller8899ed32004-09-12 15:18:55 +1000250 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000251}
252
Damien Miller8899ed32004-09-12 15:18:55 +1000253/*
254 * login_get_lastlog_time(int) - Retrieve the last login time
andre6bb92372000-06-19 08:20:03 +0000255 *
256 * Retrieve the last login time for the given uid. Will try to use the
257 * system lastlog facilities if they are available, but will fall back
258 * to looking in wtmp/wtmpx if necessary
259 *
260 * Returns:
261 * 0 on failure, or if user has never logged in
262 * Time in seconds from the epoch if successful
263 *
264 * Useful preprocessor symbols:
265 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
266 * info
267 * USE_LASTLOG: If set, indicates the presence of system lastlog
268 * facilities. If this and DISABLE_LASTLOG are not set,
269 * try to retrieve lastlog information from wtmp/wtmpx.
270 */
andre61e67252000-06-04 17:07:49 +0000271unsigned int
Damien Miller34ee4202010-11-05 10:52:37 +1100272login_get_lastlog_time(const uid_t uid)
andre61e67252000-06-04 17:07:49 +0000273{
274 struct logininfo li;
275
andre6bb92372000-06-19 08:20:03 +0000276 if (login_get_lastlog(&li, uid))
Damien Miller8899ed32004-09-12 15:18:55 +1000277 return (li.tv_sec);
andre6bb92372000-06-19 08:20:03 +0000278 else
Damien Miller8899ed32004-09-12 15:18:55 +1000279 return (0);
andre61e67252000-06-04 17:07:49 +0000280}
281
Damien Miller8899ed32004-09-12 15:18:55 +1000282/*
283 * login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
andre6bb92372000-06-19 08:20:03 +0000284 *
285 * Retrieve a logininfo structure populated (only partially) with
286 * information from the system lastlog data, or from wtmp/wtmpx if no
287 * system lastlog information exists.
288 *
289 * Note this routine must be given a pre-allocated logininfo.
290 *
291 * Returns:
292 * >0: A pointer to your struct logininfo if successful
293 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
andre6bb92372000-06-19 08:20:03 +0000294 */
andre61e67252000-06-04 17:07:49 +0000295struct logininfo *
Damien Miller34ee4202010-11-05 10:52:37 +1100296login_get_lastlog(struct logininfo *li, const uid_t uid)
andre61e67252000-06-04 17:07:49 +0000297{
andre6bb92372000-06-19 08:20:03 +0000298 struct passwd *pw;
andre6bb92372000-06-19 08:20:03 +0000299
Damien Miller348c9b72000-08-15 10:01:22 +1000300 memset(li, '\0', sizeof(*li));
andre61e67252000-06-04 17:07:49 +0000301 li->uid = uid;
andre6bb92372000-06-19 08:20:03 +0000302
Kevin Stevesef4eea92001-02-05 12:42:17 +0000303 /*
Damien Miller53c5d462000-06-28 00:50:50 +1000304 * If we don't have a 'real' lastlog, we need the username to
andre6bb92372000-06-19 08:20:03 +0000305 * reliably search wtmp(x) for the last login (see
Kevin Stevesef4eea92001-02-05 12:42:17 +0000306 * wtmp_get_entry().)
Damien Miller53c5d462000-06-28 00:50:50 +1000307 */
andre6bb92372000-06-19 08:20:03 +0000308 pw = getpwuid(uid);
Damien Millerdd47aa22000-06-27 11:18:27 +1000309 if (pw == NULL)
Damien Miller34ee4202010-11-05 10:52:37 +1100310 fatal("%s: Cannot find account for uid %ld", __func__,
311 (long)uid);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000312
Damien Miller7d97fd92013-12-29 17:40:18 +1100313 if (strlcpy(li->username, pw->pw_name, sizeof(li->username)) >=
314 sizeof(li->username)) {
315 error("%s: username too long (%lu > max %lu)", __func__,
316 strlen(pw->pw_name), sizeof(li->username) - 1);
317 return NULL;
318 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000319
andre61e67252000-06-04 17:07:49 +0000320 if (getlast_entry(li))
Damien Miller8899ed32004-09-12 15:18:55 +1000321 return (li);
andre61e67252000-06-04 17:07:49 +0000322 else
Damien Miller8899ed32004-09-12 15:18:55 +1000323 return (NULL);
andre61e67252000-06-04 17:07:49 +0000324}
325
Damien Miller8899ed32004-09-12 15:18:55 +1000326/*
327 * login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
Kevin Stevesef4eea92001-02-05 12:42:17 +0000328 * a logininfo structure
329 *
andre6bb92372000-06-19 08:20:03 +0000330 * This function creates a new struct logininfo, a data structure
331 * meant to carry the information required to portably record login info.
332 *
333 * Returns a pointer to a newly created struct logininfo. If memory
334 * allocation fails, the program halts.
335 */
andre61e67252000-06-04 17:07:49 +0000336struct
Damien Miller34ee4202010-11-05 10:52:37 +1100337logininfo *login_alloc_entry(pid_t pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000338 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000339{
andre2ff7b5d2000-06-03 14:57:40 +0000340 struct logininfo *newli;
341
Damien Miller8899ed32004-09-12 15:18:55 +1000342 newli = xmalloc(sizeof(*newli));
343 login_init_entry(newli, pid, username, hostname, line);
344 return (newli);
andre61e67252000-06-04 17:07:49 +0000345}
andre2ff7b5d2000-06-03 14:57:40 +0000346
347
andre6bb92372000-06-19 08:20:03 +0000348/* login_free_entry(struct logininfo *) - free struct memory */
andre61e67252000-06-04 17:07:49 +0000349void
350login_free_entry(struct logininfo *li)
351{
Darren Tuckerf60845f2013-06-02 08:07:31 +1000352 free(li);
andre61e67252000-06-04 17:07:49 +0000353}
354
andre2ff7b5d2000-06-03 14:57:40 +0000355
andre6bb92372000-06-19 08:20:03 +0000356/* login_init_entry(struct logininfo *, int, char*, char*, char*)
357 * - initialise a struct logininfo
Kevin Stevesef4eea92001-02-05 12:42:17 +0000358 *
andre6bb92372000-06-19 08:20:03 +0000359 * Populates a new struct logininfo, a data structure meant to carry
360 * the information required to portably record login info.
361 *
362 * Returns: 1
363 */
andre61e67252000-06-04 17:07:49 +0000364int
Damien Miller34ee4202010-11-05 10:52:37 +1100365login_init_entry(struct logininfo *li, pid_t pid, const char *username,
Damien Miller8899ed32004-09-12 15:18:55 +1000366 const char *hostname, const char *line)
andre61e67252000-06-04 17:07:49 +0000367{
Damien Millerf8af08d2000-06-27 09:40:06 +1000368 struct passwd *pw;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000369
Damien Miller348c9b72000-08-15 10:01:22 +1000370 memset(li, 0, sizeof(*li));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000371
andre61e67252000-06-04 17:07:49 +0000372 li->pid = pid;
Damien Millerf8af08d2000-06-27 09:40:06 +1000373
andre2ff7b5d2000-06-03 14:57:40 +0000374 /* set the line information */
andre61e67252000-06-04 17:07:49 +0000375 if (line)
andre2ff7b5d2000-06-03 14:57:40 +0000376 line_fullname(li->line, line, sizeof(li->line));
andre2ff7b5d2000-06-03 14:57:40 +0000377
Damien Millerf8af08d2000-06-27 09:40:06 +1000378 if (username) {
andre2ff7b5d2000-06-03 14:57:40 +0000379 strlcpy(li->username, username, sizeof(li->username));
Damien Millerf8af08d2000-06-27 09:40:06 +1000380 pw = getpwnam(li->username);
Damien Miller8899ed32004-09-12 15:18:55 +1000381 if (pw == NULL) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000382 fatal("%s: Cannot find user \"%s\"", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +1000383 li->username);
384 }
Damien Millerf8af08d2000-06-27 09:40:06 +1000385 li->uid = pw->pw_uid;
386 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000387
andre61e67252000-06-04 17:07:49 +0000388 if (hostname)
andre2ff7b5d2000-06-03 14:57:40 +0000389 strlcpy(li->hostname, hostname, sizeof(li->hostname));
Damien Millerf8af08d2000-06-27 09:40:06 +1000390
Damien Miller8899ed32004-09-12 15:18:55 +1000391 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000392}
393
Damien Miller94cf4c82005-07-17 17:04:47 +1000394/*
Damien Miller8899ed32004-09-12 15:18:55 +1000395 * login_set_current_time(struct logininfo *) - set the current time
andre6bb92372000-06-19 08:20:03 +0000396 *
397 * Set the current time in a logininfo structure. This function is
398 * meant to eliminate the need to deal with system dependencies for
399 * time handling.
400 */
andre2ff7b5d2000-06-03 14:57:40 +0000401void
andre61e67252000-06-04 17:07:49 +0000402login_set_current_time(struct logininfo *li)
403{
andre2ff7b5d2000-06-03 14:57:40 +0000404 struct timeval tv;
405
406 gettimeofday(&tv, NULL);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000407
Damien Millerf8af08d2000-06-27 09:40:06 +1000408 li->tv_sec = tv.tv_sec;
409 li->tv_usec = tv.tv_usec;
andre2ff7b5d2000-06-03 14:57:40 +0000410}
411
andre61e67252000-06-04 17:07:49 +0000412/* copy a sockaddr_* into our logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000413void
andre61e67252000-06-04 17:07:49 +0000414login_set_addr(struct logininfo *li, const struct sockaddr *sa,
Damien Miller8899ed32004-09-12 15:18:55 +1000415 const unsigned int sa_size)
andre61e67252000-06-04 17:07:49 +0000416{
417 unsigned int bufsize = sa_size;
418
419 /* make sure we don't overrun our union */
420 if (sizeof(li->hostaddr) < sa_size)
421 bufsize = sizeof(li->hostaddr);
422
Damien Miller8899ed32004-09-12 15:18:55 +1000423 memcpy(&li->hostaddr.sa, sa, bufsize);
andre2ff7b5d2000-06-03 14:57:40 +0000424}
425
andre2ff7b5d2000-06-03 14:57:40 +0000426
andre61e67252000-06-04 17:07:49 +0000427/**
428 ** login_write: Call low-level recording functions based on autoconf
429 ** results
430 **/
andre2ff7b5d2000-06-03 14:57:40 +0000431int
Damien Miller8899ed32004-09-12 15:18:55 +1000432login_write(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000433{
Damien Millerbac2d8a2000-09-05 16:13:06 +1100434#ifndef HAVE_CYGWIN
Damien Miller8899ed32004-09-12 15:18:55 +1000435 if (geteuid() != 0) {
436 logit("Attempt to write login records by non-root user (aborting)");
437 return (1);
andre2ff7b5d2000-06-03 14:57:40 +0000438 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100439#endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000440
andre2ff7b5d2000-06-03 14:57:40 +0000441 /* set the timestamp */
442 login_set_current_time(li);
443#ifdef USE_LOGIN
444 syslogin_write_entry(li);
445#endif
446#ifdef USE_LASTLOG
Damien Miller8899ed32004-09-12 15:18:55 +1000447 if (li->type == LTYPE_LOGIN)
andre2ff7b5d2000-06-03 14:57:40 +0000448 lastlog_write_entry(li);
andre2ff7b5d2000-06-03 14:57:40 +0000449#endif
450#ifdef USE_UTMP
451 utmp_write_entry(li);
452#endif
453#ifdef USE_WTMP
454 wtmp_write_entry(li);
455#endif
456#ifdef USE_UTMPX
457 utmpx_write_entry(li);
458#endif
459#ifdef USE_WTMPX
460 wtmpx_write_entry(li);
461#endif
Darren Tucker397a2f22004-08-15 00:09:11 +1000462#ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN
Damien Miller94cf4c82005-07-17 17:04:47 +1000463 if (li->type == LTYPE_LOGIN &&
Damien Millerb6f72f52005-07-17 17:26:43 +1000464 !sys_auth_record_login(li->username,li->hostname,li->line,
465 &loginmsg))
Darren Tucker397a2f22004-08-15 00:09:11 +1000466 logit("Writing login record failed for %s", li->username);
467#endif
Darren Tucker2e0cf0d2005-02-08 21:52:47 +1100468#ifdef SSH_AUDIT_EVENTS
Darren Tucker269a1ea2005-02-03 00:20:53 +1100469 if (li->type == LTYPE_LOGIN)
Darren Tuckerea52a822011-01-17 21:15:27 +1100470 audit_session_open(li);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100471 else if (li->type == LTYPE_LOGOUT)
Darren Tuckerea52a822011-01-17 21:15:27 +1100472 audit_session_close(li);
Darren Tucker269a1ea2005-02-03 00:20:53 +1100473#endif
Damien Miller8899ed32004-09-12 15:18:55 +1000474 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000475}
476
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000477#ifdef LOGIN_NEEDS_UTMPX
478int
479login_utmp_only(struct logininfo *li)
480{
Damien Millera8e06ce2003-11-21 23:48:55 +1100481 li->type = LTYPE_LOGIN;
Ben Lindstrom9197c592001-10-26 15:56:55 +0000482 login_set_current_time(li);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000483# ifdef USE_UTMP
484 utmp_write_entry(li);
485# endif
486# ifdef USE_WTMP
487 wtmp_write_entry(li);
488# endif
489# ifdef USE_UTMPX
490 utmpx_write_entry(li);
491# endif
492# ifdef USE_WTMPX
493 wtmpx_write_entry(li);
494# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000495 return (0);
Ben Lindstrom97c677d2001-05-08 20:33:05 +0000496}
497#endif
498
andre2ff7b5d2000-06-03 14:57:40 +0000499/**
andre61e67252000-06-04 17:07:49 +0000500 ** getlast_entry: Call low-level functions to retrieve the last login
501 ** time.
andre2ff7b5d2000-06-03 14:57:40 +0000502 **/
503
andre61e67252000-06-04 17:07:49 +0000504/* take the uid in li and return the last login time */
505int
506getlast_entry(struct logininfo *li)
507{
508#ifdef USE_LASTLOG
Damien Miller53c5d462000-06-28 00:50:50 +1000509 return(lastlog_get_entry(li));
Damien Millerdd47aa22000-06-27 11:18:27 +1000510#else /* !USE_LASTLOG */
Darren Tucker261d93a2010-04-09 18:13:27 +1000511#if defined(USE_UTMPX) && defined(HAVE_SETUTXDB) && \
512 defined(UTXDB_LASTLOGIN) && defined(HAVE_GETUTXUSER)
513 return (utmpx_get_entry(li));
514#endif
andre61e67252000-06-04 17:07:49 +0000515
Damien Miller8899ed32004-09-12 15:18:55 +1000516#if defined(DISABLE_LASTLOG)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000517 /* On some systems we shouldn't even try to obtain last login
andreecaabf12000-06-12 22:21:44 +0000518 * time, e.g. AIX */
Damien Miller8899ed32004-09-12 15:18:55 +1000519 return (0);
520# elif defined(USE_WTMP) && \
521 (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
andre61e67252000-06-04 17:07:49 +0000522 /* retrieve last login time from utmp */
Damien Millerdd47aa22000-06-27 11:18:27 +1000523 return (wtmp_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000524# elif defined(USE_WTMPX) && \
525 (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
andre61e67252000-06-04 17:07:49 +0000526 /* If wtmp isn't available, try wtmpx */
Damien Millerdd47aa22000-06-27 11:18:27 +1000527 return (wtmpx_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000528# else
andre61e67252000-06-04 17:07:49 +0000529 /* Give up: No means of retrieving last login time */
Damien Miller8899ed32004-09-12 15:18:55 +1000530 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000531# endif /* DISABLE_LASTLOG */
Damien Millerdd47aa22000-06-27 11:18:27 +1000532#endif /* USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000533}
534
535
536
andre2ff7b5d2000-06-03 14:57:40 +0000537/*
andre61e67252000-06-04 17:07:49 +0000538 * 'line' string utility functions
539 *
540 * These functions process the 'line' string into one of three forms:
541 *
andre2ff7b5d2000-06-03 14:57:40 +0000542 * 1. The full filename (including '/dev')
543 * 2. The stripped name (excluding '/dev')
andre61e67252000-06-04 17:07:49 +0000544 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
545 * /dev/pts/1 -> ts/1 )
andre2ff7b5d2000-06-03 14:57:40 +0000546 *
547 * Form 3 is used on some systems to identify a .tmp.? entry when
548 * attempting to remove it. Typically both addition and removal is
andre61e67252000-06-04 17:07:49 +0000549 * performed by one application - say, sshd - so as long as the choice
550 * uniquely identifies a terminal it's ok.
andre2ff7b5d2000-06-03 14:57:40 +0000551 */
552
553
Damien Miller8899ed32004-09-12 15:18:55 +1000554/*
555 * line_fullname(): add the leading '/dev/' if it doesn't exist make
556 * sure dst has enough space, if not just copy src (ugh)
557 */
andre2ff7b5d2000-06-03 14:57:40 +0000558char *
Damien Miller52c8afe2005-06-19 10:19:43 +1000559line_fullname(char *dst, const char *src, u_int dstsize)
andre61e67252000-06-04 17:07:49 +0000560{
andre2ff7b5d2000-06-03 14:57:40 +0000561 memset(dst, '\0', dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000562 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
andre2ff7b5d2000-06-03 14:57:40 +0000563 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000564 else {
Damien Miller1a132252000-06-13 21:23:17 +1000565 strlcpy(dst, "/dev/", dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000566 strlcat(dst, src, dstsize);
567 }
Damien Miller8899ed32004-09-12 15:18:55 +1000568 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000569}
570
andre61e67252000-06-04 17:07:49 +0000571/* line_stripname(): strip the leading '/dev' if it exists, return dst */
andre2ff7b5d2000-06-03 14:57:40 +0000572char *
andre61e67252000-06-04 17:07:49 +0000573line_stripname(char *dst, const char *src, int dstsize)
574{
andre2ff7b5d2000-06-03 14:57:40 +0000575 memset(dst, '\0', dstsize);
576 if (strncmp(src, "/dev/", 5) == 0)
Damien Millerf5a81472000-09-30 21:34:44 +1100577 strlcpy(dst, src + 5, dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000578 else
579 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000580 return (dst);
andre61e67252000-06-04 17:07:49 +0000581}
582
Damien Miller94cf4c82005-07-17 17:04:47 +1000583/*
Damien Miller8899ed32004-09-12 15:18:55 +1000584 * line_abbrevname(): Return the abbreviated (usually four-character)
andre61e67252000-06-04 17:07:49 +0000585 * form of the line (Just use the last <dstsize> characters of the
586 * full name.)
587 *
588 * NOTE: use strncpy because we do NOT necessarily want zero
Damien Miller8899ed32004-09-12 15:18:55 +1000589 * termination
590 */
andre2ff7b5d2000-06-03 14:57:40 +0000591char *
Kevin Stevesef4eea92001-02-05 12:42:17 +0000592line_abbrevname(char *dst, const char *src, int dstsize)
Damien Millerdd47aa22000-06-27 11:18:27 +1000593{
594 size_t len;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000595
andre2ff7b5d2000-06-03 14:57:40 +0000596 memset(dst, '\0', dstsize);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000597
Damien Miller8e81ed32000-07-01 13:17:42 +1000598 /* Always skip prefix if present */
599 if (strncmp(src, "/dev/", 5) == 0)
600 src += 5;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000601
Damien Millerf1b9d112002-04-23 23:09:19 +1000602#ifdef WITH_ABBREV_NO_TTY
603 if (strncmp(src, "tty", 3) == 0)
604 src += 3;
605#endif
606
Damien Millerdd47aa22000-06-27 11:18:27 +1000607 len = strlen(src);
608
Damien Miller8e81ed32000-07-01 13:17:42 +1000609 if (len > 0) {
610 if (((int)len - dstsize) > 0)
611 src += ((int)len - dstsize);
612
613 /* note: _don't_ change this to strlcpy */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000614 strncpy(dst, src, (size_t)dstsize);
Damien Millerdd47aa22000-06-27 11:18:27 +1000615 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000616
Damien Miller8899ed32004-09-12 15:18:55 +1000617 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000618}
619
andre2ff7b5d2000-06-03 14:57:40 +0000620/**
621 ** utmp utility functions
andre61e67252000-06-04 17:07:49 +0000622 **
623 ** These functions manipulate struct utmp, taking system differences
624 ** into account.
andre2ff7b5d2000-06-03 14:57:40 +0000625 **/
626
627#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
628
andre2ff7b5d2000-06-03 14:57:40 +0000629/* build the utmp structure */
630void
andre61e67252000-06-04 17:07:49 +0000631set_utmp_time(struct logininfo *li, struct utmp *ut)
632{
Damien Miller8899ed32004-09-12 15:18:55 +1000633# if defined(HAVE_TV_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000634 ut->ut_tv.tv_sec = li->tv_sec;
635 ut->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000636# elif defined(HAVE_TIME_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000637 ut->ut_time = li->tv_sec;
Damien Millerdd47aa22000-06-27 11:18:27 +1000638# endif
andre2ff7b5d2000-06-03 14:57:40 +0000639}
640
641void
642construct_utmp(struct logininfo *li,
andre61e67252000-06-04 17:07:49 +0000643 struct utmp *ut)
644{
Damien Miller02e16ad2003-01-03 14:42:27 +1100645# ifdef HAVE_ADDR_V6_IN_UTMP
646 struct sockaddr_in6 *sa6;
Damien Miller8899ed32004-09-12 15:18:55 +1000647# endif
648
Damien Miller348c9b72000-08-15 10:01:22 +1000649 memset(ut, '\0', sizeof(*ut));
andre6bb92372000-06-19 08:20:03 +0000650
651 /* First fill out fields used for both logins and logouts */
652
Damien Millerdd47aa22000-06-27 11:18:27 +1000653# ifdef HAVE_ID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000654 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +1000655# endif
andre2ff7b5d2000-06-03 14:57:40 +0000656
Damien Millerdd47aa22000-06-27 11:18:27 +1000657# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +0000658 /* This is done here to keep utmp constants out of struct logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000659 switch (li->type) {
660 case LTYPE_LOGIN:
661 ut->ut_type = USER_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700662#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000663 cray_set_tmpdir(ut);
664#endif
andre2ff7b5d2000-06-03 14:57:40 +0000665 break;
666 case LTYPE_LOGOUT:
667 ut->ut_type = DEAD_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700668#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000669 cray_retain_utmp(ut, li->pid);
670#endif
andre2ff7b5d2000-06-03 14:57:40 +0000671 break;
672 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000673# endif
andre6bb92372000-06-19 08:20:03 +0000674 set_utmp_time(li, ut);
andre2ff7b5d2000-06-03 14:57:40 +0000675
andre6bb92372000-06-19 08:20:03 +0000676 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +1000677
678# ifdef HAVE_PID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000679 ut->ut_pid = li->pid;
Damien Millerdd47aa22000-06-27 11:18:27 +1000680# endif
andre6bb92372000-06-19 08:20:03 +0000681
682 /* If we're logging out, leave all other fields blank */
683 if (li->type == LTYPE_LOGOUT)
Damien Miller8899ed32004-09-12 15:18:55 +1000684 return;
andre6bb92372000-06-19 08:20:03 +0000685
Damien Millerdd47aa22000-06-27 11:18:27 +1000686 /*
687 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000688 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000689 */
andre6bb92372000-06-19 08:20:03 +0000690
691 /* Use strncpy because we don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000692 strncpy(ut->ut_name, li->username,
693 MIN_SIZEOF(ut->ut_name, li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000694# ifdef HAVE_HOST_IN_UTMP
Damien Miller8899ed32004-09-12 15:18:55 +1000695 strncpy(ut->ut_host, li->hostname,
696 MIN_SIZEOF(ut->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000697# endif
698# ifdef HAVE_ADDR_IN_UTMP
andre61e67252000-06-04 17:07:49 +0000699 /* this is just a 32-bit IP address */
700 if (li->hostaddr.sa.sa_family == AF_INET)
701 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000702# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100703# ifdef HAVE_ADDR_V6_IN_UTMP
704 /* this is just a 128-bit IPv6 address */
705 if (li->hostaddr.sa.sa_family == AF_INET6) {
706 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
707 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
708 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
709 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
710 ut->ut_addr_v6[1] = 0;
711 ut->ut_addr_v6[2] = 0;
712 ut->ut_addr_v6[3] = 0;
713 }
714 }
715# endif
andre61e67252000-06-04 17:07:49 +0000716}
Damien Millerdd47aa22000-06-27 11:18:27 +1000717#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
andre61e67252000-06-04 17:07:49 +0000718
andre2ff7b5d2000-06-03 14:57:40 +0000719/**
720 ** utmpx utility functions
andre61e67252000-06-04 17:07:49 +0000721 **
722 ** These functions manipulate struct utmpx, accounting for system
723 ** variations.
andre2ff7b5d2000-06-03 14:57:40 +0000724 **/
725
726#if defined(USE_UTMPX) || defined (USE_WTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000727/* build the utmpx structure */
728void
andre61e67252000-06-04 17:07:49 +0000729set_utmpx_time(struct logininfo *li, struct utmpx *utx)
730{
Damien Miller8899ed32004-09-12 15:18:55 +1000731# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000732 utx->ut_tv.tv_sec = li->tv_sec;
733 utx->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000734# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000735 utx->ut_time = li->tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +1000736# endif
andre2ff7b5d2000-06-03 14:57:40 +0000737}
738
andre61e67252000-06-04 17:07:49 +0000739void
740construct_utmpx(struct logininfo *li, struct utmpx *utx)
741{
Damien Miller02e16ad2003-01-03 14:42:27 +1100742# ifdef HAVE_ADDR_V6_IN_UTMP
743 struct sockaddr_in6 *sa6;
744# endif
Damien Miller348c9b72000-08-15 10:01:22 +1000745 memset(utx, '\0', sizeof(*utx));
Damien Miller8899ed32004-09-12 15:18:55 +1000746
Damien Miller8e81ed32000-07-01 13:17:42 +1000747# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000748 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
Damien Miller8e81ed32000-07-01 13:17:42 +1000749# endif
andre2ff7b5d2000-06-03 14:57:40 +0000750
751 /* this is done here to keep utmp constants out of loginrec.h */
752 switch (li->type) {
753 case LTYPE_LOGIN:
754 utx->ut_type = USER_PROCESS;
755 break;
756 case LTYPE_LOGOUT:
757 utx->ut_type = DEAD_PROCESS;
758 break;
759 }
andre2ff7b5d2000-06-03 14:57:40 +0000760 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
andre2ff7b5d2000-06-03 14:57:40 +0000761 set_utmpx_time(li, utx);
andre6bb92372000-06-19 08:20:03 +0000762 utx->ut_pid = li->pid;
Damien Miller8899ed32004-09-12 15:18:55 +1000763
Tim Ricee06ae4a2002-02-24 17:56:46 -0800764 /* strncpy(): Don't necessarily want null termination */
Darren Tucker0b8a2262010-01-09 18:18:04 +1100765 strncpy(utx->ut_user, li->username,
766 MIN_SIZEOF(utx->ut_user, li->username));
andre6bb92372000-06-19 08:20:03 +0000767
768 if (li->type == LTYPE_LOGOUT)
769 return;
770
Damien Millerdd47aa22000-06-27 11:18:27 +1000771 /*
772 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000773 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000774 */
andre6bb92372000-06-19 08:20:03 +0000775
Damien Millerdd47aa22000-06-27 11:18:27 +1000776# ifdef HAVE_HOST_IN_UTMPX
Damien Miller8899ed32004-09-12 15:18:55 +1000777 strncpy(utx->ut_host, li->hostname,
778 MIN_SIZEOF(utx->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000779# endif
780# ifdef HAVE_ADDR_IN_UTMPX
Damien Millerd6f204d2000-09-23 13:57:27 +1100781 /* this is just a 32-bit IP address */
782 if (li->hostaddr.sa.sa_family == AF_INET)
783 utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Damien Millerdd47aa22000-06-27 11:18:27 +1000784# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100785# ifdef HAVE_ADDR_V6_IN_UTMP
786 /* this is just a 128-bit IPv6 address */
787 if (li->hostaddr.sa.sa_family == AF_INET6) {
788 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
789 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
790 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
791 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
792 ut->ut_addr_v6[1] = 0;
793 ut->ut_addr_v6[2] = 0;
794 ut->ut_addr_v6[3] = 0;
795 }
796 }
797# endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000798# ifdef HAVE_SYSLEN_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +0000799 /* ut_syslen is the length of the utx_host string */
800 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +1000801# endif
andre61e67252000-06-04 17:07:49 +0000802}
Damien Millerdd47aa22000-06-27 11:18:27 +1000803#endif /* USE_UTMPX || USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +0000804
805/**
andre61e67252000-06-04 17:07:49 +0000806 ** Low-level utmp functions
andre2ff7b5d2000-06-03 14:57:40 +0000807 **/
808
809/* FIXME: (ATL) utmp_write_direct needs testing */
andre2ff7b5d2000-06-03 14:57:40 +0000810#ifdef USE_UTMP
811
andre2ff7b5d2000-06-03 14:57:40 +0000812/* if we can, use pututline() etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000813# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
814 defined(HAVE_PUTUTLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000815# define UTMP_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000816# endif
andre2ff7b5d2000-06-03 14:57:40 +0000817
818
819/* write a utmp entry with the system's help (pututline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000820# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000821static int
andre61e67252000-06-04 17:07:49 +0000822utmp_write_library(struct logininfo *li, struct utmp *ut)
823{
andre2ff7b5d2000-06-03 14:57:40 +0000824 setutent();
825 pututline(ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000826# ifdef HAVE_ENDUTENT
andre2ff7b5d2000-06-03 14:57:40 +0000827 endutent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000828# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000829 return (1);
andre61e67252000-06-04 17:07:49 +0000830}
Damien Millerdd47aa22000-06-27 11:18:27 +1000831# else /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000832
Damien Miller94cf4c82005-07-17 17:04:47 +1000833/*
Damien Miller8899ed32004-09-12 15:18:55 +1000834 * Write a utmp entry direct to the file
835 * This is a slightly modification of code in OpenBSD's login.c
836 */
andre2ff7b5d2000-06-03 14:57:40 +0000837static int
andre61e67252000-06-04 17:07:49 +0000838utmp_write_direct(struct logininfo *li, struct utmp *ut)
839{
andre2ff7b5d2000-06-03 14:57:40 +0000840 struct utmp old_ut;
841 register int fd;
842 int tty;
843
andre6bb92372000-06-19 08:20:03 +0000844 /* FIXME: (ATL) ttyslot() needs local implementation */
Damien Miller348c9b72000-08-15 10:01:22 +1000845
Damien Millere5192fa2000-08-29 14:30:37 +1100846#if defined(HAVE_GETTTYENT)
Damien Miller8899ed32004-09-12 15:18:55 +1000847 struct ttyent *ty;
Damien Miller348c9b72000-08-15 10:01:22 +1000848
849 tty=0;
Damien Miller348c9b72000-08-15 10:01:22 +1000850 setttyent();
Damien Miller8899ed32004-09-12 15:18:55 +1000851 while (NULL != (ty = getttyent())) {
Damien Miller348c9b72000-08-15 10:01:22 +1000852 tty++;
853 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
854 break;
855 }
856 endttyent();
857
Damien Miller8899ed32004-09-12 15:18:55 +1000858 if (NULL == ty) {
Damien Miller81409592004-08-15 19:12:52 +1000859 logit("%s: tty not found", __func__);
860 return (0);
Damien Miller348c9b72000-08-15 10:01:22 +1000861 }
862#else /* FIXME */
863
andre2ff7b5d2000-06-03 14:57:40 +0000864 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
865
Damien Millere5192fa2000-08-29 14:30:37 +1100866#endif /* HAVE_GETTTYENT */
Damien Miller348c9b72000-08-15 10:01:22 +1000867
andre2ff7b5d2000-06-03 14:57:40 +0000868 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
Damien Miller81409592004-08-15 19:12:52 +1000869 off_t pos, ret;
870
871 pos = (off_t)tty * sizeof(struct utmp);
872 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000873 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller4a06f922011-01-02 21:43:59 +1100874 close(fd);
Damien Miller81409592004-08-15 19:12:52 +1000875 return (0);
876 }
877 if (ret != pos) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000878 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Millerb0419f22004-08-23 21:53:28 +1000879 __func__, tty, UTMP_FILE);
Damien Miller4a06f922011-01-02 21:43:59 +1100880 close(fd);
Damien Miller81409592004-08-15 19:12:52 +1000881 return (0);
882 }
andre2ff7b5d2000-06-03 14:57:40 +0000883 /*
884 * Prevent luser from zero'ing out ut_host.
885 * If the new ut_line is empty but the old one is not
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000886 * and ut_line and ut_name match, preserve the old ut_line.
andre2ff7b5d2000-06-03 14:57:40 +0000887 */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000888 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
Damien Miller8899ed32004-09-12 15:18:55 +1000889 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
890 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
891 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0))
892 memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000893
Damien Miller81409592004-08-15 19:12:52 +1000894 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000895 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller4a06f922011-01-02 21:43:59 +1100896 close(fd);
Damien Miller81409592004-08-15 19:12:52 +1000897 return (0);
898 }
899 if (ret != pos) {
Damien Millerb0419f22004-08-23 21:53:28 +1000900 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Miller81409592004-08-15 19:12:52 +1000901 __func__, tty, UTMP_FILE);
Damien Miller4a06f922011-01-02 21:43:59 +1100902 close(fd);
Damien Miller81409592004-08-15 19:12:52 +1000903 return (0);
904 }
Damien Miller8899ed32004-09-12 15:18:55 +1000905 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
Damien Miller81409592004-08-15 19:12:52 +1000906 logit("%s: error writing %s: %s", __func__,
andre6bb92372000-06-19 08:20:03 +0000907 UTMP_FILE, strerror(errno));
Damien Miller4a06f922011-01-02 21:43:59 +1100908 close(fd);
909 return (0);
Damien Miller8899ed32004-09-12 15:18:55 +1000910 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000911
Damien Miller8899ed32004-09-12 15:18:55 +1000912 close(fd);
913 return (1);
Damien Miller53c5d462000-06-28 00:50:50 +1000914 } else {
Damien Miller8899ed32004-09-12 15:18:55 +1000915 return (0);
Damien Miller53c5d462000-06-28 00:50:50 +1000916 }
andre61e67252000-06-04 17:07:49 +0000917}
Damien Millerdd47aa22000-06-27 11:18:27 +1000918# endif /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000919
920static int
andre61e67252000-06-04 17:07:49 +0000921utmp_perform_login(struct logininfo *li)
922{
andre2ff7b5d2000-06-03 14:57:40 +0000923 struct utmp ut;
924
925 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000926# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000927 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000928 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000929 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000930 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000931# else
andre2ff7b5d2000-06-03 14:57:40 +0000932 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000933 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000934 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000935 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000936# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000937 return (1);
andre61e67252000-06-04 17:07:49 +0000938}
andre2ff7b5d2000-06-03 14:57:40 +0000939
940
941static int
andre61e67252000-06-04 17:07:49 +0000942utmp_perform_logout(struct logininfo *li)
943{
andre2ff7b5d2000-06-03 14:57:40 +0000944 struct utmp ut;
945
andre6bb92372000-06-19 08:20:03 +0000946 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000947# ifdef UTMP_USE_LIBRARY
andre6bb92372000-06-19 08:20:03 +0000948 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000949 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000950 return (0);
andre6bb92372000-06-19 08:20:03 +0000951 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000952# else
andre6bb92372000-06-19 08:20:03 +0000953 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000954 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000955 return (0);
andre6bb92372000-06-19 08:20:03 +0000956 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000957# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000958 return (1);
andre61e67252000-06-04 17:07:49 +0000959}
andre2ff7b5d2000-06-03 14:57:40 +0000960
961
962int
andre61e67252000-06-04 17:07:49 +0000963utmp_write_entry(struct logininfo *li)
964{
andre2ff7b5d2000-06-03 14:57:40 +0000965 switch(li->type) {
966 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +1000967 return (utmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +0000968
969 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +1000970 return (utmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +0000971
972 default:
Damien Miller6b0279c2004-09-12 15:25:17 +1000973 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000974 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000975 }
andre61e67252000-06-04 17:07:49 +0000976}
Damien Millerdd47aa22000-06-27 11:18:27 +1000977#endif /* USE_UTMP */
andre2ff7b5d2000-06-03 14:57:40 +0000978
979
980/**
andre61e67252000-06-04 17:07:49 +0000981 ** Low-level utmpx functions
andre2ff7b5d2000-06-03 14:57:40 +0000982 **/
983
984/* not much point if we don't want utmpx entries */
985#ifdef USE_UTMPX
986
andre2ff7b5d2000-06-03 14:57:40 +0000987/* if we have the wherewithall, use pututxline etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000988# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
989 defined(HAVE_PUTUTXLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000990# define UTMPX_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000991# endif
andre2ff7b5d2000-06-03 14:57:40 +0000992
993
994/* write a utmpx entry with the system's help (pututxline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000995# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000996static int
andre61e67252000-06-04 17:07:49 +0000997utmpx_write_library(struct logininfo *li, struct utmpx *utx)
998{
andre2ff7b5d2000-06-03 14:57:40 +0000999 setutxent();
1000 pututxline(utx);
1001
Damien Millerdd47aa22000-06-27 11:18:27 +10001002# ifdef HAVE_ENDUTXENT
andre2ff7b5d2000-06-03 14:57:40 +00001003 endutxent();
Damien Millerdd47aa22000-06-27 11:18:27 +10001004# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001005 return (1);
andre61e67252000-06-04 17:07:49 +00001006}
andre2ff7b5d2000-06-03 14:57:40 +00001007
Damien Millerdd47aa22000-06-27 11:18:27 +10001008# else /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +00001009
1010/* write a utmp entry direct to the file */
1011static int
andre61e67252000-06-04 17:07:49 +00001012utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
Kevin Stevesef4eea92001-02-05 12:42:17 +00001013{
Damien Miller6b0279c2004-09-12 15:25:17 +10001014 logit("%s: not implemented!", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001015 return (0);
andre61e67252000-06-04 17:07:49 +00001016}
Damien Millerdd47aa22000-06-27 11:18:27 +10001017# endif /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +00001018
1019static int
andre61e67252000-06-04 17:07:49 +00001020utmpx_perform_login(struct logininfo *li)
1021{
andre2ff7b5d2000-06-03 14:57:40 +00001022 struct utmpx utx;
1023
1024 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001025# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001026 if (!utmpx_write_library(li, &utx)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001027 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001028 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001029 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001030# else
andre2ff7b5d2000-06-03 14:57:40 +00001031 if (!utmpx_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001032 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001033 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001034 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001035# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001036 return (1);
andre61e67252000-06-04 17:07:49 +00001037}
andre2ff7b5d2000-06-03 14:57:40 +00001038
1039
1040static int
andre61e67252000-06-04 17:07:49 +00001041utmpx_perform_logout(struct logininfo *li)
1042{
andre2ff7b5d2000-06-03 14:57:40 +00001043 struct utmpx utx;
1044
Tim Ricee06ae4a2002-02-24 17:56:46 -08001045 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001046# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001047 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +10001048# endif
1049# ifdef HAVE_TYPE_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001050 utx.ut_type = DEAD_PROCESS;
Damien Millerdd47aa22000-06-27 11:18:27 +10001051# endif
andre2ff7b5d2000-06-03 14:57:40 +00001052
Damien Millerdd47aa22000-06-27 11:18:27 +10001053# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001054 utmpx_write_library(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001055# else
andre2ff7b5d2000-06-03 14:57:40 +00001056 utmpx_write_direct(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001057# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001058 return (1);
andre61e67252000-06-04 17:07:49 +00001059}
andre2ff7b5d2000-06-03 14:57:40 +00001060
andre2ff7b5d2000-06-03 14:57:40 +00001061int
andre61e67252000-06-04 17:07:49 +00001062utmpx_write_entry(struct logininfo *li)
1063{
andre2ff7b5d2000-06-03 14:57:40 +00001064 switch(li->type) {
1065 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001066 return (utmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001067 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001068 return (utmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001069 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001070 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001071 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001072 }
andre61e67252000-06-04 17:07:49 +00001073}
Damien Millerdd47aa22000-06-27 11:18:27 +10001074#endif /* USE_UTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001075
1076
1077/**
andre61e67252000-06-04 17:07:49 +00001078 ** Low-level wtmp functions
andre2ff7b5d2000-06-03 14:57:40 +00001079 **/
1080
Kevin Stevesef4eea92001-02-05 12:42:17 +00001081#ifdef USE_WTMP
andre2ff7b5d2000-06-03 14:57:40 +00001082
Damien Miller94cf4c82005-07-17 17:04:47 +10001083/*
Damien Miller8899ed32004-09-12 15:18:55 +10001084 * Write a wtmp entry direct to the end of the file
1085 * This is a slight modification of code in OpenBSD's logwtmp.c
1086 */
andre2ff7b5d2000-06-03 14:57:40 +00001087static int
andre61e67252000-06-04 17:07:49 +00001088wtmp_write(struct logininfo *li, struct utmp *ut)
1089{
andre2ff7b5d2000-06-03 14:57:40 +00001090 struct stat buf;
1091 int fd, ret = 1;
1092
1093 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001094 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001095 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001096 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001097 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001098 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001099 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
andre2ff7b5d2000-06-03 14:57:40 +00001100 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001101 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001102 WTMP_FILE, strerror(errno));
1103 ret = 0;
1104 }
Damien Miller8899ed32004-09-12 15:18:55 +10001105 close(fd);
1106 return (ret);
andre61e67252000-06-04 17:07:49 +00001107}
andre2ff7b5d2000-06-03 14:57:40 +00001108
andre2ff7b5d2000-06-03 14:57:40 +00001109static int
Damien Millerdd47aa22000-06-27 11:18:27 +10001110wtmp_perform_login(struct logininfo *li)
1111{
andre2ff7b5d2000-06-03 14:57:40 +00001112 struct utmp ut;
1113
1114 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001115 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001116}
andre2ff7b5d2000-06-03 14:57:40 +00001117
1118
1119static int
andre61e67252000-06-04 17:07:49 +00001120wtmp_perform_logout(struct logininfo *li)
1121{
andre2ff7b5d2000-06-03 14:57:40 +00001122 struct utmp ut;
1123
1124 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001125 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001126}
andre2ff7b5d2000-06-03 14:57:40 +00001127
1128
1129int
andre61e67252000-06-04 17:07:49 +00001130wtmp_write_entry(struct logininfo *li)
1131{
andre2ff7b5d2000-06-03 14:57:40 +00001132 switch(li->type) {
1133 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001134 return (wtmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001135 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001136 return (wtmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001137 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001138 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001139 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001140 }
andre61e67252000-06-04 17:07:49 +00001141}
andre2ff7b5d2000-06-03 14:57:40 +00001142
1143
Damien Miller94cf4c82005-07-17 17:04:47 +10001144/*
Damien Miller8899ed32004-09-12 15:18:55 +10001145 * Notes on fetching login data from wtmp/wtmpx
Kevin Stevesef4eea92001-02-05 12:42:17 +00001146 *
andre6bb92372000-06-19 08:20:03 +00001147 * Logouts are usually recorded with (amongst other things) a blank
1148 * username on a given tty line. However, some systems (HP-UX is one)
1149 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1150 *
1151 * Since we're only looking for logins here, we know that the username
1152 * must be set correctly. On systems that leave it in, we check for
1153 * ut_type==USER_PROCESS (indicating a login.)
1154 *
1155 * Portability: Some systems may set something other than USER_PROCESS
1156 * to indicate a login process. I don't know of any as I write. Also,
1157 * it's possible that some systems may both leave the username in
1158 * place and not have ut_type.
1159 */
1160
andre6bb92372000-06-19 08:20:03 +00001161/* return true if this wtmp entry indicates a login */
1162static int
1163wtmp_islogin(struct logininfo *li, struct utmp *ut)
1164{
Kevin Stevesef4eea92001-02-05 12:42:17 +00001165 if (strncmp(li->username, ut->ut_name,
Damien Miller8899ed32004-09-12 15:18:55 +10001166 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001167# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001168 if (ut->ut_type & USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001169 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001170# else
Damien Miller8899ed32004-09-12 15:18:55 +10001171 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001172# endif
andre6bb92372000-06-19 08:20:03 +00001173 }
Damien Miller8899ed32004-09-12 15:18:55 +10001174 return (0);
andre6bb92372000-06-19 08:20:03 +00001175}
1176
andre2ff7b5d2000-06-03 14:57:40 +00001177int
andre61e67252000-06-04 17:07:49 +00001178wtmp_get_entry(struct logininfo *li)
1179{
andre2ff7b5d2000-06-03 14:57:40 +00001180 struct stat st;
1181 struct utmp ut;
Damien Miller8899ed32004-09-12 15:18:55 +10001182 int fd, found = 0;
andre6bb92372000-06-19 08:20:03 +00001183
1184 /* Clear the time entries in our logininfo */
1185 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001186
1187 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001188 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001189 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001190 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001191 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001192 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001193 logit("%s: couldn't stat %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001194 WTMP_FILE, strerror(errno));
1195 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001196 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001197 }
andre2ff7b5d2000-06-03 14:57:40 +00001198
andre6bb92372000-06-19 08:20:03 +00001199 /* Seek to the start of the last struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001200 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
andre6bb92372000-06-19 08:20:03 +00001201 /* Looks like we've got a fresh wtmp file */
1202 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001203 return (0);
andre6bb92372000-06-19 08:20:03 +00001204 }
1205
1206 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001207 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001208 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001209 WTMP_FILE, strerror(errno));
1210 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001211 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001212 }
Damien Miller4a06f922011-01-02 21:43:59 +11001213 if (wtmp_islogin(li, &ut) ) {
andre6bb92372000-06-19 08:20:03 +00001214 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001215 /*
1216 * We've already checked for a time in struct
1217 * utmp, in login_getlast()
1218 */
Damien Millerdd47aa22000-06-27 11:18:27 +10001219# ifdef HAVE_TIME_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +00001220 li->tv_sec = ut.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001221# else
andre2ff7b5d2000-06-03 14:57:40 +00001222# if HAVE_TV_IN_UTMP
1223 li->tv_sec = ut.ut_tv.tv_sec;
1224# endif
Damien Millerdd47aa22000-06-27 11:18:27 +10001225# endif
andre6bb92372000-06-19 08:20:03 +00001226 line_fullname(li->line, ut.ut_line,
Damien Miller8899ed32004-09-12 15:18:55 +10001227 MIN_SIZEOF(li->line, ut.ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +10001228# ifdef HAVE_HOST_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001229 strlcpy(li->hostname, ut.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001230 MIN_SIZEOF(li->hostname, ut.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001231# endif
andre6bb92372000-06-19 08:20:03 +00001232 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001233 }
andre6bb92372000-06-19 08:20:03 +00001234 /* Seek back 2 x struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001235 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
andre6bb92372000-06-19 08:20:03 +00001236 /* We've found the start of the file, so quit */
Damien Miller8899ed32004-09-12 15:18:55 +10001237 close(fd);
1238 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001239 }
andre6bb92372000-06-19 08:20:03 +00001240 }
1241
1242 /* We found an entry. Tidy up and return */
1243 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001244 return (1);
andre61e67252000-06-04 17:07:49 +00001245}
Damien Millerdd47aa22000-06-27 11:18:27 +10001246# endif /* USE_WTMP */
andre2ff7b5d2000-06-03 14:57:40 +00001247
1248
1249/**
andre61e67252000-06-04 17:07:49 +00001250 ** Low-level wtmpx functions
andre2ff7b5d2000-06-03 14:57:40 +00001251 **/
1252
1253#ifdef USE_WTMPX
Damien Miller8899ed32004-09-12 15:18:55 +10001254/*
1255 * Write a wtmpx entry direct to the end of the file
1256 * This is a slight modification of code in OpenBSD's logwtmp.c
1257 */
andre2ff7b5d2000-06-03 14:57:40 +00001258static int
andre61e67252000-06-04 17:07:49 +00001259wtmpx_write(struct logininfo *li, struct utmpx *utx)
1260{
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001261#ifndef HAVE_UPDWTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001262 struct stat buf;
1263 int fd, ret = 1;
1264
1265 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001266 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001267 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001268 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001269 }
1270
Kevin Stevesef4eea92001-02-05 12:42:17 +00001271 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001272 if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
andre2ff7b5d2000-06-03 14:57:40 +00001273 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001274 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001275 WTMPX_FILE, strerror(errno));
1276 ret = 0;
1277 }
Damien Miller8899ed32004-09-12 15:18:55 +10001278 close(fd);
andre2ff7b5d2000-06-03 14:57:40 +00001279
Damien Miller8899ed32004-09-12 15:18:55 +10001280 return (ret);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001281#else
1282 updwtmpx(WTMPX_FILE, utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001283 return (1);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001284#endif
andre61e67252000-06-04 17:07:49 +00001285}
andre2ff7b5d2000-06-03 14:57:40 +00001286
1287
1288static int
andre61e67252000-06-04 17:07:49 +00001289wtmpx_perform_login(struct logininfo *li)
1290{
andre2ff7b5d2000-06-03 14:57:40 +00001291 struct utmpx utx;
1292
1293 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001294 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001295}
andre2ff7b5d2000-06-03 14:57:40 +00001296
1297
1298static int
andre61e67252000-06-04 17:07:49 +00001299wtmpx_perform_logout(struct logininfo *li)
1300{
andre2ff7b5d2000-06-03 14:57:40 +00001301 struct utmpx utx;
1302
1303 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001304 return (wtmpx_write(li, &utx));
andre61e67252000-06-04 17:07:49 +00001305}
andre2ff7b5d2000-06-03 14:57:40 +00001306
1307
1308int
andre61e67252000-06-04 17:07:49 +00001309wtmpx_write_entry(struct logininfo *li)
1310{
andre2ff7b5d2000-06-03 14:57:40 +00001311 switch(li->type) {
1312 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001313 return (wtmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001314 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001315 return (wtmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001316 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001317 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001318 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001319 }
andre61e67252000-06-04 17:07:49 +00001320}
andre2ff7b5d2000-06-03 14:57:40 +00001321
andre6bb92372000-06-19 08:20:03 +00001322/* Please see the notes above wtmp_islogin() for information about the
1323 next two functions */
1324
1325/* Return true if this wtmpx entry indicates a login */
1326static int
1327wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1328{
Darren Tucker0b8a2262010-01-09 18:18:04 +11001329 if (strncmp(li->username, utx->ut_user,
1330 MIN_SIZEOF(li->username, utx->ut_user)) == 0 ) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001331# ifdef HAVE_TYPE_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +00001332 if (utx->ut_type == USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001333 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001334# else
Damien Miller8899ed32004-09-12 15:18:55 +10001335 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001336# endif
andre6bb92372000-06-19 08:20:03 +00001337 }
Damien Miller8899ed32004-09-12 15:18:55 +10001338 return (0);
andre6bb92372000-06-19 08:20:03 +00001339}
1340
andre2ff7b5d2000-06-03 14:57:40 +00001341
1342int
andre61e67252000-06-04 17:07:49 +00001343wtmpx_get_entry(struct logininfo *li)
1344{
andre2ff7b5d2000-06-03 14:57:40 +00001345 struct stat st;
1346 struct utmpx utx;
andre6bb92372000-06-19 08:20:03 +00001347 int fd, found=0;
1348
1349 /* Clear the time entries */
1350 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001351
1352 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001353 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001354 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001355 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001356 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001357 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001358 logit("%s: couldn't stat %s: %s", __func__,
Tim Ricecdb82942002-07-14 15:33:20 -07001359 WTMPX_FILE, strerror(errno));
andre2ff7b5d2000-06-03 14:57:40 +00001360 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001361 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001362 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001363
andre6bb92372000-06-19 08:20:03 +00001364 /* Seek to the start of the last struct utmpx */
Kevin Steves52172652001-10-02 00:29:00 +00001365 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
andre6bb92372000-06-19 08:20:03 +00001366 /* probably a newly rotated wtmpx file */
1367 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001368 return (0);
andre6bb92372000-06-19 08:20:03 +00001369 }
andre2ff7b5d2000-06-03 14:57:40 +00001370
andre6bb92372000-06-19 08:20:03 +00001371 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001372 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001373 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001374 WTMPX_FILE, strerror(errno));
1375 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001376 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001377 }
Damien Miller8899ed32004-09-12 15:18:55 +10001378 /*
Damien Miller94cf4c82005-07-17 17:04:47 +10001379 * Logouts are recorded as a blank username on a particular
Damien Miller8899ed32004-09-12 15:18:55 +10001380 * line. So, we just need to find the username in struct utmpx
1381 */
1382 if (wtmpx_islogin(li, &utx)) {
Tim Rice370e0ba2002-07-14 15:50:51 -07001383 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001384# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001385 li->tv_sec = utx.ut_tv.tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +10001386# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001387 li->tv_sec = utx.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001388# endif
Damien Miller1a132252000-06-13 21:23:17 +10001389 line_fullname(li->line, utx.ut_line, sizeof(li->line));
Damien Miller8899ed32004-09-12 15:18:55 +10001390# if defined(HAVE_HOST_IN_UTMPX)
andre6bb92372000-06-19 08:20:03 +00001391 strlcpy(li->hostname, utx.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001392 MIN_SIZEOF(li->hostname, utx.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001393# endif
andre6bb92372000-06-19 08:20:03 +00001394 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001395 }
Kevin Steves52172652001-10-02 00:29:00 +00001396 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
Damien Miller8899ed32004-09-12 15:18:55 +10001397 close(fd);
1398 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001399 }
andre6bb92372000-06-19 08:20:03 +00001400 }
1401
1402 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001403 return (1);
andre61e67252000-06-04 17:07:49 +00001404}
Damien Millerd5bf3072000-06-07 21:32:13 +10001405#endif /* USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001406
andre2ff7b5d2000-06-03 14:57:40 +00001407/**
andre61e67252000-06-04 17:07:49 +00001408 ** Low-level libutil login() functions
andre2ff7b5d2000-06-03 14:57:40 +00001409 **/
1410
1411#ifdef USE_LOGIN
andre2ff7b5d2000-06-03 14:57:40 +00001412static int
andre61e67252000-06-04 17:07:49 +00001413syslogin_perform_login(struct logininfo *li)
1414{
andre2ff7b5d2000-06-03 14:57:40 +00001415 struct utmp *ut;
1416
Damien Millerb0aae332004-09-12 15:26:00 +10001417 ut = xmalloc(sizeof(*ut));
andre2ff7b5d2000-06-03 14:57:40 +00001418 construct_utmp(li, ut);
1419 login(ut);
Damien Millerf211efc2003-03-10 11:23:06 +11001420 free(ut);
andre2ff7b5d2000-06-03 14:57:40 +00001421
Damien Miller8899ed32004-09-12 15:18:55 +10001422 return (1);
andre61e67252000-06-04 17:07:49 +00001423}
1424
andre2ff7b5d2000-06-03 14:57:40 +00001425static int
andre61e67252000-06-04 17:07:49 +00001426syslogin_perform_logout(struct logininfo *li)
1427{
Damien Millerdd47aa22000-06-27 11:18:27 +10001428# ifdef HAVE_LOGOUT
Darren Tucker4d2f3612004-04-08 10:57:05 +10001429 char line[UT_LINESIZE];
Kevin Stevesef4eea92001-02-05 12:42:17 +00001430
andre2ff7b5d2000-06-03 14:57:40 +00001431 (void)line_stripname(line, li->line, sizeof(line));
1432
Damien Miller8899ed32004-09-12 15:18:55 +10001433 if (!logout(line))
Damien Miller6b0279c2004-09-12 15:25:17 +10001434 logit("%s: logout() returned an error", __func__);
Damien Millerdd47aa22000-06-27 11:18:27 +10001435# ifdef HAVE_LOGWTMP
Damien Miller8899ed32004-09-12 15:18:55 +10001436 else
andre2ff7b5d2000-06-03 14:57:40 +00001437 logwtmp(line, "", "");
Damien Millerdd47aa22000-06-27 11:18:27 +10001438# endif
andre6bb92372000-06-19 08:20:03 +00001439 /* FIXME: (ATL - if the need arises) What to do if we have
1440 * login, but no logout? what if logout but no logwtmp? All
1441 * routines are in libutil so they should all be there,
1442 * but... */
Damien Millerdd47aa22000-06-27 11:18:27 +10001443# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001444 return (1);
andre61e67252000-06-04 17:07:49 +00001445}
andre2ff7b5d2000-06-03 14:57:40 +00001446
andre2ff7b5d2000-06-03 14:57:40 +00001447int
andre61e67252000-06-04 17:07:49 +00001448syslogin_write_entry(struct logininfo *li)
1449{
andre2ff7b5d2000-06-03 14:57:40 +00001450 switch (li->type) {
1451 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001452 return (syslogin_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001453 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001454 return (syslogin_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001455 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001456 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001457 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001458 }
andre61e67252000-06-04 17:07:49 +00001459}
Damien Millerd5bf3072000-06-07 21:32:13 +10001460#endif /* USE_LOGIN */
andre2ff7b5d2000-06-03 14:57:40 +00001461
1462/* end of file log-syslogin.c */
1463
andre2ff7b5d2000-06-03 14:57:40 +00001464/**
andre61e67252000-06-04 17:07:49 +00001465 ** Low-level lastlog functions
andre2ff7b5d2000-06-03 14:57:40 +00001466 **/
1467
1468#ifdef USE_LASTLOG
1469
Damien Miller20e231f2009-02-12 13:12:21 +11001470#if !defined(LASTLOG_WRITE_PUTUTXLINE) || !defined(HAVE_GETLASTLOGXBYNAME)
1471/* open the file (using filemode) and seek to the login entry */
andre2ff7b5d2000-06-03 14:57:40 +00001472static int
Damien Miller20e231f2009-02-12 13:12:21 +11001473lastlog_openseek(struct logininfo *li, int *fd, int filemode)
andre61e67252000-06-04 17:07:49 +00001474{
Damien Miller20e231f2009-02-12 13:12:21 +11001475 off_t offset;
1476 char lastlog_file[1024];
andre2ff7b5d2000-06-03 14:57:40 +00001477 struct stat st;
1478
Damien Millerdd47aa22000-06-27 11:18:27 +10001479 if (stat(LASTLOG_FILE, &st) != 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001480 logit("%s: Couldn't stat %s: %s", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001481 LASTLOG_FILE, strerror(errno));
1482 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001483 }
Damien Miller20e231f2009-02-12 13:12:21 +11001484 if (S_ISDIR(st.st_mode)) {
Damien Miller8899ed32004-09-12 15:18:55 +10001485 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1486 LASTLOG_FILE, li->username);
Damien Miller20e231f2009-02-12 13:12:21 +11001487 } else if (S_ISREG(st.st_mode)) {
1488 strlcpy(lastlog_file, LASTLOG_FILE, sizeof(lastlog_file));
1489 } else {
Damien Miller6b0279c2004-09-12 15:25:17 +10001490 logit("%s: %.100s is not a file or directory!", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001491 LASTLOG_FILE);
1492 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001493 }
andre2ff7b5d2000-06-03 14:57:40 +00001494
Damien Miller405dc602003-04-09 21:12:52 +10001495 *fd = open(lastlog_file, filemode, 0600);
Damien Miller8899ed32004-09-12 15:18:55 +10001496 if (*fd < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001497 debug("%s: Couldn't open %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001498 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001499 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001500 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001501
Damien Miller20e231f2009-02-12 13:12:21 +11001502 if (S_ISREG(st.st_mode)) {
Damien Millere477ef62000-08-15 10:21:17 +10001503 /* find this uid's offset in the lastlog file */
Damien Miller34ee4202010-11-05 10:52:37 +11001504 offset = (off_t) ((u_long)li->uid * sizeof(struct lastlog));
andre2ff7b5d2000-06-03 14:57:40 +00001505
Damien Miller8899ed32004-09-12 15:18:55 +10001506 if (lseek(*fd, offset, SEEK_SET) != offset) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001507 logit("%s: %s->lseek(): %s", __func__,
1508 lastlog_file, strerror(errno));
Damien Miller4a06f922011-01-02 21:43:59 +11001509 close(*fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001510 return (0);
Damien Millere477ef62000-08-15 10:21:17 +10001511 }
andre2ff7b5d2000-06-03 14:57:40 +00001512 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001513
Damien Miller8899ed32004-09-12 15:18:55 +10001514 return (1);
andre61e67252000-06-04 17:07:49 +00001515}
Damien Miller20e231f2009-02-12 13:12:21 +11001516#endif /* !LASTLOG_WRITE_PUTUTXLINE || !HAVE_GETLASTLOGXBYNAME */
andre2ff7b5d2000-06-03 14:57:40 +00001517
Damien Miller20e231f2009-02-12 13:12:21 +11001518#ifdef LASTLOG_WRITE_PUTUTXLINE
andre2ff7b5d2000-06-03 14:57:40 +00001519int
andre61e67252000-06-04 17:07:49 +00001520lastlog_write_entry(struct logininfo *li)
1521{
andre2ff7b5d2000-06-03 14:57:40 +00001522 switch(li->type) {
1523 case LTYPE_LOGIN:
Damien Miller20e231f2009-02-12 13:12:21 +11001524 return 1; /* lastlog written by pututxline */
1525 default:
1526 logit("lastlog_write_entry: Invalid type field");
1527 return 0;
1528 }
1529}
1530#else /* LASTLOG_WRITE_PUTUTXLINE */
1531int
1532lastlog_write_entry(struct logininfo *li)
1533{
1534 struct lastlog last;
1535 int fd;
1536
1537 switch(li->type) {
1538 case LTYPE_LOGIN:
1539 /* create our struct lastlog */
1540 memset(&last, '\0', sizeof(last));
1541 line_stripname(last.ll_line, li->line, sizeof(last.ll_line));
1542 strlcpy(last.ll_host, li->hostname,
1543 MIN_SIZEOF(last.ll_host, li->hostname));
1544 last.ll_time = li->tv_sec;
1545
1546 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
1547 return (0);
1548
1549 /* write the entry */
1550 if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) {
1551 close(fd);
1552 logit("%s: Error writing to %s: %s", __func__,
1553 LASTLOG_FILE, strerror(errno));
1554 return (0);
1555 }
1556
1557 close(fd);
1558 return (1);
andre2ff7b5d2000-06-03 14:57:40 +00001559 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001560 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001561 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001562 }
andre61e67252000-06-04 17:07:49 +00001563}
Damien Miller20e231f2009-02-12 13:12:21 +11001564#endif /* LASTLOG_WRITE_PUTUTXLINE */
andre2ff7b5d2000-06-03 14:57:40 +00001565
Damien Miller20e231f2009-02-12 13:12:21 +11001566#ifdef HAVE_GETLASTLOGXBYNAME
1567int
1568lastlog_get_entry(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +00001569{
Damien Miller20e231f2009-02-12 13:12:21 +11001570 struct lastlogx l, *ll;
andre2ff7b5d2000-06-03 14:57:40 +00001571
Damien Miller20e231f2009-02-12 13:12:21 +11001572 if ((ll = getlastlogxbyname(li->username, &l)) == NULL) {
1573 memset(&l, '\0', sizeof(l));
1574 ll = &l;
1575 }
1576 line_fullname(li->line, ll->ll_line, sizeof(li->line));
1577 strlcpy(li->hostname, ll->ll_host,
1578 MIN_SIZEOF(li->hostname, ll->ll_host));
1579 li->tv_sec = ll->ll_tv.tv_sec;
1580 li->tv_usec = ll->ll_tv.tv_usec;
1581 return (1);
1582}
1583#else /* HAVE_GETLASTLOGXBYNAME */
andre2ff7b5d2000-06-03 14:57:40 +00001584int
andre61e67252000-06-04 17:07:49 +00001585lastlog_get_entry(struct logininfo *li)
1586{
andre2ff7b5d2000-06-03 14:57:40 +00001587 struct lastlog last;
Damien Miller7df881d2003-01-07 16:46:58 +11001588 int fd, ret;
andre2ff7b5d2000-06-03 14:57:40 +00001589
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001590 if (!lastlog_openseek(li, &fd, O_RDONLY))
Damien Miller7df881d2003-01-07 16:46:58 +11001591 return (0);
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001592
Damien Miller7df881d2003-01-07 16:46:58 +11001593 ret = atomicio(read, fd, &last, sizeof(last));
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001594 close(fd);
1595
Damien Miller7df881d2003-01-07 16:46:58 +11001596 switch (ret) {
1597 case 0:
1598 memset(&last, '\0', sizeof(last));
1599 /* FALLTHRU */
1600 case sizeof(last):
Damien Miller20e231f2009-02-12 13:12:21 +11001601 line_fullname(li->line, last.ll_line, sizeof(li->line));
1602 strlcpy(li->hostname, last.ll_host,
1603 MIN_SIZEOF(li->hostname, last.ll_host));
1604 li->tv_sec = last.ll_time;
Damien Miller7df881d2003-01-07 16:46:58 +11001605 return (1);
1606 case -1:
Damien Millera8e06ce2003-11-21 23:48:55 +11001607 error("%s: Error reading from %s: %s", __func__,
Damien Miller7df881d2003-01-07 16:46:58 +11001608 LASTLOG_FILE, strerror(errno));
1609 return (0);
1610 default:
1611 error("%s: Error reading from %s: Expecting %d, got %d",
Darren Tuckerefc17472005-11-22 19:55:13 +11001612 __func__, LASTLOG_FILE, (int)sizeof(last), ret);
Damien Miller7df881d2003-01-07 16:46:58 +11001613 return (0);
1614 }
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001615
Damien Miller7df881d2003-01-07 16:46:58 +11001616 /* NOTREACHED */
1617 return (0);
andre61e67252000-06-04 17:07:49 +00001618}
Damien Miller20e231f2009-02-12 13:12:21 +11001619#endif /* HAVE_GETLASTLOGXBYNAME */
Damien Millerd5bf3072000-06-07 21:32:13 +10001620#endif /* USE_LASTLOG */
Darren Tucker2fba9932005-02-02 23:30:24 +11001621
Darren Tucker261d93a2010-04-09 18:13:27 +10001622#if defined(USE_UTMPX) && defined(HAVE_SETUTXDB) && \
1623 defined(UTXDB_LASTLOGIN) && defined(HAVE_GETUTXUSER)
1624int
1625utmpx_get_entry(struct logininfo *li)
1626{
1627 struct utmpx *utx;
1628
1629 if (setutxdb(UTXDB_LASTLOGIN, NULL) != 0)
1630 return (0);
1631 utx = getutxuser(li->username);
1632 if (utx == NULL) {
1633 endutxent();
1634 return (0);
1635 }
1636
1637 line_fullname(li->line, utx->ut_line,
1638 MIN_SIZEOF(li->line, utx->ut_line));
1639 strlcpy(li->hostname, utx->ut_host,
1640 MIN_SIZEOF(li->hostname, utx->ut_host));
1641 li->tv_sec = utx->ut_tv.tv_sec;
1642 li->tv_usec = utx->ut_tv.tv_usec;
1643 endutxent();
1644 return (1);
1645}
1646#endif /* USE_UTMPX && HAVE_SETUTXDB && UTXDB_LASTLOGIN && HAVE_GETUTXUSER */
1647
Darren Tucker2fba9932005-02-02 23:30:24 +11001648#ifdef USE_BTMP
1649 /*
1650 * Logs failed login attempts in _PATH_BTMP if that exists.
1651 * The most common login failure is to give password instead of username.
1652 * So the _PATH_BTMP file checked for the correct permission, so that
1653 * only root can read it.
1654 */
1655
1656void
1657record_failed_login(const char *username, const char *hostname,
1658 const char *ttyn)
1659{
1660 int fd;
1661 struct utmp ut;
1662 struct sockaddr_storage from;
Darren Tuckerefc17472005-11-22 19:55:13 +11001663 socklen_t fromlen = sizeof(from);
Darren Tucker2fba9932005-02-02 23:30:24 +11001664 struct sockaddr_in *a4;
1665 struct sockaddr_in6 *a6;
1666 time_t t;
1667 struct stat fst;
1668
1669 if (geteuid() != 0)
1670 return;
1671 if ((fd = open(_PATH_BTMP, O_WRONLY | O_APPEND)) < 0) {
1672 debug("Unable to open the btmp file %s: %s", _PATH_BTMP,
1673 strerror(errno));
1674 return;
1675 }
1676 if (fstat(fd, &fst) < 0) {
1677 logit("%s: fstat of %s failed: %s", __func__, _PATH_BTMP,
1678 strerror(errno));
1679 goto out;
1680 }
Damien Miller88e341e2010-11-24 10:36:15 +11001681 if((fst.st_mode & (S_IXGRP | S_IRWXO)) || (fst.st_uid != 0)){
Darren Tucker2fba9932005-02-02 23:30:24 +11001682 logit("Excess permission or bad ownership on file %s",
1683 _PATH_BTMP);
1684 goto out;
1685 }
1686
1687 memset(&ut, 0, sizeof(ut));
1688 /* strncpy because we don't necessarily want nul termination */
1689 strncpy(ut.ut_user, username, sizeof(ut.ut_user));
1690 strlcpy(ut.ut_line, "ssh:notty", sizeof(ut.ut_line));
1691
1692 time(&t);
1693 ut.ut_time = t; /* ut_time is not always a time_t */
1694 ut.ut_type = LOGIN_PROCESS;
1695 ut.ut_pid = getpid();
1696
1697 /* strncpy because we don't necessarily want nul termination */
1698 strncpy(ut.ut_host, hostname, sizeof(ut.ut_host));
1699
1700 if (packet_connection_is_on_socket() &&
1701 getpeername(packet_get_connection_in(),
1702 (struct sockaddr *)&from, &fromlen) == 0) {
1703 ipv64_normalise_mapped(&from, &fromlen);
1704 if (from.ss_family == AF_INET) {
1705 a4 = (struct sockaddr_in *)&from;
1706 memcpy(&ut.ut_addr, &(a4->sin_addr),
1707 MIN_SIZEOF(ut.ut_addr, a4->sin_addr));
1708 }
1709#ifdef HAVE_ADDR_V6_IN_UTMP
1710 if (from.ss_family == AF_INET6) {
1711 a6 = (struct sockaddr_in6 *)&from;
1712 memcpy(&ut.ut_addr_v6, &(a6->sin6_addr),
1713 MIN_SIZEOF(ut.ut_addr_v6, a6->sin6_addr));
1714 }
1715#endif
1716 }
1717
1718 if (atomicio(vwrite, fd, &ut, sizeof(ut)) != sizeof(ut))
1719 error("Failed to write to %s: %s", _PATH_BTMP,
1720 strerror(errno));
1721
1722out:
1723 close(fd);
1724}
1725#endif /* USE_BTMP */