Commit 20ef042e authored by Leigh Stoller's avatar Leigh Stoller

Add lastweblogin and lastuserslogin support, as per Jay's request.

I added a DB table to record last Web login, and I've added a backed
program (lastlogin.c) to get the lastlogin information from
users:/var/log/lastlog (mounted on boss:/usr/testbed/usersvar). These
two bits of info are now displayed in the user infomation page.
parent 69c387bc
......@@ -8,7 +8,7 @@ SUBDIR = security
include $(OBJDIR)/Makeconf
BINS = suexec
BINS = suexec lastlogin
SCRIPTS = paperbag plasticwrap
#
......@@ -19,15 +19,19 @@ all: $(BINS) $(SCRIPTS)
include $(TESTBED_SRCDIR)/GNUmakerules
CFLAGS = -O -g \
-DLOG_EXEC='"$(prefix)/log/suexec.log"' \
-DDOC_ROOT='"$(prefix)/libexec/"' \
-DSAFE_PATH='"$(prefix)/libexec:/usr/local/bin:/usr/bin:/bin"'
CFLAGS = -O -g
suexec: suexec.o
$(CC) $(CFLAGS) -o suexec suexec.o
suexec: suexec.c suexec.h
$(CC) $(CFLAGS) \
-DLOG_EXEC='"$(prefix)/log/suexec.log"' \
-DDOC_ROOT='"$(prefix)/libexec/"' \
-DSAFE_PATH='"$(prefix)/libexec:/usr/local/bin:/usr/bin:/bin"'\
-o suexec $<
suexec.o: suexec.c suexec.h
lastlogin: lastlogin.c
$(CC) $(CFLAGS) \
-DUSERSVAR='"$(prefix)/usersvar"' \
-o lastlogin $<
install: $(addprefix $(INSTALL_LIBEXECDIR)/, $(BINS)) \
$(addprefix $(INSTALL_SBINDIR)/, paperbag)
......
/*
* A little ditty to pull the last log info out and report a list of
* the last time each person has logged in.
*/
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <utmp.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <setjmp.h>
#ifndef USERSVAR
#define USERSVAR "/usr/testbed/usersvar"
#endif
static char *progname;
static int doentry(FILE *fp, uid_t uid, int umode);
static jmp_buf deadline;
static int deadfl;
void
usage(void)
{
fprintf(stderr, "Usage: %s [-u user]\n", progname);
exit(-1);
}
static void
dead()
{
deadfl = 1;
longjmp(deadline, 1);
}
int
main(int argc, char **argv)
{
FILE *fp;
char buf[BUFSIZ], *uidarg;
uid_t uid;
int ch, rval, umode = 0;
/*
* Web server sillyness. Must be some kind of FD mistake cause
* stderr is lost if we don't do this.
*/
dup2(1, 2);
progname = argv[0];
while ((ch = getopt(argc, argv, "u:")) != -1)
switch(ch) {
case 'u':
umode = 1;
uidarg = optarg;
break;
case 'h':
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (argc)
usage();
/*
* Allow either numeric or alpha arg.
*/
if (umode) {
if (isdigit(uidarg[0]))
uid = atoi(uidarg);
else {
struct passwd *pw = getpwnam(uidarg);
if (pw == NULL) {
fprintf(stderr, "Bad user %s!\n", uidarg);
exit(-1);
}
uid = pw->pw_uid;
}
}
sprintf(buf, "%s/log/lastlog", USERSVAR);
/*
* Protect against NFS timeout.
*/
alarm(3);
fp = fopen(buf, "r");
alarm(0);
if (deadfl) {
fprintf(stderr, "Timed out opening %s\n", buf);
exit(-1);
}
if (fp == NULL) {
warn("Opening %s", buf);
exit(-1);
}
if (umode) {
long seekoff = (long) (uid * sizeof(struct lastlog));
fseek(fp, 0L, SEEK_END);
if (seekoff > ftell(fp)) {
fprintf(stderr, "Error Locating uid %d.\n", uid);
exit(-1);
}
rval = fseek(fp, (long) (uid * sizeof(struct lastlog)),
SEEK_SET);
if (rval) {
warn("Seeking to %ld", seekoff);
exit(-1);
}
rval = doentry(fp, uid, 1);
}
else {
for (uid = 0; ; uid++) {
if (rval = doentry(fp, uid, 0))
break;
}
}
fclose(fp);
exit(rval);
}
static int
doentry(FILE *fp, uid_t uid, int umode)
{
struct lastlog ll;
char buf[BUFSIZ];
if (fread(&ll, sizeof(ll), 1, fp) != 1) {
if (ferror(fp)) {
fprintf(stderr, "Error reading entry %u\n", uid);
return -1;
}
return 1;
}
if (ll.ll_time) {
strftime(buf, sizeof(buf),
"20%y-%m-%d %H:%M:%S", localtime(&ll.ll_time));
if (umode)
printf("%s\n", buf);
else
printf("%u %s\n", uid, buf);
}
return 0;
}
......@@ -74,7 +74,7 @@ function TBERROR ($message, $death) {
"Errors-To: $TBMAIL_WWW");
# Allow sendmail to run.
sleep(2);
sleep(1);
if ($death) {
echo "<h3><br><br>
......@@ -190,6 +190,45 @@ function VERIFYURL($url) {
return 0;
}
#
# Get the last USERS node login for a user (or all users). If this fails,
# the let testbed ops know, but its not a fatal problem.
#
function LASTUSERSLOGIN($uid) {
global $TBLIBEXEC_DIR;
$output = array();
$retval = 0;
#
# Either a specific UID or a list of all UIDs.
#
$uidarg = "";
if ($uid) {
$uidarg = "-u $uid";
}
$cmdandargs = "$TBLIBEXEC_DIR/lastlogin $uidarg";
$result = exec($cmdandargs, $output, $retval);
if ($retval) {
if (0) {
$foo = "";
for ($i = 0; $i < count($output); $i++) {
$foo = "$foo $output[$i]";
}
TBERROR("LASTUSERSLOGIN: ".
"Cmd was \"$cmdandargs\". \n".
"Error output:\n\n $foo\n", 0);
}
return 0;
}
if ($uid) {
return $output[0];
}
return $output;
}
#
# Beware empty spaces (cookies)!
......
......@@ -159,6 +159,14 @@ function SHOWUSER($uid) {
$usr_title = $row[usr_title];
$usr_affil = $row[usr_affil];
$status = $row[status];
#
# Last Login info.
#
if (($lastweblogin = LASTWEBLOGIN($uid)) == 0)
$lastweblogin = "&nbsp";
if (($lastuserslogin = LASTUSERSLOGIN($uid)) == 0)
$lastuserslogin = "&nbsp";
echo "<table align=center border=1>\n";
......@@ -212,6 +220,16 @@ function SHOWUSER($uid) {
<td>$status</td>
</tr>\n";
echo "<tr>
<td>Last Web Login:</td>
<td>$lastweblogin</td>
</tr>\n";
echo "<tr>
<td>Last Users Login:</td>
<td>$lastuserslogin</td>
</tr>\n";
echo "</table>\n";
}
......@@ -509,6 +527,9 @@ function SHOWIMAGEID($imageid, $edit) {
$part1_osid = $row[part1_osid];
$part2_osid = $row[part2_osid];
$part3_osid = $row[part3_osid];
$part4_osid = $row[part4_osid];
$default_osid= $row[default_osid];
$path = $row[path];
......
......@@ -209,6 +209,12 @@ function DOLOGIN($uid, $password) {
TBERROR("Database Error logging in $uid: $err\n", 1);
}
#
# Create a last login record.
#
$query_result = mysql_db_query($TBDBNAME,
"REPLACE into lastlogin (uid, time) VALUES ('$uid', NOW())");
#
# Issue the cookie requests so that subsequent pages come back
# with the hash value and auth usr embedded.
......@@ -299,6 +305,19 @@ function NOLOGINS() {
return $nologins;
}
function LASTWEBLOGIN($uid) {
global $TBDBNAME;
$query_result = mysql_db_query($TBDBNAME,
"SELECT time from lastlogin where uid=\"$uid\"");
if (mysql_num_rows($query_result)) {
$lastrow = mysql_fetch_array($query_result);
return $lastrow[time];
}
return 0;
}
#
# Beware empty spaces (cookies)!
#
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment