]>
git.proxmox.com Git - pve-access-control.git/blob - src/PVE/Auth/LDAP.pm
1 package PVE
::Auth
::LDAP
;
11 use base
qw(PVE::Auth::Plugin);
13 my $dn_part_regex = qr!("[^"]+"|[^ ,+"/<>;=#][^,+"/<>;=]*[^ ,+"/<>;=]|[^ ,+"/<>;=#])!;
14 our $dn_regex = qr!\w+=${dn_part_regex}(,\s*\w+=${dn_part_regex})*!;
23 description
=> "LDAP base domain name",
30 description
=> "LDAP user attribute name",
37 description
=> "LDAP bind domain name",
44 description
=> "LDAP bind password. Will be stored in '/etc/pve/priv/realm/<REALM>.pw'.",
49 description
=> "Verify the server's SSL certificate",
55 description
=> "Path to the CA certificate store",
58 default => '/etc/ssl/certs',
61 description
=> "Path to the client certificate",
66 description
=> "Path to the client certificate key",
71 description
=> "LDAP filter for user sync.",
77 description
=> "Comma separated list of key=value pairs for specifying"
78 ." which LDAP attributes map to which PVE user field. For example,"
79 ." to map the LDAP attribute 'mail' to PVEs 'email', write "
80 ." 'email=mail'. By default, each PVE user field is represented "
81 ." by an LDAP attribute of the same name.",
84 pattern
=> '\w+=[^,]+(,\s*\w+=[^,]+)*',
87 description
=> "The objectclasses for users.",
89 default => 'inetorgperson, posixaccount, person, user',
90 format
=> 'ldap-simple-attr-list',
94 description
=> "LDAP base domain name for group sync. If not set, the"
95 ." base_dn will be used.",
102 description
=> "LDAP attribute representing a groups name. If not set"
103 ." or found, the first value of the DN will be used as name.",
105 format
=> 'ldap-simple-attr',
110 description
=> "LDAP filter for group sync.",
116 description
=> "The objectclasses for groups.",
118 default => 'groupOfNames, group, univentionGroup, ipausergroup',
119 format
=> 'ldap-simple-attr-list',
122 'sync-defaults-options' => {
123 description
=> "The default options for behavior of synchronizations.",
125 format
=> 'realm-sync-options',
129 description
=> "LDAP protocol mode.",
131 enum
=> [ 'ldap', 'ldaps', 'ldap+starttls'],
135 'case-sensitive' => {
136 description
=> "username is case-sensitive",
147 server2
=> { optional
=> 1 },
149 bind_dn
=> { optional
=> 1 },
150 password
=> { optional
=> 1 },
152 port
=> { optional
=> 1 },
153 secure
=> { optional
=> 1 },
154 sslversion
=> { optional
=> 1 },
155 default => { optional
=> 1 },
156 comment
=> { optional
=> 1 },
157 tfa
=> { optional
=> 1 },
158 verify
=> { optional
=> 1 },
159 capath
=> { optional
=> 1 },
160 cert
=> { optional
=> 1 },
161 certkey
=> { optional
=> 1 },
162 filter
=> { optional
=> 1 },
163 sync_attributes
=> { optional
=> 1 },
164 user_classes
=> { optional
=> 1 },
165 group_dn
=> { optional
=> 1 },
166 group_name_attr
=> { optional
=> 1 },
167 group_filter
=> { optional
=> 1 },
168 group_classes
=> { optional
=> 1 },
169 'sync-defaults-options' => { optional
=> 1 },
170 mode
=> { optional
=> 1 },
171 'case-sensitive' => { optional
=> 1 },
175 sub get_scheme_and_port
{
176 my ($class, $config) = @_;
178 my $scheme = $config->{mode
} // ($config->{secure
} ?
'ldaps' : 'ldap');
180 my $default_port = $scheme eq 'ldaps' ?
636 : 389;
181 my $port = $config->{port
} // $default_port;
183 return ($scheme, $port);
186 sub connect_and_bind
{
187 my ($class, $config, $realm) = @_;
189 my $servers = [$config->{server1
}];
190 push @$servers, $config->{server2
} if $config->{server2
};
192 my ($scheme, $port) = $class->get_scheme_and_port($config);
195 if ($config->{verify
}) {
196 $ldap_args{verify
} = 'require';
197 $ldap_args{clientcert
} = $config->{cert
} if $config->{cert
};
198 $ldap_args{clientkey
} = $config->{certkey
} if $config->{certkey
};
199 if (defined(my $capath = $config->{capath
})) {
201 $ldap_args{capath
} = $capath;
203 $ldap_args{cafile
} = $capath;
207 $ldap_args{verify
} = 'none';
210 if ($scheme ne 'ldap') {
211 $ldap_args{sslversion
} = $config->{sslversion
} || 'tlsv1_2';
214 my $ldap = PVE
::LDAP
::ldap_connect
($servers, $scheme, $port, \
%ldap_args);
216 if ($config->{bind_dn
}) {
217 my $bind_dn = $config->{bind_dn
};
218 my $bind_pass = ldap_get_credentials
($realm);
219 die "missing password for realm $realm\n" if !defined($bind_pass);
220 PVE
::LDAP
::ldap_bind
($ldap, $bind_dn, $bind_pass);
221 } elsif ($config->{cert
} && $config->{certkey
}) {
222 warn "skipping anonymous bind with clientcert\n";
224 PVE
::LDAP
::ldap_bind
($ldap);
227 if (!$config->{base_dn
}) {
228 my $root = $ldap->root_dse(attrs
=> [ 'defaultNamingContext' ]);
229 $config->{base_dn
} = $root->get_value('defaultNamingContext');
237 # 'username@realm' => {
238 # 'attr1' => 'value1',
239 # 'attr2' => 'value2',
245 # or in list context:
248 # 'username@realm' => {
249 # 'attr1' => 'value1',
250 # 'attr2' => 'value2',
256 # 'uid=username,dc=....' => 'username@realm',
260 # the map of dn->username is needed for group membership sync
262 my ($class, $config, $realm) = @_;
264 my $ldap = $class->connect_and_bind($config, $realm);
266 my $user_name_attr = $config->{user_attr
} // 'uid';
267 my $ldap_attribute_map = {
268 $user_name_attr => 'username',
271 firstname
=> 'firstname',
272 lastname
=> 'lastname',
274 comment
=> 'comment',
278 foreach my $attr (PVE
::Tools
::split_list
($config->{sync_attributes
})) {
279 my ($ours, $ldap) = ($attr =~ m/^\s*(\w+)=(.*)\s*$/);
280 $ldap_attribute_map->{$ldap} = $ours;
283 my $filter = $config->{filter
};
284 my $basedn = $config->{base_dn
};
286 $config->{user_classes
} //= 'inetorgperson, posixaccount, person, user';
287 my $classes = [PVE
::Tools
::split_list
($config->{user_classes
})];
289 my $users = PVE
::LDAP
::query_users
($ldap, $filter, [keys %$ldap_attribute_map], $basedn, $classes);
294 foreach my $user (@$users) {
295 my $user_attributes = $user->{attributes
};
296 my $userid = $user_attributes->{$user_name_attr}->[0];
297 my $username = "$userid\@$realm";
299 # we cannot sync usernames that do not meet our criteria
300 eval { PVE
::Auth
::Plugin
::verify_username
($username) };
306 $ret->{$username} = {};
308 foreach my $attr (keys %$user_attributes) {
309 if (my $ours = $ldap_attribute_map->{$attr}) {
310 $ret->{$username}->{$ours} = $user_attributes->{$attr}->[0];
315 my $dn = $user->{dn
};
316 $dnmap->{lc($dn)} = $username;
320 return wantarray ?
($ret, $dnmap) : $ret;
323 # needs a map for dn -> username, we get this from the get_users call
324 # otherwise we cannot determine the group membership
326 my ($class, $config, $realm, $dnmap) = @_;
328 my $filter = $config->{group_filter
};
329 my $basedn = $config->{group_dn
} // $config->{base_dn
};
330 my $attr = $config->{group_name_attr
};
331 $config->{group_classes
} //= 'groupOfNames, group, univentionGroup, ipausergroup';
332 my $classes = [PVE
::Tools
::split_list
($config->{group_classes
})];
334 my $ldap = $class->connect_and_bind($config, $realm);
336 my $groups = PVE
::LDAP
::query_groups
($ldap, $basedn, $classes, $filter, $attr);
340 foreach my $group (@$groups) {
341 my $name = $group->{name
};
342 if (!$name && $group->{dn
} =~ m/^[^=]+=([^,]+),/){
343 $name = PVE
::Tools
::trim
($1);
348 # we cannot sync groups that do not meet our criteria
349 eval { PVE
::AccessControl
::verify_groupname
($name) };
355 $ret->{$name} = { users
=> {} };
356 foreach my $member (@{$group->{members
}}) {
357 if (my $user = $dnmap->{lc($member)}) {
358 $ret->{$name}->{users
}->{$user} = 1;
367 sub authenticate_user
{
368 my ($class, $config, $realm, $username, $password) = @_;
370 my $ldap = $class->connect_and_bind($config, $realm);
372 my $user_dn = PVE
::LDAP
::get_user_dn
($ldap, $username, $config->{user_attr
}, $config->{base_dn
});
373 PVE
::LDAP
::auth_user_dn
($ldap, $user_dn, $password);
379 my $ldap_pw_dir = "/etc/pve/priv/realm";
381 sub ldap_cred_file_name
{
383 return "${ldap_pw_dir}/${realmid}.pw";
389 my $cred_file = ldap_cred_file_name
($realmid);
392 } elsif (-e
"/etc/pve/priv/ldap/${realmid}.pw") {
393 # FIXME: remove fallback with 7.0 by doing a rename on upgrade from 6.x
394 return "/etc/pve/priv/ldap/${realmid}.pw";
400 sub ldap_set_credentials
{
401 my ($password, $realmid) = @_;
403 my $cred_file = ldap_cred_file_name
($realmid);
406 PVE
::Tools
::file_set_contents
($cred_file, $password);
411 sub ldap_get_credentials
{
414 if (my $cred_file = get_cred_file
($realmid)) {
415 return PVE
::Tools
::file_read_firstline
($cred_file);
420 sub ldap_delete_credentials
{
423 if (my $cred_file = get_cred_file
($realmid)) {
424 return if ! -e
$cred_file; # nothing to do
425 unlink($cred_file) or warn "removing LDAP credentials '$cred_file' failed: $!\n";
430 my ($class, $realm, $config, %param) = @_;
432 if (defined($param{password
})) {
433 ldap_set_credentials
($param{password
}, $realm);
435 ldap_delete_credentials
($realm);
440 my ($class, $realm, $config, %param) = @_;
442 return if !exists($param{password
});
444 if (defined($param{password
})) {
445 ldap_set_credentials
($param{password
}, $realm);
447 ldap_delete_credentials
($realm);
452 my ($class, $realm, $config) = @_;
454 ldap_delete_credentials
($realm);