3356f691c55d1b5ff8bc3e1b5c5a54e63ac57a8e
1 package PVE
::Auth
::Plugin
;
8 use PVE
::SectionConfig
;
9 use PVE
::JSONSchema
qw(get_standard_option);
10 use PVE
::Cluster
qw(cfs_register_file cfs_read_file cfs_lock_file);
14 use base
qw(PVE::SectionConfig);
16 my $domainconfigfile = "domains.cfg";
18 cfs_register_file
($domainconfigfile,
19 sub { __PACKAGE__-
>parse_config(@_); },
20 sub { __PACKAGE__-
>write_config(@_); });
22 sub lock_domain_config
{
23 my ($code, $errmsg) = @_;
25 cfs_lock_file
($domainconfigfile, undef, $code);
28 $errmsg ?
die "$errmsg: $err" : die $err;
32 my $realm_regex = qr/[A-Za-z][A-Za-z0-9\.\-_]+/;
34 PVE
::JSONSchema
::register_format
('pve-realm', \
&pve_verify_realm
);
35 sub pve_verify_realm
{
36 my ($realm, $noerr) = @_;
38 if ($realm !~ m/^${realm_regex}$/) {
39 return undef if $noerr;
40 die "value does not look like a valid realm\n";
45 PVE
::JSONSchema
::register_standard_option
('realm', {
46 description
=> "Authentication domain ID",
47 type
=> 'string', format
=> 'pve-realm',
51 PVE
::JSONSchema
::register_format
('pve-userid', \
&verify_username
);
53 my ($username, $noerr) = @_;
55 $username = '' if !$username;
56 my $len = length($username);
58 die "user name '$username' is too short\n" if !$noerr;
62 die "user name '$username' is too long ($len > 64)\n" if !$noerr;
66 # we only allow a limited set of characters
67 # colon is not allowed, because we store usernames in
68 # colon separated lists)!
69 # slash is not allowed because it is used as pve API delimiter
70 # also see "man useradd"
71 if ($username =~ m!^([^\s:/]+)\@(${realm_regex})$!) {
72 return wantarray ?
($username, $1, $2) : $username;
75 die "value '$username' does not look like a valid user name\n" if !$noerr;
80 PVE
::JSONSchema
::register_standard_option
('userid', {
81 description
=> "User ID",
82 type
=> 'string', format
=> 'pve-userid',
86 PVE
::JSONSchema
::register_format
('pve-tfa-config', \
&verify_tfa_config
);
87 sub verify_tfa_config
{
88 my ($value, $noerr) = @_;
90 return $value if parse_tfa_config
($value);
92 return undef if $noerr;
94 die "unable to parse tfa option\n";
97 PVE
::JSONSchema
::register_standard_option
('tfa', {
98 description
=> "Use Two-factor authentication.",
99 type
=> 'string', format
=> 'pve-tfa-config',
104 sub parse_tfa_config
{
109 foreach my $kvp (split(/,/, $data)) {
111 if ($kvp =~ m/^type=(yubico|oath)$/) {
113 } elsif ($kvp =~ m/^id=(\S+)$/) {
115 } elsif ($kvp =~ m/^key=(\S+)$/) {
117 } elsif ($kvp =~ m/^url=(\S+)$/) {
119 } elsif ($kvp =~ m/^digits=([6|7|8])$/) {
121 } elsif ($kvp =~ m/^step=([1-9]\d+)$/) {
128 return undef if !$res->{type
};
133 my $salt_starter = time();
139 my $salt = substr(Digest
::SHA
::sha1_base64
(time() + $salt_starter + $$), 0, 8);
141 # crypt does not want '+' in salt (see 'man crypt')
144 return crypt(encode
("utf8", $pw), "\$5\$$salt\$");
149 type
=> { description
=> "Realm type." },
150 realm
=> get_standard_option
('realm'),
158 sub parse_section_header
{
159 my ($class, $line) = @_;
161 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
162 my ($type, $realm) = (lc($1), $2);
163 my $errmsg = undef; # set if you want to skip whole section
164 eval { pve_verify_realm
($realm); };
166 my $config = {}; # to return additional attributes
167 return ($type, $realm, $errmsg, $config);
173 my ($class, $filename, $raw) = @_;
175 my $cfg = $class->SUPER::parse_config
($filename, $raw);
178 foreach my $realm (keys %{$cfg->{ids
}}) {
179 my $data = $cfg->{ids
}->{$realm};
180 # make sure there is only one default marker
181 if ($data->{default}) {
183 delete $data->{default};
189 if ($data->{comment
}) {
190 $data->{comment
} = PVE
::Tools
::decode_text
($data->{comment
});
195 # add default domains
197 $cfg->{ids
}->{pve
}->{type
} = 'pve'; # force type
198 $cfg->{ids
}->{pve
}->{comment
} = "Proxmox VE authentication server"
199 if !$cfg->{ids
}->{pve
}->{comment
};
201 $cfg->{ids
}->{pam
}->{type
} = 'pam'; # force type
202 $cfg->{ids
}->{pam
}->{plugin
} = 'PVE::Auth::PAM';
203 $cfg->{ids
}->{pam
}->{comment
} = "Linux PAM standard authentication"
204 if !$cfg->{ids
}->{pam
}->{comment
};
210 my ($class, $filename, $cfg) = @_;
212 foreach my $realm (keys %{$cfg->{ids
}}) {
213 my $data = $cfg->{ids
}->{$realm};
214 if ($data->{comment
}) {
215 $data->{comment
} = PVE
::Tools
::encode_text
($data->{comment
});
219 $class->SUPER::write_config
($filename, $cfg);
222 sub authenticate_user
{
223 my ($class, $config, $realm, $username, $password) = @_;
229 my ($class, $config, $realm, $username, $password) = @_;
231 my $type = $class->type();
233 die "can't set password on auth type '$type'\n";
237 my ($class, $config, $realm, $username) = @_;
239 # do nothing by default