File: macusers.in

package info (click to toggle)
netatalk 3.1.12~ds-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,756 kB
  • sloc: ansic: 104,976; sh: 8,247; xml: 7,394; perl: 1,936; makefile: 1,430; python: 1,342; yacc: 309; lex: 49
file content (141 lines) | stat: -rw-r--r-- 5,117 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!@PERL@

use strict;
use Socket;
use File::Basename;
use vars qw($MAIN_PID $NETATALK_PROCESS $AFPD_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 <marcus@marcuscom.com>.
# Support Solaris added by Frank Lahm <franklahm@googlemail.com>.
# Support has also been added for 16 character usernames.

if ($ARGV[0] =~ /^(-v|-version|--version)$/ ) {
        printf ("%s \(Netatalk @NETATALK_VERSION@\)\n", basename($0));
        exit(1);
} elsif ($ARGV[0] =~ /^(-h|-help|--help)$/ ) {
        printf ("usage: %s \[-v|-version|--version|-h|-help|--help\]\n", basename($0));
        printf ("Show users connecting via AFP\n");
        exit(1);
}

$NETATALK_PROCESS = "netatalk";
$AFPD_PROCESS = "afpd";
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;

# Change to 0 if you don't have lsof
$LSOF = 1;
my %mac = ();

if ($^O eq "freebsd") {
        open(SOCKSTAT, "sockstat -4 | grep $AFPD_PROCESS | grep -v grep |");

        while (<SOCKSTAT>) {
                next if ($_ !~ /$AFPD_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 (<LSOF>) {
                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''";

$MAIN_PID = 1;
while (<PS>) {
        next if ($_ !~ /$NETATALK_PROCESS/);
        my ($user, $pid, $ppid, $time, $name, $uid, $t, $ip);
        $_ =~ /$MATCH_STR/;
        $MAIN_PID = $2;
}

close(PS);
open(PS, "ps $PS_STR |") || die "Unable to open a pipe to ``ps''";

while (<PS>) {
        next if ($_ !~ /$AFPD_PROCESS/);
        my ($user, $pid, $ppid, $time, $name, $uid, $t, $ip);
        $_ =~ /$MATCH_STR/;
        $user = $1;
        $pid  = $2;
        $ppid = $3;
        $time = $4;

        if ($ppid != $MAIN_PID) {
                if ($^O eq "solaris") {
                        open(PFILES, "pfiles $pid |");
                        while (<PFILES>) {
                                next if ($_ !~ /port: $ASIP_PORT_NO/);
                                while (<PFILES>) {
                                        next if ($_ !~ /peername/);
                                        if ($_ =~ /AF_INET (.*) port/) {
                                            $ip = $1;
                                            if ($ip =~ /::ffff:(.*)/ ) {
                                                $ip = $1;
                                            }
                                        }
                                        $mac{$pid} = $ip;
                                        last;
                                }
                                last;
                        }
                        close(PFILES);
                }

                # Deal with truncated usernames. Caution: this does make the
                # assumption that no username will be all-numeric.
                if ($user =~ /^[0-9]+$/) {
                        $uid = $user;
                        ($user, $t, $t, $t, $t, $t, $name, $t, $t) = getpwuid($uid);
                } else {
                        ($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);