Skip to content

Instantly share code, notes, and snippets.

@bdrewery
Created March 8, 2011 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdrewery/860707 to your computer and use it in GitHub Desktop.
Save bdrewery/860707 to your computer and use it in GitHub Desktop.
FreeBSD w patch for GeoIP (Country) and restricted per user
Summary:
Makefile | 3 ++-
w.c | 48 ++++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 46 insertions(+), 5 deletions(-)
----
Files modified:
commit dbbd24c73757c764d38ca8cb4cefcb0b08e40b16
Author: Bryan Drewery <bryan@shatow.net>
Date: Tue Mar 8 12:01:09 2011 -0600
* Restrict w output by user
w.c | 15 ++++++++++++++-
1 files changed, 14 insertions(+), 1 deletions(-)
commit d01d88dfa68a2178eb1a49a9583af3dca798c8c3
Author: Bryan Drewery <bryan@shatow.net>
Date: Tue Mar 8 11:48:52 2011 -0600
* Display GeoIP location in its own column
w.c | 14 +++++++-------
1 files changed, 7 insertions(+), 7 deletions(-)
commit e08d529597650eea593e8b885d3da04728c25b6a
Author: Bryan Drewery <bryan@shatow.net>
Date: Tue Mar 8 11:33:07 2011 -0600
* DRY the GeoIP lookup the support IPv6
w.c | 35 +++++++++++++++++++++++------------
1 files changed, 23 insertions(+), 12 deletions(-)
commit 8bef407622864e299fe6d1c3aed8945ba4966363
Author: Estella <Estella@Mystagic.com>
Date: Mon Mar 7 22:04:01 2011 -0600
* Add GeoIP patch
Makefile | 3 ++-
w.c | 16 ++++++++++++++++
2 files changed, 18 insertions(+), 1 deletions(-)
----
diff --git ./usr.bin/w.orig/Makefile ./usr.bin/w/Makefile
index 555e96a..4793bff 100644
--- ./usr.bin/w.orig/Makefile
+++ ./usr.bin/w/Makefile
@@ -5,10 +5,11 @@ PROG= w
SRCS= fmt.c pr_time.c proc_compare.c w.c
MAN= w.1 uptime.1
DPADD= ${LIBKVM} ${LIBUTIL}
-LDADD= -lkvm -lutil
+LDADD= -lkvm -lutil /usr/local/lib/libGeoIP.so
#BINGRP= kmem
#BINMODE=2555
LINKS= ${BINDIR}/w ${BINDIR}/uptime
+CFLAGS= -I/usr/local/include
.PATH: ${.CURDIR}/../../bin/ps
diff --git ./usr.bin/w.orig/w.c ./usr.bin/w/w.c
index 37bbc1f..387b4c4 100644
--- ./usr.bin/w.orig/w.c
+++ ./usr.bin/w/w.c
@@ -87,6 +87,10 @@ static const char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
#include <utmp.h>
#include <vis.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <GeoIP.h>
+
#include "extern.h"
struct timeval boottime;
@@ -121,6 +125,7 @@ struct entry {
/* W_DISPHOSTSIZE should not be greater than UT_HOSTSIZE */
#define W_DISPHOSTSIZE 16
+#define W_DISPGEOSIZE 20
static void pr_header(time_t *, int);
static struct stat *ttystat(char *, int);
@@ -128,6 +133,7 @@ static void usage(int);
static int this_is_uptime(const char *s);
char *fmt_argv(char **, char *, int); /* ../../bin/ps/fmt.c */
+const char* geoiplookup(const char*);
int
main(int argc, char *argv[])
@@ -143,6 +149,9 @@ main(int argc, char *argv[])
char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
char fn[MAXHOSTNAMELEN];
char *dot;
+ const char *country_name = NULL;
+ int restricted = 1;
+ struct passwd *pw = NULL;
(void)setlocale(LC_ALL, "");
use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
@@ -205,6 +214,11 @@ main(int argc, char *argv[])
if (dropgid)
setgid(getgid());
+ if (geteuid() == 0)
+ restricted = 0;
+
+ pw = getpwuid(getuid());
+
if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf)) == NULL)
errx(1, "%s", errbuf);
@@ -212,7 +226,7 @@ main(int argc, char *argv[])
if ((ut = fopen(_PATH_UTMP, "r")) == NULL)
err(1, "%s", _PATH_UTMP);
- if (*argv)
+ if (*argv && !restricted)
sel_users = argv;
for (nusers = 0; fread(&utmp, sizeof(utmp), 1, ut);) {
@@ -223,6 +237,10 @@ main(int argc, char *argv[])
++nusers;
if (wcmd == 0)
continue;
+
+ if (restricted && strncmp(utmp.ut_name, pw->pw_name, UT_NAMESIZE))
+ continue;
+
if (sel_users) {
int usermatch;
char **user;
@@ -270,14 +288,16 @@ main(int argc, char *argv[])
#define HEADER_USER "USER"
#define HEADER_TTY "TTY"
#define HEADER_FROM "FROM"
+#define HEADER_GEO "GEO"
#define HEADER_LOGIN_IDLE "LOGIN@ IDLE "
#define HEADER_WHAT "WHAT\n"
#define WUSED (UT_NAMESIZE + UT_LINESIZE + W_DISPHOSTSIZE + \
sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */
- (void)printf("%-*.*s %-*.*s %-*.*s %s",
+ (void)printf("%-*.*s %-*.*s %-*.*s %-*.*s %s",
UT_NAMESIZE, UT_NAMESIZE, HEADER_USER,
UT_LINESIZE, UT_LINESIZE, HEADER_TTY,
W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM,
+ W_DISPGEOSIZE, W_DISPGEOSIZE, HEADER_GEO,
HEADER_LOGIN_IDLE HEADER_WHAT);
}
@@ -399,13 +419,17 @@ main(int argc, char *argv[])
dkp->ki_pid, ptr);
}
}
- (void)printf("%-*.*s %-*.*s %-*.*s ",
+
+ country_name = geoiplookup(host_buf);
+
+ (void)printf("%-*.*s %-*.*s %-*.*s %-*.*s ",
UT_NAMESIZE, UT_NAMESIZE, ep->utmp.ut_name,
UT_LINESIZE, UT_LINESIZE,
strncmp(ep->utmp.ut_line, "tty", 3) &&
strncmp(ep->utmp.ut_line, "cua", 3) ?
ep->utmp.ut_line : ep->utmp.ut_line + 3,
- W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
+ W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-",
+ W_DISPGEOSIZE, W_DISPGEOSIZE, country_name ? country_name : "-");
t = _time_to_time32(ep->utmp.ut_time);
longattime = pr_attime(&t, &now);
longidle = pr_idle(ep->idle);
@@ -500,6 +524,22 @@ usage(int wcmd)
exit(1);
}
+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);
+ country_name = (country_id > 0) ? GeoIP_country_name[country_id] : NULL;
+ GeoIP_delete(gip);
+ }
+ }
+
+ return country_name;
+}
+
static int
this_is_uptime(const char *s)
{
@bdrewery
Copy link
Author

bdrewery commented Mar 8, 2011

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment