]> arthur.barton.de Git - netatalk.git/blob - contrib/macusers/macusers
Add some FreeBSD-specific patches.
[netatalk.git] / contrib / macusers / macusers
1 #!/usr/bin/perl
2
3 use strict;
4 use Socket;
5 use 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
9 # Support for FreeBSD added by Joe Clarke <marcus@marcuscom.com>.
10 # Support could probably be extended for *BSD, but I do not have Net or
11 # OpenBSD machines to test with.  Code has also been cleaned up and made
12 # to compile under strict.
13 #
14 # The new lsof call should also be quicker as it does not involve a 
15 # second pipeline.
16 #
17 # Support has also been added for 16 character usernames.
18
19 $MAC_PROCESS = "afpd";
20 if ( $^O eq "freebsd" ) {
21         $PS_STR    = "-awwxouser,pid,ppid,start,command";
22         $MATCH_STR = '(\w+)\s+(\d+)\s+(\d+)\s+([\d\w:]+)';
23 }
24 else {
25         $PS_STR    = "-ef";
26         $MATCH_STR = '\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)';
27 }
28 $ASIP_PORT    = "afpovertcp";
29 $ASIP_PORT_NO = 548;
30
31 # Change to 0 if you don't have lsof
32 $LSOF = 0;
33 my %mac = ();
34
35 if ( $LSOF == 1 ) {
36         open( LSOF, "lsof -i :$ASIP_PORT |" );
37
38         while (<LSOF>) {
39                 next if ( $_ !~ /$ASIP_PORT/ );
40                 $_ =~ /\w+\s+(\d+).*->([\w\.-]+).*/;
41                 my ( $pid, $host );
42                 $pid = $1;
43                 $host = $2;
44                 ($host) = ( $host =~ /(^[\w\d\-]+)/ );
45                 $mac{$pid} = $host;
46         }
47         print
48 "PID      UID      Username         Name                 Logintime Mac\n";
49         close(LSOF);
50 }
51 elsif ( $^O eq "freebsd" ) {
52         open( SOCKSTAT, "sockstat -4 | grep $MAC_PROCESS | grep -v grep |" );
53
54         while (<SOCKSTAT>) {
55                 next if ( $_ !~ /$MAC_PROCESS/ );
56                 $_ =~
57                   /\S+\s+\S+\s+(\d+)\s+\d+\s+[\w\d]+\s+[\d\.:]+\s+([\d\.]+)/;
58                 my ( $pid, $addr, $host );
59                 $pid  = $1;
60                 $addr = $2;
61                 $host =
62                   gethostbyaddr( pack( 'C4', split ( /\./, $addr ) ), AF_INET );
63                 ($host) = ( $host =~ /(^[\w\d\-]+)/ );
64                 $mac{$pid} = $host;
65         }
66         print
67 "PID      UID      Username         Name                 Logintime Mac\n";
68 }
69 else {
70         print
71           "PID      UID      Username         Name                 Logintime\n";
72 }
73
74 open( PS, "ps $PS_STR |" ) || die "Unable to open a pipe to ``ps''";
75
76 while (<PS>) {
77         next if ( $_ !~ /$MAC_PROCESS/ );
78         my ( $user, $pid, $ppid, $time, $name, $uid, $t );
79         $_ =~ /$MATCH_STR/;
80         $user = $1;
81         $pid  = $2;
82         $ppid = $3;
83         $time = $4;
84
85         if ( $ppid != 1 ) {
86                 ( $t, $t, $uid, $t, $t, $t, $name, $t, $t ) = getpwnam($user);
87                 printf "%-8d %-8d %-16s %-20s %-9s %s\n", $pid, $uid, $user,
88                   $name, $time, $mac{$pid};
89         }
90 }
91
92 close(PS);