blob: 61bceb180b37538e7ae41dab4cc6afff24127343 [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.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Markus Friedl.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/**
34 ** loginrec.c: platform-independent login recording and lastlog retrieval
35 **/
36
andre61e67252000-06-04 17:07:49 +000037/*
38 The new login code explained
39 ============================
40
41 This code attempts to provide a common interface to login recording
42 (utmp and friends) and last login time retrieval.
43
44 Its primary means of achieving this is to use 'struct logininfo', a
45 union of all the useful fields in the various different types of
46 system login record structures one finds on UNIX variants.
47
48 We depend on autoconf to define which recording methods are to be
49 used, and which fields are contained in the relevant data structures
50 on the local system. Many C preprocessor symbols affect which code
51 gets compiled here.
52
53 The code is designed to make it easy to modify a particular
54 recording method, without affecting other methods nor requiring so
55 many nested conditional compilation blocks as were commonplace in
56 the old code.
57
58 For login recording, we try to use the local system's libraries as
59 these are clearly most likely to work correctly. For utmp systems
60 this usually means login() and logout() or setutent() etc., probably
61 in libutil, along with logwtmp() etc. On these systems, we fall back
62 to writing the files directly if we have to, though this method
63 requires very thorough testing so we do not corrupt local auditing
64 information. These files and their access methods are very system
65 specific indeed.
66
67 For utmpx systems, the corresponding library functions are
68 setutxent() etc. To the author's knowledge, all utmpx systems have
69 these library functions and so no direct write is attempted. If such
70 a system exists and needs support, direct analogues of the [uw]tmp
71 code should suffice.
72
73 Retrieving the time of last login ('lastlog') is in some ways even
74 more problemmatic than login recording. Some systems provide a
75 simple table of all users which we seek based on uid and retrieve a
76 relatively standard structure. Others record the same information in
77 a directory with a separate file, and others don't record the
78 information separately at all. For systems in the latter category,
79 we look backwards in the wtmp or wtmpx file for the last login entry
80 for our user. Naturally this is slower and on busy systems could
81 incur a significant performance penalty.
82
83 Calling the new code
84 --------------------
85
86 In OpenSSH all login recording and retrieval is performed in
87 login.c. Here you'll find working examples. Also, in the logintest.c
88 program there are more examples.
89
90 Internal handler calling method
91 -------------------------------
92
93 When a call is made to login_login() or login_logout(), both
94 routines set a struct logininfo flag defining which action (log in,
95 or log out) is to be taken. They both then call login_write(), which
96 calls whichever of the many structure-specific handlers autoconf
97 selects for the local system.
98
99 The handlers themselves handle system data structure specifics. Both
100 struct utmp and struct utmpx have utility functions (see
101 construct_utmp*()) to try to make it simpler to add extra systems
102 that introduce new features to either structure.
103
104 While it may seem terribly wasteful to replicate so much similar
105 code for each method, experience has shown that maintaining code to
106 write both struct utmp and utmpx in one function, whilst maintaining
107 support for all systems whether they have library support or not, is
108 a difficult and time-consuming task.
109
110 Lastlog support proceeds similarly. Functions login_get_lastlog()
111 (and its OpenSSH-tuned friend login_get_lastlog_time()) call
112 getlast_entry(), which tries one of three methods to find the last
113 login time. It uses local system lastlog support if it can,
114 otherwise it tries wtmp or wtmpx before giving up and returning 0,
115 meaning "tilt".
116
117 Maintenance
118 -----------
119
120 In many cases it's possible to tweak autoconf to select the correct
121 methods for a particular platform, either by improving the detection
122 code (best), or by presetting DISABLE_<method> or CONF_<method>_FILE
123 symbols for the platform.
124
125 Use logintest to check which symbols are defined before modifying
126 configure.in and loginrec.c. (You have to build logintest yourself
127 with 'make logintest' as it's not built by default.)
128
129 Otherwise, patches to the specific method(s) are very helpful!
130
131*/
132
andre2ff7b5d2000-06-03 14:57:40 +0000133/**
134 ** TODO:
Damien Millere5192fa2000-08-29 14:30:37 +1100135 ** homegrown ttyslot()
andre61e67252000-06-04 17:07:49 +0000136 ** test, test, test
andre2ff7b5d2000-06-03 14:57:40 +0000137 **
138 ** Platform status:
139 ** ----------------
140 **
141 ** Known good:
Damien Millere5192fa2000-08-29 14:30:37 +1100142 ** Linux (Redhat 6.2, Debian)
143 ** Solaris
andre2ff7b5d2000-06-03 14:57:40 +0000144 ** HP-UX 10.20 (gcc only)
andre6bb92372000-06-19 08:20:03 +0000145 ** IRIX
Damien Millere5192fa2000-08-29 14:30:37 +1100146 ** NeXT - M68k/HPPA (4.2/3.3)
andre2ff7b5d2000-06-03 14:57:40 +0000147 **
148 ** Testing required: Please send reports!
andre2ff7b5d2000-06-03 14:57:40 +0000149 ** NetBSD
150 ** HP-UX 11
andre60f3c982000-06-03 16:18:19 +0000151 ** AIX
andre2ff7b5d2000-06-03 14:57:40 +0000152 **
153 ** Platforms with known problems:
Damien Millere5192fa2000-08-29 14:30:37 +1100154 ** Some variants of Slackware Linux
andre2ff7b5d2000-06-03 14:57:40 +0000155 **
156 **/
157
158#include "includes.h"
159
andre2ff7b5d2000-06-03 14:57:40 +0000160#include "ssh.h"
161#include "xmalloc.h"
162#include "loginrec.h"
163
Damien Millere5192fa2000-08-29 14:30:37 +1100164RCSID("$Id: loginrec.c,v 1.22 2000/08/29 03:30:37 djm Exp $");
andre2ff7b5d2000-06-03 14:57:40 +0000165
166/**
167 ** prototypes for helper functions in this file
168 **/
169
170#if HAVE_UTMP_H
andre2ff7b5d2000-06-03 14:57:40 +0000171void set_utmp_time(struct logininfo *li, struct utmp *ut);
172void construct_utmp(struct logininfo *li, struct utmp *ut);
173#endif
174
175#ifdef HAVE_UTMPX_H
andre2ff7b5d2000-06-03 14:57:40 +0000176void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
177void construct_utmpx(struct logininfo *li, struct utmpx *ut);
178#endif
179
180int utmp_write_entry(struct logininfo *li);
181int utmpx_write_entry(struct logininfo *li);
182int wtmp_write_entry(struct logininfo *li);
183int wtmpx_write_entry(struct logininfo *li);
184int lastlog_write_entry(struct logininfo *li);
185int syslogin_write_entry(struct logininfo *li);
186
187int getlast_entry(struct logininfo *li);
188int lastlog_get_entry(struct logininfo *li);
189int wtmp_get_entry(struct logininfo *li);
190int wtmpx_get_entry(struct logininfo *li);
191
andre6bb92372000-06-19 08:20:03 +0000192/* pick the shortest string */
193#define MIN_SIZEOF(s1,s2) ( sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2) )
194
andre2ff7b5d2000-06-03 14:57:40 +0000195/**
196 ** platform-independent login functions
197 **/
198
andre6bb92372000-06-19 08:20:03 +0000199/* login_login(struct logininfo *) -Record a login
200 *
201 * Call with a pointer to a struct logininfo initialised with
202 * login_init_entry() or login_alloc_entry()
203 *
204 * Returns:
205 * >0 if successful
206 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
207 */
andre61e67252000-06-04 17:07:49 +0000208int
209login_login (struct logininfo *li)
210{
211 li->type = LTYPE_LOGIN;
212 return login_write(li);
213}
214
215
andre6bb92372000-06-19 08:20:03 +0000216/* login_logout(struct logininfo *) - Record a logout
217 *
218 * Call as with login_login()
219 *
220 * Returns:
221 * >0 if successful
222 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
223 */
andre61e67252000-06-04 17:07:49 +0000224int
225login_logout(struct logininfo *li)
226{
227 li->type = LTYPE_LOGOUT;
228 return login_write(li);
229}
230
andre6bb92372000-06-19 08:20:03 +0000231/* login_get_lastlog_time(int) - Retrieve the last login time
232 *
233 * Retrieve the last login time for the given uid. Will try to use the
234 * system lastlog facilities if they are available, but will fall back
235 * to looking in wtmp/wtmpx if necessary
236 *
237 * Returns:
238 * 0 on failure, or if user has never logged in
239 * Time in seconds from the epoch if successful
240 *
241 * Useful preprocessor symbols:
242 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
243 * info
244 * USE_LASTLOG: If set, indicates the presence of system lastlog
245 * facilities. If this and DISABLE_LASTLOG are not set,
246 * try to retrieve lastlog information from wtmp/wtmpx.
247 */
andre61e67252000-06-04 17:07:49 +0000248unsigned int
249login_get_lastlog_time(const int uid)
250{
251 struct logininfo li;
252
andre6bb92372000-06-19 08:20:03 +0000253 if (login_get_lastlog(&li, uid))
254 return li.tv_sec;
255 else
256 return 0;
andre61e67252000-06-04 17:07:49 +0000257}
258
andre6bb92372000-06-19 08:20:03 +0000259/* login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
260 *
261 * Retrieve a logininfo structure populated (only partially) with
262 * information from the system lastlog data, or from wtmp/wtmpx if no
263 * system lastlog information exists.
264 *
265 * Note this routine must be given a pre-allocated logininfo.
266 *
267 * Returns:
268 * >0: A pointer to your struct logininfo if successful
269 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
270 *
271 */
andre61e67252000-06-04 17:07:49 +0000272struct logininfo *
273login_get_lastlog(struct logininfo *li, const int uid)
274{
andre6bb92372000-06-19 08:20:03 +0000275 struct passwd *pw;
andre6bb92372000-06-19 08:20:03 +0000276
Damien Miller348c9b72000-08-15 10:01:22 +1000277 memset(li, '\0', sizeof(*li));
andre61e67252000-06-04 17:07:49 +0000278 li->uid = uid;
andre6bb92372000-06-19 08:20:03 +0000279
Damien Miller53c5d462000-06-28 00:50:50 +1000280 /*
281 * If we don't have a 'real' lastlog, we need the username to
andre6bb92372000-06-19 08:20:03 +0000282 * reliably search wtmp(x) for the last login (see
Damien Miller53c5d462000-06-28 00:50:50 +1000283 * wtmp_get_entry().)
284 */
andre6bb92372000-06-19 08:20:03 +0000285 pw = getpwuid(uid);
Damien Millerdd47aa22000-06-27 11:18:27 +1000286 if (pw == NULL)
287 fatal("login_get_lastlog: Cannot find account for uid %i", uid);
288
andre98cabe02000-06-19 09:11:30 +0000289 /* No MIN_SIZEOF here - we absolutely *must not* truncate the
290 * username */
Damien Millerf8af08d2000-06-27 09:40:06 +1000291 strlcpy(li->username, pw->pw_name, sizeof(li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000292
andre61e67252000-06-04 17:07:49 +0000293 if (getlast_entry(li))
294 return li;
295 else
Damien Millerdd47aa22000-06-27 11:18:27 +1000296 return NULL;
andre61e67252000-06-04 17:07:49 +0000297}
298
299
andre6bb92372000-06-19 08:20:03 +0000300/* login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
301 * a logininfo structure
302 *
303 * This function creates a new struct logininfo, a data structure
304 * meant to carry the information required to portably record login info.
305 *
306 * Returns a pointer to a newly created struct logininfo. If memory
307 * allocation fails, the program halts.
308 */
andre61e67252000-06-04 17:07:49 +0000309struct
310logininfo *login_alloc_entry(int pid, const char *username,
311 const char *hostname, const char *line)
312{
andre2ff7b5d2000-06-03 14:57:40 +0000313 struct logininfo *newli;
314
Damien Miller348c9b72000-08-15 10:01:22 +1000315 newli = (struct logininfo *) xmalloc (sizeof(*newli));
andre61e67252000-06-04 17:07:49 +0000316 (void)login_init_entry(newli, pid, username, hostname, line);
317 return newli;
318}
andre2ff7b5d2000-06-03 14:57:40 +0000319
320
andre6bb92372000-06-19 08:20:03 +0000321/* login_free_entry(struct logininfo *) - free struct memory */
andre61e67252000-06-04 17:07:49 +0000322void
323login_free_entry(struct logininfo *li)
324{
325 xfree(li);
326}
327
andre2ff7b5d2000-06-03 14:57:40 +0000328
andre6bb92372000-06-19 08:20:03 +0000329/* login_init_entry(struct logininfo *, int, char*, char*, char*)
330 * - initialise a struct logininfo
331 *
332 * Populates a new struct logininfo, a data structure meant to carry
333 * the information required to portably record login info.
334 *
335 * Returns: 1
336 */
andre61e67252000-06-04 17:07:49 +0000337int
338login_init_entry(struct logininfo *li, int pid, const char *username,
339 const char *hostname, const char *line)
340{
Damien Millerf8af08d2000-06-27 09:40:06 +1000341 struct passwd *pw;
342
Damien Miller348c9b72000-08-15 10:01:22 +1000343 memset(li, 0, sizeof(*li));
andre2ff7b5d2000-06-03 14:57:40 +0000344
andre61e67252000-06-04 17:07:49 +0000345 li->pid = pid;
Damien Millerf8af08d2000-06-27 09:40:06 +1000346
andre2ff7b5d2000-06-03 14:57:40 +0000347 /* set the line information */
andre61e67252000-06-04 17:07:49 +0000348 if (line)
andre2ff7b5d2000-06-03 14:57:40 +0000349 line_fullname(li->line, line, sizeof(li->line));
andre2ff7b5d2000-06-03 14:57:40 +0000350
Damien Millerf8af08d2000-06-27 09:40:06 +1000351 if (username) {
andre2ff7b5d2000-06-03 14:57:40 +0000352 strlcpy(li->username, username, sizeof(li->username));
Damien Millerf8af08d2000-06-27 09:40:06 +1000353 pw = getpwnam(li->username);
354 if (pw == NULL)
355 fatal("login_init_entry: Cannot find user \"%s\"", li->username);
356 li->uid = pw->pw_uid;
357 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000358
andre61e67252000-06-04 17:07:49 +0000359 if (hostname)
andre2ff7b5d2000-06-03 14:57:40 +0000360 strlcpy(li->hostname, hostname, sizeof(li->hostname));
Damien Millerf8af08d2000-06-27 09:40:06 +1000361
andre61e67252000-06-04 17:07:49 +0000362 return 1;
andre2ff7b5d2000-06-03 14:57:40 +0000363}
364
andre6bb92372000-06-19 08:20:03 +0000365/* login_set_current_time(struct logininfo *) - set the current time
366 *
367 * Set the current time in a logininfo structure. This function is
368 * meant to eliminate the need to deal with system dependencies for
369 * time handling.
370 */
andre2ff7b5d2000-06-03 14:57:40 +0000371void
andre61e67252000-06-04 17:07:49 +0000372login_set_current_time(struct logininfo *li)
373{
andre2ff7b5d2000-06-03 14:57:40 +0000374 struct timeval tv;
375
376 gettimeofday(&tv, NULL);
Damien Millerf8af08d2000-06-27 09:40:06 +1000377
378 li->tv_sec = tv.tv_sec;
379 li->tv_usec = tv.tv_usec;
andre2ff7b5d2000-06-03 14:57:40 +0000380}
381
andre61e67252000-06-04 17:07:49 +0000382/* copy a sockaddr_* into our logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000383void
andre61e67252000-06-04 17:07:49 +0000384login_set_addr(struct logininfo *li, const struct sockaddr *sa,
385 const unsigned int sa_size)
386{
387 unsigned int bufsize = sa_size;
388
389 /* make sure we don't overrun our union */
390 if (sizeof(li->hostaddr) < sa_size)
391 bufsize = sizeof(li->hostaddr);
392
393 memcpy((void *)&(li->hostaddr.sa), (const void *)sa, bufsize);
andre2ff7b5d2000-06-03 14:57:40 +0000394}
395
andre2ff7b5d2000-06-03 14:57:40 +0000396
andre61e67252000-06-04 17:07:49 +0000397/**
398 ** login_write: Call low-level recording functions based on autoconf
399 ** results
400 **/
andre2ff7b5d2000-06-03 14:57:40 +0000401int
andre61e67252000-06-04 17:07:49 +0000402login_write (struct logininfo *li)
403{
andre2ff7b5d2000-06-03 14:57:40 +0000404 if ((int)geteuid() != 0) {
405 log("Attempt to write login records by non-root user (aborting)");
406 return 1;
407 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000408
andre2ff7b5d2000-06-03 14:57:40 +0000409 /* set the timestamp */
410 login_set_current_time(li);
411#ifdef USE_LOGIN
412 syslogin_write_entry(li);
413#endif
414#ifdef USE_LASTLOG
415 if (li->type == LTYPE_LOGIN) {
416 lastlog_write_entry(li);
417 }
418#endif
419#ifdef USE_UTMP
420 utmp_write_entry(li);
421#endif
422#ifdef USE_WTMP
423 wtmp_write_entry(li);
424#endif
425#ifdef USE_UTMPX
426 utmpx_write_entry(li);
427#endif
428#ifdef USE_WTMPX
429 wtmpx_write_entry(li);
430#endif
431 return 0;
432}
433
andre2ff7b5d2000-06-03 14:57:40 +0000434/**
andre61e67252000-06-04 17:07:49 +0000435 ** getlast_entry: Call low-level functions to retrieve the last login
436 ** time.
andre2ff7b5d2000-06-03 14:57:40 +0000437 **/
438
andre61e67252000-06-04 17:07:49 +0000439/* take the uid in li and return the last login time */
440int
441getlast_entry(struct logininfo *li)
442{
443#ifdef USE_LASTLOG
Damien Miller53c5d462000-06-28 00:50:50 +1000444 return(lastlog_get_entry(li));
Damien Millerdd47aa22000-06-27 11:18:27 +1000445#else /* !USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000446
Damien Millerdd47aa22000-06-27 11:18:27 +1000447#ifdef DISABLE_LASTLOG
andreecaabf12000-06-12 22:21:44 +0000448 /* On some systems we shouldn't even try to obtain last login
449 * time, e.g. AIX */
450 return 0;
Damien Millerdd47aa22000-06-27 11:18:27 +1000451# else /* DISABLE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000452 /* Try to retrieve the last login time from wtmp */
Damien Millerdd47aa22000-06-27 11:18:27 +1000453# if defined(USE_WTMP) && (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
andre61e67252000-06-04 17:07:49 +0000454 /* retrieve last login time from utmp */
Damien Millerdd47aa22000-06-27 11:18:27 +1000455 return (wtmp_get_entry(li));
456# else /* defined(USE_WTMP) && (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP)) */
andre61e67252000-06-04 17:07:49 +0000457 /* If wtmp isn't available, try wtmpx */
Damien Millerdd47aa22000-06-27 11:18:27 +1000458# if defined(USE_WTMPX) && (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
andre61e67252000-06-04 17:07:49 +0000459 /* retrieve last login time from utmpx */
Damien Millerdd47aa22000-06-27 11:18:27 +1000460 return (wtmpx_get_entry(li));
461# else
andre61e67252000-06-04 17:07:49 +0000462 /* Give up: No means of retrieving last login time */
463 return 0;
Damien Millerdd47aa22000-06-27 11:18:27 +1000464# endif /* USE_WTMPX && (HAVE_TIME_IN_UTMPX || HAVE_TV_IN_UTMPX) */
465# endif /* USE_WTMP && (HAVE_TIME_IN_UTMP || HAVE_TV_IN_UTMP) */
466# endif /* DISABLE_LASTLOG */
467#endif /* USE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000468}
469
470
471
andre2ff7b5d2000-06-03 14:57:40 +0000472/*
andre61e67252000-06-04 17:07:49 +0000473 * 'line' string utility functions
474 *
475 * These functions process the 'line' string into one of three forms:
476 *
andre2ff7b5d2000-06-03 14:57:40 +0000477 * 1. The full filename (including '/dev')
478 * 2. The stripped name (excluding '/dev')
andre61e67252000-06-04 17:07:49 +0000479 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
480 * /dev/pts/1 -> ts/1 )
andre2ff7b5d2000-06-03 14:57:40 +0000481 *
482 * Form 3 is used on some systems to identify a .tmp.? entry when
483 * attempting to remove it. Typically both addition and removal is
andre61e67252000-06-04 17:07:49 +0000484 * performed by one application - say, sshd - so as long as the choice
485 * uniquely identifies a terminal it's ok.
andre2ff7b5d2000-06-03 14:57:40 +0000486 */
487
488
andre61e67252000-06-04 17:07:49 +0000489/* line_fullname(): add the leading '/dev/' if it doesn't exist make
490 * sure dst has enough space, if not just copy src (ugh) */
andre2ff7b5d2000-06-03 14:57:40 +0000491char *
andre61e67252000-06-04 17:07:49 +0000492line_fullname(char *dst, const char *src, int dstsize)
493{
andre2ff7b5d2000-06-03 14:57:40 +0000494 memset(dst, '\0', dstsize);
495 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
496 strlcpy(dst, src, dstsize);
497 else {
Damien Miller1a132252000-06-13 21:23:17 +1000498 strlcpy(dst, "/dev/", dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000499 strlcat(dst, src, dstsize);
500 }
501 return dst;
502}
503
andre61e67252000-06-04 17:07:49 +0000504/* line_stripname(): strip the leading '/dev' if it exists, return dst */
andre2ff7b5d2000-06-03 14:57:40 +0000505char *
andre61e67252000-06-04 17:07:49 +0000506line_stripname(char *dst, const char *src, int dstsize)
507{
andre2ff7b5d2000-06-03 14:57:40 +0000508 memset(dst, '\0', dstsize);
509 if (strncmp(src, "/dev/", 5) == 0)
510 strlcpy(dst, &src[5], dstsize);
511 else
512 strlcpy(dst, src, dstsize);
513 return dst;
andre61e67252000-06-04 17:07:49 +0000514}
515
andre61e67252000-06-04 17:07:49 +0000516/* line_abbrevname(): Return the abbreviated (usually four-character)
517 * form of the line (Just use the last <dstsize> characters of the
518 * full name.)
519 *
520 * NOTE: use strncpy because we do NOT necessarily want zero
521 * termination */
andre2ff7b5d2000-06-03 14:57:40 +0000522char *
Damien Millerdd47aa22000-06-27 11:18:27 +1000523line_abbrevname(char *dst, const char *src, int dstsize)
524{
525 size_t len;
526
andre2ff7b5d2000-06-03 14:57:40 +0000527 memset(dst, '\0', dstsize);
Damien Millerdd47aa22000-06-27 11:18:27 +1000528
Damien Miller8e81ed32000-07-01 13:17:42 +1000529 /* Always skip prefix if present */
530 if (strncmp(src, "/dev/", 5) == 0)
531 src += 5;
532
Damien Millerdd47aa22000-06-27 11:18:27 +1000533 len = strlen(src);
534
Damien Miller8e81ed32000-07-01 13:17:42 +1000535 if (len > 0) {
536 if (((int)len - dstsize) > 0)
537 src += ((int)len - dstsize);
538
539 /* note: _don't_ change this to strlcpy */
540 strncpy(dst, src, (size_t)dstsize);
Damien Millerdd47aa22000-06-27 11:18:27 +1000541 }
542
andre2ff7b5d2000-06-03 14:57:40 +0000543 return dst;
544}
545
andre2ff7b5d2000-06-03 14:57:40 +0000546/**
547 ** utmp utility functions
andre61e67252000-06-04 17:07:49 +0000548 **
549 ** These functions manipulate struct utmp, taking system differences
550 ** into account.
andre2ff7b5d2000-06-03 14:57:40 +0000551 **/
552
553#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
554
andre2ff7b5d2000-06-03 14:57:40 +0000555/* build the utmp structure */
556void
andre61e67252000-06-04 17:07:49 +0000557set_utmp_time(struct logininfo *li, struct utmp *ut)
558{
Damien Millerdd47aa22000-06-27 11:18:27 +1000559# ifdef HAVE_TV_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000560 ut->ut_tv.tv_sec = li->tv_sec;
561 ut->ut_tv.tv_usec = li->tv_usec;
Damien Millerdd47aa22000-06-27 11:18:27 +1000562# else
andre2ff7b5d2000-06-03 14:57:40 +0000563# ifdef HAVE_TIME_IN_UTMP
564 ut->ut_time = li->tv_sec;
565# endif
Damien Millerdd47aa22000-06-27 11:18:27 +1000566# endif
andre2ff7b5d2000-06-03 14:57:40 +0000567}
568
569void
570construct_utmp(struct logininfo *li,
andre61e67252000-06-04 17:07:49 +0000571 struct utmp *ut)
572{
Damien Miller348c9b72000-08-15 10:01:22 +1000573 memset(ut, '\0', sizeof(*ut));
andre6bb92372000-06-19 08:20:03 +0000574
575 /* First fill out fields used for both logins and logouts */
576
Damien Millerdd47aa22000-06-27 11:18:27 +1000577# ifdef HAVE_ID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000578 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +1000579# endif
andre2ff7b5d2000-06-03 14:57:40 +0000580
Damien Millerdd47aa22000-06-27 11:18:27 +1000581# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +0000582 /* This is done here to keep utmp constants out of struct logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000583 switch (li->type) {
584 case LTYPE_LOGIN:
585 ut->ut_type = USER_PROCESS;
586 break;
587 case LTYPE_LOGOUT:
588 ut->ut_type = DEAD_PROCESS;
589 break;
590 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000591# endif
andre6bb92372000-06-19 08:20:03 +0000592 set_utmp_time(li, ut);
andre2ff7b5d2000-06-03 14:57:40 +0000593
andre6bb92372000-06-19 08:20:03 +0000594 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +1000595
596# ifdef HAVE_PID_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +0000597 ut->ut_pid = li->pid;
Damien Millerdd47aa22000-06-27 11:18:27 +1000598# endif
andre6bb92372000-06-19 08:20:03 +0000599
600 /* If we're logging out, leave all other fields blank */
601 if (li->type == LTYPE_LOGOUT)
602 return;
603
Damien Millerdd47aa22000-06-27 11:18:27 +1000604 /*
605 * These fields are only used when logging in, and are blank
606 * for logouts.
607 */
andre6bb92372000-06-19 08:20:03 +0000608
609 /* Use strncpy because we don't necessarily want null termination */
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000610 strncpy(ut->ut_name, li->username, MIN_SIZEOF(ut->ut_name, li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000611# ifdef HAVE_HOST_IN_UTMP
andre6bb92372000-06-19 08:20:03 +0000612 strncpy(ut->ut_host, li->hostname, MIN_SIZEOF(ut->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000613# endif
614# ifdef HAVE_ADDR_IN_UTMP
andre61e67252000-06-04 17:07:49 +0000615 /* this is just a 32-bit IP address */
616 if (li->hostaddr.sa.sa_family == AF_INET)
617 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
Damien Millerdd47aa22000-06-27 11:18:27 +1000618# endif
andre61e67252000-06-04 17:07:49 +0000619}
Damien Millerdd47aa22000-06-27 11:18:27 +1000620#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
andre61e67252000-06-04 17:07:49 +0000621
andre2ff7b5d2000-06-03 14:57:40 +0000622/**
623 ** utmpx utility functions
andre61e67252000-06-04 17:07:49 +0000624 **
625 ** These functions manipulate struct utmpx, accounting for system
626 ** variations.
andre2ff7b5d2000-06-03 14:57:40 +0000627 **/
628
629#if defined(USE_UTMPX) || defined (USE_WTMPX)
andre2ff7b5d2000-06-03 14:57:40 +0000630/* build the utmpx structure */
631void
andre61e67252000-06-04 17:07:49 +0000632set_utmpx_time(struct logininfo *li, struct utmpx *utx)
633{
Damien Millerdd47aa22000-06-27 11:18:27 +1000634# ifdef HAVE_TV_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000635 utx->ut_tv.tv_sec = li->tv_sec;
636 utx->ut_tv.tv_usec = li->tv_usec;
Damien Millerdd47aa22000-06-27 11:18:27 +1000637# else /* HAVE_TV_IN_UTMPX */
andre2ff7b5d2000-06-03 14:57:40 +0000638# ifdef HAVE_TIME_IN_UTMPX
639 utx->ut_time = li->tv_sec;
Damien Millerdd47aa22000-06-27 11:18:27 +1000640# endif /* HAVE_TIME_IN_UTMPX */
641# endif /* HAVE_TV_IN_UTMPX */
andre2ff7b5d2000-06-03 14:57:40 +0000642}
643
andre61e67252000-06-04 17:07:49 +0000644void
645construct_utmpx(struct logininfo *li, struct utmpx *utx)
646{
Damien Miller348c9b72000-08-15 10:01:22 +1000647 memset(utx, '\0', sizeof(*utx));
Damien Miller8e81ed32000-07-01 13:17:42 +1000648# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000649 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
Damien Miller8e81ed32000-07-01 13:17:42 +1000650# endif
andre2ff7b5d2000-06-03 14:57:40 +0000651
652 /* this is done here to keep utmp constants out of loginrec.h */
653 switch (li->type) {
654 case LTYPE_LOGIN:
655 utx->ut_type = USER_PROCESS;
656 break;
657 case LTYPE_LOGOUT:
658 utx->ut_type = DEAD_PROCESS;
659 break;
660 }
andre2ff7b5d2000-06-03 14:57:40 +0000661 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
andre2ff7b5d2000-06-03 14:57:40 +0000662 set_utmpx_time(li, utx);
andre6bb92372000-06-19 08:20:03 +0000663 utx->ut_pid = li->pid;
664
665 if (li->type == LTYPE_LOGOUT)
666 return;
667
Damien Millerdd47aa22000-06-27 11:18:27 +1000668 /*
669 * These fields are only used when logging in, and are blank
670 * for logouts.
671 */
andre6bb92372000-06-19 08:20:03 +0000672
673 /* strncpy(): Don't necessarily want null termination */
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000674 strncpy(utx->ut_name, li->username, MIN_SIZEOF(utx->ut_name, li->username));
Damien Millerdd47aa22000-06-27 11:18:27 +1000675# ifdef HAVE_HOST_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +0000676 strncpy(utx->ut_host, li->hostname, MIN_SIZEOF(utx->ut_host, li->hostname));
Damien Millerdd47aa22000-06-27 11:18:27 +1000677# endif
678# ifdef HAVE_ADDR_IN_UTMPX
andre61e67252000-06-04 17:07:49 +0000679 /* FIXME: (ATL) not supported yet */
Damien Millerdd47aa22000-06-27 11:18:27 +1000680# endif
681# ifdef HAVE_SYSLEN_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +0000682 /* ut_syslen is the length of the utx_host string */
683 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +1000684# endif
andre61e67252000-06-04 17:07:49 +0000685}
Damien Millerdd47aa22000-06-27 11:18:27 +1000686#endif /* USE_UTMPX || USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +0000687
688/**
andre61e67252000-06-04 17:07:49 +0000689 ** Low-level utmp functions
andre2ff7b5d2000-06-03 14:57:40 +0000690 **/
691
692/* FIXME: (ATL) utmp_write_direct needs testing */
andre2ff7b5d2000-06-03 14:57:40 +0000693#ifdef USE_UTMP
694
andre2ff7b5d2000-06-03 14:57:40 +0000695/* if we can, use pututline() etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000696# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
697 defined(HAVE_PUTUTLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000698# define UTMP_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000699# endif
andre2ff7b5d2000-06-03 14:57:40 +0000700
701
702/* write a utmp entry with the system's help (pututline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000703# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000704static int
andre61e67252000-06-04 17:07:49 +0000705utmp_write_library(struct logininfo *li, struct utmp *ut)
706{
andre2ff7b5d2000-06-03 14:57:40 +0000707 setutent();
708 pututline(ut);
709
Damien Millerdd47aa22000-06-27 11:18:27 +1000710# ifdef HAVE_ENDUTENT
andre2ff7b5d2000-06-03 14:57:40 +0000711 endutent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000712# endif
andre2ff7b5d2000-06-03 14:57:40 +0000713 return 1;
andre61e67252000-06-04 17:07:49 +0000714}
Damien Millerdd47aa22000-06-27 11:18:27 +1000715# else /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000716
717/* write a utmp entry direct to the file */
andre61e67252000-06-04 17:07:49 +0000718/* This is a slightly modification of code in OpenBSD's login.c */
andre2ff7b5d2000-06-03 14:57:40 +0000719static int
andre61e67252000-06-04 17:07:49 +0000720utmp_write_direct(struct logininfo *li, struct utmp *ut)
721{
andre2ff7b5d2000-06-03 14:57:40 +0000722 struct utmp old_ut;
723 register int fd;
724 int tty;
725
andre6bb92372000-06-19 08:20:03 +0000726 /* FIXME: (ATL) ttyslot() needs local implementation */
Damien Miller348c9b72000-08-15 10:01:22 +1000727
Damien Millere5192fa2000-08-29 14:30:37 +1100728#if defined(HAVE_GETTTYENT)
Damien Miller348c9b72000-08-15 10:01:22 +1000729 register struct ttyent *ty;
730
731 tty=0;
732
733 setttyent();
734 while ((struct ttyent *)0 != (ty = getttyent())) {
735 tty++;
736 if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line)))
737 break;
738 }
739 endttyent();
740
741 if((struct ttyent *)0 == ty) {
742 log("utmp_write_entry: tty not found");
743 return(1);
744 }
745#else /* FIXME */
746
andre2ff7b5d2000-06-03 14:57:40 +0000747 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
748
Damien Millere5192fa2000-08-29 14:30:37 +1100749#endif /* HAVE_GETTTYENT */
Damien Miller348c9b72000-08-15 10:01:22 +1000750
andre2ff7b5d2000-06-03 14:57:40 +0000751 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
752 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
753 /*
754 * Prevent luser from zero'ing out ut_host.
755 * If the new ut_line is empty but the old one is not
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000756 * and ut_line and ut_name match, preserve the old ut_line.
andre2ff7b5d2000-06-03 14:57:40 +0000757 */
Damien Miller53c5d462000-06-28 00:50:50 +1000758 if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) &&
759 (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') &&
760 (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) &&
Damien Miller7a0e5dc2000-07-11 12:15:54 +1000761 (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0)) {
andre2ff7b5d2000-06-03 14:57:40 +0000762 (void)memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
Damien Miller53c5d462000-06-28 00:50:50 +1000763 }
764
andre2ff7b5d2000-06-03 14:57:40 +0000765 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
Damien Miller36ccb5c2000-08-09 16:34:27 +1000766 if (atomicio(write, fd, ut, sizeof(*ut)) != sizeof(*ut))
andre2ff7b5d2000-06-03 14:57:40 +0000767 log("utmp_write_direct: error writing %s: %s",
andre6bb92372000-06-19 08:20:03 +0000768 UTMP_FILE, strerror(errno));
andre2ff7b5d2000-06-03 14:57:40 +0000769
770 (void)close(fd);
771 return 1;
Damien Miller53c5d462000-06-28 00:50:50 +1000772 } else {
andre2ff7b5d2000-06-03 14:57:40 +0000773 return 0;
Damien Miller53c5d462000-06-28 00:50:50 +1000774 }
andre61e67252000-06-04 17:07:49 +0000775}
Damien Millerdd47aa22000-06-27 11:18:27 +1000776# endif /* UTMP_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000777
778static int
andre61e67252000-06-04 17:07:49 +0000779utmp_perform_login(struct logininfo *li)
780{
andre2ff7b5d2000-06-03 14:57:40 +0000781 struct utmp ut;
782
783 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000784# ifdef UTMP_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000785 if (!utmp_write_library(li, &ut)) {
andre6bb92372000-06-19 08:20:03 +0000786 log("utmp_perform_login: utmp_write_library() failed");
andre2ff7b5d2000-06-03 14:57:40 +0000787 return 0;
788 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000789# else
andre2ff7b5d2000-06-03 14:57:40 +0000790 if (!utmp_write_direct(li, &ut)) {
791 log("utmp_perform_login: utmp_write_direct() failed");
792 return 0;
793 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000794# endif
andre2ff7b5d2000-06-03 14:57:40 +0000795 return 1;
andre61e67252000-06-04 17:07:49 +0000796}
andre2ff7b5d2000-06-03 14:57:40 +0000797
798
799static int
andre61e67252000-06-04 17:07:49 +0000800utmp_perform_logout(struct logininfo *li)
801{
andre2ff7b5d2000-06-03 14:57:40 +0000802 struct utmp ut;
803
andre6bb92372000-06-19 08:20:03 +0000804 construct_utmp(li, &ut);
Damien Millerdd47aa22000-06-27 11:18:27 +1000805# ifdef UTMP_USE_LIBRARY
andre6bb92372000-06-19 08:20:03 +0000806 if (!utmp_write_library(li, &ut)) {
807 log("utmp_perform_logout: utmp_write_library() failed");
808 return 0;
809 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000810# else
andre6bb92372000-06-19 08:20:03 +0000811 if (!utmp_write_direct(li, &ut)) {
812 log("utmp_perform_logout: utmp_write_direct() failed");
813 return 0;
814 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000815# endif
andre2ff7b5d2000-06-03 14:57:40 +0000816 return 1;
andre61e67252000-06-04 17:07:49 +0000817}
andre2ff7b5d2000-06-03 14:57:40 +0000818
819
820int
andre61e67252000-06-04 17:07:49 +0000821utmp_write_entry(struct logininfo *li)
822{
andre2ff7b5d2000-06-03 14:57:40 +0000823 switch(li->type) {
824 case LTYPE_LOGIN:
825 return utmp_perform_login(li);
826
827 case LTYPE_LOGOUT:
828 return utmp_perform_logout(li);
829
830 default:
831 log("utmp_write_entry: invalid type field");
832 return 0;
833 }
andre61e67252000-06-04 17:07:49 +0000834}
Damien Millerdd47aa22000-06-27 11:18:27 +1000835#endif /* USE_UTMP */
andre2ff7b5d2000-06-03 14:57:40 +0000836
837
838/**
andre61e67252000-06-04 17:07:49 +0000839 ** Low-level utmpx functions
andre2ff7b5d2000-06-03 14:57:40 +0000840 **/
841
842/* not much point if we don't want utmpx entries */
843#ifdef USE_UTMPX
844
andre2ff7b5d2000-06-03 14:57:40 +0000845/* if we have the wherewithall, use pututxline etc. */
Damien Millerdd47aa22000-06-27 11:18:27 +1000846# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
847 defined(HAVE_PUTUTXLINE)
andre2ff7b5d2000-06-03 14:57:40 +0000848# define UTMPX_USE_LIBRARY
Damien Millerdd47aa22000-06-27 11:18:27 +1000849# endif
andre2ff7b5d2000-06-03 14:57:40 +0000850
851
852/* write a utmpx entry with the system's help (pututxline() and pals) */
Damien Millerdd47aa22000-06-27 11:18:27 +1000853# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000854static int
andre61e67252000-06-04 17:07:49 +0000855utmpx_write_library(struct logininfo *li, struct utmpx *utx)
856{
andre2ff7b5d2000-06-03 14:57:40 +0000857 setutxent();
858 pututxline(utx);
859
Damien Millerdd47aa22000-06-27 11:18:27 +1000860# ifdef HAVE_ENDUTXENT
andre2ff7b5d2000-06-03 14:57:40 +0000861 endutxent();
Damien Millerdd47aa22000-06-27 11:18:27 +1000862# endif
andre2ff7b5d2000-06-03 14:57:40 +0000863 return 1;
andre61e67252000-06-04 17:07:49 +0000864}
andre2ff7b5d2000-06-03 14:57:40 +0000865
Damien Millerdd47aa22000-06-27 11:18:27 +1000866# else /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000867
868/* write a utmp entry direct to the file */
869static int
andre61e67252000-06-04 17:07:49 +0000870utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
871{
andre2ff7b5d2000-06-03 14:57:40 +0000872 log("utmpx_write_direct: not implemented!");
873 return 0;
andre61e67252000-06-04 17:07:49 +0000874}
Damien Millerdd47aa22000-06-27 11:18:27 +1000875# endif /* UTMPX_USE_LIBRARY */
andre2ff7b5d2000-06-03 14:57:40 +0000876
877static int
andre61e67252000-06-04 17:07:49 +0000878utmpx_perform_login(struct logininfo *li)
879{
andre2ff7b5d2000-06-03 14:57:40 +0000880 struct utmpx utx;
881
882 construct_utmpx(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +1000883# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000884 if (!utmpx_write_library(li, &utx)) {
885 log("utmpx_perform_login: utmp_write_library() failed");
886 return 0;
887 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000888# else
andre2ff7b5d2000-06-03 14:57:40 +0000889 if (!utmpx_write_direct(li, &ut)) {
890 log("utmpx_perform_login: utmp_write_direct() failed");
891 return 0;
892 }
Damien Millerdd47aa22000-06-27 11:18:27 +1000893# endif
andre2ff7b5d2000-06-03 14:57:40 +0000894 return 1;
andre61e67252000-06-04 17:07:49 +0000895}
andre2ff7b5d2000-06-03 14:57:40 +0000896
897
898static int
andre61e67252000-06-04 17:07:49 +0000899utmpx_perform_logout(struct logininfo *li)
900{
andre2ff7b5d2000-06-03 14:57:40 +0000901 struct utmpx utx;
902
903 memset(&utx, '\0', sizeof(utx));
904 set_utmpx_time(li, &utx);
905 line_stripname(utx.ut_line, li->line, sizeof(utx.ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +1000906# ifdef HAVE_ID_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000907 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
Damien Millerdd47aa22000-06-27 11:18:27 +1000908# endif
909# ifdef HAVE_TYPE_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +0000910 utx.ut_type = DEAD_PROCESS;
Damien Millerdd47aa22000-06-27 11:18:27 +1000911# endif
andre2ff7b5d2000-06-03 14:57:40 +0000912
Damien Millerdd47aa22000-06-27 11:18:27 +1000913# ifdef UTMPX_USE_LIBRARY
andre2ff7b5d2000-06-03 14:57:40 +0000914 utmpx_write_library(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +1000915# else
andre2ff7b5d2000-06-03 14:57:40 +0000916 utmpx_write_direct(li, &utx);
Damien Millerdd47aa22000-06-27 11:18:27 +1000917# endif
andre2ff7b5d2000-06-03 14:57:40 +0000918 return 1;
andre61e67252000-06-04 17:07:49 +0000919}
andre2ff7b5d2000-06-03 14:57:40 +0000920
andre2ff7b5d2000-06-03 14:57:40 +0000921int
andre61e67252000-06-04 17:07:49 +0000922utmpx_write_entry(struct logininfo *li)
923{
andre2ff7b5d2000-06-03 14:57:40 +0000924 switch(li->type) {
925 case LTYPE_LOGIN:
926 return utmpx_perform_login(li);
927 case LTYPE_LOGOUT:
928 return utmpx_perform_logout(li);
929 default:
930 log("utmpx_write_entry: invalid type field");
931 return 0;
932 }
andre61e67252000-06-04 17:07:49 +0000933}
Damien Millerdd47aa22000-06-27 11:18:27 +1000934#endif /* USE_UTMPX */
andre2ff7b5d2000-06-03 14:57:40 +0000935
936
937/**
andre61e67252000-06-04 17:07:49 +0000938 ** Low-level wtmp functions
andre2ff7b5d2000-06-03 14:57:40 +0000939 **/
940
941#ifdef USE_WTMP
942
andre2ff7b5d2000-06-03 14:57:40 +0000943/* write a wtmp entry direct to the end of the file */
andre61e67252000-06-04 17:07:49 +0000944/* This is a slight modification of code in OpenBSD's logwtmp.c */
andre2ff7b5d2000-06-03 14:57:40 +0000945static int
andre61e67252000-06-04 17:07:49 +0000946wtmp_write(struct logininfo *li, struct utmp *ut)
947{
andre2ff7b5d2000-06-03 14:57:40 +0000948 struct stat buf;
949 int fd, ret = 1;
950
951 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
952 log("wtmp_write: problem writing %s: %s",
953 WTMP_FILE, strerror(errno));
954 return 0;
955 }
andre61e67252000-06-04 17:07:49 +0000956 if (fstat(fd, &buf) == 0)
Damien Miller53c5d462000-06-28 00:50:50 +1000957 if (atomicio(write, fd, ut, sizeof(*ut)) != sizeof(*ut)) {
andre2ff7b5d2000-06-03 14:57:40 +0000958 ftruncate(fd, buf.st_size);
959 log("wtmp_write: problem writing %s: %s",
960 WTMP_FILE, strerror(errno));
961 ret = 0;
962 }
963 (void)close(fd);
andre2ff7b5d2000-06-03 14:57:40 +0000964 return ret;
andre61e67252000-06-04 17:07:49 +0000965}
andre2ff7b5d2000-06-03 14:57:40 +0000966
andre2ff7b5d2000-06-03 14:57:40 +0000967static int
Damien Millerdd47aa22000-06-27 11:18:27 +1000968wtmp_perform_login(struct logininfo *li)
969{
andre2ff7b5d2000-06-03 14:57:40 +0000970 struct utmp ut;
971
972 construct_utmp(li, &ut);
973 return wtmp_write(li, &ut);
andre61e67252000-06-04 17:07:49 +0000974}
andre2ff7b5d2000-06-03 14:57:40 +0000975
976
977static int
andre61e67252000-06-04 17:07:49 +0000978wtmp_perform_logout(struct logininfo *li)
979{
andre2ff7b5d2000-06-03 14:57:40 +0000980 struct utmp ut;
981
982 construct_utmp(li, &ut);
andre2ff7b5d2000-06-03 14:57:40 +0000983 return wtmp_write(li, &ut);
andre61e67252000-06-04 17:07:49 +0000984}
andre2ff7b5d2000-06-03 14:57:40 +0000985
986
987int
andre61e67252000-06-04 17:07:49 +0000988wtmp_write_entry(struct logininfo *li)
989{
andre2ff7b5d2000-06-03 14:57:40 +0000990 switch(li->type) {
991 case LTYPE_LOGIN:
992 return wtmp_perform_login(li);
993 case LTYPE_LOGOUT:
994 return wtmp_perform_logout(li);
995 default:
996 log("wtmp_write_entry: invalid type field");
997 return 0;
998 }
andre61e67252000-06-04 17:07:49 +0000999}
andre2ff7b5d2000-06-03 14:57:40 +00001000
1001
andre6bb92372000-06-19 08:20:03 +00001002/* Notes on fetching login data from wtmp/wtmpx
1003 *
1004 * Logouts are usually recorded with (amongst other things) a blank
1005 * username on a given tty line. However, some systems (HP-UX is one)
1006 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1007 *
1008 * Since we're only looking for logins here, we know that the username
1009 * must be set correctly. On systems that leave it in, we check for
1010 * ut_type==USER_PROCESS (indicating a login.)
1011 *
1012 * Portability: Some systems may set something other than USER_PROCESS
1013 * to indicate a login process. I don't know of any as I write. Also,
1014 * it's possible that some systems may both leave the username in
1015 * place and not have ut_type.
1016 */
1017
andre6bb92372000-06-19 08:20:03 +00001018/* return true if this wtmp entry indicates a login */
1019static int
1020wtmp_islogin(struct logininfo *li, struct utmp *ut)
1021{
Damien Miller7a0e5dc2000-07-11 12:15:54 +10001022 if (strncmp(li->username, ut->ut_name,
1023 MIN_SIZEOF(li->username, ut->ut_name)) == 0) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001024# ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001025 if (ut->ut_type & USER_PROCESS)
1026 return 1;
Damien Millerdd47aa22000-06-27 11:18:27 +10001027# else
andre6bb92372000-06-19 08:20:03 +00001028 return 1;
Damien Millerdd47aa22000-06-27 11:18:27 +10001029# endif
andre6bb92372000-06-19 08:20:03 +00001030 }
1031 return 0;
1032}
1033
andre2ff7b5d2000-06-03 14:57:40 +00001034int
andre61e67252000-06-04 17:07:49 +00001035wtmp_get_entry(struct logininfo *li)
1036{
andre2ff7b5d2000-06-03 14:57:40 +00001037 struct stat st;
1038 struct utmp ut;
andre6bb92372000-06-19 08:20:03 +00001039 int fd, found=0;
1040
1041 /* Clear the time entries in our logininfo */
1042 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001043
1044 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
1045 log("wtmp_get_entry: problem opening %s: %s",
1046 WTMP_FILE, strerror(errno));
1047 return 0;
1048 }
andre61e67252000-06-04 17:07:49 +00001049 if (fstat(fd, &st) != 0) {
andre2ff7b5d2000-06-03 14:57:40 +00001050 log("wtmp_get_entry: couldn't stat %s: %s",
1051 WTMP_FILE, strerror(errno));
1052 close(fd);
1053 return 0;
1054 }
andre2ff7b5d2000-06-03 14:57:40 +00001055
andre6bb92372000-06-19 08:20:03 +00001056 /* Seek to the start of the last struct utmp */
Damien Miller348c9b72000-08-15 10:01:22 +10001057 if (lseek(fd, (off_t)(0 - sizeof(struct utmp)), SEEK_END) == -1) {
andre6bb92372000-06-19 08:20:03 +00001058 /* Looks like we've got a fresh wtmp file */
1059 close(fd);
1060 return 0;
1061 }
1062
1063 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001064 if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) {
andre2ff7b5d2000-06-03 14:57:40 +00001065 log("wtmp_get_entry: read of %s failed: %s",
1066 WTMP_FILE, strerror(errno));
1067 close (fd);
1068 return 0;
1069 }
andre6bb92372000-06-19 08:20:03 +00001070 if ( wtmp_islogin(li, &ut) ) {
1071 found = 1;
1072 /* We've already checked for a time in struct
1073 * utmp, in login_getlast(). */
Damien Millerdd47aa22000-06-27 11:18:27 +10001074# ifdef HAVE_TIME_IN_UTMP
andre2ff7b5d2000-06-03 14:57:40 +00001075 li->tv_sec = ut.ut_time;
Damien Millerdd47aa22000-06-27 11:18:27 +10001076# else
andre2ff7b5d2000-06-03 14:57:40 +00001077# if HAVE_TV_IN_UTMP
1078 li->tv_sec = ut.ut_tv.tv_sec;
1079# endif
Damien Millerdd47aa22000-06-27 11:18:27 +10001080# endif
andre6bb92372000-06-19 08:20:03 +00001081 line_fullname(li->line, ut.ut_line,
1082 MIN_SIZEOF(li->line, ut.ut_line));
Damien Millerdd47aa22000-06-27 11:18:27 +10001083# ifdef HAVE_HOST_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001084 strlcpy(li->hostname, ut.ut_host,
1085 MIN_SIZEOF(li->hostname, ut.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001086# endif
andre6bb92372000-06-19 08:20:03 +00001087 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001088 }
andre6bb92372000-06-19 08:20:03 +00001089 /* Seek back 2 x struct utmp */
andre2ff7b5d2000-06-03 14:57:40 +00001090 if (lseek(fd, (off_t)(0-2*sizeof(struct utmp)), SEEK_CUR) == -1) {
andre6bb92372000-06-19 08:20:03 +00001091 /* We've found the start of the file, so quit */
andre2ff7b5d2000-06-03 14:57:40 +00001092 close (fd);
1093 return 0;
1094 }
andre6bb92372000-06-19 08:20:03 +00001095 }
1096
1097 /* We found an entry. Tidy up and return */
1098 close(fd);
andre2ff7b5d2000-06-03 14:57:40 +00001099 return 1;
andre61e67252000-06-04 17:07:49 +00001100}
Damien Millerdd47aa22000-06-27 11:18:27 +10001101# endif /* USE_WTMP */
andre2ff7b5d2000-06-03 14:57:40 +00001102
1103
1104/**
andre61e67252000-06-04 17:07:49 +00001105 ** Low-level wtmpx functions
andre2ff7b5d2000-06-03 14:57:40 +00001106 **/
1107
1108#ifdef USE_WTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001109/* write a wtmpx entry direct to the end of the file */
andre61e67252000-06-04 17:07:49 +00001110/* This is a slight modification of code in OpenBSD's logwtmp.c */
andre2ff7b5d2000-06-03 14:57:40 +00001111static int
andre61e67252000-06-04 17:07:49 +00001112wtmpx_write(struct logininfo *li, struct utmpx *utx)
1113{
andre2ff7b5d2000-06-03 14:57:40 +00001114 struct stat buf;
1115 int fd, ret = 1;
1116
1117 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
1118 log("wtmpx_write: problem opening %s: %s",
1119 WTMPX_FILE, strerror(errno));
1120 return 0;
1121 }
1122
1123 if (fstat(fd, &buf) == 0)
Damien Miller53c5d462000-06-28 00:50:50 +10001124 if (atomicio(write, fd, utx, sizeof(*utx)) != sizeof(*utx)) {
andre2ff7b5d2000-06-03 14:57:40 +00001125 ftruncate(fd, buf.st_size);
1126 log("wtmpx_write: problem writing %s: %s",
1127 WTMPX_FILE, strerror(errno));
1128 ret = 0;
1129 }
1130 (void)close(fd);
1131
1132 return ret;
andre61e67252000-06-04 17:07:49 +00001133}
andre2ff7b5d2000-06-03 14:57:40 +00001134
1135
1136static int
andre61e67252000-06-04 17:07:49 +00001137wtmpx_perform_login(struct logininfo *li)
1138{
andre2ff7b5d2000-06-03 14:57:40 +00001139 struct utmpx utx;
1140
1141 construct_utmpx(li, &utx);
1142 return wtmpx_write(li, &utx);
andre61e67252000-06-04 17:07:49 +00001143}
andre2ff7b5d2000-06-03 14:57:40 +00001144
1145
1146static int
andre61e67252000-06-04 17:07:49 +00001147wtmpx_perform_logout(struct logininfo *li)
1148{
andre2ff7b5d2000-06-03 14:57:40 +00001149 struct utmpx utx;
1150
1151 construct_utmpx(li, &utx);
andre2ff7b5d2000-06-03 14:57:40 +00001152 return wtmpx_write(li, &utx);
andre61e67252000-06-04 17:07:49 +00001153}
andre2ff7b5d2000-06-03 14:57:40 +00001154
1155
1156int
andre61e67252000-06-04 17:07:49 +00001157wtmpx_write_entry(struct logininfo *li)
1158{
andre2ff7b5d2000-06-03 14:57:40 +00001159 switch(li->type) {
1160 case LTYPE_LOGIN:
1161 return wtmpx_perform_login(li);
1162 case LTYPE_LOGOUT:
1163 return wtmpx_perform_logout(li);
1164 default:
1165 log("wtmpx_write_entry: invalid type field");
1166 return 0;
1167 }
andre61e67252000-06-04 17:07:49 +00001168}
andre2ff7b5d2000-06-03 14:57:40 +00001169
andre6bb92372000-06-19 08:20:03 +00001170/* Please see the notes above wtmp_islogin() for information about the
1171 next two functions */
1172
1173/* Return true if this wtmpx entry indicates a login */
1174static int
1175wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1176{
Damien Miller7a0e5dc2000-07-11 12:15:54 +10001177 if ( strncmp(li->username, utx->ut_name,
1178 MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001179# ifdef HAVE_TYPE_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +00001180 if (utx->ut_type == USER_PROCESS)
1181 return 1;
Damien Millerdd47aa22000-06-27 11:18:27 +10001182# else
andre6bb92372000-06-19 08:20:03 +00001183 return 1;
Damien Millerdd47aa22000-06-27 11:18:27 +10001184# endif
andre6bb92372000-06-19 08:20:03 +00001185 }
1186 return 0;
1187}
1188
andre2ff7b5d2000-06-03 14:57:40 +00001189
1190int
andre61e67252000-06-04 17:07:49 +00001191wtmpx_get_entry(struct logininfo *li)
1192{
andre2ff7b5d2000-06-03 14:57:40 +00001193 struct stat st;
1194 struct utmpx utx;
andre6bb92372000-06-19 08:20:03 +00001195 int fd, found=0;
1196
1197 /* Clear the time entries */
1198 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001199
1200 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
1201 log("wtmpx_get_entry: problem opening %s: %s",
1202 WTMPX_FILE, strerror(errno));
1203 return 0;
1204 }
andre61e67252000-06-04 17:07:49 +00001205 if (fstat(fd, &st) != 0) {
andre2ff7b5d2000-06-03 14:57:40 +00001206 log("wtmpx_get_entry: couldn't stat %s: %s",
1207 WTMP_FILE, strerror(errno));
1208 close(fd);
1209 return 0;
1210 }
andre6bb92372000-06-19 08:20:03 +00001211
1212 /* Seek to the start of the last struct utmpx */
1213 if (lseek(fd, (off_t)(0-sizeof(struct utmpx)), SEEK_END) == -1 ) {
1214 /* probably a newly rotated wtmpx file */
1215 close(fd);
1216 return 0;
1217 }
andre2ff7b5d2000-06-03 14:57:40 +00001218
andre6bb92372000-06-19 08:20:03 +00001219 while (!found) {
Damien Miller53c5d462000-06-28 00:50:50 +10001220 if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) {
andre2ff7b5d2000-06-03 14:57:40 +00001221 log("wtmpx_get_entry: read of %s failed: %s",
1222 WTMPX_FILE, strerror(errno));
1223 close (fd);
1224 return 0;
1225 }
andre2ff7b5d2000-06-03 14:57:40 +00001226 /* Logouts are recorded as a blank username on a particular line.
1227 * So, we just need to find the username in struct utmpx */
andre6bb92372000-06-19 08:20:03 +00001228 if ( wtmpx_islogin(li, &utx) ) {
Damien Millerdd47aa22000-06-27 11:18:27 +10001229# ifdef HAVE_TV_IN_UTMPX
andre2ff7b5d2000-06-03 14:57:40 +00001230 li->tv_sec = utx.ut_tv.tv_sec;
Damien Millerdd47aa22000-06-27 11:18:27 +10001231# else
andre2ff7b5d2000-06-03 14:57:40 +00001232# ifdef HAVE_TIME_IN_UTMPX
1233 li->tv_sec = utx.ut_time;
1234# endif
Damien Millerdd47aa22000-06-27 11:18:27 +10001235# endif
Damien Miller1a132252000-06-13 21:23:17 +10001236 line_fullname(li->line, utx.ut_line, sizeof(li->line));
Damien Millerdd47aa22000-06-27 11:18:27 +10001237# ifdef HAVE_HOST_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +00001238 strlcpy(li->hostname, utx.ut_host,
1239 MIN_SIZEOF(li->hostname, utx.ut_host));
Damien Millerdd47aa22000-06-27 11:18:27 +10001240# endif
andre6bb92372000-06-19 08:20:03 +00001241 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001242 }
1243 if (lseek(fd, (off_t)(0-2*sizeof(struct utmpx)), SEEK_CUR) == -1) {
1244 close (fd);
1245 return 0;
1246 }
andre6bb92372000-06-19 08:20:03 +00001247 }
1248
1249 close(fd);
andre2ff7b5d2000-06-03 14:57:40 +00001250 return 1;
andre61e67252000-06-04 17:07:49 +00001251}
Damien Millerd5bf3072000-06-07 21:32:13 +10001252#endif /* USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001253
andre2ff7b5d2000-06-03 14:57:40 +00001254/**
andre61e67252000-06-04 17:07:49 +00001255 ** Low-level libutil login() functions
andre2ff7b5d2000-06-03 14:57:40 +00001256 **/
1257
1258#ifdef USE_LOGIN
andre2ff7b5d2000-06-03 14:57:40 +00001259static int
andre61e67252000-06-04 17:07:49 +00001260syslogin_perform_login(struct logininfo *li)
1261{
andre2ff7b5d2000-06-03 14:57:40 +00001262 struct utmp *ut;
1263
Damien Miller348c9b72000-08-15 10:01:22 +10001264 if (! (ut = (struct utmp *)malloc(sizeof(*ut)))) {
andre2ff7b5d2000-06-03 14:57:40 +00001265 log("syslogin_perform_login: couldn't malloc()");
1266 return 0;
1267 }
1268 construct_utmp(li, ut);
1269 login(ut);
1270
1271 return 1;
andre61e67252000-06-04 17:07:49 +00001272}
1273
andre2ff7b5d2000-06-03 14:57:40 +00001274static int
andre61e67252000-06-04 17:07:49 +00001275syslogin_perform_logout(struct logininfo *li)
1276{
Damien Millerdd47aa22000-06-27 11:18:27 +10001277# ifdef HAVE_LOGOUT
andre2ff7b5d2000-06-03 14:57:40 +00001278 char line[8];
1279
1280 (void)line_stripname(line, li->line, sizeof(line));
1281
1282 if (!logout(line)) {
1283 log("syslogin_perform_logout: logout() returned an error");
Damien Millerdd47aa22000-06-27 11:18:27 +10001284# ifdef HAVE_LOGWTMP
andre2ff7b5d2000-06-03 14:57:40 +00001285 } else {
1286 logwtmp(line, "", "");
Damien Millerdd47aa22000-06-27 11:18:27 +10001287# endif
Damien Miller9b6d4ab2000-07-02 08:43:18 +10001288 }
andre6bb92372000-06-19 08:20:03 +00001289 /* FIXME: (ATL - if the need arises) What to do if we have
1290 * login, but no logout? what if logout but no logwtmp? All
1291 * routines are in libutil so they should all be there,
1292 * but... */
Damien Millerdd47aa22000-06-27 11:18:27 +10001293# endif
andre2ff7b5d2000-06-03 14:57:40 +00001294 return 1;
andre61e67252000-06-04 17:07:49 +00001295}
andre2ff7b5d2000-06-03 14:57:40 +00001296
andre2ff7b5d2000-06-03 14:57:40 +00001297int
andre61e67252000-06-04 17:07:49 +00001298syslogin_write_entry(struct logininfo *li)
1299{
andre2ff7b5d2000-06-03 14:57:40 +00001300 switch (li->type) {
1301 case LTYPE_LOGIN:
1302 return syslogin_perform_login(li);
1303 case LTYPE_LOGOUT:
1304 return syslogin_perform_logout(li);
1305 default:
1306 log("syslogin_write_entry: Invalid type field");
1307 return 0;
1308 }
andre61e67252000-06-04 17:07:49 +00001309}
Damien Millerd5bf3072000-06-07 21:32:13 +10001310#endif /* USE_LOGIN */
andre2ff7b5d2000-06-03 14:57:40 +00001311
1312/* end of file log-syslogin.c */
1313
andre2ff7b5d2000-06-03 14:57:40 +00001314/**
andre61e67252000-06-04 17:07:49 +00001315 ** Low-level lastlog functions
andre2ff7b5d2000-06-03 14:57:40 +00001316 **/
1317
1318#ifdef USE_LASTLOG
Damien Millerdd47aa22000-06-27 11:18:27 +10001319#define LL_FILE 1
1320#define LL_DIR 2
1321#define LL_OTHER 3
andre2ff7b5d2000-06-03 14:57:40 +00001322
andre2ff7b5d2000-06-03 14:57:40 +00001323static void
andre61e67252000-06-04 17:07:49 +00001324lastlog_construct(struct logininfo *li, struct lastlog *last)
1325{
andre2ff7b5d2000-06-03 14:57:40 +00001326 /* clear the structure */
Damien Miller348c9b72000-08-15 10:01:22 +10001327 memset(last, '\0', sizeof(*last));
andre2ff7b5d2000-06-03 14:57:40 +00001328
Damien Millerdd47aa22000-06-27 11:18:27 +10001329 (void)line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
andre6bb92372000-06-19 08:20:03 +00001330 strlcpy(last->ll_host, li->hostname,
1331 MIN_SIZEOF(last->ll_host, li->hostname));
andre2ff7b5d2000-06-03 14:57:40 +00001332 last->ll_time = li->tv_sec;
andre61e67252000-06-04 17:07:49 +00001333}
andre2ff7b5d2000-06-03 14:57:40 +00001334
andre2ff7b5d2000-06-03 14:57:40 +00001335static int
andre61e67252000-06-04 17:07:49 +00001336lastlog_filetype(char *filename)
1337{
andre2ff7b5d2000-06-03 14:57:40 +00001338 struct stat st;
1339
Damien Millerdd47aa22000-06-27 11:18:27 +10001340 if (stat(LASTLOG_FILE, &st) != 0) {
1341 log("lastlog_perform_login: Couldn't stat %s: %s", LASTLOG_FILE,
1342 strerror(errno));
andre2ff7b5d2000-06-03 14:57:40 +00001343 return 0;
1344 }
andre2ff7b5d2000-06-03 14:57:40 +00001345 if (S_ISDIR(st.st_mode))
1346 return LL_DIR;
1347 else if (S_ISREG(st.st_mode))
1348 return LL_FILE;
1349 else
1350 return LL_OTHER;
andre61e67252000-06-04 17:07:49 +00001351}
andre2ff7b5d2000-06-03 14:57:40 +00001352
1353
1354/* open the file (using filemode) and seek to the login entry */
1355static int
andre61e67252000-06-04 17:07:49 +00001356lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1357{
andre2ff7b5d2000-06-03 14:57:40 +00001358 off_t offset;
1359 int type;
1360 char lastlog_file[1024];
1361
1362 type = lastlog_filetype(LASTLOG_FILE);
1363 switch (type) {
Damien Millerf8af08d2000-06-27 09:40:06 +10001364 case LL_FILE:
1365 strlcpy(lastlog_file, LASTLOG_FILE, sizeof(lastlog_file));
1366 break;
1367 case LL_DIR:
1368 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1369 LASTLOG_FILE, li->username);
1370 break;
1371 default:
1372 log("lastlog_openseek: %.100s is not a file or directory!",
1373 LASTLOG_FILE);
1374 return 0;
Damien Millerdd47aa22000-06-27 11:18:27 +10001375 }
andre2ff7b5d2000-06-03 14:57:40 +00001376
1377 *fd = open(lastlog_file, filemode);
1378 if ( *fd < 0) {
Damien Miller53c5d462000-06-28 00:50:50 +10001379 debug("lastlog_openseek: Couldn't open %s: %s",
andre2ff7b5d2000-06-03 14:57:40 +00001380 lastlog_file, strerror(errno));
1381 return 0;
1382 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001383
Damien Millere477ef62000-08-15 10:21:17 +10001384 if (type == LL_FILE) {
1385 /* find this uid's offset in the lastlog file */
1386 offset = (off_t) ( (long)li->uid * sizeof(struct lastlog));
andre2ff7b5d2000-06-03 14:57:40 +00001387
Damien Millere477ef62000-08-15 10:21:17 +10001388 if ( lseek(*fd, offset, SEEK_SET) != offset ) {
1389 log("lastlog_openseek: %s->lseek(): %s",
1390 lastlog_file, strerror(errno));
1391 return 0;
1392 }
andre2ff7b5d2000-06-03 14:57:40 +00001393 }
Damien Millere477ef62000-08-15 10:21:17 +10001394
andre2ff7b5d2000-06-03 14:57:40 +00001395 return 1;
andre61e67252000-06-04 17:07:49 +00001396}
andre2ff7b5d2000-06-03 14:57:40 +00001397
1398static int
andre61e67252000-06-04 17:07:49 +00001399lastlog_perform_login(struct logininfo *li)
1400{
andre2ff7b5d2000-06-03 14:57:40 +00001401 struct lastlog last;
1402 int fd;
1403
1404 /* create our struct lastlog */
1405 lastlog_construct(li, &last);
1406
Damien Millerc1132e72000-08-18 14:08:38 +10001407 if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT))
1408 return(0);
1409
andre2ff7b5d2000-06-03 14:57:40 +00001410 /* write the entry */
Damien Millerc1132e72000-08-18 14:08:38 +10001411 if (atomicio(write, fd, &last, sizeof(last)) != sizeof(last)) {
1412 close(fd);
1413 log("lastlog_write_filemode: Error writing to %s: %s",
1414 LASTLOG_FILE, strerror(errno));
andre2ff7b5d2000-06-03 14:57:40 +00001415 return 0;
Damien Millerdd47aa22000-06-27 11:18:27 +10001416 }
Damien Millerc1132e72000-08-18 14:08:38 +10001417
1418 close(fd);
1419 return 1;
andre61e67252000-06-04 17:07:49 +00001420}
andre2ff7b5d2000-06-03 14:57:40 +00001421
andre2ff7b5d2000-06-03 14:57:40 +00001422int
andre61e67252000-06-04 17:07:49 +00001423lastlog_write_entry(struct logininfo *li)
1424{
andre2ff7b5d2000-06-03 14:57:40 +00001425 switch(li->type) {
1426 case LTYPE_LOGIN:
1427 return lastlog_perform_login(li);
1428 default:
1429 log("lastlog_write_entry: Invalid type field");
1430 return 0;
1431 }
andre61e67252000-06-04 17:07:49 +00001432}
andre2ff7b5d2000-06-03 14:57:40 +00001433
andre2ff7b5d2000-06-03 14:57:40 +00001434static void
andre61e67252000-06-04 17:07:49 +00001435lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1436{
andre2ff7b5d2000-06-03 14:57:40 +00001437 line_fullname(li->line, last->ll_line, sizeof(li->line));
Damien Millerdd47aa22000-06-27 11:18:27 +10001438 strlcpy(li->hostname, last->ll_host,
andre6bb92372000-06-19 08:20:03 +00001439 MIN_SIZEOF(li->hostname, last->ll_host));
andre2ff7b5d2000-06-03 14:57:40 +00001440 li->tv_sec = last->ll_time;
andre61e67252000-06-04 17:07:49 +00001441}
andre2ff7b5d2000-06-03 14:57:40 +00001442
andre2ff7b5d2000-06-03 14:57:40 +00001443int
andre61e67252000-06-04 17:07:49 +00001444lastlog_get_entry(struct logininfo *li)
1445{
andre2ff7b5d2000-06-03 14:57:40 +00001446 struct lastlog last;
1447 int fd;
1448
1449 if (lastlog_openseek(li, &fd, O_RDONLY)) {
Damien Miller53c5d462000-06-28 00:50:50 +10001450 if (atomicio(read, fd, &last, sizeof(last)) != sizeof(last)) {
1451 log("lastlog_get_entry: Error reading from %s: %s",
andre2ff7b5d2000-06-03 14:57:40 +00001452 LASTLOG_FILE, strerror(errno));
1453 return 0;
1454 } else {
1455 lastlog_populate_entry(li, &last);
1456 return 1;
1457 }
Damien Millerdd47aa22000-06-27 11:18:27 +10001458 } else {
andre2ff7b5d2000-06-03 14:57:40 +00001459 return 0;
Damien Millerdd47aa22000-06-27 11:18:27 +10001460 }
andre61e67252000-06-04 17:07:49 +00001461}
Damien Millerd5bf3072000-06-07 21:32:13 +10001462#endif /* USE_LASTLOG */