]> git.proxmox.com Git - pve-manager.git/blame - PVE/API2/Backup.pm
backup: add missing user check in update_job
[pve-manager.git] / PVE / API2 / Backup.pm
CommitLineData
ac27b58d
DM
1package PVE::API2::Backup;
2
3use strict;
4use warnings;
52878b0a 5use Digest::SHA;
ac27b58d
DM
6
7use PVE::SafeSyslog;
8use PVE::Tools qw(extract_param);
2424074e 9use PVE::Cluster qw(cfs_lock_file cfs_read_file cfs_write_file);
ac27b58d
DM
10use PVE::RESTHandler;
11use PVE::RPCEnvironment;
12use PVE::JSONSchema;
13use PVE::Storage;
14use PVE::Exception qw(raise_param_exc);
15use PVE::VZDump;
2424074e 16use PVE::VZDump::Common;
ac27b58d
DM
17
18use base qw(PVE::RESTHandler);
19
ac27b58d
DM
20PVE::JSONSchema::register_format('pve-day-of-week', \&verify_day_of_week);
21sub verify_day_of_week {
22 my ($value, $noerr) = @_;
23
24 return $value if $value =~ m/^(mon|tue|wed|thu|fri|sat|sun)$/;
25
26 return undef if $noerr;
27
28 die "invalid day '$value'\n";
29}
30
43b2494b
SR
31my $vzdump_job_id_prop = {
32 type => 'string',
33 description => "The job ID.",
34 maxLength => 50
35};
ac27b58d 36
ac27b58d 37__PACKAGE__->register_method({
60e049c2
TM
38 name => 'index',
39 path => '',
ac27b58d
DM
40 method => 'GET',
41 description => "List vzdump backup schedule.",
937515d6
DM
42 permissions => {
43 check => ['perm', '/', ['Sys.Audit']],
44 },
ac27b58d
DM
45 parameters => {
46 additionalProperties => 0,
47 properties => {},
48 },
49 returns => {
50 type => 'array',
51 items => {
52 type => "object",
53 properties => {
43b2494b 54 id => $vzdump_job_id_prop
ac27b58d
DM
55 },
56 },
57 links => [ { rel => 'child', href => "{id}" } ],
58 },
59 code => sub {
60 my ($param) = @_;
61
62 my $rpcenv = PVE::RPCEnvironment::get();
63 my $user = $rpcenv->get_user();
64
b0905e3a 65 my $data = cfs_read_file('vzdump.cron');
ac27b58d
DM
66
67 my $res = $data->{jobs} || [];
68
69 return $res;
70 }});
71
72__PACKAGE__->register_method({
60e049c2
TM
73 name => 'create_job',
74 path => '',
ac27b58d
DM
75 method => 'POST',
76 protected => 1,
77 description => "Create new vzdump backup job.",
937515d6
DM
78 permissions => {
79 check => ['perm', '/', ['Sys.Modify']],
f0bbc084 80 description => "The 'tmpdir', 'dumpdir' and 'script' parameters are additionally restricted to the 'root\@pam' user.",
937515d6 81 },
ac27b58d
DM
82 parameters => {
83 additionalProperties => 0,
2424074e 84 properties => PVE::VZDump::Common::json_config_properties({
7625ea19
DM
85 starttime => {
86 type => 'string',
87 description => "Job Start time.",
88 pattern => '\d{1,2}:\d{1,2}',
89 typetext => 'HH:MM',
ac27b58d
DM
90 },
91 dow => {
92 type => 'string', format => 'pve-day-of-week-list',
93 optional => 1,
94 description => "Day of week selection.",
95 default => 'mon,tue,wed,thu,fri,sat,sun',
96 },
4341db1d
TL
97 enabled => {
98 type => 'boolean',
99 optional => 1,
100 description => "Enable or disable the job.",
101 default => '1',
102 },
ac27b58d
DM
103 }),
104 },
105 returns => { type => 'null' },
106 code => sub {
107 my ($param) = @_;
108
109 my $rpcenv = PVE::RPCEnvironment::get();
110 my $user = $rpcenv->get_user();
111
f0bbc084
FG
112 foreach my $key (qw(tmpdir dumpdir script)) {
113 raise_param_exc({ $key => "Only root may set this option."})
114 if defined($param->{$key}) && ($user ne 'root@pam');
115 }
116
c92c54d5
TL
117 if (my $pool = $param->{pool}) {
118 $rpcenv->check_pool_exist($pool);
119 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
120 }
121
122
200cef80
CE
123 my $create_job = sub {
124 my $data = cfs_read_file('vzdump.cron');
ac27b58d 125
200cef80
CE
126 $param->{dow} = 'mon,tue,wed,thu,fri,sat,sun' if !defined($param->{dow});
127 $param->{enabled} = 1 if !defined($param->{enabled});
128 PVE::VZDump::verify_vzdump_parameters($param, 1);
ac27b58d 129
200cef80 130 push @{$data->{jobs}}, $param;
ac27b58d 131
200cef80
CE
132 cfs_write_file('vzdump.cron', $data);
133 };
134 cfs_lock_file('vzdump.cron', undef, $create_job);
135 die "$@" if ($@);
ac27b58d
DM
136
137 return undef;
138 }});
139
140__PACKAGE__->register_method({
60e049c2
TM
141 name => 'read_job',
142 path => '{id}',
ac27b58d
DM
143 method => 'GET',
144 description => "Read vzdump backup job definition.",
937515d6
DM
145 permissions => {
146 check => ['perm', '/', ['Sys.Audit']],
147 },
ac27b58d
DM
148 parameters => {
149 additionalProperties => 0,
150 properties => {
43b2494b 151 id => $vzdump_job_id_prop
ac27b58d
DM
152 },
153 },
154 returns => {
155 type => 'object',
156 },
157 code => sub {
158 my ($param) = @_;
159
160 my $rpcenv = PVE::RPCEnvironment::get();
161 my $user = $rpcenv->get_user();
162
b0905e3a 163 my $data = cfs_read_file('vzdump.cron');
ac27b58d
DM
164
165 my $jobs = $data->{jobs} || [];
166
167 foreach my $job (@$jobs) {
168 return $job if $job->{id} eq $param->{id};
169 }
170
171 raise_param_exc({ id => "No such job '$param->{id}'" });
172
173 }});
174
175__PACKAGE__->register_method({
60e049c2
TM
176 name => 'delete_job',
177 path => '{id}',
ac27b58d
DM
178 method => 'DELETE',
179 description => "Delete vzdump backup job definition.",
937515d6
DM
180 permissions => {
181 check => ['perm', '/', ['Sys.Modify']],
182 },
ac27b58d
DM
183 protected => 1,
184 parameters => {
185 additionalProperties => 0,
186 properties => {
43b2494b 187 id => $vzdump_job_id_prop
ac27b58d
DM
188 },
189 },
190 returns => { type => 'null' },
191 code => sub {
192 my ($param) = @_;
193
194 my $rpcenv = PVE::RPCEnvironment::get();
195 my $user = $rpcenv->get_user();
196
200cef80
CE
197 my $delete_job = sub {
198 my $data = cfs_read_file('vzdump.cron');
ac27b58d 199
200cef80
CE
200 my $jobs = $data->{jobs} || [];
201 my $newjobs = [];
ac27b58d 202
200cef80
CE
203 my $found;
204 foreach my $job (@$jobs) {
205 if ($job->{id} eq $param->{id}) {
206 $found = 1;
207 } else {
208 push @$newjobs, $job;
209 }
ac27b58d 210 }
ac27b58d 211
200cef80 212 raise_param_exc({ id => "No such job '$param->{id}'" }) if !$found;
ac27b58d 213
200cef80 214 $data->{jobs} = $newjobs;
ac27b58d 215
200cef80
CE
216 cfs_write_file('vzdump.cron', $data);
217 };
218 cfs_lock_file('vzdump.cron', undef, $delete_job);
219 die "$@" if ($@);
ac27b58d
DM
220
221 return undef;
222 }});
223
224__PACKAGE__->register_method({
60e049c2
TM
225 name => 'update_job',
226 path => '{id}',
ac27b58d
DM
227 method => 'PUT',
228 protected => 1,
229 description => "Update vzdump backup job definition.",
937515d6
DM
230 permissions => {
231 check => ['perm', '/', ['Sys.Modify']],
232 },
ac27b58d
DM
233 parameters => {
234 additionalProperties => 0,
2424074e 235 properties => PVE::VZDump::Common::json_config_properties({
43b2494b 236 id => $vzdump_job_id_prop,
7625ea19
DM
237 starttime => {
238 type => 'string',
239 description => "Job Start time.",
240 pattern => '\d{1,2}:\d{1,2}',
241 typetext => 'HH:MM',
ac27b58d
DM
242 },
243 dow => {
244 type => 'string', format => 'pve-day-of-week-list',
245 optional => 1,
246 description => "Day of week selection.",
247 },
53c6bb6c
DM
248 delete => {
249 type => 'string', format => 'pve-configid-list',
250 description => "A list of settings you want to delete.",
251 optional => 1,
252 },
4341db1d
TL
253 enabled => {
254 type => 'boolean',
255 optional => 1,
256 description => "Enable or disable the job.",
257 default => '1',
258 },
ac27b58d
DM
259 }),
260 },
261 returns => { type => 'null' },
262 code => sub {
263 my ($param) = @_;
264
265 my $rpcenv = PVE::RPCEnvironment::get();
266 my $user = $rpcenv->get_user();
267
d5b9f2e1
OB
268 foreach my $key (qw(tmpdir dumpdir script)) {
269 raise_param_exc({ $key => "Only root may set this option."})
270 if defined($param->{$key}) && ($user ne 'root@pam');
271 }
272
273
16f5b283
TL
274 if (my $pool = $param->{pool}) {
275 $rpcenv->check_pool_exist($pool);
276 $rpcenv->check($user, "/pool/$pool", ['VM.Backup']);
277 }
278
200cef80
CE
279 my $update_job = sub {
280 my $data = cfs_read_file('vzdump.cron');
ac27b58d 281
200cef80 282 my $jobs = $data->{jobs} || [];
ac27b58d 283
200cef80 284 die "no options specified\n" if !scalar(keys %$param);
53c6bb6c 285
200cef80 286 PVE::VZDump::verify_vzdump_parameters($param);
ac27b58d 287
200cef80 288 my @delete = PVE::Tools::split_list(extract_param($param, 'delete'));
53c6bb6c 289
200cef80
CE
290 foreach my $job (@$jobs) {
291 if ($job->{id} eq $param->{id}) {
ac27b58d 292
200cef80
CE
293 foreach my $k (@delete) {
294 if (!PVE::VZDump::option_exists($k)) {
295 raise_param_exc({ delete => "unknown option '$k'" });
296 }
53c6bb6c 297
200cef80
CE
298 delete $job->{$k};
299 }
53c6bb6c 300
200cef80
CE
301 foreach my $k (keys %$param) {
302 $job->{$k} = $param->{$k};
303 }
ac27b58d 304
f3376261 305 $job->{all} = 1 if (defined($job->{exclude}) && !defined($job->{pool}));
ac27b58d 306
200cef80
CE
307 if (defined($param->{vmid})) {
308 delete $job->{all};
309 delete $job->{exclude};
f3376261 310 delete $job->{pool};
200cef80
CE
311 } elsif ($param->{all}) {
312 delete $job->{vmid};
f3376261
TM
313 delete $job->{pool};
314 } elsif ($job->{pool}) {
315 delete $job->{vmid};
316 delete $job->{all};
b05c9908 317 delete $job->{exclude};
200cef80 318 }
ac27b58d 319
200cef80 320 PVE::VZDump::verify_vzdump_parameters($job, 1);
ac27b58d 321
200cef80 322 cfs_write_file('vzdump.cron', $data);
ac27b58d 323
200cef80
CE
324 return undef;
325 }
ac27b58d 326 }
200cef80
CE
327 raise_param_exc({ id => "No such job '$param->{id}'" });
328 };
329 cfs_lock_file('vzdump.cron', undef, $update_job);
330 die "$@" if ($@);
ac27b58d
DM
331 }});
332
3331;