]>
Commit | Line | Data |
---|---|---|
1 | package PVE::AccessControl; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Encode; | |
6 | use Crypt::OpenSSL::Random; | |
7 | use Crypt::OpenSSL::RSA; | |
8 | use Net::SSLeay; | |
9 | use Net::IP; | |
10 | use MIME::Base64; | |
11 | use Digest::SHA; | |
12 | use URI::Escape; | |
13 | use LWP::UserAgent; | |
14 | use PVE::Tools qw(run_command lock_file file_get_contents split_list safe_print); | |
15 | use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file); | |
16 | use PVE::JSONSchema; | |
17 | ||
18 | use PVE::Auth::Plugin; | |
19 | use PVE::Auth::AD; | |
20 | use PVE::Auth::LDAP; | |
21 | use PVE::Auth::PVE; | |
22 | use PVE::Auth::PAM; | |
23 | ||
24 | use Data::Dumper; # fixme: remove | |
25 | ||
26 | # load and initialize all plugins | |
27 | ||
28 | PVE::Auth::AD->register(); | |
29 | PVE::Auth::LDAP->register(); | |
30 | PVE::Auth::PVE->register(); | |
31 | PVE::Auth::PAM->register(); | |
32 | PVE::Auth::Plugin->init(); | |
33 | ||
34 | # $authdir must be writable by root only! | |
35 | my $confdir = "/etc/pve"; | |
36 | my $authdir = "$confdir/priv"; | |
37 | my $authprivkeyfn = "$authdir/authkey.key"; | |
38 | my $authpubkeyfn = "$confdir/authkey.pub"; | |
39 | my $pve_www_key_fn = "$confdir/pve-www.key"; | |
40 | ||
41 | my $ticket_lifetime = 3600*2; # 2 hours | |
42 | ||
43 | Crypt::OpenSSL::RSA->import_random_seed(); | |
44 | ||
45 | cfs_register_file('user.cfg', | |
46 | \&parse_user_config, | |
47 | \&write_user_config); | |
48 | ||
49 | ||
50 | sub verify_username { | |
51 | PVE::Auth::Plugin::verify_username(@_); | |
52 | } | |
53 | ||
54 | sub pve_verify_realm { | |
55 | PVE::Auth::Plugin::pve_verify_realm(@_); | |
56 | } | |
57 | ||
58 | sub lock_user_config { | |
59 | my ($code, $errmsg) = @_; | |
60 | ||
61 | cfs_lock_file("user.cfg", undef, $code); | |
62 | if (my $err = $@) { | |
63 | $errmsg ? die "$errmsg: $err" : die $err; | |
64 | } | |
65 | } | |
66 | ||
67 | my $pve_auth_pub_key; | |
68 | sub get_pubkey { | |
69 | ||
70 | return $pve_auth_pub_key if $pve_auth_pub_key; | |
71 | ||
72 | my $input = PVE::Tools::file_get_contents($authpubkeyfn); | |
73 | ||
74 | $pve_auth_pub_key = Crypt::OpenSSL::RSA->new_public_key($input); | |
75 | ||
76 | return $pve_auth_pub_key; | |
77 | } | |
78 | ||
79 | my $csrf_prevention_secret; | |
80 | my $get_csrfr_secret = sub { | |
81 | if (!$csrf_prevention_secret) { | |
82 | my $input = PVE::Tools::file_get_contents($pve_www_key_fn); | |
83 | $csrf_prevention_secret = Digest::SHA::sha1_base64($input); | |
84 | } | |
85 | return $csrf_prevention_secret; | |
86 | }; | |
87 | ||
88 | sub assemble_csrf_prevention_token { | |
89 | my ($username) = @_; | |
90 | ||
91 | my $timestamp = sprintf("%08X", time()); | |
92 | ||
93 | my $digest = Digest::SHA::sha1_base64("$timestamp:$username", &$get_csrfr_secret()); | |
94 | ||
95 | return "$timestamp:$digest"; | |
96 | } | |
97 | ||
98 | sub verify_csrf_prevention_token { | |
99 | my ($username, $token, $noerr) = @_; | |
100 | ||
101 | if ($token =~ m/^([A-Z0-9]{8}):(\S+)$/) { | |
102 | my $sig = $2; | |
103 | my $timestamp = $1; | |
104 | my $ttime = hex($timestamp); | |
105 | ||
106 | my $digest = Digest::SHA::sha1_base64("$timestamp:$username", &$get_csrfr_secret()); | |
107 | ||
108 | my $age = time() - $ttime; | |
109 | return if ($digest eq $sig) && ($age > -300) && ($age < $ticket_lifetime); | |
110 | } | |
111 | ||
112 | die "Permission denied - invalid csrf token\n" if !$noerr; | |
113 | ||
114 | return undef; | |
115 | } | |
116 | ||
117 | my $pve_auth_priv_key; | |
118 | sub get_privkey { | |
119 | ||
120 | return $pve_auth_priv_key if $pve_auth_priv_key; | |
121 | ||
122 | my $input = PVE::Tools::file_get_contents($authprivkeyfn); | |
123 | ||
124 | $pve_auth_priv_key = Crypt::OpenSSL::RSA->new_private_key($input); | |
125 | ||
126 | return $pve_auth_priv_key; | |
127 | } | |
128 | ||
129 | sub assemble_ticket { | |
130 | my ($username) = @_; | |
131 | ||
132 | my $rsa_priv = get_privkey(); | |
133 | ||
134 | my $timestamp = sprintf("%08X", time()); | |
135 | ||
136 | my $plain = "PVE:$username:$timestamp"; | |
137 | ||
138 | my $ticket = $plain . "::" . encode_base64($rsa_priv->sign($plain), ''); | |
139 | ||
140 | return $ticket; | |
141 | } | |
142 | ||
143 | sub verify_ticket { | |
144 | my ($ticket, $noerr) = @_; | |
145 | ||
146 | if ($ticket && $ticket =~ m/^(PVE:\S+)::([^:\s]+)$/) { | |
147 | my $plain = $1; | |
148 | my $sig = $2; | |
149 | ||
150 | my $rsa_pub = get_pubkey(); | |
151 | if ($rsa_pub->verify($plain, decode_base64($sig))) { | |
152 | if ($plain =~ m/^PVE:(\S+):([A-Z0-9]{8})$/) { | |
153 | my $username = $1; | |
154 | my $timestamp = $2; | |
155 | my $ttime = hex($timestamp); | |
156 | ||
157 | my $age = time() - $ttime; | |
158 | ||
159 | if (PVE::Auth::Plugin::verify_username($username, 1) && | |
160 | ($age > -300) && ($age < $ticket_lifetime)) { | |
161 | return wantarray ? ($username, $age) : $username; | |
162 | } | |
163 | } | |
164 | } | |
165 | } | |
166 | ||
167 | die "permission denied - invalid ticket\n" if !$noerr; | |
168 | ||
169 | return undef; | |
170 | } | |
171 | ||
172 | # VNC tickets | |
173 | # - they do not contain the username in plain text | |
174 | # - they are restricted to a specific resource path (example: '/vms/100') | |
175 | sub assemble_vnc_ticket { | |
176 | my ($username, $path) = @_; | |
177 | ||
178 | my $rsa_priv = get_privkey(); | |
179 | ||
180 | my $timestamp = sprintf("%08X", time()); | |
181 | ||
182 | my $plain = "PVEVNC:$timestamp"; | |
183 | ||
184 | $path = normalize_path($path); | |
185 | ||
186 | my $full = "$plain:$username:$path"; | |
187 | ||
188 | my $ticket = $plain . "::" . encode_base64($rsa_priv->sign($full), ''); | |
189 | ||
190 | return $ticket; | |
191 | } | |
192 | ||
193 | sub verify_vnc_ticket { | |
194 | my ($ticket, $username, $path, $noerr) = @_; | |
195 | ||
196 | if ($ticket && $ticket =~ m/^(PVEVNC:\S+)::([^:\s]+)$/) { | |
197 | my $plain = $1; | |
198 | my $sig = $2; | |
199 | my $full = "$plain:$username:$path"; | |
200 | ||
201 | my $rsa_pub = get_pubkey(); | |
202 | # Note: sign only match if $username and $path is correct | |
203 | if ($rsa_pub->verify($full, decode_base64($sig))) { | |
204 | if ($plain =~ m/^PVEVNC:([A-Z0-9]{8})$/) { | |
205 | my $ttime = hex($1); | |
206 | ||
207 | my $age = time() - $ttime; | |
208 | ||
209 | if (($age > -20) && ($age < 40)) { | |
210 | return 1; | |
211 | } | |
212 | } | |
213 | } | |
214 | } | |
215 | ||
216 | die "permission denied - invalid vnc ticket\n" if !$noerr; | |
217 | ||
218 | return undef; | |
219 | } | |
220 | ||
221 | sub assemble_spice_ticket { | |
222 | my ($username, $vmid, $node) = @_; | |
223 | ||
224 | my $rsa_priv = get_privkey(); | |
225 | ||
226 | my $timestamp = sprintf("%08x", time()); | |
227 | ||
228 | my $randomstr = "PVESPICE:$timestamp:$vmid:$node:" . rand(10); | |
229 | ||
230 | # this should be used as one-time password | |
231 | # max length is 60 chars (spice limit) | |
232 | # we pass this to qemu set_pasword and limit lifetime there | |
233 | # keep this secret | |
234 | my $ticket = Digest::SHA::sha1_hex($rsa_priv->sign($randomstr)); | |
235 | ||
236 | # Note: spice proxy connects with HTTP, so $proxyticket is exposed to public | |
237 | # we use a signature/timestamp to make sure nobody can fake such a ticket | |
238 | # an attacker can use this $proxyticket, but he will fail because $ticket is | |
239 | # private. | |
240 | # The proxy needs to be able to extract/verify the ticket | |
241 | # Note: data needs to be lower case only, because virt-viewer needs that | |
242 | # Note: RSA signature are too long (>=256 charaters) and make problems with remote-viewer | |
243 | ||
244 | my $secret = &$get_csrfr_secret(); | |
245 | my $plain = "pvespiceproxy:$timestamp:$vmid:" . lc($node); | |
246 | ||
247 | # produces 40 characters | |
248 | my $sig = unpack("H*", Digest::SHA::sha1($plain, &$get_csrfr_secret())); | |
249 | ||
250 | #my $sig = unpack("H*", $rsa_priv->sign($plain)); # this produce too long strings (512) | |
251 | ||
252 | my $proxyticket = $plain . "::" . $sig; | |
253 | ||
254 | return ($ticket, $proxyticket); | |
255 | } | |
256 | ||
257 | sub verify_spice_connect_url { | |
258 | my ($connect_str) = @_; | |
259 | ||
260 | # Note: we pass the spice ticket as 'host', so the | |
261 | # spice viewer connects with "$ticket:$port" | |
262 | ||
263 | return undef if !$connect_str; | |
264 | ||
265 | if ($connect_str =~m/^pvespiceproxy:([a-z0-9]{8}):(\d+):(\S+)::([a-z0-9]{40}):(\d+)$/) { | |
266 | my ($timestamp, $vmid, $node, $hexsig, $port) = ($1, $2, $3, $4, $5, $6); | |
267 | my $ttime = hex($timestamp); | |
268 | my $age = time() - $ttime; | |
269 | ||
270 | # use very limited lifetime - is this enough? | |
271 | return undef if !(($age > -20) && ($age < 40)); | |
272 | ||
273 | my $plain = "pvespiceproxy:$timestamp:$vmid:$node"; | |
274 | my $sig = unpack("H*", Digest::SHA::sha1($plain, &$get_csrfr_secret())); | |
275 | ||
276 | if ($sig eq $hexsig) { | |
277 | return ($vmid, $node, $port); | |
278 | } | |
279 | } | |
280 | ||
281 | return undef; | |
282 | } | |
283 | ||
284 | sub read_x509_subject_spice { | |
285 | my ($filename) = @_; | |
286 | ||
287 | # read x509 subject | |
288 | my $bio = Net::SSLeay::BIO_new_file($filename, 'r'); | |
289 | die "Could not open $filename using OpenSSL\n" | |
290 | if !$bio; | |
291 | ||
292 | my $x509 = Net::SSLeay::PEM_read_bio_X509($bio); | |
293 | Net::SSLeay::BIO_free($bio); | |
294 | ||
295 | die "Could not parse X509 certificate in $filename\n" | |
296 | if !$x509; | |
297 | ||
298 | my $nameobj = Net::SSLeay::X509_get_subject_name($x509); | |
299 | my $subject = Net::SSLeay::X509_NAME_oneline($nameobj); | |
300 | Net::SSLeay::X509_free($x509); | |
301 | ||
302 | # remote-viewer wants comma as seperator (not '/') | |
303 | $subject =~ s!^/!!; | |
304 | $subject =~ s!/(\w+=)!,$1!g; | |
305 | ||
306 | return $subject; | |
307 | } | |
308 | ||
309 | # helper to generate SPICE remote-viewer configuration | |
310 | sub remote_viewer_config { | |
311 | my ($authuser, $vmid, $node, $proxy, $title, $port) = @_; | |
312 | ||
313 | if (!$proxy) { | |
314 | my $host = `hostname -f` || PVE::INotify::nodename(); | |
315 | chomp $host; | |
316 | $proxy = $host; | |
317 | } | |
318 | ||
319 | my ($ticket, $proxyticket) = assemble_spice_ticket($authuser, $vmid, $node); | |
320 | ||
321 | my $filename = "/etc/pve/local/pve-ssl.pem"; | |
322 | my $subject = read_x509_subject_spice($filename); | |
323 | ||
324 | my $cacert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192); | |
325 | $cacert =~ s/\n/\\n/g; | |
326 | ||
327 | $proxy = "[$proxy]" if Net::IP::ip_is_ipv6($proxy); | |
328 | my $config = { | |
329 | 'secure-attention' => "Ctrl+Alt+Ins", | |
330 | 'toggle-fullscreen' => "Shift+F11", | |
331 | 'release-cursor' => "Ctrl+Alt+R", | |
332 | type => 'spice', | |
333 | title => $title, | |
334 | host => $proxyticket, # this breaks tls hostname verification, so we need to use 'host-subject' | |
335 | proxy => "http://$proxy:3128", | |
336 | 'tls-port' => $port, | |
337 | 'host-subject' => $subject, | |
338 | ca => $cacert, | |
339 | password => $ticket, | |
340 | 'delete-this-file' => 1, | |
341 | }; | |
342 | ||
343 | return ($ticket, $proxyticket, $config); | |
344 | } | |
345 | ||
346 | sub check_user_exist { | |
347 | my ($usercfg, $username, $noerr) = @_; | |
348 | ||
349 | $username = PVE::Auth::Plugin::verify_username($username, $noerr); | |
350 | return undef if !$username; | |
351 | ||
352 | return $usercfg->{users}->{$username} if $usercfg && $usercfg->{users}->{$username}; | |
353 | ||
354 | die "no such user ('$username')\n" if !$noerr; | |
355 | ||
356 | return undef; | |
357 | } | |
358 | ||
359 | sub check_user_enabled { | |
360 | my ($usercfg, $username, $noerr) = @_; | |
361 | ||
362 | my $data = check_user_exist($usercfg, $username, $noerr); | |
363 | return undef if !$data; | |
364 | ||
365 | return 1 if $data->{enable}; | |
366 | ||
367 | die "user '$username' is disabled\n" if !$noerr; | |
368 | ||
369 | return undef; | |
370 | } | |
371 | ||
372 | sub verify_one_time_pw { | |
373 | my ($usercfg, $username, $tfa_cfg, $otp) = @_; | |
374 | ||
375 | my $type = $tfa_cfg->{type}; | |
376 | ||
377 | die "missing one time password for two-factor authentication '$type'\n" if !$otp; | |
378 | ||
379 | # fixme: proxy support? | |
380 | my $proxy; | |
381 | ||
382 | if ($type eq 'yubico') { | |
383 | my $keys = $usercfg->{users}->{$username}->{keys}; | |
384 | yubico_verify_otp($otp, $keys, $tfa_cfg->{url}, $tfa_cfg->{id}, $tfa_cfg->{key}, $proxy); | |
385 | } elsif ($type eq 'oath') { | |
386 | my $keys = $usercfg->{users}->{$username}->{keys}; | |
387 | oath_verify_otp($otp, $keys, $tfa_cfg->{step}, $tfa_cfg->{digits}); | |
388 | } else { | |
389 | die "unknown tfa type '$type'\n"; | |
390 | } | |
391 | } | |
392 | ||
393 | # password should be utf8 encoded | |
394 | # Note: some plugins delay/sleep if auth fails | |
395 | sub authenticate_user { | |
396 | my ($username, $password, $otp) = @_; | |
397 | ||
398 | die "no username specified\n" if !$username; | |
399 | ||
400 | my ($ruid, $realm); | |
401 | ||
402 | ($username, $ruid, $realm) = PVE::Auth::Plugin::verify_username($username); | |
403 | ||
404 | my $usercfg = cfs_read_file('user.cfg'); | |
405 | ||
406 | check_user_enabled($usercfg, $username); | |
407 | ||
408 | my $ctime = time(); | |
409 | my $expire = $usercfg->{users}->{$username}->{expire}; | |
410 | ||
411 | die "account expired\n" if $expire && ($expire < $ctime); | |
412 | ||
413 | my $domain_cfg = cfs_read_file('domains.cfg'); | |
414 | ||
415 | my $cfg = $domain_cfg->{ids}->{$realm}; | |
416 | die "auth domain '$realm' does not exists\n" if !$cfg; | |
417 | my $plugin = PVE::Auth::Plugin->lookup($cfg->{type}); | |
418 | $plugin->authenticate_user($cfg, $realm, $ruid, $password); | |
419 | ||
420 | if ($cfg->{tfa}) { | |
421 | my $tfa_cfg = PVE::Auth::Plugin::parse_tfa_config($cfg->{tfa}); | |
422 | verify_one_time_pw($usercfg, $username, $tfa_cfg, $otp); | |
423 | } | |
424 | ||
425 | return $username; | |
426 | } | |
427 | ||
428 | sub domain_set_password { | |
429 | my ($realm, $username, $password) = @_; | |
430 | ||
431 | die "no auth domain specified" if !$realm; | |
432 | ||
433 | my $domain_cfg = cfs_read_file('domains.cfg'); | |
434 | ||
435 | my $cfg = $domain_cfg->{ids}->{$realm}; | |
436 | die "auth domain '$realm' does not exist\n" if !$cfg; | |
437 | my $plugin = PVE::Auth::Plugin->lookup($cfg->{type}); | |
438 | $plugin->store_password($cfg, $realm, $username, $password); | |
439 | } | |
440 | ||
441 | sub add_user_group { | |
442 | my ($username, $usercfg, $group) = @_; | |
443 | ||
444 | $usercfg->{users}->{$username}->{groups}->{$group} = 1; | |
445 | $usercfg->{groups}->{$group}->{users}->{$username} = 1; | |
446 | } | |
447 | ||
448 | sub delete_user_group { | |
449 | my ($username, $usercfg) = @_; | |
450 | ||
451 | foreach my $group (keys %{$usercfg->{groups}}) { | |
452 | ||
453 | delete ($usercfg->{groups}->{$group}->{users}->{$username}) | |
454 | if $usercfg->{groups}->{$group}->{users}->{$username}; | |
455 | } | |
456 | } | |
457 | ||
458 | sub delete_user_acl { | |
459 | my ($username, $usercfg) = @_; | |
460 | ||
461 | foreach my $acl (keys %{$usercfg->{acl}}) { | |
462 | ||
463 | delete ($usercfg->{acl}->{$acl}->{users}->{$username}) | |
464 | if $usercfg->{acl}->{$acl}->{users}->{$username}; | |
465 | } | |
466 | } | |
467 | ||
468 | sub delete_group_acl { | |
469 | my ($group, $usercfg) = @_; | |
470 | ||
471 | foreach my $acl (keys %{$usercfg->{acl}}) { | |
472 | ||
473 | delete ($usercfg->{acl}->{$acl}->{groups}->{$group}) | |
474 | if $usercfg->{acl}->{$acl}->{groups}->{$group}; | |
475 | } | |
476 | } | |
477 | ||
478 | sub delete_pool_acl { | |
479 | my ($pool, $usercfg) = @_; | |
480 | ||
481 | my $path = "/pool/$pool"; | |
482 | ||
483 | delete ($usercfg->{acl}->{$path}) | |
484 | } | |
485 | ||
486 | # we automatically create some predefined roles by splitting privs | |
487 | # into 3 groups (per category) | |
488 | # root: only root is allowed to do that | |
489 | # admin: an administrator can to that | |
490 | # user: a normal user/customer can to that | |
491 | my $privgroups = { | |
492 | VM => { | |
493 | root => [], | |
494 | admin => [ | |
495 | 'VM.Config.Disk', | |
496 | 'VM.Config.CPU', | |
497 | 'VM.Config.Memory', | |
498 | 'VM.Config.Network', | |
499 | 'VM.Config.HWType', | |
500 | 'VM.Config.Options', # covers all other things | |
501 | 'VM.Allocate', | |
502 | 'VM.Clone', | |
503 | 'VM.Migrate', | |
504 | 'VM.Monitor', | |
505 | 'VM.Snapshot', | |
506 | ], | |
507 | user => [ | |
508 | 'VM.Config.CDROM', # change CDROM media | |
509 | 'VM.Console', | |
510 | 'VM.Backup', | |
511 | 'VM.PowerMgmt', | |
512 | ], | |
513 | audit => [ | |
514 | 'VM.Audit', | |
515 | ], | |
516 | }, | |
517 | Sys => { | |
518 | root => [ | |
519 | 'Sys.PowerMgmt', | |
520 | 'Sys.Modify', # edit/change node settings | |
521 | ], | |
522 | admin => [ | |
523 | 'Permissions.Modify', | |
524 | 'Sys.Console', | |
525 | 'Sys.Syslog', | |
526 | ], | |
527 | user => [], | |
528 | audit => [ | |
529 | 'Sys.Audit', | |
530 | ], | |
531 | }, | |
532 | Datastore => { | |
533 | root => [], | |
534 | admin => [ | |
535 | 'Datastore.Allocate', | |
536 | 'Datastore.AllocateTemplate', | |
537 | ], | |
538 | user => [ | |
539 | 'Datastore.AllocateSpace', | |
540 | ], | |
541 | audit => [ | |
542 | 'Datastore.Audit', | |
543 | ], | |
544 | }, | |
545 | User => { | |
546 | root => [ | |
547 | 'Realm.Allocate', | |
548 | ], | |
549 | admin => [ | |
550 | 'User.Modify', | |
551 | 'Group.Allocate', # edit/change group settings | |
552 | 'Realm.AllocateUser', | |
553 | ], | |
554 | user => [], | |
555 | audit => [], | |
556 | }, | |
557 | Pool => { | |
558 | root => [], | |
559 | admin => [ | |
560 | 'Pool.Allocate', # create/delete pools | |
561 | ], | |
562 | user => [], | |
563 | audit => [], | |
564 | }, | |
565 | }; | |
566 | ||
567 | my $valid_privs = {}; | |
568 | ||
569 | my $special_roles = { | |
570 | 'NoAccess' => {}, # no privileges | |
571 | 'Administrator' => $valid_privs, # all privileges | |
572 | }; | |
573 | ||
574 | sub create_roles { | |
575 | ||
576 | foreach my $cat (keys %$privgroups) { | |
577 | my $cd = $privgroups->{$cat}; | |
578 | foreach my $p (@{$cd->{root}}, @{$cd->{admin}}, | |
579 | @{$cd->{user}}, @{$cd->{audit}}) { | |
580 | $valid_privs->{$p} = 1; | |
581 | } | |
582 | foreach my $p (@{$cd->{admin}}, @{$cd->{user}}, @{$cd->{audit}}) { | |
583 | ||
584 | $special_roles->{"PVE${cat}Admin"}->{$p} = 1; | |
585 | $special_roles->{"PVEAdmin"}->{$p} = 1; | |
586 | } | |
587 | if (scalar(@{$cd->{user}})) { | |
588 | foreach my $p (@{$cd->{user}}, @{$cd->{audit}}) { | |
589 | $special_roles->{"PVE${cat}User"}->{$p} = 1; | |
590 | } | |
591 | } | |
592 | foreach my $p (@{$cd->{audit}}) { | |
593 | $special_roles->{"PVEAuditor"}->{$p} = 1; | |
594 | } | |
595 | } | |
596 | ||
597 | $special_roles->{"PVETemplateUser"} = { 'VM.Clone' => 1, 'VM.Audit' => 1 }; | |
598 | }; | |
599 | ||
600 | create_roles(); | |
601 | ||
602 | sub add_role_privs { | |
603 | my ($role, $usercfg, $privs) = @_; | |
604 | ||
605 | return if !$privs; | |
606 | ||
607 | die "role '$role' does not exist\n" if !$usercfg->{roles}->{$role}; | |
608 | ||
609 | foreach my $priv (split_list($privs)) { | |
610 | if (defined ($valid_privs->{$priv})) { | |
611 | $usercfg->{roles}->{$role}->{$priv} = 1; | |
612 | } else { | |
613 | die "invalid privilege '$priv'\n"; | |
614 | } | |
615 | } | |
616 | } | |
617 | ||
618 | sub normalize_path { | |
619 | my $path = shift; | |
620 | ||
621 | $path =~ s|/+|/|g; | |
622 | ||
623 | $path =~ s|/$||; | |
624 | ||
625 | $path = '/' if !$path; | |
626 | ||
627 | $path = "/$path" if $path !~ m|^/|; | |
628 | ||
629 | return undef if $path !~ m|^[[:alnum:]\.\-\_\/]+$|; | |
630 | ||
631 | return $path; | |
632 | } | |
633 | ||
634 | ||
635 | PVE::JSONSchema::register_format('pve-groupid', \&verify_groupname); | |
636 | sub verify_groupname { | |
637 | my ($groupname, $noerr) = @_; | |
638 | ||
639 | if ($groupname !~ m/^[A-Za-z0-9\.\-_]+$/) { | |
640 | ||
641 | die "group name '$groupname' contains invalid characters\n" if !$noerr; | |
642 | ||
643 | return undef; | |
644 | } | |
645 | ||
646 | return $groupname; | |
647 | } | |
648 | ||
649 | PVE::JSONSchema::register_format('pve-roleid', \&verify_rolename); | |
650 | sub verify_rolename { | |
651 | my ($rolename, $noerr) = @_; | |
652 | ||
653 | if ($rolename !~ m/^[A-Za-z0-9\.\-_]+$/) { | |
654 | ||
655 | die "role name '$rolename' contains invalid characters\n" if !$noerr; | |
656 | ||
657 | return undef; | |
658 | } | |
659 | ||
660 | return $rolename; | |
661 | } | |
662 | ||
663 | PVE::JSONSchema::register_format('pve-poolid', \&verify_groupname); | |
664 | sub verify_poolname { | |
665 | my ($poolname, $noerr) = @_; | |
666 | ||
667 | if ($poolname !~ m/^[A-Za-z0-9\.\-_]+$/) { | |
668 | ||
669 | die "pool name '$poolname' contains invalid characters\n" if !$noerr; | |
670 | ||
671 | return undef; | |
672 | } | |
673 | ||
674 | return $poolname; | |
675 | } | |
676 | ||
677 | PVE::JSONSchema::register_format('pve-priv', \&verify_privname); | |
678 | sub verify_privname { | |
679 | my ($priv, $noerr) = @_; | |
680 | ||
681 | if (!$valid_privs->{$priv}) { | |
682 | die "invalid privilege '$priv'\n" if !$noerr; | |
683 | ||
684 | return undef; | |
685 | } | |
686 | ||
687 | return $priv; | |
688 | } | |
689 | ||
690 | sub userconfig_force_defaults { | |
691 | my ($cfg) = @_; | |
692 | ||
693 | foreach my $r (keys %$special_roles) { | |
694 | $cfg->{roles}->{$r} = $special_roles->{$r}; | |
695 | } | |
696 | ||
697 | # add root user if not exists | |
698 | if (!$cfg->{users}->{'root@pam'}) { | |
699 | $cfg->{users}->{'root@pam'}->{enable} = 1; | |
700 | } | |
701 | } | |
702 | ||
703 | sub parse_user_config { | |
704 | my ($filename, $raw) = @_; | |
705 | ||
706 | my $cfg = {}; | |
707 | ||
708 | userconfig_force_defaults($cfg); | |
709 | ||
710 | $raw = '' if !defined($raw); | |
711 | while ($raw =~ /^\s*(.+?)\s*$/gm) { | |
712 | my $line = $1; | |
713 | my @data; | |
714 | ||
715 | foreach my $d (split (/:/, $line)) { | |
716 | $d =~ s/^\s+//; | |
717 | $d =~ s/\s+$//; | |
718 | push @data, $d | |
719 | } | |
720 | ||
721 | my $et = shift @data; | |
722 | ||
723 | if ($et eq 'user') { | |
724 | my ($user, $enable, $expire, $firstname, $lastname, $email, $comment, $keys) = @data; | |
725 | ||
726 | my (undef, undef, $realm) = PVE::Auth::Plugin::verify_username($user, 1); | |
727 | if (!$realm) { | |
728 | warn "user config - ignore user '$user' - invalid user name\n"; | |
729 | next; | |
730 | } | |
731 | ||
732 | $enable = $enable ? 1 : 0; | |
733 | ||
734 | $expire = 0 if !$expire; | |
735 | ||
736 | if ($expire !~ m/^\d+$/) { | |
737 | warn "user config - ignore user '$user' - (illegal characters in expire '$expire')\n"; | |
738 | next; | |
739 | } | |
740 | $expire = int($expire); | |
741 | ||
742 | #if (!verify_groupname ($group, 1)) { | |
743 | # warn "user config - ignore user '$user' - invalid characters in group name\n"; | |
744 | # next; | |
745 | #} | |
746 | ||
747 | $cfg->{users}->{$user} = { | |
748 | enable => $enable, | |
749 | # group => $group, | |
750 | }; | |
751 | $cfg->{users}->{$user}->{firstname} = PVE::Tools::decode_text($firstname) if $firstname; | |
752 | $cfg->{users}->{$user}->{lastname} = PVE::Tools::decode_text($lastname) if $lastname; | |
753 | $cfg->{users}->{$user}->{email} = $email; | |
754 | $cfg->{users}->{$user}->{comment} = PVE::Tools::decode_text($comment) if $comment; | |
755 | $cfg->{users}->{$user}->{expire} = $expire; | |
756 | # keys: allowed yubico key ids or oath secrets (base32 encoded) | |
757 | $cfg->{users}->{$user}->{keys} = $keys if $keys; | |
758 | ||
759 | #$cfg->{users}->{$user}->{groups}->{$group} = 1; | |
760 | #$cfg->{groups}->{$group}->{$user} = 1; | |
761 | ||
762 | } elsif ($et eq 'group') { | |
763 | my ($group, $userlist, $comment) = @data; | |
764 | ||
765 | if (!verify_groupname($group, 1)) { | |
766 | warn "user config - ignore group '$group' - invalid characters in group name\n"; | |
767 | next; | |
768 | } | |
769 | ||
770 | # make sure to add the group (even if there are no members) | |
771 | $cfg->{groups}->{$group} = { users => {} } if !$cfg->{groups}->{$group}; | |
772 | ||
773 | $cfg->{groups}->{$group}->{comment} = PVE::Tools::decode_text($comment) if $comment; | |
774 | ||
775 | foreach my $user (split_list($userlist)) { | |
776 | ||
777 | if (!PVE::Auth::Plugin::verify_username($user, 1)) { | |
778 | warn "user config - ignore invalid group member '$user'\n"; | |
779 | next; | |
780 | } | |
781 | ||
782 | if ($cfg->{users}->{$user}) { # user exists | |
783 | $cfg->{users}->{$user}->{groups}->{$group} = 1; | |
784 | $cfg->{groups}->{$group}->{users}->{$user} = 1; | |
785 | } else { | |
786 | warn "user config - ignore invalid group member '$user'\n"; | |
787 | } | |
788 | } | |
789 | ||
790 | } elsif ($et eq 'role') { | |
791 | my ($role, $privlist) = @data; | |
792 | ||
793 | if (!verify_rolename($role, 1)) { | |
794 | warn "user config - ignore role '$role' - invalid characters in role name\n"; | |
795 | next; | |
796 | } | |
797 | ||
798 | # make sure to add the role (even if there are no privileges) | |
799 | $cfg->{roles}->{$role} = {} if !$cfg->{roles}->{$role}; | |
800 | ||
801 | foreach my $priv (split_list($privlist)) { | |
802 | if (defined ($valid_privs->{$priv})) { | |
803 | $cfg->{roles}->{$role}->{$priv} = 1; | |
804 | } else { | |
805 | warn "user config - ignore invalid priviledge '$priv'\n"; | |
806 | } | |
807 | } | |
808 | ||
809 | } elsif ($et eq 'acl') { | |
810 | my ($propagate, $pathtxt, $uglist, $rolelist) = @data; | |
811 | ||
812 | if (my $path = normalize_path($pathtxt)) { | |
813 | foreach my $role (split_list($rolelist)) { | |
814 | ||
815 | if (!verify_rolename($role, 1)) { | |
816 | warn "user config - ignore invalid role name '$role' in acl\n"; | |
817 | next; | |
818 | } | |
819 | ||
820 | foreach my $ug (split_list($uglist)) { | |
821 | if ($ug =~ m/^@(\S+)$/) { | |
822 | my $group = $1; | |
823 | if ($cfg->{groups}->{$group}) { # group exists | |
824 | $cfg->{acl}->{$path}->{groups}->{$group}->{$role} = $propagate; | |
825 | } else { | |
826 | warn "user config - ignore invalid acl group '$group'\n"; | |
827 | } | |
828 | } elsif (PVE::Auth::Plugin::verify_username($ug, 1)) { | |
829 | if ($cfg->{users}->{$ug}) { # user exists | |
830 | $cfg->{acl}->{$path}->{users}->{$ug}->{$role} = $propagate; | |
831 | } else { | |
832 | warn "user config - ignore invalid acl member '$ug'\n"; | |
833 | } | |
834 | } else { | |
835 | warn "user config - invalid user/group '$ug' in acl\n"; | |
836 | } | |
837 | } | |
838 | } | |
839 | } else { | |
840 | warn "user config - ignore invalid path in acl '$pathtxt'\n"; | |
841 | } | |
842 | } elsif ($et eq 'pool') { | |
843 | my ($pool, $comment, $vmlist, $storelist) = @data; | |
844 | ||
845 | if (!verify_poolname($pool, 1)) { | |
846 | warn "user config - ignore pool '$pool' - invalid characters in pool name\n"; | |
847 | next; | |
848 | } | |
849 | ||
850 | # make sure to add the pool (even if there are no members) | |
851 | $cfg->{pools}->{$pool} = { vms => {}, storage => {} } if !$cfg->{pools}->{$pool}; | |
852 | ||
853 | $cfg->{pools}->{$pool}->{comment} = PVE::Tools::decode_text($comment) if $comment; | |
854 | ||
855 | foreach my $vmid (split_list($vmlist)) { | |
856 | if ($vmid !~ m/^\d+$/) { | |
857 | warn "user config - ignore invalid vmid '$vmid' in pool '$pool'\n"; | |
858 | next; | |
859 | } | |
860 | $vmid = int($vmid); | |
861 | ||
862 | if ($cfg->{vms}->{$vmid}) { | |
863 | warn "user config - ignore duplicate vmid '$vmid' in pool '$pool'\n"; | |
864 | next; | |
865 | } | |
866 | ||
867 | $cfg->{pools}->{$pool}->{vms}->{$vmid} = 1; | |
868 | ||
869 | # record vmid ==> pool relation | |
870 | $cfg->{vms}->{$vmid} = $pool; | |
871 | } | |
872 | ||
873 | foreach my $storeid (split_list($storelist)) { | |
874 | if ($storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) { | |
875 | warn "user config - ignore invalid storage '$storeid' in pool '$pool'\n"; | |
876 | next; | |
877 | } | |
878 | $cfg->{pools}->{$pool}->{storage}->{$storeid} = 1; | |
879 | } | |
880 | } else { | |
881 | warn "user config - ignore config line: $line\n"; | |
882 | } | |
883 | } | |
884 | ||
885 | userconfig_force_defaults($cfg); | |
886 | ||
887 | return $cfg; | |
888 | } | |
889 | ||
890 | sub write_user_config { | |
891 | my ($filename, $cfg) = @_; | |
892 | ||
893 | my $data = ''; | |
894 | ||
895 | foreach my $user (keys %{$cfg->{users}}) { | |
896 | my $d = $cfg->{users}->{$user}; | |
897 | my $firstname = $d->{firstname} ? PVE::Tools::encode_text($d->{firstname}) : ''; | |
898 | my $lastname = $d->{lastname} ? PVE::Tools::encode_text($d->{lastname}) : ''; | |
899 | my $email = $d->{email} || ''; | |
900 | my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : ''; | |
901 | my $expire = int($d->{expire} || 0); | |
902 | my $enable = $d->{enable} ? 1 : 0; | |
903 | my $keys = $d->{keys} ? $d->{keys} : ''; | |
904 | $data .= "user:$user:$enable:$expire:$firstname:$lastname:$email:$comment:$keys:\n"; | |
905 | } | |
906 | ||
907 | $data .= "\n"; | |
908 | ||
909 | foreach my $group (keys %{$cfg->{groups}}) { | |
910 | my $d = $cfg->{groups}->{$group}; | |
911 | my $list = join (',', keys %{$d->{users}}); | |
912 | my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : ''; | |
913 | $data .= "group:$group:$list:$comment:\n"; | |
914 | } | |
915 | ||
916 | $data .= "\n"; | |
917 | ||
918 | foreach my $pool (keys %{$cfg->{pools}}) { | |
919 | my $d = $cfg->{pools}->{$pool}; | |
920 | my $vmlist = join (',', keys %{$d->{vms}}); | |
921 | my $storelist = join (',', keys %{$d->{storage}}); | |
922 | my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : ''; | |
923 | $data .= "pool:$pool:$comment:$vmlist:$storelist:\n"; | |
924 | } | |
925 | ||
926 | $data .= "\n"; | |
927 | ||
928 | foreach my $role (keys %{$cfg->{roles}}) { | |
929 | next if $special_roles->{$role}; | |
930 | ||
931 | my $d = $cfg->{roles}->{$role}; | |
932 | my $list = join (',', keys %$d); | |
933 | $data .= "role:$role:$list:\n"; | |
934 | } | |
935 | ||
936 | $data .= "\n"; | |
937 | ||
938 | foreach my $path (sort keys %{$cfg->{acl}}) { | |
939 | my $d = $cfg->{acl}->{$path}; | |
940 | ||
941 | my $ra = {}; | |
942 | ||
943 | foreach my $group (keys %{$d->{groups}}) { | |
944 | my $l0 = ''; | |
945 | my $l1 = ''; | |
946 | foreach my $role (sort keys %{$d->{groups}->{$group}}) { | |
947 | my $propagate = $d->{groups}->{$group}->{$role}; | |
948 | if ($propagate) { | |
949 | $l1 .= ',' if $l1; | |
950 | $l1 .= $role; | |
951 | } else { | |
952 | $l0 .= ',' if $l0; | |
953 | $l0 .= $role; | |
954 | } | |
955 | } | |
956 | $ra->{0}->{$l0}->{"\@$group"} = 1 if $l0; | |
957 | $ra->{1}->{$l1}->{"\@$group"} = 1 if $l1; | |
958 | } | |
959 | ||
960 | foreach my $user (keys %{$d->{users}}) { | |
961 | # no need to save, because root is always 'Administrator' | |
962 | next if $user eq 'root@pam'; | |
963 | ||
964 | my $l0 = ''; | |
965 | my $l1 = ''; | |
966 | foreach my $role (sort keys %{$d->{users}->{$user}}) { | |
967 | my $propagate = $d->{users}->{$user}->{$role}; | |
968 | if ($propagate) { | |
969 | $l1 .= ',' if $l1; | |
970 | $l1 .= $role; | |
971 | } else { | |
972 | $l0 .= ',' if $l0; | |
973 | $l0 .= $role; | |
974 | } | |
975 | } | |
976 | $ra->{0}->{$l0}->{$user} = 1 if $l0; | |
977 | $ra->{1}->{$l1}->{$user} = 1 if $l1; | |
978 | } | |
979 | ||
980 | foreach my $rolelist (sort keys %{$ra->{0}}) { | |
981 | my $uglist = join (',', keys %{$ra->{0}->{$rolelist}}); | |
982 | $data .= "acl:0:$path:$uglist:$rolelist:\n"; | |
983 | } | |
984 | foreach my $rolelist (sort keys %{$ra->{1}}) { | |
985 | my $uglist = join (',', keys %{$ra->{1}->{$rolelist}}); | |
986 | $data .= "acl:1:$path:$uglist:$rolelist:\n"; | |
987 | } | |
988 | } | |
989 | ||
990 | return $data; | |
991 | } | |
992 | ||
993 | sub roles { | |
994 | my ($cfg, $user, $path) = @_; | |
995 | ||
996 | # NOTE: we do not consider pools here. | |
997 | # You need to use $rpcenv->roles() instead if you want that. | |
998 | ||
999 | return 'Administrator' if $user eq 'root@pam'; # root can do anything | |
1000 | ||
1001 | my $perm = {}; | |
1002 | ||
1003 | foreach my $p (sort keys %{$cfg->{acl}}) { | |
1004 | my $final = ($path eq $p); | |
1005 | ||
1006 | next if !(($p eq '/') || $final || ($path =~ m|^$p/|)); | |
1007 | ||
1008 | my $acl = $cfg->{acl}->{$p}; | |
1009 | ||
1010 | #print "CHECKACL $path $p\n"; | |
1011 | #print "ACL $path = " . Dumper ($acl); | |
1012 | ||
1013 | if (my $ri = $acl->{users}->{$user}) { | |
1014 | my $new; | |
1015 | foreach my $role (keys %$ri) { | |
1016 | my $propagate = $ri->{$role}; | |
1017 | if ($final || $propagate) { | |
1018 | #print "APPLY ROLE $p $user $role\n"; | |
1019 | $new = {} if !$new; | |
1020 | $new->{$role} = 1; | |
1021 | } | |
1022 | } | |
1023 | if ($new) { | |
1024 | $perm = $new; # overwrite previous settings | |
1025 | next; # user privs always override group privs | |
1026 | } | |
1027 | } | |
1028 | ||
1029 | my $new; | |
1030 | foreach my $g (keys %{$acl->{groups}}) { | |
1031 | next if !$cfg->{groups}->{$g}->{users}->{$user}; | |
1032 | if (my $ri = $acl->{groups}->{$g}) { | |
1033 | foreach my $role (keys %$ri) { | |
1034 | my $propagate = $ri->{$role}; | |
1035 | if ($final || $propagate) { | |
1036 | #print "APPLY ROLE $p \@$g $role\n"; | |
1037 | $new = {} if !$new; | |
1038 | $new->{$role} = 1; | |
1039 | } | |
1040 | } | |
1041 | } | |
1042 | } | |
1043 | if ($new) { | |
1044 | $perm = $new; # overwrite previous settings | |
1045 | next; | |
1046 | } | |
1047 | } | |
1048 | ||
1049 | return ('NoAccess') if defined ($perm->{NoAccess}); | |
1050 | #return () if defined ($perm->{NoAccess}); | |
1051 | ||
1052 | #print "permission $user $path = " . Dumper ($perm); | |
1053 | ||
1054 | my @ra = keys %$perm; | |
1055 | ||
1056 | #print "roles $user $path = " . join (',', @ra) . "\n"; | |
1057 | ||
1058 | return @ra; | |
1059 | } | |
1060 | ||
1061 | sub permission { | |
1062 | my ($cfg, $user, $path) = @_; | |
1063 | ||
1064 | $user = PVE::Auth::Plugin::verify_username($user, 1); | |
1065 | return {} if !$user; | |
1066 | ||
1067 | my @ra = roles($cfg, $user, $path); | |
1068 | ||
1069 | my $privs = {}; | |
1070 | ||
1071 | foreach my $role (@ra) { | |
1072 | if (my $privset = $cfg->{roles}->{$role}) { | |
1073 | foreach my $p (keys %$privset) { | |
1074 | $privs->{$p} = 1; | |
1075 | } | |
1076 | } | |
1077 | } | |
1078 | ||
1079 | #print "priviledges $user $path = " . Dumper ($privs); | |
1080 | ||
1081 | return $privs; | |
1082 | } | |
1083 | ||
1084 | sub check_permissions { | |
1085 | my ($username, $path, $privlist) = @_; | |
1086 | ||
1087 | $path = normalize_path($path); | |
1088 | my $usercfg = cfs_read_file('user.cfg'); | |
1089 | my $perm = permission($usercfg, $username, $path); | |
1090 | ||
1091 | foreach my $priv (split_list($privlist)) { | |
1092 | return undef if !$perm->{$priv}; | |
1093 | }; | |
1094 | ||
1095 | return 1; | |
1096 | } | |
1097 | ||
1098 | sub remove_vm_access { | |
1099 | my ($vmid) = @_; | |
1100 | my $delVMaccessFn = sub { | |
1101 | my $usercfg = cfs_read_file("user.cfg"); | |
1102 | my $modified; | |
1103 | ||
1104 | if (my $acl = $usercfg->{acl}->{"/vms/$vmid"}) { | |
1105 | delete $usercfg->{acl}->{"/vms/$vmid"}; | |
1106 | $modified = 1; | |
1107 | } | |
1108 | if (my $pool = $usercfg->{vms}->{$vmid}) { | |
1109 | if (my $data = $usercfg->{pools}->{$pool}) { | |
1110 | delete $data->{vms}->{$vmid}; | |
1111 | delete $usercfg->{vms}->{$vmid}; | |
1112 | $modified = 1; | |
1113 | } | |
1114 | } | |
1115 | cfs_write_file("user.cfg", $usercfg) if $modified; | |
1116 | }; | |
1117 | ||
1118 | lock_user_config($delVMaccessFn, "access permissions cleanup for VM $vmid failed"); | |
1119 | } | |
1120 | ||
1121 | sub remove_storage_access { | |
1122 | my ($storeid) = @_; | |
1123 | ||
1124 | my $deleteStorageAccessFn = sub { | |
1125 | my $usercfg = cfs_read_file("user.cfg"); | |
1126 | my $modified; | |
1127 | ||
1128 | if (my $storage = $usercfg->{acl}->{"/storage/$storeid"}) { | |
1129 | delete $usercfg->{acl}->{"/storage/$storeid"}; | |
1130 | $modified = 1; | |
1131 | } | |
1132 | foreach my $pool (keys %{$usercfg->{pools}}) { | |
1133 | delete $usercfg->{pools}->{$pool}->{storage}->{$storeid}; | |
1134 | $modified = 1; | |
1135 | } | |
1136 | cfs_write_file("user.cfg", $usercfg) if $modified; | |
1137 | }; | |
1138 | ||
1139 | lock_user_config($deleteStorageAccessFn, | |
1140 | "access permissions cleanup for storage $storeid failed"); | |
1141 | } | |
1142 | ||
1143 | sub add_vm_to_pool { | |
1144 | my ($vmid, $pool) = @_; | |
1145 | ||
1146 | my $addVMtoPoolFn = sub { | |
1147 | my $usercfg = cfs_read_file("user.cfg"); | |
1148 | if (my $data = $usercfg->{pools}->{$pool}) { | |
1149 | $data->{vms}->{$vmid} = 1; | |
1150 | $usercfg->{vms}->{$vmid} = $pool; | |
1151 | cfs_write_file("user.cfg", $usercfg); | |
1152 | } | |
1153 | }; | |
1154 | ||
1155 | lock_user_config($addVMtoPoolFn, "can't add VM $vmid to pool '$pool'"); | |
1156 | } | |
1157 | ||
1158 | sub remove_vm_from_pool { | |
1159 | my ($vmid) = @_; | |
1160 | ||
1161 | my $delVMfromPoolFn = sub { | |
1162 | my $usercfg = cfs_read_file("user.cfg"); | |
1163 | if (my $pool = $usercfg->{vms}->{$vmid}) { | |
1164 | if (my $data = $usercfg->{pools}->{$pool}) { | |
1165 | delete $data->{vms}->{$vmid}; | |
1166 | delete $usercfg->{vms}->{$vmid}; | |
1167 | cfs_write_file("user.cfg", $usercfg); | |
1168 | } | |
1169 | } | |
1170 | }; | |
1171 | ||
1172 | lock_user_config($delVMfromPoolFn, "pool cleanup for VM $vmid failed"); | |
1173 | } | |
1174 | ||
1175 | # experimental code for yubico OTP verification | |
1176 | ||
1177 | sub yubico_compute_param_sig { | |
1178 | my ($param, $api_key) = @_; | |
1179 | ||
1180 | my $paramstr = ''; | |
1181 | foreach my $key (sort keys %$param) { | |
1182 | $paramstr .= '&' if $paramstr; | |
1183 | $paramstr .= "$key=$param->{$key}"; | |
1184 | } | |
1185 | ||
1186 | # hmac_sha1_base64 does not add '=' padding characters, so we use encode_base64 | |
1187 | my $sig = uri_escape(encode_base64(Digest::SHA::hmac_sha1($paramstr, decode_base64($api_key || '')), '')); | |
1188 | ||
1189 | return ($paramstr, $sig); | |
1190 | } | |
1191 | ||
1192 | sub yubico_verify_otp { | |
1193 | my ($otp, $keys, $url, $api_id, $api_key, $proxy) = @_; | |
1194 | ||
1195 | die "yubico: missing password\n" if !defined($otp); | |
1196 | die "yubico: missing API ID\n" if !defined($api_id); | |
1197 | die "yubico: missing API KEY\n" if !defined($api_key); | |
1198 | die "yubico: no associated yubico keys\n" if $keys =~ m/^\s+$/; | |
1199 | ||
1200 | die "yubico: wrong OTP length\n" if (length($otp) < 32) || (length($otp) > 48); | |
1201 | ||
1202 | $url = 'http://api2.yubico.com/wsapi/2.0/verify' if !defined($url); | |
1203 | ||
1204 | my $params = { | |
1205 | nonce => Digest::SHA::hmac_sha1_hex(time(), rand()), | |
1206 | id => $api_id, | |
1207 | otp => uri_escape($otp), | |
1208 | timestamp => 1, | |
1209 | }; | |
1210 | ||
1211 | my ($paramstr, $sig) = yubico_compute_param_sig($params, $api_key); | |
1212 | ||
1213 | $paramstr .= "&h=$sig" if $api_key; | |
1214 | ||
1215 | my $req = HTTP::Request->new('GET' => "$url?$paramstr"); | |
1216 | ||
1217 | my $ua = LWP::UserAgent->new(protocols_allowed => ['http', 'https'], timeout => 30); | |
1218 | ||
1219 | if ($proxy) { | |
1220 | $ua->proxy(['http', 'https'], $proxy); | |
1221 | } else { | |
1222 | $ua->env_proxy; | |
1223 | } | |
1224 | ||
1225 | my $response = $ua->request($req); | |
1226 | my $code = $response->code; | |
1227 | ||
1228 | if ($code != 200) { | |
1229 | my $msg = $response->message || 'unknown'; | |
1230 | die "Invalid response from server: $code $msg\n"; | |
1231 | } | |
1232 | ||
1233 | my $raw = $response->decoded_content; | |
1234 | ||
1235 | my $result = {}; | |
1236 | foreach my $kvpair (split(/\n/, $raw)) { | |
1237 | chomp $kvpair; | |
1238 | if($kvpair =~ /^\S+=/) { | |
1239 | my ($k, $v) = split(/=/, $kvpair, 2); | |
1240 | $v =~ s/\s//g; | |
1241 | $result->{$k} = $v; | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | my $rsig = $result->{h}; | |
1246 | delete $result->{h}; | |
1247 | ||
1248 | if ($api_key) { | |
1249 | my ($datastr, $vsig) = yubico_compute_param_sig($result, $api_key); | |
1250 | $vsig = uri_unescape($vsig); | |
1251 | die "yubico: result signature verification failed\n" if $rsig ne $vsig; | |
1252 | } | |
1253 | ||
1254 | die "yubico auth failed: $result->{status}\n" if $result->{status} ne 'OK'; | |
1255 | ||
1256 | my $publicid = $result->{publicid} = substr(lc($result->{otp}), 0, 12); | |
1257 | ||
1258 | my $found; | |
1259 | foreach my $k (PVE::Tools::split_list($keys)) { | |
1260 | if ($k eq $publicid) { | |
1261 | $found = 1; | |
1262 | last; | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | die "yubico auth failed: key does not belong to user\n" if !$found; | |
1267 | ||
1268 | return $result; | |
1269 | } | |
1270 | ||
1271 | sub oath_verify_otp { | |
1272 | my ($otp, $keys, $step, $digits) = @_; | |
1273 | ||
1274 | die "oath: missing password\n" if !defined($otp); | |
1275 | die "oath: no associated oath keys\n" if $keys =~ m/^\s+$/; | |
1276 | ||
1277 | $step = 30 if !$step; | |
1278 | $digits = 6 if !$digits; | |
1279 | ||
1280 | my $found; | |
1281 | ||
1282 | my $parser = sub { | |
1283 | my $line = shift; | |
1284 | ||
1285 | if ($line =~ m/^\d{6}$/) { | |
1286 | $found = 1 if $otp eq $line; | |
1287 | } | |
1288 | }; | |
1289 | ||
1290 | foreach my $k (PVE::Tools::split_list($keys)) { | |
1291 | # Note: we generate 3 values to allow small time drift | |
1292 | my $now = localtime(time() - $step); | |
1293 | my $cmd = ['oathtool', '--totp', '--digits', $digits, '-N', $now, '-s', $step, '-w', '2', '-b', $k]; | |
1294 | eval { run_command($cmd, outfunc => $parser, errfunc => sub {}); }; | |
1295 | last if $found; | |
1296 | } | |
1297 | ||
1298 | die "oath auth failed\n" if !$found; | |
1299 | } | |
1300 | ||
1301 | # bash completion helpers | |
1302 | ||
1303 | sub complete_username { | |
1304 | ||
1305 | my $user_cfg = cfs_read_file('user.cfg'); | |
1306 | ||
1307 | return [ keys %{$user_cfg->{users}} ]; | |
1308 | } | |
1309 | ||
1310 | sub complete_group { | |
1311 | ||
1312 | my $user_cfg = cfs_read_file('user.cfg'); | |
1313 | ||
1314 | return [ keys %{$user_cfg->{groups}} ]; | |
1315 | } | |
1316 | ||
1317 | sub complete_realm { | |
1318 | ||
1319 | my $domain_cfg = cfs_read_file('domains.cfg'); | |
1320 | ||
1321 | return [ keys %{$domain_cfg->{ids}} ]; | |
1322 | } | |
1323 | ||
1324 | 1; |