summaryrefslogtreecommitdiff
path: root/loginrec.c
diff options
context:
space:
mode:
Diffstat (limited to 'loginrec.c')
-rw-r--r--loginrec.c336
1 files changed, 250 insertions, 86 deletions
diff --git a/loginrec.c b/loginrec.c
index 59ce1808a..e51d686bf 100644
--- a/loginrec.c
+++ b/loginrec.c
@@ -141,10 +141,10 @@
141 ** Known good: 141 ** Known good:
142 ** Linux (Redhat 6.2, need more variants) 142 ** Linux (Redhat 6.2, need more variants)
143 ** HP-UX 10.20 (gcc only) 143 ** HP-UX 10.20 (gcc only)
144 ** IRIX
144 ** 145 **
145 ** Testing required: Please send reports! 146 ** Testing required: Please send reports!
146 ** Solaris 147 ** Solaris
147 ** IRIX
148 ** NetBSD 148 ** NetBSD
149 ** HP-UX 11 149 ** HP-UX 11
150 ** AIX 150 ** AIX
@@ -170,7 +170,7 @@
170#include "xmalloc.h" 170#include "xmalloc.h"
171#include "loginrec.h" 171#include "loginrec.h"
172 172
173RCSID("$Id: loginrec.c,v 1.6 2000/06/13 11:23:17 djm Exp $"); 173RCSID("$Id: loginrec.c,v 1.7 2000/06/19 08:20:03 andre Exp $");
174 174
175/** 175/**
176 ** prototypes for helper functions in this file 176 ** prototypes for helper functions in this file
@@ -199,11 +199,28 @@ int wtmp_get_entry(struct logininfo *li);
199int wtmpx_get_entry(struct logininfo *li); 199int wtmpx_get_entry(struct logininfo *li);
200 200
201 201
202#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
202/** 211/**
203 ** platform-independent login functions 212 ** platform-independent login functions
204 **/ 213 **/
205 214
206/* Record a login */ 215/* 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 */
207int 224int
208login_login (struct logininfo *li) 225login_login (struct logininfo *li)
209{ 226{
@@ -212,7 +229,14 @@ login_login (struct logininfo *li)
212} 229}
213 230
214 231
215/* Record a logout */ 232/* 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 */
216int 240int
217login_logout(struct logininfo *li) 241login_logout(struct logininfo *li)
218{ 242{
@@ -221,22 +245,65 @@ login_logout(struct logininfo *li)
221} 245}
222 246
223 247
224/* Retrieve the last login time for a user (or fake on from wtmp/wtmpx) */ 248/* 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 */
225unsigned int 265unsigned int
226login_get_lastlog_time(const int uid) 266login_get_lastlog_time(const int uid)
227{ 267{
228 struct logininfo li; 268 struct logininfo li;
229 269
230 login_get_lastlog(&li, uid); 270 if (login_get_lastlog(&li, uid))
231 return li.tv_sec; 271 return li.tv_sec;
272 else
273 return 0;
232} 274}
233 275
234/* Retrieve a lastlog entry (or fake one from wtmp/wtmpx) */ 276/* 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 */
235struct logininfo * 289struct logininfo *
236login_get_lastlog(struct logininfo *li, const int uid) 290login_get_lastlog(struct logininfo *li, const int uid)
237{ 291{
292#ifndef USE_LASTLOG
293 struct passwd *pw;
294#endif
295
238 memset(li, '\0', sizeof(struct logininfo)); 296 memset(li, '\0', sizeof(struct logininfo));
239 li->uid = uid; 297 li->uid = uid;
298
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);
304 strlcpy(li->username, pw->pw_name,
305 MIN_SIZEOF(li->username, pw->pw_name));
306#endif
240 if (getlast_entry(li)) 307 if (getlast_entry(li))
241 return li; 308 return li;
242 else 309 else
@@ -244,7 +311,15 @@ login_get_lastlog(struct logininfo *li, const int uid)
244} 311}
245 312
246 313
247/* login_alloc_entry() - allocate and initialise a logininfo */ 314/* login_alloc_entry(int, char*, char*, char*) - Allocate and initialise
315 * a logininfo structure
316 *
317 * This function creates a new struct logininfo, a data structure
318 * meant to carry the information required to portably record login info.
319 *
320 * Returns a pointer to a newly created struct logininfo. If memory
321 * allocation fails, the program halts.
322 */
248struct 323struct
249logininfo *login_alloc_entry(int pid, const char *username, 324logininfo *login_alloc_entry(int pid, const char *username,
250 const char *hostname, const char *line) 325 const char *hostname, const char *line)
@@ -257,7 +332,7 @@ logininfo *login_alloc_entry(int pid, const char *username,
257} 332}
258 333
259 334
260/* login_free_entry() - free struct memory (trivial) */ 335/* login_free_entry(struct logininfo *) - free struct memory */
261void 336void
262login_free_entry(struct logininfo *li) 337login_free_entry(struct logininfo *li)
263{ 338{
@@ -265,7 +340,14 @@ login_free_entry(struct logininfo *li)
265} 340}
266 341
267 342
268/* login_init_entry() - initialise a struct logininfo */ 343/* login_init_entry(struct logininfo *, int, char*, char*, char*)
344 * - initialise a struct logininfo
345 *
346 * Populates a new struct logininfo, a data structure meant to carry
347 * the information required to portably record login info.
348 *
349 * Returns: 1
350 */
269int 351int
270login_init_entry(struct logininfo *li, int pid, const char *username, 352login_init_entry(struct logininfo *li, int pid, const char *username,
271 const char *hostname, const char *line) 353 const char *hostname, const char *line)
@@ -285,7 +367,12 @@ login_init_entry(struct logininfo *li, int pid, const char *username,
285 return 1; 367 return 1;
286} 368}
287 369
288 370/* login_set_current_time(struct logininfo *) - set the current time
371 *
372 * Set the current time in a logininfo structure. This function is
373 * meant to eliminate the need to deal with system dependencies for
374 * time handling.
375 */
289void 376void
290login_set_current_time(struct logininfo *li) 377login_set_current_time(struct logininfo *li)
291{ 378{
@@ -502,12 +589,15 @@ construct_utmp(struct logininfo *li,
502 struct utmp *ut) 589 struct utmp *ut)
503{ 590{
504 memset(ut, '\0', sizeof(struct utmp)); 591 memset(ut, '\0', sizeof(struct utmp));
592
593 /* First fill out fields used for both logins and logouts */
594
505#ifdef HAVE_ID_IN_UTMP 595#ifdef HAVE_ID_IN_UTMP
506 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id)); 596 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
507#endif 597#endif
508 598
509#ifdef HAVE_TYPE_IN_UTMP 599#ifdef HAVE_TYPE_IN_UTMP
510 /* This is done here to keep utmp constants out of login.h */ 600 /* This is done here to keep utmp constants out of struct logininfo */
511 switch (li->type) { 601 switch (li->type) {
512 case LTYPE_LOGIN: 602 case LTYPE_LOGIN:
513 ut->ut_type = USER_PROCESS; 603 ut->ut_type = USER_PROCESS;
@@ -517,15 +607,24 @@ construct_utmp(struct logininfo *li,
517 break; 607 break;
518 } 608 }
519#endif 609#endif
610 set_utmp_time(li, ut);
520 611
612 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
521#ifdef HAVE_PID_IN_UTMP 613#ifdef HAVE_PID_IN_UTMP
522 ut->ut_pid = li->pid; 614 ut->ut_pid = li->pid;
523#endif 615#endif
524 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line)); 616
525 strlcpy(ut->ut_name, li->username, sizeof(ut->ut_name)); 617 /* If we're logging out, leave all other fields blank */
526 set_utmp_time(li, ut); 618 if (li->type == LTYPE_LOGOUT)
619 return;
620
621 /* These fields are only used when logging in, and are blank
622 * for logouts. */
623
624 /* Use strncpy because we don't necessarily want null termination */
625 strncpy(ut->ut_user, li->username, MIN_SIZEOF(ut->ut_user, li->username));
527#ifdef HAVE_HOST_IN_UTMP 626#ifdef HAVE_HOST_IN_UTMP
528 strlcpy(ut->ut_host, li->hostname, sizeof(ut->ut_host)); 627 strncpy(ut->ut_host, li->hostname, MIN_SIZEOF(ut->ut_host, li->hostname));
529#endif 628#endif
530#ifdef HAVE_ADDR_IN_UTMP 629#ifdef HAVE_ADDR_IN_UTMP
531 /* this is just a 32-bit IP address */ 630 /* this is just a 32-bit IP address */
@@ -578,19 +677,27 @@ construct_utmpx(struct logininfo *li, struct utmpx *utx)
578 utx->ut_type = DEAD_PROCESS; 677 utx->ut_type = DEAD_PROCESS;
579 break; 678 break;
580 } 679 }
581 utx->ut_pid = li->pid;
582 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line)); 680 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); 681 set_utmpx_time(li, utx);
682 utx->ut_pid = li->pid;
683
684 if (li->type == LTYPE_LOGOUT)
685 return;
686
687 /* These fields are only used when logging in, and are blank
688 * for logouts. */
689
690 /* strncpy(): Don't necessarily want null termination */
691 strncpy(utx->ut_name, li->username, MIN_SIZEOF(utx->ut_name, li->username));
585#ifdef HAVE_HOST_IN_UTMPX 692#ifdef HAVE_HOST_IN_UTMPX
586 strlcpy(utx->ut_host, li->hostname, sizeof(utx->ut_host)); 693 strncpy(utx->ut_host, li->hostname, MIN_SIZEOF(utx->ut_host, li->hostname));
587#endif 694#endif
588#ifdef HAVE_ADDR_IN_UTMPX 695#ifdef HAVE_ADDR_IN_UTMPX
589 /* FIXME: (ATL) not supported yet */ 696 /* FIXME: (ATL) not supported yet */
590#endif 697#endif
591#ifdef HAVE_SYSLEN_IN_UTMPX 698#ifdef HAVE_SYSLEN_IN_UTMPX
592 /* this is safe because of the extra nulls in logininfo */ 699 /* ut_syslen is the length of the utx_host string */
593 utx->ut_syslen = strlen(li->hostname); 700 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
594#endif 701#endif
595} 702}
596 703
@@ -639,6 +746,7 @@ utmp_write_direct(struct logininfo *li, struct utmp *ut)
639 register int fd; 746 register int fd;
640 int tty; 747 int tty;
641 748
749 /* FIXME: (ATL) ttyslot() needs local implementation */
642 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */ 750 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
643 751
644 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) { 752 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
@@ -658,7 +766,7 @@ utmp_write_direct(struct logininfo *li, struct utmp *ut)
658 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET); 766 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
659 if (write(fd, ut, sizeof(struct utmp))==-1) 767 if (write(fd, ut, sizeof(struct utmp))==-1)
660 log("utmp_write_direct: error writing %s: %s", 768 log("utmp_write_direct: error writing %s: %s",
661 UTMP_FILE, strerror(errno)); 769 UTMP_FILE, strerror(errno));
662 770
663 (void)close(fd); 771 (void)close(fd);
664 return 1; 772 return 1;
@@ -676,7 +784,7 @@ utmp_perform_login(struct logininfo *li)
676 construct_utmp(li, &ut); 784 construct_utmp(li, &ut);
677#ifdef UTMP_USE_LIBRARY 785#ifdef UTMP_USE_LIBRARY
678 if (!utmp_write_library(li, &ut)) { 786 if (!utmp_write_library(li, &ut)) {
679 log("utmp_perform_login: utmp_write_library() failed"); 787 log("utmp_perform_login: utmp_write_library() failed");
680 return 0; 788 return 0;
681 } 789 }
682#else 790#else
@@ -694,23 +802,18 @@ utmp_perform_logout(struct logininfo *li)
694{ 802{
695 struct utmp ut; 803 struct utmp ut;
696 804
697 memset(&ut, '\0', sizeof(ut)); 805 construct_utmp(li, &ut);
698 set_utmp_time(li, &ut); 806#ifdef UTMP_USE_LIBRARY
699 line_stripname(ut.ut_line, li->line, sizeof(ut.ut_line)); 807 if (!utmp_write_library(li, &ut)) {
700#ifdef HAVE_ID_IN_UTMP 808 log("utmp_perform_logout: utmp_write_library() failed");
701 line_abbrevname(ut.ut_id, li->line, sizeof(ut.ut_id)); 809 return 0;
702#endif 810 }
703#ifdef HAVE_TYPE_IN_UTMP
704 ut.ut_type = DEAD_PROCESS;
705#endif
706
707#if !defined(DISABLE_PUTUTLINE) \
708 && defined(HAVE_SETUTENT) && defined(HAVE_PUTUTLINE)
709 utmp_write_library(li, &ut);
710#else 811#else
711 utmp_write_direct(li, &ut); 812 if (!utmp_write_direct(li, &ut)) {
813 log("utmp_perform_logout: utmp_write_direct() failed");
814 return 0;
815 }
712#endif 816#endif
713
714 return 1; 817 return 1;
715} 818}
716 819
@@ -890,17 +993,6 @@ wtmp_perform_logout(struct logininfo *li)
890 struct utmp ut; 993 struct utmp ut;
891 994
892 construct_utmp(li, &ut); 995 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));
897#endif
898#ifdef HAVE_HOST_IN_UTMP
899 memset(&(ut.ut_host), '\0', sizeof(ut.ut_host));
900#endif
901#ifdef HAVE_ADDR_IN_UTMP
902 memset(&(ut.ut_addr), '\0', sizeof(ut.ut_addr));
903#endif
904 return wtmp_write(li, &ut); 996 return wtmp_write(li, &ut);
905} 997}
906 998
@@ -920,12 +1012,49 @@ wtmp_write_entry(struct logininfo *li)
920} 1012}
921 1013
922 1014
1015/* Notes on fetching login data from wtmp/wtmpx
1016 *
1017 * Logouts are usually recorded with (amongst other things) a blank
1018 * username on a given tty line. However, some systems (HP-UX is one)
1019 * leave all fields set, but change the ut_type field to DEAD_PROCESS.
1020 *
1021 * Since we're only looking for logins here, we know that the username
1022 * must be set correctly. On systems that leave it in, we check for
1023 * ut_type==USER_PROCESS (indicating a login.)
1024 *
1025 * Portability: Some systems may set something other than USER_PROCESS
1026 * to indicate a login process. I don't know of any as I write. Also,
1027 * it's possible that some systems may both leave the username in
1028 * place and not have ut_type.
1029 */
1030
1031
1032/* return true if this wtmp entry indicates a login */
1033static int
1034wtmp_islogin(struct logininfo *li, struct utmp *ut)
1035{
1036 if ( strncmp(li->username, ut->ut_user,
1037 MIN_SIZEOF(li->username, ut->ut_user)) == 0 ) {
1038#ifdef HAVE_TYPE_IN_UTMP
1039 if (ut->ut_type & USER_PROCESS)
1040 return 1;
1041#else
1042 return 1;
1043#endif
1044 }
1045 return 0;
1046}
1047
1048
923int 1049int
924wtmp_get_entry(struct logininfo *li) 1050wtmp_get_entry(struct logininfo *li)
925{ 1051{
926 struct stat st; 1052 struct stat st;
927 struct utmp ut; 1053 struct utmp ut;
928 int fd; 1054 int fd, found=0;
1055
1056 /* Clear the time entries in our logininfo */
1057 li->tv_sec = li->tv_usec = 0;
929 1058
930 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) { 1059 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
931 log("wtmp_get_entry: problem opening %s: %s", 1060 log("wtmp_get_entry: problem opening %s: %s",
@@ -938,19 +1067,25 @@ wtmp_get_entry(struct logininfo *li)
938 close(fd); 1067 close(fd);
939 return 0; 1068 return 0;
940 } 1069 }
941 (void)lseek(fd, (off_t)(0-sizeof(struct utmp)), SEEK_END);
942 1070
943 do { 1071 /* Seek to the start of the last struct utmp */
1072 if (lseek(fd, (off_t)(0-sizeof(struct utmp)), SEEK_END) == -1) {
1073 /* Looks like we've got a fresh wtmp file */
1074 close(fd);
1075 return 0;
1076 }
1077
1078 while (!found) {
944 if (read(fd, &ut, sizeof(ut)) != sizeof(ut)) { 1079 if (read(fd, &ut, sizeof(ut)) != sizeof(ut)) {
945 log("wtmp_get_entry: read of %s failed: %s", 1080 log("wtmp_get_entry: read of %s failed: %s",
946 WTMP_FILE, strerror(errno)); 1081 WTMP_FILE, strerror(errno));
947 close (fd); 1082 close (fd);
948 return 0; 1083 return 0;
949 } 1084 }
950 /* Logouts are recorded as a blank username on a particular line. 1085 if ( wtmp_islogin(li, &ut) ) {
951 * So, we just need to find the username in struct utmp */ 1086 found = 1;
952 if ( strncmp(li->username, ut.ut_user, 8) == 0 ) { 1087 /* We've already checked for a time in struct
953 /* note we've already made sure there's a time in struct utmp */ 1088 * utmp, in login_getlast(). */
954#ifdef HAVE_TIME_IN_UTMP 1089#ifdef HAVE_TIME_IN_UTMP
955 li->tv_sec = ut.ut_time; 1090 li->tv_sec = ut.ut_time;
956#else 1091#else
@@ -958,17 +1093,24 @@ wtmp_get_entry(struct logininfo *li)
958 li->tv_sec = ut.ut_tv.tv_sec; 1093 li->tv_sec = ut.ut_tv.tv_sec;
959# endif 1094# endif
960#endif 1095#endif
961 line_fullname(li->line, ut.ut_line, sizeof(li->line)); 1096 line_fullname(li->line, ut.ut_line,
1097 MIN_SIZEOF(li->line, ut.ut_line));
962#ifdef HAVE_HOST_IN_UTMP 1098#ifdef HAVE_HOST_IN_UTMP
963 strlcpy(li->hostname, ut.ut_host, sizeof(li->hostname)); 1099 strlcpy(li->hostname, ut.ut_host,
1100 MIN_SIZEOF(li->hostname, ut.ut_host));
964#endif 1101#endif
1102 continue;
965 } 1103 }
1104 /* Seek back 2 x struct utmp */
966 if (lseek(fd, (off_t)(0-2*sizeof(struct utmp)), SEEK_CUR) == -1) { 1105 if (lseek(fd, (off_t)(0-2*sizeof(struct utmp)), SEEK_CUR) == -1) {
1106 /* We've found the start of the file, so quit */
967 close (fd); 1107 close (fd);
968 return 0; 1108 return 0;
969 } 1109 }
970 } while (li->tv_sec == 0); 1110 }
971 1111
1112 /* We found an entry. Tidy up and return */
1113 close(fd);
972 return 1; 1114 return 1;
973} 1115}
974 1116
@@ -1027,19 +1169,7 @@ wtmpx_perform_logout(struct logininfo *li)
1027 struct utmpx utx; 1169 struct utmpx utx;
1028 1170
1029 construct_utmpx(li, &utx); 1171 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));
1034#endif
1035#ifdef HAVE_HOST_IN_UTMPX
1036 memset(&(utx.ut_host), '\0', sizeof(utx.ut_host));
1037#endif
1038#ifdef HAVE_ADDR_IN_UTMPX
1039 memset(&(utx.ut_addr), '\0', sizeof(utx.ut_addr));
1040#endif
1041 return wtmpx_write(li, &utx); 1172 return wtmpx_write(li, &utx);
1042
1043} 1173}
1044 1174
1045 1175
@@ -1057,13 +1187,35 @@ wtmpx_write_entry(struct logininfo *li)
1057 } 1187 }
1058} 1188}
1059 1189
1190/* Please see the notes above wtmp_islogin() for information about the
1191 next two functions */
1192
1193/* Return true if this wtmpx entry indicates a login */
1194static int
1195wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1196{
1197 if ( strncmp(li->username, utx->ut_user,
1198 MIN_SIZEOF(li->username, utx->us_user)) == 0 ) {
1199#ifdef HAVE_TYPE_IN_UTMPX
1200 if (utx->ut_type == USER_PROCESS)
1201 return 1;
1202#else
1203 return 1;
1204#endif
1205 }
1206 return 0;
1207}
1208
1060 1209
1061int 1210int
1062wtmpx_get_entry(struct logininfo *li) 1211wtmpx_get_entry(struct logininfo *li)
1063{ 1212{
1064 struct stat st; 1213 struct stat st;
1065 struct utmpx utx; 1214 struct utmpx utx;
1066 int fd; 1215 int fd, found=0;
1216
1217 /* Clear the time entries */
1218 li->tv_sec = li->tv_usec = 0;
1067 1219
1068 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) { 1220 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
1069 log("wtmpx_get_entry: problem opening %s: %s", 1221 log("wtmpx_get_entry: problem opening %s: %s",
@@ -1076,9 +1228,15 @@ wtmpx_get_entry(struct logininfo *li)
1076 close(fd); 1228 close(fd);
1077 return 0; 1229 return 0;
1078 } 1230 }
1079 (void)lseek(fd, (off_t)(0-sizeof(struct utmpx)), SEEK_END); 1231
1232 /* Seek to the start of the last struct utmpx */
1233 if (lseek(fd, (off_t)(0-sizeof(struct utmpx)), SEEK_END) == -1 ) {
1234 /* probably a newly rotated wtmpx file */
1235 close(fd);
1236 return 0;
1237 }
1080 1238
1081 do { 1239 while (!found) {
1082 if (read(fd, &utx, sizeof(utx)) != sizeof(utx)) { 1240 if (read(fd, &utx, sizeof(utx)) != sizeof(utx)) {
1083 log("wtmpx_get_entry: read of %s failed: %s", 1241 log("wtmpx_get_entry: read of %s failed: %s",
1084 WTMPX_FILE, strerror(errno)); 1242 WTMPX_FILE, strerror(errno));
@@ -1087,8 +1245,7 @@ wtmpx_get_entry(struct logininfo *li)
1087 } 1245 }
1088 /* Logouts are recorded as a blank username on a particular line. 1246 /* Logouts are recorded as a blank username on a particular line.
1089 * So, we just need to find the username in struct utmpx */ 1247 * So, we just need to find the username in struct utmpx */
1090 if ( strncmp(li->username, utx.ut_user, 8) == 0 ) { 1248 if ( wtmpx_islogin(li, &utx) ) {
1091 /* note we've already made sure there's a time in struct utmp */
1092#ifdef HAVE_TV_IN_UTMPX 1249#ifdef HAVE_TV_IN_UTMPX
1093 li->tv_sec = utx.ut_tv.tv_sec; 1250 li->tv_sec = utx.ut_tv.tv_sec;
1094#else 1251#else
@@ -1098,14 +1255,18 @@ wtmpx_get_entry(struct logininfo *li)
1098#endif 1255#endif
1099 line_fullname(li->line, utx.ut_line, sizeof(li->line)); 1256 line_fullname(li->line, utx.ut_line, sizeof(li->line));
1100#ifdef HAVE_HOST_IN_UTMPX 1257#ifdef HAVE_HOST_IN_UTMPX
1101 strlcpy(li->hostname, utx.ut_host, sizeof(li->hostname)); 1258 strlcpy(li->hostname, utx.ut_host,
1259 MIN_SIZEOF(li->hostname, utx.ut_host));
1102#endif 1260#endif
1261 continue;
1103 } 1262 }
1104 if (lseek(fd, (off_t)(0-2*sizeof(struct utmpx)), SEEK_CUR) == -1) { 1263 if (lseek(fd, (off_t)(0-2*sizeof(struct utmpx)), SEEK_CUR) == -1) {
1105 close (fd); 1264 close (fd);
1106 return 0; 1265 return 0;
1107 } 1266 }
1108 } while (li->tv_sec == 0); 1267 }
1268
1269 close(fd);
1109 return 1; 1270 return 1;
1110} 1271}
1111 1272
@@ -1150,9 +1311,10 @@ syslogin_perform_logout(struct logininfo *li)
1150 logwtmp(line, "", ""); 1311 logwtmp(line, "", "");
1151 } 1312 }
1152# endif 1313# endif
1153 /* TODO: what to do if we have login, but no logout? 1314 /* FIXME: (ATL - if the need arises) What to do if we have
1154 * what if logout but no logwtmp? All routines are in libutil 1315 * login, but no logout? what if logout but no logwtmp? All
1155 * so they should all be there, but... */ 1316 * routines are in libutil so they should all be there,
1317 * but... */
1156#endif 1318#endif
1157 return 1; 1319 return 1;
1158} 1320}
@@ -1192,7 +1354,8 @@ lastlog_construct(struct logininfo *li, struct lastlog *last)
1192 1354
1193 (void)line_stripname(last->ll_line, li->line, 1355 (void)line_stripname(last->ll_line, li->line,
1194 sizeof(last->ll_line)); 1356 sizeof(last->ll_line));
1195 strlcpy(last->ll_host, li->hostname, sizeof(last->ll_host)); 1357 strlcpy(last->ll_host, li->hostname,
1358 MIN_SIZEOF(last->ll_host, li->hostname));
1196 last->ll_time = li->tv_sec; 1359 last->ll_time = li->tv_sec;
1197} 1360}
1198 1361
@@ -1300,7 +1463,8 @@ static void
1300lastlog_populate_entry(struct logininfo *li, struct lastlog *last) 1463lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1301{ 1464{
1302 line_fullname(li->line, last->ll_line, sizeof(li->line)); 1465 line_fullname(li->line, last->ll_line, sizeof(li->line));
1303 strlcpy(li->hostname, last->ll_host, sizeof(li->hostname)); 1466 strlcpy(li->hostname, last->ll_host,
1467 MIN_SIZEOF(li->hostname, last->ll_host));
1304 li->tv_sec = last->ll_time; 1468 li->tv_sec = last->ll_time;
1305} 1469}
1306 1470