]> git.proxmox.com Git - pve-container.git/blame - src/PVE/API2/LXC/Config.pm
fix permission check skipping
[pve-container.git] / src / PVE / API2 / LXC / Config.pm
CommitLineData
52389a07
DM
1package PVE::API2::LXC::Config;
2
3use strict;
4use warnings;
5
6use PVE::SafeSyslog;
7use PVE::Tools qw(extract_param run_command);
8use PVE::Exception qw(raise raise_param_exc);
9use PVE::INotify;
10use PVE::Cluster qw(cfs_read_file);
11use PVE::AccessControl;
12use PVE::Firewall;
13use PVE::Storage;
14use PVE::RESTHandler;
15use PVE::RPCEnvironment;
16use PVE::LXC;
17use PVE::LXC::Create;
52389a07
DM
18use PVE::JSONSchema qw(get_standard_option);
19use base qw(PVE::RESTHandler);
20
21use Data::Dumper; # fixme: remove
22
23__PACKAGE__->register_method({
24 name => 'vm_config',
25 path => '',
26 method => 'GET',
27 proxyto => 'node',
28 description => "Get container configuration.",
29 permissions => {
30 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
31 },
32 parameters => {
33 additionalProperties => 0,
34 properties => {
35 node => get_standard_option('pve-node'),
68e8f3c5 36 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
52389a07
DM
37 },
38 },
39 returns => {
40 type => "object",
41 properties => {
42 digest => {
43 type => 'string',
44 description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
45 }
46 },
47 },
48 code => sub {
49 my ($param) = @_;
50
51 my $conf = PVE::LXC::load_config($param->{vmid});
52
53 delete $conf->{snapshots};
54 delete $conf->{lxc};
55
56 return $conf;
57 }});
58
59my $vm_config_perm_list = [
60 'VM.Config.Disk',
61 'VM.Config.CPU',
62 'VM.Config.Memory',
63 'VM.Config.Network',
64 'VM.Config.Options',
65 ];
66
67__PACKAGE__->register_method({
68 name => 'update_vm',
69 path => '',
70 method => 'PUT',
71 protected => 1,
72 proxyto => 'node',
73 description => "Set container options.",
74 permissions => {
75 check => ['perm', '/vms/{vmid}', $vm_config_perm_list, any => 1],
76 },
77 parameters => {
78 additionalProperties => 0,
79 properties => PVE::LXC::json_config_properties(
80 {
81 node => get_standard_option('pve-node'),
68e8f3c5 82 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid }),
52389a07
DM
83 delete => {
84 type => 'string', format => 'pve-configid-list',
85 description => "A list of settings you want to delete.",
86 optional => 1,
87 },
88 digest => {
89 type => 'string',
90 description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
91 maxLength => 40,
92 optional => 1,
93 }
94 }),
95 },
96 returns => { type => 'null'},
97 code => sub {
98 my ($param) = @_;
99
100 my $rpcenv = PVE::RPCEnvironment::get();
101
102 my $authuser = $rpcenv->get_user();
103
104 my $node = extract_param($param, 'node');
105
106 my $vmid = extract_param($param, 'vmid');
107
108 my $digest = extract_param($param, 'digest');
109
110 die "no options specified\n" if !scalar(keys %$param);
111
112 my $delete_str = extract_param($param, 'delete');
113 my @delete = PVE::Tools::split_list($delete_str);
114
115 PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [@delete]);
116
117 foreach my $opt (@delete) {
118 raise_param_exc({ delete => "you can't use '-$opt' and " .
119 "-delete $opt' at the same time" })
120 if defined($param->{$opt});
121
122 if (!PVE::LXC::option_exists($opt)) {
123 raise_param_exc({ delete => "unknown option '$opt'" });
124 }
125 }
126
127 PVE::LXC::check_ct_modify_config_perm($rpcenv, $authuser, $vmid, undef, [keys %$param]);
128
129 my $storage_cfg = cfs_read_file("storage.cfg");
130
131 my $code = sub {
132
133 my $conf = PVE::LXC::load_config($vmid);
134 PVE::LXC::check_lock($conf);
135
136 PVE::Tools::assert_if_modified($digest, $conf->{digest});
137
138 my $running = PVE::LXC::check_running($vmid);
139
140 PVE::LXC::update_pct_config($vmid, $conf, $running, $param, \@delete);
141
142 PVE::LXC::write_config($vmid, $conf);
143 PVE::LXC::update_lxc_config($storage_cfg, $vmid, $conf);
144 };
145
3cc56749 146 PVE::LXC::lock_config($vmid, $code);
52389a07
DM
147
148 return undef;
149 }});
150
1511;