Created
March 8, 2011 21:34
-
-
Save bdrewery/861126 to your computer and use it in GitHub Desktop.
Patch to FreeBSD last to show GeoIP Country and restrict per user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Summary: | |
Makefile | 2 ++ | |
last.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- | |
2 files changed, 46 insertions(+), 2 deletions(-) | |
---- | |
Files modified: | |
commit f400c8ea0d5417aea5a80c53c06511f595d84ab8 | |
Author: Bryan Drewery <[email protected]> | |
Date: Tue Mar 8 15:24:26 2011 -0600 | |
* Restrict per user | |
last.c | 15 ++++++++++++++- | |
1 files changed, 14 insertions(+), 1 deletions(-) | |
commit 91bb70b703052306204ed64223c82593eb6ba6df | |
Author: Bryan Drewery <[email protected]> | |
Date: Tue Mar 8 15:12:25 2011 -0600 | |
* Add GeoIP support | |
last.c | 31 ++++++++++++++++++++++++++++++- | |
1 files changed, 30 insertions(+), 1 deletions(-) | |
commit 0e7bb42a2bd4c76589520ec694f277ba4a3c0c94 | |
Author: Bryan Drewery <[email protected]> | |
Date: Tue Mar 8 14:07:19 2011 -0600 | |
* Link in GeoIP when building | |
Makefile | 2 ++ | |
1 files changed, 2 insertions(+), 0 deletions(-) | |
---- | |
diff --git ./usr.bin/last.orig/Makefile ./usr.bin/last/Makefile | |
index 613f5b6..f9579aa 100644 | |
--- ./usr.bin/last.orig/Makefile | |
+++ ./usr.bin/last/Makefile | |
@@ -2,5 +2,7 @@ | |
# $FreeBSD: src/usr.bin/last/Makefile,v 1.3.40.1 2010/12/21 17:10:29 kensmith Exp $ | |
PROG= last | |
+LDADD= /usr/local/lib/libGeoIP.so | |
+CFLAGS= -I/usr/local/include | |
.include <bsd.prog.mk> | |
diff --git ./usr.bin/last.orig/last.c ./usr.bin/last/last.c | |
index 2f6d396..3a1eab8 100644 | |
--- ./usr.bin/last.orig/last.c | |
+++ ./usr.bin/last/last.c | |
@@ -62,6 +62,10 @@ __FBSDID("$FreeBSD: src/usr.bin/last/last.c,v 1.34.34.1 2010/12/21 17:10:29 kens | |
#include <utmp.h> | |
#include <sys/queue.h> | |
+#include <sys/types.h> | |
+#include <pwd.h> | |
+#include <GeoIP.h> | |
+ | |
#define NO 0 /* false/no */ | |
#define YES 1 /* true/yes */ | |
#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; | |
@@ -120,6 +124,9 @@ usage(void) | |
exit(1); | |
} | |
+#define W_DISPGEOSIZE 20 | |
+const char* geoiplookup(const char*); | |
+ | |
int | |
main(int argc, char *argv[]) | |
{ | |
@@ -213,6 +220,13 @@ wtmp(void) | |
char ct[80]; | |
struct tm *tm; | |
time_t t; | |
+ struct passwd *pw; | |
+ int restricted = 1; | |
+ | |
+ if (geteuid() == 0) | |
+ restricted = 0; | |
+ | |
+ pw = getpwuid(getuid()); | |
LIST_INIT(&ttylist); | |
@@ -229,8 +243,12 @@ wtmp(void) | |
if (lseek(wfd, (off_t)(bl * sizeof(buf)), L_SET) == -1 || | |
(bytes = read(wfd, buf, sizeof(buf))) == -1) | |
err(1, "%s", file); | |
- for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) | |
+ for (bp = &buf[bytes / sizeof(buf[0]) - 1]; bp >= buf; --bp) { | |
+ if (restricted && strncmp(bp->ut_name, pw->pw_name, UT_NAMESIZE)) | |
+ continue; | |
+ | |
doentry(bp); | |
+ } | |
} | |
t = _int_to_time(buf[0].ut_time); | |
tm = localtime(&t); | |
@@ -336,6 +354,8 @@ printentry(struct utmp *bp, struct ttytab *tt) | |
struct tm *tm; | |
time_t delta; /* time difference */ | |
time_t t; | |
+ char host_buf[UT_HOSTSIZE + 1]; | |
+ const char *country_name = NULL; | |
if (maxrec != -1 && !maxrec--) | |
exit(0); | |
@@ -344,10 +364,14 @@ printentry(struct utmp *bp, struct ttytab *tt) | |
(void) strftime(ct, sizeof(ct), d_first ? | |
(yflag ? "%a %e %b %Y %R" : "%a %e %b %R") : | |
(yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm); | |
- printf("%-*.*s %-*.*s %-*.*s %s%c", | |
+ host_buf[UT_HOSTSIZE] = '\0'; | |
+ strncpy(host_buf, bp->ut_host, UT_HOSTSIZE); | |
+ country_name = geoiplookup(host_buf); | |
+ printf("%-*.*s %-*.*s %-*.*s %-*.*s %s%c", | |
UT_NAMESIZE, UT_NAMESIZE, bp->ut_name, | |
UT_LINESIZE, UT_LINESIZE, bp->ut_line, | |
UT_HOSTSIZE, UT_HOSTSIZE, bp->ut_host, | |
+ W_DISPGEOSIZE, W_DISPGEOSIZE, country_name ? country_name : "-", | |
ct, tt == NULL ? '\n' : ' '); | |
if (tt == NULL) | |
return; | |
@@ -553,6 +577,24 @@ terr: errx(1, | |
return timet; | |
} | |
+const char* geoiplookup(const char *name) { | |
+ const char *country_name = NULL; | |
+ | |
+ int gip_type = strchr(name, ':') ? GEOIP_COUNTRY_EDITION_V6 : GEOIP_COUNTRY_EDITION; | |
+ if (GeoIP_db_avail(gip_type)) { | |
+ GeoIP *gip = GeoIP_open_type(gip_type, GEOIP_STANDARD); | |
+ | |
+ if (gip) { | |
+ int country_id = gip_type == GEOIP_COUNTRY_EDITION ? GeoIP_id_by_name(gip, name) : GeoIP_id_by_name_v6(gip, name); | |
+ if (country_id > 0) | |
+ country_name = GeoIP_country_name[country_id]; | |
+ GeoIP_delete(gip); | |
+ } | |
+ } | |
+ | |
+ return country_name; | |
+} | |
+ | |
/* | |
* onintr -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pw groupadd -n utmp
chmod 0660 /var/run/utmp
chown root:utmp /var/run/utmp
chmod 2111 /usr/bin/last
chown root:utmp /usr/bin/last