-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpermit
executable file
·60 lines (56 loc) · 1.73 KB
/
permit
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
#!/usr/bin/env ruby
require File.expand_path(File.dirname(__FILE__) + '/shared')
require 'optparse'
def parse_options
options = {}
parser = OptionParser.new do |opts|
nl = "\n" + ' ' * 37
opts.banner = "Usage: permit <USERNAME> <DIRECTORIES...> [options]"
opts.separator "Give USERNAME read-only or read-write permission to the given directories."
opts.separator ""
opts.separator "Options:"
opts.on("--read-write",
"Give read-write access. Default is#{nl}" +
"read-only unless this option is given.") do
options[:read_write] = true
end
end
begin
parser.parse!
rescue OptionParser::ParseError => e
puts e
puts
puts "Please see '--help' for valid options."
exit 1
end
if options[:help]
puts parser
exit
elsif ARGV.size < 2
puts parser
exit 1
else
return options
end
end
options = parse_options
username, *dirs = ARGV
dirs.each do |dir|
print_activity "cd #{dir}"
Dir.chdir(dir) do
executable_files = "/tmp/executable-files.#{$$}"
sh "find -type f -executable -print0 > #{executable_files}"
if options[:read_write]
sh "find -type f -print0 | xargs -0 -n 1000 -r setfacl -m user:#{username}:rw-"
sh "find -type d -print0 | xargs -0 -n 1000 -r setfacl -m user:#{username}:rwx"
sh "find -type d -print0 | xargs -0 -n 1000 -r setfacl -d -m user:#{username}:rwx"
else
sh "find -type f -print0 | xargs -0 -n 1000 -r setfacl -m user:#{username}:r-"
sh "find -type d -print0 | xargs -0 -n 1000 -r setfacl -m user:#{username}:r-x"
sh "find -type d -print0 | xargs -0 -n 1000 -r setfacl -d -m user:#{username}:r-x"
end
sh "find -type f -print0 | xargs -0 -n 1000 -r chmod -x"
sh "cat #{executable_files} | xargs -0 -n 1000 -r chmod +x"
sh "rm -f #{executable_files}"
end
end