• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/ap/gpl/timemachine/netatalk-2.2.0/contrib/macusers/
1#!/usr/bin/perl
2
3use strict;
4use Socket;
5use vars qw($MAC_PROCESS $PS_STR $MATCH_STR $ASIP_PORT_NO $ASIP_PORT $LSOF);
6
7# Written for linux; may have to be modified for your brand of Unix.
8# Support for FreeBSD added by Joe Clarke <marcus@marcuscom.com>.
9# Support Solaris added by Frank Lahm <franklahm@googlemail.com>.
10# Support has also been added for 16 character usernames.
11
12$MAC_PROCESS = "afpd";
13if ($^O eq "freebsd" || $^O eq "openbsd") {
14        $PS_STR    = "-awwxouser,pid,ppid,start,command";
15        $MATCH_STR = '(\w+)\s+(\d+)\s+(\d+)\s+([\d\w:]+)';
16} elsif ($^O eq "solaris") {
17        $PS_STR    = "-eo user,pid,ppid,c,stime,tty,time,comm";
18        $MATCH_STR = '\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)';
19} else {
20        $PS_STR    = "-eo user:32,pid,ppid,c,stime,tty,time,cmd";
21        $MATCH_STR = '\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)';
22}
23$ASIP_PORT    = "afpovertcp";
24$ASIP_PORT_NO = 548;
25
26# Change to 0 if you don't have lsof
27$LSOF = 1;
28my %mac = ();
29
30if ($^O eq "freebsd") {
31        open(SOCKSTAT, "sockstat -4 | grep $MAC_PROCESS | grep -v grep |");
32
33        while (<SOCKSTAT>) {
34                next if ($_ !~ /$MAC_PROCESS/);
35                $_ =~
36                    /\S+\s+\S+\s+(\d+)\s+\d+\s+[\w\d]+\s+[\d\.:]+\s+([\d\.]+)/;
37                my ($pid, $addr, $host);
38                $pid  = $1;
39                $addr = $2;
40                $host = gethostbyaddr(pack('C4', split (/\./, $addr)), AF_INET);
41                ($host) = ( $host =~ /(^(\d+\.){3}\d+|[\w\d\-]+)/ );
42                $mac{$pid} = $host;
43        }
44        print
45            "PID      UID      Username         Name                 Logintime Mac\n";
46        close(SOCKSTAT);
47} elsif ($^O eq "solaris") {
48        if ($< != 0) {
49            print "must be run as root\n";
50            exit(1);
51        }
52        print "PID      UID      Username         Name                 Logintime Mac\n";
53} elsif ($LSOF == 1) {
54        open(LSOF, "lsof -i :$ASIP_PORT |");
55
56        while (<LSOF>) {
57                next if ($_ !~ /$ASIP_PORT/);
58                $_ =~ /\w+\s+(\d+).*->([\w\.-]+).*/;
59                my ($pid, $host);
60                $pid  = $1;
61                $host = $2;
62                ($host) = ( $host =~ /(^(\d+\.){3}\d+|[\w\d\-]+)/ );
63                $mac{$pid} = $host;
64        }
65        print
66            "PID      UID      Username         Name                 Logintime Mac\n";
67        close(LSOF);
68} else {
69        print
70            "PID      UID      Username         Name                 Logintime\n";
71}
72
73open(PS, "ps $PS_STR |") || die "Unable to open a pipe to ``ps''";
74
75while (<PS>) {
76        next if ($_ !~ /$MAC_PROCESS/);
77        my ($user, $pid, $ppid, $time, $name, $uid, $t, $ip);
78        $_ =~ /$MATCH_STR/;
79        $user = $1;
80        $pid  = $2;
81        $ppid = $3;
82        $time = $4;
83
84        if ($ppid != 1) {
85                if ($^O eq "solaris") {
86                        open(PFILES, "pfiles $pid |");
87                        while (<PFILES>) {
88                                next if ($_ !~ /port: $ASIP_PORT_NO/);
89                                while (<PFILES>) {
90                                        next if ($_ !~ /peername/);
91                                        if ($_ =~ /AF_INET (.*) port/) {
92                                            $ip = $1;
93                                            if ($ip =~ /::ffff:(.*)/ ) {
94                                                $ip = $1;
95                                            }
96                                        }
97                                        $mac{$pid} = $ip;
98                                        last;
99                                }
100                                last;
101                        }
102                        close(PFILES);
103                }
104
105                ($t, $t, $uid, $t, $t, $t, $name, $t, $t) = getpwnam($user);
106                ($name) = ( $name =~ /(^[^,]+)/ );
107                printf "%-8d %-8d %-16s %-20s %-9s %s\n", $pid, $uid, $user,
108                    $name, $time, $mac{$pid};
109        }
110}
111
112close(PS);
113