2015-03-19 23:21:57 +00:00
|
|
|
#!<%= cmd_paths['ruby'] %>
|
2014-07-25 00:50:24 +00:00
|
|
|
# Automatically created by vagrant-lxc
|
|
|
|
|
|
|
|
class Whitelist
|
|
|
|
class << self
|
|
|
|
def add(command, *args)
|
2015-01-11 22:59:38 +00:00
|
|
|
list[command] ||= []
|
2014-07-25 00:50:24 +00:00
|
|
|
list[command] << args
|
|
|
|
end
|
|
|
|
|
2015-01-11 22:59:38 +00:00
|
|
|
def add_regex(regex, *args)
|
|
|
|
regex_list << [regex, [args]]
|
|
|
|
end
|
|
|
|
|
2014-07-25 00:50:24 +00:00
|
|
|
def list
|
2015-01-11 22:59:38 +00:00
|
|
|
@list ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def regex_list
|
|
|
|
@regex_list ||= []
|
2014-07-25 00:50:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def allowed(command)
|
2015-01-11 22:59:38 +00:00
|
|
|
list[command] || allowed_regex(command) || []
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed_regex(command)
|
|
|
|
found = regex_list.find { |r| r[0] =~ command }
|
|
|
|
return found[1] if found
|
2014-07-25 00:50:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run!(argv)
|
|
|
|
begin
|
|
|
|
command, args = `which #{argv.shift}`.chomp, argv || []
|
|
|
|
check!(command, args)
|
2015-01-11 22:59:38 +00:00
|
|
|
system "#{command} #{args.join(" ")}"
|
2015-01-08 04:18:34 +00:00
|
|
|
|
|
|
|
exit_code = $?.to_i
|
|
|
|
exit_code = 1 if exit_code == 256
|
|
|
|
|
|
|
|
exit exit_code
|
2014-07-25 00:50:24 +00:00
|
|
|
rescue => e
|
|
|
|
STDERR.puts e.message
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def check!(command, args)
|
|
|
|
allowed(command).each do |checks|
|
|
|
|
return if valid_args?(args, checks)
|
|
|
|
end
|
|
|
|
raise_invalid(command, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_args?(args, checks)
|
|
|
|
return false unless valid_length?(args, checks)
|
|
|
|
check = nil
|
|
|
|
args.each_with_index do |provided, i|
|
|
|
|
check = checks[i] unless check == '**'
|
|
|
|
return false unless match?(provided, check)
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_length?(args, checks)
|
|
|
|
args.length == checks.length || checks.last == '**'
|
|
|
|
end
|
|
|
|
|
|
|
|
def match?(arg, check)
|
|
|
|
check == '**' || check.is_a?(Regexp) && !!check.match(arg) || arg == check
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_invalid(command, args)
|
|
|
|
raise "Invalid arguments for command #{command}, " <<
|
|
|
|
"provided args: #{args.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
base = "/var/lib/lxc"
|
|
|
|
base_path = %r{\A#{base}/.*\z}
|
|
|
|
templates_path = %r{\A/usr/(share|lib|lib64|local/lib)/lxc/templates/.*\z}
|
|
|
|
|
|
|
|
##
|
|
|
|
# Commands from provider.rb
|
|
|
|
# - Check lxc is installed
|
|
|
|
Whitelist.add '<%= cmd_paths['which'] %>', /\Alxc-\w+\z/
|
|
|
|
|
|
|
|
##
|
|
|
|
# Commands from driver.rb
|
|
|
|
# - Container config file
|
|
|
|
Whitelist.add '<%= cmd_paths['cat'] %>', base_path
|
|
|
|
# - Shared folders
|
|
|
|
Whitelist.add '<%= cmd_paths['mkdir'] %>', '-p', base_path
|
|
|
|
# - Container config customizations and pruning
|
|
|
|
Whitelist.add '<%= cmd_paths['cp'] %>', '-f', %r{/tmp/.*}, base_path
|
|
|
|
Whitelist.add '<%= cmd_paths['chown'] %>', 'root:root', base_path
|
|
|
|
# - Template import
|
|
|
|
Whitelist.add '<%= cmd_paths['cp'] %>', %r{\A.*\z}, templates_path
|
|
|
|
Whitelist.add '<%= cmd_paths['chmod'] %>', '+x', templates_path
|
|
|
|
# - Template removal
|
|
|
|
Whitelist.add '<%= cmd_paths['rm'] %>', templates_path
|
|
|
|
# - Packaging
|
|
|
|
Whitelist.add '<%= cmd_paths['tar'] %>', '--numeric-owner', '-cvzf', %r{/tmp/.*/rootfs.tar.gz}, '-C', base_path, './rootfs'
|
|
|
|
Whitelist.add '<%= cmd_paths['chown'] %>', /\A\d+:\d+\z/, %r{\A/tmp/.*/rootfs\.tar\.gz\z}
|
2015-01-11 22:59:38 +00:00
|
|
|
# - Private network script and commands
|
|
|
|
Whitelist.add '<%= cmd_paths['ip'] %>', 'addr', 'add', /(\d+|\.)+\/24/, 'dev', /.+/
|
2015-08-25 08:18:02 +00:00
|
|
|
Whitelist.add '<%= cmd_paths['ip'] %>', 'link', 'set', /.+/, /(up|down)/
|
2015-07-06 15:44:35 +00:00
|
|
|
Whitelist.add '<%= cmd_paths['brctl'] %>', /(addbr|delbr)/, /.+/
|
2015-01-11 22:59:38 +00:00
|
|
|
Whitelist.add_regex %r{<%= pipework_regex %>}, '**'
|
2014-07-25 00:50:24 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Commands from driver/cli.rb
|
|
|
|
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-version'
|
|
|
|
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-ls'
|
|
|
|
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-info', '--name', /.*/
|
|
|
|
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-create', '-B', /.*/, '--template', /.*/, '--name', /.*/, '**'
|
2014-10-15 04:01:43 +00:00
|
|
|
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-create', '--version'
|
2014-07-25 00:50:24 +00:00
|
|
|
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-destroy', '--name', /.*/
|
|
|
|
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-start', '-d', '--name', /.*/, '**'
|
|
|
|
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-stop', '--name', /.*/
|
|
|
|
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-shutdown', '--name', /.*/
|
|
|
|
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-attach', '--name', /.*/, '**'
|
|
|
|
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-attach', '-h'
|
2014-10-15 04:01:43 +00:00
|
|
|
Whitelist.add '<%= cmd_paths['lxc_bin'] %>/lxc-config', 'lxc.lxcpath'
|
2014-07-25 00:50:24 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Commands from driver/action/remove_temporary_files.rb
|
|
|
|
Whitelist.add '<%= cmd_paths['rm'] %>', '-rf', %r{\A#{base}/.*/rootfs/tmp/.*}
|
|
|
|
|
|
|
|
# Watch out for stones
|
|
|
|
Whitelist.run!(ARGV)
|