]> git.proxmox.com Git - pmg-api.git/blame - PMG/API2/MyNetworks.pm
reload postfix instead of restart on config change
[pmg-api.git] / PMG / API2 / MyNetworks.pm
CommitLineData
bef31f06
DM
1package PMG::API2::MyNetworks;
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 of trusted networks from where SMTP clients are allowed to relay mail through Proxmox Mail Gateway.",
23 proxyto => 'master',
defb9add 24 permissions => { check => [ 'admin', 'audit' ] },
bef31f06
DM
25 parameters => {
26 additionalProperties => 0,
27 properties => {},
28 },
29 returns => {
30 type => 'array',
31 items => {
32 type => "object",
33 properties => {
34 cidr => { type => 'string'},
35 },
36 },
96af1c94 37 links => [ { rel => 'child', href => "{cidr}" } ],
bef31f06
DM
38 },
39 code => sub {
40 my ($param) = @_;
41
42 my $mynetworks = PVE::INotify::read_file('mynetworks');
43
44 my $res = [];
45
46 foreach my $cidr (sort keys %$mynetworks) {
47 push @$res, $mynetworks->{$cidr};
48 }
49
50 return $res;
51 }});
52
53__PACKAGE__->register_method ({
54 name => 'create',
55 path => '',
56 method => 'POST',
57 proxyto => 'master',
58 protected => 1,
defb9add 59 permissions => { check => [ 'admin' ] },
bef31f06
DM
60 description => "Add a trusted network.",
61 parameters => {
62 additionalProperties => 0,
63 properties => {
64 cidr => {
65 description => "IPv4 or IPv6 network in CIDR notation.",
66 type => 'string', format => 'CIDR',
67 },
68 comment => {
69 description => "Comment.",
70 type => 'string',
71 optional => 1,
72 },
73 },
74 },
75 returns => { type => 'null' },
76 code => sub {
77 my ($param) = @_;
78
79 my $code = sub {
80
81 my $mynetworks = PVE::INotify::read_file('mynetworks');
82
83 die "trusted network '$param->{cidr}' already exists\n"
84 if $mynetworks->{$param->{cidr}};
85
86 $mynetworks->{$param->{cidr}} = {
87 comment => $param->{comment} // '',
88 };
89
90 PVE::INotify::write_file('mynetworks', $mynetworks);
91
6d473888
DM
92 my $cfg = PMG::Config->new();
93
94 if ($cfg->rewrite_config_postfix()) {
2473cb81 95 PMG::Utils::service_cmd('postfix', 'reload');
6d473888 96 }
bef31f06
DM
97 };
98
99 PMG::Config::lock_config($code, "add trusted network failed");
100
101 return undef;
102 }});
103
104__PACKAGE__->register_method ({
105 name => 'read',
106 path => '{cidr}',
107 method => 'GET',
108 description => "Read trusted network data (comment).",
109 proxyto => 'master',
defb9add 110 permissions => { check => [ 'admin', 'audit' ] },
bef31f06
DM
111 parameters => {
112 additionalProperties => 0,
113 properties => {
114 cidr => {
115 description => "IPv4 or IPv6 network in CIDR notation.",
116 type => 'string', format => 'CIDR',
117 },
118 },
119 },
120 returns => {
121 type => "object",
122 properties => {
123 cidr => { type => 'string'},
124 comment => { type => 'string'},
125 },
126 },
127 code => sub {
128 my ($param) = @_;
129
130 my $mynetworks = PVE::INotify::read_file('mynetworks');
131
132 die "trusted network '$param->{cidr}' does not exist\n"
133 if !$mynetworks->{$param->{cidr}};
134
135 return $mynetworks->{$param->{cidr}}
136 }});
137
138__PACKAGE__->register_method ({
139 name => 'write',
140 path => '{cidr}',
141 method => 'PUT',
142 description => "Update trusted data (comment).",
143 protected => 1,
defb9add 144 permissions => { check => [ 'admin' ] },
bef31f06
DM
145 proxyto => 'master',
146 parameters => {
147 additionalProperties => 0,
148 properties => {
149 cidr => {
150 description => "IPv4 or IPv6 network in CIDR notation.",
151 type => 'string', #format => 'CIDR',
152 },
153 comment => {
154 description => "Comment.",
155 type => 'string',
156 },
157 },
158 },
159 returns => { type => 'null' },
160 code => sub {
161 my ($param) = @_;
162
163 my $code = sub {
164
165 my $mynetworks = PVE::INotify::read_file('mynetworks');
166
167 die "trusted network '$param->{cidr}' does not exist\n"
168 if !$mynetworks->{$param->{cidr}};
169
170 $mynetworks->{$param->{cidr}}->{comment} = $param->{comment};
171
172 PVE::INotify::write_file('mynetworks', $mynetworks);
bef31f06
DM
173 };
174
175 PMG::Config::lock_config($code, "update trusted network failed");
176
177 return undef;
178 }});
179
180__PACKAGE__->register_method ({
181 name => 'delete',
182 path => '{cidr}',
183 method => 'DELETE',
184 description => "Delete a truster network",
185 protected => 1,
defb9add 186 permissions => { check => [ 'admin' ] },
bef31f06
DM
187 proxyto => 'master',
188 parameters => {
189 additionalProperties => 0,
190 properties => {
191 cidr => {
192 description => "IPv4 or IPv6 network in CIDR notation.",
193 type => 'string', format => 'CIDR',
194 },
195 }
196 },
197 returns => { type => 'null' },
198 code => sub {
199 my ($param) = @_;
200
201 my $code = sub {
202
203 my $mynetworks = PVE::INotify::read_file('mynetworks');
204
205 die "trusted network '$param->{cidr}' does not exist\n"
206 if !$mynetworks->{$param->{cidr}};
207
208 delete $mynetworks->{$param->{cidr}};
209
210 PVE::INotify::write_file('mynetworks', $mynetworks);
211
6d473888
DM
212 my $cfg = PMG::Config->new();
213
214 if ($cfg->rewrite_config_postfix()) {
2473cb81 215 PMG::Utils::service_cmd('postfix', 'reload');
6d473888 216 }
bef31f06
DM
217 };
218
219 PMG::Config::lock_config($code, "delete trusted network failed");
220
221 return undef;
222 }});
223
2241;