X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=contrib%2Fmacusers%2Fmacusers.in;h=75ddb5c4366e41eb78b2e0d432a9209ae5a06470;hb=5116ed2346ea7ee6e5a9858dee94f3eacdc00d7e;hp=5ef0eb22c0e419a95fdf98d3a003142d6cd91bb5;hpb=fbf2ec27deeb6001adf2fc59f9a1ed2556631e1f;p=netatalk.git diff --git a/contrib/macusers/macusers.in b/contrib/macusers/macusers.in index 5ef0eb22..75ddb5c4 100644 --- a/contrib/macusers/macusers.in +++ b/contrib/macusers/macusers.in @@ -5,25 +5,20 @@ use Socket; use vars qw($MAC_PROCESS $PS_STR $MATCH_STR $ASIP_PORT_NO $ASIP_PORT $LSOF); # Written for linux; may have to be modified for your brand of Unix. - # Support for FreeBSD added by Joe Clarke . -# Support could probably be extended for *BSD, but I do not have Net or -# OpenBSD machines to test with. Code has also been cleaned up and made -# to compile under strict. -# -# The new lsof call should also be quicker as it does not involve a -# second pipeline. -# +# Support Solaris added by Frank Lahm . # Support has also been added for 16 character usernames. $MAC_PROCESS = "afpd"; -if ( $^O eq "freebsd" ) { - $PS_STR = "-awwxouser,pid,ppid,start,command"; - $MATCH_STR = '(\w+)\s+(\d+)\s+(\d+)\s+([\d\w:]+)'; -} -else { - $PS_STR = "-ef"; - $MATCH_STR = '\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)'; +if ($^O eq "freebsd" || $^O eq "openbsd") { + $PS_STR = "-awwxouser,pid,ppid,start,command"; + $MATCH_STR = '(\w+)\s+(\d+)\s+(\d+)\s+([\d\w:]+)'; +} elsif ($^O eq "solaris") { + $PS_STR = "-eo user,pid,ppid,c,stime,tty,time,comm"; + $MATCH_STR = '\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)'; +} else { + $PS_STR = "-eo user:32,pid,ppid,c,stime,tty,time,cmd"; + $MATCH_STR = '\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)'; } $ASIP_PORT = "afpovertcp"; $ASIP_PORT_NO = 548; @@ -32,59 +27,86 @@ $ASIP_PORT_NO = 548; $LSOF = 1; my %mac = (); -if ( $^O eq "freebsd" ) { - open( SOCKSTAT, "sockstat -4 | grep $MAC_PROCESS | grep -v grep |" ); +if ($^O eq "freebsd") { + open(SOCKSTAT, "sockstat -4 | grep $MAC_PROCESS | grep -v grep |"); - while () { - next if ( $_ !~ /$MAC_PROCESS/ ); - $_ =~ /\S+\s+\S+\s+(\d+)\s+\d+\s+[\w\d]+\s+[\d\.:]+\s+([\d\.]+)/; - my ( $pid, $addr, $host ); - $pid = $1; - $addr = $2; - $host = gethostbyaddr( pack( 'C4', split ( /\./, $addr ) ), AF_INET ); - ($host) = ( $host =~ /(^[\w\d\-]+)/ ); - $mac{$pid} = $host; - } - print - "PID UID Username Name Logintime Mac\n"; - close(SOCKSTAT); -} -elsif ( $LSOF == 1 ) { - open( LSOF, "lsof -i :$ASIP_PORT |" ); + while () { + next if ($_ !~ /$MAC_PROCESS/); + $_ =~ + /\S+\s+\S+\s+(\d+)\s+\d+\s+[\w\d]+\s+[\d\.:]+\s+([\d\.]+)/; + my ($pid, $addr, $host); + $pid = $1; + $addr = $2; + $host = gethostbyaddr(pack('C4', split (/\./, $addr)), AF_INET); + ($host) = ( $host =~ /(^(\d+\.){3}\d+|[\w\d\-]+)/ ); + $mac{$pid} = $host; + } + print + "PID UID Username Name Logintime Mac\n"; + close(SOCKSTAT); +} elsif ($^O eq "solaris") { + if ($< != 0) { + print "must be run as root\n"; + exit(1); + } + print "PID UID Username Name Logintime Mac\n"; +} elsif ($LSOF == 1) { + open(LSOF, "lsof -i :$ASIP_PORT |"); - while () { - next if ( $_ !~ /$ASIP_PORT/ ); - $_ =~ /\w+\s+(\d+).*->([\w\.-]+).*/; - my ( $pid, $host ); - $pid = $1; - $host = $2; - ($host) = ( $host =~ /(^[\w\d\-]+)/ ); - $mac{$pid} = $host; - } - print - "PID UID Username Name Logintime Mac\n"; - close(LSOF); -} -else { - print "PID UID Username Name Logintime\n"; + while () { + next if ($_ !~ /$ASIP_PORT/); + $_ =~ /\w+\s+(\d+).*->([\w\.-]+).*/; + my ($pid, $host); + $pid = $1; + $host = $2; + ($host) = ( $host =~ /(^(\d+\.){3}\d+|[\w\d\-]+)/ ); + $mac{$pid} = $host; + } + print + "PID UID Username Name Logintime Mac\n"; + close(LSOF); +} else { + print + "PID UID Username Name Logintime\n"; } -open( PS, "ps $PS_STR |" ) || die "Unable to open a pipe to ``ps''"; +open(PS, "ps $PS_STR |") || die "Unable to open a pipe to ``ps''"; while () { - next if ( $_ !~ /$MAC_PROCESS/ ); - my ( $user, $pid, $ppid, $time, $name, $uid, $t ); - $_ =~ /$MATCH_STR/; - $user = $1; - $pid = $2; - $ppid = $3; - $time = $4; + next if ($_ !~ /$MAC_PROCESS/); + my ($user, $pid, $ppid, $time, $name, $uid, $t, $ip); + $_ =~ /$MATCH_STR/; + $user = $1; + $pid = $2; + $ppid = $3; + $time = $4; + + if ($ppid != 1) { + if ($^O eq "solaris") { + open(PFILES, "pfiles $pid |"); + while () { + next if ($_ !~ /port: $ASIP_PORT_NO/); + while () { + next if ($_ !~ /peername/); + if ($_ =~ /AF_INET (.*) port/) { + $ip = $1; + if ($ip =~ /::ffff:(.*)/ ) { + $ip = $1; + } + } + $mac{$pid} = $ip; + last; + } + last; + } + close(PFILES); + } - if ( $ppid != 1 ) { - ( $t, $t, $uid, $t, $t, $t, $name, $t, $t ) = getpwnam($user); - printf "%-8d %-8d %-16s %-20s %-9s %s\n", $pid, $uid, $user, $name, - $time, $mac{$pid}; - } + ($t, $t, $uid, $t, $t, $t, $name, $t, $t) = getpwnam($user); + ($name) = ( $name =~ /(^[^,]+)/ ); + printf "%-8d %-8d %-16s %-20s %-9s %s\n", $pid, $uid, $user, + $name, $time, $mac{$pid}; + } } close(PS);