]>
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", title
=> "User ID",
80 type
=> 'string', format
=> 'pve-userid',
84 PVE
::JSONSchema
::register_format
('pve-tfa-config', \
&verify_tfa_config
);
85 sub verify_tfa_config
{
86 my ($value, $noerr) = @_;
88 return $value if parse_tfa_config
($value);
90 return undef if $noerr;
92 die "unable to parse tfa option\n";
95 PVE
::JSONSchema
::register_standard_option
('tfa', {
96 description
=> "Use Two-factor authentication.",
97 type
=> 'string', format
=> 'pve-tfa-config',
102 sub parse_tfa_config
{
107 foreach my $kvp (split(/,/, $data)) {
109 if ($kvp =~ m/^type=(yubico|oath)$/) {
111 } elsif ($kvp =~ m/^id=(\S+)$/) {
113 } elsif ($kvp =~ m/^key=(\S+)$/) {
115 } elsif ($kvp =~ m/^url=(\S+)$/) {
117 } elsif ($kvp =~ m/^digits=([6|7|8])$/) {
119 } elsif ($kvp =~ m/^step=([1-9]\d+)$/) {
126 return undef if !$res->{type
};
133 type
=> { description
=> "Realm type." },
134 realm
=> get_standard_option
('realm'),
142 sub parse_section_header
{
143 my ($class, $line) = @_;
145 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
146 my ($type, $realm) = (lc($1), $2);
147 my $errmsg = undef; # set if you want to skip whole section
148 eval { pve_verify_realm
($realm); };
150 my $config = {}; # to return additional attributes
151 return ($type, $realm, $errmsg, $config);
157 my ($class, $filename, $raw) = @_;
159 my $cfg = $class->SUPER::parse_config
($filename, $raw);
162 foreach my $realm (keys %{$cfg->{ids
}}) {
163 my $data = $cfg->{ids
}->{$realm};
164 # make sure there is only one default marker
165 if ($data->{default}) {
167 delete $data->{default};
173 if ($data->{comment
}) {
174 $data->{comment
} = PVE
::Tools
::decode_text
($data->{comment
});
179 # add default domains
181 $cfg->{ids
}->{pve
}->{type
} = 'pve'; # force type
182 $cfg->{ids
}->{pve
}->{comment
} = "Proxmox VE authentication server"
183 if !$cfg->{ids
}->{pve
}->{comment
};
185 $cfg->{ids
}->{pam
}->{type
} = 'pam'; # force type
186 $cfg->{ids
}->{pam
}->{plugin
} = 'PVE::Auth::PAM';
187 $cfg->{ids
}->{pam
}->{comment
} = "Linux PAM standard authentication"
188 if !$cfg->{ids
}->{pam
}->{comment
};
194 my ($class, $filename, $cfg) = @_;
196 foreach my $realm (keys %{$cfg->{ids
}}) {
197 my $data = $cfg->{ids
}->{$realm};
198 if ($data->{comment
}) {
199 $data->{comment
} = PVE
::Tools
::encode_text
($data->{comment
});
203 $class->SUPER::write_config
($filename, $cfg);
206 sub authenticate_user
{
207 my ($class, $config, $realm, $username, $password) = @_;
213 my ($class, $config, $realm, $username, $password) = @_;
215 my $type = $class->type();
217 die "can't set password on auth type '$type'\n";
221 my ($class, $config, $realm, $username) = @_;
223 # do nothing by default