]> git.proxmox.com Git - pve-common.git/blob - src/PVE/PBSClient.pm
pbs client: delete password: return success for non-existent file
[pve-common.git] / src / PVE / PBSClient.pm
1 package PVE::PBSClient;
2 # utility functions for interaction with Proxmox Backup client CLI executable
3
4 use strict;
5 use warnings;
6
7 use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
8 use File::Temp qw(tempdir);
9 use IO::File;
10 use JSON;
11 use POSIX qw(mkfifo strftime ENOENT);
12
13 use PVE::JSONSchema qw(get_standard_option);
14 use PVE::Tools qw(run_command file_set_contents file_get_contents file_read_firstline $IPV6RE);
15
16 # returns a repository string suitable for proxmox-backup-client, pbs-restore, etc.
17 # $scfg must have the following structure:
18 # {
19 # datastore
20 # server
21 # port (optional defaults to 8007)
22 # username (optional defaults to 'root@pam')
23 # }
24 sub get_repository {
25 my ($scfg) = @_;
26
27 my $server = $scfg->{server};
28 die "no server given\n" if !defined($server);
29
30 $server = "[$server]" if $server =~ /^$IPV6RE$/;
31
32 if (my $port = $scfg->{port}) {
33 $server .= ":$port" if $port != 8007;
34 }
35
36 my $datastore = $scfg->{datastore};
37 die "no datastore given\n" if !defined($datastore);
38
39 my $username = $scfg->{username} // 'root@pam';
40
41 return "$username\@$server:$datastore";
42 }
43
44 sub new {
45 my ($class, $scfg, $storeid, $sdir) = @_;
46
47 die "no section config provided\n" if ref($scfg) eq '';
48 die "undefined store id\n" if !defined($storeid);
49
50 my $secret_dir = $sdir // '/etc/pve/priv/storage';
51
52 my $self = bless {
53 scfg => $scfg,
54 storeid => $storeid,
55 secret_dir => $secret_dir
56 }, $class;
57 return $self;
58 }
59
60 my sub password_file_name {
61 my ($self) = @_;
62
63 return "$self->{secret_dir}/$self->{storeid}.pw";
64 }
65
66 sub set_password {
67 my ($self, $password) = @_;
68
69 my $pwfile = password_file_name($self);
70 mkdir $self->{secret_dir};
71
72 PVE::Tools::file_set_contents($pwfile, "$password\n", 0600);
73 };
74
75 sub delete_password {
76 my ($self) = @_;
77
78 my $pwfile = password_file_name($self);
79
80 unlink $pwfile or $! == ENOENT or die "deleting password file failed - $!\n";
81 };
82
83 sub get_password {
84 my ($self) = @_;
85
86 my $pwfile = password_file_name($self);
87
88 return PVE::Tools::file_read_firstline($pwfile);
89 }
90
91 sub encryption_key_file_name {
92 my ($self) = @_;
93
94 return "$self->{secret_dir}/$self->{storeid}.enc";
95 };
96
97 sub set_encryption_key {
98 my ($self, $key) = @_;
99
100 my $encfile = $self->encryption_key_file_name();
101 mkdir $self->{secret_dir};
102
103 PVE::Tools::file_set_contents($encfile, "$key\n", 0600);
104 };
105
106 sub delete_encryption_key {
107 my ($self) = @_;
108
109 my $encfile = $self->encryption_key_file_name();
110
111 if (!unlink $encfile) {
112 return if $! == ENOENT;
113 die "failed to delete encryption key! $!\n";
114 }
115 };
116
117 # Returns a file handle if there is an encryption key, or `undef` if there is not. Dies on error.
118 my sub open_encryption_key {
119 my ($self) = @_;
120
121 my $encryption_key_file = $self->encryption_key_file_name();
122
123 my $keyfd;
124 if (!open($keyfd, '<', $encryption_key_file)) {
125 return undef if $! == ENOENT;
126 die "failed to open encryption key: $encryption_key_file: $!\n";
127 }
128
129 return $keyfd;
130 }
131
132 my $USE_CRYPT_PARAMS = {
133 'proxmox-backup-client' => {
134 backup => 1,
135 restore => 1,
136 'upload-log' => 1,
137 },
138 'proxmox-file-restore' => {
139 list => 1,
140 extract => 1,
141 },
142 };
143
144 my sub do_raw_client_cmd {
145 my ($self, $client_cmd, $param, %opts) = @_;
146
147 my $client_bin = (delete $opts{binary}) || 'proxmox-backup-client';
148 my $use_crypto = $USE_CRYPT_PARAMS->{$client_bin}->{$client_cmd} // 0;
149
150 my $client_exe = "/usr/bin/$client_bin";
151 die "executable not found '$client_exe'! $client_bin not installed?\n" if ! -x $client_exe;
152
153 my $scfg = $self->{scfg};
154 my $repo = get_repository($scfg);
155
156 my $userns_cmd = delete $opts{userns_cmd};
157
158 my $cmd = [];
159
160 push @$cmd, @$userns_cmd if defined($userns_cmd);
161
162 push @$cmd, $client_exe, $client_cmd;
163
164 # This must live in the top scope to not get closed before the `run_command`
165 my $keyfd;
166 if ($use_crypto) {
167 if (defined($keyfd = open_encryption_key($self))) {
168 my $flags = fcntl($keyfd, F_GETFD, 0)
169 // die "failed to get file descriptor flags: $!\n";
170 fcntl($keyfd, F_SETFD, $flags & ~FD_CLOEXEC)
171 or die "failed to remove FD_CLOEXEC from encryption key file descriptor\n";
172 push @$cmd, '--crypt-mode=encrypt', '--keyfd='.fileno($keyfd);
173 } else {
174 push @$cmd, '--crypt-mode=none';
175 }
176 }
177
178 push @$cmd, @$param if defined($param);
179
180 push @$cmd, "--repository", $repo;
181 if (defined(my $ns = delete($opts{namespace}))) {
182 push @$cmd, '--ns', $ns;
183 }
184
185 local $ENV{PBS_PASSWORD} = $self->get_password();
186
187 local $ENV{PBS_FINGERPRINT} = $scfg->{fingerprint};
188
189 # no ascii-art on task logs
190 local $ENV{PROXMOX_OUTPUT_NO_BORDER} = 1;
191 local $ENV{PROXMOX_OUTPUT_NO_HEADER} = 1;
192
193 if (my $logfunc = $opts{logfunc}) {
194 $logfunc->("run: " . join(' ', @$cmd));
195 }
196
197 run_command($cmd, %opts);
198 }
199
200 my sub run_raw_client_cmd : prototype($$$%) {
201 my ($self, $client_cmd, $param, %opts) = @_;
202 return do_raw_client_cmd($self, $client_cmd, $param, %opts);
203 }
204
205 my sub run_client_cmd : prototype($$;$$$$) {
206 my ($self, $client_cmd, $param, $no_output, $binary, $namespace) = @_;
207
208 my $json_str = '';
209 my $outfunc = sub { $json_str .= "$_[0]\n" };
210
211 $binary //= 'proxmox-backup-client';
212
213 $param = [] if !defined($param);
214 $param = [ $param ] if !ref($param);
215
216 $param = [@$param, '--output-format=json'] if !$no_output;
217
218 do_raw_client_cmd(
219 $self,
220 $client_cmd,
221 $param,
222 outfunc => $outfunc,
223 errmsg => "$binary failed",
224 binary => $binary,
225 namespace => $namespace,
226 );
227
228 return undef if $no_output;
229
230 my $res = decode_json($json_str);
231
232 return $res;
233 }
234
235 sub autogen_encryption_key {
236 my ($self) = @_;
237 my $encfile = $self->encryption_key_file_name();
238 run_command(
239 ['proxmox-backup-client', 'key', 'create', '--kdf', 'none', $encfile],
240 errmsg => 'failed to create encryption key'
241 );
242 return file_get_contents($encfile);
243 };
244
245 # Snapshot or group parameters can be either just a string and will then refer to the root
246 # namespace, or a tuple of `[namespace, snapshot]`.
247 my sub split_namespaced_parameter : prototype($) {
248 my ($snapshot) = @_;
249 return (undef, $snapshot) if !ref($snapshot);
250
251 (my $namespace, $snapshot) = @$snapshot;
252 return ($namespace, $snapshot);
253 }
254
255 # lists all snapshots, optionally limited to a specific group
256 sub get_snapshots {
257 my ($self, $group) = @_;
258
259 my $namespace;
260 if (defined($group)) {
261 ($namespace, $group) = split_namespaced_parameter($group);
262 }
263
264 my $param = [];
265 push @$param, $group if defined($group);
266
267 return run_client_cmd($self, "snapshots", $param, undef, undef, $namespace);
268 };
269
270 # create a new PXAR backup of a FS directory tree - doesn't cross FS boundary
271 # by default.
272 sub backup_fs_tree {
273 my ($self, $root, $id, $pxarname, $cmd_opts, $namespace) = @_;
274
275 die "backup-id not provided\n" if !defined($id);
276 die "backup root dir not provided\n" if !defined($root);
277 die "archive name not provided\n" if !defined($pxarname);
278
279 my $param = [
280 "$pxarname.pxar:$root",
281 '--backup-type', 'host',
282 '--backup-id', $id,
283 ];
284
285 $cmd_opts //= {};
286
287 $cmd_opts->{namespace} = $namespace if defined($namespace);
288
289 return run_raw_client_cmd($self, 'backup', $param, %$cmd_opts);
290 };
291
292 sub restore_pxar {
293 my ($self, $snapshot, $pxarname, $target, $cmd_opts) = @_;
294
295 die "snapshot not provided\n" if !defined($snapshot);
296 die "archive name not provided\n" if !defined($pxarname);
297 die "restore-target not provided\n" if !defined($target);
298
299 (my $namespace, $snapshot) = split_namespaced_parameter($snapshot);
300
301 my $param = [
302 "$snapshot",
303 "$pxarname.pxar",
304 "$target",
305 "--allow-existing-dirs", 0,
306 ];
307 $cmd_opts //= {};
308
309 $cmd_opts->{namespace} = $namespace;
310
311 return run_raw_client_cmd($self, 'restore', $param, %$cmd_opts);
312 };
313
314 sub forget_snapshot {
315 my ($self, $snapshot) = @_;
316
317 die "snapshot not provided\n" if !defined($snapshot);
318
319 (my $namespace, $snapshot) = split_namespaced_parameter($snapshot);
320
321 return run_raw_client_cmd($self, 'forget', ["$snapshot"], namespace => $namespace);
322 };
323
324 sub prune_group {
325 my ($self, $opts, $prune_opts, $group) = @_;
326
327 die "group not provided\n" if !defined($group);
328
329 (my $namespace, $group) = split_namespaced_parameter($group);
330
331 # do nothing if no keep options specified for remote
332 return [] if scalar(keys %$prune_opts) == 0;
333
334 my $param = [];
335
336 push @$param, "--quiet";
337
338 if (defined($opts->{'dry-run'}) && $opts->{'dry-run'}) {
339 push @$param, "--dry-run", $opts->{'dry-run'};
340 }
341
342 foreach my $keep_opt (keys %$prune_opts) {
343 push @$param, "--$keep_opt", $prune_opts->{$keep_opt};
344 }
345 push @$param, "$group";
346
347 return run_client_cmd($self, 'prune', $param, undef, undef, $namespace);
348 };
349
350 sub status {
351 my ($self) = @_;
352
353 my $total = 0;
354 my $free = 0;
355 my $used = 0;
356 my $active = 0;
357
358 eval {
359 my $res = run_client_cmd($self, "status");
360
361 $active = 1;
362 $total = $res->{total};
363 $used = $res->{used};
364 $free = $res->{avail};
365 };
366 if (my $err = $@) {
367 warn $err;
368 }
369
370 return ($total, $free, $used, $active);
371 };
372
373 sub file_restore_list {
374 my ($self, $snapshot, $filepath, $base64) = @_;
375
376 (my $namespace, $snapshot) = split_namespaced_parameter($snapshot);
377
378 return run_client_cmd(
379 $self,
380 "list",
381 [ $snapshot, $filepath, "--base64", $base64 ? 1 : 0 ],
382 0,
383 "proxmox-file-restore",
384 $namespace,
385 );
386 }
387
388 # call sync from API, returns a fifo path for streaming data to clients,
389 # pass it to file_restore_extract to start transfering data
390 sub file_restore_extract_prepare {
391 my ($self) = @_;
392
393 my $tmpdir = tempdir();
394 mkfifo("$tmpdir/fifo", 0600)
395 or die "creating file download fifo '$tmpdir/fifo' failed: $!\n";
396
397 # allow reading data for proxy user
398 my $wwwid = getpwnam('www-data') ||
399 die "getpwnam failed";
400 chown $wwwid, -1, "$tmpdir"
401 or die "changing permission on fifo dir '$tmpdir' failed: $!\n";
402 chown $wwwid, -1, "$tmpdir/fifo"
403 or die "changing permission on fifo '$tmpdir/fifo' failed: $!\n";
404
405 return "$tmpdir/fifo";
406 }
407
408 # this blocks while data is transfered, call this from a background worker
409 sub file_restore_extract {
410 my ($self, $output_file, $snapshot, $filepath, $base64) = @_;
411
412 (my $namespace, $snapshot) = split_namespaced_parameter($snapshot);
413
414 my $ret = eval {
415 local $SIG{ALRM} = sub { die "got timeout\n" };
416 alarm(30);
417 sysopen(my $fh, "$output_file", O_WRONLY)
418 or die "open target '$output_file' for writing failed: $!\n";
419 alarm(0);
420
421 my $fn = fileno($fh);
422 my $errfunc = sub { print $_[0], "\n"; };
423
424 return run_raw_client_cmd(
425 $self,
426 "extract",
427 [ $snapshot, $filepath, "-", "--base64", $base64 ? 1 : 0 ],
428 binary => "proxmox-file-restore",
429 namespace => $namespace,
430 errfunc => $errfunc,
431 output => ">&$fn",
432 );
433 };
434 my $err = $@;
435
436 unlink($output_file);
437 $output_file =~ s/fifo$//;
438 rmdir($output_file) if -d $output_file;
439
440 die "file restore task failed: $err" if $err;
441 return $ret;
442 }
443
444 1;