]> git.proxmox.com Git - pmg-api.git/blame - PMG/API2/Transport.pm
fix #1673: Allow email addresses in transport maps
[pmg-api.git] / PMG / API2 / Transport.pm
CommitLineData
3118b703
DM
1package PMG::API2::Transport;
2
3use strict;
4use warnings;
5use Data::Dumper;
6
7use PVE::SafeSyslog;
8use PVE::Tools qw(extract_param);
9use HTTP::Status qw(:constants);
10use PVE::JSONSchema qw(get_standard_option);
11use PVE::RESTHandler;
12use PVE::INotify;
13
14use PMG::Config;
15
16use base qw(PVE::RESTHandler);
17
18__PACKAGE__->register_method ({
19 name => 'index',
20 path => '',
21 method => 'GET',
22 description => "List transport map entries.",
23 proxyto => 'master',
e6e62522 24 permissions => { check => [ 'admin', 'audit' ] },
3118b703
DM
25 parameters => {
26 additionalProperties => 0,
27 properties => {},
28 },
29 returns => {
30 type => 'array',
31 items => {
32 type => "object",
33 properties => {
34 domain => { type => 'string' },
35 host => { type => 'string' },
36 port => { type => 'integer' },
53904163 37 use_mx => { type => 'boolean' },
3118b703
DM
38 comment => { type => 'string'},
39 },
40 },
cc4512f6 41 links => [ { rel => 'child', href => "{domain}" } ],
3118b703
DM
42 },
43 code => sub {
44 my ($param) = @_;
45
46 my $tmap = PVE::INotify::read_file('transport');
47
48 my $res = [];
49
50 foreach my $domain (sort keys %$tmap) {
51 push @$res, $tmap->{$domain};
52 }
53
54 return $res;
55 }});
56
57__PACKAGE__->register_method ({
58 name => 'create',
59 path => '',
60 method => 'POST',
61 proxyto => 'master',
62 protected => 1,
e6e62522 63 permissions => { check => [ 'admin' ] },
3118b703
DM
64 description => "Add transport map entry.",
65 parameters => {
66 additionalProperties => 0,
67 properties => {
68 domain => {
69 description => "Domain name.",
22c25daf 70 type => 'string', format => 'transport-domain-or-email',
3118b703
DM
71 },
72 host => {
73 description => "Target host (name or IP address).",
74 type => 'string', format => 'address',
75 },
76 port => {
77 description => "SMTP port.",
78 type => 'integer',
79 minimum => 1,
80 maximum => 65535,
81 optional => 1,
82 default => 25,
83 },
53904163
DM
84 use_mx => {
85 description => "Enable MX lookups.",
3118b703
DM
86 type => 'boolean',
87 optional => 1,
53904163 88 default => 1,
3118b703
DM
89 },
90 comment => {
91 description => "Comment.",
92 type => 'string',
93 optional => 1,
94 },
95 },
96 },
97 returns => { type => 'null' },
98 code => sub {
99 my ($param) = @_;
100
101 my $code = sub {
102
103 my $tmap = PVE::INotify::read_file('transport');
104
105 die "Transport map entry '$param->{domain}' already exists\n"
106 if $tmap->{$param->{domain}};
107
108 $tmap->{$param->{domain}} = {
109 domain => $param->{domain},
110 host => $param->{host},
111 port => $param->{port} // 25,
53904163 112 use_mx => $param->{use_mx} // 1,
3118b703
DM
113 comment => $param->{comment} // '',
114 };
115
116 PVE::INotify::write_file('transport', $tmap);
117
118 PMG::Config::postmap_pmg_transport();
119 };
120
121 PMG::Config::lock_config($code, "add transport map entry failed");
122
123 return undef;
124 }});
125
126__PACKAGE__->register_method ({
127 name => 'read',
128 path => '{domain}',
129 method => 'GET',
130 description => "Read transport map entry.",
131 proxyto => 'master',
e6e62522 132 permissions => { check => [ 'admin', 'audit' ] },
3118b703
DM
133 parameters => {
134 additionalProperties => 0,
135 properties => {
136 domain => {
137 description => "Domain name.",
22c25daf 138 type => 'string', format => 'transport-domain-or-email',
3118b703
DM
139 },
140 },
141 },
142 returns => {
143 type => "object",
144 properties => {
145 domain => { type => 'string'},
146 host => { type => 'string'},
147 port => { type => 'integer'},
53904163 148 use_mx => { type => 'boolean'},
3118b703
DM
149 comment => { type => 'string'},
150 },
151 },
152 code => sub {
153 my ($param) = @_;
154
155 my $tmap = PVE::INotify::read_file('transport');
156
157 if (my $entry = $tmap->{$param->{domain}}) {
158 return $entry;
159 }
160
161 die "Transport map entry '$param->{domain}' does not exist\n";
162 }});
163
164__PACKAGE__->register_method ({
165 name => 'write',
166 path => '{domain}',
167 method => 'PUT',
168 description => "Update transport map entry.",
169 protected => 1,
e6e62522 170 permissions => { check => [ 'admin' ] },
3118b703
DM
171 proxyto => 'master',
172 parameters => {
173 additionalProperties => 0,
174 properties => {
175 domain => {
176 description => "Domain name.",
22c25daf 177 type => 'string', format => 'transport-domain-or-email',
3118b703
DM
178 },
179 host => {
180 description => "Target host (name or IP address).",
181 type => 'string', format => 'address',
182 optional => 1,
183 },
184 port => {
185 description => "SMTP port.",
186 type => 'integer',
187 minimum => 1,
188 maximum => 65535,
189 optional => 1,
190 },
53904163
DM
191 use_mx => {
192 description => "Enable MX lookups.",
3118b703
DM
193 type => 'boolean',
194 optional => 1,
195 },
196 comment => {
197 description => "Comment.",
198 type => 'string',
199 optional => 1,
200 },
201 },
202 },
203 returns => { type => 'null' },
204 code => sub {
205 my ($param) = @_;
206
207 my $code = sub {
208
209 my $tmap = PVE::INotify::read_file('transport');
210
211 my $domain = extract_param($param, 'domain');
212
213 my $data = $tmap->{$domain};
214
215 die "Transport map entry '$param->{domain}' does not exist\n" if !$data;
216
217 die "no options specified\n" if !scalar(keys %$param);
218
53904163 219 for my $prop (qw(host port use_mx comment)) {
3118b703
DM
220 $data->{$prop} = $param->{$prop} if defined($param->{$prop});
221 }
222
223 PVE::INotify::write_file('transport', $tmap);
224
225 PMG::Config::postmap_pmg_transport();
226 };
227
228 PMG::Config::lock_config($code, "update transport map entry failed");
229
230 return undef;
231 }});
232
233__PACKAGE__->register_method ({
234 name => 'delete',
235 path => '{domain}',
236 method => 'DELETE',
237 description => "Delete a transport map entry",
238 protected => 1,
e6e62522 239 permissions => { check => [ 'admin' ] },
3118b703
DM
240 proxyto => 'master',
241 parameters => {
242 additionalProperties => 0,
243 properties => {
244 domain => {
245 description => "Domain name.",
22c25daf 246 type => 'string', format => 'transport-domain-or-email',
3118b703
DM
247 },
248 }
249 },
250 returns => { type => 'null' },
251 code => sub {
252 my ($param) = @_;
253
254 my $code = sub {
255
256 my $tmap = PVE::INotify::read_file('transport');
257
258 die "Transport map entry '$param->{domain}' does not exist\n"
259 if !$tmap->{$param->{domain}};
260
261 delete $tmap->{$param->{domain}};
262
263 PVE::INotify::write_file('transport', $tmap);
264
265 PMG::Config::postmap_pmg_transport();
266 };
267
268 PMG::Config::lock_config($code, "delete transport map extry failed");
269
270 return undef;
271 }});
272
2731;