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