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