]>
git.proxmox.com Git - pve-access-control.git/blob - PVE/Auth/Plugin.pm
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);
12 use base
qw(PVE::SectionConfig);
14 my $domainconfigfile = "domains.cfg";
16 cfs_register_file
($domainconfigfile,
17 sub { __PACKAGE__-
>parse_config(@_); },
18 sub { __PACKAGE__-
>write_config(@_); });
20 sub lock_domain_config
{
21 my ($code, $errmsg) = @_;
23 cfs_lock_file
($domainconfigfile, undef, $code);
26 $errmsg ?
die "$errmsg: $err" : die $err;
30 my $realm_regex = qr/[A-Za-z][A-Za-z0-9\.\-_]+/;
32 PVE
::JSONSchema
::register_format
('pve-realm', \
&pve_verify_realm
);
33 sub pve_verify_realm
{
34 my ($realm, $noerr) = @_;
36 if ($realm !~ m/^${realm_regex}$/) {
37 return undef if $noerr;
38 die "value does not look like a valid realm\n";
43 PVE
::JSONSchema
::register_standard_option
('realm', {
44 description
=> "Authentication domain ID",
45 type
=> 'string', format
=> 'pve-realm',
49 PVE
::JSONSchema
::register_format
('pve-userid', \
&verify_username
);
51 my ($username, $noerr) = @_;
53 $username = '' if !$username;
54 my $len = length($username);
56 die "user name '$username' is too short\n" if !$noerr;
60 die "user name '$username' is too long ($len > 64)\n" if !$noerr;
64 # we only allow a limited set of characters
65 # colon is not allowed, because we store usernames in
66 # colon separated lists)!
67 # slash is not allowed because it is used as pve API delimiter
68 # also see "man useradd"
69 if ($username =~ m!^([^\s:/]+)\@(${realm_regex})$!) {
70 return wantarray ?
($username, $1, $2) : $username;
73 die "value '$username' does not look like a valid user name\n" if !$noerr;
78 PVE
::JSONSchema
::register_standard_option
('userid', {
79 description
=> "User ID",
80 type
=> 'string', format
=> 'pve-userid',
86 description
=> "The type of 2nd factor authentication.",
87 format_description
=> 'TFATYPE',
89 enum
=> [qw(yubico oath)],
92 description
=> "Yubico API ID.",
93 format_description
=> 'ID',
98 description
=> "Yubico API Key.",
99 format_description
=> 'KEY',
104 description
=> "Yubico API URL.",
105 format_description
=> 'URL',
110 description
=> "TOTP digits.",
111 format_description
=> 'COUNT',
113 minimum
=> 6, maximum
=> 8,
118 description
=> "TOTP time period.",
119 format_description
=> 'SECONDS',
127 PVE
::JSONSchema
::register_format
('pve-tfa-config', $tfa_format);
129 PVE
::JSONSchema
::register_standard_option
('tfa', {
130 description
=> "Use Two-factor authentication.",
131 type
=> 'string', format
=> 'pve-tfa-config',
136 sub parse_tfa_config
{
139 return PVE
::JSONSchema
::parse_property_string
($tfa_format, $data);
144 type
=> { description
=> "Realm type." },
145 realm
=> get_standard_option
('realm'),
153 sub parse_section_header
{
154 my ($class, $line) = @_;
156 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
157 my ($type, $realm) = (lc($1), $2);
158 my $errmsg = undef; # set if you want to skip whole section
159 eval { pve_verify_realm
($realm); };
161 my $config = {}; # to return additional attributes
162 return ($type, $realm, $errmsg, $config);
168 my ($class, $filename, $raw) = @_;
170 my $cfg = $class->SUPER::parse_config
($filename, $raw);
173 foreach my $realm (keys %{$cfg->{ids
}}) {
174 my $data = $cfg->{ids
}->{$realm};
175 # make sure there is only one default marker
176 if ($data->{default}) {
178 delete $data->{default};
184 if ($data->{comment
}) {
185 $data->{comment
} = PVE
::Tools
::decode_text
($data->{comment
});
190 # add default domains
192 $cfg->{ids
}->{pve
}->{type
} = 'pve'; # force type
193 $cfg->{ids
}->{pve
}->{comment
} = "Proxmox VE authentication server"
194 if !$cfg->{ids
}->{pve
}->{comment
};
196 $cfg->{ids
}->{pam
}->{type
} = 'pam'; # force type
197 $cfg->{ids
}->{pam
}->{plugin
} = 'PVE::Auth::PAM';
198 $cfg->{ids
}->{pam
}->{comment
} = "Linux PAM standard authentication"
199 if !$cfg->{ids
}->{pam
}->{comment
};
205 my ($class, $filename, $cfg) = @_;
207 foreach my $realm (keys %{$cfg->{ids
}}) {
208 my $data = $cfg->{ids
}->{$realm};
209 if ($data->{comment
}) {
210 $data->{comment
} = PVE
::Tools
::encode_text
($data->{comment
});
214 $class->SUPER::write_config
($filename, $cfg);
217 sub authenticate_user
{
218 my ($class, $config, $realm, $username, $password) = @_;
224 my ($class, $config, $realm, $username, $password) = @_;
226 my $type = $class->type();
228 die "can't set password on auth type '$type'\n";
232 my ($class, $config, $realm, $username) = @_;
234 # do nothing by default