]> arthur.barton.de Git - netatalk.git/blob - contrib/permtest/permtest.pl.in
permission testing tool, from Frank Lahm
[netatalk.git] / contrib / permtest / permtest.pl.in
1 #!@PERL@ -w
2
3 ###########################################################################
4 #
5 # Characterization testing netatalks permission model
6 #
7 # (c) 2008 by Frank Lahm <franklahm@googlemail.com>
8 #
9 #   This program is free software; you can redistribute it and/or modify
10 #   it under the terms of the GNU General Public License as published by
11 #   the Free Software Foundation; either version 2 of the License, or
12 #   (at your option) any later version.
13
14 #   This program is distributed in the hope that it will be useful,
15 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #   GNU General Public License for more details.
18
19 ###########################################################################
20
21 ###########################################################################
22 #
23 # Usage:
24 #
25 #  "permtest.cfg" must be in your CWD. Must be run on a OS X host. Tested
26 #  with 10.4.11. Uses Applescript through system("osascript ...") to mount
27 #  AFP Volumes. Uses `ssh LOGIN@HOST stat FILE|DIR`. Therefor PKI
28 #  authentication must be setup and working!
29 #  See "permtest.cfg" for more details, it's pretty much self-explaining.
30
31 ###########################################################################
32
33 use strict;
34
35 my $DEBUG = 0;
36
37 ###########################################################################
38
39 sub parseConfig;
40 sub mountAPFVols;
41 sub createTestFiles;
42 sub createTestDirs;
43 sub verifyTestFiles;
44 sub verifyTestDirs;
45 sub unmountAfp;
46
47 my ($sshLogin, $sshResult, %sshStat, @AFPVols, @createFiles, @createDirs, @testFiles, @testDirs);
48 my ($dir, $file, $user, $group, $perms, $mode, $cmd);
49
50 parseConfig();
51 mountAPFVols();
52 createTestFiles();
53 createTestDirs();
54 print "\n";
55 verifyTestFiles();
56 verifyTestDirs();
57 unmountAfp();
58
59 exit 0;
60
61 ###########################################################################
62
63 # parse config file
64 sub parseConfig
65 {
66         open CFG, "permtest.cfg" or die "Config file not found!";
67         while (<CFG>) {
68                 chop;
69             if (/^#/) { next; };
70             if (/^sshLogin/) {
71                         $sshLogin = $_;
72                         $sshLogin =~ s/^sshLogin ?= ?// ;
73                         next;
74                 }
75                 if (/^mountAFPVolume/) {
76                         s/^mountAFPVolume ?= ?// ;
77                         print "Found AFP Volume Definition \"$_\"\n" if $DEBUG;
78                         push @AFPVols, $_;
79                         next;
80                 }
81             if (/^createFile/) {
82                         s/^createFile ?= ?// ;
83                         push @createFiles, $_;
84                         next;
85                 }
86                 if (/^createDir/) {
87                         s/^createDir ?= ?// ;
88                         push @createDirs, $_;
89                         next;
90                 }
91                 if (/^testFile/) {
92                         push @testFiles, $_;
93                         next;
94                 }
95                 if (/^testDir/) {
96                         push @testDirs, $_;
97                         next;
98                 }
99         }
100         close CFG;
101 }
102
103 # mount AFP Volumes
104 sub mountAPFVols
105 {
106         foreach (@AFPVols) {
107                 print "Mounting AFP Volume \"$_\"\n";
108                 $cmd = "osascript -e 'tell application \"Finder\"' -e 'mount volume \"$_\"' -e 'end tell' &> /dev/null";
109                 print "Going to run the following Applescript:\n" . $cmd . "\n\n" if $DEBUG;
110                 system($cmd);
111                 if ($? != 0) { die "Error mounting \"$_\"\n"; }
112         }
113 }
114
115 # Create test files
116 sub createTestFiles
117 {
118         foreach (@createFiles) {
119                 s/^createFile ?= ?// ;
120                 system("rm \"$_\" &> /dev/null");
121                 print "Creating file: \"$_\"\n";
122                 system("touch \"$_\"");
123                 if ($? != 0) { die "Error creating file \"$_\"\n"; }
124         }
125 }
126
127 # Create test dirs
128 sub createTestDirs
129 {
130         foreach (@createDirs) {
131                 s/^createDir ?= ?// ;
132                 system("rmdir \"$_\" &> /dev/null");
133                 print "Creating dir: \"$_\"\n";
134                 system("mkdir \"$_\"");
135                 if ($? != 0) { die "Error creating dir \"$_\"\n"; }
136         }
137 }
138
139 # Verify files and dirs
140 sub verifyTestFiles
141 {
142         foreach (@testFiles) {
143                 my @line = split(",");
144                 foreach (@line) {
145                         if (/^testFile/) {
146                                 $file = $_;
147                                 $file =~ s/^testFile ?= ?//;
148                         }
149                         elsif (/^user/) {
150                                 $user = $_;
151                                 $user =~ s/^user ?= ?//;
152                         }
153                         elsif (/^group/) {
154                                 $group = $_;
155                                 $group =~ s/^group ?= ?//;
156                         }
157                         elsif (/^mode/) {
158                                 $mode = $_;
159                                 $mode =~ s/^mode ?= ?//;
160                         }
161                 } # foreach (@elems)
162                 print "File: $file, User: $user, Group: $group, Perms: $perms\n" if $DEBUG;
163         
164                 $sshResult = `ssh $sshLogin stat -c \"user,%U,group,%G,mode,0%a\" \"$file\"`;
165                 if ($? != 0) { die "Error stat'ing file \"$file\"\n"; }
166                 chop $sshResult;
167                 print "ssh stat $file gave us: $sshResult\n" if $DEBUG;
168         
169                 %sshStat = split(",", $sshResult);
170                 if ( ($sshStat{user} ne $user) or ($sshStat{group} ne $group) or ($sshStat{mode} ne $mode) ) {
171                         print "Creatin error for: \"$file\"!\nExpected:\t$user, $group, $mode.\nGot:\t\t$sshStat{user}, $sshStat{group}, $sshStat{mode}.\n\n";
172                 }
173                 system("rm \"$file\"");
174                 if ($? != 0) { die "Couldn't delete \"$file\"\n"; }
175         }
176 }
177
178 sub verifyTestDirs
179 {
180         foreach (@testDirs) {
181                 my @line = split(",");
182                 foreach (@line) {
183                         if (/^testDir/) {
184                                 $dir = $_;
185                                 $dir =~ s/^testDir ?= ?//;
186                         }
187                         elsif (/^user/) {
188                                 $user = $_;
189                                 $user =~ s/^user ?= ?//;
190                         }
191                         elsif (/^group/) {
192                                 $group = $_;
193                                 $group =~ s/^group ?= ?//;
194                         }
195                         elsif (/^mode/) {
196                                 $mode = $_;
197                                 $mode =~ s/^mode ?= ?//;
198                         }
199                 } # foreach (@elems)
200                 print "Dir: $dir, User: $user, Group: $group, Perms: $perms\n" if $DEBUG;
201         
202                 $sshResult = `ssh $sshLogin stat -c \"user,%U,group,%G,mode,0%a\" \"$dir\"`;
203                 if ($? != 0) { die "Error stat'ing file \"$dir\"\n"; }
204                 chop $sshResult;
205                 print "ssh stat $dir gave us: $sshResult\n" if $DEBUG;
206         
207                 %sshStat = split(",", $sshResult);
208                 if ( ($sshStat{user} ne $user) or ($sshStat{group} ne $group) or ($sshStat{mode} ne $mode) ) {
209                         print "Creatin error for: \"$dir\"!\nExpected:\t$user, $group, $mode.\nGot:\t\t$sshStat{user}, $sshStat{group}, $sshStat{mode}.\n\n";
210                 }
211                 system("rmdir \"$dir\""); if ($? != 0) { die "Couldn't delete \"$dir\"\n"; }
212         }
213 }
214
215 sub unmountAfp
216 {
217         foreach (@AFPVols) {
218                 print "Goint to eject Volume \"$_\"\n";
219                 s#^(.*/)## ;
220                 $cmd = "osascript -e 'tell application \"Finder\"' -e 'eject \"$_\"' -e 'end tell' &> /dev/null";
221                 print "Going to run the following Applescript:\n" . $cmd . "\n\n" if $DEBUG;
222                 system($cmd);
223         }
224 }