]> git.proxmox.com Git - pmg-api.git/blame - PMG/LDAPConfig.pm
PMG/Cluster.pm: add rsync helpers to sync quarantine files
[pmg-api.git] / PMG / LDAPConfig.pm
CommitLineData
a6e3ac60
DM
1package PMG::LDAPConfig;
2
3use strict;
4use warnings;
49a16f65 5use MIME::Base64;
a6e3ac60
DM
6use Data::Dumper;
7
8use PVE::Tools;
9use PVE::JSONSchema qw(get_standard_option);
10use PVE::INotify;
11use PVE::SectionConfig;
12
13use base qw(PVE::SectionConfig);
14
7d90f962
DM
15my $inotify_file_id = 'pmg-ldap.conf';
16my $config_filename = '/etc/pmg/ldap.conf';
17
a6e3ac60
DM
18my $defaultData = {
19 propertyList => {
20 type => { description => "Section type." },
c2ef4490 21 profile => {
2aeda4ac 22 description => "Profile ID.",
a6e3ac60
DM
23 type => 'string', format => 'pve-configid',
24 },
2fdba966
DM
25 },
26};
27
28sub properties {
29 return {
1c4fa5b1
DM
30 disable => {
31 description => "Flag to disable/deactivate the entry.",
32 type => 'boolean',
33 optional => 1,
34 },
bfed5777
DM
35 comment => {
36 description => "Description.",
37 type => 'string',
38 optional => 1,
39 maxLength => 4096,
40 },
a6e3ac60
DM
41 mode => {
42 description => "LDAP protocol mode ('ldap' or 'ldaps').",
43 type => 'string',
44 enum => ['ldap', 'ldaps'],
45 default => 'ldap',
46 },
49a16f65
DM
47 server1 => {
48 description => "Server address.",
49 type => 'string', format => 'address',
bfed5777 50 maxLength => 256,
49a16f65
DM
51 },
52 server2 => {
53 description => "Fallback server address. Userd when the first server is not available.",
54 type => 'string', format => 'address',
bfed5777 55 maxLength => 256,
49a16f65
DM
56 },
57 port => {
58 description => "Specify the port to connect to.",
59 type => 'integer',
60 minimum => 1,
61 maximum => 65535,
62 },
63 binddn => {
64 description => "Bind domain name.",
65 type => 'string',
66 },
67 bindpw => {
68 description => "Bind password.",
69 type => 'string',
70 },
71 basedn => {
72 description => "Base domain name.",
73 type => 'string',
74 },
75 groupbasedn => {
76 description => "Base domain name for groups.",
77 type => 'string',
78 },
79 filter => {
80 description => "LDAP filter.",
81 type => 'string',
82 },
83 accountattr => {
84 description => "Account attribute name name.",
85 type => 'string',
86 pattern => '[a-zA-Z0-9]+',
87 default => 'sAMAccountName',
88 },
89 mailattr => {
90 description => "List of mail attribute names.",
e1c64277 91 type => 'string', format => 'string-list',
49a16f65
DM
92 pattern => '[a-zA-Z0-9]+',
93 default => "mail, userPrincipalName, proxyAddresses, othermailbox",
94 },
2fdba966
DM
95 };
96}
a6e3ac60
DM
97
98sub options {
99 return {
ff4776b6 100 disable => { optional => 1 },
bfed5777 101 comment => { optional => 1 },
49a16f65
DM
102 server1 => { optional => 0 },
103 server2 => { optional => 1 },
104 port => { optional => 1 },
a6e3ac60 105 mode => { optional => 1 },
49a16f65
DM
106 binddn => { optional => 1 },
107 bindpw => { optional => 1 },
108 basedn => { optional => 1 },
109 groupbasedn => { optional => 1 },
110 filter => { optional => 1 },
111 accountattr => { optional => 1 },
112 mailattr => { optional => 1 },
a6e3ac60
DM
113 };
114}
115
116sub type {
117 return 'ldap';
118}
119
120sub private {
121 return $defaultData;
122}
123
05b856e3
DM
124sub parse_section_header {
125 my ($class, $line) = @_;
126
127 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
c2ef4490 128 my ($type, $profileId) = ($1, $2);
05b856e3 129 my $errmsg = undef; # set if you want to skip whole section
c2ef4490 130 eval { PVE::JSONSchema::pve_verify_configid($profileId); };
05b856e3
DM
131 $errmsg = $@ if $@;
132 my $config = {}; # to return additional attributes
c2ef4490 133 return ($type, $profileId, $errmsg, $config);
05b856e3
DM
134 }
135 return undef;
136}
137
bfed5777
DM
138sub parse_config {
139 my ($class, $filename, $raw) = @_;
140
141 my $cfg = $class->SUPER::parse_config($filename, $raw);
142
c2ef4490
DM
143 foreach my $profile (keys %{$cfg->{ids}}) {
144 my $data = $cfg->{ids}->{$profile};
bfed5777
DM
145
146 $data->{comment} = PVE::Tools::decode_text($data->{comment})
147 if defined($data->{comment});
148
149 $data->{bindpw} = decode_base64($data->{bindpw})
150 if defined($data->{bindpw});
151 }
152
153 return $cfg;
154}
155
156sub write_config {
157 my ($class, $filename, $cfg) = @_;
158
c2ef4490
DM
159 foreach my $profile (keys %{$cfg->{ids}}) {
160 my $data = $cfg->{ids}->{$profile};
bfed5777
DM
161
162 $data->{comment} = PVE::Tools::encode_text($data->{comment})
163 if defined($data->{comment});
164
165 $data->{bindpw} = encode_base64($data->{bindpw}, '')
166 if defined($data->{bindpw});
167 }
168
169 $class->SUPER::write_config($filename, $cfg);
170}
171
7d90f962
DM
172sub new {
173 my ($type) = @_;
174
175 my $class = ref($type) || $type;
176
177 my $cfg = PVE::INotify::read_file($inotify_file_id);
178
179 return bless $cfg, $class;
180}
181
182sub write {
183 my ($self) = @_;
184
185 PVE::INotify::write_file($inotify_file_id, $self);
186}
187
e1c64277
DM
188my $lockfile = "/var/lock/pmgldapconfig.lck";
189
190sub lock_config {
191 my ($code, $errmsg) = @_;
192
193 my $p = PVE::Tools::lock_file($lockfile, undef, $code);
194 if (my $err = $@) {
195 $errmsg ? die "$errmsg: $err" : die $err;
196 }
197}
198
49a16f65 199
a6e3ac60
DM
200__PACKAGE__->register();
201__PACKAGE__->init();
202
203sub read_pmg_ldap_conf {
204 my ($filename, $fh) = @_;
205
206 local $/ = undef; # slurp mode
207
bdbc2bc5 208 my $raw = defined($fh) ? <$fh> : '';
a6e3ac60
DM
209
210 return __PACKAGE__->parse_config($filename, $raw);
211}
212
213sub write_pmg_ldap_conf {
214 my ($filename, $fh, $cfg) = @_;
215
216 my $raw = __PACKAGE__->write_config($filename, $cfg);
217
bdbc2bc5
DM
218 my $gid = getgrnam('www-data');
219 chown(0, $gid, $fh);
220 chmod(0640, $fh);
d5121ced 221
a6e3ac60
DM
222 PVE::Tools::safe_print($filename, $fh, $raw);
223}
224
7d90f962 225PVE::INotify::register_file($inotify_file_id, $config_filename,
a6e3ac60 226 \&read_pmg_ldap_conf,
bdbc2bc5
DM
227 \&write_pmg_ldap_conf,
228 undef,
229 always_call_parser => 1);
a6e3ac60
DM
230
231
2321;