]> git.proxmox.com Git - proxmox-acme.git/blame - src/PVE/ACME/DNSChallenge.pm
move DNS plugin schema to separate JSON based file
[proxmox-acme.git] / src / PVE / ACME / DNSChallenge.pm
CommitLineData
98b96d9e
WL
1package PVE::ACME::DNSChallenge;
2
3use strict;
4use warnings;
5
6use Digest::SHA qw(sha256);
4195bf0a
TL
7use JSON;
8
98b96d9e
WL
9use PVE::Tools;
10
c617455e
WB
11use PVE::ACME;
12
98b96d9e
WL
13use base qw(PVE::ACME::Challenge);
14
15my $ACME_PATH = '/usr/share/proxmox-acme/proxmox-acme';
16
17sub supported_challenge_types {
122626b3 18 return ["dns-01"];
98b96d9e
WL
19}
20
21sub type {
22 return 'dns';
23}
24
4195bf0a
TL
25my $DNS_API_CHALLENGE_SCHEMA_FN = '/usr/share/proxmox-acme/dns-challenge-schema.json';
26my $plugins = from_json(PVE::Tools::file_get_contents($DNS_API_CHALLENGE_SCHEMA_FN));
6f5be4aa 27
e3924936 28sub get_supported_plugins {
6f5be4aa 29 return $plugins;
e3924936 30}
98b96d9e
WL
31
32sub properties {
33 return {
34 api => {
35 description => "API plugin name",
36 type => 'string',
6f5be4aa 37 enum => [sort keys %$plugins],
98b96d9e
WL
38 },
39 data => {
40 type => 'string',
6372e898 41 description => 'DNS plugin data. (base64 encoded)',
98b96d9e 42 },
4317ba99
TL
43 'validation-delay' => {
44 type => 'integer',
45 description => 'Extra delay in seconds to wait before requesting validation.'
46 .' Allows to cope with a long TTL of DNS records.',
47 # low default, but our bet is that the acme-challenge domain isn't
48 # cached at all, so it hopefully shouldn't run into TTL issues
49 default => 30,
50 optional => 1,
51 minimum => 0,
52 maximum => 2 * 24 * 60 * 60,
53 }
98b96d9e
WL
54 };
55}
56
57sub options {
58 return {
59 api => {},
13b63882 60 data => { optional => 1 },
98b96d9e
WL
61 nodes => { optional => 1 },
62 disable => { optional => 1 },
4317ba99 63 'validation-delay' => { optional => 1 },
98b96d9e
WL
64 };
65}
66
f00829fd
FG
67my $proxmox_acme_command = sub {
68 my ($self, $acme, $auth, $data, $action) = @_;
98b96d9e
WL
69
70 die "No plugin data for DNSChallenge\n" if !defined($data->{plugin});
f00829fd
FG
71
72 my $alias = $data->{alias};
73 my $domain = $auth->{identifier}->{value};
74
75 my $challenge = $self->extract_challenge($auth->{challenges});
76 my $key_auth = $acme->key_authorization($challenge->{token});
77
78 my $txtvalue = PVE::ACME::encode(sha256($key_auth));
98b96d9e
WL
79 my $dnsplugin = $data->{plugin}->{api};
80 my $plugin_conf_string = $data->{plugin}->{data};
81
82 # for security reasons, we execute the command as nobody
83 # we can't verify that the code of the DNSPlugins are harmless.
f0ed0733 84 my $cmd = ["setpriv", "--reuid", "nobody", "--regid", "nogroup", "--clear-groups", "--reset-env", "--"];
98b96d9e 85
f00829fd
FG
86 # The order of the parameters passed to proxmox-acme is important
87 # proxmox-acme <setup|teardown> $plugin <$domain|$alias> $txtvalue [$plugin_conf_string]
88 push @$cmd, "/bin/bash", $ACME_PATH, $action, $dnsplugin;
89 if ($alias) {
90 push @$cmd, $alias;
91 } else {
92 push @$cmd, $domain;
93 }
13bc64ea
FG
94 my $input = "$txtvalue\n";
95 $input .= "$plugin_conf_string\n" if $plugin_conf_string;
f00829fd 96
13bc64ea 97 PVE::Tools::run_command($cmd, input => $input);
f00829fd
FG
98
99 $data->{url} = $challenge->{url};
100
101 return $domain;
102};
103
104sub setup {
105 my ($self, $acme, $auth, $data) = @_;
106
107 my $domain = $proxmox_acme_command->($self, $acme, $auth, $data, 'setup');
98b96d9e 108 print "Add TXT record: _acme-challenge.$domain\n";
4317ba99 109
1192b595 110 my $delay = $data->{plugin}->{'validation-delay'} // 30;
4317ba99
TL
111 if ($delay > 0) {
112 print "Sleeping $delay seconds to wait for TXT record propagation\n";
113 sleep($delay); # don't care for EINTR
114 }
98b96d9e
WL
115}
116
98b96d9e 117sub teardown {
f00829fd 118 my ($self, $acme, $auth, $data) = @_;
98b96d9e 119
f00829fd 120 my $domain = $proxmox_acme_command->($self, $acme, $auth, $data, 'teardown');
98b96d9e
WL
121 print "Remove TXT record: _acme-challenge.$domain\n";
122}
123
1241;