]> git.proxmox.com Git - pmg-api.git/blame - src/PMG/CLI/pmgconfig.pm
add scheduled backup to PBS remotes
[pmg-api.git] / src / PMG / CLI / pmgconfig.pm
CommitLineData
f983300f
DM
1package PMG::CLI::pmgconfig;
2
3use strict;
4use warnings;
5use IO::File;
6use Data::Dumper;
7
8use PVE::SafeSyslog;
9use PVE::Tools qw(extract_param);
10use PVE::INotify;
11use PVE::CLIHandler;
12
80a43e18 13use PMG::RESTEnvironment;
ca35eda1
DM
14use PMG::RuleDB;
15use PMG::RuleCache;
f983300f 16use PMG::Cluster;
d79b9b0c 17use PMG::LDAPConfig;
f983300f
DM
18use PMG::LDAPSet;
19use PMG::Config;
bc44eb02 20use PMG::Ticket;
7c70f29e 21use PMG::API2::DKIMSign;
f983300f
DM
22
23use base qw(PVE::CLIHandler);
24
80a43e18
DM
25sub setup_environment {
26 PMG::RESTEnvironment->setup_default_cli_env();
27}
28
f983300f 29__PACKAGE__->register_method ({
7a10bd95
DM
30 name => 'dump',
31 path => 'dump',
f983300f 32 method => 'POST',
7a10bd95
DM
33 description => "Print configuration setting which can be used in templates.",
34 parameters => {
35 additionalProperties => 0,
36 properties => {},
37 },
38 returns => { type => 'null'},
39 code => sub {
40 my ($param) = @_;
41
42 my $cfg = PMG::Config->new();
43 my $vars = $cfg->get_template_vars();
44
45 foreach my $realm (sort keys %$vars) {
46 foreach my $section (sort keys %{$vars->{$realm}}) {
47 my $secvalue = $vars->{$realm}->{$section} // '';
48 if (ref($secvalue)) {
49 foreach my $key (sort keys %{$vars->{$realm}->{$section}}) {
50 my $value = $vars->{$realm}->{$section}->{$key} // '';
51 print "$realm.$section.$key = $value\n";
52 }
53 } else {
54 print "$realm.$section = $secvalue\n";
55 }
56 }
57 }
58
59 return undef;
60 }});
61
62__PACKAGE__->register_method ({
63 name => 'sync',
64 path => 'sync',
65 method => 'POST',
66 description => "Syncronize Proxmox Mail Gateway configurations with system configuration.",
f983300f
DM
67 parameters => {
68 additionalProperties => 0,
69 properties => {
7a10bd95
DM
70 restart => {
71 description => "Restart services if necessary.",
f983300f
DM
72 type => 'boolean',
73 default => 0,
74 optional => 1,
75 },
76 },
77 },
78 returns => { type => 'null'},
79 code => sub {
80 my ($param) = @_;
81
82 my $cfg = PMG::Config->new();
ca35eda1
DM
83
84 my $ruledb = PMG::RuleDB->new();
85 my $rulecache = PMG::RuleCache->new($ruledb);
86
87 $cfg->rewrite_config($rulecache, $param->{restart});
f983300f 88
7a10bd95
DM
89 return undef;
90 }});
f983300f 91
7a10bd95
DM
92__PACKAGE__->register_method ({
93 name => 'ldapsync',
94 path => 'ldapsync',
95 method => 'POST',
96 description => "Syncronize the LDAP database.",
97 parameters => {
98 additionalProperties => 0,
99 properties => {},
100 },
101 returns => { type => 'null'},
102 code => sub {
103 my ($param) = @_;
f983300f 104
7a10bd95
DM
105 my $ldap_cfg = PVE::INotify::read_file("pmg-ldap.conf");
106 PMG::LDAPSet::ldap_resync($ldap_cfg, 1);
f983300f
DM
107
108 return undef;
109 }});
110
bc44eb02
DM
111__PACKAGE__->register_method ({
112 name => 'apicert',
113 path => 'apicert',
114 method => 'POST',
115 description => "Generate /etc/pmg/pmg-api.pem (self signed certificate for GUI and REST API).",
116 parameters => {
117 additionalProperties => 0,
118 properties => {
119 force => {
0b257970 120 description => "Overwrite existing certificate.",
bc44eb02
DM
121 type => 'boolean',
122 optional => 1,
123 default => 0,
124 },
125 },
126 },
127 returns => { type => 'null'},
128 code => sub {
129 my ($param) = @_;
130
131 PMG::Ticket::generate_api_cert($param->{force});
132
133 return undef;
134 }});
135
136__PACKAGE__->register_method ({
137 name => 'tlscert',
138 path => 'tlscert',
139 method => 'POST',
140 description => "Generate /etc/pmg/pmg-tls.pem (self signed certificate for encrypted SMTP traffic).",
141 parameters => {
142 additionalProperties => 0,
143 properties => {
144 force => {
0b257970 145 description => "Overwrite existing certificate.",
bc44eb02
DM
146 type => 'boolean',
147 optional => 1,
148 default => 0,
149 },
150 },
151 },
152 returns => { type => 'null'},
153 code => sub {
154 my ($param) = @_;
155
156 PMG::Utils::gen_proxmox_tls_cert($param->{force});
157
158 return undef;
159 }});
160
ec5c426d
DM
161__PACKAGE__->register_method ({
162 name => 'init',
163 path => 'init',
164 method => 'POST',
165 description => "Generate required files in /etc/pmg/",
166 parameters => {
167 additionalProperties => 0,
168 properties => {},
169 },
170 returns => { type => 'null'},
171 code => sub {
172 my ($param) = @_;
173
174 my $cfg = PMG::Config->new();
175
fb2955bd 176 PMG::Ticket::generate_api_cert();
ec5c426d
DM
177 PMG::Ticket::generate_csrf_key();
178 PMG::Ticket::generate_auth_key();
ca35eda1 179
ec5c426d 180 if ($cfg->get('mail', 'tls')) {
fb2955bd 181 PMG::Utils::gen_proxmox_tls_cert();
ec5c426d 182 }
ca35eda1 183
ec5c426d
DM
184 return undef;
185 }});
186
7a10bd95
DM
187our $cmddef = {
188 'dump' => [ __PACKAGE__, 'dump', []],
189 sync => [ __PACKAGE__, 'sync', []],
190 ldapsync => [ __PACKAGE__, 'ldapsync', []],
bc44eb02
DM
191 apicert => [ __PACKAGE__, 'apicert', []],
192 tlscert => [ __PACKAGE__, 'tlscert', []],
ec5c426d 193 init => [ __PACKAGE__, 'init', []],
7c70f29e
SI
194 dkim_set => [ 'PMG::API2::DKIMSign', 'set_selector', []],
195 dkim_record => [ 'PMG::API2::DKIMSign', 'get_selector_info', [], undef,
196 sub {
197 my ($res) = @_;
19fe5184 198 die "no dkim_selector configured\n" if !defined($res->{record});
7c70f29e
SI
199 print "$res->{record}\n";
200 }],
7a10bd95 201};
f983300f
DM
202
203
2041;