blob: 8df2e78e21b043b4c39243237dcdea0c26d2e643 [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:
andre61e67252000-06-04 17:07:49 +0000135 ** homegrown ttyslot()q
136 ** test, test, test
andre2ff7b5d2000-06-03 14:57:40 +0000137 **
138 ** Platform status:
139 ** ----------------
140 **
141 ** Known good:
142 ** Linux (Redhat 6.2, need more variants)
143 ** HP-UX 10.20 (gcc only)
andre6bb92372000-06-19 08:20:03 +0000144 ** IRIX
andre2ff7b5d2000-06-03 14:57:40 +0000145 **
146 ** Testing required: Please send reports!
147 ** Solaris
andre2ff7b5d2000-06-03 14:57:40 +0000148 ** NetBSD
149 ** HP-UX 11
andre60f3c982000-06-03 16:18:19 +0000150 ** AIX
andre2ff7b5d2000-06-03 14:57:40 +0000151 **
152 ** Platforms with known problems:
andre2ff7b5d2000-06-03 14:57:40 +0000153 ** NeXT
154 **
155 **/
156
157#include "includes.h"
158
andre61e67252000-06-04 17:07:49 +0000159#if HAVE_UTMP_H
160# include <utmp.h>
161#endif
162#if HAVE_UTMPX_H
163# include <utmpx.h>
164#endif
165#if HAVE_LASTLOG_H
166# include <lastlog.h>
167#endif
andre2ff7b5d2000-06-03 14:57:40 +0000168
169#include "ssh.h"
170#include "xmalloc.h"
171#include "loginrec.h"
172
andre98cabe02000-06-19 09:11:30 +0000173RCSID("$Id: loginrec.c,v 1.8 2000/06/19 09:11:30 andre Exp $");
andre2ff7b5d2000-06-03 14:57:40 +0000174
175/**
176 ** prototypes for helper functions in this file
177 **/
178
179#if HAVE_UTMP_H
andre2ff7b5d2000-06-03 14:57:40 +0000180void set_utmp_time(struct logininfo *li, struct utmp *ut);
181void construct_utmp(struct logininfo *li, struct utmp *ut);
182#endif
183
184#ifdef HAVE_UTMPX_H
andre2ff7b5d2000-06-03 14:57:40 +0000185void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
186void construct_utmpx(struct logininfo *li, struct utmpx *ut);
187#endif
188
189int utmp_write_entry(struct logininfo *li);
190int utmpx_write_entry(struct logininfo *li);
191int wtmp_write_entry(struct logininfo *li);
192int wtmpx_write_entry(struct logininfo *li);
193int lastlog_write_entry(struct logininfo *li);
194int syslogin_write_entry(struct logininfo *li);
195
196int getlast_entry(struct logininfo *li);
197int lastlog_get_entry(struct logininfo *li);
198int wtmp_get_entry(struct logininfo *li);
199int wtmpx_get_entry(struct logininfo *li);
200
201
andre6bb92372000-06-19 08:20:03 +0000202#ifdef MIN
203# undef MIN
204# define MIN(a,b) ( (a)<(b) ? (a) : (b) )
205#endif
206
207/* pick the shortest string */
208#define MIN_SIZEOF(s1,s2) ( sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2) )
209
210
andre2ff7b5d2000-06-03 14:57:40 +0000211/**
212 ** platform-independent login functions
213 **/
214
andre6bb92372000-06-19 08:20:03 +0000215/* login_login(struct logininfo *) -Record a login
216 *
217 * Call with a pointer to a struct logininfo initialised with
218 * login_init_entry() or login_alloc_entry()
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_login (struct logininfo *li)
226{
227 li->type = LTYPE_LOGIN;
228 return login_write(li);
229}
230
231
andre6bb92372000-06-19 08:20:03 +0000232/* login_logout(struct logininfo *) - Record a logout
233 *
234 * Call as with login_login()
235 *
236 * Returns:
237 * >0 if successful
238 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
239 */
andre61e67252000-06-04 17:07:49 +0000240int
241login_logout(struct logininfo *li)
242{
243 li->type = LTYPE_LOGOUT;
244 return login_write(li);
245}
246
247
andre6bb92372000-06-19 08:20:03 +0000248/* login_get_lastlog_time(int) - Retrieve the last login time
249 *
250 * Retrieve the last login time for the given uid. Will try to use the
251 * system lastlog facilities if they are available, but will fall back
252 * to looking in wtmp/wtmpx if necessary
253 *
254 * Returns:
255 * 0 on failure, or if user has never logged in
256 * Time in seconds from the epoch if successful
257 *
258 * Useful preprocessor symbols:
259 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog
260 * info
261 * USE_LASTLOG: If set, indicates the presence of system lastlog
262 * facilities. If this and DISABLE_LASTLOG are not set,
263 * try to retrieve lastlog information from wtmp/wtmpx.
264 */
andre61e67252000-06-04 17:07:49 +0000265unsigned int
266login_get_lastlog_time(const int uid)
267{
268 struct logininfo li;
269
andre6bb92372000-06-19 08:20:03 +0000270 if (login_get_lastlog(&li, uid))
271 return li.tv_sec;
272 else
273 return 0;
andre61e67252000-06-04 17:07:49 +0000274}
275
andre6bb92372000-06-19 08:20:03 +0000276/* login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry
277 *
278 * Retrieve a logininfo structure populated (only partially) with
279 * information from the system lastlog data, or from wtmp/wtmpx if no
280 * system lastlog information exists.
281 *
282 * Note this routine must be given a pre-allocated logininfo.
283 *
284 * Returns:
285 * >0: A pointer to your struct logininfo if successful
286 * 0 on failure (will use OpenSSH's logging facilities for diagnostics)
287 *
288 */
andre61e67252000-06-04 17:07:49 +0000289struct logininfo *
290login_get_lastlog(struct logininfo *li, const int uid)
291{
andre6bb92372000-06-19 08:20:03 +0000292#ifndef USE_LASTLOG
293 struct passwd *pw;
294#endif
295
andre61e67252000-06-04 17:07:49 +0000296 memset(li, '\0', sizeof(struct logininfo));
297 li->uid = uid;
andre6bb92372000-06-19 08:20:03 +0000298
299#ifndef USE_LASTLOG
300 /* If we don't have a 'real' lastlog, we need the username to
301 * reliably search wtmp(x) for the last login (see
302 * wtmp_get_entry().) */
303 pw = getpwuid(uid);
andre98cabe02000-06-19 09:11:30 +0000304 /* No MIN_SIZEOF here - we absolutely *must not* truncate the
305 * username */
306 strlcpy(li->username, pw->pw_name, li->username);
andre6bb92372000-06-19 08:20:03 +0000307#endif
andre61e67252000-06-04 17:07:49 +0000308 if (getlast_entry(li))
309 return li;
310 else
311 return 0;
312}
313
314
andre6bb92372000-06-19 08:20:03 +0000315/* login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
316 * a logininfo structure
317 *
318 * This function creates a new struct logininfo, a data structure
319 * meant to carry the information required to portably record login info.
320 *
321 * Returns a pointer to a newly created struct logininfo. If memory
322 * allocation fails, the program halts.
323 */
andre61e67252000-06-04 17:07:49 +0000324struct
325logininfo *login_alloc_entry(int pid, const char *username,
326 const char *hostname, const char *line)
327{
andre2ff7b5d2000-06-03 14:57:40 +0000328 struct logininfo *newli;
329
330 newli = (struct logininfo *) xmalloc (sizeof(struct logininfo));
andre61e67252000-06-04 17:07:49 +0000331 (void)login_init_entry(newli, pid, username, hostname, line);
332 return newli;
333}
andre2ff7b5d2000-06-03 14:57:40 +0000334
335
andre6bb92372000-06-19 08:20:03 +0000336/* login_free_entry(struct logininfo *) - free struct memory */
andre61e67252000-06-04 17:07:49 +0000337void
338login_free_entry(struct logininfo *li)
339{
340 xfree(li);
341}
342
andre2ff7b5d2000-06-03 14:57:40 +0000343
andre6bb92372000-06-19 08:20:03 +0000344/* login_init_entry(struct logininfo *, int, char*, char*, char*)
345 * - initialise a struct logininfo
346 *
347 * Populates a new struct logininfo, a data structure meant to carry
348 * the information required to portably record login info.
349 *
350 * Returns: 1
351 */
andre61e67252000-06-04 17:07:49 +0000352int
353login_init_entry(struct logininfo *li, int pid, const char *username,
354 const char *hostname, const char *line)
355{
andre2ff7b5d2000-06-03 14:57:40 +0000356 /* zero the structure */
357 memset(li, 0, sizeof(struct logininfo));
358
andre61e67252000-06-04 17:07:49 +0000359 li->pid = pid;
andre2ff7b5d2000-06-03 14:57:40 +0000360 /* set the line information */
andre61e67252000-06-04 17:07:49 +0000361 if (line)
andre2ff7b5d2000-06-03 14:57:40 +0000362 line_fullname(li->line, line, sizeof(li->line));
andre2ff7b5d2000-06-03 14:57:40 +0000363
andre61e67252000-06-04 17:07:49 +0000364 if (username)
andre2ff7b5d2000-06-03 14:57:40 +0000365 strlcpy(li->username, username, sizeof(li->username));
andre61e67252000-06-04 17:07:49 +0000366 if (hostname)
andre2ff7b5d2000-06-03 14:57:40 +0000367 strlcpy(li->hostname, hostname, sizeof(li->hostname));
andre61e67252000-06-04 17:07:49 +0000368 return 1;
andre2ff7b5d2000-06-03 14:57:40 +0000369}
370
andre6bb92372000-06-19 08:20:03 +0000371/* login_set_current_time(struct logininfo *) - set the current time
372 *
373 * Set the current time in a logininfo structure. This function is
374 * meant to eliminate the need to deal with system dependencies for
375 * time handling.
376 */
andre2ff7b5d2000-06-03 14:57:40 +0000377void
andre61e67252000-06-04 17:07:49 +0000378login_set_current_time(struct logininfo *li)
379{
andre2ff7b5d2000-06-03 14:57:40 +0000380#ifdef HAVE_SYS_TIME_H
381 struct timeval tv;
382
383 gettimeofday(&tv, NULL);
384 li->tv_sec = tv.tv_sec ; li->tv_usec = tv.tv_usec;
385#else
andre61e67252000-06-04 17:07:49 +0000386 time_t tm = time(0);
387
388 li->tv_sec = tm; li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +0000389#endif
390}
391
andre61e67252000-06-04 17:07:49 +0000392
393/* copy a sockaddr_* into our logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000394void
andre61e67252000-06-04 17:07:49 +0000395login_set_addr(struct logininfo *li, const struct sockaddr *sa,
396 const unsigned int sa_size)
397{
398 unsigned int bufsize = sa_size;
399
400 /* make sure we don't overrun our union */
401 if (sizeof(li->hostaddr) < sa_size)
402 bufsize = sizeof(li->hostaddr);
403
404 memcpy((void *)&(li->hostaddr.sa), (const void *)sa, bufsize);
andre2ff7b5d2000-06-03 14:57:40 +0000405}
406
andre2ff7b5d2000-06-03 14:57:40 +0000407
andre61e67252000-06-04 17:07:49 +0000408/**
409 ** login_write: Call low-level recording functions based on autoconf
410 ** results
411 **/
andre2ff7b5d2000-06-03 14:57:40 +0000412
413int
andre61e67252000-06-04 17:07:49 +0000414login_write (struct logininfo *li)
415{
andre2ff7b5d2000-06-03 14:57:40 +0000416 if ((int)geteuid() != 0) {
417 log("Attempt to write login records by non-root user (aborting)");
418 return 1;
419 }
420 /* set the timestamp */
421 login_set_current_time(li);
422#ifdef USE_LOGIN
423 syslogin_write_entry(li);
424#endif
425#ifdef USE_LASTLOG
426 if (li->type == LTYPE_LOGIN) {
427 lastlog_write_entry(li);
428 }
429#endif
430#ifdef USE_UTMP
431 utmp_write_entry(li);
432#endif
433#ifdef USE_WTMP
434 wtmp_write_entry(li);
435#endif
436#ifdef USE_UTMPX
437 utmpx_write_entry(li);
438#endif
439#ifdef USE_WTMPX
440 wtmpx_write_entry(li);
441#endif
442 return 0;
443}
444
andre2ff7b5d2000-06-03 14:57:40 +0000445
446/**
andre61e67252000-06-04 17:07:49 +0000447 ** getlast_entry: Call low-level functions to retrieve the last login
448 ** time.
andre2ff7b5d2000-06-03 14:57:40 +0000449 **/
450
andre61e67252000-06-04 17:07:49 +0000451/* take the uid in li and return the last login time */
452int
453getlast_entry(struct logininfo *li)
454{
455#ifdef USE_LASTLOG
456 if (lastlog_get_entry(li))
457 return 1;
458 else
459 return 0;
460#else
461 /* !USE_LASTLOG */
462
andreecaabf12000-06-12 22:21:44 +0000463# ifdef DISABLE_LASTLOG
464 /* On some systems we shouldn't even try to obtain last login
465 * time, e.g. AIX */
466 return 0;
467
468# else
andre61e67252000-06-04 17:07:49 +0000469 /* Try to retrieve the last login time from wtmp */
andreecaabf12000-06-12 22:21:44 +0000470# if defined(USE_WTMP) && (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
andre61e67252000-06-04 17:07:49 +0000471 /* retrieve last login time from utmp */
472 if (wtmp_get_entry(li))
473 return 1;
474 else
475 return 0;
andreecaabf12000-06-12 22:21:44 +0000476# else
andre61e67252000-06-04 17:07:49 +0000477
478 /* If wtmp isn't available, try wtmpx */
479
andreecaabf12000-06-12 22:21:44 +0000480# if defined(USE_WTMPX) && (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
andre61e67252000-06-04 17:07:49 +0000481 /* retrieve last login time from utmpx */
482 if (wtmpx_get_entry(li))
483 return 1;
484 else
485 return 0;
andreecaabf12000-06-12 22:21:44 +0000486# else
andre61e67252000-06-04 17:07:49 +0000487
488 /* Give up: No means of retrieving last login time */
489 return 0;
andreecaabf12000-06-12 22:21:44 +0000490# endif
491 /* USE_WTMPX && (HAVE_TIME_IN_UTMPX || HAVE_TV_IN_UTMPX) */
492
andre61e67252000-06-04 17:07:49 +0000493# endif
andreecaabf12000-06-12 22:21:44 +0000494 /* USE_WTMP && (HAVE_TIME_IN_UTMP || HAVE_TV_IN_UTMP) */
andre61e67252000-06-04 17:07:49 +0000495# endif
andreecaabf12000-06-12 22:21:44 +0000496 /* DISABLE_LASTLOG */
andre61e67252000-06-04 17:07:49 +0000497#endif
498/* USE_LASTLOG */
499}
500
501
502
andre2ff7b5d2000-06-03 14:57:40 +0000503/*
andre61e67252000-06-04 17:07:49 +0000504 * 'line' string utility functions
505 *
506 * These functions process the 'line' string into one of three forms:
507 *
andre2ff7b5d2000-06-03 14:57:40 +0000508 * 1. The full filename (including '/dev')
509 * 2. The stripped name (excluding '/dev')
andre61e67252000-06-04 17:07:49 +0000510 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
511 * /dev/pts/1 -> ts/1 )
andre2ff7b5d2000-06-03 14:57:40 +0000512 *
513 * Form 3 is used on some systems to identify a .tmp.? entry when
514 * attempting to remove it. Typically both addition and removal is
andre61e67252000-06-04 17:07:49 +0000515 * performed by one application - say, sshd - so as long as the choice
516 * uniquely identifies a terminal it's ok.
andre2ff7b5d2000-06-03 14:57:40 +0000517 */
518
519
andre61e67252000-06-04 17:07:49 +0000520/* line_fullname(): add the leading '/dev/' if it doesn't exist make
521 * sure dst has enough space, if not just copy src (ugh) */
andre2ff7b5d2000-06-03 14:57:40 +0000522char *
andre61e67252000-06-04 17:07:49 +0000523line_fullname(char *dst, const char *src, int dstsize)
524{
andre2ff7b5d2000-06-03 14:57:40 +0000525 memset(dst, '\0', dstsize);
526 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
527 strlcpy(dst, src, dstsize);
528 else {
Damien Miller1a132252000-06-13 21:23:17 +1000529 strlcpy(dst, "/dev/", dstsize);
andre2ff7b5d2000-06-03 14:57:40 +0000530 strlcat(dst, src, dstsize);
531 }
532 return dst;
533}
534
andre61e67252000-06-04 17:07:49 +0000535
536/* line_stripname(): strip the leading '/dev' if it exists, return dst */
andre2ff7b5d2000-06-03 14:57:40 +0000537char *
andre61e67252000-06-04 17:07:49 +0000538line_stripname(char *dst, const char *src, int dstsize)
539{
andre2ff7b5d2000-06-03 14:57:40 +0000540 memset(dst, '\0', dstsize);
541 if (strncmp(src, "/dev/", 5) == 0)
542 strlcpy(dst, &src[5], dstsize);
543 else
544 strlcpy(dst, src, dstsize);
545 return dst;
andre61e67252000-06-04 17:07:49 +0000546}
547
andre2ff7b5d2000-06-03 14:57:40 +0000548
andre61e67252000-06-04 17:07:49 +0000549/* line_abbrevname(): Return the abbreviated (usually four-character)
550 * form of the line (Just use the last <dstsize> characters of the
551 * full name.)
552 *
553 * NOTE: use strncpy because we do NOT necessarily want zero
554 * termination */
andre2ff7b5d2000-06-03 14:57:40 +0000555char *
556line_abbrevname(char *dst, const char *src, int dstsize) {
557 memset(dst, '\0', dstsize);
andre61e67252000-06-04 17:07:49 +0000558 src += (strlen(src) - dstsize);
559 strncpy(dst, src, dstsize); /* note: _don't_ change this to strlcpy */
andre2ff7b5d2000-06-03 14:57:40 +0000560 return dst;
561}
562
563
564/**
565 ** utmp utility functions
andre61e67252000-06-04 17:07:49 +0000566 **
567 ** These functions manipulate struct utmp, taking system differences
568 ** into account.
andre2ff7b5d2000-06-03 14:57:40 +0000569 **/
570
571#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
572
andre2ff7b5d2000-06-03 14:57:40 +0000573/* build the utmp structure */
574void
andre61e67252000-06-04 17:07:49 +0000575set_utmp_time(struct logininfo *li, struct utmp *ut)
576{
andre2ff7b5d2000-06-03 14:57:40 +0000577#ifdef HAVE_TV_IN_UTMP
578 ut->ut_tv.tv_sec = li->tv_sec;
579 ut->ut_tv.tv_usec = li->tv_usec;
580#else
581# ifdef HAVE_TIME_IN_UTMP
582 ut->ut_time = li->tv_sec;
583# endif
584#endif
585}
586
andre61e67252000-06-04 17:07:49 +0000587
andre2ff7b5d2000-06-03 14:57:40 +0000588void
589construct_utmp(struct logininfo *li,
andre61e67252000-06-04 17:07:49 +0000590 struct utmp *ut)
591{
andre2ff7b5d2000-06-03 14:57:40 +0000592 memset(ut, '\0', sizeof(struct utmp));
andre6bb92372000-06-19 08:20:03 +0000593
594 /* First fill out fields used for both logins and logouts */
595
andre2ff7b5d2000-06-03 14:57:40 +0000596#ifdef HAVE_ID_IN_UTMP
597 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
598#endif
599
600#ifdef HAVE_TYPE_IN_UTMP
andre6bb92372000-06-19 08:20:03 +0000601 /* This is done here to keep utmp constants out of struct logininfo */
andre2ff7b5d2000-06-03 14:57:40 +0000602 switch (li->type) {
603 case LTYPE_LOGIN:
604 ut->ut_type = USER_PROCESS;
605 break;
606 case LTYPE_LOGOUT:
607 ut->ut_type = DEAD_PROCESS;
608 break;
609 }
610#endif
andre6bb92372000-06-19 08:20:03 +0000611 set_utmp_time(li, ut);
andre2ff7b5d2000-06-03 14:57:40 +0000612
andre6bb92372000-06-19 08:20:03 +0000613 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
andre2ff7b5d2000-06-03 14:57:40 +0000614#ifdef HAVE_PID_IN_UTMP
615 ut->ut_pid = li->pid;
616#endif
andre6bb92372000-06-19 08:20:03 +0000617
618 /* If we're logging out, leave all other fields blank */
619 if (li->type == LTYPE_LOGOUT)
620 return;
621
622 /* These fields are only used when logging in, and are blank
623 * for logouts. */
624
625 /* Use strncpy because we don't necessarily want null termination */
626 strncpy(ut->ut_user, li->username, MIN_SIZEOF(ut->ut_user, li->username));
andre2ff7b5d2000-06-03 14:57:40 +0000627#ifdef HAVE_HOST_IN_UTMP
andre6bb92372000-06-19 08:20:03 +0000628 strncpy(ut->ut_host, li->hostname, MIN_SIZEOF(ut->ut_host, li->hostname));
andre2ff7b5d2000-06-03 14:57:40 +0000629#endif
630#ifdef HAVE_ADDR_IN_UTMP
andre61e67252000-06-04 17:07:49 +0000631 /* this is just a 32-bit IP address */
632 if (li->hostaddr.sa.sa_family == AF_INET)
633 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
634#endif
635}
andre2ff7b5d2000-06-03 14:57:40 +0000636
637#endif
638/* USE_UTMP || USE_WTMP || USE_LOGIN */
639
andre61e67252000-06-04 17:07:49 +0000640
641
andre2ff7b5d2000-06-03 14:57:40 +0000642/**
643 ** utmpx utility functions
andre61e67252000-06-04 17:07:49 +0000644 **
645 ** These functions manipulate struct utmpx, accounting for system
646 ** variations.
andre2ff7b5d2000-06-03 14:57:40 +0000647 **/
648
649#if defined(USE_UTMPX) || defined (USE_WTMPX)
650
andre2ff7b5d2000-06-03 14:57:40 +0000651/* build the utmpx structure */
652void
andre61e67252000-06-04 17:07:49 +0000653set_utmpx_time(struct logininfo *li, struct utmpx *utx)
654{
andre2ff7b5d2000-06-03 14:57:40 +0000655#ifdef HAVE_TV_IN_UTMPX
656 utx->ut_tv.tv_sec = li->tv_sec;
657 utx->ut_tv.tv_usec = li->tv_usec;
658#else
659# ifdef HAVE_TIME_IN_UTMPX
660 utx->ut_time = li->tv_sec;
661# endif
662#endif
663}
664
andre2ff7b5d2000-06-03 14:57:40 +0000665
andre61e67252000-06-04 17:07:49 +0000666void
667construct_utmpx(struct logininfo *li, struct utmpx *utx)
668{
669 memset(utx, '\0', sizeof(struct utmpx));
andre2ff7b5d2000-06-03 14:57:40 +0000670 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
671
672 /* this is done here to keep utmp constants out of loginrec.h */
673 switch (li->type) {
674 case LTYPE_LOGIN:
675 utx->ut_type = USER_PROCESS;
676 break;
677 case LTYPE_LOGOUT:
678 utx->ut_type = DEAD_PROCESS;
679 break;
680 }
andre2ff7b5d2000-06-03 14:57:40 +0000681 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
andre2ff7b5d2000-06-03 14:57:40 +0000682 set_utmpx_time(li, utx);
andre6bb92372000-06-19 08:20:03 +0000683 utx->ut_pid = li->pid;
684
685 if (li->type == LTYPE_LOGOUT)
686 return;
687
688 /* These fields are only used when logging in, and are blank
689 * for logouts. */
690
691 /* strncpy(): Don't necessarily want null termination */
692 strncpy(utx->ut_name, li->username, MIN_SIZEOF(utx->ut_name, li->username));
andre2ff7b5d2000-06-03 14:57:40 +0000693#ifdef HAVE_HOST_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +0000694 strncpy(utx->ut_host, li->hostname, MIN_SIZEOF(utx->ut_host, li->hostname));
andre2ff7b5d2000-06-03 14:57:40 +0000695#endif
696#ifdef HAVE_ADDR_IN_UTMPX
andre61e67252000-06-04 17:07:49 +0000697 /* FIXME: (ATL) not supported yet */
andre2ff7b5d2000-06-03 14:57:40 +0000698#endif
andre6bb92372000-06-19 08:20:03 +0000699#ifdef HAVE_SYSLEN_IN_UTMPX
700 /* ut_syslen is the length of the utx_host string */
701 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
andre2ff7b5d2000-06-03 14:57:40 +0000702#endif
andre61e67252000-06-04 17:07:49 +0000703}
andre2ff7b5d2000-06-03 14:57:40 +0000704
705#endif
706/* USE_UTMPX || USE_WTMPX */
707
708
709
710/**
andre61e67252000-06-04 17:07:49 +0000711 ** Low-level utmp functions
andre2ff7b5d2000-06-03 14:57:40 +0000712 **/
713
714/* FIXME: (ATL) utmp_write_direct needs testing */
715
716#ifdef USE_UTMP
717
andre2ff7b5d2000-06-03 14:57:40 +0000718/* if we can, use pututline() etc. */
719#if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
720 defined(HAVE_PUTUTLINE)
721# define UTMP_USE_LIBRARY
722#endif
723
724
725/* write a utmp entry with the system's help (pututline() and pals) */
726#ifdef UTMP_USE_LIBRARY
727static int
andre61e67252000-06-04 17:07:49 +0000728utmp_write_library(struct logininfo *li, struct utmp *ut)
729{
andre2ff7b5d2000-06-03 14:57:40 +0000730 setutent();
731 pututline(ut);
732
733#ifdef HAVE_ENDUTENT
734 endutent();
735#endif
736 return 1;
andre61e67252000-06-04 17:07:49 +0000737}
andre2ff7b5d2000-06-03 14:57:40 +0000738
739#else
740
741/* write a utmp entry direct to the file */
andre61e67252000-06-04 17:07:49 +0000742/* This is a slightly modification of code in OpenBSD's login.c */
andre2ff7b5d2000-06-03 14:57:40 +0000743static int
andre61e67252000-06-04 17:07:49 +0000744utmp_write_direct(struct logininfo *li, struct utmp *ut)
745{
andre2ff7b5d2000-06-03 14:57:40 +0000746 struct utmp old_ut;
747 register int fd;
748 int tty;
749
andre6bb92372000-06-19 08:20:03 +0000750 /* FIXME: (ATL) ttyslot() needs local implementation */
andre2ff7b5d2000-06-03 14:57:40 +0000751 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
752
753 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
754 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
755 /*
756 * Prevent luser from zero'ing out ut_host.
757 * If the new ut_line is empty but the old one is not
758 * and ut_line and ut_name match, preserve the old ut_line.
759 */
760 if ( read(fd, &old_ut, sizeof(struct utmp)) == sizeof(struct utmp)
761 && ut->ut_host[0] == '\0'
762 && old_ut.ut_host[0] != '\0'
763 && strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0
764 && strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0 )
765 (void)memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
766
767 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
768 if (write(fd, ut, sizeof(struct utmp))==-1)
769 log("utmp_write_direct: error writing %s: %s",
andre6bb92372000-06-19 08:20:03 +0000770 UTMP_FILE, strerror(errno));
andre2ff7b5d2000-06-03 14:57:40 +0000771
772 (void)close(fd);
773 return 1;
774 } else
775 return 0;
andre61e67252000-06-04 17:07:49 +0000776}
andre2ff7b5d2000-06-03 14:57:40 +0000777#endif /* UTMP_USE_LIBRARY */
778
779
780static int
andre61e67252000-06-04 17:07:49 +0000781utmp_perform_login(struct logininfo *li)
782{
andre2ff7b5d2000-06-03 14:57:40 +0000783 struct utmp ut;
784
785 construct_utmp(li, &ut);
andre2ff7b5d2000-06-03 14:57:40 +0000786#ifdef UTMP_USE_LIBRARY
787 if (!utmp_write_library(li, &ut)) {
andre6bb92372000-06-19 08:20:03 +0000788 log("utmp_perform_login: utmp_write_library() failed");
andre2ff7b5d2000-06-03 14:57:40 +0000789 return 0;
790 }
791#else
792 if (!utmp_write_direct(li, &ut)) {
793 log("utmp_perform_login: utmp_write_direct() failed");
794 return 0;
795 }
796#endif
797 return 1;
andre61e67252000-06-04 17:07:49 +0000798}
andre2ff7b5d2000-06-03 14:57:40 +0000799
800
801static int
andre61e67252000-06-04 17:07:49 +0000802utmp_perform_logout(struct logininfo *li)
803{
andre2ff7b5d2000-06-03 14:57:40 +0000804 struct utmp ut;
805
andre6bb92372000-06-19 08:20:03 +0000806 construct_utmp(li, &ut);
807#ifdef UTMP_USE_LIBRARY
808 if (!utmp_write_library(li, &ut)) {
809 log("utmp_perform_logout: utmp_write_library() failed");
810 return 0;
811 }
andre2ff7b5d2000-06-03 14:57:40 +0000812#else
andre6bb92372000-06-19 08:20:03 +0000813 if (!utmp_write_direct(li, &ut)) {
814 log("utmp_perform_logout: utmp_write_direct() failed");
815 return 0;
816 }
andre2ff7b5d2000-06-03 14:57:40 +0000817#endif
andre2ff7b5d2000-06-03 14:57:40 +0000818 return 1;
andre61e67252000-06-04 17:07:49 +0000819}
andre2ff7b5d2000-06-03 14:57:40 +0000820
821
822int
andre61e67252000-06-04 17:07:49 +0000823utmp_write_entry(struct logininfo *li)
824{
andre2ff7b5d2000-06-03 14:57:40 +0000825 switch(li->type) {
826 case LTYPE_LOGIN:
827 return utmp_perform_login(li);
828
829 case LTYPE_LOGOUT:
830 return utmp_perform_logout(li);
831
832 default:
833 log("utmp_write_entry: invalid type field");
834 return 0;
835 }
andre61e67252000-06-04 17:07:49 +0000836}
andre2ff7b5d2000-06-03 14:57:40 +0000837
838
839#endif
840/* USE_UTMP */
841
842
843/**
andre61e67252000-06-04 17:07:49 +0000844 ** Low-level utmpx functions
andre2ff7b5d2000-06-03 14:57:40 +0000845 **/
846
847/* not much point if we don't want utmpx entries */
848#ifdef USE_UTMPX
849
andre2ff7b5d2000-06-03 14:57:40 +0000850/* if we have the wherewithall, use pututxline etc. */
851#if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) \
852 && defined(HAVE_PUTUTXLINE)
853# define UTMPX_USE_LIBRARY
854#endif
855
856
857/* write a utmpx entry with the system's help (pututxline() and pals) */
858#ifdef UTMPX_USE_LIBRARY
859static int
andre61e67252000-06-04 17:07:49 +0000860utmpx_write_library(struct logininfo *li, struct utmpx *utx)
861{
andre2ff7b5d2000-06-03 14:57:40 +0000862 setutxent();
863 pututxline(utx);
864
865#ifdef HAVE_ENDUTXENT
866 endutxent();
867#endif
868 return 1;
andre61e67252000-06-04 17:07:49 +0000869}
andre2ff7b5d2000-06-03 14:57:40 +0000870
871#else
872/* UTMPX_USE_LIBRARY */
873
874
875/* write a utmp entry direct to the file */
876static int
andre61e67252000-06-04 17:07:49 +0000877utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
878{
andre2ff7b5d2000-06-03 14:57:40 +0000879 log("utmpx_write_direct: not implemented!");
880 return 0;
andre61e67252000-06-04 17:07:49 +0000881}
andre2ff7b5d2000-06-03 14:57:40 +0000882
883#endif
884/* UTMPX_USE_LIBRARY */
885
886static int
andre61e67252000-06-04 17:07:49 +0000887utmpx_perform_login(struct logininfo *li)
888{
andre2ff7b5d2000-06-03 14:57:40 +0000889 struct utmpx utx;
890
891 construct_utmpx(li, &utx);
andre2ff7b5d2000-06-03 14:57:40 +0000892#ifdef UTMPX_USE_LIBRARY
893 if (!utmpx_write_library(li, &utx)) {
894 log("utmpx_perform_login: utmp_write_library() failed");
895 return 0;
896 }
897#else
898 if (!utmpx_write_direct(li, &ut)) {
899 log("utmpx_perform_login: utmp_write_direct() failed");
900 return 0;
901 }
902#endif
903 return 1;
andre61e67252000-06-04 17:07:49 +0000904}
andre2ff7b5d2000-06-03 14:57:40 +0000905
906
907static int
andre61e67252000-06-04 17:07:49 +0000908utmpx_perform_logout(struct logininfo *li)
909{
andre2ff7b5d2000-06-03 14:57:40 +0000910 struct utmpx utx;
911
912 memset(&utx, '\0', sizeof(utx));
913 set_utmpx_time(li, &utx);
914 line_stripname(utx.ut_line, li->line, sizeof(utx.ut_line));
915#ifdef HAVE_ID_IN_UTMPX
916 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
917#endif
918#ifdef HAVE_TYPE_IN_UTMPX
919 utx.ut_type = DEAD_PROCESS;
920#endif
921
922#ifdef UTMPX_USE_LIBRARY
923 utmpx_write_library(li, &utx);
924#else
925 utmpx_write_direct(li, &utx);
926#endif
andre2ff7b5d2000-06-03 14:57:40 +0000927 return 1;
andre61e67252000-06-04 17:07:49 +0000928}
andre2ff7b5d2000-06-03 14:57:40 +0000929
930
931int
andre61e67252000-06-04 17:07:49 +0000932utmpx_write_entry(struct logininfo *li)
933{
andre2ff7b5d2000-06-03 14:57:40 +0000934 switch(li->type) {
935 case LTYPE_LOGIN:
936 return utmpx_perform_login(li);
937 case LTYPE_LOGOUT:
938 return utmpx_perform_logout(li);
939 default:
940 log("utmpx_write_entry: invalid type field");
941 return 0;
942 }
andre61e67252000-06-04 17:07:49 +0000943}
andre2ff7b5d2000-06-03 14:57:40 +0000944
945
946#endif
947/* USE_UTMPX */
948
949
950/**
andre61e67252000-06-04 17:07:49 +0000951 ** Low-level wtmp functions
andre2ff7b5d2000-06-03 14:57:40 +0000952 **/
953
954#ifdef USE_WTMP
955
andre2ff7b5d2000-06-03 14:57:40 +0000956/* write a wtmp entry direct to the end of the file */
andre61e67252000-06-04 17:07:49 +0000957/* This is a slight modification of code in OpenBSD's logwtmp.c */
andre2ff7b5d2000-06-03 14:57:40 +0000958static int
andre61e67252000-06-04 17:07:49 +0000959wtmp_write(struct logininfo *li, struct utmp *ut)
960{
andre2ff7b5d2000-06-03 14:57:40 +0000961 struct stat buf;
962 int fd, ret = 1;
963
964 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
965 log("wtmp_write: problem writing %s: %s",
966 WTMP_FILE, strerror(errno));
967 return 0;
968 }
andre61e67252000-06-04 17:07:49 +0000969 if (fstat(fd, &buf) == 0)
andre2ff7b5d2000-06-03 14:57:40 +0000970 if (write(fd, (char *)ut, sizeof(struct utmp)) !=
971 sizeof(struct utmp)) {
972 ftruncate(fd, buf.st_size);
973 log("wtmp_write: problem writing %s: %s",
974 WTMP_FILE, strerror(errno));
975 ret = 0;
976 }
977 (void)close(fd);
andre2ff7b5d2000-06-03 14:57:40 +0000978 return ret;
andre61e67252000-06-04 17:07:49 +0000979}
andre2ff7b5d2000-06-03 14:57:40 +0000980
981
982static int
andre61e67252000-06-04 17:07:49 +0000983wtmp_perform_login(struct logininfo *li){
andre2ff7b5d2000-06-03 14:57:40 +0000984 struct utmp ut;
985
986 construct_utmp(li, &ut);
987 return wtmp_write(li, &ut);
andre61e67252000-06-04 17:07:49 +0000988}
andre2ff7b5d2000-06-03 14:57:40 +0000989
990
991static int
andre61e67252000-06-04 17:07:49 +0000992wtmp_perform_logout(struct logininfo *li)
993{
andre2ff7b5d2000-06-03 14:57:40 +0000994 struct utmp ut;
995
996 construct_utmp(li, &ut);
andre2ff7b5d2000-06-03 14:57:40 +0000997 return wtmp_write(li, &ut);
andre61e67252000-06-04 17:07:49 +0000998}
andre2ff7b5d2000-06-03 14:57:40 +0000999
1000
1001int
andre61e67252000-06-04 17:07:49 +00001002wtmp_write_entry(struct logininfo *li)
1003{
andre2ff7b5d2000-06-03 14:57:40 +00001004 switch(li->type) {
1005 case LTYPE_LOGIN:
1006 return wtmp_perform_login(li);
1007 case LTYPE_LOGOUT:
1008 return wtmp_perform_logout(li);
1009 default:
1010 log("wtmp_write_entry: invalid type field");
1011 return 0;
1012 }
andre61e67252000-06-04 17:07:49 +00001013}
andre2ff7b5d2000-06-03 14:57:40 +00001014
1015
andre6bb92372000-06-19 08:20:03 +00001016/* Notes on fetching login data from wtmp/wtmpx
1017 *
1018 * Logouts are usually recorded with (amongst other things) a blank
1019 * username on a given tty line. However, some systems (HP-UX is one)
1020 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1021 *
1022 * Since we're only looking for logins here, we know that the username
1023 * must be set correctly. On systems that leave it in, we check for
1024 * ut_type==USER_PROCESS (indicating a login.)
1025 *
1026 * Portability: Some systems may set something other than USER_PROCESS
1027 * to indicate a login process. I don't know of any as I write. Also,
1028 * it's possible that some systems may both leave the username in
1029 * place and not have ut_type.
1030 */
1031
1032
1033/* return true if this wtmp entry indicates a login */
1034static int
1035wtmp_islogin(struct logininfo *li, struct utmp *ut)
1036{
1037 if ( strncmp(li->username, ut->ut_user,
1038 MIN_SIZEOF(li->username, ut->ut_user)) == 0 ) {
1039#ifdef HAVE_TYPE_IN_UTMP
1040 if (ut->ut_type & USER_PROCESS)
1041 return 1;
1042#else
1043 return 1;
1044#endif
1045 }
1046 return 0;
1047}
1048
1049
andre2ff7b5d2000-06-03 14:57:40 +00001050int
andre61e67252000-06-04 17:07:49 +00001051wtmp_get_entry(struct logininfo *li)
1052{
andre2ff7b5d2000-06-03 14:57:40 +00001053 struct stat st;
1054 struct utmp ut;
andre6bb92372000-06-19 08:20:03 +00001055 int fd, found=0;
1056
1057 /* Clear the time entries in our logininfo */
1058 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001059
1060 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
1061 log("wtmp_get_entry: problem opening %s: %s",
1062 WTMP_FILE, strerror(errno));
1063 return 0;
1064 }
andre61e67252000-06-04 17:07:49 +00001065 if (fstat(fd, &st) != 0) {
andre2ff7b5d2000-06-03 14:57:40 +00001066 log("wtmp_get_entry: couldn't stat %s: %s",
1067 WTMP_FILE, strerror(errno));
1068 close(fd);
1069 return 0;
1070 }
andre2ff7b5d2000-06-03 14:57:40 +00001071
andre6bb92372000-06-19 08:20:03 +00001072 /* Seek to the start of the last struct utmp */
1073 if (lseek(fd, (off_t)(0-sizeof(struct utmp)), SEEK_END) == -1) {
1074 /* Looks like we've got a fresh wtmp file */
1075 close(fd);
1076 return 0;
1077 }
1078
1079 while (!found) {
andre2ff7b5d2000-06-03 14:57:40 +00001080 if (read(fd, &ut, sizeof(ut)) != sizeof(ut)) {
1081 log("wtmp_get_entry: read of %s failed: %s",
1082 WTMP_FILE, strerror(errno));
1083 close (fd);
1084 return 0;
1085 }
andre6bb92372000-06-19 08:20:03 +00001086 if ( wtmp_islogin(li, &ut) ) {
1087 found = 1;
1088 /* We've already checked for a time in struct
1089 * utmp, in login_getlast(). */
andre2ff7b5d2000-06-03 14:57:40 +00001090#ifdef HAVE_TIME_IN_UTMP
1091 li->tv_sec = ut.ut_time;
1092#else
1093# if HAVE_TV_IN_UTMP
1094 li->tv_sec = ut.ut_tv.tv_sec;
1095# endif
1096#endif
andre6bb92372000-06-19 08:20:03 +00001097 line_fullname(li->line, ut.ut_line,
1098 MIN_SIZEOF(li->line, ut.ut_line));
andre2ff7b5d2000-06-03 14:57:40 +00001099#ifdef HAVE_HOST_IN_UTMP
andre6bb92372000-06-19 08:20:03 +00001100 strlcpy(li->hostname, ut.ut_host,
1101 MIN_SIZEOF(li->hostname, ut.ut_host));
andre2ff7b5d2000-06-03 14:57:40 +00001102#endif
andre6bb92372000-06-19 08:20:03 +00001103 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001104 }
andre6bb92372000-06-19 08:20:03 +00001105 /* Seek back 2 x struct utmp */
andre2ff7b5d2000-06-03 14:57:40 +00001106 if (lseek(fd, (off_t)(0-2*sizeof(struct utmp)), SEEK_CUR) == -1) {
andre6bb92372000-06-19 08:20:03 +00001107 /* We've found the start of the file, so quit */
andre2ff7b5d2000-06-03 14:57:40 +00001108 close (fd);
1109 return 0;
1110 }
andre6bb92372000-06-19 08:20:03 +00001111 }
1112
1113 /* We found an entry. Tidy up and return */
1114 close(fd);
andre2ff7b5d2000-06-03 14:57:40 +00001115 return 1;
andre61e67252000-06-04 17:07:49 +00001116}
andre2ff7b5d2000-06-03 14:57:40 +00001117
1118
1119#endif
1120/* USE_WTMP */
1121
1122
1123/**
andre61e67252000-06-04 17:07:49 +00001124 ** Low-level wtmpx functions
andre2ff7b5d2000-06-03 14:57:40 +00001125 **/
1126
1127#ifdef USE_WTMPX
1128
andre2ff7b5d2000-06-03 14:57:40 +00001129/* write a wtmpx entry direct to the end of the file */
andre61e67252000-06-04 17:07:49 +00001130/* This is a slight modification of code in OpenBSD's logwtmp.c */
andre2ff7b5d2000-06-03 14:57:40 +00001131static int
andre61e67252000-06-04 17:07:49 +00001132wtmpx_write(struct logininfo *li, struct utmpx *utx)
1133{
andre2ff7b5d2000-06-03 14:57:40 +00001134 struct stat buf;
1135 int fd, ret = 1;
1136
1137 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
1138 log("wtmpx_write: problem opening %s: %s",
1139 WTMPX_FILE, strerror(errno));
1140 return 0;
1141 }
1142
1143 if (fstat(fd, &buf) == 0)
1144 if (write(fd, (char *)utx, sizeof(struct utmpx)) !=
1145 sizeof(struct utmpx)) {
1146 ftruncate(fd, buf.st_size);
1147 log("wtmpx_write: problem writing %s: %s",
1148 WTMPX_FILE, strerror(errno));
1149 ret = 0;
1150 }
1151 (void)close(fd);
1152
1153 return ret;
andre61e67252000-06-04 17:07:49 +00001154}
andre2ff7b5d2000-06-03 14:57:40 +00001155
1156
1157static int
andre61e67252000-06-04 17:07:49 +00001158wtmpx_perform_login(struct logininfo *li)
1159{
andre2ff7b5d2000-06-03 14:57:40 +00001160 struct utmpx utx;
1161
1162 construct_utmpx(li, &utx);
1163 return wtmpx_write(li, &utx);
andre61e67252000-06-04 17:07:49 +00001164}
andre2ff7b5d2000-06-03 14:57:40 +00001165
1166
1167static int
andre61e67252000-06-04 17:07:49 +00001168wtmpx_perform_logout(struct logininfo *li)
1169{
andre2ff7b5d2000-06-03 14:57:40 +00001170 struct utmpx utx;
1171
1172 construct_utmpx(li, &utx);
andre2ff7b5d2000-06-03 14:57:40 +00001173 return wtmpx_write(li, &utx);
andre61e67252000-06-04 17:07:49 +00001174}
andre2ff7b5d2000-06-03 14:57:40 +00001175
1176
1177int
andre61e67252000-06-04 17:07:49 +00001178wtmpx_write_entry(struct logininfo *li)
1179{
andre2ff7b5d2000-06-03 14:57:40 +00001180 switch(li->type) {
1181 case LTYPE_LOGIN:
1182 return wtmpx_perform_login(li);
1183 case LTYPE_LOGOUT:
1184 return wtmpx_perform_logout(li);
1185 default:
1186 log("wtmpx_write_entry: invalid type field");
1187 return 0;
1188 }
andre61e67252000-06-04 17:07:49 +00001189}
andre2ff7b5d2000-06-03 14:57:40 +00001190
andre6bb92372000-06-19 08:20:03 +00001191/* Please see the notes above wtmp_islogin() for information about the
1192 next two functions */
1193
1194/* Return true if this wtmpx entry indicates a login */
1195static int
1196wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1197{
1198 if ( strncmp(li->username, utx->ut_user,
1199 MIN_SIZEOF(li->username, utx->us_user)) == 0 ) {
1200#ifdef HAVE_TYPE_IN_UTMPX
1201 if (utx->ut_type == USER_PROCESS)
1202 return 1;
1203#else
1204 return 1;
1205#endif
1206 }
1207 return 0;
1208}
1209
andre2ff7b5d2000-06-03 14:57:40 +00001210
1211int
andre61e67252000-06-04 17:07:49 +00001212wtmpx_get_entry(struct logininfo *li)
1213{
andre2ff7b5d2000-06-03 14:57:40 +00001214 struct stat st;
1215 struct utmpx utx;
andre6bb92372000-06-19 08:20:03 +00001216 int fd, found=0;
1217
1218 /* Clear the time entries */
1219 li->tv_sec = li->tv_usec = 0;
andre2ff7b5d2000-06-03 14:57:40 +00001220
1221 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
1222 log("wtmpx_get_entry: problem opening %s: %s",
1223 WTMPX_FILE, strerror(errno));
1224 return 0;
1225 }
andre61e67252000-06-04 17:07:49 +00001226 if (fstat(fd, &st) != 0) {
andre2ff7b5d2000-06-03 14:57:40 +00001227 log("wtmpx_get_entry: couldn't stat %s: %s",
1228 WTMP_FILE, strerror(errno));
1229 close(fd);
1230 return 0;
1231 }
andre6bb92372000-06-19 08:20:03 +00001232
1233 /* Seek to the start of the last struct utmpx */
1234 if (lseek(fd, (off_t)(0-sizeof(struct utmpx)), SEEK_END) == -1 ) {
1235 /* probably a newly rotated wtmpx file */
1236 close(fd);
1237 return 0;
1238 }
andre2ff7b5d2000-06-03 14:57:40 +00001239
andre6bb92372000-06-19 08:20:03 +00001240 while (!found) {
andre2ff7b5d2000-06-03 14:57:40 +00001241 if (read(fd, &utx, sizeof(utx)) != sizeof(utx)) {
1242 log("wtmpx_get_entry: read of %s failed: %s",
1243 WTMPX_FILE, strerror(errno));
1244 close (fd);
1245 return 0;
1246 }
andre2ff7b5d2000-06-03 14:57:40 +00001247 /* Logouts are recorded as a blank username on a particular line.
1248 * So, we just need to find the username in struct utmpx */
andre6bb92372000-06-19 08:20:03 +00001249 if ( wtmpx_islogin(li, &utx) ) {
andre2ff7b5d2000-06-03 14:57:40 +00001250#ifdef HAVE_TV_IN_UTMPX
1251 li->tv_sec = utx.ut_tv.tv_sec;
1252#else
1253# ifdef HAVE_TIME_IN_UTMPX
1254 li->tv_sec = utx.ut_time;
1255# endif
1256#endif
Damien Miller1a132252000-06-13 21:23:17 +10001257 line_fullname(li->line, utx.ut_line, sizeof(li->line));
andre2ff7b5d2000-06-03 14:57:40 +00001258#ifdef HAVE_HOST_IN_UTMPX
andre6bb92372000-06-19 08:20:03 +00001259 strlcpy(li->hostname, utx.ut_host,
1260 MIN_SIZEOF(li->hostname, utx.ut_host));
andre2ff7b5d2000-06-03 14:57:40 +00001261#endif
andre6bb92372000-06-19 08:20:03 +00001262 continue;
andre2ff7b5d2000-06-03 14:57:40 +00001263 }
1264 if (lseek(fd, (off_t)(0-2*sizeof(struct utmpx)), SEEK_CUR) == -1) {
1265 close (fd);
1266 return 0;
1267 }
andre6bb92372000-06-19 08:20:03 +00001268 }
1269
1270 close(fd);
andre2ff7b5d2000-06-03 14:57:40 +00001271 return 1;
andre61e67252000-06-04 17:07:49 +00001272}
andre2ff7b5d2000-06-03 14:57:40 +00001273
1274
Damien Millerd5bf3072000-06-07 21:32:13 +10001275#endif /* USE_WTMPX */
andre2ff7b5d2000-06-03 14:57:40 +00001276
1277
1278/**
andre61e67252000-06-04 17:07:49 +00001279 ** Low-level libutil login() functions
andre2ff7b5d2000-06-03 14:57:40 +00001280 **/
1281
1282#ifdef USE_LOGIN
1283
andre2ff7b5d2000-06-03 14:57:40 +00001284static int
andre61e67252000-06-04 17:07:49 +00001285syslogin_perform_login(struct logininfo *li)
1286{
andre2ff7b5d2000-06-03 14:57:40 +00001287 struct utmp *ut;
1288
1289 if (! (ut = (struct utmp *)malloc(sizeof(struct utmp)))) {
1290 log("syslogin_perform_login: couldn't malloc()");
1291 return 0;
1292 }
1293 construct_utmp(li, ut);
1294 login(ut);
1295
1296 return 1;
andre61e67252000-06-04 17:07:49 +00001297}
1298
andre2ff7b5d2000-06-03 14:57:40 +00001299
1300static int
andre61e67252000-06-04 17:07:49 +00001301syslogin_perform_logout(struct logininfo *li)
1302{
andre2ff7b5d2000-06-03 14:57:40 +00001303#ifdef HAVE_LOGOUT
1304 char line[8];
1305
1306 (void)line_stripname(line, li->line, sizeof(line));
1307
1308 if (!logout(line)) {
1309 log("syslogin_perform_logout: logout() returned an error");
Damien Millerd5bf3072000-06-07 21:32:13 +10001310# ifdef HAVE_LOGWTMP
andre2ff7b5d2000-06-03 14:57:40 +00001311 } else {
1312 logwtmp(line, "", "");
1313 }
Damien Millerd5bf3072000-06-07 21:32:13 +10001314# endif
andre6bb92372000-06-19 08:20:03 +00001315 /* FIXME: (ATL - if the need arises) What to do if we have
1316 * login, but no logout? what if logout but no logwtmp? All
1317 * routines are in libutil so they should all be there,
1318 * but... */
andre2ff7b5d2000-06-03 14:57:40 +00001319#endif
1320 return 1;
andre61e67252000-06-04 17:07:49 +00001321}
andre2ff7b5d2000-06-03 14:57:40 +00001322
1323
1324int
andre61e67252000-06-04 17:07:49 +00001325syslogin_write_entry(struct logininfo *li)
1326{
andre2ff7b5d2000-06-03 14:57:40 +00001327 switch (li->type) {
1328 case LTYPE_LOGIN:
1329 return syslogin_perform_login(li);
1330 case LTYPE_LOGOUT:
1331 return syslogin_perform_logout(li);
1332 default:
1333 log("syslogin_write_entry: Invalid type field");
1334 return 0;
1335 }
andre61e67252000-06-04 17:07:49 +00001336}
andre2ff7b5d2000-06-03 14:57:40 +00001337
1338
Damien Millerd5bf3072000-06-07 21:32:13 +10001339#endif /* USE_LOGIN */
andre2ff7b5d2000-06-03 14:57:40 +00001340
1341/* end of file log-syslogin.c */
1342
1343
1344/**
andre61e67252000-06-04 17:07:49 +00001345 ** Low-level lastlog functions
andre2ff7b5d2000-06-03 14:57:40 +00001346 **/
1347
1348#ifdef USE_LASTLOG
1349
andre2ff7b5d2000-06-03 14:57:40 +00001350static void
andre61e67252000-06-04 17:07:49 +00001351lastlog_construct(struct logininfo *li, struct lastlog *last)
1352{
andre2ff7b5d2000-06-03 14:57:40 +00001353 /* clear the structure */
1354 memset(last, '\0', sizeof(struct lastlog));
1355
1356 (void)line_stripname(last->ll_line, li->line,
1357 sizeof(last->ll_line));
andre6bb92372000-06-19 08:20:03 +00001358 strlcpy(last->ll_host, li->hostname,
1359 MIN_SIZEOF(last->ll_host, li->hostname));
andre2ff7b5d2000-06-03 14:57:40 +00001360 last->ll_time = li->tv_sec;
andre61e67252000-06-04 17:07:49 +00001361}
andre2ff7b5d2000-06-03 14:57:40 +00001362
1363
1364#define LL_FILE 1
1365#define LL_DIR 2
1366#define LL_OTHER 3
1367
1368static int
andre61e67252000-06-04 17:07:49 +00001369lastlog_filetype(char *filename)
1370{
andre2ff7b5d2000-06-03 14:57:40 +00001371 struct stat st;
1372
1373 if ( stat(LASTLOG_FILE, &st) != 0) {
1374 log("lastlog_perform_login: Couldn't stat %s: %s",
1375 LASTLOG_FILE, strerror(errno));
1376 return 0;
1377 }
andre2ff7b5d2000-06-03 14:57:40 +00001378 if (S_ISDIR(st.st_mode))
1379 return LL_DIR;
1380 else if (S_ISREG(st.st_mode))
1381 return LL_FILE;
1382 else
1383 return LL_OTHER;
andre61e67252000-06-04 17:07:49 +00001384}
andre2ff7b5d2000-06-03 14:57:40 +00001385
1386
1387/* open the file (using filemode) and seek to the login entry */
1388static int
andre61e67252000-06-04 17:07:49 +00001389lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1390{
andre2ff7b5d2000-06-03 14:57:40 +00001391 off_t offset;
1392 int type;
1393 char lastlog_file[1024];
1394
1395 type = lastlog_filetype(LASTLOG_FILE);
1396 switch (type) {
1397 case LL_FILE:
1398 strlcpy(lastlog_file, LASTLOG_FILE, sizeof(lastlog_file));
1399 break;
1400 case LL_DIR:
1401 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1402 LASTLOG_FILE, li->username);
1403 break;
1404 default:
1405 log("lastlog_openseek: %.100s is not a file or directory!",
1406 LASTLOG_FILE);
1407 return 0;
1408 } /* switch */
1409
1410 *fd = open(lastlog_file, filemode);
1411 if ( *fd < 0) {
1412 log("lastlog_openseek: Couldn't open %s: %s",
1413 lastlog_file, strerror(errno));
1414 return 0;
1415 }
andre2ff7b5d2000-06-03 14:57:40 +00001416 /* find this uid's offset in the lastlog file */
1417 offset = (off_t) ( (long)li->uid * sizeof(struct lastlog));
1418
1419 if ( lseek(*fd, offset, SEEK_SET) != offset ) {
1420 log("lastlog_openseek: %s->lseek(): %s",
1421 lastlog_file, strerror(errno));
1422 return 0;
1423 }
1424 return 1;
andre61e67252000-06-04 17:07:49 +00001425}
andre2ff7b5d2000-06-03 14:57:40 +00001426
1427static int
andre61e67252000-06-04 17:07:49 +00001428lastlog_perform_login(struct logininfo *li)
1429{
andre2ff7b5d2000-06-03 14:57:40 +00001430 struct lastlog last;
1431 int fd;
1432
1433 /* create our struct lastlog */
1434 lastlog_construct(li, &last);
1435
1436 /* write the entry */
1437 if (lastlog_openseek(li, &fd, O_RDWR)) {
1438 if ( write(fd, &last, sizeof(struct lastlog))
1439 != sizeof(struct lastlog) ) {
1440 log("lastlog_write_filemode: Error writing to %s: %s",
1441 LASTLOG_FILE, strerror(errno));
1442 return 0;
1443 }
1444 return 1;
1445 } else
1446 return 0;
andre61e67252000-06-04 17:07:49 +00001447}
andre2ff7b5d2000-06-03 14:57:40 +00001448
1449
1450int
andre61e67252000-06-04 17:07:49 +00001451lastlog_write_entry(struct logininfo *li)
1452{
andre2ff7b5d2000-06-03 14:57:40 +00001453 switch(li->type) {
1454 case LTYPE_LOGIN:
1455 return lastlog_perform_login(li);
1456 default:
1457 log("lastlog_write_entry: Invalid type field");
1458 return 0;
1459 }
andre61e67252000-06-04 17:07:49 +00001460}
andre2ff7b5d2000-06-03 14:57:40 +00001461
1462
1463static void
andre61e67252000-06-04 17:07:49 +00001464lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1465{
andre2ff7b5d2000-06-03 14:57:40 +00001466 line_fullname(li->line, last->ll_line, sizeof(li->line));
andre6bb92372000-06-19 08:20:03 +00001467 strlcpy(li->hostname, last->ll_host,
1468 MIN_SIZEOF(li->hostname, last->ll_host));
andre2ff7b5d2000-06-03 14:57:40 +00001469 li->tv_sec = last->ll_time;
andre61e67252000-06-04 17:07:49 +00001470}
andre2ff7b5d2000-06-03 14:57:40 +00001471
1472
1473int
andre61e67252000-06-04 17:07:49 +00001474lastlog_get_entry(struct logininfo *li)
1475{
andre2ff7b5d2000-06-03 14:57:40 +00001476 struct lastlog last;
1477 int fd;
1478
1479 if (lastlog_openseek(li, &fd, O_RDONLY)) {
1480 if ( read(fd, &last, sizeof(struct lastlog))
1481 != sizeof(struct lastlog) ) {
1482 log("lastlog_write_filemode: Error reading from %s: %s",
1483 LASTLOG_FILE, strerror(errno));
1484 return 0;
1485 } else {
1486 lastlog_populate_entry(li, &last);
1487 return 1;
1488 }
andre2ff7b5d2000-06-03 14:57:40 +00001489 } else
1490 return 0;
andre61e67252000-06-04 17:07:49 +00001491}
andre2ff7b5d2000-06-03 14:57:40 +00001492
1493
Damien Millerd5bf3072000-06-07 21:32:13 +10001494#endif /* USE_LASTLOG */