]>
git.proxmox.com Git - pve-access-control.git/blob - src/PVE/Auth/LDAP.pm
4d771e7ff8adde4b3bd8a2c7168e0e8e8859d543
1 package PVE
::Auth
::LDAP
;
11 use base
qw(PVE::Auth::Plugin);
13 our $dn_regex = qr!\w+=("[\w ,+/<>;=]+"|[^ ,+"/<>;=]+)(,\s*\w+=("[\w ,+/<>;=]+"|[^ ,+"/<>;=]+))*!;
22 description
=> "LDAP base domain name",
29 description
=> "LDAP user attribute name",
36 description
=> "LDAP bind domain name",
43 description
=> "LDAP bind password. Will be stored in '/etc/pve/priv/realm/<REALM>.pw'.",
48 description
=> "Verify the server's SSL certificate",
54 description
=> "Path to the CA certificate store",
57 default => '/etc/ssl/certs',
60 description
=> "Path to the client certificate",
65 description
=> "Path to the client certificate key",
70 description
=> "LDAP filter for user sync.",
76 description
=> "Comma separated list of key=value pairs for specifying"
77 ." which LDAP attributes map to which PVE user field. For example,"
78 ." to map the LDAP attribute 'mail' to PVEs 'email', write "
79 ." 'email=mail'. By default, each PVE user field is represented "
80 ." by an LDAP attribute of the same name.",
83 pattern
=> '\w+=[^,]+(,\s*\w+=[^,]+)*',
86 description
=> "The objectclasses for users.",
88 default => 'inetorgperson, posixaccount, person, user',
89 format
=> 'ldap-simple-attr-list',
93 description
=> "LDAP base domain name for group sync. If not set, the"
94 ." base_dn will be used.",
101 description
=> "LDAP attribute representing a groups name. If not set"
102 ." or found, the first value of the DN will be used as name.",
104 format
=> 'ldap-simple-attr',
109 description
=> "LDAP filter for group sync.",
115 description
=> "The objectclasses for groups.",
117 default => 'groupOfNames, group, univentionGroup, ipausergroup',
118 format
=> 'ldap-simple-attr-list',
121 'sync-defaults-options' => {
122 description
=> "The default options for behavior of synchronizations.",
124 format
=> 'realm-sync-options',
128 description
=> "LDAP protocol mode.",
130 enum
=> [ 'ldap', 'ldaps', 'ldap+starttls'],
134 'case-sensitive' => {
135 description
=> "username is case-sensitive",
146 server2
=> { optional
=> 1 },
148 bind_dn
=> { optional
=> 1 },
149 password
=> { optional
=> 1 },
151 port
=> { optional
=> 1 },
152 secure
=> { optional
=> 1 },
153 sslversion
=> { optional
=> 1 },
154 default => { optional
=> 1 },
155 comment
=> { optional
=> 1 },
156 tfa
=> { optional
=> 1 },
157 verify
=> { optional
=> 1 },
158 capath
=> { optional
=> 1 },
159 cert
=> { optional
=> 1 },
160 certkey
=> { optional
=> 1 },
161 filter
=> { optional
=> 1 },
162 sync_attributes
=> { optional
=> 1 },
163 user_classes
=> { optional
=> 1 },
164 group_dn
=> { optional
=> 1 },
165 group_name_attr
=> { optional
=> 1 },
166 group_filter
=> { optional
=> 1 },
167 group_classes
=> { optional
=> 1 },
168 'sync-defaults-options' => { optional
=> 1 },
169 mode
=> { optional
=> 1 },
170 'case-sensitive' => { optional
=> 1 },
174 sub get_scheme_and_port
{
175 my ($class, $config) = @_;
177 my $scheme = $config->{mode
} // ($config->{secure
} ?
'ldaps' : 'ldap');
179 my $default_port = $scheme eq 'ldaps' ?
636 : 389;
180 my $port = $config->{port
} // $default_port;
182 return ($scheme, $port);
185 sub connect_and_bind
{
186 my ($class, $config, $realm) = @_;
188 my $servers = [$config->{server1
}];
189 push @$servers, $config->{server2
} if $config->{server2
};
191 my ($scheme, $port) = $class->get_scheme_and_port($config);
194 if ($config->{verify
}) {
195 $ldap_args{verify
} = 'require';
196 $ldap_args{clientcert
} = $config->{cert
} if $config->{cert
};
197 $ldap_args{clientkey
} = $config->{certkey
} if $config->{certkey
};
198 if (defined(my $capath = $config->{capath
})) {
200 $ldap_args{capath
} = $capath;
202 $ldap_args{cafile
} = $capath;
206 $ldap_args{verify
} = 'none';
209 if ($scheme ne 'ldap') {
210 $ldap_args{sslversion
} = $config->{sslversion
} || 'tlsv1_2';
213 my $ldap = PVE
::LDAP
::ldap_connect
($servers, $scheme, $port, \
%ldap_args);
215 if ($config->{bind_dn
}) {
216 my $bind_dn = $config->{bind_dn
};
217 my $bind_pass = ldap_get_credentials
($realm);
218 die "missing password for realm $realm\n" if !defined($bind_pass);
219 PVE
::LDAP
::ldap_bind
($ldap, $bind_dn, $bind_pass);
220 } elsif ($config->{cert
} && $config->{certkey
}) {
221 warn "skipping anonymous bind with clientcert\n";
223 PVE
::LDAP
::ldap_bind
($ldap);
226 if (!$config->{base_dn
}) {
227 my $root = $ldap->root_dse(attrs
=> [ 'defaultNamingContext' ]);
228 $config->{base_dn
} = $root->get_value('defaultNamingContext');
236 # 'username@realm' => {
237 # 'attr1' => 'value1',
238 # 'attr2' => 'value2',
244 # or in list context:
247 # 'username@realm' => {
248 # 'attr1' => 'value1',
249 # 'attr2' => 'value2',
255 # 'uid=username,dc=....' => 'username@realm',
259 # the map of dn->username is needed for group membership sync
261 my ($class, $config, $realm) = @_;
263 my $ldap = $class->connect_and_bind($config, $realm);
265 my $user_name_attr = $config->{user_attr
} // 'uid';
266 my $ldap_attribute_map = {
267 $user_name_attr => 'username',
270 firstname
=> 'firstname',
271 lastname
=> 'lastname',
273 comment
=> 'comment',
277 foreach my $attr (PVE
::Tools
::split_list
($config->{sync_attributes
})) {
278 my ($ours, $ldap) = ($attr =~ m/^\s*(\w+)=(.*)\s*$/);
279 $ldap_attribute_map->{$ldap} = $ours;
282 my $filter = $config->{filter
};
283 my $basedn = $config->{base_dn
};
285 $config->{user_classes
} //= 'inetorgperson, posixaccount, person, user';
286 my $classes = [PVE
::Tools
::split_list
($config->{user_classes
})];
288 my $users = PVE
::LDAP
::query_users
($ldap, $filter, [keys %$ldap_attribute_map], $basedn, $classes);
293 foreach my $user (@$users) {
294 my $user_attributes = $user->{attributes
};
295 my $userid = $user_attributes->{$user_name_attr}->[0];
296 my $username = "$userid\@$realm";
298 # we cannot sync usernames that do not meet our criteria
299 eval { PVE
::Auth
::Plugin
::verify_username
($username) };
305 $ret->{$username} = {};
307 foreach my $attr (keys %$user_attributes) {
308 if (my $ours = $ldap_attribute_map->{$attr}) {
309 $ret->{$username}->{$ours} = $user_attributes->{$attr}->[0];
314 my $dn = $user->{dn
};
315 $dnmap->{lc($dn)} = $username;
319 return wantarray ?
($ret, $dnmap) : $ret;
322 # needs a map for dn -> username, we get this from the get_users call
323 # otherwise we cannot determine the group membership
325 my ($class, $config, $realm, $dnmap) = @_;
327 my $filter = $config->{group_filter
};
328 my $basedn = $config->{group_dn
} // $config->{base_dn
};
329 my $attr = $config->{group_name_attr
};
330 $config->{group_classes
} //= 'groupOfNames, group, univentionGroup, ipausergroup';
331 my $classes = [PVE
::Tools
::split_list
($config->{group_classes
})];
333 my $ldap = $class->connect_and_bind($config, $realm);
335 my $groups = PVE
::LDAP
::query_groups
($ldap, $basedn, $classes, $filter, $attr);
339 foreach my $group (@$groups) {
340 my $name = $group->{name
};
341 if (!$name && $group->{dn
} =~ m/^[^=]+=([^,]+),/){
342 $name = PVE
::Tools
::trim
($1);
347 # we cannot sync groups that do not meet our criteria
348 eval { PVE
::AccessControl
::verify_groupname
($name) };
354 $ret->{$name} = { users
=> {} };
355 foreach my $member (@{$group->{members
}}) {
356 if (my $user = $dnmap->{lc($member)}) {
357 $ret->{$name}->{users
}->{$user} = 1;
366 sub authenticate_user
{
367 my ($class, $config, $realm, $username, $password) = @_;
369 my $ldap = $class->connect_and_bind($config, $realm);
371 my $user_dn = PVE
::LDAP
::get_user_dn
($ldap, $username, $config->{user_attr
}, $config->{base_dn
});
372 PVE
::LDAP
::auth_user_dn
($ldap, $user_dn, $password);
378 my $ldap_pw_dir = "/etc/pve/priv/realm";
380 sub ldap_cred_file_name
{
382 return "${ldap_pw_dir}/${realmid}.pw";
388 my $cred_file = ldap_cred_file_name
($realmid);
391 } elsif (-e
"/etc/pve/priv/ldap/${realmid}.pw") {
392 # FIXME: remove fallback with 7.0 by doing a rename on upgrade from 6.x
393 return "/etc/pve/priv/ldap/${realmid}.pw";
399 sub ldap_set_credentials
{
400 my ($password, $realmid) = @_;
402 my $cred_file = ldap_cred_file_name
($realmid);
405 PVE
::Tools
::file_set_contents
($cred_file, $password);
410 sub ldap_get_credentials
{
413 if (my $cred_file = get_cred_file
($realmid)) {
414 return PVE
::Tools
::file_read_firstline
($cred_file);
419 sub ldap_delete_credentials
{
422 if (my $cred_file = get_cred_file
($realmid)) {
423 return if ! -e
$cred_file; # nothing to do
424 unlink($cred_file) or warn "removing LDAP credentials '$cred_file' failed: $!\n";
429 my ($class, $realm, $config, %param) = @_;
431 if (defined($param{password
})) {
432 ldap_set_credentials
($param{password
}, $realm);
434 ldap_delete_credentials
($realm);
439 my ($class, $realm, $config, %param) = @_;
441 return if !exists($param{password
});
443 if (defined($param{password
})) {
444 ldap_set_credentials
($param{password
}, $realm);
446 ldap_delete_credentials
($realm);
451 my ($class, $realm, $config) = @_;
453 ldap_delete_credentials
($realm);