3 # ====================================================================
4 # commit-access-control.pl: check if the user that submitted the
5 # transaction TXN-NAME has the appropriate rights to perform the
6 # commit in repository REPOS using the permissions listed in the
7 # configuration file CONF_FILE.
12 # $LastChangedRevision$
14 # Usage: commit-access-control.pl REPOS TXN-NAME CONF_FILE
16 # ====================================================================
17 # Copyright (c) 2000-2004 CollabNet. All rights reserved.
19 # This software is licensed as described in the file COPYING, which
20 # you should have received as part of this distribution. The terms
21 # are also available at http://subversion.tigris.org/license-1.html.
22 # If newer versions of this license are posted there, you may use a
23 # newer version instead, at your option.
25 # This software consists of voluntary contributions made by many
26 # individuals. For exact contribution history, see the revision
27 # history and logs, available at http://subversion.tigris.org/.
28 # ====================================================================
30 # Turn on warnings the best way depending on the Perl version.
33 { require warnings
; import warnings
; }
40 use Config
::IniFiles
2.27;
42 ######################################################################
43 # Configuration section.
46 my $svnlook = "@SVN_BINDIR@/svnlook";
48 # Since the path to svnlook depends upon the local installation
49 # preferences, check that the required program exists to insure that
50 # the administrator has set up the script properly.
53 foreach my $program ($svnlook)
59 warn "$0: required program `$program' is not executable, ",
66 warn "$0: required program `$program' does not exist, edit $0.\n";
73 ######################################################################
74 # Initial setup/command-line handling.
76 &usage
unless @ARGV == 3;
80 my $cfg_filename = shift;
84 &usage
("$0: repository directory `$repos' does not exist.");
88 &usage
("$0: repository directory `$repos' is not a directory.");
90 unless (-e
$cfg_filename)
92 &usage
("$0: configuration file `$cfg_filename' does not exist.");
94 unless (-r
$cfg_filename)
96 &usage
("$0: configuration file `$cfg_filename' is not readable.");
99 # Define two constant subroutines to stand for read-only or read-write
100 # access to the repository.
101 sub ACCESS_READ_ONLY
() { 'read-only' }
102 sub ACCESS_READ_WRITE
() { 'read-write' }
104 ######################################################################
105 # Load the configuration file and validate it.
106 my $cfg = Config
::IniFiles
->new(-file
=> $cfg_filename);
109 die "$0: error in loading configuration file `$cfg_filename'",
110 @Config::IniFiles
::errors ?
":\n@Config::IniFiles::errors\n"
114 # Go through each section of the configuration file, validate that
115 # each section has the required parameters and complain about unknown
116 # parameters. Compile any regular expressions.
117 my @sections = $cfg->Sections;
120 foreach my $section (@sections)
122 # First check for any unknown parameters.
123 foreach my $param ($cfg->Parameters($section))
125 next if $param eq 'match';
126 next if $param eq 'users';
127 next if $param eq 'access';
128 warn "$0: config file `$cfg_filename' section `$section' parameter ",
129 "`$param' is being ignored.\n";
130 $cfg->delval($section, $param);
133 my $access = $cfg->val($section, 'access');
136 unless ($access eq ACCESS_READ_ONLY
or $access eq ACCESS_READ_WRITE
)
138 warn "$0: config file `$cfg_filename' section `$section' sets ",
139 "`access' to illegal value `$access'.\n";
145 warn "$0: config file `$cfg_filename' section `$section' does ",
146 "not set `access' parameter.\n";
150 my $match_regex = $cfg->val($section, 'match');
151 if (defined $match_regex)
153 # To help users that automatically write regular expressions
154 # that match the beginning of absolute paths using ^/,
155 # remove the / character because subversion paths, while
156 # they start at the root level, do not begin with a /.
157 $match_regex =~ s
#^\^/#^#;
160 eval { $match_re = qr/$match_regex/ };
163 warn "$0: config file `$cfg_filename' section `$section' ",
164 "`match' regex `$match_regex' does not compile:\n$@\n";
169 $cfg->newval($section, 'match_re', $match_re);
174 warn "$0: config file `$cfg_filename' section `$section' does ",
175 "not set `match' parameter.\n";
182 ######################################################################
183 # Harvest data using svnlook.
185 # Change into /tmp so that svnlook diff can create its .svnlook
187 my $tmp_dir = '/tmp';
189 or die "$0: cannot chdir `$tmp_dir': $!\n";
191 # Get the author from svnlook.
192 my @svnlooklines = &read_from_process
($svnlook, 'author', $repos, '-t', $txn);
193 my $author = shift @svnlooklines;
194 unless (length $author)
196 die "$0: txn `$txn' has no author.\n";
199 # Figure out what directories have changed using svnlook..
200 my @dirs_changed = &read_from_process
($svnlook, 'dirs-changed', $repos,
203 # Lose the trailing slash in the directory names if one exists, except
204 # in the case of '/'.
206 for (my $i=0; $i<@dirs_changed; ++$i)
208 if ($dirs_changed[$i] eq '/')
214 $dirs_changed[$i] =~ s
#^(.+)[/\\]$#$1#;
218 # Figure out what files have changed using svnlook.
220 foreach my $line (&read_from_process
($svnlook, 'changed', $repos, '-t', $txn))
222 # Split the line up into the modification code and path, ignoring
223 # property modifications.
224 if ($line =~ /^.. (.*)$/)
226 push(@files_changed, $1);
230 # Create the list of all modified paths.
231 my @changed = (@dirs_changed, @files_changed);
233 # There should always be at least one changed path. If there are
234 # none, then there maybe something fishy going on, so just exit now
235 # indicating that the commit should not proceed.
238 die "$0: no changed paths found in txn `$txn'.\n";
241 ######################################################################
242 # Populate the permissions table.
244 # Set a hash keeping track of the access rights to each path. Because
245 # this is an access control script, set the default permissions to
248 foreach my $path (@changed)
250 $permissions{$path} = ACCESS_READ_ONLY
;
253 foreach my $section (@sections)
255 # Decide if this section should be used. It should be used if
256 # there are no users listed at all for this section, or if there
257 # are users listed and the author is one of them.
258 my $use_this_section;
260 # If there are any users listed, then check if the author of this
261 # commit is listed in the list. If not, then delete the section,
262 # because it won't apply.
264 # The configuration file can list users like this on multiple
266 # users = joe@mysite.com betty@mysite.com
267 # users = bob@yoursite.com
269 # Because of the way Config::IniFiles works, check if there are
270 # any users at all with the scalar return from val() and if there,
271 # then get the array value to get all users.
272 my $users = $cfg->val($section, 'users');
273 if (defined $users and length $users)
276 foreach my $entry ($cfg->val($section, 'users'))
280 foreach my $user (split(' ', $entry))
282 if ($author eq $user)
291 $use_this_section = $match_user;
295 $use_this_section = 1;
298 next unless $use_this_section;
300 # Go through each modified path and match it to the regular
301 # expression and set the access right if the regular expression
303 my $access = $cfg->val($section, 'access');
304 my $match_re = $cfg->val($section, 'match_re');
305 foreach my $path (@changed)
307 $permissions{$path} = $access if $path =~ $match_re;
311 # Go through all the modified paths and see if any permissions are
312 # read-only. If so, then fail the commit.
314 foreach my $path (@changed)
316 if ($permissions{$path} ne ACCESS_READ_WRITE
)
318 push(@failed_paths, $path);
324 warn "$0: user `$author' does not have permission to commit to ",
325 @failed_paths > 1 ?
"these paths:\n " : "this path:\n ",
326 join("\n ", @failed_paths), "\n";
337 die "usage: $0 REPOS TXN-NAME CONF_FILE\n";
340 sub safe_read_from_pipe
344 croak
"$0: safe_read_from_pipe passed no arguments.\n";
346 print "Running @_\n";
347 my $pid = open(SAFE_READ
, '-|');
348 unless (defined $pid)
350 die "$0: cannot fork: $!\n";
354 open(STDERR
, ">&STDOUT")
355 or die "$0: cannot dup STDOUT: $!\n";
357 or die "$0: cannot exec `@_': $!\n";
367 my $exit = $result >> 8;
368 my $signal = $result & 127;
369 my $cd = $result & 128 ?
"with core dump" : "";
372 warn "$0: pipe from `@_' failed $cd: exit=$exit signal=$signal\n";
376 return ($result, @output);
384 sub read_from_process
388 croak
"$0: read_from_process passed no arguments.\n";
390 my ($status, @output) = &safe_read_from_pipe
(@_);
395 die "$0: `@_' failed with this output:\n", join("\n", @output), "\n";
399 die "$0: `@_' failed with no output.\n";