blob: b411141987214f137962aad010b97715ef81e9f7 [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
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000183#ifdef HAVE_LIBUTIL_H
Damien Miller8899ed32004-09-12 15:18:55 +1000184# include <libutil.h>
Ben Lindstrome2fb8d32000-12-28 00:07:07 +0000185#endif
186
andre2ff7b5d2000-06-03 14:57:40 +0000187/**
188 ** prototypes for helper functions in this file
189 **/
190
191#if HAVE_UTMP_H
andre2ff7b5d2000-06-03 14:57:40 +0000192void set_utmp_time(struct logininfo *li, struct utmp *ut);
193void construct_utmp(struct logininfo *li, struct utmp *ut);
194#endif
195
196#ifdef HAVE_UTMPX_H
andre2ff7b5d2000-06-03 14:57:40 +0000197void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
198void construct_utmpx(struct logininfo *li, struct utmpx *ut);
199#endif
200
201int utmp_write_entry(struct logininfo *li);
202int utmpx_write_entry(struct logininfo *li);
203int wtmp_write_entry(struct logininfo *li);
204int wtmpx_write_entry(struct logininfo *li);
205int lastlog_write_entry(struct logininfo *li);
206int syslogin_write_entry(struct logininfo *li);
207
208int getlast_entry(struct logininfo *li);
209int lastlog_get_entry(struct logininfo *li);
210int wtmp_get_entry(struct logininfo *li);
211int wtmpx_get_entry(struct logininfo *li);
212
Darren Tucker691d5232005-02-15 21:45:57 +1100213extern Buffer loginmsg;
214
andre6bb92372000-06-19 08:20:03 +0000215/* pick the shortest string */
Damien Miller8899ed32004-09-12 15:18:55 +1000216#define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2))
andre6bb92372000-06-19 08:20:03 +0000217
andre2ff7b5d2000-06-03 14:57:40 +0000218/**
219 ** platform-independent login functions
220 **/
221
Damien Miller8899ed32004-09-12 15:18:55 +1000222/*
223 * login_login(struct logininfo *) - Record a login
Kevin Stevesef4eea92001-02-05 12:42:17 +0000224 *
andre6bb92372000-06-19 08:20:03 +0000225 * Call with a pointer to a struct logininfo initialised with
226 * login_init_entry() or login_alloc_entry()
227 *
228 * Returns:
229 * >0 if successful
230 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
231 */
andre61e67252000-06-04 17:07:49 +0000232int
Damien Miller8899ed32004-09-12 15:18:55 +1000233login_login(struct logininfo *li)
andre61e67252000-06-04 17:07:49 +0000234{
235 li->type = LTYPE_LOGIN;
Damien Miller8899ed32004-09-12 15:18:55 +1000236 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000237}
238
239
Damien Miller8899ed32004-09-12 15:18:55 +1000240/*
241 * login_logout(struct logininfo *) - Record a logout
andre6bb92372000-06-19 08:20:03 +0000242 *
243 * Call as with login_login()
244 *
245 * Returns:
246 * >0 if successful
247 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
248 */
andre61e67252000-06-04 17:07:49 +0000249int
250login_logout(struct logininfo *li)
251{
252 li->type = LTYPE_LOGOUT;
Damien Miller8899ed32004-09-12 15:18:55 +1000253 return (login_write(li));
andre61e67252000-06-04 17:07:49 +0000254}
255
Damien Miller8899ed32004-09-12 15:18:55 +1000256/*
257 * login_get_lastlog_time(int) - Retrieve the last login time
andre6bb92372000-06-19 08:20:03 +0000258 *
259 * Retrieve the last login time for the given uid. Will try to use the
260 * system lastlog facilities if they are available, but will fall back
261 * to looking in wtmp/wtmpx if necessary
262 *
263 * Returns:
264 * 0 on failure, or if user has never logged in
265 * Time in seconds from the epoch if successful
266 *
267 * Useful preprocessor symbols:
268 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
269 * info
270 * USE_LASTLOG: If set, indicates the presence of system lastlog
271 * facilities. If this and DISABLE_LASTLOG are not set,
272 * try to retrieve lastlog information from wtmp/wtmpx.
273 */
andre61e67252000-06-04 17:07:49 +0000274unsigned int
275login_get_lastlog_time(const int uid)
276{
277 struct logininfo li;
278
andre6bb92372000-06-19 08:20:03 +0000279 if (login_get_lastlog(&li, uid))
Damien Miller8899ed32004-09-12 15:18:55 +1000280 return (li.tv_sec);
andre6bb92372000-06-19 08:20:03 +0000281 else
Damien Miller8899ed32004-09-12 15:18:55 +1000282 return (0);
andre61e67252000-06-04 17:07:49 +0000283}
284
Damien Miller8899ed32004-09-12 15:18:55 +1000285/*
286 * login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
andre6bb92372000-06-19 08:20:03 +0000287 *
288 * Retrieve a logininfo structure populated (only partially) with
289 * information from the system lastlog data, or from wtmp/wtmpx if no
290 * system lastlog information exists.
291 *
292 * Note this routine must be given a pre-allocated logininfo.
293 *
294 * Returns:
295 * >0: A pointer to your struct logininfo if successful
296 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
andre6bb92372000-06-19 08:20:03 +0000297 */
andre61e67252000-06-04 17:07:49 +0000298struct logininfo *
299login_get_lastlog(struct logininfo *li, const int uid)
300{
andre6bb92372000-06-19 08:20:03 +0000301 struct passwd *pw;
andre6bb92372000-06-19 08:20:03 +0000302
Damien Miller348c9b72000-08-15 10:01:22 +1000303 memset(li, '\0', sizeof(*li));
andre61e67252000-06-04 17:07:49 +0000304 li->uid = uid;
andre6bb92372000-06-19 08:20:03 +0000305
Kevin Stevesef4eea92001-02-05 12:42:17 +0000306 /*
Damien Miller53c5d462000-06-28 00:50:50 +1000307 * If we don't have a 'real' lastlog, we need the username to
andre6bb92372000-06-19 08:20:03 +0000308 * reliably search wtmp(x) for the last login (see
Kevin Stevesef4eea92001-02-05 12:42:17 +0000309 * wtmp_get_entry().)
Damien Miller53c5d462000-06-28 00:50:50 +1000310 */
andre6bb92372000-06-19 08:20:03 +0000311 pw = getpwuid(uid);
Damien Millerdd47aa22000-06-27 11:18:27 +1000312 if (pw == NULL)
Damien Miller6b0279c2004-09-12 15:25:17 +1000313 fatal("%s: Cannot find account for uid %i", __func__, uid);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000314
andre98cabe02000-06-19 09:11:30 +0000315 /* No MIN_SIZEOF here - we absolutely *must not* truncate the
Damien Miller8899ed32004-09-12 15:18:55 +1000316 * username (XXX - so check for trunc!) */
Damien Millerf8af08d2000-06-27 09:40:06 +1000317 strlcpy(li->username, pw->pw_name, sizeof(li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000318
andre61e67252000-06-04 17:07:49 +0000319 if (getlast_entry(li))
Damien Miller8899ed32004-09-12 15:18:55 +1000320 return (li);
andre61e67252000-06-04 17:07:49 +0000321 else
Damien Miller8899ed32004-09-12 15:18:55 +1000322 return (NULL);
andre61e67252000-06-04 17:07:49 +0000323}
324
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
337logininfo *login_alloc_entry(int 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{
352 xfree(li);
353}
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
Kevin Stevesef4eea92001-02-05 12:42:17 +0000365login_init_entry(struct logininfo *li, int 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)
470 audit_session_open(li->line);
471 else if (li->type == LTYPE_LOGOUT)
472 audit_session_close(li->line);
473#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 */
andre61e67252000-06-04 17:07:49 +0000511
Damien Miller8899ed32004-09-12 15:18:55 +1000512#if defined(DISABLE_LASTLOG)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000513 /* On some systems we shouldn't even try to obtain last login
andreecaabf12000-06-12 22:21:44 +0000514 * time, e.g. AIX */
Damien Miller8899ed32004-09-12 15:18:55 +1000515 return (0);
516# elif defined(USE_WTMP) && \
517 (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
andre61e67252000-06-04 17:07:49 +0000518 /* retrieve last login time from utmp */
Damien Millerdd47aa22000-06-27 11:18:27 +1000519 return (wtmp_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000520# elif defined(USE_WTMPX) && \
521 (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
andre61e67252000-06-04 17:07:49 +0000522 /* If wtmp isn't available, try wtmpx */
Damien Millerdd47aa22000-06-27 11:18:27 +1000523 return (wtmpx_get_entry(li));
Damien Miller8899ed32004-09-12 15:18:55 +1000524# else
andre61e67252000-06-04 17:07:49 +0000525 /* Give up: No means of retrieving last login time */
Damien Miller8899ed32004-09-12 15:18:55 +1000526 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000527# endif /* DISABLE_LASTLOG */
Damien Millerdd47aa22000-06-27 11:18:27 +1000528#endif /* USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000529}
530
531
532
andre2ff7b5d2000-06-03 14:57:40 +0000533/*
andre61e67252000-06-04 17:07:49 +0000534 * 'line' string utility functions
535 *
536 * These functions process the 'line' string into one of three forms:
537 *
andre2ff7b5d2000-06-03 14:57:40 +0000538 * 1. The full filename (including '/dev')
539 * 2. The stripped name (excluding '/dev')
andre61e67252000-06-04 17:07:49 +0000540 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
541 * /dev/pts/1 -> ts/1 )
andre2ff7b5d2000-06-03 14:57:40 +0000542 *
543 * Form 3 is used on some systems to identify a .tmp.? entry when
544 * attempting to remove it. Typically both addition and removal is
andre61e67252000-06-04 17:07:49 +0000545 * performed by one application - say, sshd - so as long as the choice
546 * uniquely identifies a terminal it's ok.
andre2ff7b5d2000-06-03 14:57:40 +0000547 */
548
549
Damien Miller8899ed32004-09-12 15:18:55 +1000550/*
551 * line_fullname(): add the leading '/dev/' if it doesn't exist make
552 * sure dst has enough space, if not just copy src (ugh)
553 */
andre2ff7b5d2000-06-03 14:57:40 +0000554char *
Damien Miller52c8afe2005-06-19 10:19:43 +1000555line_fullname(char *dst, const char *src, u_int dstsize)
andre61e67252000-06-04 17:07:49 +0000556{
andre2ff7b5d2000-06-03 14:57:40 +0000557 memset(dst, '\0', dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000558 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
andre2ff7b5d2000-06-03 14:57:40 +0000559 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000560 else {
Damien Miller1a132252000-06-13 21:23:17 +1000561 strlcpy(dst, "/dev/", dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000562 strlcat(dst, src, dstsize);
563 }
Damien Miller8899ed32004-09-12 15:18:55 +1000564 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000565}
566
andre61e67252000-06-04 17:07:49 +0000567/* line_stripname(): strip the leading '/dev' if it exists, return dst */
andre2ff7b5d2000-06-03 14:57:40 +0000568char *
andre61e67252000-06-04 17:07:49 +0000569line_stripname(char *dst, const char *src, int dstsize)
570{
andre2ff7b5d2000-06-03 14:57:40 +0000571 memset(dst, '\0', dstsize);
572 if (strncmp(src, "/dev/", 5) == 0)
Damien Millerf5a81472000-09-30 21:34:44 +1100573 strlcpy(dst, src + 5, dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000574 else
575 strlcpy(dst, src, dstsize);
Damien Miller8899ed32004-09-12 15:18:55 +1000576 return (dst);
andre61e67252000-06-04 17:07:49 +0000577}
578
Damien Miller94cf4c82005-07-17 17:04:47 +1000579/*
Damien Miller8899ed32004-09-12 15:18:55 +1000580 * line_abbrevname(): Return the abbreviated (usually four-character)
andre61e67252000-06-04 17:07:49 +0000581 * form of the line (Just use the last <dstsize> characters of the
582 * full name.)
583 *
584 * NOTE: use strncpy because we do NOT necessarily want zero
Damien Miller8899ed32004-09-12 15:18:55 +1000585 * termination
586 */
andre2ff7b5d2000-06-03 14:57:40 +0000587char *
Kevin Stevesef4eea92001-02-05 12:42:17 +0000588line_abbrevname(char *dst, const char *src, int dstsize)
Damien Millerdd47aa22000-06-27 11:18:27 +1000589{
590 size_t len;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000591
andre2ff7b5d2000-06-03 14:57:40 +0000592 memset(dst, '\0', dstsize);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000593
Damien Miller8e81ed32000-07-01 13:17:42 +1000594 /* Always skip prefix if present */
595 if (strncmp(src, "/dev/", 5) == 0)
596 src += 5;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000597
Damien Millerf1b9d112002-04-23 23:09:19 +1000598#ifdef WITH_ABBREV_NO_TTY
599 if (strncmp(src, "tty", 3) == 0)
600 src += 3;
601#endif
602
Damien Millerdd47aa22000-06-27 11:18:27 +1000603 len = strlen(src);
604
Damien Miller8e81ed32000-07-01 13:17:42 +1000605 if (len > 0) {
606 if (((int)len - dstsize) > 0)
607 src += ((int)len - dstsize);
608
609 /* note: _don't_ change this to strlcpy */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000610 strncpy(dst, src, (size_t)dstsize);
Damien Millerdd47aa22000-06-27 11:18:27 +1000611 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000612
Damien Miller8899ed32004-09-12 15:18:55 +1000613 return (dst);
andre2ff7b5d2000-06-03 14:57:40 +0000614}
615
andre2ff7b5d2000-06-03 14:57:40 +0000616/**
617 ** utmp utility functions
andre61e67252000-06-04 17:07:49 +0000618 **
619 ** These functions manipulate struct utmp, taking system differences
620 ** into account.
andre2ff7b5d2000-06-03 14:57:40 +0000621 **/
622
623#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
624
andre2ff7b5d2000-06-03 14:57:40 +0000625/* build the utmp structure */
626void
andre61e67252000-06-04 17:07:49 +0000627set_utmp_time(struct logininfo *li, struct utmp *ut)
628{
Damien Miller8899ed32004-09-12 15:18:55 +1000629# if defined(HAVE_TV_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000630 ut->ut_tv.tv_sec = li->tv_sec;
631 ut->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000632# elif defined(HAVE_TIME_IN_UTMP)
andre2ff7b5d2000-06-03 14:57:40 +0000633 ut->ut_time = li->tv_sec;
Damien Millerdd47aa22000-06-27 11:18:27 +1000634# endif
andre2ff7b5d2000-06-03 14:57:40 +0000635}
636
637void
638construct_utmp(struct logininfo *li,
andre61e67252000-06-04 17:07:49 +0000639 struct utmp *ut)
640{
Damien Miller02e16ad2003-01-03 14:42:27 +1100641# ifdef HAVE_ADDR_V6_IN_UTMP
642 struct sockaddr_in6 *sa6;
Damien Miller8899ed32004-09-12 15:18:55 +1000643# endif
644
Damien Miller348c9b72000-08-15 10:01:22 +1000645 memset(ut, '\0', sizeof(*ut));
andre6bb92372000-06-19 08:20:03 +0000646
647 /* First fill out fields used for both logins and logouts */
648
Damien Millerdd47aa22000-06-27 11:18:27 +1000649# ifdef HAVE_ID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000650 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +1000651# endif
andre2ff7b5d2000-06-03 14:57:40 +0000652
Damien Millerdd47aa22000-06-27 11:18:27 +1000653# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +0000654 /* This is done here to keep utmp constants out of struct logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000655 switch (li->type) {
656 case LTYPE_LOGIN:
657 ut->ut_type = USER_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700658#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000659 cray_set_tmpdir(ut);
660#endif
andre2ff7b5d2000-06-03 14:57:40 +0000661 break;
662 case LTYPE_LOGOUT:
663 ut->ut_type = DEAD_PROCESS;
Tim Rice81ed5182002-09-25 17:38:46 -0700664#ifdef _UNICOS
Ben Lindstrom6db66ff2001-08-06 23:29:16 +0000665 cray_retain_utmp(ut, li->pid);
666#endif
andre2ff7b5d2000-06-03 14:57:40 +0000667 break;
668 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000669# endif
andre6bb92372000-06-19 08:20:03 +0000670 set_utmp_time(li, ut);
andre2ff7b5d2000-06-03 14:57:40 +0000671
andre6bb92372000-06-19 08:20:03 +0000672 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +1000673
674# ifdef HAVE_PID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000675 ut->ut_pid = li->pid;
Damien Millerdd47aa22000-06-27 11:18:27 +1000676# endif
andre6bb92372000-06-19 08:20:03 +0000677
678 /* If we're logging out, leave all other fields blank */
679 if (li->type == LTYPE_LOGOUT)
Damien Miller8899ed32004-09-12 15:18:55 +1000680 return;
andre6bb92372000-06-19 08:20:03 +0000681
Damien Millerdd47aa22000-06-27 11:18:27 +1000682 /*
683 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000684 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000685 */
andre6bb92372000-06-19 08:20:03 +0000686
687 /* Use strncpy because we don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000688 strncpy(ut->ut_name, li->username,
689 MIN_SIZEOF(ut->ut_name, li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000690# ifdef HAVE_HOST_IN_UTMP
Damien Miller8899ed32004-09-12 15:18:55 +1000691 strncpy(ut->ut_host, li->hostname,
692 MIN_SIZEOF(ut->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000693# endif
694# ifdef HAVE_ADDR_IN_UTMP
andre61e67252000-06-04 17:07:49 +0000695 /* this is just a 32-bit IP address */
696 if (li->hostaddr.sa.sa_family == AF_INET)
697 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000698# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100699# ifdef HAVE_ADDR_V6_IN_UTMP
700 /* this is just a 128-bit IPv6 address */
701 if (li->hostaddr.sa.sa_family == AF_INET6) {
702 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
703 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
704 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
705 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
706 ut->ut_addr_v6[1] = 0;
707 ut->ut_addr_v6[2] = 0;
708 ut->ut_addr_v6[3] = 0;
709 }
710 }
711# endif
andre61e67252000-06-04 17:07:49 +0000712}
Damien Millerdd47aa22000-06-27 11:18:27 +1000713#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
andre61e67252000-06-04 17:07:49 +0000714
andre2ff7b5d2000-06-03 14:57:40 +0000715/**
716 ** utmpx utility functions
andre61e67252000-06-04 17:07:49 +0000717 **
718 ** These functions manipulate struct utmpx, accounting for system
719 ** variations.
andre2ff7b5d2000-06-03 14:57:40 +0000720 **/
721
722#if defined(USE_UTMPX) || defined (USE_WTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000723/* build the utmpx structure */
724void
andre61e67252000-06-04 17:07:49 +0000725set_utmpx_time(struct logininfo *li, struct utmpx *utx)
726{
Damien Miller8899ed32004-09-12 15:18:55 +1000727# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000728 utx->ut_tv.tv_sec = li->tv_sec;
729 utx->ut_tv.tv_usec = li->tv_usec;
Damien Miller8899ed32004-09-12 15:18:55 +1000730# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000731 utx->ut_time = li->tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +1000732# endif
andre2ff7b5d2000-06-03 14:57:40 +0000733}
734
andre61e67252000-06-04 17:07:49 +0000735void
736construct_utmpx(struct logininfo *li, struct utmpx *utx)
737{
Damien Miller02e16ad2003-01-03 14:42:27 +1100738# ifdef HAVE_ADDR_V6_IN_UTMP
739 struct sockaddr_in6 *sa6;
740# endif
Damien Miller348c9b72000-08-15 10:01:22 +1000741 memset(utx, '\0', sizeof(*utx));
Damien Miller8899ed32004-09-12 15:18:55 +1000742
Damien Miller8e81ed32000-07-01 13:17:42 +1000743# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000744 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
Damien Miller8e81ed32000-07-01 13:17:42 +1000745# endif
andre2ff7b5d2000-06-03 14:57:40 +0000746
747 /* this is done here to keep utmp constants out of loginrec.h */
748 switch (li->type) {
749 case LTYPE_LOGIN:
750 utx->ut_type = USER_PROCESS;
751 break;
752 case LTYPE_LOGOUT:
753 utx->ut_type = DEAD_PROCESS;
754 break;
755 }
andre2ff7b5d2000-06-03 14:57:40 +0000756 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
andre2ff7b5d2000-06-03 14:57:40 +0000757 set_utmpx_time(li, utx);
andre6bb92372000-06-19 08:20:03 +0000758 utx->ut_pid = li->pid;
Damien Miller8899ed32004-09-12 15:18:55 +1000759
Tim Ricee06ae4a2002-02-24 17:56:46 -0800760 /* strncpy(): Don't necessarily want null termination */
Damien Miller8899ed32004-09-12 15:18:55 +1000761 strncpy(utx->ut_name, li->username,
762 MIN_SIZEOF(utx->ut_name, li->username));
andre6bb92372000-06-19 08:20:03 +0000763
764 if (li->type == LTYPE_LOGOUT)
765 return;
766
Damien Millerdd47aa22000-06-27 11:18:27 +1000767 /*
768 * These fields are only used when logging in, and are blank
Kevin Stevesef4eea92001-02-05 12:42:17 +0000769 * for logouts.
Damien Millerdd47aa22000-06-27 11:18:27 +1000770 */
andre6bb92372000-06-19 08:20:03 +0000771
Damien Millerdd47aa22000-06-27 11:18:27 +1000772# ifdef HAVE_HOST_IN_UTMPX
Damien Miller8899ed32004-09-12 15:18:55 +1000773 strncpy(utx->ut_host, li->hostname,
774 MIN_SIZEOF(utx->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000775# endif
776# ifdef HAVE_ADDR_IN_UTMPX
Damien Millerd6f204d2000-09-23 13:57:27 +1100777 /* this is just a 32-bit IP address */
778 if (li->hostaddr.sa.sa_family == AF_INET)
779 utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Damien Millerdd47aa22000-06-27 11:18:27 +1000780# endif
Damien Miller02e16ad2003-01-03 14:42:27 +1100781# ifdef HAVE_ADDR_V6_IN_UTMP
782 /* this is just a 128-bit IPv6 address */
783 if (li->hostaddr.sa.sa_family == AF_INET6) {
784 sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa);
785 memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16);
786 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
787 ut->ut_addr_v6[0] = ut->ut_addr_v6[3];
788 ut->ut_addr_v6[1] = 0;
789 ut->ut_addr_v6[2] = 0;
790 ut->ut_addr_v6[3] = 0;
791 }
792 }
793# endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000794# ifdef HAVE_SYSLEN_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +0000795 /* ut_syslen is the length of the utx_host string */
796 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +1000797# endif
andre61e67252000-06-04 17:07:49 +0000798}
Damien Millerdd47aa22000-06-27 11:18:27 +1000799#endif /* USE_UTMPX || USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +0000800
801/**
andre61e67252000-06-04 17:07:49 +0000802 ** Low-level utmp functions
andre2ff7b5d2000-06-03 14:57:40 +0000803 **/
804
805/* FIXME: (ATL) utmp_write_direct needs testing */
andre2ff7b5d2000-06-03 14:57:40 +0000806#ifdef USE_UTMP
807
andre2ff7b5d2000-06-03 14:57:40 +0000808/* if we can, use pututline() etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000809# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
810 defined(HAVE_PUTUTLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000811# define UTMP_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000812# endif
andre2ff7b5d2000-06-03 14:57:40 +0000813
814
815/* write a utmp entry with the system's help (pututline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000816# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000817static int
andre61e67252000-06-04 17:07:49 +0000818utmp_write_library(struct logininfo *li, struct utmp *ut)
819{
andre2ff7b5d2000-06-03 14:57:40 +0000820 setutent();
821 pututline(ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000822# ifdef HAVE_ENDUTENT
andre2ff7b5d2000-06-03 14:57:40 +0000823 endutent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000824# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000825 return (1);
andre61e67252000-06-04 17:07:49 +0000826}
Damien Millerdd47aa22000-06-27 11:18:27 +1000827# else /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000828
Damien Miller94cf4c82005-07-17 17:04:47 +1000829/*
Damien Miller8899ed32004-09-12 15:18:55 +1000830 * Write a utmp entry direct to the file
831 * This is a slightly modification of code in OpenBSD's login.c
832 */
andre2ff7b5d2000-06-03 14:57:40 +0000833static int
andre61e67252000-06-04 17:07:49 +0000834utmp_write_direct(struct logininfo *li, struct utmp *ut)
835{
andre2ff7b5d2000-06-03 14:57:40 +0000836 struct utmp old_ut;
837 register int fd;
838 int tty;
839
andre6bb92372000-06-19 08:20:03 +0000840 /* FIXME: (ATL) ttyslot() needs local implementation */
Damien Miller348c9b72000-08-15 10:01:22 +1000841
Damien Millere5192fa2000-08-29 14:30:37 +1100842#if defined(HAVE_GETTTYENT)
Damien Miller8899ed32004-09-12 15:18:55 +1000843 struct ttyent *ty;
Damien Miller348c9b72000-08-15 10:01:22 +1000844
845 tty=0;
Damien Miller348c9b72000-08-15 10:01:22 +1000846 setttyent();
Damien Miller8899ed32004-09-12 15:18:55 +1000847 while (NULL != (ty = getttyent())) {
Damien Miller348c9b72000-08-15 10:01:22 +1000848 tty++;
849 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
850 break;
851 }
852 endttyent();
853
Damien Miller8899ed32004-09-12 15:18:55 +1000854 if (NULL == ty) {
Damien Miller81409592004-08-15 19:12:52 +1000855 logit("%s: tty not found", __func__);
856 return (0);
Damien Miller348c9b72000-08-15 10:01:22 +1000857 }
858#else /* FIXME */
859
andre2ff7b5d2000-06-03 14:57:40 +0000860 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
861
Damien Millere5192fa2000-08-29 14:30:37 +1100862#endif /* HAVE_GETTTYENT */
Damien Miller348c9b72000-08-15 10:01:22 +1000863
andre2ff7b5d2000-06-03 14:57:40 +0000864 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
Damien Miller81409592004-08-15 19:12:52 +1000865 off_t pos, ret;
866
867 pos = (off_t)tty * sizeof(struct utmp);
868 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000869 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000870 return (0);
871 }
872 if (ret != pos) {
Damien Miller94cf4c82005-07-17 17:04:47 +1000873 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Millerb0419f22004-08-23 21:53:28 +1000874 __func__, tty, UTMP_FILE);
Damien Miller81409592004-08-15 19:12:52 +1000875 return (0);
876 }
andre2ff7b5d2000-06-03 14:57:40 +0000877 /*
878 * Prevent luser from zero'ing out ut_host.
879 * If the new ut_line is empty but the old one is not
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000880 * and ut_line and ut_name match, preserve the old ut_line.
andre2ff7b5d2000-06-03 14:57:40 +0000881 */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000882 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
Damien Miller8899ed32004-09-12 15:18:55 +1000883 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
884 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
885 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0))
886 memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000887
Damien Miller81409592004-08-15 19:12:52 +1000888 if ((ret = lseek(fd, pos, SEEK_SET)) == -1) {
Damien Millerb0419f22004-08-23 21:53:28 +1000889 logit("%s: lseek: %s", __func__, strerror(errno));
Damien Miller81409592004-08-15 19:12:52 +1000890 return (0);
891 }
892 if (ret != pos) {
Damien Millerb0419f22004-08-23 21:53:28 +1000893 logit("%s: Couldn't seek to tty %d slot in %s",
Damien Miller81409592004-08-15 19:12:52 +1000894 __func__, tty, UTMP_FILE);
895 return (0);
896 }
Damien Miller8899ed32004-09-12 15:18:55 +1000897 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
Damien Miller81409592004-08-15 19:12:52 +1000898 logit("%s: error writing %s: %s", __func__,
andre6bb92372000-06-19 08:20:03 +0000899 UTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +1000900 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000901
Damien Miller8899ed32004-09-12 15:18:55 +1000902 close(fd);
903 return (1);
Damien Miller53c5d462000-06-28 00:50:50 +1000904 } else {
Damien Miller8899ed32004-09-12 15:18:55 +1000905 return (0);
Damien Miller53c5d462000-06-28 00:50:50 +1000906 }
andre61e67252000-06-04 17:07:49 +0000907}
Damien Millerdd47aa22000-06-27 11:18:27 +1000908# endif /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000909
910static int
andre61e67252000-06-04 17:07:49 +0000911utmp_perform_login(struct logininfo *li)
912{
andre2ff7b5d2000-06-03 14:57:40 +0000913 struct utmp ut;
914
915 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000916# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000917 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000918 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000919 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000920 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000921# else
andre2ff7b5d2000-06-03 14:57:40 +0000922 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000923 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000924 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000925 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000926# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000927 return (1);
andre61e67252000-06-04 17:07:49 +0000928}
andre2ff7b5d2000-06-03 14:57:40 +0000929
930
931static int
andre61e67252000-06-04 17:07:49 +0000932utmp_perform_logout(struct logininfo *li)
933{
andre2ff7b5d2000-06-03 14:57:40 +0000934 struct utmp ut;
935
andre6bb92372000-06-19 08:20:03 +0000936 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000937# ifdef UTMP_USE_LIBRARY
andre6bb92372000-06-19 08:20:03 +0000938 if (!utmp_write_library(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000939 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000940 return (0);
andre6bb92372000-06-19 08:20:03 +0000941 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000942# else
andre6bb92372000-06-19 08:20:03 +0000943 if (!utmp_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +1000944 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000945 return (0);
andre6bb92372000-06-19 08:20:03 +0000946 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000947# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000948 return (1);
andre61e67252000-06-04 17:07:49 +0000949}
andre2ff7b5d2000-06-03 14:57:40 +0000950
951
952int
andre61e67252000-06-04 17:07:49 +0000953utmp_write_entry(struct logininfo *li)
954{
andre2ff7b5d2000-06-03 14:57:40 +0000955 switch(li->type) {
956 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +1000957 return (utmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +0000958
959 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +1000960 return (utmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +0000961
962 default:
Damien Miller6b0279c2004-09-12 15:25:17 +1000963 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +1000964 return (0);
andre2ff7b5d2000-06-03 14:57:40 +0000965 }
andre61e67252000-06-04 17:07:49 +0000966}
Damien Millerdd47aa22000-06-27 11:18:27 +1000967#endif /* USE_UTMP */
andre2ff7b5d2000-06-03 14:57:40 +0000968
969
970/**
andre61e67252000-06-04 17:07:49 +0000971 ** Low-level utmpx functions
andre2ff7b5d2000-06-03 14:57:40 +0000972 **/
973
974/* not much point if we don't want utmpx entries */
975#ifdef USE_UTMPX
976
andre2ff7b5d2000-06-03 14:57:40 +0000977/* if we have the wherewithall, use pututxline etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000978# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
979 defined(HAVE_PUTUTXLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000980# define UTMPX_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000981# endif
andre2ff7b5d2000-06-03 14:57:40 +0000982
983
984/* write a utmpx entry with the system's help (pututxline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000985# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000986static int
andre61e67252000-06-04 17:07:49 +0000987utmpx_write_library(struct logininfo *li, struct utmpx *utx)
988{
andre2ff7b5d2000-06-03 14:57:40 +0000989 setutxent();
990 pututxline(utx);
991
Damien Millerdd47aa22000-06-27 11:18:27 +1000992# ifdef HAVE_ENDUTXENT
andre2ff7b5d2000-06-03 14:57:40 +0000993 endutxent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000994# endif
Damien Miller8899ed32004-09-12 15:18:55 +1000995 return (1);
andre61e67252000-06-04 17:07:49 +0000996}
andre2ff7b5d2000-06-03 14:57:40 +0000997
Damien Millerdd47aa22000-06-27 11:18:27 +1000998# else /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000999
1000/* write a utmp entry direct to the file */
1001static int
andre61e67252000-06-04 17:07:49 +00001002utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
Kevin Stevesef4eea92001-02-05 12:42:17 +00001003{
Damien Miller6b0279c2004-09-12 15:25:17 +10001004 logit("%s: not implemented!", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001005 return (0);
andre61e67252000-06-04 17:07:49 +00001006}
Damien Millerdd47aa22000-06-27 11:18:27 +10001007# endif /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +00001008
1009static int
andre61e67252000-06-04 17:07:49 +00001010utmpx_perform_login(struct logininfo *li)
1011{
andre2ff7b5d2000-06-03 14:57:40 +00001012 struct utmpx utx;
1013
1014 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001015# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001016 if (!utmpx_write_library(li, &utx)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001017 logit("%s: utmp_write_library() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001018 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001019 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001020# else
andre2ff7b5d2000-06-03 14:57:40 +00001021 if (!utmpx_write_direct(li, &ut)) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001022 logit("%s: utmp_write_direct() failed", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001023 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001024 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001025# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001026 return (1);
andre61e67252000-06-04 17:07:49 +00001027}
andre2ff7b5d2000-06-03 14:57:40 +00001028
1029
1030static int
andre61e67252000-06-04 17:07:49 +00001031utmpx_perform_logout(struct logininfo *li)
1032{
andre2ff7b5d2000-06-03 14:57:40 +00001033 struct utmpx utx;
1034
Tim Ricee06ae4a2002-02-24 17:56:46 -08001035 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001036# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001037 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +10001038# endif
1039# ifdef HAVE_TYPE_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001040 utx.ut_type = DEAD_PROCESS;
Damien Millerdd47aa22000-06-27 11:18:27 +10001041# endif
andre2ff7b5d2000-06-03 14:57:40 +00001042
Damien Millerdd47aa22000-06-27 11:18:27 +10001043# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +00001044 utmpx_write_library(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001045# else
andre2ff7b5d2000-06-03 14:57:40 +00001046 utmpx_write_direct(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +10001047# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001048 return (1);
andre61e67252000-06-04 17:07:49 +00001049}
andre2ff7b5d2000-06-03 14:57:40 +00001050
andre2ff7b5d2000-06-03 14:57:40 +00001051int
andre61e67252000-06-04 17:07:49 +00001052utmpx_write_entry(struct logininfo *li)
1053{
andre2ff7b5d2000-06-03 14:57:40 +00001054 switch(li->type) {
1055 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001056 return (utmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001057 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001058 return (utmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001059 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001060 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001061 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001062 }
andre61e67252000-06-04 17:07:49 +00001063}
Damien Millerdd47aa22000-06-27 11:18:27 +10001064#endif /* USE_UTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001065
1066
1067/**
andre61e67252000-06-04 17:07:49 +00001068 ** Low-level wtmp functions
andre2ff7b5d2000-06-03 14:57:40 +00001069 **/
1070
Kevin Stevesef4eea92001-02-05 12:42:17 +00001071#ifdef USE_WTMP
andre2ff7b5d2000-06-03 14:57:40 +00001072
Damien Miller94cf4c82005-07-17 17:04:47 +10001073/*
Damien Miller8899ed32004-09-12 15:18:55 +10001074 * Write a wtmp entry direct to the end of the file
1075 * This is a slight modification of code in OpenBSD's logwtmp.c
1076 */
andre2ff7b5d2000-06-03 14:57:40 +00001077static int
andre61e67252000-06-04 17:07:49 +00001078wtmp_write(struct logininfo *li, struct utmp *ut)
1079{
andre2ff7b5d2000-06-03 14:57:40 +00001080 struct stat buf;
1081 int fd, ret = 1;
1082
1083 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001084 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001085 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001086 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001087 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001088 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001089 if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
andre2ff7b5d2000-06-03 14:57:40 +00001090 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001091 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001092 WTMP_FILE, strerror(errno));
1093 ret = 0;
1094 }
Damien Miller8899ed32004-09-12 15:18:55 +10001095 close(fd);
1096 return (ret);
andre61e67252000-06-04 17:07:49 +00001097}
andre2ff7b5d2000-06-03 14:57:40 +00001098
andre2ff7b5d2000-06-03 14:57:40 +00001099static int
Damien Millerdd47aa22000-06-27 11:18:27 +10001100wtmp_perform_login(struct logininfo *li)
1101{
andre2ff7b5d2000-06-03 14:57:40 +00001102 struct utmp ut;
1103
1104 construct_utmp(li, &ut);
Damien Miller8899ed32004-09-12 15:18:55 +10001105 return (wtmp_write(li, &ut));
andre61e67252000-06-04 17:07:49 +00001106}
andre2ff7b5d2000-06-03 14:57:40 +00001107
1108
1109static int
andre61e67252000-06-04 17:07:49 +00001110wtmp_perform_logout(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
1119int
andre61e67252000-06-04 17:07:49 +00001120wtmp_write_entry(struct logininfo *li)
1121{
andre2ff7b5d2000-06-03 14:57:40 +00001122 switch(li->type) {
1123 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001124 return (wtmp_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001125 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001126 return (wtmp_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001127 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001128 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001129 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001130 }
andre61e67252000-06-04 17:07:49 +00001131}
andre2ff7b5d2000-06-03 14:57:40 +00001132
1133
Damien Miller94cf4c82005-07-17 17:04:47 +10001134/*
Damien Miller8899ed32004-09-12 15:18:55 +10001135 * Notes on fetching login data from wtmp/wtmpx
Kevin Stevesef4eea92001-02-05 12:42:17 +00001136 *
andre6bb92372000-06-19 08:20:03 +00001137 * Logouts are usually recorded with (amongst other things) a blank
1138 * username on a given tty line. However, some systems (HP-UX is one)
1139 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1140 *
1141 * Since we're only looking for logins here, we know that the username
1142 * must be set correctly. On systems that leave it in, we check for
1143 * ut_type==USER_PROCESS (indicating a login.)
1144 *
1145 * Portability: Some systems may set something other than USER_PROCESS
1146 * to indicate a login process. I don't know of any as I write. Also,
1147 * it's possible that some systems may both leave the username in
1148 * place and not have ut_type.
1149 */
1150
andre6bb92372000-06-19 08:20:03 +00001151/* return true if this wtmp entry indicates a login */
1152static int
1153wtmp_islogin(struct logininfo *li, struct utmp *ut)
1154{
Kevin Stevesef4eea92001-02-05 12:42:17 +00001155 if (strncmp(li->username, ut->ut_name,
Damien Miller8899ed32004-09-12 15:18:55 +10001156 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001157# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001158 if (ut->ut_type & USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001159 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001160# else
Damien Miller8899ed32004-09-12 15:18:55 +10001161 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001162# endif
andre6bb92372000-06-19 08:20:03 +00001163 }
Damien Miller8899ed32004-09-12 15:18:55 +10001164 return (0);
andre6bb92372000-06-19 08:20:03 +00001165}
1166
andre2ff7b5d2000-06-03 14:57:40 +00001167int
andre61e67252000-06-04 17:07:49 +00001168wtmp_get_entry(struct logininfo *li)
1169{
andre2ff7b5d2000-06-03 14:57:40 +00001170 struct stat st;
1171 struct utmp ut;
Damien Miller8899ed32004-09-12 15:18:55 +10001172 int fd, found = 0;
andre6bb92372000-06-19 08:20:03 +00001173
1174 /* Clear the time entries in our logininfo */
1175 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001176
1177 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001178 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001179 WTMP_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001180 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001181 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001182 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001183 logit("%s: couldn't stat %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001184 WTMP_FILE, strerror(errno));
1185 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001186 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001187 }
andre2ff7b5d2000-06-03 14:57:40 +00001188
andre6bb92372000-06-19 08:20:03 +00001189 /* Seek to the start of the last struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001190 if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) {
andre6bb92372000-06-19 08:20:03 +00001191 /* Looks like we've got a fresh wtmp file */
1192 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001193 return (0);
andre6bb92372000-06-19 08:20:03 +00001194 }
1195
1196 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001197 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001198 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001199 WTMP_FILE, strerror(errno));
1200 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001201 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001202 }
andre6bb92372000-06-19 08:20:03 +00001203 if ( wtmp_islogin(li, &ut) ) {
1204 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001205 /*
1206 * We've already checked for a time in struct
1207 * utmp, in login_getlast()
1208 */
Damien Millerdd47aa22000-06-27 11:18:27 +10001209# ifdef HAVE_TIME_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +00001210 li->tv_sec = ut.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001211# else
andre2ff7b5d2000-06-03 14:57:40 +00001212# if HAVE_TV_IN_UTMP
1213 li->tv_sec = ut.ut_tv.tv_sec;
1214# endif
Damien Millerdd47aa22000-06-27 11:18:27 +10001215# endif
andre6bb92372000-06-19 08:20:03 +00001216 line_fullname(li->line, ut.ut_line,
Damien Miller8899ed32004-09-12 15:18:55 +10001217 MIN_SIZEOF(li->line, ut.ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +10001218# ifdef HAVE_HOST_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001219 strlcpy(li->hostname, ut.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001220 MIN_SIZEOF(li->hostname, ut.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001221# endif
andre6bb92372000-06-19 08:20:03 +00001222 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001223 }
andre6bb92372000-06-19 08:20:03 +00001224 /* Seek back 2 x struct utmp */
Kevin Steves52172652001-10-02 00:29:00 +00001225 if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) {
andre6bb92372000-06-19 08:20:03 +00001226 /* We've found the start of the file, so quit */
Damien Miller8899ed32004-09-12 15:18:55 +10001227 close(fd);
1228 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001229 }
andre6bb92372000-06-19 08:20:03 +00001230 }
1231
1232 /* We found an entry. Tidy up and return */
1233 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001234 return (1);
andre61e67252000-06-04 17:07:49 +00001235}
Damien Millerdd47aa22000-06-27 11:18:27 +10001236# endif /* USE_WTMP */
andre2ff7b5d2000-06-03 14:57:40 +00001237
1238
1239/**
andre61e67252000-06-04 17:07:49 +00001240 ** Low-level wtmpx functions
andre2ff7b5d2000-06-03 14:57:40 +00001241 **/
1242
1243#ifdef USE_WTMPX
Damien Miller8899ed32004-09-12 15:18:55 +10001244/*
1245 * Write a wtmpx entry direct to the end of the file
1246 * This is a slight modification of code in OpenBSD's logwtmp.c
1247 */
andre2ff7b5d2000-06-03 14:57:40 +00001248static int
andre61e67252000-06-04 17:07:49 +00001249wtmpx_write(struct logininfo *li, struct utmpx *utx)
1250{
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001251#ifndef HAVE_UPDWTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001252 struct stat buf;
1253 int fd, ret = 1;
1254
1255 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001256 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001257 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001258 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001259 }
1260
Kevin Stevesef4eea92001-02-05 12:42:17 +00001261 if (fstat(fd, &buf) == 0)
Darren Tucker8661b562003-07-06 15:20:46 +10001262 if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
andre2ff7b5d2000-06-03 14:57:40 +00001263 ftruncate(fd, buf.st_size);
Damien Miller6b0279c2004-09-12 15:25:17 +10001264 logit("%s: problem writing %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001265 WTMPX_FILE, strerror(errno));
1266 ret = 0;
1267 }
Damien Miller8899ed32004-09-12 15:18:55 +10001268 close(fd);
andre2ff7b5d2000-06-03 14:57:40 +00001269
Damien Miller8899ed32004-09-12 15:18:55 +10001270 return (ret);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001271#else
1272 updwtmpx(WTMPX_FILE, utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001273 return (1);
Darren Tuckerc28b88a2004-02-10 16:49:35 +11001274#endif
andre61e67252000-06-04 17:07:49 +00001275}
andre2ff7b5d2000-06-03 14:57:40 +00001276
1277
1278static int
andre61e67252000-06-04 17:07:49 +00001279wtmpx_perform_login(struct logininfo *li)
1280{
andre2ff7b5d2000-06-03 14:57:40 +00001281 struct utmpx utx;
1282
1283 construct_utmpx(li, &utx);
Damien Miller8899ed32004-09-12 15:18:55 +10001284 return (wtmpx_write(li, &utx));
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_logout(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
1298int
andre61e67252000-06-04 17:07:49 +00001299wtmpx_write_entry(struct logininfo *li)
1300{
andre2ff7b5d2000-06-03 14:57:40 +00001301 switch(li->type) {
1302 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001303 return (wtmpx_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001304 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001305 return (wtmpx_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001306 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001307 logit("%s: invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001308 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001309 }
andre61e67252000-06-04 17:07:49 +00001310}
andre2ff7b5d2000-06-03 14:57:40 +00001311
andre6bb92372000-06-19 08:20:03 +00001312/* Please see the notes above wtmp_islogin() for information about the
1313 next two functions */
1314
1315/* Return true if this wtmpx entry indicates a login */
1316static int
1317wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1318{
Damien Miller8899ed32004-09-12 15:18:55 +10001319 if (strncmp(li->username, utx->ut_name,
1320 MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001321# ifdef HAVE_TYPE_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +00001322 if (utx->ut_type == USER_PROCESS)
Damien Miller8899ed32004-09-12 15:18:55 +10001323 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001324# else
Damien Miller8899ed32004-09-12 15:18:55 +10001325 return (1);
Damien Millerdd47aa22000-06-27 11:18:27 +10001326# endif
andre6bb92372000-06-19 08:20:03 +00001327 }
Damien Miller8899ed32004-09-12 15:18:55 +10001328 return (0);
andre6bb92372000-06-19 08:20:03 +00001329}
1330
andre2ff7b5d2000-06-03 14:57:40 +00001331
1332int
andre61e67252000-06-04 17:07:49 +00001333wtmpx_get_entry(struct logininfo *li)
1334{
andre2ff7b5d2000-06-03 14:57:40 +00001335 struct stat st;
1336 struct utmpx utx;
andre6bb92372000-06-19 08:20:03 +00001337 int fd, found=0;
1338
1339 /* Clear the time entries */
1340 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001341
1342 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001343 logit("%s: problem opening %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001344 WTMPX_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001345 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001346 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001347 if (fstat(fd, &st) != 0) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001348 logit("%s: couldn't stat %s: %s", __func__,
Tim Ricecdb82942002-07-14 15:33:20 -07001349 WTMPX_FILE, strerror(errno));
andre2ff7b5d2000-06-03 14:57:40 +00001350 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001351 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001352 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001353
andre6bb92372000-06-19 08:20:03 +00001354 /* Seek to the start of the last struct utmpx */
Kevin Steves52172652001-10-02 00:29:00 +00001355 if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) {
andre6bb92372000-06-19 08:20:03 +00001356 /* probably a newly rotated wtmpx file */
1357 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001358 return (0);
andre6bb92372000-06-19 08:20:03 +00001359 }
andre2ff7b5d2000-06-03 14:57:40 +00001360
andre6bb92372000-06-19 08:20:03 +00001361 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001362 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
Damien Miller94cf4c82005-07-17 17:04:47 +10001363 logit("%s: read of %s failed: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001364 WTMPX_FILE, strerror(errno));
1365 close (fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001366 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001367 }
Damien Miller8899ed32004-09-12 15:18:55 +10001368 /*
Damien Miller94cf4c82005-07-17 17:04:47 +10001369 * Logouts are recorded as a blank username on a particular
Damien Miller8899ed32004-09-12 15:18:55 +10001370 * line. So, we just need to find the username in struct utmpx
1371 */
1372 if (wtmpx_islogin(li, &utx)) {
Tim Rice370e0ba2002-07-14 15:50:51 -07001373 found = 1;
Damien Miller8899ed32004-09-12 15:18:55 +10001374# if defined(HAVE_TV_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001375 li->tv_sec = utx.ut_tv.tv_sec;
Damien Miller8899ed32004-09-12 15:18:55 +10001376# elif defined(HAVE_TIME_IN_UTMPX)
andre2ff7b5d2000-06-03 14:57:40 +00001377 li->tv_sec = utx.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001378# endif
Damien Miller1a132252000-06-13 21:23:17 +10001379 line_fullname(li->line, utx.ut_line, sizeof(li->line));
Damien Miller8899ed32004-09-12 15:18:55 +10001380# if defined(HAVE_HOST_IN_UTMPX)
andre6bb92372000-06-19 08:20:03 +00001381 strlcpy(li->hostname, utx.ut_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001382 MIN_SIZEOF(li->hostname, utx.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001383# endif
andre6bb92372000-06-19 08:20:03 +00001384 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001385 }
Kevin Steves52172652001-10-02 00:29:00 +00001386 if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) {
Damien Miller8899ed32004-09-12 15:18:55 +10001387 close(fd);
1388 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001389 }
andre6bb92372000-06-19 08:20:03 +00001390 }
1391
1392 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001393 return (1);
andre61e67252000-06-04 17:07:49 +00001394}
Damien Millerd5bf3072000-06-07 21:32:13 +10001395#endif /* USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001396
andre2ff7b5d2000-06-03 14:57:40 +00001397/**
andre61e67252000-06-04 17:07:49 +00001398 ** Low-level libutil login() functions
andre2ff7b5d2000-06-03 14:57:40 +00001399 **/
1400
1401#ifdef USE_LOGIN
andre2ff7b5d2000-06-03 14:57:40 +00001402static int
andre61e67252000-06-04 17:07:49 +00001403syslogin_perform_login(struct logininfo *li)
1404{
andre2ff7b5d2000-06-03 14:57:40 +00001405 struct utmp *ut;
1406
Damien Millerb0aae332004-09-12 15:26:00 +10001407 ut = xmalloc(sizeof(*ut));
andre2ff7b5d2000-06-03 14:57:40 +00001408 construct_utmp(li, ut);
1409 login(ut);
Damien Millerf211efc2003-03-10 11:23:06 +11001410 free(ut);
andre2ff7b5d2000-06-03 14:57:40 +00001411
Damien Miller8899ed32004-09-12 15:18:55 +10001412 return (1);
andre61e67252000-06-04 17:07:49 +00001413}
1414
andre2ff7b5d2000-06-03 14:57:40 +00001415static int
andre61e67252000-06-04 17:07:49 +00001416syslogin_perform_logout(struct logininfo *li)
1417{
Damien Millerdd47aa22000-06-27 11:18:27 +10001418# ifdef HAVE_LOGOUT
Darren Tucker4d2f3612004-04-08 10:57:05 +10001419 char line[UT_LINESIZE];
Kevin Stevesef4eea92001-02-05 12:42:17 +00001420
andre2ff7b5d2000-06-03 14:57:40 +00001421 (void)line_stripname(line, li->line, sizeof(line));
1422
Damien Miller8899ed32004-09-12 15:18:55 +10001423 if (!logout(line))
Damien Miller6b0279c2004-09-12 15:25:17 +10001424 logit("%s: logout() returned an error", __func__);
Damien Millerdd47aa22000-06-27 11:18:27 +10001425# ifdef HAVE_LOGWTMP
Damien Miller8899ed32004-09-12 15:18:55 +10001426 else
andre2ff7b5d2000-06-03 14:57:40 +00001427 logwtmp(line, "", "");
Damien Millerdd47aa22000-06-27 11:18:27 +10001428# endif
andre6bb92372000-06-19 08:20:03 +00001429 /* FIXME: (ATL - if the need arises) What to do if we have
1430 * login, but no logout? what if logout but no logwtmp? All
1431 * routines are in libutil so they should all be there,
1432 * but... */
Damien Millerdd47aa22000-06-27 11:18:27 +10001433# endif
Damien Miller8899ed32004-09-12 15:18:55 +10001434 return (1);
andre61e67252000-06-04 17:07:49 +00001435}
andre2ff7b5d2000-06-03 14:57:40 +00001436
andre2ff7b5d2000-06-03 14:57:40 +00001437int
andre61e67252000-06-04 17:07:49 +00001438syslogin_write_entry(struct logininfo *li)
1439{
andre2ff7b5d2000-06-03 14:57:40 +00001440 switch (li->type) {
1441 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001442 return (syslogin_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001443 case LTYPE_LOGOUT:
Damien Miller8899ed32004-09-12 15:18:55 +10001444 return (syslogin_perform_logout(li));
andre2ff7b5d2000-06-03 14:57:40 +00001445 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001446 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001447 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001448 }
andre61e67252000-06-04 17:07:49 +00001449}
Damien Millerd5bf3072000-06-07 21:32:13 +10001450#endif /* USE_LOGIN */
andre2ff7b5d2000-06-03 14:57:40 +00001451
1452/* end of file log-syslogin.c */
1453
andre2ff7b5d2000-06-03 14:57:40 +00001454/**
andre61e67252000-06-04 17:07:49 +00001455 ** Low-level lastlog functions
andre2ff7b5d2000-06-03 14:57:40 +00001456 **/
1457
1458#ifdef USE_LASTLOG
Damien Millerdd47aa22000-06-27 11:18:27 +10001459#define LL_FILE 1
1460#define LL_DIR 2
1461#define LL_OTHER 3
andre2ff7b5d2000-06-03 14:57:40 +00001462
andre2ff7b5d2000-06-03 14:57:40 +00001463static void
andre61e67252000-06-04 17:07:49 +00001464lastlog_construct(struct logininfo *li, struct lastlog *last)
1465{
andre2ff7b5d2000-06-03 14:57:40 +00001466 /* clear the structure */
Damien Miller348c9b72000-08-15 10:01:22 +10001467 memset(last, '\0', sizeof(*last));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001468
Damien Miller8899ed32004-09-12 15:18:55 +10001469 line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
andre6bb92372000-06-19 08:20:03 +00001470 strlcpy(last->ll_host, li->hostname,
1471 MIN_SIZEOF(last->ll_host, li->hostname));
andre2ff7b5d2000-06-03 14:57:40 +00001472 last->ll_time = li->tv_sec;
andre61e67252000-06-04 17:07:49 +00001473}
andre2ff7b5d2000-06-03 14:57:40 +00001474
andre2ff7b5d2000-06-03 14:57:40 +00001475static int
andre61e67252000-06-04 17:07:49 +00001476lastlog_filetype(char *filename)
1477{
andre2ff7b5d2000-06-03 14:57:40 +00001478 struct stat st;
1479
Damien Millerdd47aa22000-06-27 11:18:27 +10001480 if (stat(LASTLOG_FILE, &st) != 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001481 logit("%s: Couldn't stat %s: %s", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001482 LASTLOG_FILE, strerror(errno));
1483 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001484 }
andre2ff7b5d2000-06-03 14:57:40 +00001485 if (S_ISDIR(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001486 return (LL_DIR);
andre2ff7b5d2000-06-03 14:57:40 +00001487 else if (S_ISREG(st.st_mode))
Damien Miller8899ed32004-09-12 15:18:55 +10001488 return (LL_FILE);
andre2ff7b5d2000-06-03 14:57:40 +00001489 else
Damien Miller8899ed32004-09-12 15:18:55 +10001490 return (LL_OTHER);
andre61e67252000-06-04 17:07:49 +00001491}
andre2ff7b5d2000-06-03 14:57:40 +00001492
1493
1494/* open the file (using filemode) and seek to the login entry */
1495static int
andre61e67252000-06-04 17:07:49 +00001496lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1497{
andre2ff7b5d2000-06-03 14:57:40 +00001498 off_t offset;
1499 int type;
1500 char lastlog_file[1024];
1501
1502 type = lastlog_filetype(LASTLOG_FILE);
1503 switch (type) {
Damien Miller8899ed32004-09-12 15:18:55 +10001504 case LL_FILE:
1505 strlcpy(lastlog_file, LASTLOG_FILE,
1506 sizeof(lastlog_file));
1507 break;
1508 case LL_DIR:
1509 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1510 LASTLOG_FILE, li->username);
1511 break;
1512 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001513 logit("%s: %.100s is not a file or directory!", __func__,
Damien Miller8899ed32004-09-12 15:18:55 +10001514 LASTLOG_FILE);
1515 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001516 }
andre2ff7b5d2000-06-03 14:57:40 +00001517
Damien Miller405dc602003-04-09 21:12:52 +10001518 *fd = open(lastlog_file, filemode, 0600);
Damien Miller8899ed32004-09-12 15:18:55 +10001519 if (*fd < 0) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001520 debug("%s: Couldn't open %s: %s", __func__,
andre2ff7b5d2000-06-03 14:57:40 +00001521 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001522 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001523 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001524
Damien Millere477ef62000-08-15 10:21:17 +10001525 if (type == LL_FILE) {
1526 /* find this uid's offset in the lastlog file */
Kevin Steves52172652001-10-02 00:29:00 +00001527 offset = (off_t) ((long)li->uid * sizeof(struct lastlog));
andre2ff7b5d2000-06-03 14:57:40 +00001528
Damien Miller8899ed32004-09-12 15:18:55 +10001529 if (lseek(*fd, offset, SEEK_SET) != offset) {
Damien Miller6b0279c2004-09-12 15:25:17 +10001530 logit("%s: %s->lseek(): %s", __func__,
1531 lastlog_file, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001532 return (0);
Damien Millere477ef62000-08-15 10:21:17 +10001533 }
andre2ff7b5d2000-06-03 14:57:40 +00001534 }
Kevin Stevesef4eea92001-02-05 12:42:17 +00001535
Damien Miller8899ed32004-09-12 15:18:55 +10001536 return (1);
andre61e67252000-06-04 17:07:49 +00001537}
andre2ff7b5d2000-06-03 14:57:40 +00001538
1539static int
andre61e67252000-06-04 17:07:49 +00001540lastlog_perform_login(struct logininfo *li)
1541{
andre2ff7b5d2000-06-03 14:57:40 +00001542 struct lastlog last;
1543 int fd;
1544
1545 /* create our struct lastlog */
1546 lastlog_construct(li, &last);
1547
Damien Miller405dc602003-04-09 21:12:52 +10001548 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
Damien Miller8899ed32004-09-12 15:18:55 +10001549 return (0);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001550
andre2ff7b5d2000-06-03 14:57:40 +00001551 /* write the entry */
Darren Tucker8661b562003-07-06 15:20:46 +10001552 if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) {
Damien Millerc1132e72000-08-18 14:08:38 +10001553 close(fd);
Damien Miller6b0279c2004-09-12 15:25:17 +10001554 logit("%s: Error writing to %s: %s", __func__,
Damien Millerc1132e72000-08-18 14:08:38 +10001555 LASTLOG_FILE, strerror(errno));
Damien Miller8899ed32004-09-12 15:18:55 +10001556 return (0);
Damien Millerdd47aa22000-06-27 11:18:27 +10001557 }
Damien Millerc1132e72000-08-18 14:08:38 +10001558
1559 close(fd);
Damien Miller8899ed32004-09-12 15:18:55 +10001560 return (1);
andre61e67252000-06-04 17:07:49 +00001561}
andre2ff7b5d2000-06-03 14:57:40 +00001562
andre2ff7b5d2000-06-03 14:57:40 +00001563int
andre61e67252000-06-04 17:07:49 +00001564lastlog_write_entry(struct logininfo *li)
1565{
andre2ff7b5d2000-06-03 14:57:40 +00001566 switch(li->type) {
1567 case LTYPE_LOGIN:
Damien Miller8899ed32004-09-12 15:18:55 +10001568 return (lastlog_perform_login(li));
andre2ff7b5d2000-06-03 14:57:40 +00001569 default:
Damien Miller6b0279c2004-09-12 15:25:17 +10001570 logit("%s: Invalid type field", __func__);
Damien Miller8899ed32004-09-12 15:18:55 +10001571 return (0);
andre2ff7b5d2000-06-03 14:57:40 +00001572 }
andre61e67252000-06-04 17:07:49 +00001573}
andre2ff7b5d2000-06-03 14:57:40 +00001574
andre2ff7b5d2000-06-03 14:57:40 +00001575static void
andre61e67252000-06-04 17:07:49 +00001576lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1577{
andre2ff7b5d2000-06-03 14:57:40 +00001578 line_fullname(li->line, last->ll_line, sizeof(li->line));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001579 strlcpy(li->hostname, last->ll_host,
Damien Miller8899ed32004-09-12 15:18:55 +10001580 MIN_SIZEOF(li->hostname, last->ll_host));
andre2ff7b5d2000-06-03 14:57:40 +00001581 li->tv_sec = last->ll_time;
andre61e67252000-06-04 17:07:49 +00001582}
andre2ff7b5d2000-06-03 14:57:40 +00001583
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):
1601 lastlog_populate_entry(li, &last);
1602 return (1);
1603 case -1:
Damien Millera8e06ce2003-11-21 23:48:55 +11001604 error("%s: Error reading from %s: %s", __func__,
Damien Miller7df881d2003-01-07 16:46:58 +11001605 LASTLOG_FILE, strerror(errno));
1606 return (0);
1607 default:
1608 error("%s: Error reading from %s: Expecting %d, got %d",
Darren Tuckerefc17472005-11-22 19:55:13 +11001609 __func__, LASTLOG_FILE, (int)sizeof(last), ret);
Damien Miller7df881d2003-01-07 16:46:58 +11001610 return (0);
1611 }
Damien Miller3a8a5cd2001-10-22 16:49:22 +10001612
Damien Miller7df881d2003-01-07 16:46:58 +11001613 /* NOTREACHED */
1614 return (0);
andre61e67252000-06-04 17:07:49 +00001615}
Damien Millerd5bf3072000-06-07 21:32:13 +10001616#endif /* USE_LASTLOG */
Darren Tucker2fba9932005-02-02 23:30:24 +11001617
1618#ifdef USE_BTMP
1619 /*
1620 * Logs failed login attempts in _PATH_BTMP if that exists.
1621 * The most common login failure is to give password instead of username.
1622 * So the _PATH_BTMP file checked for the correct permission, so that
1623 * only root can read it.
1624 */
1625
1626void
1627record_failed_login(const char *username, const char *hostname,
1628 const char *ttyn)
1629{
1630 int fd;
1631 struct utmp ut;
1632 struct sockaddr_storage from;
Darren Tuckerefc17472005-11-22 19:55:13 +11001633 socklen_t fromlen = sizeof(from);
Darren Tucker2fba9932005-02-02 23:30:24 +11001634 struct sockaddr_in *a4;
1635 struct sockaddr_in6 *a6;
1636 time_t t;
1637 struct stat fst;
1638
1639 if (geteuid() != 0)
1640 return;
1641 if ((fd = open(_PATH_BTMP, O_WRONLY | O_APPEND)) < 0) {
1642 debug("Unable to open the btmp file %s: %s", _PATH_BTMP,
1643 strerror(errno));
1644 return;
1645 }
1646 if (fstat(fd, &fst) < 0) {
1647 logit("%s: fstat of %s failed: %s", __func__, _PATH_BTMP,
1648 strerror(errno));
1649 goto out;
1650 }
1651 if((fst.st_mode & (S_IRWXG | S_IRWXO)) || (fst.st_uid != 0)){
1652 logit("Excess permission or bad ownership on file %s",
1653 _PATH_BTMP);
1654 goto out;
1655 }
1656
1657 memset(&ut, 0, sizeof(ut));
1658 /* strncpy because we don't necessarily want nul termination */
1659 strncpy(ut.ut_user, username, sizeof(ut.ut_user));
1660 strlcpy(ut.ut_line, "ssh:notty", sizeof(ut.ut_line));
1661
1662 time(&t);
1663 ut.ut_time = t; /* ut_time is not always a time_t */
1664 ut.ut_type = LOGIN_PROCESS;
1665 ut.ut_pid = getpid();
1666
1667 /* strncpy because we don't necessarily want nul termination */
1668 strncpy(ut.ut_host, hostname, sizeof(ut.ut_host));
1669
1670 if (packet_connection_is_on_socket() &&
1671 getpeername(packet_get_connection_in(),
1672 (struct sockaddr *)&from, &fromlen) == 0) {
1673 ipv64_normalise_mapped(&from, &fromlen);
1674 if (from.ss_family == AF_INET) {
1675 a4 = (struct sockaddr_in *)&from;
1676 memcpy(&ut.ut_addr, &(a4->sin_addr),
1677 MIN_SIZEOF(ut.ut_addr, a4->sin_addr));
1678 }
1679#ifdef HAVE_ADDR_V6_IN_UTMP
1680 if (from.ss_family == AF_INET6) {
1681 a6 = (struct sockaddr_in6 *)&from;
1682 memcpy(&ut.ut_addr_v6, &(a6->sin6_addr),
1683 MIN_SIZEOF(ut.ut_addr_v6, a6->sin6_addr));
1684 }
1685#endif
1686 }
1687
1688 if (atomicio(vwrite, fd, &ut, sizeof(ut)) != sizeof(ut))
1689 error("Failed to write to %s: %s", _PATH_BTMP,
1690 strerror(errno));
1691
1692out:
1693 close(fd);
1694}
1695#endif /* USE_BTMP */