1 package PMG
::API2
::Subscription
;
9 use PVE
::Exception
qw(raise_param_exc);
11 use PMG
::RESTEnvironment
;
12 use PVE
::JSONSchema
qw(get_standard_option);
13 use PVE
::Subscription
;
18 use base
qw(PVE::RESTHandler);
20 PVE
::INotify
::register_file
('subscription', "/etc/pmg/subscription",
21 \
&read_etc_pmg_subscription
,
22 \
&write_etc_pmg_subscription
);
24 my $subscription_pattern = 'pmg([cbsp])-[0-9a-f]{10}';
27 my ($key, $noerr) = @_;
29 if ($key =~ m/^${subscription_pattern}$/) {
30 return $1 # subscription level
32 return undef if $noerr;
34 die "Wrong subscription key format\n";
37 sub read_etc_pmg_subscription
{
38 my ($filename, $fh) = @_;
40 my $server_id = PMG
::Utils
::get_hwaddress
();
42 my $info = PVE
::Subscription
::read_subscription
($server_id, $filename, $fh);
43 my $level = parse_key
($info->{key
});
45 if ($info->{status
} eq 'Active') {
46 $info->{level
} = $level;
52 sub write_etc_pmg_subscription
{
53 my ($filename, $fh, $info) = @_;
55 my $server_id = PMG
::Utils
::get_hwaddress
();
57 PVE
::Subscription
::write_subscription
($server_id, $filename, $fh, $info);
60 __PACKAGE__-
>register_method ({
64 description
=> "Read subscription info.",
66 permissions
=> { check
=> [ 'admin', 'qmanager', 'audit', 'quser'] },
68 additionalProperties
=> 0,
70 node
=> get_standard_option
('pve-node'),
73 returns
=> { type
=> 'object'},
77 my $server_id = PMG
::Utils
::get_hwaddress
();
78 my $url = "https://www.proxmox.com/proxmox-mail-gateway/pricing";
79 my $info = PVE
::INotify
::read_file
('subscription');
83 message
=> "There is no subscription key",
84 serverid
=> $server_id,
89 $info->{serverid
} = $server_id;
95 __PACKAGE__-
>register_method ({
99 description
=> "Update subscription info.",
102 permissions
=> { check
=> [ 'admin' ] },
104 additionalProperties
=> 0,
106 node
=> get_standard_option
('pve-node'),
108 description
=> "Always connect to server, even if we have up to date info inside local cache.",
115 returns
=> { type
=> 'null'},
119 my $info = PVE
::INotify
::read_file
('subscription');
120 return undef if !$info;
122 my $server_id = PMG
::Utils
::get_hwaddress
();
123 my $key = $info->{key
};
126 PVE
::Subscription
::update_apt_auth
($key, $server_id);
129 if (!$param->{force
} && $info->{status
} eq 'Active') {
130 my $age = time() - $info->{checktime
};
131 return undef if $age < $PVE::Subscription
::localkeydays
*60*60*24;
134 my $pmg_cfg = PMG
::Config-
>new();
135 my $proxy = $pmg_cfg->get('admin', 'http_proxy');
137 $info = PVE
::Subscription
::check_subscription
($key, $server_id, $proxy);
139 PVE
::INotify
::write_file
('subscription', $info);
144 __PACKAGE__-
>register_method ({
148 description
=> "Set subscription key.",
152 additionalProperties
=> 0,
154 node
=> get_standard_option
('pve-node'),
156 description
=> "Proxmox Mail Gateway subscription key",
158 pattern
=> $subscription_pattern,
163 returns
=> { type
=> 'null'},
167 my $key = PVE
::Tools
::trim
($param->{key
});
169 my $level = parse_key
($key);
177 my $server_id = PMG
::Utils
::get_hwaddress
();
179 PVE
::INotify
::write_file
('subscription', $info);
181 my $pmg_cfg = PMG
::Config-
>new();
182 my $proxy = $pmg_cfg->get('admin', 'http_proxy');
184 $info = PVE
::Subscription
::check_subscription
($key, $server_id, $proxy);
186 PVE
::INotify
::write_file
('subscription', $info);
191 __PACKAGE__-
>register_method ({
195 description
=> "Delete subscription key.",
199 additionalProperties
=> 0,
201 node
=> get_standard_option
('pve-node'),
204 returns
=> { type
=> 'null'},
206 my $subscription_file = '/etc/pmg/subscription';
207 return if ! -e
$subscription_file;
208 unlink($subscription_file) or die "cannot delete subscription key: $!";