]>
Commit | Line | Data |
---|---|---|
87952a8d DM |
1 | package PMG::API2::Subscription; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
726cfb99 FG |
6 | use Proxmox::RS::Subscription; |
7 | ||
87952a8d DM |
8 | use PVE::Tools; |
9 | use PVE::SafeSyslog; | |
10 | use PVE::INotify; | |
11 | use PVE::Exception qw(raise_param_exc); | |
12 | use PVE::RESTHandler; | |
13 | use PMG::RESTEnvironment; | |
14 | use PVE::JSONSchema qw(get_standard_option); | |
15 | ||
16 | use PMG::Utils; | |
d34cada7 | 17 | use PMG::Config; |
87952a8d DM |
18 | |
19 | use base qw(PVE::RESTHandler); | |
20 | ||
d34cada7 | 21 | my $subscription_pattern = 'pmg([cbsp])-[0-9a-f]{10}'; |
726cfb99 | 22 | my $filename = "/etc/pmg/subscription"; |
d34cada7 DM |
23 | |
24 | sub parse_key { | |
25 | my ($key, $noerr) = @_; | |
26 | ||
27 | if ($key =~ m/^${subscription_pattern}$/) { | |
28 | return $1 # subscription level | |
29 | } | |
30 | return undef if $noerr; | |
31 | ||
32 | die "Wrong subscription key format\n"; | |
33 | } | |
34 | ||
726cfb99 | 35 | sub read_etc_subscription { |
d34cada7 DM |
36 | my $server_id = PMG::Utils::get_hwaddress(); |
37 | ||
726cfb99 | 38 | my $info = Proxmox::RS::Subscription::read_subscription($filename); |
a9cbbb5b | 39 | return $info if !$info; |
726cfb99 | 40 | |
d34cada7 DM |
41 | my $level = parse_key($info->{key}); |
42 | ||
726cfb99 | 43 | if ($info->{status} eq 'active') { |
d34cada7 DM |
44 | $info->{level} = $level; |
45 | } | |
87952a8d DM |
46 | |
47 | return $info; | |
d34cada7 | 48 | }; |
87952a8d | 49 | |
726cfb99 FG |
50 | sub write_etc_subscription { |
51 | my ($info) = @_; | |
87952a8d | 52 | |
d34cada7 | 53 | my $server_id = PMG::Utils::get_hwaddress(); |
87952a8d | 54 | |
726cfb99 | 55 | Proxmox::RS::Subscription::write_subscription($filename, "/etc/apt/auth.conf.d/pmg.conf", "enterprise.proxmox.com/debian/pmg", $info); |
87952a8d DM |
56 | } |
57 | ||
58 | __PACKAGE__->register_method ({ | |
59 | name => 'get', | |
60 | path => '', | |
61 | method => 'GET', | |
62 | description => "Read subscription info.", | |
63 | proxyto => 'node', | |
64 | permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] }, | |
65 | parameters => { | |
66 | additionalProperties => 0, | |
67 | properties => { | |
68 | node => get_standard_option('pve-node'), | |
69 | }, | |
70 | }, | |
71 | returns => { type => 'object'}, | |
72 | code => sub { | |
73 | my ($param) = @_; | |
74 | ||
75 | my $server_id = PMG::Utils::get_hwaddress(); | |
ecf6a63e | 76 | my $url = "https://www.proxmox.com/proxmox-mail-gateway/pricing"; |
726cfb99 | 77 | my $info = read_etc_subscription(); |
87952a8d DM |
78 | if (!$info) { |
79 | return { | |
726cfb99 | 80 | status => "notfound", |
87952a8d DM |
81 | message => "There is no subscription key", |
82 | serverid => $server_id, | |
83 | url => $url, | |
84 | } | |
85 | } | |
86 | ||
87 | $info->{serverid} = $server_id; | |
87952a8d DM |
88 | $info->{url} = $url; |
89 | ||
90 | return $info | |
91 | }}); | |
92 | ||
d34cada7 DM |
93 | __PACKAGE__->register_method ({ |
94 | name => 'update', | |
95 | path => '', | |
96 | method => 'POST', | |
97 | description => "Update subscription info.", | |
98 | proxyto => 'node', | |
99 | protected => 1, | |
b894a99a | 100 | permissions => { check => [ 'admin' ] }, |
d34cada7 DM |
101 | parameters => { |
102 | additionalProperties => 0, | |
103 | properties => { | |
104 | node => get_standard_option('pve-node'), | |
105 | force => { | |
106 | description => "Always connect to server, even if we have up to date info inside local cache.", | |
107 | type => 'boolean', | |
108 | optional => 1, | |
109 | default => 0 | |
110 | } | |
111 | }, | |
112 | }, | |
113 | returns => { type => 'null'}, | |
114 | code => sub { | |
115 | my ($param) = @_; | |
116 | ||
726cfb99 | 117 | my $info = read_etc_subscription(); |
d34cada7 DM |
118 | return undef if !$info; |
119 | ||
120 | my $server_id = PMG::Utils::get_hwaddress(); | |
121 | my $key = $info->{key}; | |
122 | ||
726cfb99 FG |
123 | # key has been recently checked |
124 | return undef | |
125 | if !$param->{force} | |
126 | && $info->{status} eq 'active' | |
127 | && Proxmox::RS::Subscription::check_age($info, 1)->{status} eq 'active'; | |
d34cada7 DM |
128 | |
129 | my $pmg_cfg = PMG::Config->new(); | |
130 | my $proxy = $pmg_cfg->get('admin', 'http_proxy'); | |
131 | ||
726cfb99 | 132 | $info = Proxmox::RS::Subscription::check_subscription($key, $server_id, "", "Proxmox Mail Gateway", $proxy); |
d34cada7 | 133 | |
726cfb99 | 134 | write_etc_subscription($info); |
d34cada7 DM |
135 | |
136 | return undef; | |
137 | }}); | |
138 | ||
139 | __PACKAGE__->register_method ({ | |
140 | name => 'set', | |
141 | path => '', | |
142 | method => 'PUT', | |
143 | description => "Set subscription key.", | |
144 | proxyto => 'node', | |
145 | protected => 1, | |
146 | parameters => { | |
147 | additionalProperties => 0, | |
148 | properties => { | |
149 | node => get_standard_option('pve-node'), | |
150 | key => { | |
151 | description => "Proxmox Mail Gateway subscription key", | |
152 | type => 'string', | |
153 | pattern => $subscription_pattern, | |
154 | maxLength => 32, | |
155 | }, | |
156 | }, | |
157 | }, | |
158 | returns => { type => 'null'}, | |
159 | code => sub { | |
160 | my ($param) = @_; | |
161 | ||
162 | my $key = PVE::Tools::trim($param->{key}); | |
163 | ||
164 | my $level = parse_key($key); | |
165 | ||
166 | my $info = { | |
726cfb99 | 167 | status => 'new', |
d34cada7 DM |
168 | key => $key, |
169 | checktime => time(), | |
170 | }; | |
171 | ||
172 | my $server_id = PMG::Utils::get_hwaddress(); | |
173 | ||
726cfb99 | 174 | write_etc_subscription($info); |
d34cada7 DM |
175 | |
176 | my $pmg_cfg = PMG::Config->new(); | |
177 | my $proxy = $pmg_cfg->get('admin', 'http_proxy'); | |
178 | ||
726cfb99 | 179 | $info = Proxmox::RS::Subscription::check_subscription($key, $server_id, "", "Proxmox Mail Gateway", $proxy); |
d34cada7 | 180 | |
726cfb99 | 181 | write_etc_subscription($info); |
d34cada7 DM |
182 | |
183 | return undef; | |
184 | }}); | |
185 | ||
da74ec9f MA |
186 | __PACKAGE__->register_method ({ |
187 | name => 'delete', | |
188 | path => '', | |
189 | method => 'DELETE', | |
190 | description => "Delete subscription key.", | |
191 | proxyto => 'node', | |
192 | protected => 1, | |
193 | parameters => { | |
194 | additionalProperties => 0, | |
195 | properties => { | |
196 | node => get_standard_option('pve-node'), | |
197 | }, | |
198 | }, | |
199 | returns => { type => 'null'}, | |
200 | code => sub { | |
726cfb99 FG |
201 | return if ! -e $filename; |
202 | unlink($filename) or die "cannot delete subscription key: $!"; | |
da74ec9f MA |
203 | return undef; |
204 | }}); | |
205 | ||
87952a8d | 206 | 1; |