2 * Copyright (c) 2000 Andre Lucas. All rights reserved.
3 * Portions copyright (c) 1998 Todd C. Miller
4 * Portions copyright (c) 1996 Jason Downs
5 * Portions copyright (c) 1996 Theo de Raadt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
34 ** loginrec.c: platform-independent login recording and lastlog retrieval
38 The new login code explained
39 ============================
41 This code attempts to provide a common interface to login recording
42 (utmp and friends) and last login time retrieval.
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.
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
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
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
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
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.
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.
90 Internal handler calling method
91 -------------------------------
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.
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.
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.
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,
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.
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.)
129 Otherwise, patches to the specific method(s) are very helpful!
135 ** homegrown ttyslot()q
142 ** Linux (Redhat 6.2, need more variants)
143 ** HP-UX 10.20 (gcc only)
145 ** Testing required: Please send reports!
152 ** Platforms with known problems:
157 #include "includes.h"
166 # include <lastlog.h>
171 #include "loginrec.h"
176 ** prototypes for helper functions in this file
180 void set_utmp_time(struct logininfo *li, struct utmp *ut);
181 void construct_utmp(struct logininfo *li, struct utmp *ut);
185 void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
186 void construct_utmpx(struct logininfo *li, struct utmpx *ut);
189 int utmp_write_entry(struct logininfo *li);
190 int utmpx_write_entry(struct logininfo *li);
191 int wtmp_write_entry(struct logininfo *li);
192 int wtmpx_write_entry(struct logininfo *li);
193 int lastlog_write_entry(struct logininfo *li);
194 int syslogin_write_entry(struct logininfo *li);
196 int getlast_entry(struct logininfo *li);
197 int lastlog_get_entry(struct logininfo *li);
198 int wtmp_get_entry(struct logininfo *li);
199 int wtmpx_get_entry(struct logininfo *li);
203 ** platform-independent login functions
208 login_login (struct logininfo *li)
210 li->type = LTYPE_LOGIN;
211 return login_write(li);
215 /* Record a logout */
217 login_logout(struct logininfo *li)
219 li->type = LTYPE_LOGOUT;
220 return login_write(li);
224 /* Retrieve the last login time for a user (or fake on from wtmp/wtmpx) */
226 login_get_lastlog_time(const int uid)
230 login_get_lastlog(&li, uid);
234 /* Retrieve a lastlog entry (or fake one from wtmp/wtmpx) */
236 login_get_lastlog(struct logininfo *li, const int uid)
238 memset(li, '\0', sizeof(struct logininfo));
240 if (getlast_entry(li))
247 /* login_alloc_entry() - allocate and initialise a logininfo */
249 logininfo *login_alloc_entry(int pid, const char *username,
250 const char *hostname, const char *line)
252 struct logininfo *newli;
254 newli = (struct logininfo *) xmalloc (sizeof(struct logininfo));
255 (void)login_init_entry(newli, pid, username, hostname, line);
260 /* login_free_entry() - free struct memory (trivial) */
262 login_free_entry(struct logininfo *li)
268 /* login_init_entry() - initialise a struct logininfo */
270 login_init_entry(struct logininfo *li, int pid, const char *username,
271 const char *hostname, const char *line)
273 /* zero the structure */
274 memset(li, 0, sizeof(struct logininfo));
277 /* set the line information */
279 line_fullname(li->line, line, sizeof(li->line));
282 strlcpy(li->username, username, sizeof(li->username));
284 strlcpy(li->hostname, hostname, sizeof(li->hostname));
290 login_set_current_time(struct logininfo *li)
292 #ifdef HAVE_SYS_TIME_H
295 gettimeofday(&tv, NULL);
296 li->tv_sec = tv.tv_sec ; li->tv_usec = tv.tv_usec;
300 li->tv_sec = tm; li->tv_usec = 0;
305 /* copy a sockaddr_* into our logininfo */
307 login_set_addr(struct logininfo *li, const struct sockaddr *sa,
308 const unsigned int sa_size)
310 unsigned int bufsize = sa_size;
312 /* make sure we don't overrun our union */
313 if (sizeof(li->hostaddr) < sa_size)
314 bufsize = sizeof(li->hostaddr);
316 memcpy((void *)&(li->hostaddr.sa), (const void *)sa, bufsize);
321 ** login_write: Call low-level recording functions based on autoconf
326 login_write (struct logininfo *li)
328 if ((int)geteuid() != 0) {
329 log("Attempt to write login records by non-root user (aborting)");
332 /* set the timestamp */
333 login_set_current_time(li);
335 syslogin_write_entry(li);
338 if (li->type == LTYPE_LOGIN) {
339 lastlog_write_entry(li);
343 utmp_write_entry(li);
346 wtmp_write_entry(li);
349 utmpx_write_entry(li);
352 wtmpx_write_entry(li);
359 ** getlast_entry: Call low-level functions to retrieve the last login
363 /* take the uid in li and return the last login time */
365 getlast_entry(struct logininfo *li)
368 if (lastlog_get_entry(li))
375 # ifdef DISABLE_LASTLOG
376 /* On some systems we shouldn't even try to obtain last login
381 /* Try to retrieve the last login time from wtmp */
382 # if defined(USE_WTMP) && (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
383 /* retrieve last login time from utmp */
384 if (wtmp_get_entry(li))
390 /* If wtmp isn't available, try wtmpx */
392 # if defined(USE_WTMPX) && (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
393 /* retrieve last login time from utmpx */
394 if (wtmpx_get_entry(li))
400 /* Give up: No means of retrieving last login time */
403 /* USE_WTMPX && (HAVE_TIME_IN_UTMPX || HAVE_TV_IN_UTMPX) */
406 /* USE_WTMP && (HAVE_TIME_IN_UTMP || HAVE_TV_IN_UTMP) */
408 /* DISABLE_LASTLOG */
416 * 'line' string utility functions
418 * These functions process the 'line' string into one of three forms:
420 * 1. The full filename (including '/dev')
421 * 2. The stripped name (excluding '/dev')
422 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
423 * /dev/pts/1 -> ts/1 )
425 * Form 3 is used on some systems to identify a .tmp.? entry when
426 * attempting to remove it. Typically both addition and removal is
427 * performed by one application - say, sshd - so as long as the choice
428 * uniquely identifies a terminal it's ok.
432 /* line_fullname(): add the leading '/dev/' if it doesn't exist make
433 * sure dst has enough space, if not just copy src (ugh) */
435 line_fullname(char *dst, const char *src, int dstsize)
437 memset(dst, '\0', dstsize);
438 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
439 strlcpy(dst, src, dstsize);
441 strlcpy(dst, "/dev/", dstsize);
442 strlcat(dst, src, dstsize);
448 /* line_stripname(): strip the leading '/dev' if it exists, return dst */
450 line_stripname(char *dst, const char *src, int dstsize)
452 memset(dst, '\0', dstsize);
453 if (strncmp(src, "/dev/", 5) == 0)
454 strlcpy(dst, &src[5], dstsize);
456 strlcpy(dst, src, dstsize);
461 /* line_abbrevname(): Return the abbreviated (usually four-character)
462 * form of the line (Just use the last <dstsize> characters of the
465 * NOTE: use strncpy because we do NOT necessarily want zero
468 line_abbrevname(char *dst, const char *src, int dstsize) {
469 memset(dst, '\0', dstsize);
470 src += (strlen(src) - dstsize);
471 strncpy(dst, src, dstsize); /* note: _don't_ change this to strlcpy */
477 ** utmp utility functions
479 ** These functions manipulate struct utmp, taking system differences
483 #if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
485 /* build the utmp structure */
487 set_utmp_time(struct logininfo *li, struct utmp *ut)
489 #ifdef HAVE_TV_IN_UTMP
490 ut->ut_tv.tv_sec = li->tv_sec;
491 ut->ut_tv.tv_usec = li->tv_usec;
493 # ifdef HAVE_TIME_IN_UTMP
494 ut->ut_time = li->tv_sec;
501 construct_utmp(struct logininfo *li,
504 memset(ut, '\0', sizeof(struct utmp));
505 #ifdef HAVE_ID_IN_UTMP
506 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
509 #ifdef HAVE_TYPE_IN_UTMP
510 /* This is done here to keep utmp constants out of login.h */
513 ut->ut_type = USER_PROCESS;
516 ut->ut_type = DEAD_PROCESS;
521 #ifdef HAVE_PID_IN_UTMP
522 ut->ut_pid = li->pid;
524 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
525 strlcpy(ut->ut_name, li->username, sizeof(ut->ut_name));
526 set_utmp_time(li, ut);
527 #ifdef HAVE_HOST_IN_UTMP
528 strlcpy(ut->ut_host, li->hostname, sizeof(ut->ut_host));
530 #ifdef HAVE_ADDR_IN_UTMP
531 /* this is just a 32-bit IP address */
532 if (li->hostaddr.sa.sa_family == AF_INET)
533 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
538 /* USE_UTMP || USE_WTMP || USE_LOGIN */
543 ** utmpx utility functions
545 ** These functions manipulate struct utmpx, accounting for system
549 #if defined(USE_UTMPX) || defined (USE_WTMPX)
551 /* build the utmpx structure */
553 set_utmpx_time(struct logininfo *li, struct utmpx *utx)
555 #ifdef HAVE_TV_IN_UTMPX
556 utx->ut_tv.tv_sec = li->tv_sec;
557 utx->ut_tv.tv_usec = li->tv_usec;
559 # ifdef HAVE_TIME_IN_UTMPX
560 utx->ut_time = li->tv_sec;
567 construct_utmpx(struct logininfo *li, struct utmpx *utx)
569 memset(utx, '\0', sizeof(struct utmpx));
570 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
572 /* this is done here to keep utmp constants out of loginrec.h */
575 utx->ut_type = USER_PROCESS;
578 utx->ut_type = DEAD_PROCESS;
581 utx->ut_pid = li->pid;
582 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
583 strlcpy(utx->ut_name, li->username, sizeof(utx->ut_name));
584 set_utmpx_time(li, utx);
585 #ifdef HAVE_HOST_IN_UTMPX
586 strlcpy(utx->ut_host, li->hostname, sizeof(utx->ut_host));
588 #ifdef HAVE_ADDR_IN_UTMPX
589 /* FIXME: (ATL) not supported yet */
591 #ifdef HAVE_SYSLEN_IN_UTMPX
592 /* this is safe because of the extra nulls in logininfo */
593 utx->ut_syslen = strlen(li->hostname);
598 /* USE_UTMPX || USE_WTMPX */
603 ** Low-level utmp functions
606 /* FIXME: (ATL) utmp_write_direct needs testing */
610 /* if we can, use pututline() etc. */
611 #if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
612 defined(HAVE_PUTUTLINE)
613 # define UTMP_USE_LIBRARY
617 /* write a utmp entry with the system's help (pututline() and pals) */
618 #ifdef UTMP_USE_LIBRARY
620 utmp_write_library(struct logininfo *li, struct utmp *ut)
633 /* write a utmp entry direct to the file */
634 /* This is a slightly modification of code in OpenBSD's login.c */
636 utmp_write_direct(struct logininfo *li, struct utmp *ut)
642 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
644 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
645 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
647 * Prevent luser from zero'ing out ut_host.
648 * If the new ut_line is empty but the old one is not
649 * and ut_line and ut_name match, preserve the old ut_line.
651 if ( read(fd, &old_ut, sizeof(struct utmp)) == sizeof(struct utmp)
652 && ut->ut_host[0] == '\0'
653 && old_ut.ut_host[0] != '\0'
654 && strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0
655 && strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0 )
656 (void)memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
658 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
659 if (write(fd, ut, sizeof(struct utmp))==-1)
660 log("utmp_write_direct: error writing %s: %s",
661 UTMP_FILE, strerror(errno));
668 #endif /* UTMP_USE_LIBRARY */
672 utmp_perform_login(struct logininfo *li)
676 construct_utmp(li, &ut);
677 #ifdef UTMP_USE_LIBRARY
678 if (!utmp_write_library(li, &ut)) {
679 log("utmp_perform_login: utmp_write_library() failed");
683 if (!utmp_write_direct(li, &ut)) {
684 log("utmp_perform_login: utmp_write_direct() failed");
693 utmp_perform_logout(struct logininfo *li)
697 memset(&ut, '\0', sizeof(ut));
698 set_utmp_time(li, &ut);
699 line_stripname(ut.ut_line, li->line, sizeof(ut.ut_line));
700 #ifdef HAVE_ID_IN_UTMP
701 line_abbrevname(ut.ut_id, li->line, sizeof(ut.ut_id));
703 #ifdef HAVE_TYPE_IN_UTMP
704 ut.ut_type = DEAD_PROCESS;
707 #if !defined(DISABLE_PUTUTLINE) \
708 && defined(HAVE_SETUTENT) && defined(HAVE_PUTUTLINE)
709 utmp_write_library(li, &ut);
711 utmp_write_direct(li, &ut);
719 utmp_write_entry(struct logininfo *li)
723 return utmp_perform_login(li);
726 return utmp_perform_logout(li);
729 log("utmp_write_entry: invalid type field");
740 ** Low-level utmpx functions
743 /* not much point if we don't want utmpx entries */
746 /* if we have the wherewithall, use pututxline etc. */
747 #if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) \
748 && defined(HAVE_PUTUTXLINE)
749 # define UTMPX_USE_LIBRARY
753 /* write a utmpx entry with the system's help (pututxline() and pals) */
754 #ifdef UTMPX_USE_LIBRARY
756 utmpx_write_library(struct logininfo *li, struct utmpx *utx)
761 #ifdef HAVE_ENDUTXENT
768 /* UTMPX_USE_LIBRARY */
771 /* write a utmp entry direct to the file */
773 utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
775 log("utmpx_write_direct: not implemented!");
780 /* UTMPX_USE_LIBRARY */
783 utmpx_perform_login(struct logininfo *li)
787 construct_utmpx(li, &utx);
788 #ifdef UTMPX_USE_LIBRARY
789 if (!utmpx_write_library(li, &utx)) {
790 log("utmpx_perform_login: utmp_write_library() failed");
794 if (!utmpx_write_direct(li, &ut)) {
795 log("utmpx_perform_login: utmp_write_direct() failed");
804 utmpx_perform_logout(struct logininfo *li)
808 memset(&utx, '\0', sizeof(utx));
809 set_utmpx_time(li, &utx);
810 line_stripname(utx.ut_line, li->line, sizeof(utx.ut_line));
811 #ifdef HAVE_ID_IN_UTMPX
812 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
814 #ifdef HAVE_TYPE_IN_UTMPX
815 utx.ut_type = DEAD_PROCESS;
818 #ifdef UTMPX_USE_LIBRARY
819 utmpx_write_library(li, &utx);
821 utmpx_write_direct(li, &utx);
828 utmpx_write_entry(struct logininfo *li)
832 return utmpx_perform_login(li);
834 return utmpx_perform_logout(li);
836 log("utmpx_write_entry: invalid type field");
847 ** Low-level wtmp functions
852 /* write a wtmp entry direct to the end of the file */
853 /* This is a slight modification of code in OpenBSD's logwtmp.c */
855 wtmp_write(struct logininfo *li, struct utmp *ut)
860 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
861 log("wtmp_write: problem writing %s: %s",
862 WTMP_FILE, strerror(errno));
865 if (fstat(fd, &buf) == 0)
866 if (write(fd, (char *)ut, sizeof(struct utmp)) !=
867 sizeof(struct utmp)) {
868 ftruncate(fd, buf.st_size);
869 log("wtmp_write: problem writing %s: %s",
870 WTMP_FILE, strerror(errno));
879 wtmp_perform_login(struct logininfo *li){
882 construct_utmp(li, &ut);
883 return wtmp_write(li, &ut);
888 wtmp_perform_logout(struct logininfo *li)
892 construct_utmp(li, &ut);
893 /* blank out unnecessary fields */
894 memset(&(ut.ut_name), '\0', sizeof(ut.ut_name));
895 #ifdef HAVE_ID_IN_UTMP
896 memset(&(ut.ut_id), '\0', sizeof(ut.ut_id));
898 #ifdef HAVE_HOST_IN_UTMP
899 memset(&(ut.ut_host), '\0', sizeof(ut.ut_host));
901 #ifdef HAVE_ADDR_IN_UTMP
902 memset(&(ut.ut_addr), '\0', sizeof(ut.ut_addr));
904 return wtmp_write(li, &ut);
909 wtmp_write_entry(struct logininfo *li)
913 return wtmp_perform_login(li);
915 return wtmp_perform_logout(li);
917 log("wtmp_write_entry: invalid type field");
924 wtmp_get_entry(struct logininfo *li)
930 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
931 log("wtmp_get_entry: problem opening %s: %s",
932 WTMP_FILE, strerror(errno));
935 if (fstat(fd, &st) != 0) {
936 log("wtmp_get_entry: couldn't stat %s: %s",
937 WTMP_FILE, strerror(errno));
941 (void)lseek(fd, (off_t)(0-sizeof(struct utmp)), SEEK_END);
944 if (read(fd, &ut, sizeof(ut)) != sizeof(ut)) {
945 log("wtmp_get_entry: read of %s failed: %s",
946 WTMP_FILE, strerror(errno));
950 /* Logouts are recorded as a blank username on a particular line.
951 * So, we just need to find the username in struct utmp */
952 if ( strncmp(li->username, ut.ut_user, 8) == 0 ) {
953 /* note we've already made sure there's a time in struct utmp */
954 #ifdef HAVE_TIME_IN_UTMP
955 li->tv_sec = ut.ut_time;
958 li->tv_sec = ut.ut_tv.tv_sec;
961 line_fullname(li->line, ut.ut_line, sizeof(li->line));
962 #ifdef HAVE_HOST_IN_UTMP
963 strlcpy(li->hostname, ut.ut_host, sizeof(li->hostname));
966 if (lseek(fd, (off_t)(0-2*sizeof(struct utmp)), SEEK_CUR) == -1) {
970 } while (li->tv_sec == 0);
981 ** Low-level wtmpx functions
986 /* write a wtmpx entry direct to the end of the file */
987 /* This is a slight modification of code in OpenBSD's logwtmp.c */
989 wtmpx_write(struct logininfo *li, struct utmpx *utx)
994 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
995 log("wtmpx_write: problem opening %s: %s",
996 WTMPX_FILE, strerror(errno));
1000 if (fstat(fd, &buf) == 0)
1001 if (write(fd, (char *)utx, sizeof(struct utmpx)) !=
1002 sizeof(struct utmpx)) {
1003 ftruncate(fd, buf.st_size);
1004 log("wtmpx_write: problem writing %s: %s",
1005 WTMPX_FILE, strerror(errno));
1015 wtmpx_perform_login(struct logininfo *li)
1019 construct_utmpx(li, &utx);
1020 return wtmpx_write(li, &utx);
1025 wtmpx_perform_logout(struct logininfo *li)
1029 construct_utmpx(li, &utx);
1030 /* blank out unnecessary fields */
1031 memset(&(utx.ut_name), '\0', sizeof(utx.ut_name));
1032 #ifdef HAVE_ID_IN_UTMPX
1033 memset(&(utx.ut_id), '\0', sizeof(utx.ut_id));
1035 #ifdef HAVE_HOST_IN_UTMPX
1036 memset(&(utx.ut_host), '\0', sizeof(utx.ut_host));
1038 #ifdef HAVE_ADDR_IN_UTMPX
1039 memset(&(utx.ut_addr), '\0', sizeof(utx.ut_addr));
1041 return wtmpx_write(li, &utx);
1047 wtmpx_write_entry(struct logininfo *li)
1051 return wtmpx_perform_login(li);
1053 return wtmpx_perform_logout(li);
1055 log("wtmpx_write_entry: invalid type field");
1062 wtmpx_get_entry(struct logininfo *li)
1068 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
1069 log("wtmpx_get_entry: problem opening %s: %s",
1070 WTMPX_FILE, strerror(errno));
1073 if (fstat(fd, &st) != 0) {
1074 log("wtmpx_get_entry: couldn't stat %s: %s",
1075 WTMP_FILE, strerror(errno));
1079 (void)lseek(fd, (off_t)(0-sizeof(struct utmpx)), SEEK_END);
1082 if (read(fd, &utx, sizeof(utx)) != sizeof(utx)) {
1083 log("wtmpx_get_entry: read of %s failed: %s",
1084 WTMPX_FILE, strerror(errno));
1088 /* Logouts are recorded as a blank username on a particular line.
1089 * So, we just need to find the username in struct utmpx */
1090 if ( strncmp(li->username, utx.ut_user, 8) == 0 ) {
1091 /* note we've already made sure there's a time in struct utmp */
1092 #ifdef HAVE_TV_IN_UTMPX
1093 li->tv_sec = utx.ut_tv.tv_sec;
1095 # ifdef HAVE_TIME_IN_UTMPX
1096 li->tv_sec = utx.ut_time;
1099 line_fullname(li->line, utx.ut_line, sizeof(li->line));
1100 #ifdef HAVE_HOST_IN_UTMPX
1101 strlcpy(li->hostname, utx.ut_host, sizeof(li->hostname));
1104 if (lseek(fd, (off_t)(0-2*sizeof(struct utmpx)), SEEK_CUR) == -1) {
1108 } while (li->tv_sec == 0);
1113 #endif /* USE_WTMPX */
1117 ** Low-level libutil login() functions
1123 syslogin_perform_login(struct logininfo *li)
1127 if (! (ut = (struct utmp *)malloc(sizeof(struct utmp)))) {
1128 log("syslogin_perform_login: couldn't malloc()");
1131 construct_utmp(li, ut);
1139 syslogin_perform_logout(struct logininfo *li)
1144 (void)line_stripname(line, li->line, sizeof(line));
1146 if (!logout(line)) {
1147 log("syslogin_perform_logout: logout() returned an error");
1148 # ifdef HAVE_LOGWTMP
1150 logwtmp(line, "", "");
1153 /* TODO: what to do if we have login, but no logout?
1154 * what if logout but no logwtmp? All routines are in libutil
1155 * so they should all be there, but... */
1162 syslogin_write_entry(struct logininfo *li)
1166 return syslogin_perform_login(li);
1168 return syslogin_perform_logout(li);
1170 log("syslogin_write_entry: Invalid type field");
1176 #endif /* USE_LOGIN */
1178 /* end of file log-syslogin.c */
1182 ** Low-level lastlog functions
1188 lastlog_construct(struct logininfo *li, struct lastlog *last)
1190 /* clear the structure */
1191 memset(last, '\0', sizeof(struct lastlog));
1193 (void)line_stripname(last->ll_line, li->line,
1194 sizeof(last->ll_line));
1195 strlcpy(last->ll_host, li->hostname, sizeof(last->ll_host));
1196 last->ll_time = li->tv_sec;
1205 lastlog_filetype(char *filename)
1209 if ( stat(LASTLOG_FILE, &st) != 0) {
1210 log("lastlog_perform_login: Couldn't stat %s: %s",
1211 LASTLOG_FILE, strerror(errno));
1214 if (S_ISDIR(st.st_mode))
1216 else if (S_ISREG(st.st_mode))
1223 /* open the file (using filemode) and seek to the login entry */
1225 lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1229 char lastlog_file[1024];
1231 type = lastlog_filetype(LASTLOG_FILE);
1234 strlcpy(lastlog_file, LASTLOG_FILE, sizeof(lastlog_file));
1237 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1238 LASTLOG_FILE, li->username);
1241 log("lastlog_openseek: %.100s is not a file or directory!",
1246 *fd = open(lastlog_file, filemode);
1248 log("lastlog_openseek: Couldn't open %s: %s",
1249 lastlog_file, strerror(errno));
1252 /* find this uid's offset in the lastlog file */
1253 offset = (off_t) ( (long)li->uid * sizeof(struct lastlog));
1255 if ( lseek(*fd, offset, SEEK_SET) != offset ) {
1256 log("lastlog_openseek: %s->lseek(): %s",
1257 lastlog_file, strerror(errno));
1264 lastlog_perform_login(struct logininfo *li)
1266 struct lastlog last;
1269 /* create our struct lastlog */
1270 lastlog_construct(li, &last);
1272 /* write the entry */
1273 if (lastlog_openseek(li, &fd, O_RDWR)) {
1274 if ( write(fd, &last, sizeof(struct lastlog))
1275 != sizeof(struct lastlog) ) {
1276 log("lastlog_write_filemode: Error writing to %s: %s",
1277 LASTLOG_FILE, strerror(errno));
1287 lastlog_write_entry(struct logininfo *li)
1291 return lastlog_perform_login(li);
1293 log("lastlog_write_entry: Invalid type field");
1300 lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1302 line_fullname(li->line, last->ll_line, sizeof(li->line));
1303 strlcpy(li->hostname, last->ll_host, sizeof(li->hostname));
1304 li->tv_sec = last->ll_time;
1309 lastlog_get_entry(struct logininfo *li)
1311 struct lastlog last;
1314 if (lastlog_openseek(li, &fd, O_RDONLY)) {
1315 if ( read(fd, &last, sizeof(struct lastlog))
1316 != sizeof(struct lastlog) ) {
1317 log("lastlog_write_filemode: Error reading from %s: %s",
1318 LASTLOG_FILE, strerror(errno));
1321 lastlog_populate_entry(li, &last);
1329 #endif /* USE_LASTLOG */