summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.in8
-rw-r--r--acconfig.h44
-rw-r--r--bsd-login.c195
-rw-r--r--bsd-login.h22
-rw-r--r--configure.in268
-rw-r--r--defines.h125
-rw-r--r--login.c277
-rw-r--r--loginrec.c1385
-rw-r--r--loginrec.h167
-rw-r--r--logintest.c307
-rw-r--r--openbsd-compat.h1
11 files changed, 2212 insertions, 587 deletions
diff --git a/Makefile.in b/Makefile.in
index 584f3054d..0a2a2cfc7 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -40,7 +40,7 @@ LIBOPENBSD_COMPAT_OBJS=bsd-base64.o bsd-bindresvport.o bsd-daemon.o bsd-misc.o b
40 40
41SSHOBJS= ssh.o sshconnect.o sshconnect1.o sshconnect2.o log-client.o readconf.o clientloop.o 41SSHOBJS= ssh.o sshconnect.o sshconnect1.o sshconnect2.o log-client.o readconf.o clientloop.o
42 42
43SSHDOBJS= sshd.o auth.o auth1.o auth2.o auth-rhosts.o auth-krb4.o auth-pam.o auth-passwd.o auth-rsa.o auth-rh-rsa.o pty.o log-server.o login.o servconf.o serverloop.o bsd-login.o md5crypt.o session.o 43SSHDOBJS= sshd.o auth.o auth1.o auth2.o auth-rhosts.o auth-krb4.o auth-pam.o auth-passwd.o auth-rsa.o auth-rh-rsa.o pty.o log-server.o login.o loginrec.o servconf.o serverloop.o md5crypt.o session.o
44 44
45TROFFMAN = scp.1 ssh-add.1 ssh-agent.1 ssh-keygen.1 ssh.1 sshd.8 45TROFFMAN = scp.1 ssh-add.1 ssh-agent.1 ssh-keygen.1 ssh.1 sshd.8
46CATMAN = scp.0 ssh-add.0 ssh-agent.0 ssh-keygen.0 ssh.0 sshd.0 46CATMAN = scp.0 ssh-add.0 ssh-agent.0 ssh-keygen.0 ssh.0 sshd.0
@@ -86,11 +86,15 @@ ssh-agent: libopenbsd-compat.a libssh.a ssh-agent.o log-client.o
86ssh-keygen: libopenbsd-compat.a libssh.a ssh-keygen.o log-client.o 86ssh-keygen: libopenbsd-compat.a libssh.a ssh-keygen.o log-client.o
87 $(LD) -o $@ ssh-keygen.o log-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS) 87 $(LD) -o $@ ssh-keygen.o log-client.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
88 88
89# test driver for the loginrec code - not built by default
90logintest: logintest.o libopenbsd-compat.a libssh.a log-client.o loginrec.o
91 $(LD) -o $@ logintest.o $(LDFLAGS) loginrec.o -lopenbsd-compat -lssh log-client.o $(LIBS)
92
89$(MANPAGES) $(CONFIGFILES):: 93$(MANPAGES) $(CONFIGFILES)::
90 $(FIXPATHSCMD) $(srcdir)/$@ 94 $(FIXPATHSCMD) $(srcdir)/$@
91 95
92clean: 96clean:
93 rm -f *.o *.a $(TARGETS) config.cache config.log 97 rm -f *.o *.a $(TARGETS) logintest config.cache config.log
94 rm -f *.out core 98 rm -f *.out core
95 99
96distclean: clean 100distclean: clean
diff --git a/acconfig.h b/acconfig.h
index 308919f90..8720dd664 100644
--- a/acconfig.h
+++ b/acconfig.h
@@ -52,10 +52,50 @@
52#undef HAVE_TYPE_IN_UTMP 52#undef HAVE_TYPE_IN_UTMP
53#undef HAVE_TYPE_IN_UTMPX 53#undef HAVE_TYPE_IN_UTMPX
54#undef HAVE_TV_IN_UTMP 54#undef HAVE_TV_IN_UTMP
55#undef HAVE_TV_IN_UTMPX
55#undef HAVE_ID_IN_UTMP 56#undef HAVE_ID_IN_UTMP
57#undef HAVE_EXIT_IN_UTMP
58#undef HAVE_TIME_IN_UTMP
59#undef HAVE_TIME_IN_UTMPX
56 60
57/* Define if you want to use utmpx */ 61/* Define if you don't want to use your system's login() call */
58#undef USE_UTMPX 62#undef DISABLE_LOGIN
63
64/* Define if you don't want to use pututline() etc. to write [uw]tmp */
65#undef DISABLE_PUTUTLINE
66
67/* Define if you don't want to use pututxline() etc. to write [uw]tmpx */
68#undef DISABLE_PUTUTXLINE
69
70/* Define if you don't want to use lastlog */
71#undef DISABLE_LASTLOG
72
73/* Define if you don't want to use utmp */
74#undef DISABLE_UTMP
75
76/* Define if you don't want to use utmpx */
77#undef DISABLE_UTMPX
78
79/* Define if you don't want to use wtmp */
80#undef DISABLE_WTMP
81
82/* Define if you don't want to use wtmpx */
83#undef DISABLE_WTMPX
84
85/* Define if you want to specify the path to your lastlog file */
86#undef CONF_LASTLOG_FILE
87
88/* Define if you want to specify the path to your utmp file */
89#undef CONF_UTMP_FILE
90
91/* Define if you want to specify the path to your wtmp file */
92#undef CONF_WTMP_FILE
93
94/* Define if you want to specify the path to your utmpx file */
95#undef CONF_UTMPX_FILE
96
97/* Define if you want to specify the path to your wtmpx file */
98#undef CONF_WTMPX_FILE
59 99
60/* Define is libutil has login() function */ 100/* Define is libutil has login() function */
61#undef HAVE_LIBUTIL_LOGIN 101#undef HAVE_LIBUTIL_LOGIN
diff --git a/bsd-login.c b/bsd-login.c
deleted file mode 100644
index a6f4acca3..000000000
--- a/bsd-login.c
+++ /dev/null
@@ -1,195 +0,0 @@
1/*
2 * This file has been heavily modified from the original OpenBSD version
3 */
4
5/* $OpenBSD: login.c,v 1.5 1998/07/13 02:11:12 millert Exp $ */
6/*
7 * Copyright (c) 1988, 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#include "config.h"
40#ifndef HAVE_LOGIN
41
42#include <errno.h>
43
44#if defined(LIBC_SCCS) && !defined(lint)
45/* from: static char sccsid[] = "@(#)login.c 8.1 (Berkeley) 6/4/93"; */
46static char *rcsid = "$OpenBSD: login.c,v 1.5 1998/07/13 02:11:12 millert Exp $";
47#endif /* LIBC_SCCS and not lint */
48
49#include <sys/types.h>
50
51#include <fcntl.h>
52#include <unistd.h>
53#include <stdlib.h>
54#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
55# include <utmpx.h>
56#endif
57#ifdef HAVE_UTMP_H
58# include <utmp.h>
59#endif
60#include <stdio.h>
61#include <string.h>
62
63#ifdef USER_PROCESS
64/*
65 * find first matching slot in utmp, or "-1" for none
66 *
67 * algorithm: for USER_PROCESS, check tty name
68 * for DEAD_PROCESS, check PID and tty name
69 *
70 */
71int find_tty_slot( utp )
72struct utmp * utp;
73{
74 int t = 0;
75 struct utmp * u;
76
77# if defined(HAVE_TYPE_IN_UTMP) || defined(HAVE_TYPE_IN_UTMPX)
78 setutent();
79
80 while((u = getutent()) != NULL) {
81 if (utp->ut_type == USER_PROCESS &&
82 (strncmp(utp->ut_line, u->ut_line, sizeof(utp->ut_line)) == 0)) {
83 endutent();
84 return(t);
85 }
86
87 if ((utp->ut_type == DEAD_PROCESS) && (utp->ut_pid == u->ut_pid) &&
88 (strncmp(utp->ut_line, u->ut_line, sizeof(utp->ut_line)) == 0 )) {
89 endutent();
90 return(t);
91 }
92 t++;
93 }
94
95 endutent();
96# endif /* defined(HAVE_TYPE_IN_UTMP) || defined(HAVE_TYPE_IN_UTMPX) */
97 return(-1);
98}
99#else /* USER_PROCESS */
100int find_tty_slot(struct utmp *utp)
101{
102 return(ttyslot());
103}
104#endif /* USER_PROCESS */
105
106#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
107void login(struct utmpx *utx)
108#else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
109void login(struct utmp *utp)
110#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
111{
112 /* Use proper API if we have it */
113#if defined(USE_UTMPX)
114# if defined(HAVE_PUTUTXLINE)
115 setutxent();
116 pututxline(utx);
117 endutxent();
118# endif /* defined(HAVE_PUTUTXLINE) */
119# if defined(HAVE_UPDWTMPX)
120 updwtmpx(_PATH_WTMPX, utx);
121# endif /* defined(HAVE_UPDWTMPX) */
122#else /* defined(USE_UTMPX) */
123# if defined(HAVE_PUTUTLINE)
124 setutent();
125 pututline(utp);
126 endutent();
127# endif /* defined(HAVE_PUTUTLINE) */
128# if defined(HAVE_UPDWTMPX)
129 updwtmp(_PATH_WTMP, utp);
130# endif /* defined(HAVE_UPDWTMP) */
131#endif /* defined(USE_UTMPX) */
132
133 /* Otherwise DIY */
134#if (defined(USE_UTMPX) && !defined(HAVE_PUTUTXLINE)) || \
135 (!defined(USE_UTMPX) && !defined(HAVE_PUTUTLINE))
136 int fd;
137 int tty;
138
139 /* can't use ttyslot here, as that will not work for logout
140 * (record_logout() is called from the master sshd, which does
141 * not have the correct tty on stdin/out, so ttyslot will return
142 * "-1" or (worse) a wrong number
143 */
144 tty = find_tty_slot(utp);
145
146#ifdef USE_UTMPX
147 /* If no tty was found, append it to utmpx */
148 if (tty == -1) {
149 if ((fd = open(_PATH_UTMPX, O_WRONLY|O_APPEND, 0)) >= 0) {
150 (void)write(fd, utp, sizeof(struct utmp));
151 (void)close(fd);
152 return;
153 }
154 }
155 /* Otherwise, tty was found - update at its location */
156 fd = open(_PATH_UTMPX, O_RDWR|O_CREAT, 0644);
157 if (fd == -1) {
158 log("Couldn't open %s: %s", _PATH_UTMPX, strerror(errno));
159 return;
160 }
161 lseek(fd, (off_t)(tty * sizeof(struct utmpx)), SEEK_SET);
162 write(fd, utx, sizeof(struct utmpx));
163 close(fd);
164 if ((fd = open(_PATH_WTMPX, O_WRONLY|O_APPEND, 0)) >= 0) {
165 (void)write(fd, utx, sizeof(struct utmpx));
166 (void)close(fd);
167 }
168#else /* USE_UTMPX */
169 /* If no tty was found, append it to utmp */
170 if (tty == -1) {
171 if ((fd = open(_PATH_UTMP, O_WRONLY|O_APPEND, 0)) >= 0) {
172 (void)write(fd, utp, sizeof(struct utmp));
173 (void)close(fd);
174 return;
175 }
176 }
177 /* Otherwise, tty was found - update at its location */
178 fd = open(_PATH_UTMP, O_RDWR|O_CREAT, 0644);
179 if (fd == -1) {
180 log("Couldn't open %s: %s", _PATH_UTMP, strerror(errno));
181 return;
182 }
183 lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
184 write(fd, utp, sizeof(struct utmp));
185 close(fd);
186 if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND, 0)) >= 0) {
187 (void)write(fd, utp, sizeof(struct utmp));
188 (void)close(fd);
189 }
190#endif /* USE_UTMPX */
191#endif /* (defined(USE_UTMPX) && !defined(HAVE_PUTUTXLINE)) || \
192 (!defined(USE_UTMPX) && !defined(HAVE_PUTUTLINE)) */
193}
194
195#endif /* HAVE_LOGIN */
diff --git a/bsd-login.h b/bsd-login.h
deleted file mode 100644
index f26f47084..000000000
--- a/bsd-login.h
+++ /dev/null
@@ -1,22 +0,0 @@
1#ifndef _BSD_LOGIN_H
2# define _BSD_LOGIN_H
3
4# include "config.h"
5# ifndef HAVE_LOGIN
6
7# include <utmp.h>
8
9# if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
10# include <utmpx.h>
11
12void login(struct utmp *utp, struct utmpx *utx);
13
14# else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
15
16void login(struct utmp *utp);
17
18# endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
19
20# endif /* !HAVE_LOGIN */
21
22#endif /* _BSD_LOGIN_H */
diff --git a/configure.in b/configure.in
index 86284aa22..8bb647c12 100644
--- a/configure.in
+++ b/configure.in
@@ -43,7 +43,6 @@ case "$host" in
43 fi 43 fi
44 CFLAGS="$CFLAGS -D_HPUX_SOURCE" 44 CFLAGS="$CFLAGS -D_HPUX_SOURCE"
45 AC_DEFINE(IPADDR_IN_DISPLAY) 45 AC_DEFINE(IPADDR_IN_DISPLAY)
46 AC_DEFINE(USE_UTMPX)
47 AC_MSG_CHECKING(for HPUX trusted system password database) 46 AC_MSG_CHECKING(for HPUX trusted system password database)
48 if test -f /tcb/files/auth/system/default; then 47 if test -f /tcb/files/auth/system/default; then
49 AC_MSG_RESULT(yes) 48 AC_MSG_RESULT(yes)
@@ -63,7 +62,6 @@ case "$host" in
63 fi 62 fi
64 CFLAGS="$CFLAGS -D_HPUX_SOURCE" 63 CFLAGS="$CFLAGS -D_HPUX_SOURCE"
65 AC_DEFINE(IPADDR_IN_DISPLAY) 64 AC_DEFINE(IPADDR_IN_DISPLAY)
66 AC_DEFINE(USE_UTMPX)
67 AC_MSG_CHECKING(for HPUX trusted system password database) 65 AC_MSG_CHECKING(for HPUX trusted system password database)
68 if test -f /tcb/files/auth/system/default; then 66 if test -f /tcb/files/auth/system/default; then
69 AC_MSG_RESULT(yes) 67 AC_MSG_RESULT(yes)
@@ -104,7 +102,8 @@ case "$host" in
104 CFLAGS="$CFLAGS -I/usr/local/include" 102 CFLAGS="$CFLAGS -I/usr/local/include"
105 LDFLAGS="$LDFLAGS -L/usr/local/lib -R/usr/local/lib -L/usr/ucblib -R/usr/ucblib" 103 LDFLAGS="$LDFLAGS -L/usr/local/lib -R/usr/local/lib -L/usr/ucblib -R/usr/ucblib"
106 need_dash_r=1 104 need_dash_r=1
107 AC_DEFINE(USE_UTMPX) 105 # hardwire lastlog location (can't detect it on some versions)
106 conf_lastlog_location="/var/adm/lastlog"
108 ;; 107 ;;
109*-*-sunos4*) 108*-*-sunos4*)
110 CFLAGS="$CFLAGS -DSUNOS4" 109 CFLAGS="$CFLAGS -DSUNOS4"
@@ -113,7 +112,6 @@ case "$host" in
113*-*-sysv*) 112*-*-sysv*)
114 CFLAGS="$CFLAGS -I/usr/local/include" 113 CFLAGS="$CFLAGS -I/usr/local/include"
115 LDFLAGS="$LDFLAGS -L/usr/local/lib" 114 LDFLAGS="$LDFLAGS -L/usr/local/lib"
116 AC_DEFINE(USE_UTMPX)
117 MANTYPE='$(CATMAN)' 115 MANTYPE='$(CATMAN)'
118 mansubdir=cat 116 mansubdir=cat
119 LIBS="$LIBS -lgen -lsocket" 117 LIBS="$LIBS -lgen -lsocket"
@@ -132,10 +130,20 @@ if test -z "$no_libnsl" ; then
132fi 130fi
133 131
134# Checks for header files. 132# Checks for header files.
135AC_CHECK_HEADERS(bstring.h endian.h lastlog.h login.h maillock.h netdb.h netgroup.h netinet/in_systm.h paths.h poll.h pty.h shadow.h security/pam_appl.h sys/bitypes.h sys/bsdtty.h sys/cdefs.h sys/poll.h sys/select.h sys/stropts.h sys/sysmacros.h sys/time.h sys/ttcompat.h stddef.h util.h utmp.h utmpx.h) 133AC_CHECK_HEADERS(bstring.h endian.h lastlog.h login.h maillock.h netdb.h netgroup.h netinet/in_systm.h paths.h poll.h pty.h shadow.h security/pam_appl.h sys/bitypes.h sys/bsdtty.h sys/cdefs.h sys/poll.h sys/select.h sys/stropts.h sys/sysmacros.h sys/time.h sys/ttcompat.h stddef.h time.h util.h utmp.h utmpx.h)
136 134
137# Checks for library functions. 135# Checks for library functions.
138AC_CHECK_FUNCS(arc4random atexit b64_ntop bcopy bindresvport_af clock freeaddrinfo gai_strerror getaddrinfo getnameinfo getrusage innetgr md5_crypt memmove mkdtemp on_exit openpty pututline pututxline rresvport_af setenv seteuid setlogin setproctitle setreuid snprintf strlcat strlcpy updwtmp updwtmpx vsnprintf vhangup _getpty __b64_ntop) 136AC_CHECK_FUNCS(arc4random atexit b64_ntop bcopy bindresvport_af clock freeaddrinfo gai_strerror getaddrinfo getnameinfo getrusage innetgr md5_crypt memmove mkdtemp on_exit openpty rresvport_af setenv seteuid setlogin setproctitle setreuid snprintf strlcat strlcpy vsnprintf vhangup _getpty __b64_ntop)
137dnl checks for time functions
138AC_CHECK_FUNCS(gettimeofday time)
139dnl checks for libutil functions
140AC_CHECK_FUNCS(login logout updwtmp logwtmp)
141dnl checks for utmp functions
142AC_CHECK_FUNCS(entutent getutent getutid getutline pututline setutent)
143AC_CHECK_FUNCS(utmpname)
144dnl checks for utmpx functions
145AC_CHECK_FUNCS(entutxent getutxent getutxid getutxline pututxline )
146AC_CHECK_FUNCS(setutxent utmpxname)
139 147
140AC_CHECK_FUNC(login, 148AC_CHECK_FUNC(login,
141 [AC_DEFINE(HAVE_LOGIN)], 149 [AC_DEFINE(HAVE_LOGIN)],
@@ -501,6 +509,11 @@ OSSH_CHECK_HEADER_FOR_FIELD(ut_addr, utmp.h, HAVE_ADDR_IN_UTMP)
501OSSH_CHECK_HEADER_FOR_FIELD(ut_addr, utmpx.h, HAVE_ADDR_IN_UTMPX) 509OSSH_CHECK_HEADER_FOR_FIELD(ut_addr, utmpx.h, HAVE_ADDR_IN_UTMPX)
502OSSH_CHECK_HEADER_FOR_FIELD(ut_addr_v6, utmp.h, HAVE_ADDR_V6_IN_UTMP) 510OSSH_CHECK_HEADER_FOR_FIELD(ut_addr_v6, utmp.h, HAVE_ADDR_V6_IN_UTMP)
503OSSH_CHECK_HEADER_FOR_FIELD(ut_addr_v6, utmpx.h, HAVE_ADDR_V6_IN_UTMPX) 511OSSH_CHECK_HEADER_FOR_FIELD(ut_addr_v6, utmpx.h, HAVE_ADDR_V6_IN_UTMPX)
512OSSH_CHECK_HEADER_FOR_FIELD(ut_exit, utmp.h, HAVE_EXIT_IN_UTMP)
513OSSH_CHECK_HEADER_FOR_FIELD(ut_time, utmp.h, HAVE_TIME_IN_UTMP)
514OSSH_CHECK_HEADER_FOR_FIELD(ut_time, utmpx.h, HAVE_TIME_IN_UTMPX)
515OSSH_CHECK_HEADER_FOR_FIELD(ut_tv, utmpx.h, HAVE_TV_IN_UTMPX)
516
504 517
505 518
506AC_CACHE_CHECK([for ss_family field in struct sockaddr_storage], 519AC_CACHE_CHECK([for ss_family field in struct sockaddr_storage],
@@ -590,48 +603,6 @@ if test ! -z "$MAIL" ; then
590 AC_DEFINE_UNQUOTED(MAIL_DIRECTORY, "$maildir") 603 AC_DEFINE_UNQUOTED(MAIL_DIRECTORY, "$maildir")
591fi 604fi
592 605
593# Look for lastlog location
594AC_ARG_WITH(lastlog,
595 [ --with-lastlog=FILE Location of lastlog file],
596 [
597 if test "x$withval" = "xno" ; then
598 AC_DEFINE(DISABLE_LASTLOG)
599 else
600 AC_DEFINE_UNQUOTED(LASTLOG_LOCATION, "$withval")
601 fi
602 ],
603 [
604 AC_MSG_CHECKING([location of lastlog file])
605 for lastlog in /var/log/lastlog /var/adm/lastlog /usr/adm/lastlog /etc/security/lastlog ; do
606 if test -f $lastlog ; then
607 gotlastlog="file"
608 break
609 fi
610 if test -d $lastlog ; then
611 gotlastlog="dir"
612 break
613 fi
614 done
615 if test -z "$gotlastlog" ; then
616 AC_MSG_RESULT(not found)
617 nolastlog=1
618 else
619 if test "x$gotlastlog" = "xdir" ; then
620 AC_MSG_RESULT(${lastlog}/)
621 AC_DEFINE(LASTLOG_IS_DIR)
622 else
623 AC_MSG_RESULT($lastlog)
624 AC_DEFINE_UNQUOTED(LASTLOG_LOCATION, "$lastlog")
625 fi
626 fi
627 ]
628)
629
630if test ! -z "$nolastlog" ; then
631 AC_MSG_WARN([*** Disabling lastlog support *** ])
632 AC_DEFINE(DISABLE_LASTLOG)
633fi
634
635if test -z "$no_dev_ptmx" ; then 606if test -z "$no_dev_ptmx" ; then
636 AC_CHECK_FILE("/dev/ptmx", 607 AC_CHECK_FILE("/dev/ptmx",
637 [ 608 [
@@ -838,16 +809,6 @@ AC_ARG_WITH(md5-passwords,
838 ] 809 ]
839) 810)
840 811
841# Check whether to enable utmpx support
842AC_ARG_WITH(utmpx,
843 [ --with-utmpx Enable utmpx support],
844 [
845 if test "x$withval" != "xno" ; then
846 AC_DEFINE(USE_UTMPX)
847 fi
848 ]
849)
850
851# Whether to disable shadow password support 812# Whether to disable shadow password support
852AC_ARG_WITH(shadow, 813AC_ARG_WITH(shadow,
853 [ --without-shadow Disable shadow password support], 814 [ --without-shadow Disable shadow password support],
@@ -922,6 +883,197 @@ AC_ARG_WITH(pid-dir,
922AC_DEFINE_UNQUOTED(PIDDIR, "$piddir") 883AC_DEFINE_UNQUOTED(PIDDIR, "$piddir")
923AC_SUBST(piddir) 884AC_SUBST(piddir)
924 885
886dnl allow user to disable some login recording features
887AC_ARG_ENABLE(lastlog,
888 [ --disable-lastlog disable use of lastlog even if detected [no]],
889 [ AC_DEFINE(DISABLE_LASTLOG) ]
890)
891AC_ARG_ENABLE(utmp,
892 [ --disable-utmp disable use of utmp even if detected [no]],
893 [ AC_DEFINE(DISABLE_UTMP) ]
894)
895AC_ARG_ENABLE(utmpx,
896 [ --disable-utmpx disable use of utmpx even if detected [no]],
897 [ AC_DEFINE(DISABLE_UTMPX) ]
898)
899AC_ARG_ENABLE(wtmp,
900 [ --disable-wtmp disable use of wtmp even if detected [no]],
901 [ AC_DEFINE(DISABLE_WTMP) ]
902)
903AC_ARG_ENABLE(wtmpx,
904 [ --disable-wtmpx disable use of wtmpx even if detected [no]],
905 [ AC_DEFINE(DISABLE_WTMPX) ]
906)
907AC_ARG_ENABLE(libutil,
908 [ --disable-libutil disable use of libutil (login() etc.) [no]],
909 [ AC_DEFINE(DISABLE_LOGIN) ]
910)
911AC_ARG_ENABLE(pututline,
912 [ --disable-pututline disable use of pututline() etc. ([uw]tmp) [no]],
913 [ AC_DEFINE(DISABLE_PUTUTLINE) ]
914)
915AC_ARG_ENABLE(pututxline,
916 [ --disable-pututxline disable use of pututxline() etc. ([uw]tmpx) [no]],
917 [ AC_DEFINE(DISABLE_PUTUTXLINE) ]
918)
919AC_ARG_WITH(lastlog,
920 [ --with-lastlog=FILE|DIR specify lastlog location [common locations]],
921 [ conf_lastlog_location="$withval"; ],)
922
923dnl lastlog, [uw]tmpx? detection
924dnl NOTE: set the paths in the platform section to avoid the
925dnl need for command-line parameters
926dnl lastlog and [uw]tmp are subject to a file search if all else fails
927
928dnl lastlog detection
929dnl NOTE: the code itself will detect if lastlog is a directory
930AC_MSG_CHECKING([if your system defines LASTLOG_FILE])
931AC_TRY_COMPILE([
932#include <sys/types.h>
933#include <utmp.h>
934#ifdef HAVE_LASTLOG_H
935# include <lastlog.h>
936#endif
937#ifdef PATHS_H
938# include <paths.h>
939#endif
940 ],
941 [ char *lastlog = LASTLOG_FILE; ],
942 [ AC_MSG_RESULT(yes) ],
943 [ AC_MSG_RESULT(no)
944 system_lastlog_path=no ]
945)
946if test -z "$conf_lastlog_location"; then
947 if test x"$system_lastlog_path" = x"no" ; then
948 for f in /var/log/lastlog /usr/adm/lastlog /var/adm/lastlog /etc/security/lastlog ; do
949 if test -e $f ; then
950 conf_lastlog_location=$f
951 fi
952 done
953 if test -z "$conf_lastlog_location"; then
954 AC_MSG_WARN([** Cannot find lastlog - disabling feature **])
955 AC_DEFINE(DISABLE_LASTLOG)
956 fi
957 fi
958fi
959
960if test -n "$conf_lastlog_location"; then
961 AC_DEFINE_UNQUOTED(CONF_LASTLOG_FILE, "$conf_lastlog_location")
962fi
963
964dnl utmp detection
965AC_MSG_CHECKING([if your system defines UTMP_FILE])
966AC_TRY_COMPILE([
967#include <sys/types.h>
968#include <utmp.h>
969#ifdef PATHS_H
970# include <paths.h>
971#endif
972 ],
973 [ char *utmp = UTMP_FILE; ],
974 [ AC_MSG_RESULT(yes) ],
975 [ AC_MSG_RESULT(no)
976 system_utmp_path=no ]
977)
978if test -z "$conf_utmp_location"; then
979 if test x"$system_utmp_path" = x"no" ; then
980 for f in /etc/utmp /usr/adm/utmp /var/run/utmp; do
981 if test -f $f ; then
982 conf_utmp_location=$f
983 fi
984 done
985 if test -z "$conf_utmp_location"; then
986 AC_DEFINE(DISABLE_UTMP)
987 fi
988 fi
989fi
990if test -n "$conf_utmp_location"; then
991 AC_DEFINE_UNQUOTED(CONF_UTMP_FILE, "$conf_utmp_location")
992fi
993
994dnl wtmp detection
995AC_MSG_CHECKING([if your system defines WTMP_FILE])
996AC_TRY_COMPILE([
997#include <sys/types.h>
998#include <utmp.h>
999#ifdef PATHS_H
1000# include <paths.h>
1001#endif
1002 ],
1003 [ char *wtmp = WTMP_FILE; ],
1004 [ AC_MSG_RESULT(yes) ],
1005 [ AC_MSG_RESULT(no)
1006 system_wtmp_path=no ]
1007)
1008if test -z "$conf_wtmp_location"; then
1009 if test x"$system_wtmp_path" = x"no" ; then
1010 for f in /usr/adm/wtmp /var/log/wtmp; do
1011 if test -f $f ; then
1012 conf_wtmp_location=$f
1013 fi
1014 done
1015 if test -z "$conf_wtmp_location"; then
1016 AC_DEFINE(DISABLE_WTMP)
1017 fi
1018 fi
1019fi
1020if test -n "$conf_wtmp_location"; then
1021 AC_DEFINE_UNQUOTED(CONF_WTMP_FILE, "$conf_wtmp_location")
1022fi
1023
1024
1025dnl utmpx detection - I don't know any system so perverse as to require
1026dnl utmpx, but not define UTMPX_FILE (ditto wtmpx.) No doubt it's out
1027dnl there, though.
1028AC_MSG_CHECKING([if your system defines UTMPX_FILE])
1029AC_TRY_COMPILE([
1030#include <sys/types.h>
1031#include <utmp.h>
1032#ifdef HAVE_UTMPX_H
1033#include <utmpx.h>
1034#endif
1035#ifdef PATHS_H
1036# include <paths.h>
1037#endif
1038 ],
1039 [ char *utmpx = UTMPX_FILE; ],
1040 [ AC_MSG_RESULT(yes) ],
1041 [ AC_MSG_RESULT(no)
1042 system_utmpx_path=no ]
1043)
1044if test -z "$conf_utmpx_location"; then
1045 if test x"$system_utmpx_path" = x"no" ; then
1046 AC_DEFINE(DISABLE_UTMPX)
1047 fi
1048else
1049 AC_DEFINE_UNQUOTED(CONF_UTMPX_FILE, "$conf_utmpx_location")
1050fi
1051
1052dnl wtmpx detection
1053AC_MSG_CHECKING([if your system defines WTMPX_FILE])
1054AC_TRY_COMPILE([
1055#include <sys/types.h>
1056#include <utmp.h>
1057#ifdef HAVE_UTMPX_H
1058#include <utmpx.h>
1059#endif
1060#ifdef PATHS_H
1061# include <paths.h>
1062#endif
1063 ],
1064 [ char *wtmpx = WTMPX_FILE; ],
1065 [ AC_MSG_RESULT(yes) ],
1066 [ AC_MSG_RESULT(no)
1067 system_wtmpx_path=no ]
1068)
1069if test -z "$conf_wtmpx_location"; then
1070 if test x"$system_wtmpx_path" = x"no" ; then
1071 AC_DEFINE(DISABLE_WTMPX)
1072 fi
1073else
1074 AC_DEFINE_UNQUOTED(CONF_WTMPX_FILE, "$conf_wtmpx_location")
1075fi
1076
925 1077
926# Change default command timeout for builtin PRNG 1078# Change default command timeout for builtin PRNG
927entropy_timeout=100 1079entropy_timeout=100
diff --git a/defines.h b/defines.h
index ef9130982..52f6c9f08 100644
--- a/defines.h
+++ b/defines.h
@@ -19,14 +19,6 @@
19# include <paths.h> /* For _PATH_XXX */ 19# include <paths.h> /* For _PATH_XXX */
20#endif 20#endif
21 21
22#ifdef HAVE_UTMP_H
23# include <utmp.h> /* For _PATH_XXX */
24#endif
25
26#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
27# include <utmpx.h> /* For _PATH_XXX */
28#endif
29
30#ifdef HAVE_SYS_TIME_H 22#ifdef HAVE_SYS_TIME_H
31# include <sys/time.h> /* For timersub */ 23# include <sys/time.h> /* For timersub */
32#endif 24#endif
@@ -161,47 +153,6 @@ typedef int ssize_t;
161 153
162/* Paths */ 154/* Paths */
163 155
164/* If _PATH_LASTLOG is not defined by system headers, set it to the */
165/* lastlog file detected by autoconf */
166#ifndef _PATH_LASTLOG
167# ifdef LASTLOG_LOCATION
168# define _PATH_LASTLOG LASTLOG_LOCATION
169# endif
170#endif
171
172#ifndef _PATH_UTMP
173# ifdef UTMP_FILE
174# define _PATH_UTMP UTMP_FILE
175# else
176# define _PATH_UTMP "/var/adm/utmp"
177# endif
178#endif
179
180#ifndef _PATH_WTMP
181# ifdef WTMP_FILE
182# define _PATH_WTMP WTMP_FILE
183# else
184# define _PATH_WTMP "/var/adm/wtmp"
185# endif
186#endif
187
188#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
189# ifndef _PATH_UTMPX
190# ifdef UTMPX_FILE
191# define _PATH_UTMPX UTMPX_FILE
192# else
193# define _PATH_UTMPX "/var/adm/utmpx"
194# endif
195# endif
196# ifndef _PATH_WTMPX
197# ifdef WTMPX_FILE
198# define _PATH_WTMPX WTMPX_FILE
199# else
200# define _PATH_WTMPX "/var/adm/wtmp"
201# endif
202# endif
203#endif
204
205#ifndef _PATH_BSHELL 156#ifndef _PATH_BSHELL
206# define _PATH_BSHELL "/bin/sh" 157# define _PATH_BSHELL "/bin/sh"
207#endif 158#endif
@@ -297,4 +248,80 @@ typedef int ssize_t;
297# define atexit(a) on_exit(a) 248# define atexit(a) on_exit(a)
298#endif /* !defined(HAVE_ATEXIT) && defined(HAVE_ON_EXIT) */ 249#endif /* !defined(HAVE_ATEXIT) && defined(HAVE_ON_EXIT) */
299 250
251/**
252 ** login recorder definitions
253 **/
254
255/* preprocess */
256
257#ifdef HAVE_UTMP_H
258# ifdef HAVE_TIME_IN_UTMP
259# include <time.h>
260# endif
261# include <utmp.h>
262#endif
263#ifdef HAVE_UTMPX_H
264# ifdef HAVE_TV_IN_UTMPX
265# include <sys/time.h>
266# endif
267# include <utmpx.h>
268#endif
269#ifdef HAVE_LASTLOG_H
270# include <lastlog.h>
271#endif
272#ifdef HAVE_PATHS_H
273# include <paths.h>
274#endif
275
276/* FIXME: put default paths back in */
277#if !defined(UTMP_FILE) && defined(_PATH_UTMP)
278# define UTMP_FILE _PATH_UTMP
279#endif
280#if !defined(WTMP_FILE) && defined(_PATH_WTMP)
281# define WTMP_FILE _PATH_WTMP
282#endif
283/* pick up the user's location for lastlog if given */
284#ifdef CONF_LASTLOG_FILE
285# define LASTLOG_FILE CONF_LASTLOG_FILE
286#endif
287#if !defined(LASTLOG_FILE) && defined(_PATH_LASTLOG)
288# define LASTLOG_FILE _PATH_LASTLOG
289#endif
290
291
292/* The login() library function in libutil is first choice */
293#if defined(HAVE_LOGIN) && !defined(DISABLE_LOGIN)
294# define USE_LOGIN
295
296#else
297/* Simply select your favourite login types. */
298/* Can't do if-else because some systems use several... <sigh> */
299# if defined(UTMPX_FILE) && !defined(DISABLE_UTMPX)
300# define USE_UTMPX
301# endif
302# if defined(UTMP_FILE) && !defined(DISABLE_UTMP)
303# define USE_UTMP
304# endif
305# if defined(WTMPX_FILE) && !defined(DISABLE_WTMPX)
306# define USE_WTMPX
307# endif
308# if defined(WTMP_FILE) && !defined(DISABLE_WTMP)
309# define USE_WTMP
310# endif
311
312#endif
313
314/* I hope that the presence of LASTLOG_FILE is enough to detect this */
315#if defined(LASTLOG_FILE) && !defined(DISABLE_LASTLOG)
316# define USE_LASTLOG
317#endif
318
319/* which type of time to use? (api.c) */
320#ifdef HAVE_SYS_TIME_H
321# define USE_TIMEVAL
322#endif
323
324/** end of login recorder definitions */
325
326
300#endif /* _DEFINES_H */ 327#endif /* _DEFINES_H */
diff --git a/login.c b/login.c
index 09e73b3de..305b67cbc 100644
--- a/login.c
+++ b/login.c
@@ -18,30 +18,9 @@
18 */ 18 */
19 19
20#include "includes.h" 20#include "includes.h"
21RCSID("$Id: login.c,v 1.30 2000/05/31 03:57:19 damien Exp $"); 21RCSID("$Id: login.c,v 1.31 2000/06/03 14:57:40 andre Exp $");
22 22
23#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) 23#include "loginrec.h"
24# include <utmpx.h>
25#endif
26#ifdef HAVE_UTMP_H
27# include <utmp.h>
28#endif
29#include "ssh.h"
30
31#ifdef HAVE_UTIL_H
32# include <util.h>
33#endif
34#ifdef HAVE_LASTLOG_H
35# include <lastlog.h>
36#endif
37#ifdef HAVE_LOGIN_H
38# include <login.h>
39#endif
40
41#ifdef WITH_AIXAUTHENTICATE
42/* This is done in do_authentication */
43# define DISABLE_LASTLOG
44#endif /* WITH_AIXAUTHENTICATE */
45 24
46/* 25/*
47 * Returns the time when the user last logged in. Returns 0 if the 26 * Returns the time when the user last logged in. Returns 0 if the
@@ -49,248 +28,32 @@ RCSID("$Id: login.c,v 1.30 2000/05/31 03:57:19 damien Exp $");
49 * The host the user logged in from will be returned in buf. 28 * The host the user logged in from will be returned in buf.
50 */ 29 */
51 30
52/*
53 * Returns the time when the user last logged in (or 0 if no previous login
54 * is found). The name of the host used last time is returned in buf.
55 */
56
57unsigned long 31unsigned long
58get_last_login_time(uid_t uid, const char *logname, 32get_last_login_time(uid_t uid, const char *logname,
59 char *buf, unsigned int bufsize) 33 char *buf, unsigned int bufsize)
60{ 34{
61#if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) 35 struct logininfo li;
62 struct lastlog ll;
63 int fd;
64# ifdef LASTLOG_IS_DIR
65 char lbuf[1024];
66
67 snprintf(lbuf, sizeof(buf), "%s/%s", _PATH_LASTLOG, logname);
68 if ((fd = open(lbuf, O_RDONLY)) < 0)
69 return 0;
70# else /* LASTLOG_IS_DIR */
71 buf[0] = '\0';
72
73 if ((fd = open(_PATH_LASTLOG, O_RDONLY)) < 0)
74 return 0;
75
76 lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
77# endif /* LASTLOG_IS_DIR */
78 if (read(fd, &ll, sizeof(ll)) != sizeof(ll)) {
79 close(fd);
80 return 0;
81 }
82
83 close(fd);
84
85 if (bufsize > sizeof(ll.ll_host) + 1)
86 bufsize = sizeof(ll.ll_host) + 1;
87 strncpy(buf, ll.ll_host, bufsize - 1);
88 buf[bufsize - 1] = 0;
89
90 return ll.ll_time;
91#else /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
92# ifdef HAVE_TYPE_IN_UTMP
93 /* Look in wtmp for the last login */
94 struct utmp wt;
95 int fd1;
96 unsigned long t = 0;
97
98 if ((fd1 = open(_PATH_WTMP, O_RDONLY)) < 0) {
99 error("Couldn't open %.100s to find last login time.", _PATH_WTMP);
100 return 0;
101 }
102
103 /* seek to last record of file */
104 lseek(fd1, (off_t)(0 - sizeof(struct utmp)), SEEK_END);
105
106 /* loop through wtmp for our last user login record */
107 do {
108 if (read(fd1, &wt, sizeof(wt)) != sizeof(wt)) {
109 close(fd1);
110 return 0;
111 }
112
113 if (wt.ut_type == USER_PROCESS) {
114 if (!strncmp(logname, wt.ut_user, 8)) {
115 t = (unsigned long)wt.ut_time;
116# ifdef HAVE_HOST_IN_UTMP
117 if (bufsize > sizeof(wt.ut_host) + 1)
118 bufsize = sizeof(wt.ut_host) + 1;
119 strncpy(buf, wt.ut_host, bufsize - 1);
120 buf[bufsize - 1] = 0;
121# else /* HAVE_HOST_IN_UTMP */
122 buf[0] = 0;
123# endif /* HAVE_HOST_IN_UTMP */
124 }
125 }
126 36
127 if (lseek(fd1, (off_t)(0 - (2 * sizeof(struct utmp))), SEEK_CUR) < 0) 37 login_getlastentry_uid(&li, uid);
128 break; 38 strncpy(buf, li.hostname, bufsize);
129 } while (t == 0); 39 return li.tv_sec;
130
131 return t;
132# else /* HAVE_TYPE_IN_UTMP */
133 return 0;
134# endif /* HAVE_TYPE_IN_UTMP */
135#endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
136} 40}
137 41
138/* 42/*
139 * Records that the user has logged in. I wish these parts of operating 43 * Records that the user has logged in. I these parts of operating systems
140 * systems were more standardized. 44 * were more standardized.
141 */ 45 */
46
142void 47void
143record_login(pid_t pid, const char *ttyname, const char *user, uid_t uid, 48record_login(pid_t pid, const char *ttyname, const char *user, uid_t uid,
144 const char *host, struct sockaddr * addr) 49 const char *host, struct sockaddr * addr)
145{ 50{
146#if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) 51 struct logininfo *li;
147 struct lastlog ll;
148# ifdef LASTLOG_IS_DIR
149 char buf[1024];
150# endif /* LASTLOG_IS_DIR */
151#endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
152 struct utmp u;
153#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
154 struct utmpx utx;
155#endif
156
157 /* Construct an utmp/wtmp entry. */
158 memset(&u, 0, sizeof(u));
159 strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));
160
161#if defined(HAVE_ID_IN_UTMP)
162# ifdef _AIX
163 strncpy(u.ut_id, ttyname + 5, sizeof(u.ut_id));
164# else /* !AIX */
165 strncpy(u.ut_id, ttyname + 8, sizeof(u.ut_id));
166# endif
167#endif /* defined(HAVE_ID_IN_UTMP) */
168
169 strncpy(u.ut_name, user, sizeof(u.ut_name));
170
171#if defined(HAVE_TV_IN_UTMP)
172 (void)gettimeofday(&u.ut_tv, NULL);
173#else /* defined(HAVE_TV_IN_UTMP) */
174 u.ut_time = time(NULL);
175#endif /* defined(HAVE_TV_IN_UTMP) */
176
177#if defined(HAVE_PID_IN_UTMP)
178 u.ut_pid = (pid_t)pid;
179#endif /* HAVE_PID_IN_UTMP */
180
181#if defined(HAVE_TYPE_IN_UTMP)
182 u.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS;
183#endif /* HAVE_TYPE_IN_UTMP */
184
185#if defined(HAVE_HOST_IN_UTMP)
186 strncpy(u.ut_host, host, sizeof(u.ut_host));
187#endif
188
189#if defined(HAVE_ADDR_IN_UTMP)
190 if (addr) {
191 switch (addr->sa_family) {
192 case AF_INET: {
193 struct sockaddr_in *in = (struct sockaddr_in*)addr;
194 memcpy(&(u.ut_addr), &(in->sin_addr), sizeof(&(in->sin_addr)));
195 break;
196 }
197# if defined(HAVE_ADDR_V6_IN_UTMP)
198 case AF_INET6: {
199 struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr;
200 memcpy(u.ut_addr_v6, &(in6->sin6_addr), sizeof(&(in6->sin6_addr)));
201 break;
202 }
203# endif /* defined(HAVE_ADDR_V6_IN_UTMP) */
204 default:
205 break;
206 }
207 }
208#endif /* defined(HAVE_ADDR_IN_UTMP) */
209 52
210#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX) 53 li = login_alloc_entry(pid, user, host, ttyname);
211 memset(&utx, 0, sizeof(utx)); 54 login_set_ip4(li, (struct sockaddr_in *)addr);
212 55 login_login(li);
213 strncpy(utx.ut_user, user, sizeof(utx.ut_name)); 56 login_free_entry(li);
214 strncpy(utx.ut_line, ttyname + 5, sizeof(utx.ut_line));
215 strncpy(utx.ut_id, ttyname + 8, sizeof(utx.ut_id));
216
217 utx.ut_pid = (pid_t)pid;
218 (void)gettimeofday(&utx.ut_tv, NULL);
219
220 utx.ut_type = (uid == -1)?DEAD_PROCESS:USER_PROCESS;
221# ifdef HAVE_HOST_IN_UTMPX
222# ifdef HAVE_SYSLEN_IN_UTMPX
223 utx.ut_syslen = strlen(host);
224 if (utx.ut_syslen + 1 > sizeof(utx.ut_host))
225 utx.ut_syslen = sizeof(utx.ut_host);
226 strncpy(utx.ut_host, host, utx.ut_syslen);
227# else
228 strncpy(utx.ut_host, host, sizeof(utx.ut_host));
229# endif /* HAVE_SYSLEN_IN_UTMPX */
230 utx.ut_host[sizeof(utx.ut_host)-1] = '\0';
231# endif
232
233# if defined(HAVE_ADDR_IN_UTMPX)
234 if (addr) {
235 switch (addr->sa_family) {
236 case AF_INET: {
237 struct sockaddr_in *in = (struct sockaddr_in*)addr;
238 memcpy(&(utx.ut_addr), &(in->sin_addr), sizeof(&(in->sin_addr)));
239 break;
240 }
241# if defined(HAVE_ADDR_V6_IN_UTMPX)
242 case AF_INET6: {
243 struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr;
244 memcpy(utx.ut_addr_v6, &(in6->sin6_addr), sizeof(&(in6->sin6_addr)));
245 break;
246 }
247# endif /* defined(HAVE_ADDR_V6_IN_UTMPX) */
248 default:
249 break;
250 }
251 }
252# endif /* defined(HAVE_ADDR_IN_UTMPX) */
253#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
254
255#if defined(HAVE_UTMPX_H) && defined(USE_UTMPX)
256 login(&utx);
257#else /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
258 login(&u);
259#endif /* defined(HAVE_UTMPX_H) && defined(USE_UTMPX) */
260
261#if defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG)
262 /* Update lastlog unless actually recording a logout. */
263 if (strcmp(user, "") != 0) {
264 int fd;
265 /*
266 * It is safer to bzero the lastlog structure first because
267 * some systems might have some extra fields in it (e.g. SGI)
268 */
269 memset(&ll, 0, sizeof(ll));
270
271 /* Update lastlog. */
272 ll.ll_time = time(NULL);
273 strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));
274 strncpy(ll.ll_host, host, sizeof(ll.ll_host));
275# ifdef LASTLOG_IS_DIR
276 snprintf(buf, sizeof(buf), "%s/%s", _PATH_LASTLOG, user);
277 if ((fd = open(buf, O_RDWR)) >= 0) {
278 if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
279 log("Could not write %.100s: %.100s", buf, strerror(errno));
280 close(fd);
281 }
282# else /* LASTLOG_IS_DIR */
283 if ((fd = open(_PATH_LASTLOG, O_RDWR)) >= 0) {
284 lseek(fd, (off_t) ((long) uid * sizeof(ll)), SEEK_SET);
285 if (write(fd, &ll, sizeof(ll)) != sizeof(ll)) {
286 log("Could not write %.100s: %.100s", _PATH_LASTLOG,
287 strerror(errno));
288 }
289 close(fd);
290 }
291# endif /* LASTLOG_IS_DIR */
292 }
293#endif /* defined(_PATH_LASTLOG) && !defined(DISABLE_LASTLOG) */
294} 57}
295 58
296/* Records that the user has logged out. */ 59/* Records that the user has logged out. */
@@ -298,11 +61,9 @@ record_login(pid_t pid, const char *ttyname, const char *user, uid_t uid,
298void 61void
299record_logout(pid_t pid, const char *ttyname) 62record_logout(pid_t pid, const char *ttyname)
300{ 63{
301#ifdef HAVE_LIBUTIL_LOGIN 64 struct logininfo *li;
302 const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */ 65
303 if (logout(line)) 66 li = login_alloc_entry(pid, NULL, NULL, ttyname);
304 logwtmp(line, "", ""); 67 login_logout(li);
305#else /* HAVE_LIBUTIL_LOGIN */ 68 login_free_entry(li);
306 record_login(pid, ttyname, "", -1, "", NULL);
307#endif /* HAVE_LIBUTIL_LOGIN */
308} 69}
diff --git a/loginrec.c b/loginrec.c
new file mode 100644
index 000000000..c35328cb6
--- /dev/null
+++ b/loginrec.c
@@ -0,0 +1,1385 @@
1/*
2 * Copyright (c) 2000 Andre Lucas. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Markus Friedl.
15 * 4. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/**
31 ** loginrec.c: platform-independent login recording and lastlog retrieval
32 **/
33
34/**
35 ** TODO:
36 ** sockaddr_* stuff isn't finished
37 **
38 ** Platform status:
39 ** ----------------
40 **
41 ** Known good:
42 ** Linux (Redhat 6.2, need more variants)
43 ** HP-UX 10.20 (gcc only)
44 **
45 ** Testing required: Please send reports!
46 ** Solaris
47 ** IRIX
48 ** NetBSD
49 ** HP-UX 11
50 **
51 ** Platforms with known problems:
52 ** AIX (need to port AIX stuff from old login code
53 ** NeXT
54 **
55 **/
56
57#include "includes.h"
58
59#include <sys/types.h>
60#include <sys/stat.h>
61#include <netinet/in.h>
62#include <unistd.h>
63#include <fcntl.h>
64#include <stdlib.h>
65#include <string.h>
66#include <stdio.h>
67#include <errno.h>
68#ifdef HAVE_PWD_H
69# include <pwd.h>
70#endif
71#ifdef HAVE_SYS_TIME_H
72# include <sys/time.h>
73#else
74# include <time.h>
75#endif
76
77#include "ssh.h"
78#include "xmalloc.h"
79#include "loginrec.h"
80
81RCSID("$Id: loginrec.c,v 1.1 2000/06/03 14:57:40 andre Exp $");
82
83
84/**
85 ** prototypes for helper functions in this file
86 **/
87
88#if HAVE_UTMP_H
89# include <utmp.h>
90void set_utmp_time(struct logininfo *li, struct utmp *ut);
91void construct_utmp(struct logininfo *li, struct utmp *ut);
92#endif
93
94#ifdef HAVE_UTMPX_H
95# include <utmpx.h>
96void set_utmpx_time(struct logininfo *li, struct utmpx *ut);
97void construct_utmpx(struct logininfo *li, struct utmpx *ut);
98#endif
99
100int utmp_write_entry(struct logininfo *li);
101int utmpx_write_entry(struct logininfo *li);
102int wtmp_write_entry(struct logininfo *li);
103int wtmpx_write_entry(struct logininfo *li);
104int lastlog_write_entry(struct logininfo *li);
105int syslogin_write_entry(struct logininfo *li);
106
107int getlast_entry(struct logininfo *li);
108int lastlog_get_entry(struct logininfo *li);
109int wtmp_get_entry(struct logininfo *li);
110int wtmpx_get_entry(struct logininfo *li);
111
112
113/**
114 ** platform-independent login functions
115 **/
116
117/* login_alloc_entry() - allocate and initialise a logininfo */
118struct logininfo *login_alloc_entry(int pid, const char *username,
119 const char *hostname,
120 const char *line) {
121 struct logininfo *newli;
122
123 newli = (struct logininfo *) xmalloc (sizeof(struct logininfo));
124
125 if (login_init_entry(newli, pid, username, hostname, line))
126 return newli;
127 else
128 return 0; /* fail */
129} /* login_alloc_entry() */
130
131
132/* login_free_entry() - free struct memory (duh) */
133void login_free_entry(struct logininfo *li) {
134 if (li && (li->line[0] != '\0'))
135 free ((void *)li);
136 else
137 log("login_free_entry: attempt to free invalid entry (warning)");
138} /* login_free_entry() */
139
140/* login_init_entry() - initialise a struct logininfo */
141int login_init_entry(struct logininfo *li,
142 int pid, const char *username,
143 const char *hostname, const char *line) {
144
145 /* zero the structure */
146 memset(li, 0, sizeof(struct logininfo));
147
148 /* progname should be set outside this call */
149 /* type stays null by default */
150 login_set_pid(li, pid);
151 /* set the line information */
152 login_set_line(li, line);
153 login_set_username(li, username);
154 login_set_hostname(li, hostname);
155 /* exit status and termination stay null by default */
156 login_set_current_time(li);
157 /* sockaddr_* stuff must be set separately (for now) */
158 return 1;
159} /* login_init_entry() */
160
161
162void
163login_set_progname(struct logininfo *li, const char *progname) {
164 memset(li->progname, '\0', sizeof(li->progname));
165 if (progname)
166 strlcpy(li->progname, progname, sizeof(li->progname));
167 else
168 li->progname[0] = '\0'; /* set to null */
169}
170
171void
172login_set_type(struct logininfo *li, int type) {
173 li->type = type;
174}
175
176void
177login_set_pid(struct logininfo *li, int pid) {
178 if (!pid)
179 li->pid = (int)getpid();
180 else
181 li->pid = pid;
182}
183
184void
185login_set_uid(struct logininfo *li, int uid) {
186 struct passwd *pw;
187
188 li->uid = uid;
189 /* now update the username */
190 pw = getpwuid(uid);
191 strlcpy(li->username, pw->pw_name, sizeof(li->username));
192}
193
194void
195login_set_line(struct logininfo *li, const char *line) {
196 if (line) {
197 /* canonical form is the full name, i.e. including '/dev' */
198 line_fullname(li->line, line, sizeof(li->line));
199 } else
200 li->line[0] = '\0';
201}
202
203void
204login_set_username(struct logininfo *li, const char *username) {
205 struct passwd *pw;
206
207 if (!username) {
208 li->username[0] = '\0';
209 li->uid = -1; /* hmm... */
210 } else {
211 strlcpy(li->username, username, sizeof(li->username));
212 /* now update the uid */
213 pw = getpwnam(username);
214 li->uid = pw->pw_uid;
215 }
216}
217
218
219void
220login_set_hostname(struct logininfo *li, const char *hostname) {
221 if (hostname) { /* can be null */
222 strlcpy(li->hostname, hostname, sizeof(li->hostname));
223 }
224}
225
226
227void
228login_set_exitstatus(struct logininfo *li,
229 int exit, int termination) {
230 /* FIXME: (ATL) And? */
231}
232
233
234/* tv_usec should be null on systems without struct timeval */
235void
236login_set_time(struct logininfo *li,
237 unsigned int tv_sec, unsigned int tv_usec) {
238 li->tv_sec = tv_sec;
239 li->tv_usec = tv_usec;
240}
241
242
243void
244login_set_current_time(struct logininfo *li) {
245#ifdef HAVE_SYS_TIME_H
246 struct timeval tv;
247
248 gettimeofday(&tv, NULL);
249 li->tv_sec = tv.tv_sec ; li->tv_usec = tv.tv_usec;
250#else
251 time_t t = time(0);
252
253 li->tv_sec = t; li->tv_usec = 0;
254#endif
255}
256
257void
258login_set_ip4(struct logininfo *li,
259 const struct sockaddr_in *sa_in4) {
260 memcpy((void *)&(li->hostaddr.sa_in4), (const void *)sa_in4,
261 sizeof(struct sockaddr_in));
262}
263
264#ifdef HAVE_IP6
265void
266login_set_ip6(struct logininfo *li,
267 const struct sockaddr_in6 *sa_in6) {
268 memcpy((void *)&(li->hostaddr.sa_in4), (const void *)sa_in6,
269 sizeof(struct sockaddr_in6));
270}
271#endif
272
273/*
274 * record the entry
275 */
276
277int
278login_write (struct logininfo *li) {
279
280 if ((int)geteuid() != 0) {
281 log("Attempt to write login records by non-root user (aborting)");
282 return 1;
283 }
284 /* set the timestamp */
285 login_set_current_time(li);
286#ifdef USE_LOGIN
287 syslogin_write_entry(li);
288#endif
289#ifdef USE_LASTLOG
290 if (li->type == LTYPE_LOGIN) {
291 lastlog_write_entry(li);
292 }
293#endif
294#ifdef USE_UTMP
295 utmp_write_entry(li);
296#endif
297#ifdef USE_WTMP
298 wtmp_write_entry(li);
299#endif
300#ifdef USE_UTMPX
301 utmpx_write_entry(li);
302#endif
303#ifdef USE_WTMPX
304 wtmpx_write_entry(li);
305#endif
306 return 0;
307}
308
309int
310login_login (struct logininfo *li) {
311 li->type = LTYPE_LOGIN;
312 return login_write(li);
313}
314
315int
316login_logout(struct logininfo *li) {
317 li->type = LTYPE_LOGOUT;
318 return login_write(li);
319}
320
321int
322login_log_entry(struct logininfo *li) {
323 return login_write(li);
324}
325
326
327unsigned int
328login_getlasttime_name(const char *username) {
329 struct logininfo li;
330
331 memset(&li, '\0', sizeof(li));
332 login_set_username(&li, username);
333 if (getlast_entry(&li))
334 return li.tv_sec;
335 else
336 return 0;
337} /* login_getlasttime_name() */
338
339
340unsigned int
341login_getlasttime_uid(const int uid) {
342 struct logininfo li;
343
344 memset(&li, '\0', sizeof(li));
345 login_set_uid(&li, uid);
346 if (getlast_entry(&li))
347 return li.tv_sec;
348 else
349 return 0;
350} /* login_getlasttime_uid() */
351
352
353struct logininfo *
354login_getlastentry_name(struct logininfo *li,
355 const char *username) {
356 login_set_username(li, username);
357 if (getlast_entry(li))
358 return li;
359 else
360 return 0;
361} /* login_getlastentry_name() */
362
363struct logininfo *
364login_getlastentry_uid(struct logininfo *li,
365 const int uid) {
366 login_set_uid(li, uid);
367 if (getlast_entry(li))
368 return li;
369 else
370 return 0;
371} /* login_getlastentry_uid() */
372
373
374/**
375 ** 'line' string utility functions
376 **/
377
378/*
379 * process the 'line' string into three forms:
380 * 1. The full filename (including '/dev')
381 * 2. The stripped name (excluding '/dev')
382 * 3. The abbreviated name (e.g. /dev/ttyp00
383 *
384 * Form 3 is used on some systems to identify a .tmp.? entry when
385 * attempting to remove it. Typically both addition and removal is
386 * performed by one application - say, sshd - so as long as the
387 * choice uniquely identifies a terminal and is the same at login and
388 * logout time, we're in good shape.
389 *
390 * NOTE: None of these calls actually allocate any memory -
391 * since their target is probably a structure, they don't
392 * need to.
393 */
394
395
396/* add the leading '/dev/' if it doesn't exist
397 * make sure dst has enough space, if not just copy src (ugh) */
398char *
399line_fullname(char *dst, const char *src, int dstsize) {
400 memset(dst, '\0', dstsize);
401 if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5)))
402 strlcpy(dst, src, dstsize);
403 else {
404 strlcpy(dst, "/dev/", 5);
405 strlcat(dst, src, dstsize);
406 }
407 return dst;
408}
409
410/* strip the leading '/dev' if it exists, return dst */
411char *
412line_stripname(char *dst, const char *src, int dstsize) {
413 memset(dst, '\0', dstsize);
414 if (strncmp(src, "/dev/", 5) == 0)
415 strlcpy(dst, &src[5], dstsize);
416 else
417 strlcpy(dst, src, dstsize);
418 return dst;
419} /* stripdev() */
420
421/* return the abbreviated (usually four-character) form *
422 * simple algorithm for making name:
423 * - first character is 'L' (arbitrary - 'lib(L)ogin' :-) )
424 * - remaining n characters are last n characters of line
425 * This is good for up to 999 ptys, I hope that's enough...
426 */
427char *
428line_abbrevname(char *dst, const char *src, int dstsize) {
429 memset(dst, '\0', dstsize);
430 dst[0]='L';
431 strlcpy(dst+1, &src[strlen(src)-(dstsize)], dstsize);
432 return dst;
433}
434
435
436/**
437 ** utmp utility functions
438 **/
439
440#if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN)
441
442#ifdef HAVE_UTMP_H
443# include <utmp.h>
444#endif
445#ifdef USE_TIMEVAL
446# include <sys/time.h>
447#else
448# include <time.h>
449#endif
450
451/* build the utmp structure */
452void
453set_utmp_time(struct logininfo *li, struct utmp *ut) {
454#ifdef HAVE_TV_IN_UTMP
455 ut->ut_tv.tv_sec = li->tv_sec;
456 ut->ut_tv.tv_usec = li->tv_usec;
457#else
458# ifdef HAVE_TIME_IN_UTMP
459 ut->ut_time = li->tv_sec;
460# endif
461#endif
462}
463
464void
465construct_utmp(struct logininfo *li,
466 struct utmp *ut) {
467 memset(ut, '\0', sizeof(struct utmp));
468
469#ifdef HAVE_ID_IN_UTMP
470 line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id));
471#endif
472
473#ifdef HAVE_TYPE_IN_UTMP
474 /* this is done here to keep utmp constants out of login.h */
475 switch (li->type) {
476 case LTYPE_LOGIN:
477 ut->ut_type = USER_PROCESS;
478 break;
479 case LTYPE_LOGOUT:
480 ut->ut_type = DEAD_PROCESS;
481 break;
482 }
483#endif
484
485#ifdef HAVE_PID_IN_UTMP
486 ut->ut_pid = li->pid;
487#endif
488 line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line));
489 strlcpy(ut->ut_name, li->username, sizeof(ut->ut_name));
490 set_utmp_time(li, ut);
491#ifdef HAVE_HOST_IN_UTMP
492 strlcpy(ut->ut_host, li->hostname, sizeof(ut->ut_host));
493#endif
494#ifdef HAVE_ADDR_IN_UTMP
495 /* !!! not supported yet (can't see its big use either) */
496#endif
497
498} /* construct_utmp() */
499
500#endif
501/* USE_UTMP || USE_WTMP || USE_LOGIN */
502
503/**
504 ** utmpx utility functions
505 **/
506
507#if defined(USE_UTMPX) || defined (USE_WTMPX)
508
509#ifdef HAVE_UTMPX_H
510# include <utmpx.h>
511#endif
512#ifdef USE_TIMEVAL
513# include <sys/time.h>
514#else
515# include <time.h>
516#endif
517
518/* build the utmpx structure */
519void
520set_utmpx_time(struct logininfo *li, struct utmpx *utx) {
521#ifdef HAVE_TV_IN_UTMPX
522 utx->ut_tv.tv_sec = li->tv_sec;
523 utx->ut_tv.tv_usec = li->tv_usec;
524#else
525# ifdef HAVE_TIME_IN_UTMPX
526 utx->ut_time = li->tv_sec;
527# endif
528#endif
529}
530
531void
532construct_utmpx(struct logininfo *li,
533 struct utmpx *utx) {
534 memset(utx, '\0', sizeof(struct utmpx));
535
536 line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id));
537
538 /* this is done here to keep utmp constants out of loginrec.h */
539 switch (li->type) {
540 case LTYPE_LOGIN:
541 utx->ut_type = USER_PROCESS;
542 break;
543 case LTYPE_LOGOUT:
544 utx->ut_type = DEAD_PROCESS;
545 break;
546 }
547
548 utx->ut_pid = li->pid;
549 line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line));
550 strlcpy(utx->ut_name, li->username, sizeof(utx->ut_name));
551 set_utmpx_time(li, utx);
552#ifdef HAVE_HOST_IN_UTMPX
553 strlcpy(utx->ut_host, li->hostname, sizeof(utx->ut_host));
554#endif
555#ifdef HAVE_ADDR_IN_UTMPX
556 /* !!! not supported yet (some issues with types of addresses) */
557#endif
558#ifdef HAVE_SYSLEN_IN_UTMPX
559 /* this is safe because of the extra nulls in logininfo */
560 utx->ut_syslen = strlen(li->hostname);
561#endif
562} /* construct_utmpx() */
563
564#endif
565/* USE_UTMPX || USE_WTMPX */
566
567
568
569/**
570 ** utmp functions
571 **/
572
573/* FIXME: (ATL) utmp_write_direct needs testing */
574
575#ifdef USE_UTMP
576
577#include <utmp.h>
578
579/* if we can, use pututline() etc. */
580#if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \
581 defined(HAVE_PUTUTLINE)
582# define UTMP_USE_LIBRARY
583#endif
584
585
586/* write a utmp entry with the system's help (pututline() and pals) */
587#ifdef UTMP_USE_LIBRARY
588static int
589utmp_write_library(struct logininfo *li, struct utmp *ut) {
590
591 setutent();
592 pututline(ut);
593
594#ifdef HAVE_ENDUTENT
595 endutent();
596#endif
597 return 1;
598} /* utmp_write_library() */
599
600#else
601
602/* write a utmp entry direct to the file */
603/* This code is a slightly modification of code in OpenBSD's login.c
604 * (in libutil) and so is subject to the OpenBSD Licensing terms. */
605static int
606utmp_write_direct(struct logininfo *li, struct utmp *ut) {
607 struct utmp old_ut;
608 register int fd;
609 int tty;
610
611 tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */
612
613 if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) {
614 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
615 /*
616 * Prevent luser from zero'ing out ut_host.
617 * If the new ut_line is empty but the old one is not
618 * and ut_line and ut_name match, preserve the old ut_line.
619 */
620 if ( read(fd, &old_ut, sizeof(struct utmp)) == sizeof(struct utmp)
621 && ut->ut_host[0] == '\0'
622 && old_ut.ut_host[0] != '\0'
623 && strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0
624 && strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0 )
625 (void)memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host));
626
627 (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET);
628 if (write(fd, ut, sizeof(struct utmp))==-1)
629 log("utmp_write_direct: error writing %s: %s",
630 UTMP_FILE, strerror(errno));
631
632 (void)close(fd);
633 return 1;
634 } else
635 return 0;
636} /* utmp_write_direct() */
637
638#endif /* UTMP_USE_LIBRARY */
639
640
641static int
642utmp_perform_login(struct logininfo *li) {
643 struct utmp ut;
644
645 construct_utmp(li, &ut);
646
647#ifdef UTMP_USE_LIBRARY
648 if (!utmp_write_library(li, &ut)) {
649 log("utmp_perform_login: utmp_write_library() failed");
650 return 0;
651 }
652#else
653 if (!utmp_write_direct(li, &ut)) {
654 log("utmp_perform_login: utmp_write_direct() failed");
655 return 0;
656 }
657#endif
658 return 1;
659} /* utmp_perform_login() */
660
661
662static int
663utmp_perform_logout(struct logininfo *li) {
664 struct utmp ut;
665
666 memset(&ut, '\0', sizeof(ut));
667 set_utmp_time(li, &ut);
668 line_stripname(ut.ut_line, li->line, sizeof(ut.ut_line));
669#ifdef HAVE_ID_IN_UTMP
670 line_abbrevname(ut.ut_id, li->line, sizeof(ut.ut_id));
671#endif
672#ifdef HAVE_TYPE_IN_UTMP
673 ut.ut_type = DEAD_PROCESS;
674#endif
675
676#if !defined(DISABLE_PUTUTLINE) \
677 && defined(HAVE_SETUTENT) && defined(HAVE_PUTUTLINE)
678 utmp_write_library(li, &ut);
679#else
680 utmp_write_direct(li, &ut);
681#endif
682
683 return 1;
684} /* utmp_perform_logout() */
685
686
687int
688utmp_write_entry(struct logininfo *li) {
689
690 switch(li->type) {
691 case LTYPE_LOGIN:
692 return utmp_perform_login(li);
693
694 case LTYPE_LOGOUT:
695 return utmp_perform_logout(li);
696
697 default:
698 log("utmp_write_entry: invalid type field");
699 return 0;
700 }
701} /* utmp_write_entry() */
702
703
704#endif
705/* USE_UTMP */
706
707
708/**
709 ** utmpx functions
710 **/
711
712/* not much point if we don't want utmpx entries */
713#ifdef USE_UTMPX
714
715#include <utmpx.h>
716
717/* if we have the wherewithall, use pututxline etc. */
718#if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) \
719 && defined(HAVE_PUTUTXLINE)
720# define UTMPX_USE_LIBRARY
721#endif
722
723
724/* write a utmpx entry with the system's help (pututxline() and pals) */
725#ifdef UTMPX_USE_LIBRARY
726static int
727utmpx_write_library(struct logininfo *li, struct utmpx *utx) {
728
729 setutxent();
730 pututxline(utx);
731
732#ifdef HAVE_ENDUTXENT
733 endutxent();
734#endif
735 return 1;
736} /* utmpx_write_library() */
737
738#else
739/* UTMPX_USE_LIBRARY */
740
741
742/* write a utmp entry direct to the file */
743static int
744utmpx_write_direct(struct logininfo *li, struct utmpx *utx) {
745
746 log("utmpx_write_direct: not implemented!");
747 return 0;
748 } /* utmpx_write_direct() */
749
750#endif
751/* UTMPX_USE_LIBRARY */
752
753static int
754utmpx_perform_login(struct logininfo *li) {
755 struct utmpx utx;
756
757 construct_utmpx(li, &utx);
758
759#ifdef UTMPX_USE_LIBRARY
760 if (!utmpx_write_library(li, &utx)) {
761 log("utmpx_perform_login: utmp_write_library() failed");
762 return 0;
763 }
764#else
765 if (!utmpx_write_direct(li, &ut)) {
766 log("utmpx_perform_login: utmp_write_direct() failed");
767 return 0;
768 }
769#endif
770 return 1;
771} /* utmpx_perform_login() */
772
773
774static int
775utmpx_perform_logout(struct logininfo *li) {
776 struct utmpx utx;
777
778 memset(&utx, '\0', sizeof(utx));
779 set_utmpx_time(li, &utx);
780 line_stripname(utx.ut_line, li->line, sizeof(utx.ut_line));
781#ifdef HAVE_ID_IN_UTMPX
782 line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id));
783#endif
784#ifdef HAVE_TYPE_IN_UTMPX
785 utx.ut_type = DEAD_PROCESS;
786#endif
787
788#ifdef UTMPX_USE_LIBRARY
789 utmpx_write_library(li, &utx);
790#else
791 utmpx_write_direct(li, &utx);
792#endif
793
794 return 1;
795} /* utmpx_perform_logout() */
796
797
798int
799utmpx_write_entry(struct logininfo *li) {
800
801 switch(li->type) {
802 case LTYPE_LOGIN:
803 return utmpx_perform_login(li);
804 case LTYPE_LOGOUT:
805 return utmpx_perform_logout(li);
806 default:
807 log("utmpx_write_entry: invalid type field");
808 return 0;
809 }
810} /* utmpx_write_entry() */
811
812
813#endif
814/* USE_UTMPX */
815
816
817/**
818 ** wtmp functions
819 **/
820
821#ifdef USE_WTMP
822
823# include <utmp.h>
824
825/* write a wtmp entry direct to the end of the file */
826/* This code is a slight modification of code in OpenBSD's logwtmp.c
827 * (in libutil) and so is subject to the OpenBSD licensing terms */
828static int
829wtmp_write(struct logininfo *li, struct utmp *ut) {
830 struct stat buf;
831 int fd, ret = 1;
832
833 if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
834 log("wtmp_write: problem writing %s: %s",
835 WTMP_FILE, strerror(errno));
836 return 0;
837 }
838
839 if (fstat(fd, &buf) == 0)
840 if (write(fd, (char *)ut, sizeof(struct utmp)) !=
841 sizeof(struct utmp)) {
842 ftruncate(fd, buf.st_size);
843 log("wtmp_write: problem writing %s: %s",
844 WTMP_FILE, strerror(errno));
845 ret = 0;
846 }
847 (void)close(fd);
848
849 return ret;
850} /* wtmp_write() */
851
852
853
854static int
855wtmp_perform_login(struct logininfo *li) {
856 struct utmp ut;
857
858 construct_utmp(li, &ut);
859 return wtmp_write(li, &ut);
860} /* wtmp_perform_login() */
861
862
863static int
864wtmp_perform_logout(struct logininfo *li) {
865 struct utmp ut;
866
867 construct_utmp(li, &ut);
868 /* blank out unnecessary fields */
869 memset(&(ut.ut_name), '\0', sizeof(ut.ut_name));
870#ifdef HAVE_ID_IN_UTMP
871 memset(&(ut.ut_id), '\0', sizeof(ut.ut_id));
872#endif
873#ifdef HAVE_HOST_IN_UTMP
874 memset(&(ut.ut_host), '\0', sizeof(ut.ut_host));
875#endif
876#ifdef HAVE_ADDR_IN_UTMP
877 memset(&(ut.ut_addr), '\0', sizeof(ut.ut_addr));
878#endif
879 return wtmp_write(li, &ut);
880} /* wtmp_perform_logout() */
881
882
883int
884wtmp_write_entry(struct logininfo *li) {
885
886 switch(li->type) {
887 case LTYPE_LOGIN:
888 return wtmp_perform_login(li);
889 case LTYPE_LOGOUT:
890 return wtmp_perform_logout(li);
891 default:
892 log("wtmp_write_entry: invalid type field");
893 return 0;
894 }
895} /* wtmp_write_entry() */
896
897
898
899int
900wtmp_get_entry(struct logininfo *li) {
901 struct stat st;
902 struct utmp ut;
903 int fd;
904
905 if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) {
906 log("wtmp_get_entry: problem opening %s: %s",
907 WTMP_FILE, strerror(errno));
908 return 0;
909 }
910
911 if (fstat(fd, &st) != 0) {
912 log("wtmp_get_entry: couldn't stat %s: %s",
913 WTMP_FILE, strerror(errno));
914 close(fd);
915 return 0;
916 }
917
918 (void)lseek(fd, (off_t)(0-sizeof(struct utmp)), SEEK_END);
919
920 do {
921 if (read(fd, &ut, sizeof(ut)) != sizeof(ut)) {
922 log("wtmp_get_entry: read of %s failed: %s",
923 WTMP_FILE, strerror(errno));
924 close (fd);
925 return 0;
926 }
927
928 /* Logouts are recorded as a blank username on a particular line.
929 * So, we just need to find the username in struct utmp */
930 if ( strncmp(li->username, ut.ut_user, 8) == 0 ) {
931 /* note we've already made sure there's a time in struct utmp */
932#ifdef HAVE_TIME_IN_UTMP
933 li->tv_sec = ut.ut_time;
934#else
935# if HAVE_TV_IN_UTMP
936 li->tv_sec = ut.ut_tv.tv_sec;
937# endif
938#endif
939 line_fullname(li->line, ut.ut_line, sizeof(ut.ut_line));
940#ifdef HAVE_HOST_IN_UTMP
941 strlcpy(li->hostname, ut.ut_host, sizeof(ut.ut_host));
942#endif
943 }
944 if (lseek(fd, (off_t)(0-2*sizeof(struct utmp)), SEEK_CUR) == -1) {
945 close (fd);
946 return 0;
947 }
948 } while (li->tv_sec == 0);
949
950 return 1;
951} /* wtmp_get_entry() */
952
953
954#endif
955/* USE_WTMP */
956
957
958/**
959 ** wtmpx functions
960 **/
961
962#ifdef USE_WTMPX
963
964# include <utmpx.h>
965
966/* write a wtmpx entry direct to the end of the file */
967/* This code is a slight modification of code in OpenBSD's logwtmp.c
968 * (in libutil) and so is subject to the OpenBSD licensing terms */
969static int
970wtmpx_write(struct logininfo *li, struct utmpx *utx) {
971 struct stat buf;
972 int fd, ret = 1;
973
974 if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) {
975 log("wtmpx_write: problem opening %s: %s",
976 WTMPX_FILE, strerror(errno));
977 return 0;
978 }
979
980 if (fstat(fd, &buf) == 0)
981 if (write(fd, (char *)utx, sizeof(struct utmpx)) !=
982 sizeof(struct utmpx)) {
983 ftruncate(fd, buf.st_size);
984 log("wtmpx_write: problem writing %s: %s",
985 WTMPX_FILE, strerror(errno));
986 ret = 0;
987 }
988 (void)close(fd);
989
990 return ret;
991} /* wtmpx_write() */
992
993
994
995static int
996wtmpx_perform_login(struct logininfo *li) {
997 struct utmpx utx;
998
999 construct_utmpx(li, &utx);
1000 return wtmpx_write(li, &utx);
1001} /* wtmpx_perform_login() */
1002
1003
1004static int
1005wtmpx_perform_logout(struct logininfo *li) {
1006 struct utmpx utx;
1007
1008 construct_utmpx(li, &utx);
1009 /* blank out unnecessary fields */
1010 memset(&(utx.ut_name), '\0', sizeof(utx.ut_name));
1011#ifdef HAVE_ID_IN_UTMPX
1012 memset(&(utx.ut_id), '\0', sizeof(utx.ut_id));
1013#endif
1014#ifdef HAVE_HOST_IN_UTMPX
1015 memset(&(utx.ut_host), '\0', sizeof(utx.ut_host));
1016#endif
1017#ifdef HAVE_ADDR_IN_UTMPX
1018 memset(&(utx.ut_addr), '\0', sizeof(utx.ut_addr));
1019#endif
1020 return wtmpx_write(li, &utx);
1021
1022} /* wtmpx_perform_logout() */
1023
1024
1025int
1026wtmpx_write_entry(struct logininfo *li) {
1027
1028 switch(li->type) {
1029 case LTYPE_LOGIN:
1030 return wtmpx_perform_login(li);
1031 case LTYPE_LOGOUT:
1032 return wtmpx_perform_logout(li);
1033 default:
1034 log("wtmpx_write_entry: invalid type field");
1035 return 0;
1036 }
1037} /* wtmpx_write_entry() */
1038
1039
1040
1041int
1042wtmpx_get_entry(struct logininfo *li) {
1043 struct stat st;
1044 struct utmpx utx;
1045 int fd;
1046
1047 if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) {
1048 log("wtmpx_get_entry: problem opening %s: %s",
1049 WTMPX_FILE, strerror(errno));
1050 return 0;
1051 }
1052
1053 if (fstat(fd, &st) != 0) {
1054 log("wtmpx_get_entry: couldn't stat %s: %s",
1055 WTMP_FILE, strerror(errno));
1056 close(fd);
1057 return 0;
1058 }
1059
1060 (void)lseek(fd, (off_t)(0-sizeof(struct utmpx)), SEEK_END);
1061
1062 do {
1063 if (read(fd, &utx, sizeof(utx)) != sizeof(utx)) {
1064 log("wtmpx_get_entry: read of %s failed: %s",
1065 WTMPX_FILE, strerror(errno));
1066 close (fd);
1067 return 0;
1068 }
1069
1070 /* Logouts are recorded as a blank username on a particular line.
1071 * So, we just need to find the username in struct utmpx */
1072 if ( strncmp(li->username, utx.ut_user, 8) == 0 ) {
1073 /* note we've already made sure there's a time in struct utmp */
1074#ifdef HAVE_TV_IN_UTMPX
1075 li->tv_sec = utx.ut_tv.tv_sec;
1076#else
1077# ifdef HAVE_TIME_IN_UTMPX
1078 li->tv_sec = utx.ut_time;
1079# endif
1080#endif
1081 line_fullname(li->line, utx.ut_line, sizeof(utx.ut_line));
1082#ifdef HAVE_HOST_IN_UTMPX
1083 strlcpy(li->hostname, utx.ut_host, sizeof(utx.ut_line));
1084#endif
1085 }
1086 if (lseek(fd, (off_t)(0-2*sizeof(struct utmpx)), SEEK_CUR) == -1) {
1087 close (fd);
1088 return 0;
1089 }
1090 } while (li->tv_sec == 0);
1091 return 1;
1092} /* wtmpx_get_entry() */
1093
1094
1095
1096#endif
1097/* USE_WTMPX */
1098
1099
1100
1101/**
1102 ** libutil login() functions
1103 **/
1104
1105#ifdef USE_LOGIN
1106
1107#ifdef HAVE_UTMP_H
1108# include <utmp.h>
1109#endif
1110#ifdef HAVE_UTIL_H
1111# include <util.h>
1112#endif
1113#ifdef USE_TIMEVAL
1114# include <sys/time.h>
1115#else
1116# include <time.h>
1117#endif
1118
1119static int
1120syslogin_perform_login(struct logininfo *li) {
1121 struct utmp *ut;
1122
1123 if (! (ut = (struct utmp *)malloc(sizeof(struct utmp)))) {
1124 log("syslogin_perform_login: couldn't malloc()");
1125 return 0;
1126 }
1127 construct_utmp(li, ut);
1128 login(ut);
1129
1130 return 1;
1131} /* syslogin_perform_login() */
1132
1133static int
1134syslogin_perform_logout(struct logininfo *li) {
1135
1136#ifdef HAVE_LOGOUT
1137 char line[8];
1138
1139 (void)line_stripname(line, li->line, sizeof(line));
1140
1141 if (!logout(line)) {
1142 log("syslogin_perform_logout: logout() returned an error");
1143# ifdef HAVE_LOGWTMP
1144 } else {
1145 logwtmp(line, "", "");
1146 }
1147# endif
1148 /* TODO: what to do if we have login, but no logout?
1149 * what if logout but no logwtmp? All routines are in libutil
1150 * so they should all be there, but... */
1151#endif
1152 return 1;
1153} /* syslogin_perform_logout() */
1154
1155
1156
1157int
1158syslogin_write_entry(struct logininfo *li) {
1159
1160 switch (li->type) {
1161 case LTYPE_LOGIN:
1162 return syslogin_perform_login(li);
1163 case LTYPE_LOGOUT:
1164 return syslogin_perform_logout(li);
1165 default:
1166 log("syslogin_write_entry: Invalid type field");
1167 return 0;
1168 }
1169} /* utmp_write_entry() */
1170
1171
1172#endif
1173/* USE_LOGIN */
1174
1175/* end of file log-syslogin.c */
1176
1177
1178/**
1179 ** lastlog functions
1180 **/
1181
1182#ifdef USE_LASTLOG
1183
1184#ifdef HAVE_LASTLOG_H
1185# include <lastlog.h>
1186#else
1187# if !defined(USE_UTMP) && !defined(USE_WTMP)
1188# include <utmp.h>
1189# endif
1190#endif
1191
1192
1193static void
1194lastlog_construct(struct logininfo *li,
1195 struct lastlog *last) {
1196 /* clear the structure */
1197 memset(last, '\0', sizeof(struct lastlog));
1198
1199 (void)line_stripname(last->ll_line, li->line,
1200 sizeof(last->ll_line));
1201 strlcpy(last->ll_host, li->hostname, sizeof(last->ll_host));
1202 last->ll_time = li->tv_sec;
1203} /* lastlog_construct() */
1204
1205
1206#define LL_FILE 1
1207#define LL_DIR 2
1208#define LL_OTHER 3
1209
1210static int
1211lastlog_filetype(char *filename) {
1212 struct stat st;
1213
1214 if ( stat(LASTLOG_FILE, &st) != 0) {
1215 log("lastlog_perform_login: Couldn't stat %s: %s",
1216 LASTLOG_FILE, strerror(errno));
1217 return 0;
1218 }
1219
1220 if (S_ISDIR(st.st_mode))
1221 return LL_DIR;
1222 else if (S_ISREG(st.st_mode))
1223 return LL_FILE;
1224 else
1225 return LL_OTHER;
1226} /* lastlog_filetype() */
1227
1228
1229/* open the file (using filemode) and seek to the login entry */
1230static int
1231lastlog_openseek(struct logininfo *li, int *fd, int filemode) {
1232
1233 off_t offset;
1234 int type;
1235 char lastlog_file[1024];
1236
1237 type = lastlog_filetype(LASTLOG_FILE);
1238 switch (type) {
1239 case LL_FILE:
1240 strlcpy(lastlog_file, LASTLOG_FILE, sizeof(lastlog_file));
1241 break;
1242 case LL_DIR:
1243 snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s",
1244 LASTLOG_FILE, li->username);
1245 break;
1246 default:
1247 log("lastlog_openseek: %.100s is not a file or directory!",
1248 LASTLOG_FILE);
1249 return 0;
1250 } /* switch */
1251
1252 *fd = open(lastlog_file, filemode);
1253 if ( *fd < 0) {
1254 log("lastlog_openseek: Couldn't open %s: %s",
1255 lastlog_file, strerror(errno));
1256 return 0;
1257 }
1258
1259 /* find this uid's offset in the lastlog file */
1260 offset = (off_t) ( (long)li->uid * sizeof(struct lastlog));
1261
1262 if ( lseek(*fd, offset, SEEK_SET) != offset ) {
1263 log("lastlog_openseek: %s->lseek(): %s",
1264 lastlog_file, strerror(errno));
1265 return 0;
1266 }
1267 return 1;
1268} /* lastlog_openseek() */
1269
1270static int
1271lastlog_perform_login(struct logininfo *li) {
1272 struct lastlog last;
1273 int fd;
1274
1275 /* create our struct lastlog */
1276 lastlog_construct(li, &last);
1277
1278 /* write the entry */
1279 if (lastlog_openseek(li, &fd, O_RDWR)) {
1280 if ( write(fd, &last, sizeof(struct lastlog))
1281 != sizeof(struct lastlog) ) {
1282 log("lastlog_write_filemode: Error writing to %s: %s",
1283 LASTLOG_FILE, strerror(errno));
1284 return 0;
1285 }
1286 return 1;
1287 } else
1288 return 0;
1289} /* lastlog_perform_login() */
1290
1291
1292int
1293lastlog_write_entry(struct logininfo *li) {
1294
1295 switch(li->type) {
1296 case LTYPE_LOGIN:
1297 return lastlog_perform_login(li);
1298 default:
1299 log("lastlog_write_entry: Invalid type field");
1300 return 0;
1301 }
1302} /* lastlog_write_entry() */
1303
1304
1305
1306static void
1307lastlog_populate_entry(struct logininfo *li,
1308 struct lastlog *last) {
1309 line_fullname(li->line, last->ll_line, sizeof(li->line));
1310 strlcpy(li->hostname, last->ll_host, sizeof(li->hostname));
1311 li->tv_sec = last->ll_time;
1312} /* lastlog_populate_entry() */
1313
1314
1315
1316int
1317lastlog_get_entry(struct logininfo *li) {
1318 struct lastlog last;
1319 int fd;
1320
1321 if (lastlog_openseek(li, &fd, O_RDONLY)) {
1322 if ( read(fd, &last, sizeof(struct lastlog))
1323 != sizeof(struct lastlog) ) {
1324 log("lastlog_write_filemode: Error reading from %s: %s",
1325 LASTLOG_FILE, strerror(errno));
1326 return 0;
1327 } else {
1328 lastlog_populate_entry(li, &last);
1329 return 1;
1330 }
1331
1332 } else
1333 return 0;
1334} /* lastlog_get_entry() */
1335
1336
1337#endif
1338/* USE_LASTLOG */
1339
1340
1341/**
1342 ** lastlog retrieval functions
1343 **/
1344
1345/* take the uid in li and return the last login time */
1346int
1347getlast_entry(struct logininfo *li) {
1348
1349#ifdef USE_LASTLOG
1350 if (lastlog_get_entry(li))
1351 return 1;
1352 else
1353 return 0;
1354#else
1355 /* !USE_LASTLOG */
1356 /* Try to retrieve the last login time from another source */
1357
1358# if defined(USE_WTMP) && (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP))
1359
1360 /* retrieve last login time from utmp */
1361 if (wtmp_get_entry(li))
1362 return 1;
1363 else
1364 return 0;
1365
1366# else
1367# if defined(USE_WTMPX) && (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX))
1368
1369 /* retrieve last login time from utmpx */
1370 if (wtmpx_get_entry(li))
1371 return 1;
1372 else
1373 return 0;
1374
1375# else
1376
1377 /* no means of retrieving last login time */
1378 return 0;
1379# endif
1380# endif
1381
1382#endif
1383 /* USE_LASTLOG */
1384
1385}
diff --git a/loginrec.h b/loginrec.h
new file mode 100644
index 000000000..0f268ce6a
--- /dev/null
+++ b/loginrec.h
@@ -0,0 +1,167 @@
1#ifndef _HAVE_LOGINREC_H_
2#define _HAVE_LOGINREC_H_
3
4/*
5 * Copyright (c) 2000 Andre Lucas. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Markus Friedl.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/**
34 ** loginrec.h: platform-independent login recording and lastlog retrieval
35 **/
36
37#include "includes.h"
38
39#include <sys/types.h>
40#include <netinet/in.h>
41#include <sys/socket.h>
42
43/* RCSID("$Id: loginrec.h,v 1.1 2000/06/03 14:57:40 andre Exp $"); */
44
45/**
46 ** you should use the login_* calls to work around platform dependencies
47 **/
48
49/* check if we have IP6 on this system */
50#if defined(AF_INET6) || defined(INET6_ADDRSTRLEN)
51# define LOGIN_HAVE_IP6
52#endif
53
54/*
55 * login_netinfo structure
56 */
57
58struct login_netinfo {
59 struct sockaddr_in sa_in4;
60#ifdef LOGIN_HAVE_IP6
61 struct sockaddr_in6 sa_in6;
62#endif
63
64}; /* struct login_netinfo */
65
66
67/*
68 * * logininfo structure *
69 */
70
71/* types - different to utmp.h 'type' macros */
72/* (though set to the same value as linux, openbsd and others...) */
73#define LTYPE_LOGIN 7
74#define LTYPE_LOGOUT 8
75
76/* string lengths - set very long */
77#define LINFO_PROGSIZE 64
78#define LINFO_LINESIZE 64
79#define LINFO_NAMESIZE 64
80#define LINFO_HOSTSIZE 256
81
82struct logininfo {
83
84 char progname[LINFO_PROGSIZE]; /* name of program (for PAM) */
85 int progname_null;
86
87 short int type; /* type of login (LTYPE_*) */
88
89 int pid; /* PID of login process */
90 int uid; /* UID of this user */
91 char line[LINFO_LINESIZE]; /* tty/pty name */
92 char username[LINFO_NAMESIZE]; /* login username */
93 char hostname[LINFO_HOSTSIZE]; /* remote hostname */
94
95 /* 'exit_status' structure components */
96 int exit; /* process exit status */
97 int termination; /* process termination status */
98
99 /* struct timeval (sys/time.h) isn't always available, if it isn't we'll
100 * use time_t's value as tv_sec and set tv_usec to 0
101 */
102 unsigned int tv_sec;
103 unsigned int tv_usec;
104
105 struct login_netinfo hostaddr; /* caller's host address(es) */
106
107}; /* struct logininfo */
108
109
110/*
111 * login recording functions
112 */
113/* construct a new login entry */
114struct logininfo *login_alloc_entry(int pid,
115 const char *username,
116 const char *hostname, const char *line);
117void login_free_entry(struct logininfo *li);
118int login_init_entry(struct logininfo *li,
119 int pid, const char *username,
120 const char *hostname, const char *line);
121void login_set_progname(struct logininfo *li,
122 const char *progname);
123/* set the type field (skip if using ...login or ...logout) */
124void login_set_type(struct logininfo *li, int type);
125void login_set_pid(struct logininfo *li, int pid);
126void login_set_uid(struct logininfo *li, int uid);
127void login_set_line(struct logininfo *li, const char *line);
128void login_set_username(struct logininfo *li, const char *username);
129void login_set_hostname(struct logininfo *li, const char *hostname);
130/* set the exit status (used by [uw]tmpx) */
131void login_set_exitstatus(struct logininfo *li, int exit, int termination);
132void login_set_time(struct logininfo *li, unsigned int tv_sec,
133 unsigned int tv_usec);
134void login_set_current_time(struct logininfo *li);
135/* set the network address based on network address type */
136void login_set_ip4(struct logininfo *li,
137 const struct sockaddr_in *sa_in4);
138# ifdef LOGIN_HAVE_IP6
139void login_set_ip6(struct logininfo *li,
140 const struct sockaddr_in6 *sa_in6);
141# endif /* LOGIN_HAVE_IP6 */
142/* record the entry */
143int login_write (struct logininfo *li);
144int login_login (struct logininfo *li);
145int login_logout(struct logininfo *li);
146int login_log_entry(struct logininfo *li);
147
148/*
149 * login record retrieval functions
150 */
151/* lastlog *entry* functions fill out a logininfo */
152struct logininfo *login_getlastentry_name(struct logininfo *li,
153 const char *username);
154struct logininfo *login_getlastentry_uid(struct logininfo *li,
155 const int pid);
156/* lastlog *time* functions return time_t equivalent (uint) */
157unsigned int login_getlasttime_name(const char *username);
158unsigned int login_getlasttime_uid(const int pid);
159
160/* produce various forms of the line filename */
161char *line_fullname(char *dst, const char *src, int dstsize);
162char *line_stripname(char *dst, const char *src, int dstsize);
163char *line_abbrevname(char *dst, const char *src, int dstsize);
164
165
166#endif /* _HAVE_LOGINREC_H_ */
167
diff --git a/logintest.c b/logintest.c
new file mode 100644
index 000000000..d26f9ef9e
--- /dev/null
+++ b/logintest.c
@@ -0,0 +1,307 @@
1/*
2 * Copyright (c) 2000 Andre Lucas. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Markus Friedl.
15 * 4. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/**
31 ** logintest.c: simple test driver for platform-independent login recording
32 ** and lastlog retrieval
33 **/
34
35#include "config.h"
36
37#include <sys/types.h>
38#include <sys/wait.h>
39#include <unistd.h>
40#include <stdlib.h>
41#include <stdio.h>
42#include <string.h>
43#include <pwd.h>
44#include <netdb.h>
45#ifdef HAVE_TIME_H
46#include <time.h>
47#endif
48
49#include "loginrec.h"
50
51RCSID("$Id: logintest.c,v 1.1 2000/06/03 14:57:40 andre Exp $");
52
53
54int nologtest = 0;
55int compile_opts_only = 0;
56int be_verbose = 0;
57
58
59#define DOTQUAD_MAXSIZE 17
60void dump_dotquad(char *s, struct in_addr *sin4) {
61 unsigned int addr;
62
63 addr = ntohl(sin4->s_addr);
64 snprintf(s, DOTQUAD_MAXSIZE, "%d.%d.%d.%d",
65 (addr >> 24)& 0xff, (addr >>16) & 0xff,
66 (addr >>8) & 0xff, addr & 0xff );
67} /* dump_dotquad */
68
69
70/* Dump a logininfo to stdout. Assumes a tab size of 8 chars. */
71void dump_logininfo(struct logininfo *li, char *descname) {
72 char a4[DOTQUAD_MAXSIZE];
73
74 dump_dotquad(a4, &(li->hostaddr.sa_in4.sin_addr));
75
76 /* yes I know how nasty this is */
77 printf("struct logininfo %s = {\n\t"
78 "progname\t'%s'\n\ttype\t\t%d\n\t"
79 "pid\t\t%d\n\tuid\t\t%d\n\t"
80 "line\t\t'%s'\n\tusername\t'%s'\n\t"
81 "hostname\t'%s'\n\texit\t\t%d\n\ttermination\t%d\n\t"
82 "tv_sec\t%d\n\ttv_usec\t%d\n\t"
83 "struct login_netinfo hostaddr {\n\t\t"
84 "struct sockaddr_in sa_in4 {\n"
85 "\t\t\tsin_port\t%d\n\t\t\t*sin_addr\t%d(%s)\n\t\t}\n"
86 "\t\t** !!! IP6 stuff not supported yet **\n"
87 "\t}\n"
88 "}\n",
89 descname, li->progname, li->type,
90 li->pid, li->uid, li->line,
91 li->username, li->hostname, li->exit,
92 li->termination, li->tv_sec, li->tv_usec,
93 ntohs(li->hostaddr.sa_in4.sin_port),
94 ntohl(li->hostaddr.sa_in4.sin_addr.s_addr), a4);
95 /* FIXME: (ATL) print sockaddr_in6 stuff */
96}
97
98
99int testAPI() {
100 struct logininfo *li1;
101 struct passwd *pw;
102 struct hostent *he;
103 struct sockaddr_in sa_in4;
104 char cmdstring[256], stripline[8];
105 char username[32];
106#ifdef HAVE_TIME_H
107 time_t t0, t1, t2;
108 char s_t0[64],s_t1[64],s_t2[64]; /* ctime() strings */
109#endif
110
111 printf("**\n** Testing the API...\n**\n");
112
113 pw = getpwuid(getuid());
114 strlcpy(username, pw->pw_name, sizeof(username));
115
116 /* gethostname(hostname, sizeof(hostname)); */
117
118 printf("login_alloc_entry test (no host info):\n");
119 /* !!! fake tty more effectively */
120 li1 = login_alloc_entry((int)getpid(), username, NULL, ttyname(0));
121 login_set_progname(li1, "testlogin");
122
123 if (be_verbose)
124 dump_logininfo(li1, "li1");
125
126 printf("Setting IPv4 host info for 'localhost' (may call out):\n");
127 if (! (he = gethostbyname("localhost"))) {
128 printf("Couldn't set hostname(lookup failed)\n");
129 } else {
130 /* NOTE: this is messy, but typically a program wouldn't have to set
131 * any of this, a sockaddr_in* would be already prepared */
132 memcpy((void *)&(sa_in4.sin_addr), (void *)&(he->h_addr_list[0][0]),
133 sizeof(struct in_addr));
134 login_set_ip4(li1, &sa_in4);
135 login_set_hostname(li1, "localhost");
136 }
137 if (be_verbose)
138 dump_logininfo(li1, "li1");
139
140 if ((int)geteuid() != 0) {
141 printf("NOT RUNNING LOGIN TESTS - you are not root!\n");
142 return 1; /* this isn't necessarily an error */
143 }
144
145 if (nologtest)
146 return 1;
147
148 line_stripname(stripline, li1->line, sizeof(stripline));
149
150 printf("Performing an invalid login attempt (no type field)\n--\n");
151 login_write(li1);
152 printf("--\n(Should have written an error to stderr)\n");
153
154#ifdef HAVE_TIME_H
155 (void)time(&t0);
156 strlcpy(s_t0, ctime(&t0), sizeof(s_t0));
157 t1 = login_getlasttime_uid(getuid());
158 strlcpy(s_t1, ctime(&t1), sizeof(s_t1));
159 printf("Before logging in:\n\tcurrent time is %d - %s\t"
160 "lastlog time is %d - %s\n",
161 (int)t0, s_t0, (int)t1, s_t1);
162#endif
163
164 printf("Performing a login on line %s...\n--\n", stripline);
165 login_login(li1);
166
167 snprintf(cmdstring, sizeof(cmdstring), "who | grep '%s '",
168 stripline);
169 system(cmdstring);
170
171 printf("--\nWaiting for a few seconds...\n");
172 sleep(2);
173
174 printf("Performing a logout (the root login "
175 "shown above should be gone)\n"
176 "If the root login hasn't gone, but another user on the same\n"
177 "pty has, this is OK - we're hacking it here, and there\n"
178 "shouldn't be two users on one pty in reality...\n"
179 "-- ('who' output follows)\n");
180 login_logout(li1);
181
182 system(cmdstring);
183 printf("-- ('who' output ends)\n");
184
185#ifdef HAVE_TIME_H
186 t2 = login_getlasttime_uid(getuid());
187 strlcpy(s_t2, ctime(&t2), sizeof(s_t2));
188 printf("After logging in, lastlog time is %d - %s\n", (int)t2, s_t2);
189 if (t1 == t2)
190 printf("The lastlog times before and after logging in are the "
191 "same.\nThis indicates that lastlog is ** NOT WORKING "
192 "CORRECTLY **\n");
193 else if (t0 != t2)
194 printf("** The login time and the lastlog time differ.\n"
195 "** This indicates that lastlog is either recording the "
196 "wrong time,\n** or retrieving the wrong entry.\n");
197 else
198 printf("lastlog agrees with the login time. This is a good thing.\n");
199
200#endif
201
202 printf("--\nThe output of 'last' shown next should have "
203 "an entry for root \n on %s for the time shown above:\n--\n",
204 stripline);
205 snprintf(cmdstring, sizeof(cmdstring), "last | grep '%s ' | head -3",
206 stripline);
207 system(cmdstring);
208
209 printf("--\nEnd of login test.\n");
210
211 login_free_entry(li1);
212
213 return 1;
214} /* testAPI() */
215
216
217void testLineName(char *line) {
218 /* have to null-terminate - these functions are designed for
219 * structures with fixed-length char arrays, and don't null-term.*/
220 char full[17], strip[9], abbrev[5];
221
222 memset(full, '\0', sizeof(full));
223 memset(strip, '\0', sizeof(strip));
224 memset(abbrev, '\0', sizeof(abbrev));
225
226 line_fullname(full, line, sizeof(full)-1);
227 line_stripname(strip, full, sizeof(strip)-1);
228 line_abbrevname(abbrev, full, sizeof(abbrev)-1);
229 printf("%s: %s, %s, %s\n", line, full, strip, abbrev);
230
231} /* testLineName() */
232
233
234int testOutput() {
235 printf("**\n** Testing linename functions\n**\n");
236 testLineName("/dev/pts/1");
237 testLineName("pts/1");
238 testLineName("pts/999");
239 testLineName("/dev/ttyp00");
240 testLineName("ttyp00");
241
242 return 1;
243} /* testOutput() */
244
245
246/* show which options got compiled in */
247void showOptions(void) {
248
249 printf("**\n** Compile-time options\n**\n");
250
251 printf("login recording methods selected:\n");
252#ifdef USE_LOGIN
253 printf("\tUSE_LOGIN\n");
254#endif
255#ifdef USE_UTMP
256 printf("\tUSE_UTMP (UTMP_FILE=%s)\n", UTMP_FILE);
257#endif
258#ifdef USE_UTMPX
259 printf("\tUSE_UTMPX (UTMPX_FILE=%s)\n", UTMPX_FILE);
260#endif
261#ifdef USE_WTMP
262 printf("\tUSE_WTMP (WTMP_FILE=%s)\n", WTMP_FILE);
263#endif
264#ifdef USE_WTMPX
265 printf("\tUSE_WTMPX (WTMPX_FILE=%s)\n", WTMPX_FILE);
266#endif
267#ifdef USE_LASTLOG
268 printf("\tUSE_LASTLOG (LASTLOG_FILE=%s)\n", LASTLOG_FILE);
269#endif
270 printf("\n");
271
272 printf("IP6 support: %s\n",
273#ifdef HAVE_IP6
274 "enabled"
275#else
276 "disabled"
277#endif
278 );
279
280
281} /* showOptions() */
282
283
284int main(int argc, char *argv[]) {
285
286 printf("Platform-independent login recording test driver");
287
288 if (argc == 2) {
289 if (strncmp(argv[1], "-i", 3) == 0)
290 compile_opts_only = 1;
291 else if (strncmp(argv[1], "-v", 3) == 0)
292 be_verbose=1;
293 }
294
295 if (!compile_opts_only) {
296 if (be_verbose && !testOutput())
297 return 1;
298
299 if (!testAPI())
300 return 1;
301 }
302
303 showOptions();
304
305 return 0;
306} /* main() */
307
diff --git a/openbsd-compat.h b/openbsd-compat.h
index eadcf7940..bef9c852d 100644
--- a/openbsd-compat.h
+++ b/openbsd-compat.h
@@ -12,7 +12,6 @@
12#include "bsd-mktemp.h" 12#include "bsd-mktemp.h"
13#include "bsd-snprintf.h" 13#include "bsd-snprintf.h"
14#include "bsd-daemon.h" 14#include "bsd-daemon.h"
15#include "bsd-login.h"
16#include "bsd-base64.h" 15#include "bsd-base64.h"
17 16
18/* rfc2553 socket API replacements */ 17/* rfc2553 socket API replacements */