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"
159 #include <sys/types.h>
160 #include <sys/stat.h>
161 #include <netinet/in.h>
171 #ifdef HAVE_SYS_TIME_H
172 # include <sys/time.h>
183 # include <lastlog.h>
188 #include "loginrec.h"
194 ** prototypes for helper functions in this file
198 void set_utmp_time(struct logininfo *li, struct utmp *ut);
199 void construct_utmp(struct logininfo *li, struct utmp *ut);
203 void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
204 void construct_utmpx(struct logininfo *li, struct utmpx *ut);
207 int utmp_write_entry(struct logininfo *li);
208 int utmpx_write_entry(struct logininfo *li);
209 int wtmp_write_entry(struct logininfo *li);
210 int wtmpx_write_entry(struct logininfo *li);
211 int lastlog_write_entry(struct logininfo *li);
212 int syslogin_write_entry(struct logininfo *li);
214 int getlast_entry(struct logininfo *li);
215 int lastlog_get_entry(struct logininfo *li);
216 int wtmp_get_entry(struct logininfo *li);
217 int wtmpx_get_entry(struct logininfo *li);
221 ** platform-independent login functions
226 login_login (struct logininfo *li)
228 li->type = LTYPE_LOGIN;
229 return login_write(li);
233 /* Record a logout */
235 login_logout(struct logininfo *li)
237 li->type = LTYPE_LOGOUT;
238 return login_write(li);
242 /* Retrieve the last login time for a user (or fake on from wtmp/wtmpx) */
244 login_get_lastlog_time(const int uid)
248 login_get_lastlog(&li, uid);
252 /* Retrieve a lastlog entry (or fake one from wtmp/wtmpx) */
254 login_get_lastlog(struct logininfo *li, const int uid)
256 memset(li, '\0', sizeof(struct logininfo));
258 if (getlast_entry(li))
265 /* login_alloc_entry() - allocate and initialise a logininfo */
267 logininfo *login_alloc_entry(int pid, const char *username,
268 const char *hostname, const char *line)
270 struct logininfo *newli;
272 newli = (struct logininfo *) xmalloc (sizeof(struct logininfo));
273 (void)login_init_entry(newli, pid, username, hostname, line);
278 /* login_free_entry() - free struct memory (trivial) */
280 login_free_entry(struct logininfo *li)
286 /* login_init_entry() - initialise a struct logininfo */
288 login_init_entry(struct logininfo *li, int pid, const char *username,
289 const char *hostname, const char *line)
291 /* zero the structure */
292 memset(li, 0, sizeof(struct logininfo));
295 /* set the line information */
297 line_fullname(li->line, line, sizeof(li->line));
300 strlcpy(li->username, username, sizeof(li->username));
302 strlcpy(li->hostname, hostname, sizeof(li->hostname));
308 login_set_current_time(struct logininfo *li)
310 #ifdef HAVE_SYS_TIME_H
313 gettimeofday(&tv, NULL);
314 li->tv_sec = tv.tv_sec ; li->tv_usec = tv.tv_usec;
318 li->tv_sec = tm; li->tv_usec = 0;
323 /* copy a sockaddr_* into our logininfo */
325 login_set_addr(struct logininfo *li, const struct sockaddr *sa,
326 const unsigned int sa_size)
328 unsigned int bufsize = sa_size;
330 /* make sure we don't overrun our union */
331 if (sizeof(li->hostaddr) < sa_size)
332 bufsize = sizeof(li->hostaddr);
334 memcpy((void *)&(li->hostaddr.sa), (const void *)sa, bufsize);
339 ** login_write: Call low-level recording functions based on autoconf
344 login_write (struct logininfo *li)
346 if ((int)geteuid() != 0) {
347 log("Attempt to write login records by non-root user (aborting)");
350 /* set the timestamp */
351 login_set_current_time(li);
353 syslogin_write_entry(li);
356 if (li->type == LTYPE_LOGIN) {
357 lastlog_write_entry(li);
361 utmp_write_entry(li);
364 wtmp_write_entry(li);
367 utmpx_write_entry(li);
370 wtmpx_write_entry(li);
377 ** getlast_entry: Call low-level functions to retrieve the last login
381 /* take the uid in li and return the last login time */
383 getlast_entry(struct logininfo *li)
386 if (lastlog_get_entry(li))
393 /* Try to retrieve the last login time from wtmp */
394 # if defined(USE_WTMP) && (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
395 /* retrieve last login time from utmp */
396 if (wtmp_get_entry(li))
402 /* If wtmp isn't available, try wtmpx */
404 # if defined(USE_WTMPX) && (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
405 /* retrieve last login time from utmpx */
406 if (wtmpx_get_entry(li))
412 /* Give up: No means of retrieving last login time */
423 * 'line' string utility functions
425 * These functions process the 'line' string into one of three forms:
427 * 1. The full filename (including '/dev')
428 * 2. The stripped name (excluding '/dev')
429 * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00
430 * /dev/pts/1 -> ts/1 )
432 * Form 3 is used on some systems to identify a .tmp.? entry when
433 * attempting to remove it. Typically both addition and removal is
434 * performed by one application - say, sshd - so as long as the choice
435 * uniquely identifies a terminal it's ok.
439 /* line_fullname(): add the leading '/dev/' if it doesn't exist make
440 * sure dst has enough space, if not just copy src (ugh) */
442 line_fullname(char *dst, const char *src, int dstsize)
444 memset(dst, '\0', dstsize);
445 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
446 strlcpy(dst, src, dstsize);
448 strlcpy(dst, "/dev/", 5);
449 strlcat(dst, src, dstsize);
455 /* line_stripname(): strip the leading '/dev' if it exists, return dst */
457 line_stripname(char *dst, const char *src, int dstsize)
459 memset(dst, '\0', dstsize);
460 if (strncmp(src, "/dev/", 5) == 0)
461 strlcpy(dst, &src[5], dstsize);
463 strlcpy(dst, src, dstsize);
468 /* line_abbrevname(): Return the abbreviated (usually four-character)
469 * form of the line (Just use the last <dstsize> characters of the
472 * NOTE: use strncpy because we do NOT necessarily want zero
475 line_abbrevname(char *dst, const char *src, int dstsize) {
476 memset(dst, '\0', dstsize);
477 src += (strlen(src) - dstsize);
478 strncpy(dst, src, dstsize); /* note: _don't_ change this to strlcpy */
484 ** utmp utility functions
486 ** These functions manipulate struct utmp, taking system differences
490 #if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
492 /* build the utmp structure */
494 set_utmp_time(struct logininfo *li, struct utmp *ut)
496 #ifdef HAVE_TV_IN_UTMP
497 ut->ut_tv.tv_sec = li->tv_sec;
498 ut->ut_tv.tv_usec = li->tv_usec;
500 # ifdef HAVE_TIME_IN_UTMP
501 ut->ut_time = li->tv_sec;
508 construct_utmp(struct logininfo *li,
511 memset(ut, '\0', sizeof(struct utmp));
512 #ifdef HAVE_ID_IN_UTMP
513 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
516 #ifdef HAVE_TYPE_IN_UTMP
517 /* This is done here to keep utmp constants out of login.h */
520 ut->ut_type = USER_PROCESS;
523 ut->ut_type = DEAD_PROCESS;
528 #ifdef HAVE_PID_IN_UTMP
529 ut->ut_pid = li->pid;
531 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
532 strlcpy(ut->ut_name, li->username, sizeof(ut->ut_name));
533 set_utmp_time(li, ut);
534 #ifdef HAVE_HOST_IN_UTMP
535 strlcpy(ut->ut_host, li->hostname, sizeof(ut->ut_host));
537 #ifdef HAVE_ADDR_IN_UTMP
538 /* this is just a 32-bit IP address */
539 if (li->hostaddr.sa.sa_family == AF_INET)
540 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
545 /* USE_UTMP || USE_WTMP || USE_LOGIN */
550 ** utmpx utility functions
552 ** These functions manipulate struct utmpx, accounting for system
556 #if defined(USE_UTMPX) || defined (USE_WTMPX)
558 /* build the utmpx structure */
560 set_utmpx_time(struct logininfo *li, struct utmpx *utx)
562 #ifdef HAVE_TV_IN_UTMPX
563 utx->ut_tv.tv_sec = li->tv_sec;
564 utx->ut_tv.tv_usec = li->tv_usec;
566 # ifdef HAVE_TIME_IN_UTMPX
567 utx->ut_time = li->tv_sec;
574 construct_utmpx(struct logininfo *li, struct utmpx *utx)
576 memset(utx, '\0', sizeof(struct utmpx));
577 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
579 /* this is done here to keep utmp constants out of loginrec.h */
582 utx->ut_type = USER_PROCESS;
585 utx->ut_type = DEAD_PROCESS;
588 utx->ut_pid = li->pid;
589 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
590 strlcpy(utx->ut_name, li->username, sizeof(utx->ut_name));
591 set_utmpx_time(li, utx);
592 #ifdef HAVE_HOST_IN_UTMPX
593 strlcpy(utx->ut_host, li->hostname, sizeof(utx->ut_host));
595 #ifdef HAVE_ADDR_IN_UTMPX
596 /* FIXME: (ATL) not supported yet */
598 #ifdef HAVE_SYSLEN_IN_UTMPX
599 /* this is safe because of the extra nulls in logininfo */
600 utx->ut_syslen = strlen(li->hostname);
605 /* USE_UTMPX || USE_WTMPX */
610 ** Low-level utmp functions
613 /* FIXME: (ATL) utmp_write_direct needs testing */
617 /* if we can, use pututline() etc. */
618 #if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
619 defined(HAVE_PUTUTLINE)
620 # define UTMP_USE_LIBRARY
624 /* write a utmp entry with the system's help (pututline() and pals) */
625 #ifdef UTMP_USE_LIBRARY
627 utmp_write_library(struct logininfo *li, struct utmp *ut)
640 /* write a utmp entry direct to the file */
641 /* This is a slightly modification of code in OpenBSD's login.c */
643 utmp_write_direct(struct logininfo *li, struct utmp *ut)
649 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
651 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
652 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
654 * Prevent luser from zero'ing out ut_host.
655 * If the new ut_line is empty but the old one is not
656 * and ut_line and ut_name match, preserve the old ut_line.
658 if ( read(fd, &old_ut, sizeof(struct utmp)) == sizeof(struct utmp)
659 && ut->ut_host[0] == '\0'
660 && old_ut.ut_host[0] != '\0'
661 && strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0
662 && strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0 )
663 (void)memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
665 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
666 if (write(fd, ut, sizeof(struct utmp))==-1)
667 log("utmp_write_direct: error writing %s: %s",
668 UTMP_FILE, strerror(errno));
675 #endif /* UTMP_USE_LIBRARY */
679 utmp_perform_login(struct logininfo *li)
683 construct_utmp(li, &ut);
684 #ifdef UTMP_USE_LIBRARY
685 if (!utmp_write_library(li, &ut)) {
686 log("utmp_perform_login: utmp_write_library() failed");
690 if (!utmp_write_direct(li, &ut)) {
691 log("utmp_perform_login: utmp_write_direct() failed");
700 utmp_perform_logout(struct logininfo *li)
704 memset(&ut, '\0', sizeof(ut));
705 set_utmp_time(li, &ut);
706 line_stripname(ut.ut_line, li->line, sizeof(ut.ut_line));
707 #ifdef HAVE_ID_IN_UTMP
708 line_abbrevname(ut.ut_id, li->line, sizeof(ut.ut_id));
710 #ifdef HAVE_TYPE_IN_UTMP
711 ut.ut_type = DEAD_PROCESS;
714 #if !defined(DISABLE_PUTUTLINE) \
715 && defined(HAVE_SETUTENT) && defined(HAVE_PUTUTLINE)
716 utmp_write_library(li, &ut);
718 utmp_write_direct(li, &ut);
726 utmp_write_entry(struct logininfo *li)
730 return utmp_perform_login(li);
733 return utmp_perform_logout(li);
736 log("utmp_write_entry: invalid type field");
747 ** Low-level utmpx functions
750 /* not much point if we don't want utmpx entries */
753 /* if we have the wherewithall, use pututxline etc. */
754 #if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) \
755 && defined(HAVE_PUTUTXLINE)
756 # define UTMPX_USE_LIBRARY
760 /* write a utmpx entry with the system's help (pututxline() and pals) */
761 #ifdef UTMPX_USE_LIBRARY
763 utmpx_write_library(struct logininfo *li, struct utmpx *utx)
768 #ifdef HAVE_ENDUTXENT
775 /* UTMPX_USE_LIBRARY */
778 /* write a utmp entry direct to the file */
780 utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
782 log("utmpx_write_direct: not implemented!");
787 /* UTMPX_USE_LIBRARY */
790 utmpx_perform_login(struct logininfo *li)
794 construct_utmpx(li, &utx);
795 #ifdef UTMPX_USE_LIBRARY
796 if (!utmpx_write_library(li, &utx)) {
797 log("utmpx_perform_login: utmp_write_library() failed");
801 if (!utmpx_write_direct(li, &ut)) {
802 log("utmpx_perform_login: utmp_write_direct() failed");
811 utmpx_perform_logout(struct logininfo *li)
815 memset(&utx, '\0', sizeof(utx));
816 set_utmpx_time(li, &utx);
817 line_stripname(utx.ut_line, li->line, sizeof(utx.ut_line));
818 #ifdef HAVE_ID_IN_UTMPX
819 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
821 #ifdef HAVE_TYPE_IN_UTMPX
822 utx.ut_type = DEAD_PROCESS;
825 #ifdef UTMPX_USE_LIBRARY
826 utmpx_write_library(li, &utx);
828 utmpx_write_direct(li, &utx);
835 utmpx_write_entry(struct logininfo *li)
839 return utmpx_perform_login(li);
841 return utmpx_perform_logout(li);
843 log("utmpx_write_entry: invalid type field");
854 ** Low-level wtmp functions
859 /* write a wtmp entry direct to the end of the file */
860 /* This is a slight modification of code in OpenBSD's logwtmp.c */
862 wtmp_write(struct logininfo *li, struct utmp *ut)
867 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
868 log("wtmp_write: problem writing %s: %s",
869 WTMP_FILE, strerror(errno));
872 if (fstat(fd, &buf) == 0)
873 if (write(fd, (char *)ut, sizeof(struct utmp)) !=
874 sizeof(struct utmp)) {
875 ftruncate(fd, buf.st_size);
876 log("wtmp_write: problem writing %s: %s",
877 WTMP_FILE, strerror(errno));
886 wtmp_perform_login(struct logininfo *li){
889 construct_utmp(li, &ut);
890 return wtmp_write(li, &ut);
895 wtmp_perform_logout(struct logininfo *li)
899 construct_utmp(li, &ut);
900 /* blank out unnecessary fields */
901 memset(&(ut.ut_name), '\0', sizeof(ut.ut_name));
902 #ifdef HAVE_ID_IN_UTMP
903 memset(&(ut.ut_id), '\0', sizeof(ut.ut_id));
905 #ifdef HAVE_HOST_IN_UTMP
906 memset(&(ut.ut_host), '\0', sizeof(ut.ut_host));
908 #ifdef HAVE_ADDR_IN_UTMP
909 memset(&(ut.ut_addr), '\0', sizeof(ut.ut_addr));
911 return wtmp_write(li, &ut);
916 wtmp_write_entry(struct logininfo *li)
920 return wtmp_perform_login(li);
922 return wtmp_perform_logout(li);
924 log("wtmp_write_entry: invalid type field");
931 wtmp_get_entry(struct logininfo *li)
937 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
938 log("wtmp_get_entry: problem opening %s: %s",
939 WTMP_FILE, strerror(errno));
942 if (fstat(fd, &st) != 0) {
943 log("wtmp_get_entry: couldn't stat %s: %s",
944 WTMP_FILE, strerror(errno));
948 (void)lseek(fd, (off_t)(0-sizeof(struct utmp)), SEEK_END);
951 if (read(fd, &ut, sizeof(ut)) != sizeof(ut)) {
952 log("wtmp_get_entry: read of %s failed: %s",
953 WTMP_FILE, strerror(errno));
957 /* Logouts are recorded as a blank username on a particular line.
958 * So, we just need to find the username in struct utmp */
959 if ( strncmp(li->username, ut.ut_user, 8) == 0 ) {
960 /* note we've already made sure there's a time in struct utmp */
961 #ifdef HAVE_TIME_IN_UTMP
962 li->tv_sec = ut.ut_time;
965 li->tv_sec = ut.ut_tv.tv_sec;
968 line_fullname(li->line, ut.ut_line, sizeof(ut.ut_line));
969 #ifdef HAVE_HOST_IN_UTMP
970 strlcpy(li->hostname, ut.ut_host, sizeof(ut.ut_host));
973 if (lseek(fd, (off_t)(0-2*sizeof(struct utmp)), SEEK_CUR) == -1) {
977 } while (li->tv_sec == 0);
988 ** Low-level wtmpx functions
993 /* write a wtmpx entry direct to the end of the file */
994 /* This is a slight modification of code in OpenBSD's logwtmp.c */
996 wtmpx_write(struct logininfo *li, struct utmpx *utx)
1001 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
1002 log("wtmpx_write: problem opening %s: %s",
1003 WTMPX_FILE, strerror(errno));
1007 if (fstat(fd, &buf) == 0)
1008 if (write(fd, (char *)utx, sizeof(struct utmpx)) !=
1009 sizeof(struct utmpx)) {
1010 ftruncate(fd, buf.st_size);
1011 log("wtmpx_write: problem writing %s: %s",
1012 WTMPX_FILE, strerror(errno));
1022 wtmpx_perform_login(struct logininfo *li)
1026 construct_utmpx(li, &utx);
1027 return wtmpx_write(li, &utx);
1032 wtmpx_perform_logout(struct logininfo *li)
1036 construct_utmpx(li, &utx);
1037 /* blank out unnecessary fields */
1038 memset(&(utx.ut_name), '\0', sizeof(utx.ut_name));
1039 #ifdef HAVE_ID_IN_UTMPX
1040 memset(&(utx.ut_id), '\0', sizeof(utx.ut_id));
1042 #ifdef HAVE_HOST_IN_UTMPX
1043 memset(&(utx.ut_host), '\0', sizeof(utx.ut_host));
1045 #ifdef HAVE_ADDR_IN_UTMPX
1046 memset(&(utx.ut_addr), '\0', sizeof(utx.ut_addr));
1048 return wtmpx_write(li, &utx);
1054 wtmpx_write_entry(struct logininfo *li)
1058 return wtmpx_perform_login(li);
1060 return wtmpx_perform_logout(li);
1062 log("wtmpx_write_entry: invalid type field");
1069 wtmpx_get_entry(struct logininfo *li)
1075 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
1076 log("wtmpx_get_entry: problem opening %s: %s",
1077 WTMPX_FILE, strerror(errno));
1080 if (fstat(fd, &st) != 0) {
1081 log("wtmpx_get_entry: couldn't stat %s: %s",
1082 WTMP_FILE, strerror(errno));
1086 (void)lseek(fd, (off_t)(0-sizeof(struct utmpx)), SEEK_END);
1089 if (read(fd, &utx, sizeof(utx)) != sizeof(utx)) {
1090 log("wtmpx_get_entry: read of %s failed: %s",
1091 WTMPX_FILE, strerror(errno));
1095 /* Logouts are recorded as a blank username on a particular line.
1096 * So, we just need to find the username in struct utmpx */
1097 if ( strncmp(li->username, utx.ut_user, 8) == 0 ) {
1098 /* note we've already made sure there's a time in struct utmp */
1099 #ifdef HAVE_TV_IN_UTMPX
1100 li->tv_sec = utx.ut_tv.tv_sec;
1102 # ifdef HAVE_TIME_IN_UTMPX
1103 li->tv_sec = utx.ut_time;
1106 line_fullname(li->line, utx.ut_line, sizeof(utx.ut_line));
1107 #ifdef HAVE_HOST_IN_UTMPX
1108 strlcpy(li->hostname, utx.ut_host, sizeof(utx.ut_line));
1111 if (lseek(fd, (off_t)(0-2*sizeof(struct utmpx)), SEEK_CUR) == -1) {
1115 } while (li->tv_sec == 0);
1126 ** Low-level libutil login() functions
1132 syslogin_perform_login(struct logininfo *li)
1136 if (! (ut = (struct utmp *)malloc(sizeof(struct utmp)))) {
1137 log("syslogin_perform_login: couldn't malloc()");
1140 construct_utmp(li, ut);
1148 syslogin_perform_logout(struct logininfo *li)
1153 (void)line_stripname(line, li->line, sizeof(line));
1155 if (!logout(line)) {
1156 log("syslogin_perform_logout: logout() returned an error");
1157 # ifdef HAVE_LOGWTMP
1159 logwtmp(line, "", "");
1162 /* TODO: what to do if we have login, but no logout?
1163 * what if logout but no logwtmp? All routines are in libutil
1164 * so they should all be there, but... */
1171 syslogin_write_entry(struct logininfo *li)
1175 return syslogin_perform_login(li);
1177 return syslogin_perform_logout(li);
1179 log("syslogin_write_entry: Invalid type field");
1188 /* end of file log-syslogin.c */
1192 ** Low-level lastlog functions
1198 lastlog_construct(struct logininfo *li, struct lastlog *last)
1200 /* clear the structure */
1201 memset(last, '\0', sizeof(struct lastlog));
1203 (void)line_stripname(last->ll_line, li->line,
1204 sizeof(last->ll_line));
1205 strlcpy(last->ll_host, li->hostname, sizeof(last->ll_host));
1206 last->ll_time = li->tv_sec;
1215 lastlog_filetype(char *filename)
1219 if ( stat(LASTLOG_FILE, &st) != 0) {
1220 log("lastlog_perform_login: Couldn't stat %s: %s",
1221 LASTLOG_FILE, strerror(errno));
1224 if (S_ISDIR(st.st_mode))
1226 else if (S_ISREG(st.st_mode))
1233 /* open the file (using filemode) and seek to the login entry */
1235 lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1239 char lastlog_file[1024];
1241 type = lastlog_filetype(LASTLOG_FILE);
1244 strlcpy(lastlog_file, LASTLOG_FILE, sizeof(lastlog_file));
1247 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1248 LASTLOG_FILE, li->username);
1251 log("lastlog_openseek: %.100s is not a file or directory!",
1256 *fd = open(lastlog_file, filemode);
1258 log("lastlog_openseek: Couldn't open %s: %s",
1259 lastlog_file, strerror(errno));
1262 /* find this uid's offset in the lastlog file */
1263 offset = (off_t) ( (long)li->uid * sizeof(struct lastlog));
1265 if ( lseek(*fd, offset, SEEK_SET) != offset ) {
1266 log("lastlog_openseek: %s->lseek(): %s",
1267 lastlog_file, strerror(errno));
1274 lastlog_perform_login(struct logininfo *li)
1276 struct lastlog last;
1279 /* create our struct lastlog */
1280 lastlog_construct(li, &last);
1282 /* write the entry */
1283 if (lastlog_openseek(li, &fd, O_RDWR)) {
1284 if ( write(fd, &last, sizeof(struct lastlog))
1285 != sizeof(struct lastlog) ) {
1286 log("lastlog_write_filemode: Error writing to %s: %s",
1287 LASTLOG_FILE, strerror(errno));
1297 lastlog_write_entry(struct logininfo *li)
1301 return lastlog_perform_login(li);
1303 log("lastlog_write_entry: Invalid type field");
1310 lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1312 line_fullname(li->line, last->ll_line, sizeof(li->line));
1313 strlcpy(li->hostname, last->ll_host, sizeof(li->hostname));
1314 li->tv_sec = last->ll_time;
1319 lastlog_get_entry(struct logininfo *li)
1321 struct lastlog last;
1324 if (lastlog_openseek(li, &fd, O_RDONLY)) {
1325 if ( read(fd, &last, sizeof(struct lastlog))
1326 != sizeof(struct lastlog) ) {
1327 log("lastlog_write_filemode: Error reading from %s: %s",
1328 LASTLOG_FILE, strerror(errno));
1331 lastlog_populate_entry(li, &last);