summaryrefslogtreecommitdiff
path: root/loginrec.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2000-06-27 11:18:27 +1000
committerDamien Miller <djm@mindrot.org>2000-06-27 11:18:27 +1000
commitdd47aa21fa928354cb53a423b4f1ebcd64a83d27 (patch)
tree9c89e26ce4de7f8dd246626e9fe3d996bbd30112 /loginrec.c
parentf8af08d7cb8e4fc0c998291a76ca1f4057972565 (diff)
- (djm) Formatting
Diffstat (limited to 'loginrec.c')
-rw-r--r--loginrec.c339
1 files changed, 145 insertions, 194 deletions
diff --git a/loginrec.c b/loginrec.c
index e4cb2a64e..5b487eb70 100644
--- a/loginrec.c
+++ b/loginrec.c
@@ -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.10 2000/06/26 23:40:06 djm Exp $"); 173RCSID("$Id: loginrec.c,v 1.11 2000/06/27 01:18:27 djm Exp $");
174 174
175/** 175/**
176 ** prototypes for helper functions in this file 176 ** prototypes for helper functions in this file
@@ -237,7 +237,6 @@ login_logout(struct logininfo *li)
237 return login_write(li); 237 return login_write(li);
238} 238}
239 239
240
241/* login_get_lastlog_time(int) - Retrieve the last login time 240/* login_get_lastlog_time(int) - Retrieve the last login time
242 * 241 *
243 * Retrieve the last login time for the given uid. Will try to use the 242 * Retrieve the last login time for the given uid. Will try to use the
@@ -294,14 +293,18 @@ login_get_lastlog(struct logininfo *li, const int uid)
294 * reliably search wtmp(x) for the last login (see 293 * reliably search wtmp(x) for the last login (see
295 * wtmp_get_entry().) */ 294 * wtmp_get_entry().) */
296 pw = getpwuid(uid); 295 pw = getpwuid(uid);
296 if (pw == NULL)
297 fatal("login_get_lastlog: Cannot find account for uid %i", uid);
298
297 /* No MIN_SIZEOF here - we absolutely *must not* truncate the 299 /* No MIN_SIZEOF here - we absolutely *must not* truncate the
298 * username */ 300 * username */
299 strlcpy(li->username, pw->pw_name, sizeof(li->username)); 301 strlcpy(li->username, pw->pw_name, sizeof(li->username));
300#endif 302#endif
303
301 if (getlast_entry(li)) 304 if (getlast_entry(li))
302 return li; 305 return li;
303 else 306 else
304 return 0; 307 return NULL;
305} 308}
306 309
307 310
@@ -348,7 +351,6 @@ login_init_entry(struct logininfo *li, int pid, const char *username,
348{ 351{
349 struct passwd *pw; 352 struct passwd *pw;
350 353
351 /* zero the structure */
352 memset(li, 0, sizeof(struct logininfo)); 354 memset(li, 0, sizeof(struct logininfo));
353 355
354 li->pid = pid; 356 li->pid = pid;
@@ -364,6 +366,7 @@ login_init_entry(struct logininfo *li, int pid, const char *username,
364 fatal("login_init_entry: Cannot find user \"%s\"", li->username); 366 fatal("login_init_entry: Cannot find user \"%s\"", li->username);
365 li->uid = pw->pw_uid; 367 li->uid = pw->pw_uid;
366 } 368 }
369
367 if (hostname) 370 if (hostname)
368 strlcpy(li->hostname, hostname, sizeof(li->hostname)); 371 strlcpy(li->hostname, hostname, sizeof(li->hostname));
369 372
@@ -387,7 +390,6 @@ login_set_current_time(struct logininfo *li)
387 li->tv_usec = tv.tv_usec; 390 li->tv_usec = tv.tv_usec;
388} 391}
389 392
390
391/* copy a sockaddr_* into our logininfo */ 393/* copy a sockaddr_* into our logininfo */
392void 394void
393login_set_addr(struct logininfo *li, const struct sockaddr *sa, 395login_set_addr(struct logininfo *li, const struct sockaddr *sa,
@@ -407,7 +409,6 @@ login_set_addr(struct logininfo *li, const struct sockaddr *sa,
407 ** login_write: Call low-level recording functions based on autoconf 409 ** login_write: Call low-level recording functions based on autoconf
408 ** results 410 ** results
409 **/ 411 **/
410
411int 412int
412login_write (struct logininfo *li) 413login_write (struct logininfo *li)
413{ 414{
@@ -415,6 +416,7 @@ login_write (struct logininfo *li)
415 log("Attempt to write login records by non-root user (aborting)"); 416 log("Attempt to write login records by non-root user (aborting)");
416 return 1; 417 return 1;
417 } 418 }
419
418 /* set the timestamp */ 420 /* set the timestamp */
419 login_set_current_time(li); 421 login_set_current_time(li);
420#ifdef USE_LOGIN 422#ifdef USE_LOGIN
@@ -440,7 +442,6 @@ login_write (struct logininfo *li)
440 return 0; 442 return 0;
441} 443}
442 444
443
444/** 445/**
445 ** getlast_entry: Call low-level functions to retrieve the last login 446 ** getlast_entry: Call low-level functions to retrieve the last login
446 ** time. 447 ** time.
@@ -455,45 +456,29 @@ getlast_entry(struct logininfo *li)
455 return 1; 456 return 1;
456 else 457 else
457 return 0; 458 return 0;
458#else 459#else /* !USE_LASTLOG */
459 /* !USE_LASTLOG */
460 460
461# ifdef DISABLE_LASTLOG 461#ifdef DISABLE_LASTLOG
462 /* On some systems we shouldn't even try to obtain last login 462 /* On some systems we shouldn't even try to obtain last login
463 * time, e.g. AIX */ 463 * time, e.g. AIX */
464 return 0; 464 return 0;
465 465# else /* DISABLE_LASTLOG */
466# else
467 /* Try to retrieve the last login time from wtmp */ 466 /* Try to retrieve the last login time from wtmp */
468# if defined(USE_WTMP) && (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP)) 467# if defined(USE_WTMP) && (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
469 /* retrieve last login time from utmp */ 468 /* retrieve last login time from utmp */
470 if (wtmp_get_entry(li)) 469 return (wtmp_get_entry(li));
471 return 1; 470# else /* defined(USE_WTMP) && (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP)) */
472 else
473 return 0;
474# else
475
476 /* If wtmp isn't available, try wtmpx */ 471 /* If wtmp isn't available, try wtmpx */
477 472# if defined(USE_WTMPX) && (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
478# if defined(USE_WTMPX) && (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
479 /* retrieve last login time from utmpx */ 473 /* retrieve last login time from utmpx */
480 if (wtmpx_get_entry(li)) 474 return (wtmpx_get_entry(li));
481 return 1; 475# else
482 else
483 return 0;
484# else
485
486 /* Give up: No means of retrieving last login time */ 476 /* Give up: No means of retrieving last login time */
487 return 0; 477 return 0;
488# endif 478# endif /* USE_WTMPX && (HAVE_TIME_IN_UTMPX || HAVE_TV_IN_UTMPX) */
489 /* USE_WTMPX && (HAVE_TIME_IN_UTMPX || HAVE_TV_IN_UTMPX) */ 479# endif /* USE_WTMP && (HAVE_TIME_IN_UTMP || HAVE_TV_IN_UTMP) */
490 480# endif /* DISABLE_LASTLOG */
491# endif 481#endif /* USE_LASTLOG */
492 /* USE_WTMP && (HAVE_TIME_IN_UTMP || HAVE_TV_IN_UTMP) */
493# endif
494 /* DISABLE_LASTLOG */
495#endif
496/* USE_LASTLOG */
497} 482}
498 483
499 484
@@ -530,7 +515,6 @@ line_fullname(char *dst, const char *src, int dstsize)
530 return dst; 515 return dst;
531} 516}
532 517
533
534/* line_stripname(): strip the leading '/dev' if it exists, return dst */ 518/* line_stripname(): strip the leading '/dev' if it exists, return dst */
535char * 519char *
536line_stripname(char *dst, const char *src, int dstsize) 520line_stripname(char *dst, const char *src, int dstsize)
@@ -543,7 +527,6 @@ line_stripname(char *dst, const char *src, int dstsize)
543 return dst; 527 return dst;
544} 528}
545 529
546
547/* line_abbrevname(): Return the abbreviated (usually four-character) 530/* line_abbrevname(): Return the abbreviated (usually four-character)
548 * form of the line (Just use the last <dstsize> characters of the 531 * form of the line (Just use the last <dstsize> characters of the
549 * full name.) 532 * full name.)
@@ -551,14 +534,22 @@ line_stripname(char *dst, const char *src, int dstsize)
551 * NOTE: use strncpy because we do NOT necessarily want zero 534 * NOTE: use strncpy because we do NOT necessarily want zero
552 * termination */ 535 * termination */
553char * 536char *
554line_abbrevname(char *dst, const char *src, int dstsize) { 537line_abbrevname(char *dst, const char *src, int dstsize)
538{
539 size_t len;
540
555 memset(dst, '\0', dstsize); 541 memset(dst, '\0', dstsize);
556 src += (strlen(src) - dstsize); 542
557 strncpy(dst, src, dstsize); /* note: _don't_ change this to strlcpy */ 543 len = strlen(src);
544
545 if (len <= 0) {
546 src += (len - dstsize);
547 strncpy(dst, src, dstsize); /* note: _don't_ change this to strlcpy */
548 }
549
558 return dst; 550 return dst;
559} 551}
560 552
561
562/** 553/**
563 ** utmp utility functions 554 ** utmp utility functions
564 ** 555 **
@@ -572,17 +563,16 @@ line_abbrevname(char *dst, const char *src, int dstsize) {
572void 563void
573set_utmp_time(struct logininfo *li, struct utmp *ut) 564set_utmp_time(struct logininfo *li, struct utmp *ut)
574{ 565{
575#ifdef HAVE_TV_IN_UTMP 566# ifdef HAVE_TV_IN_UTMP
576 ut->ut_tv.tv_sec = li->tv_sec; 567 ut->ut_tv.tv_sec = li->tv_sec;
577 ut->ut_tv.tv_usec = li->tv_usec; 568 ut->ut_tv.tv_usec = li->tv_usec;
578#else 569# else
579# ifdef HAVE_TIME_IN_UTMP 570# ifdef HAVE_TIME_IN_UTMP
580 ut->ut_time = li->tv_sec; 571 ut->ut_time = li->tv_sec;
581# endif 572# endif
582#endif 573# endif
583} 574}
584 575
585
586void 576void
587construct_utmp(struct logininfo *li, 577construct_utmp(struct logininfo *li,
588 struct utmp *ut) 578 struct utmp *ut)
@@ -591,11 +581,11 @@ construct_utmp(struct logininfo *li,
591 581
592 /* First fill out fields used for both logins and logouts */ 582 /* First fill out fields used for both logins and logouts */
593 583
594#ifdef HAVE_ID_IN_UTMP 584# ifdef HAVE_ID_IN_UTMP
595 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id)); 585 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
596#endif 586# endif
597 587
598#ifdef HAVE_TYPE_IN_UTMP 588# ifdef HAVE_TYPE_IN_UTMP
599 /* This is done here to keep utmp constants out of struct logininfo */ 589 /* This is done here to keep utmp constants out of struct logininfo */
600 switch (li->type) { 590 switch (li->type) {
601 case LTYPE_LOGIN: 591 case LTYPE_LOGIN:
@@ -605,37 +595,36 @@ construct_utmp(struct logininfo *li,
605 ut->ut_type = DEAD_PROCESS; 595 ut->ut_type = DEAD_PROCESS;
606 break; 596 break;
607 } 597 }
608#endif 598# endif
609 set_utmp_time(li, ut); 599 set_utmp_time(li, ut);
610 600
611 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line)); 601 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
612#ifdef HAVE_PID_IN_UTMP 602
603# ifdef HAVE_PID_IN_UTMP
613 ut->ut_pid = li->pid; 604 ut->ut_pid = li->pid;
614#endif 605# endif
615 606
616 /* If we're logging out, leave all other fields blank */ 607 /* If we're logging out, leave all other fields blank */
617 if (li->type == LTYPE_LOGOUT) 608 if (li->type == LTYPE_LOGOUT)
618 return; 609 return;
619 610
620 /* These fields are only used when logging in, and are blank 611 /*
621 * for logouts. */ 612 * These fields are only used when logging in, and are blank
613 * for logouts.
614 */
622 615
623 /* Use strncpy because we don't necessarily want null termination */ 616 /* Use strncpy because we don't necessarily want null termination */
624 strncpy(ut->ut_user, li->username, MIN_SIZEOF(ut->ut_user, li->username)); 617 strncpy(ut->ut_user, li->username, MIN_SIZEOF(ut->ut_user, li->username));
625#ifdef HAVE_HOST_IN_UTMP 618# ifdef HAVE_HOST_IN_UTMP
626 strncpy(ut->ut_host, li->hostname, MIN_SIZEOF(ut->ut_host, li->hostname)); 619 strncpy(ut->ut_host, li->hostname, MIN_SIZEOF(ut->ut_host, li->hostname));
627#endif 620# endif
628#ifdef HAVE_ADDR_IN_UTMP 621# ifdef HAVE_ADDR_IN_UTMP
629 /* this is just a 32-bit IP address */ 622 /* this is just a 32-bit IP address */
630 if (li->hostaddr.sa.sa_family == AF_INET) 623 if (li->hostaddr.sa.sa_family == AF_INET)
631 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr; 624 ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr;
632#endif 625# endif
633} 626}
634 627#endif /* USE_UTMP || USE_WTMP || USE_LOGIN */
635#endif
636/* USE_UTMP || USE_WTMP || USE_LOGIN */
637
638
639 628
640/** 629/**
641 ** utmpx utility functions 630 ** utmpx utility functions
@@ -645,22 +634,20 @@ construct_utmp(struct logininfo *li,
645 **/ 634 **/
646 635
647#if defined(USE_UTMPX) || defined (USE_WTMPX) 636#if defined(USE_UTMPX) || defined (USE_WTMPX)
648
649/* build the utmpx structure */ 637/* build the utmpx structure */
650void 638void
651set_utmpx_time(struct logininfo *li, struct utmpx *utx) 639set_utmpx_time(struct logininfo *li, struct utmpx *utx)
652{ 640{
653#ifdef HAVE_TV_IN_UTMPX 641# ifdef HAVE_TV_IN_UTMPX
654 utx->ut_tv.tv_sec = li->tv_sec; 642 utx->ut_tv.tv_sec = li->tv_sec;
655 utx->ut_tv.tv_usec = li->tv_usec; 643 utx->ut_tv.tv_usec = li->tv_usec;
656#else 644# else /* HAVE_TV_IN_UTMPX */
657# ifdef HAVE_TIME_IN_UTMPX 645# ifdef HAVE_TIME_IN_UTMPX
658 utx->ut_time = li->tv_sec; 646 utx->ut_time = li->tv_sec;
659# endif 647# endif /* HAVE_TIME_IN_UTMPX */
660#endif 648# endif /* HAVE_TV_IN_UTMPX */
661} 649}
662 650
663
664void 651void
665construct_utmpx(struct logininfo *li, struct utmpx *utx) 652construct_utmpx(struct logininfo *li, struct utmpx *utx)
666{ 653{
@@ -683,58 +670,54 @@ construct_utmpx(struct logininfo *li, struct utmpx *utx)
683 if (li->type == LTYPE_LOGOUT) 670 if (li->type == LTYPE_LOGOUT)
684 return; 671 return;
685 672
686 /* These fields are only used when logging in, and are blank 673 /*
687 * for logouts. */ 674 * These fields are only used when logging in, and are blank
675 * for logouts.
676 */
688 677
689 /* strncpy(): Don't necessarily want null termination */ 678 /* strncpy(): Don't necessarily want null termination */
690 strncpy(utx->ut_name, li->username, MIN_SIZEOF(utx->ut_name, li->username)); 679 strncpy(utx->ut_name, li->username, MIN_SIZEOF(utx->ut_name, li->username));
691#ifdef HAVE_HOST_IN_UTMPX 680# ifdef HAVE_HOST_IN_UTMPX
692 strncpy(utx->ut_host, li->hostname, MIN_SIZEOF(utx->ut_host, li->hostname)); 681 strncpy(utx->ut_host, li->hostname, MIN_SIZEOF(utx->ut_host, li->hostname));
693#endif 682# endif
694#ifdef HAVE_ADDR_IN_UTMPX 683# ifdef HAVE_ADDR_IN_UTMPX
695 /* FIXME: (ATL) not supported yet */ 684 /* FIXME: (ATL) not supported yet */
696#endif 685# endif
697#ifdef HAVE_SYSLEN_IN_UTMPX 686# ifdef HAVE_SYSLEN_IN_UTMPX
698 /* ut_syslen is the length of the utx_host string */ 687 /* ut_syslen is the length of the utx_host string */
699 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host)); 688 utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host));
700#endif 689# endif
701} 690}
702 691#endif /* USE_UTMPX || USE_WTMPX */
703#endif
704/* USE_UTMPX || USE_WTMPX */
705
706
707 692
708/** 693/**
709 ** Low-level utmp functions 694 ** Low-level utmp functions
710 **/ 695 **/
711 696
712/* FIXME: (ATL) utmp_write_direct needs testing */ 697/* FIXME: (ATL) utmp_write_direct needs testing */
713
714#ifdef USE_UTMP 698#ifdef USE_UTMP
715 699
716/* if we can, use pututline() etc. */ 700/* if we can, use pututline() etc. */
717#if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \ 701# if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
718 defined(HAVE_PUTUTLINE) 702 defined(HAVE_PUTUTLINE)
719# define UTMP_USE_LIBRARY 703# define UTMP_USE_LIBRARY
720#endif 704# endif
721 705
722 706
723/* write a utmp entry with the system's help (pututline() and pals) */ 707/* write a utmp entry with the system's help (pututline() and pals) */
724#ifdef UTMP_USE_LIBRARY 708# ifdef UTMP_USE_LIBRARY
725static int 709static int
726utmp_write_library(struct logininfo *li, struct utmp *ut) 710utmp_write_library(struct logininfo *li, struct utmp *ut)
727{ 711{
728 setutent(); 712 setutent();
729 pututline(ut); 713 pututline(ut);
730 714
731#ifdef HAVE_ENDUTENT 715# ifdef HAVE_ENDUTENT
732 endutent(); 716 endutent();
733#endif 717# endif
734 return 1; 718 return 1;
735} 719}
736 720# else /* UTMP_USE_LIBRARY */
737#else
738 721
739/* write a utmp entry direct to the file */ 722/* write a utmp entry direct to the file */
740/* This is a slightly modification of code in OpenBSD's login.c */ 723/* This is a slightly modification of code in OpenBSD's login.c */
@@ -772,8 +755,7 @@ utmp_write_direct(struct logininfo *li, struct utmp *ut)
772 } else 755 } else
773 return 0; 756 return 0;
774} 757}
775#endif /* UTMP_USE_LIBRARY */ 758# endif /* UTMP_USE_LIBRARY */
776
777 759
778static int 760static int
779utmp_perform_login(struct logininfo *li) 761utmp_perform_login(struct logininfo *li)
@@ -781,17 +763,17 @@ utmp_perform_login(struct logininfo *li)
781 struct utmp ut; 763 struct utmp ut;
782 764
783 construct_utmp(li, &ut); 765 construct_utmp(li, &ut);
784#ifdef UTMP_USE_LIBRARY 766# ifdef UTMP_USE_LIBRARY
785 if (!utmp_write_library(li, &ut)) { 767 if (!utmp_write_library(li, &ut)) {
786 log("utmp_perform_login: utmp_write_library() failed"); 768 log("utmp_perform_login: utmp_write_library() failed");
787 return 0; 769 return 0;
788 } 770 }
789#else 771# else
790 if (!utmp_write_direct(li, &ut)) { 772 if (!utmp_write_direct(li, &ut)) {
791 log("utmp_perform_login: utmp_write_direct() failed"); 773 log("utmp_perform_login: utmp_write_direct() failed");
792 return 0; 774 return 0;
793 } 775 }
794#endif 776# endif
795 return 1; 777 return 1;
796} 778}
797 779
@@ -802,17 +784,17 @@ utmp_perform_logout(struct logininfo *li)
802 struct utmp ut; 784 struct utmp ut;
803 785
804 construct_utmp(li, &ut); 786 construct_utmp(li, &ut);
805#ifdef UTMP_USE_LIBRARY 787# ifdef UTMP_USE_LIBRARY
806 if (!utmp_write_library(li, &ut)) { 788 if (!utmp_write_library(li, &ut)) {
807 log("utmp_perform_logout: utmp_write_library() failed"); 789 log("utmp_perform_logout: utmp_write_library() failed");
808 return 0; 790 return 0;
809 } 791 }
810#else 792# else
811 if (!utmp_write_direct(li, &ut)) { 793 if (!utmp_write_direct(li, &ut)) {
812 log("utmp_perform_logout: utmp_write_direct() failed"); 794 log("utmp_perform_logout: utmp_write_direct() failed");
813 return 0; 795 return 0;
814 } 796 }
815#endif 797# endif
816 return 1; 798 return 1;
817} 799}
818 800
@@ -832,10 +814,7 @@ utmp_write_entry(struct logininfo *li)
832 return 0; 814 return 0;
833 } 815 }
834} 816}
835 817#endif /* USE_UTMP */
836
837#endif
838/* USE_UTMP */
839 818
840 819
841/** 820/**
@@ -846,29 +825,27 @@ utmp_write_entry(struct logininfo *li)
846#ifdef USE_UTMPX 825#ifdef USE_UTMPX
847 826
848/* if we have the wherewithall, use pututxline etc. */ 827/* if we have the wherewithall, use pututxline etc. */
849#if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) \ 828# if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \
850 && defined(HAVE_PUTUTXLINE) 829 defined(HAVE_PUTUTXLINE)
851# define UTMPX_USE_LIBRARY 830# define UTMPX_USE_LIBRARY
852#endif 831# endif
853 832
854 833
855/* write a utmpx entry with the system's help (pututxline() and pals) */ 834/* write a utmpx entry with the system's help (pututxline() and pals) */
856#ifdef UTMPX_USE_LIBRARY 835# ifdef UTMPX_USE_LIBRARY
857static int 836static int
858utmpx_write_library(struct logininfo *li, struct utmpx *utx) 837utmpx_write_library(struct logininfo *li, struct utmpx *utx)
859{ 838{
860 setutxent(); 839 setutxent();
861 pututxline(utx); 840 pututxline(utx);
862 841
863#ifdef HAVE_ENDUTXENT 842# ifdef HAVE_ENDUTXENT
864 endutxent(); 843 endutxent();
865#endif 844# endif
866 return 1; 845 return 1;
867} 846}
868 847
869#else 848# else /* UTMPX_USE_LIBRARY */
870/* UTMPX_USE_LIBRARY */
871
872 849
873/* write a utmp entry direct to the file */ 850/* write a utmp entry direct to the file */
874static int 851static int
@@ -877,9 +854,7 @@ utmpx_write_direct(struct logininfo *li, struct utmpx *utx)
877 log("utmpx_write_direct: not implemented!"); 854 log("utmpx_write_direct: not implemented!");
878 return 0; 855 return 0;
879} 856}
880 857# endif /* UTMPX_USE_LIBRARY */
881#endif
882/* UTMPX_USE_LIBRARY */
883 858
884static int 859static int
885utmpx_perform_login(struct logininfo *li) 860utmpx_perform_login(struct logininfo *li)
@@ -887,17 +862,17 @@ utmpx_perform_login(struct logininfo *li)
887 struct utmpx utx; 862 struct utmpx utx;
888 863
889 construct_utmpx(li, &utx); 864 construct_utmpx(li, &utx);
890#ifdef UTMPX_USE_LIBRARY 865# ifdef UTMPX_USE_LIBRARY
891 if (!utmpx_write_library(li, &utx)) { 866 if (!utmpx_write_library(li, &utx)) {
892 log("utmpx_perform_login: utmp_write_library() failed"); 867 log("utmpx_perform_login: utmp_write_library() failed");
893 return 0; 868 return 0;
894 } 869 }
895#else 870# else
896 if (!utmpx_write_direct(li, &ut)) { 871 if (!utmpx_write_direct(li, &ut)) {
897 log("utmpx_perform_login: utmp_write_direct() failed"); 872 log("utmpx_perform_login: utmp_write_direct() failed");
898 return 0; 873 return 0;
899 } 874 }
900#endif 875# endif
901 return 1; 876 return 1;
902} 877}
903 878
@@ -910,22 +885,21 @@ utmpx_perform_logout(struct logininfo *li)
910 memset(&utx, '\0', sizeof(utx)); 885 memset(&utx, '\0', sizeof(utx));
911 set_utmpx_time(li, &utx); 886 set_utmpx_time(li, &utx);
912 line_stripname(utx.ut_line, li->line, sizeof(utx.ut_line)); 887 line_stripname(utx.ut_line, li->line, sizeof(utx.ut_line));
913#ifdef HAVE_ID_IN_UTMPX 888# ifdef HAVE_ID_IN_UTMPX
914 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id)); 889 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
915#endif 890# endif
916#ifdef HAVE_TYPE_IN_UTMPX 891# ifdef HAVE_TYPE_IN_UTMPX
917 utx.ut_type = DEAD_PROCESS; 892 utx.ut_type = DEAD_PROCESS;
918#endif 893# endif
919 894
920#ifdef UTMPX_USE_LIBRARY 895# ifdef UTMPX_USE_LIBRARY
921 utmpx_write_library(li, &utx); 896 utmpx_write_library(li, &utx);
922#else 897# else
923 utmpx_write_direct(li, &utx); 898 utmpx_write_direct(li, &utx);
924#endif 899# endif
925 return 1; 900 return 1;
926} 901}
927 902
928
929int 903int
930utmpx_write_entry(struct logininfo *li) 904utmpx_write_entry(struct logininfo *li)
931{ 905{
@@ -939,10 +913,7 @@ utmpx_write_entry(struct logininfo *li)
939 return 0; 913 return 0;
940 } 914 }
941} 915}
942 916#endif /* USE_UTMPX */
943
944#endif
945/* USE_UTMPX */
946 917
947 918
948/** 919/**
@@ -976,9 +947,9 @@ wtmp_write(struct logininfo *li, struct utmp *ut)
976 return ret; 947 return ret;
977} 948}
978 949
979
980static int 950static int
981wtmp_perform_login(struct logininfo *li){ 951wtmp_perform_login(struct logininfo *li)
952{
982 struct utmp ut; 953 struct utmp ut;
983 954
984 construct_utmp(li, &ut); 955 construct_utmp(li, &ut);
@@ -1027,24 +998,22 @@ wtmp_write_entry(struct logininfo *li)
1027 * place and not have ut_type. 998 * place and not have ut_type.
1028 */ 999 */
1029 1000
1030
1031/* return true if this wtmp entry indicates a login */ 1001/* return true if this wtmp entry indicates a login */
1032static int 1002static int
1033wtmp_islogin(struct logininfo *li, struct utmp *ut) 1003wtmp_islogin(struct logininfo *li, struct utmp *ut)
1034{ 1004{
1035 if ( strncmp(li->username, ut->ut_user, 1005 if (strncmp(li->username, ut->ut_user,
1036 MIN_SIZEOF(li->username, ut->ut_user)) == 0 ) { 1006 MIN_SIZEOF(li->username, ut->ut_user)) == 0) {
1037#ifdef HAVE_TYPE_IN_UTMP 1007# ifdef HAVE_TYPE_IN_UTMP
1038 if (ut->ut_type & USER_PROCESS) 1008 if (ut->ut_type & USER_PROCESS)
1039 return 1; 1009 return 1;
1040#else 1010# else
1041 return 1; 1011 return 1;
1042#endif 1012# endif
1043 } 1013 }
1044 return 0; 1014 return 0;
1045} 1015}
1046 1016
1047
1048int 1017int
1049wtmp_get_entry(struct logininfo *li) 1018wtmp_get_entry(struct logininfo *li)
1050{ 1019{
@@ -1085,19 +1054,19 @@ wtmp_get_entry(struct logininfo *li)
1085 found = 1; 1054 found = 1;
1086 /* We've already checked for a time in struct 1055 /* We've already checked for a time in struct
1087 * utmp, in login_getlast(). */ 1056 * utmp, in login_getlast(). */
1088#ifdef HAVE_TIME_IN_UTMP 1057# ifdef HAVE_TIME_IN_UTMP
1089 li->tv_sec = ut.ut_time; 1058 li->tv_sec = ut.ut_time;
1090#else 1059# else
1091# if HAVE_TV_IN_UTMP 1060# if HAVE_TV_IN_UTMP
1092 li->tv_sec = ut.ut_tv.tv_sec; 1061 li->tv_sec = ut.ut_tv.tv_sec;
1093# endif 1062# endif
1094#endif 1063# endif
1095 line_fullname(li->line, ut.ut_line, 1064 line_fullname(li->line, ut.ut_line,
1096 MIN_SIZEOF(li->line, ut.ut_line)); 1065 MIN_SIZEOF(li->line, ut.ut_line));
1097#ifdef HAVE_HOST_IN_UTMP 1066# ifdef HAVE_HOST_IN_UTMP
1098 strlcpy(li->hostname, ut.ut_host, 1067 strlcpy(li->hostname, ut.ut_host,
1099 MIN_SIZEOF(li->hostname, ut.ut_host)); 1068 MIN_SIZEOF(li->hostname, ut.ut_host));
1100#endif 1069# endif
1101 continue; 1070 continue;
1102 } 1071 }
1103 /* Seek back 2 x struct utmp */ 1072 /* Seek back 2 x struct utmp */
@@ -1112,10 +1081,7 @@ wtmp_get_entry(struct logininfo *li)
1112 close(fd); 1081 close(fd);
1113 return 1; 1082 return 1;
1114} 1083}
1115 1084# endif /* USE_WTMP */
1116
1117#endif
1118/* USE_WTMP */
1119 1085
1120 1086
1121/** 1087/**
@@ -1123,7 +1089,6 @@ wtmp_get_entry(struct logininfo *li)
1123 **/ 1089 **/
1124 1090
1125#ifdef USE_WTMPX 1091#ifdef USE_WTMPX
1126
1127/* write a wtmpx entry direct to the end of the file */ 1092/* write a wtmpx entry direct to the end of the file */
1128/* This is a slight modification of code in OpenBSD's logwtmp.c */ 1093/* This is a slight modification of code in OpenBSD's logwtmp.c */
1129static int 1094static int
@@ -1194,13 +1159,13 @@ static int
1194wtmpx_islogin(struct logininfo *li, struct utmpx *utx) 1159wtmpx_islogin(struct logininfo *li, struct utmpx *utx)
1195{ 1160{
1196 if ( strncmp(li->username, utx->ut_user, 1161 if ( strncmp(li->username, utx->ut_user,
1197 MIN_SIZEOF(li->username, utx->ut_user)) == 0 ) { 1162 MIN_SIZEOF(li->username, utx->ut_user)) == 0 ) {
1198#ifdef HAVE_TYPE_IN_UTMPX 1163# ifdef HAVE_TYPE_IN_UTMPX
1199 if (utx->ut_type == USER_PROCESS) 1164 if (utx->ut_type == USER_PROCESS)
1200 return 1; 1165 return 1;
1201#else 1166# else
1202 return 1; 1167 return 1;
1203#endif 1168# endif
1204 } 1169 }
1205 return 0; 1170 return 0;
1206} 1171}
@@ -1245,18 +1210,18 @@ wtmpx_get_entry(struct logininfo *li)
1245 /* Logouts are recorded as a blank username on a particular line. 1210 /* Logouts are recorded as a blank username on a particular line.
1246 * So, we just need to find the username in struct utmpx */ 1211 * So, we just need to find the username in struct utmpx */
1247 if ( wtmpx_islogin(li, &utx) ) { 1212 if ( wtmpx_islogin(li, &utx) ) {
1248#ifdef HAVE_TV_IN_UTMPX 1213# ifdef HAVE_TV_IN_UTMPX
1249 li->tv_sec = utx.ut_tv.tv_sec; 1214 li->tv_sec = utx.ut_tv.tv_sec;
1250#else 1215# else
1251# ifdef HAVE_TIME_IN_UTMPX 1216# ifdef HAVE_TIME_IN_UTMPX
1252 li->tv_sec = utx.ut_time; 1217 li->tv_sec = utx.ut_time;
1253# endif 1218# endif
1254#endif 1219# endif
1255 line_fullname(li->line, utx.ut_line, sizeof(li->line)); 1220 line_fullname(li->line, utx.ut_line, sizeof(li->line));
1256#ifdef HAVE_HOST_IN_UTMPX 1221# ifdef HAVE_HOST_IN_UTMPX
1257 strlcpy(li->hostname, utx.ut_host, 1222 strlcpy(li->hostname, utx.ut_host,
1258 MIN_SIZEOF(li->hostname, utx.ut_host)); 1223 MIN_SIZEOF(li->hostname, utx.ut_host));
1259#endif 1224# endif
1260 continue; 1225 continue;
1261 } 1226 }
1262 if (lseek(fd, (off_t)(0-2*sizeof(struct utmpx)), SEEK_CUR) == -1) { 1227 if (lseek(fd, (off_t)(0-2*sizeof(struct utmpx)), SEEK_CUR) == -1) {
@@ -1268,17 +1233,13 @@ wtmpx_get_entry(struct logininfo *li)
1268 close(fd); 1233 close(fd);
1269 return 1; 1234 return 1;
1270} 1235}
1271
1272
1273#endif /* USE_WTMPX */ 1236#endif /* USE_WTMPX */
1274 1237
1275
1276/** 1238/**
1277 ** Low-level libutil login() functions 1239 ** Low-level libutil login() functions
1278 **/ 1240 **/
1279 1241
1280#ifdef USE_LOGIN 1242#ifdef USE_LOGIN
1281
1282static int 1243static int
1283syslogin_perform_login(struct logininfo *li) 1244syslogin_perform_login(struct logininfo *li)
1284{ 1245{
@@ -1294,31 +1255,29 @@ syslogin_perform_login(struct logininfo *li)
1294 return 1; 1255 return 1;
1295} 1256}
1296 1257
1297
1298static int 1258static int
1299syslogin_perform_logout(struct logininfo *li) 1259syslogin_perform_logout(struct logininfo *li)
1300{ 1260{
1301#ifdef HAVE_LOGOUT 1261# ifdef HAVE_LOGOUT
1302 char line[8]; 1262 char line[8];
1303 1263
1304 (void)line_stripname(line, li->line, sizeof(line)); 1264 (void)line_stripname(line, li->line, sizeof(line));
1305 1265
1306 if (!logout(line)) { 1266 if (!logout(line)) {
1307 log("syslogin_perform_logout: logout() returned an error"); 1267 log("syslogin_perform_logout: logout() returned an error");
1308# ifdef HAVE_LOGWTMP 1268# ifdef HAVE_LOGWTMP
1309 } else { 1269 } else {
1310 logwtmp(line, "", ""); 1270 logwtmp(line, "", "");
1311 } 1271 }
1312# endif 1272# endif
1313 /* FIXME: (ATL - if the need arises) What to do if we have 1273 /* FIXME: (ATL - if the need arises) What to do if we have
1314 * login, but no logout? what if logout but no logwtmp? All 1274 * login, but no logout? what if logout but no logwtmp? All
1315 * routines are in libutil so they should all be there, 1275 * routines are in libutil so they should all be there,
1316 * but... */ 1276 * but... */
1317#endif 1277# endif
1318 return 1; 1278 return 1;
1319} 1279}
1320 1280
1321
1322int 1281int
1323syslogin_write_entry(struct logininfo *li) 1282syslogin_write_entry(struct logininfo *li)
1324{ 1283{
@@ -1332,18 +1291,18 @@ syslogin_write_entry(struct logininfo *li)
1332 return 0; 1291 return 0;
1333 } 1292 }
1334} 1293}
1335
1336
1337#endif /* USE_LOGIN */ 1294#endif /* USE_LOGIN */
1338 1295
1339/* end of file log-syslogin.c */ 1296/* end of file log-syslogin.c */
1340 1297
1341
1342/** 1298/**
1343 ** Low-level lastlog functions 1299 ** Low-level lastlog functions
1344 **/ 1300 **/
1345 1301
1346#ifdef USE_LASTLOG 1302#ifdef USE_LASTLOG
1303#define LL_FILE 1
1304#define LL_DIR 2
1305#define LL_OTHER 3
1347 1306
1348static void 1307static void
1349lastlog_construct(struct logininfo *li, struct lastlog *last) 1308lastlog_construct(struct logininfo *li, struct lastlog *last)
@@ -1351,26 +1310,20 @@ lastlog_construct(struct logininfo *li, struct lastlog *last)
1351 /* clear the structure */ 1310 /* clear the structure */
1352 memset(last, '\0', sizeof(struct lastlog)); 1311 memset(last, '\0', sizeof(struct lastlog));
1353 1312
1354 (void)line_stripname(last->ll_line, li->line, 1313 (void)line_stripname(last->ll_line, li->line, sizeof(last->ll_line));
1355 sizeof(last->ll_line));
1356 strlcpy(last->ll_host, li->hostname, 1314 strlcpy(last->ll_host, li->hostname,
1357 MIN_SIZEOF(last->ll_host, li->hostname)); 1315 MIN_SIZEOF(last->ll_host, li->hostname));
1358 last->ll_time = li->tv_sec; 1316 last->ll_time = li->tv_sec;
1359} 1317}
1360 1318
1361
1362#define LL_FILE 1
1363#define LL_DIR 2
1364#define LL_OTHER 3
1365
1366static int 1319static int
1367lastlog_filetype(char *filename) 1320lastlog_filetype(char *filename)
1368{ 1321{
1369 struct stat st; 1322 struct stat st;
1370 1323
1371 if ( stat(LASTLOG_FILE, &st) != 0) { 1324 if (stat(LASTLOG_FILE, &st) != 0) {
1372 log("lastlog_perform_login: Couldn't stat %s: %s", 1325 log("lastlog_perform_login: Couldn't stat %s: %s", LASTLOG_FILE,
1373 LASTLOG_FILE, strerror(errno)); 1326 strerror(errno));
1374 return 0; 1327 return 0;
1375 } 1328 }
1376 if (S_ISDIR(st.st_mode)) 1329 if (S_ISDIR(st.st_mode))
@@ -1403,7 +1356,7 @@ lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1403 log("lastlog_openseek: %.100s is not a file or directory!", 1356 log("lastlog_openseek: %.100s is not a file or directory!",
1404 LASTLOG_FILE); 1357 LASTLOG_FILE);
1405 return 0; 1358 return 0;
1406 } /* switch */ 1359 }
1407 1360
1408 *fd = open(lastlog_file, filemode); 1361 *fd = open(lastlog_file, filemode);
1409 if ( *fd < 0) { 1362 if ( *fd < 0) {
@@ -1411,6 +1364,7 @@ lastlog_openseek(struct logininfo *li, int *fd, int filemode)
1411 lastlog_file, strerror(errno)); 1364 lastlog_file, strerror(errno));
1412 return 0; 1365 return 0;
1413 } 1366 }
1367
1414 /* find this uid's offset in the lastlog file */ 1368 /* find this uid's offset in the lastlog file */
1415 offset = (off_t) ( (long)li->uid * sizeof(struct lastlog)); 1369 offset = (off_t) ( (long)li->uid * sizeof(struct lastlog));
1416 1370
@@ -1433,18 +1387,18 @@ lastlog_perform_login(struct logininfo *li)
1433 1387
1434 /* write the entry */ 1388 /* write the entry */
1435 if (lastlog_openseek(li, &fd, O_RDWR)) { 1389 if (lastlog_openseek(li, &fd, O_RDWR)) {
1436 if ( write(fd, &last, sizeof(struct lastlog)) 1390 if (write(fd, &last, sizeof(struct lastlog)) !=
1437 != sizeof(struct lastlog) ) { 1391 sizeof(struct lastlog)) {
1438 log("lastlog_write_filemode: Error writing to %s: %s", 1392 log("lastlog_write_filemode: Error writing to %s: %s",
1439 LASTLOG_FILE, strerror(errno)); 1393 LASTLOG_FILE, strerror(errno));
1440 return 0; 1394 return 0;
1441 } 1395 }
1442 return 1; 1396 return 1;
1443 } else 1397 } else {
1444 return 0; 1398 return 0;
1399 }
1445} 1400}
1446 1401
1447
1448int 1402int
1449lastlog_write_entry(struct logininfo *li) 1403lastlog_write_entry(struct logininfo *li)
1450{ 1404{
@@ -1457,17 +1411,15 @@ lastlog_write_entry(struct logininfo *li)
1457 } 1411 }
1458} 1412}
1459 1413
1460
1461static void 1414static void
1462lastlog_populate_entry(struct logininfo *li, struct lastlog *last) 1415lastlog_populate_entry(struct logininfo *li, struct lastlog *last)
1463{ 1416{
1464 line_fullname(li->line, last->ll_line, sizeof(li->line)); 1417 line_fullname(li->line, last->ll_line, sizeof(li->line));
1465 strlcpy(li->hostname, last->ll_host, 1418 strlcpy(li->hostname, last->ll_host,
1466 MIN_SIZEOF(li->hostname, last->ll_host)); 1419 MIN_SIZEOF(li->hostname, last->ll_host));
1467 li->tv_sec = last->ll_time; 1420 li->tv_sec = last->ll_time;
1468} 1421}
1469 1422
1470
1471int 1423int
1472lastlog_get_entry(struct logininfo *li) 1424lastlog_get_entry(struct logininfo *li)
1473{ 1425{
@@ -1475,8 +1427,8 @@ lastlog_get_entry(struct logininfo *li)
1475 int fd; 1427 int fd;
1476 1428
1477 if (lastlog_openseek(li, &fd, O_RDONLY)) { 1429 if (lastlog_openseek(li, &fd, O_RDONLY)) {
1478 if ( read(fd, &last, sizeof(struct lastlog)) 1430 if ( read(fd, &last, sizeof(struct lastlog)) !=
1479 != sizeof(struct lastlog) ) { 1431 sizeof(struct lastlog) ) {
1480 log("lastlog_write_filemode: Error reading from %s: %s", 1432 log("lastlog_write_filemode: Error reading from %s: %s",
1481 LASTLOG_FILE, strerror(errno)); 1433 LASTLOG_FILE, strerror(errno));
1482 return 0; 1434 return 0;
@@ -1484,9 +1436,8 @@ lastlog_get_entry(struct logininfo *li)
1484 lastlog_populate_entry(li, &last); 1436 lastlog_populate_entry(li, &last);
1485 return 1; 1437 return 1;
1486 } 1438 }
1487 } else 1439 } else {
1488 return 0; 1440 return 0;
1441 }
1489} 1442}
1490
1491
1492#endif /* USE_LASTLOG */ 1443#endif /* USE_LASTLOG */