display | more...
Similar to who, w can be used to gain the host/ip address of other online users. Not only that, but w also tells you what another user is doing. Because I administrate a server that has many paying customers who would rather keep their privacy, I thought it might be a good idea to modify w to accompany my who modifications to further ensure a user's privacy.

The purpose of the w modification is twofold. Firstly, to hide the host and activity information from users, and secondly, to make sure that server admins can still see this information very easily. To that end I modified w to incorporate both of these goals with ease.


You'll need a couple more variables for the changes to work. Insert these at the top of main with the other variable declarations.

    gid_t gidset[5];
    int groups;
    int i;
    int access=0;

This code can be inserted anywhere early on in the code outside the main loops. It is where it is determined whether the user is in group wheel (can su to root), and therefor eligible to see full information.
    groups = getgroups(5, gidset);
    for (i=0; i<=groups-1; i++)
        if (gidset[i]==0) access=1;
    if (getegid()==0) access=1;

In the next code chunk we're simply disabling the -d option for non-wheel people. The only modification is the if (acess==1) but I've followed it up with the next few lines so you know where it goes.
    if (access==1)
        if (dflag) {
            for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {

The final two modifications are to differentiate the printout for wheel and non-wheel people. The first one is to hide the hostname.
    if (access==1) (void)printf("%-*.*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,
        UT_HOSTSIZE, UT_HOSTSIZE, *p ? p : "-");
    else (void)printf("%-*.*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,
        UT_HOSTSIZE, UT_HOSTSIZE, "-");	

And finally, the modification so that normal users see a - instead of the actual current activity of a user.
    if (access==1)
        (void)printf("%.*s\n", argwidth - longidle, ep->args);
    else (void)printf("%.*s\n", argwidth - longidle, "-");

Log in or register to write something here or to contact authors.