]> arthur.barton.de Git - netatalk.git/blob - contrib/macusers/macusers.in
Support long usernames. From Alain Richard.
[netatalk.git] / contrib / macusers / macusers.in
1 #!@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" || $^O eq "openbsd") {
21         $PS_STR    = "-awwxouser,pid,ppid,start,command";
22         $MATCH_STR = '(\w+)\s+(\d+)\s+(\d+)\s+([\d\w:]+)';
23 } else {
24         $PS_STR    = "-eo user:32,pid,ppid,c,stime,tty,time,cmd";
25         $MATCH_STR = '\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)';
26 }
27 $ASIP_PORT    = "afpovertcp";
28 $ASIP_PORT_NO = 548;
29
30 # Change to 0 if you don't have lsof
31 $LSOF = 1;
32 my %mac = ();
33
34 if ($^O eq "freebsd") {
35         open(SOCKSTAT, "sockstat -4 | grep $MAC_PROCESS | grep -v grep |");
36
37         while (<SOCKSTAT>) {
38                 next if ($_ !~ /$MAC_PROCESS/);
39                 $_ =~
40                     /\S+\s+\S+\s+(\d+)\s+\d+\s+[\w\d]+\s+[\d\.:]+\s+([\d\.]+)/;
41                 my ($pid, $addr, $host);
42                 $pid  = $1;
43                 $addr = $2;
44                 $host = gethostbyaddr(pack('C4', split (/\./, $addr)), AF_INET);
45                 ($host) = ( $host =~ /(^(\d+\.){3}\d+|[\w\d\-]+)/ );
46                 $mac{$pid} = $host;
47         }
48         print
49             "PID      UID      Username         Name                 Logintime Mac\n";
50         close(SOCKSTAT);
51 } elsif ($LSOF == 1) {
52         open(LSOF, "lsof -i :$ASIP_PORT |");
53
54         while (<LSOF>) {
55                 next if ($_ !~ /$ASIP_PORT/);
56                 $_ =~ /\w+\s+(\d+).*->([\w\.-]+).*/;
57                 my ($pid, $host);
58                 $pid  = $1;
59                 $host = $2;
60                 ($host) = ( $host =~ /(^(\d+\.){3}\d+|[\w\d\-]+)/ );
61                 $mac{$pid} = $host;
62         }
63         print
64             "PID      UID      Username         Name                 Logintime Mac\n";
65         close(LSOF);
66 } else {
67         print
68             "PID      UID      Username         Name                 Logintime\n";
69 }
70
71 open(PS, "ps $PS_STR |") || die "Unable to open a pipe to ``ps''";
72
73 while (<PS>) {
74         next if ($_ !~ /$MAC_PROCESS/);
75         my ($user, $pid, $ppid, $time, $name, $uid, $t);
76         $_ =~ /$MATCH_STR/;
77         $user = $1;
78         $pid  = $2;
79         $ppid = $3;
80         $time = $4;
81
82         if ($ppid != 1) {
83                 ($t, $t, $uid, $t, $t, $t, $name, $t, $t) = getpwnam($user);
84                 ($name) = ( $name =~ /(^[^,]+)/ );
85                 printf "%-8d %-8d %-16s %-20s %-9s %s\n", $pid, $uid, $user,
86                     $name, $time, $mac{$pid};
87         }
88 }
89
90 close(PS);