]>
Commit | Line | Data |
---|---|---|
2c3a6c0a DM |
1 | package PVE::AccessControl; |
2 | ||
3 | use strict; | |
4 | use Encode; | |
5 | use Crypt::OpenSSL::Random; | |
6 | use Crypt::OpenSSL::RSA; | |
7 | use MIME::Base64; | |
8 | use Digest::SHA; | |
2c3a6c0a DM |
9 | use PVE::Tools qw(run_command lock_file file_get_contents split_list safe_print); |
10 | use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file); | |
11 | use PVE::JSONSchema; | |
5bb4e06a DM |
12 | |
13 | use PVE::Auth::Plugin; | |
14 | use PVE::Auth::AD; | |
15 | use PVE::Auth::LDAP; | |
16 | use PVE::Auth::PVE; | |
17 | use PVE::Auth::PAM; | |
2c3a6c0a DM |
18 | |
19 | use Data::Dumper; # fixme: remove | |
20 | ||
5bb4e06a DM |
21 | # load and initialize all plugins |
22 | ||
23 | PVE::Auth::AD->register(); | |
24 | PVE::Auth::LDAP->register(); | |
25 | PVE::Auth::PVE->register(); | |
26 | PVE::Auth::PAM->register(); | |
27 | PVE::Auth::Plugin->init(); | |
28 | ||
2c3a6c0a DM |
29 | # $authdir must be writable by root only! |
30 | my $confdir = "/etc/pve"; | |
31 | my $authdir = "$confdir/priv"; | |
32 | my $authprivkeyfn = "$authdir/authkey.key"; | |
33 | my $authpubkeyfn = "$confdir/authkey.pub"; | |
2c3a6c0a DM |
34 | my $pve_www_key_fn = "$confdir/pve-www.key"; |
35 | ||
36 | my $ticket_lifetime = 3600*2; # 2 hours | |
37 | ||
38 | Crypt::OpenSSL::RSA->import_random_seed(); | |
39 | ||
40 | cfs_register_file('user.cfg', | |
41 | \&parse_user_config, | |
42 | \&write_user_config); | |
43 | ||
2c3a6c0a | 44 | |
5bb4e06a DM |
45 | sub verify_username { |
46 | PVE::Auth::Plugin::verify_username(@_); | |
2c3a6c0a DM |
47 | } |
48 | ||
5bb4e06a DM |
49 | sub pve_verify_realm { |
50 | PVE::Auth::Plugin::pve_verify_realm(@_); | |
2c3a6c0a DM |
51 | } |
52 | ||
5bb4e06a | 53 | sub lock_user_config { |
2c3a6c0a DM |
54 | my ($code, $errmsg) = @_; |
55 | ||
5bb4e06a DM |
56 | cfs_lock_file("user.cfg", undef, $code); |
57 | if (my $err = $@) { | |
2c3a6c0a DM |
58 | $errmsg ? die "$errmsg: $err" : die $err; |
59 | } | |
60 | } | |
61 | ||
62 | my $pve_auth_pub_key; | |
63 | sub get_pubkey { | |
64 | ||
65 | return $pve_auth_pub_key if $pve_auth_pub_key; | |
66 | ||
67 | my $input = PVE::Tools::file_get_contents($authpubkeyfn); | |
68 | ||
69 | $pve_auth_pub_key = Crypt::OpenSSL::RSA->new_public_key($input); | |
70 | ||
71 | return $pve_auth_pub_key; | |
72 | } | |
73 | ||
74 | my $csrf_prevention_secret; | |
75 | my $get_csrfr_secret = sub { | |
76 | if (!$csrf_prevention_secret) { | |
77 | my $input = PVE::Tools::file_get_contents($pve_www_key_fn); | |
78 | $csrf_prevention_secret = Digest::SHA::sha1_base64($input); | |
79 | } | |
80 | return $csrf_prevention_secret; | |
81 | }; | |
82 | ||
83 | sub assemble_csrf_prevention_token { | |
84 | my ($username) = @_; | |
85 | ||
86 | my $timestamp = sprintf("%08X", time()); | |
87 | ||
88 | my $digest = Digest::SHA::sha1_base64("$timestamp:$username", &$get_csrfr_secret()); | |
89 | ||
90 | return "$timestamp:$digest"; | |
91 | } | |
92 | ||
93 | sub verify_csrf_prevention_token { | |
94 | my ($username, $token, $noerr) = @_; | |
95 | ||
96 | if ($token =~ m/^([A-Z0-9]{8}):(\S+)$/) { | |
97 | my $sig = $2; | |
98 | my $timestamp = $1; | |
99 | my $ttime = hex($timestamp); | |
100 | ||
101 | my $digest = Digest::SHA::sha1_base64("$timestamp:$username", &$get_csrfr_secret()); | |
102 | ||
103 | my $age = time() - $ttime; | |
104 | return if ($digest eq $sig) && ($age > -300) && ($age < $ticket_lifetime); | |
105 | } | |
106 | ||
107 | die "Permission denied - invalid csrf token\n" if !$noerr; | |
108 | ||
109 | return undef; | |
110 | } | |
111 | ||
112 | my $pve_auth_priv_key; | |
113 | sub get_privkey { | |
114 | ||
115 | return $pve_auth_priv_key if $pve_auth_priv_key; | |
116 | ||
117 | my $input = PVE::Tools::file_get_contents($authprivkeyfn); | |
118 | ||
119 | $pve_auth_priv_key = Crypt::OpenSSL::RSA->new_private_key($input); | |
120 | ||
121 | return $pve_auth_priv_key; | |
122 | } | |
123 | ||
124 | sub assemble_ticket { | |
125 | my ($username) = @_; | |
126 | ||
127 | my $rsa_priv = get_privkey(); | |
128 | ||
129 | my $timestamp = sprintf("%08X", time()); | |
130 | ||
131 | my $plain = "PVE:$username:$timestamp"; | |
132 | ||
133 | my $ticket = $plain . "::" . encode_base64($rsa_priv->sign($plain), ''); | |
134 | ||
135 | return $ticket; | |
136 | } | |
137 | ||
138 | sub verify_ticket { | |
139 | my ($ticket, $noerr) = @_; | |
140 | ||
adf8d771 | 141 | if ($ticket && $ticket =~ m/^(PVE:\S+)::([^:\s]+)$/) { |
2c3a6c0a DM |
142 | my $plain = $1; |
143 | my $sig = $2; | |
144 | ||
145 | my $rsa_pub = get_pubkey(); | |
146 | if ($rsa_pub->verify($plain, decode_base64($sig))) { | |
533219a1 | 147 | if ($plain =~ m/^PVE:(\S+):([A-Z0-9]{8})$/) { |
2c3a6c0a | 148 | my $username = $1; |
533219a1 | 149 | my $timestamp = $2; |
2c3a6c0a DM |
150 | my $ttime = hex($timestamp); |
151 | ||
152 | my $age = time() - $ttime; | |
153 | ||
5bb4e06a | 154 | if (PVE::Auth::Plugin::verify_username($username, 1) && |
533219a1 | 155 | ($age > -300) && ($age < $ticket_lifetime)) { |
2c3a6c0a DM |
156 | return wantarray ? ($username, $age) : $username; |
157 | } | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
162 | die "permission denied - invalid ticket\n" if !$noerr; | |
163 | ||
164 | return undef; | |
165 | } | |
166 | ||
adf8d771 DM |
167 | # VNC tickets |
168 | # - they do not contain the username in plain text | |
169 | # - they are restricted to a specific resource path (example: '/vms/100') | |
170 | sub assemble_vnc_ticket { | |
171 | my ($username, $path) = @_; | |
172 | ||
173 | my $rsa_priv = get_privkey(); | |
174 | ||
175 | my $timestamp = sprintf("%08X", time()); | |
176 | ||
177 | my $plain = "PVEVNC:$timestamp"; | |
178 | ||
179 | $path = normalize_path($path); | |
180 | ||
181 | my $full = "$plain:$username:$path"; | |
182 | ||
183 | my $ticket = $plain . "::" . encode_base64($rsa_priv->sign($full), ''); | |
184 | ||
185 | return $ticket; | |
186 | } | |
187 | ||
188 | sub verify_vnc_ticket { | |
189 | my ($ticket, $username, $path, $noerr) = @_; | |
190 | ||
191 | if ($ticket && $ticket =~ m/^(PVEVNC:\S+)::([^:\s]+)$/) { | |
192 | my $plain = $1; | |
193 | my $sig = $2; | |
194 | my $full = "$plain:$username:$path"; | |
195 | ||
196 | my $rsa_pub = get_pubkey(); | |
197 | # Note: sign only match if $username and $path is correct | |
198 | if ($rsa_pub->verify($full, decode_base64($sig))) { | |
199 | if ($plain =~ m/^PVEVNC:([A-Z0-9]{8})$/) { | |
200 | my $ttime = hex($1); | |
201 | ||
202 | my $age = time() - $ttime; | |
203 | ||
204 | if (($age > -20) && ($age < 40)) { | |
205 | return 1; | |
206 | } | |
207 | } | |
208 | } | |
209 | } | |
210 | ||
211 | die "permission denied - invalid vnc ticket\n" if !$noerr; | |
212 | ||
213 | return undef; | |
214 | } | |
215 | ||
37d45deb | 216 | sub check_user_exist { |
7070c1ae | 217 | my ($usercfg, $username, $noerr) = @_; |
2c3a6c0a | 218 | |
5bb4e06a | 219 | $username = PVE::Auth::Plugin::verify_username($username, $noerr); |
2c3a6c0a DM |
220 | return undef if !$username; |
221 | ||
37d45deb DM |
222 | return $usercfg->{users}->{$username} if $usercfg && $usercfg->{users}->{$username}; |
223 | ||
224 | die "no such user ('$username')\n" if !$noerr; | |
225 | ||
226 | return undef; | |
227 | } | |
228 | ||
229 | sub check_user_enabled { | |
230 | my ($usercfg, $username, $noerr) = @_; | |
231 | ||
232 | my $data = check_user_exist($usercfg, $username, $noerr); | |
233 | return undef if !$data; | |
234 | ||
235 | return 1 if $data->{enable}; | |
2c3a6c0a DM |
236 | |
237 | return 1 if $username eq 'root@pam'; # root is always enabled | |
238 | ||
37d45deb | 239 | die "user '$username' is disabled\n" if !$noerr; |
7070c1ae DM |
240 | |
241 | return undef; | |
2c3a6c0a DM |
242 | } |
243 | ||
244 | # password should be utf8 encoded | |
245 | sub authenticate_user { | |
246 | my ($username, $password) = @_; | |
247 | ||
248 | die "no username specified\n" if !$username; | |
249 | ||
5bb4e06a | 250 | my ($ruid, $realm); |
2c3a6c0a | 251 | |
5bb4e06a | 252 | ($username, $ruid, $realm) = PVE::Auth::Plugin::verify_username($username); |
2c3a6c0a DM |
253 | |
254 | my $usercfg = cfs_read_file('user.cfg'); | |
255 | ||
7070c1ae DM |
256 | eval { check_user_enabled($usercfg, $username); }; |
257 | if (my $err = $@) { | |
2c3a6c0a | 258 | sleep(2); |
7070c1ae | 259 | die $err; |
2c3a6c0a DM |
260 | } |
261 | ||
262 | my $ctime = time(); | |
263 | my $expire = $usercfg->{users}->{$username}->{expire}; | |
264 | ||
265 | if ($expire && ($expire < $ctime)) { | |
266 | sleep(2); | |
267 | die "account expired\n" | |
268 | } | |
269 | ||
5bb4e06a | 270 | my $domain_cfg = cfs_read_file('domains.cfg'); |
2c3a6c0a | 271 | |
5bb4e06a DM |
272 | eval { |
273 | my $cfg = $domain_cfg->{ids}->{$realm}; | |
274 | die "auth domain '$realm' does not exists\n" if !$cfg; | |
275 | my $plugin = PVE::Auth::Plugin->lookup($cfg->{type}); | |
276 | $plugin->authenticate_user($cfg, $realm, $ruid, $password); | |
277 | }; | |
278 | if (my $err = $@) { | |
279 | sleep(2); # timeout after failed auth | |
280 | die $err; | |
281 | } | |
2c3a6c0a | 282 | |
5bb4e06a | 283 | return $username; |
2c3a6c0a DM |
284 | } |
285 | ||
286 | sub domain_set_password { | |
5bb4e06a | 287 | my ($realm, $username, $password) = @_; |
2c3a6c0a DM |
288 | |
289 | die "no auth domain specified" if !$realm; | |
290 | ||
5bb4e06a DM |
291 | my $domain_cfg = cfs_read_file('domains.cfg'); |
292 | ||
293 | my $cfg = $domain_cfg->{ids}->{$realm}; | |
294 | die "auth domain '$realm' does not exists\n" if !$cfg; | |
295 | my $plugin = PVE::Auth::Plugin->lookup($cfg->{type}); | |
296 | $plugin->store_password($cfg, $realm, $username, $password); | |
2c3a6c0a DM |
297 | } |
298 | ||
299 | sub add_user_group { | |
300 | ||
301 | my ($username, $usercfg, $group) = @_; | |
302 | $usercfg->{users}->{$username}->{groups}->{$group} = 1; | |
303 | $usercfg->{groups}->{$group}->{users}->{$username} = 1; | |
304 | } | |
305 | ||
306 | sub delete_user_group { | |
307 | ||
308 | my ($username, $usercfg) = @_; | |
309 | ||
310 | foreach my $group (keys %{$usercfg->{groups}}) { | |
311 | ||
312 | delete ($usercfg->{groups}->{$group}->{users}->{$username}) | |
313 | if $usercfg->{groups}->{$group}->{users}->{$username}; | |
314 | } | |
315 | } | |
316 | ||
317 | sub delete_user_acl { | |
318 | ||
319 | my ($username, $usercfg) = @_; | |
320 | ||
321 | foreach my $acl (keys %{$usercfg->{acl}}) { | |
322 | ||
323 | delete ($usercfg->{acl}->{$acl}->{users}->{$username}) | |
324 | if $usercfg->{acl}->{$acl}->{users}->{$username}; | |
325 | } | |
2c3a6c0a | 326 | } |
39c85db8 | 327 | |
2c3a6c0a DM |
328 | sub delete_group_acl { |
329 | ||
330 | my ($group, $usercfg) = @_; | |
331 | ||
332 | foreach my $acl (keys %{$usercfg->{acl}}) { | |
333 | ||
334 | delete ($usercfg->{acl}->{$acl}->{groups}->{$group}) | |
335 | if $usercfg->{acl}->{$acl}->{groups}->{$group}; | |
336 | } | |
39c85db8 DM |
337 | } |
338 | ||
339 | sub delete_pool_acl { | |
340 | ||
341 | my ($pool, $usercfg) = @_; | |
2c3a6c0a | 342 | |
39c85db8 DM |
343 | my $path = "/pool/$pool"; |
344 | ||
345 | foreach my $aclpath (keys %{$usercfg->{acl}}) { | |
346 | delete ($usercfg->{acl}->{$aclpath}) | |
347 | if $usercfg->{acl}->{$aclpath} eq 'path'; | |
348 | } | |
2c3a6c0a DM |
349 | } |
350 | ||
351 | # we automatically create some predefined roles by splitting privs | |
352 | # into 3 groups (per category) | |
353 | # root: only root is allowed to do that | |
354 | # admin: an administrator can to that | |
355 | # user: a normak user/customer can to that | |
356 | my $privgroups = { | |
357 | VM => { | |
358 | root => [], | |
359 | admin => [ | |
c0fead8c | 360 | 'VM.Config.Disk', |
c0fead8c DM |
361 | 'VM.Config.CPU', |
362 | 'VM.Config.Memory', | |
363 | 'VM.Config.Network', | |
364 | 'VM.Config.HWType', | |
365 | 'VM.Config.Options', # covers all other things | |
2c3a6c0a | 366 | 'VM.Allocate', |
7b395f99 | 367 | 'VM.Clone', |
2c3a6c0a | 368 | 'VM.Migrate', |
c0fead8c | 369 | 'VM.Monitor', |
e3e6510c | 370 | 'VM.Snapshot', |
2c3a6c0a DM |
371 | ], |
372 | user => [ | |
cc7bdf33 | 373 | 'VM.Config.CDROM', # change CDROM media |
2c3a6c0a | 374 | 'VM.Console', |
68d5a86d | 375 | 'VM.Backup', |
2c3a6c0a DM |
376 | 'VM.PowerMgmt', |
377 | ], | |
378 | audit => [ | |
82b63965 | 379 | 'VM.Audit', |
2c3a6c0a DM |
380 | ], |
381 | }, | |
382 | Sys => { | |
383 | root => [ | |
384 | 'Sys.PowerMgmt', | |
37d45deb | 385 | 'Sys.Modify', # edit/change node settings |
2c3a6c0a DM |
386 | ], |
387 | admin => [ | |
2e376c58 | 388 | 'Permissions.Modify', |
2c3a6c0a DM |
389 | 'Sys.Console', |
390 | 'Sys.Syslog', | |
391 | ], | |
392 | user => [], | |
393 | audit => [ | |
394 | 'Sys.Audit', | |
395 | ], | |
396 | }, | |
397 | Datastore => { | |
2e376c58 | 398 | root => [], |
19f60b5e DM |
399 | admin => [ |
400 | 'Datastore.Allocate', | |
373cb383 | 401 | 'Datastore.AllocateTemplate', |
19f60b5e | 402 | ], |
2c3a6c0a DM |
403 | user => [ |
404 | 'Datastore.AllocateSpace', | |
405 | ], | |
406 | audit => [ | |
407 | 'Datastore.Audit', | |
408 | ], | |
409 | }, | |
12683df7 | 410 | User => { |
82b63965 DM |
411 | root => [ |
412 | 'Realm.Allocate', | |
413 | ], | |
12683df7 DM |
414 | admin => [ |
415 | 'User.Modify', | |
82b63965 DM |
416 | 'Group.Allocate', # edit/change group settings |
417 | 'Realm.AllocateUser', | |
19f60b5e | 418 | ], |
12683df7 DM |
419 | user => [], |
420 | audit => [], | |
421 | }, | |
dee1c882 DM |
422 | Pool => { |
423 | root => [], | |
424 | admin => [ | |
425 | 'Pool.Allocate', # create/delete pools | |
426 | ], | |
427 | user => [], | |
428 | audit => [], | |
429 | }, | |
2c3a6c0a DM |
430 | }; |
431 | ||
432 | my $valid_privs = {}; | |
433 | ||
434 | my $special_roles = { | |
435 | 'NoAccess' => {}, # no priviledges | |
436 | 'Administrator' => $valid_privs, # all priviledges | |
437 | }; | |
438 | ||
439 | sub create_roles { | |
440 | ||
441 | foreach my $cat (keys %$privgroups) { | |
442 | my $cd = $privgroups->{$cat}; | |
443 | foreach my $p (@{$cd->{root}}, @{$cd->{admin}}, | |
444 | @{$cd->{user}}, @{$cd->{audit}}) { | |
445 | $valid_privs->{$p} = 1; | |
446 | } | |
447 | foreach my $p (@{$cd->{admin}}, @{$cd->{user}}, @{$cd->{audit}}) { | |
448 | ||
449 | $special_roles->{"PVE${cat}Admin"}->{$p} = 1; | |
450 | $special_roles->{"PVEAdmin"}->{$p} = 1; | |
451 | } | |
452 | if (scalar(@{$cd->{user}})) { | |
453 | foreach my $p (@{$cd->{user}}, @{$cd->{audit}}) { | |
454 | $special_roles->{"PVE${cat}User"}->{$p} = 1; | |
455 | } | |
456 | } | |
457 | foreach my $p (@{$cd->{audit}}) { | |
458 | $special_roles->{"PVEAuditor"}->{$p} = 1; | |
459 | } | |
460 | } | |
ff4b2235 | 461 | |
7b395f99 | 462 | $special_roles->{"PVETemplateUser"} = { 'VM.Clone' => 1, 'VM.Audit' => 1 }; |
2c3a6c0a DM |
463 | }; |
464 | ||
465 | create_roles(); | |
466 | ||
2c3a6c0a DM |
467 | sub add_role_privs { |
468 | my ($role, $usercfg, $privs) = @_; | |
469 | ||
470 | return if !$privs; | |
471 | ||
472 | die "role '$role' does not exist\n" if !$usercfg->{roles}->{$role}; | |
473 | ||
474 | foreach my $priv (split_list($privs)) { | |
475 | if (defined ($valid_privs->{$priv})) { | |
476 | $usercfg->{roles}->{$role}->{$priv} = 1; | |
477 | } else { | |
478 | die "invalid priviledge '$priv'\n"; | |
479 | } | |
480 | } | |
481 | } | |
482 | ||
483 | sub normalize_path { | |
484 | my $path = shift; | |
485 | ||
4bc17477 | 486 | $path =~ s|/+|/|g; |
2c3a6c0a DM |
487 | |
488 | $path =~ s|/$||; | |
489 | ||
490 | $path = '/' if !$path; | |
491 | ||
4bc17477 DM |
492 | $path = "/$path" if $path !~ m|^/|; |
493 | ||
2c3a6c0a DM |
494 | return undef if $path !~ m|^[[:alnum:]\-\_\/]+$|; |
495 | ||
496 | return $path; | |
497 | } | |
498 | ||
2c3a6c0a DM |
499 | |
500 | PVE::JSONSchema::register_format('pve-groupid', \&verify_groupname); | |
501 | sub verify_groupname { | |
502 | my ($groupname, $noerr) = @_; | |
503 | ||
504 | if ($groupname !~ m/^[A-Za-z0-9\.\-_]+$/) { | |
505 | ||
506 | die "group name '$groupname' contains invalid characters\n" if !$noerr; | |
507 | ||
508 | return undef; | |
509 | } | |
510 | ||
511 | return $groupname; | |
512 | } | |
513 | ||
514 | PVE::JSONSchema::register_format('pve-roleid', \&verify_rolename); | |
515 | sub verify_rolename { | |
516 | my ($rolename, $noerr) = @_; | |
517 | ||
518 | if ($rolename !~ m/^[A-Za-z0-9\.\-_]+$/) { | |
519 | ||
520 | die "role name '$rolename' contains invalid characters\n" if !$noerr; | |
521 | ||
522 | return undef; | |
523 | } | |
524 | ||
525 | return $rolename; | |
526 | } | |
527 | ||
39c85db8 DM |
528 | PVE::JSONSchema::register_format('pve-poolid', \&verify_groupname); |
529 | sub verify_poolname { | |
530 | my ($poolname, $noerr) = @_; | |
531 | ||
532 | if ($poolname !~ m/^[A-Za-z0-9\.\-_]+$/) { | |
533 | ||
534 | die "pool name '$poolname' contains invalid characters\n" if !$noerr; | |
535 | ||
536 | return undef; | |
537 | } | |
538 | ||
539 | return $poolname; | |
540 | } | |
541 | ||
2c3a6c0a DM |
542 | PVE::JSONSchema::register_format('pve-priv', \&verify_privname); |
543 | sub verify_privname { | |
544 | my ($priv, $noerr) = @_; | |
545 | ||
546 | if (!$valid_privs->{$priv}) { | |
547 | die "invalid priviledge '$priv'\n" if !$noerr; | |
548 | ||
549 | return undef; | |
550 | } | |
551 | ||
552 | return $priv; | |
553 | } | |
554 | ||
555 | sub userconfig_force_defaults { | |
556 | my ($cfg) = @_; | |
557 | ||
558 | foreach my $r (keys %$special_roles) { | |
559 | $cfg->{roles}->{$r} = $special_roles->{$r}; | |
560 | } | |
561 | ||
562 | # fixme: remove 'root' group (not required)? | |
563 | ||
564 | # add root user | |
565 | $cfg->{users}->{'root@pam'}->{enable} = 1; | |
566 | } | |
567 | ||
568 | sub parse_user_config { | |
569 | my ($filename, $raw) = @_; | |
570 | ||
571 | my $cfg = {}; | |
572 | ||
573 | userconfig_force_defaults($cfg); | |
574 | ||
575 | while ($raw && $raw =~ s/^(.*?)(\n|$)//) { | |
576 | my $line = $1; | |
577 | ||
578 | next if $line =~ m/^\s*$/; # skip empty lines | |
579 | ||
580 | my @data; | |
581 | ||
582 | foreach my $d (split (/:/, $line)) { | |
583 | $d =~ s/^\s+//; | |
584 | $d =~ s/\s+$//; | |
585 | push @data, $d | |
586 | } | |
587 | ||
588 | my $et = shift @data; | |
589 | ||
590 | if ($et eq 'user') { | |
591 | my ($user, $enable, $expire, $firstname, $lastname, $email, $comment) = @data; | |
592 | ||
5bb4e06a | 593 | my (undef, undef, $realm) = PVE::Auth::Plugin::verify_username($user, 1); |
2c3a6c0a DM |
594 | if (!$realm) { |
595 | warn "user config - ignore user '$user' - invalid user name\n"; | |
596 | next; | |
597 | } | |
598 | ||
599 | $enable = $enable ? 1 : 0; | |
600 | ||
601 | $expire = 0 if !$expire; | |
602 | ||
603 | if ($expire !~ m/^\d+$/) { | |
604 | warn "user config - ignore user '$user' - (illegal characters in expire '$expire')\n"; | |
605 | next; | |
606 | } | |
607 | $expire = int($expire); | |
608 | ||
609 | #if (!verify_groupname ($group, 1)) { | |
610 | # warn "user config - ignore user '$user' - invalid characters in group name\n"; | |
611 | # next; | |
612 | #} | |
613 | ||
614 | $cfg->{users}->{$user} = { | |
615 | enable => $enable, | |
616 | # group => $group, | |
617 | }; | |
618 | $cfg->{users}->{$user}->{firstname} = PVE::Tools::decode_text($firstname) if $firstname; | |
619 | $cfg->{users}->{$user}->{lastname} = PVE::Tools::decode_text($lastname) if $lastname; | |
620 | $cfg->{users}->{$user}->{email} = $email; | |
621 | $cfg->{users}->{$user}->{comment} = PVE::Tools::decode_text($comment) if $comment; | |
622 | $cfg->{users}->{$user}->{expire} = $expire; | |
623 | ||
624 | #$cfg->{users}->{$user}->{groups}->{$group} = 1; | |
625 | #$cfg->{groups}->{$group}->{$user} = 1; | |
626 | ||
627 | } elsif ($et eq 'group') { | |
628 | my ($group, $userlist, $comment) = @data; | |
629 | ||
630 | if (!verify_groupname($group, 1)) { | |
631 | warn "user config - ignore group '$group' - invalid characters in group name\n"; | |
632 | next; | |
633 | } | |
634 | ||
635 | # make sure to add the group (even if there are no members) | |
636 | $cfg->{groups}->{$group} = { users => {} } if !$cfg->{groups}->{$group}; | |
637 | ||
638 | $cfg->{groups}->{$group}->{comment} = PVE::Tools::decode_text($comment) if $comment; | |
639 | ||
640 | foreach my $user (split_list($userlist)) { | |
641 | ||
5bb4e06a | 642 | if (!PVE::Auth::Plugin::verify_username($user, 1)) { |
2c3a6c0a DM |
643 | warn "user config - ignore invalid group member '$user'\n"; |
644 | next; | |
645 | } | |
646 | ||
647 | if ($cfg->{users}->{$user}) { # user exists | |
648 | $cfg->{users}->{$user}->{groups}->{$group} = 1; | |
649 | $cfg->{groups}->{$group}->{users}->{$user} = 1; | |
650 | } else { | |
651 | warn "user config - ignore invalid group member '$user'\n"; | |
652 | } | |
653 | } | |
654 | ||
655 | } elsif ($et eq 'role') { | |
656 | my ($role, $privlist) = @data; | |
657 | ||
658 | if (!verify_rolename($role, 1)) { | |
659 | warn "user config - ignore role '$role' - invalid characters in role name\n"; | |
660 | next; | |
661 | } | |
662 | ||
663 | # make sure to add the role (even if there are no privileges) | |
664 | $cfg->{roles}->{$role} = {} if !$cfg->{roles}->{$role}; | |
665 | ||
666 | foreach my $priv (split_list($privlist)) { | |
667 | if (defined ($valid_privs->{$priv})) { | |
668 | $cfg->{roles}->{$role}->{$priv} = 1; | |
669 | } else { | |
670 | warn "user config - ignore invalid priviledge '$priv'\n"; | |
671 | } | |
672 | } | |
673 | ||
674 | } elsif ($et eq 'acl') { | |
675 | my ($propagate, $pathtxt, $uglist, $rolelist) = @data; | |
676 | ||
677 | if (my $path = normalize_path($pathtxt)) { | |
678 | foreach my $role (split_list($rolelist)) { | |
679 | ||
680 | if (!verify_rolename($role, 1)) { | |
681 | warn "user config - ignore invalid role name '$role' in acl\n"; | |
682 | next; | |
683 | } | |
684 | ||
685 | foreach my $ug (split_list($uglist)) { | |
9b217226 | 686 | if ($ug =~ m/^@(\S+)$/) { |
2c3a6c0a DM |
687 | my $group = $1; |
688 | if ($cfg->{groups}->{$group}) { # group exists | |
689 | $cfg->{acl}->{$path}->{groups}->{$group}->{$role} = $propagate; | |
690 | } else { | |
691 | warn "user config - ignore invalid acl group '$group'\n"; | |
692 | } | |
5bb4e06a | 693 | } elsif (PVE::Auth::Plugin::verify_username($ug, 1)) { |
2c3a6c0a DM |
694 | if ($cfg->{users}->{$ug}) { # user exists |
695 | $cfg->{acl}->{$path}->{users}->{$ug}->{$role} = $propagate; | |
696 | } else { | |
697 | warn "user config - ignore invalid acl member '$ug'\n"; | |
698 | } | |
699 | } else { | |
700 | warn "user config - invalid user/group '$ug' in acl\n"; | |
701 | } | |
702 | } | |
703 | } | |
704 | } else { | |
705 | warn "user config - ignore invalid path in acl '$pathtxt'\n"; | |
706 | } | |
4bc17477 | 707 | } elsif ($et eq 'pool') { |
39c85db8 | 708 | my ($pool, $comment, $vmlist, $storelist) = @data; |
4bc17477 | 709 | |
39c85db8 DM |
710 | if (!verify_poolname($pool, 1)) { |
711 | warn "user config - ignore pool '$pool' - invalid characters in pool name\n"; | |
712 | next; | |
713 | } | |
4bc17477 | 714 | |
39c85db8 DM |
715 | # make sure to add the pool (even if there are no members) |
716 | $cfg->{pools}->{$pool} = { vms => {}, storage => {} } if !$cfg->{pools}->{$pool}; | |
4bc17477 | 717 | |
39c85db8 | 718 | $cfg->{pools}->{$pool}->{comment} = PVE::Tools::decode_text($comment) if $comment; |
4bc17477 | 719 | |
39c85db8 DM |
720 | foreach my $vmid (split_list($vmlist)) { |
721 | if ($vmid !~ m/^\d+$/) { | |
722 | warn "user config - ignore invalid vmid '$vmid' in pool '$pool'\n"; | |
723 | next; | |
4bc17477 | 724 | } |
39c85db8 | 725 | $vmid = int($vmid); |
4bc17477 | 726 | |
39c85db8 DM |
727 | if ($cfg->{vms}->{$vmid}) { |
728 | warn "user config - ignore duplicate vmid '$vmid' in pool '$pool'\n"; | |
729 | next; | |
4bc17477 DM |
730 | } |
731 | ||
39c85db8 DM |
732 | $cfg->{pools}->{$pool}->{vms}->{$vmid} = 1; |
733 | ||
734 | # record vmid ==> pool relation | |
735 | $cfg->{vms}->{$vmid} = $pool; | |
736 | } | |
737 | ||
738 | foreach my $storeid (split_list($storelist)) { | |
739 | if ($storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) { | |
740 | warn "user config - ignore invalid storage '$storeid' in pool '$pool'\n"; | |
741 | next; | |
742 | } | |
743 | $cfg->{pools}->{$pool}->{storage}->{$storeid} = 1; | |
4bc17477 | 744 | } |
2c3a6c0a DM |
745 | } else { |
746 | warn "user config - ignore config line: $line\n"; | |
747 | } | |
748 | } | |
749 | ||
750 | userconfig_force_defaults($cfg); | |
751 | ||
752 | return $cfg; | |
753 | } | |
754 | ||
2c3a6c0a DM |
755 | sub write_user_config { |
756 | my ($filename, $cfg) = @_; | |
757 | ||
758 | my $data = ''; | |
759 | ||
760 | foreach my $user (keys %{$cfg->{users}}) { | |
2c3a6c0a DM |
761 | my $d = $cfg->{users}->{$user}; |
762 | my $firstname = $d->{firstname} ? PVE::Tools::encode_text($d->{firstname}) : ''; | |
763 | my $lastname = $d->{lastname} ? PVE::Tools::encode_text($d->{lastname}) : ''; | |
764 | my $email = $d->{email} || ''; | |
765 | my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : ''; | |
5eabc984 | 766 | my $expire = int($d->{expire} || 0); |
2c3a6c0a DM |
767 | my $enable = $d->{enable} ? 1 : 0; |
768 | $data .= "user:$user:$enable:$expire:$firstname:$lastname:$email:$comment:\n"; | |
769 | } | |
770 | ||
771 | $data .= "\n"; | |
772 | ||
773 | foreach my $group (keys %{$cfg->{groups}}) { | |
774 | my $d = $cfg->{groups}->{$group}; | |
775 | my $list = join (',', keys %{$d->{users}}); | |
776 | my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : ''; | |
777 | $data .= "group:$group:$list:$comment:\n"; | |
778 | } | |
779 | ||
780 | $data .= "\n"; | |
781 | ||
39c85db8 DM |
782 | foreach my $pool (keys %{$cfg->{pools}}) { |
783 | my $d = $cfg->{pools}->{$pool}; | |
4bc17477 DM |
784 | my $vmlist = join (',', keys %{$d->{vms}}); |
785 | my $storelist = join (',', keys %{$d->{storage}}); | |
786 | my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : ''; | |
39c85db8 | 787 | $data .= "pool:$pool:$comment:$vmlist:$storelist:\n"; |
4bc17477 DM |
788 | } |
789 | ||
790 | $data .= "\n"; | |
791 | ||
2c3a6c0a DM |
792 | foreach my $role (keys %{$cfg->{roles}}) { |
793 | next if $special_roles->{$role}; | |
794 | ||
795 | my $d = $cfg->{roles}->{$role}; | |
796 | my $list = join (',', keys %$d); | |
797 | $data .= "role:$role:$list:\n"; | |
798 | } | |
799 | ||
800 | $data .= "\n"; | |
801 | ||
802 | foreach my $path (sort keys %{$cfg->{acl}}) { | |
803 | my $d = $cfg->{acl}->{$path}; | |
804 | ||
805 | my $ra = {}; | |
806 | ||
807 | foreach my $group (keys %{$d->{groups}}) { | |
808 | my $l0 = ''; | |
809 | my $l1 = ''; | |
810 | foreach my $role (sort keys %{$d->{groups}->{$group}}) { | |
811 | my $propagate = $d->{groups}->{$group}->{$role}; | |
812 | if ($propagate) { | |
813 | $l1 .= ',' if $l1; | |
814 | $l1 .= $role; | |
815 | } else { | |
816 | $l0 .= ',' if $l0; | |
817 | $l0 .= $role; | |
818 | } | |
819 | } | |
820 | $ra->{0}->{$l0}->{"\@$group"} = 1 if $l0; | |
821 | $ra->{1}->{$l1}->{"\@$group"} = 1 if $l1; | |
822 | } | |
823 | ||
824 | foreach my $user (keys %{$d->{users}}) { | |
825 | # no need to save, because root is always 'Administartor' | |
826 | next if $user eq 'root@pam'; | |
827 | ||
828 | my $l0 = ''; | |
829 | my $l1 = ''; | |
830 | foreach my $role (sort keys %{$d->{users}->{$user}}) { | |
831 | my $propagate = $d->{users}->{$user}->{$role}; | |
832 | if ($propagate) { | |
833 | $l1 .= ',' if $l1; | |
834 | $l1 .= $role; | |
835 | } else { | |
836 | $l0 .= ',' if $l0; | |
837 | $l0 .= $role; | |
838 | } | |
839 | } | |
840 | $ra->{0}->{$l0}->{$user} = 1 if $l0; | |
841 | $ra->{1}->{$l1}->{$user} = 1 if $l1; | |
842 | } | |
843 | ||
844 | foreach my $rolelist (sort keys %{$ra->{0}}) { | |
845 | my $uglist = join (',', keys %{$ra->{0}->{$rolelist}}); | |
846 | $data .= "acl:0:$path:$uglist:$rolelist:\n"; | |
847 | } | |
848 | foreach my $rolelist (sort keys %{$ra->{1}}) { | |
849 | my $uglist = join (',', keys %{$ra->{1}->{$rolelist}}); | |
850 | $data .= "acl:1:$path:$uglist:$rolelist:\n"; | |
851 | } | |
852 | } | |
853 | ||
854 | return $data; | |
855 | } | |
856 | ||
857 | sub roles { | |
858 | my ($cfg, $user, $path) = @_; | |
859 | ||
4bc17477 DM |
860 | # NOTE: we do not consider pools here. |
861 | # You need to use $rpcenv->roles() instead if you want that. | |
862 | ||
2c3a6c0a DM |
863 | return 'Administrator' if $user eq 'root@pam'; # root can do anything |
864 | ||
865 | my $perm = {}; | |
866 | ||
867 | foreach my $p (sort keys %{$cfg->{acl}}) { | |
868 | my $final = ($path eq $p); | |
869 | ||
870 | next if !(($p eq '/') || $final || ($path =~ m|^$p/|)); | |
871 | ||
872 | my $acl = $cfg->{acl}->{$p}; | |
873 | ||
874 | #print "CHECKACL $path $p\n"; | |
875 | #print "ACL $path = " . Dumper ($acl); | |
876 | ||
877 | if (my $ri = $acl->{users}->{$user}) { | |
878 | my $new; | |
879 | foreach my $role (keys %$ri) { | |
880 | my $propagate = $ri->{$role}; | |
881 | if ($final || $propagate) { | |
882 | #print "APPLY ROLE $p $user $role\n"; | |
883 | $new = {} if !$new; | |
884 | $new->{$role} = 1; | |
885 | } | |
886 | } | |
887 | if ($new) { | |
888 | $perm = $new; # overwrite previous settings | |
889 | next; # user privs always override group privs | |
890 | } | |
891 | } | |
892 | ||
893 | my $new; | |
894 | foreach my $g (keys %{$acl->{groups}}) { | |
895 | next if !$cfg->{groups}->{$g}->{users}->{$user}; | |
896 | if (my $ri = $acl->{groups}->{$g}) { | |
897 | foreach my $role (keys %$ri) { | |
898 | my $propagate = $ri->{$role}; | |
899 | if ($final || $propagate) { | |
900 | #print "APPLY ROLE $p \@$g $role\n"; | |
901 | $new = {} if !$new; | |
902 | $new->{$role} = 1; | |
903 | } | |
904 | } | |
905 | } | |
906 | } | |
907 | if ($new) { | |
908 | $perm = $new; # overwrite previous settings | |
909 | next; | |
910 | } | |
2c3a6c0a DM |
911 | } |
912 | ||
4bc17477 DM |
913 | return ('NoAccess') if defined ($perm->{NoAccess}); |
914 | #return () if defined ($perm->{NoAccess}); | |
2c3a6c0a DM |
915 | |
916 | #print "permission $user $path = " . Dumper ($perm); | |
917 | ||
4bc17477 | 918 | my @ra = keys %$perm; |
2c3a6c0a DM |
919 | |
920 | #print "roles $user $path = " . join (',', @ra) . "\n"; | |
921 | ||
922 | return @ra; | |
923 | } | |
924 | ||
925 | sub permission { | |
926 | my ($cfg, $user, $path) = @_; | |
927 | ||
5bb4e06a | 928 | $user = PVE::Auth::Plugin::verify_username($user, 1); |
2c3a6c0a DM |
929 | return {} if !$user; |
930 | ||
931 | my @ra = roles($cfg, $user, $path); | |
932 | ||
933 | my $privs = {}; | |
934 | ||
935 | foreach my $role (@ra) { | |
936 | if (my $privset = $cfg->{roles}->{$role}) { | |
937 | foreach my $p (keys %$privset) { | |
938 | $privs->{$p} = 1; | |
939 | } | |
940 | } | |
941 | } | |
942 | ||
943 | #print "priviledges $user $path = " . Dumper ($privs); | |
944 | ||
945 | return $privs; | |
946 | } | |
947 | ||
948 | sub check_permissions { | |
949 | my ($username, $path, $privlist) = @_; | |
950 | ||
951 | $path = normalize_path($path); | |
952 | my $usercfg = cfs_read_file('user.cfg'); | |
953 | my $perm = permission($usercfg, $username, $path); | |
954 | ||
955 | foreach my $priv (split_list($privlist)) { | |
956 | return undef if !$perm->{$priv}; | |
957 | }; | |
958 | ||
959 | return 1; | |
960 | } | |
961 | ||
018ae3a9 DM |
962 | sub add_vm_to_pool { |
963 | my ($vmid, $pool) = @_; | |
964 | ||
965 | my $addVMtoPoolFn = sub { | |
966 | my $usercfg = cfs_read_file("user.cfg"); | |
967 | if (my $data = $usercfg->{pools}->{$pool}) { | |
968 | $data->{vms}->{$vmid} = 1; | |
969 | $usercfg->{vms}->{$vmid} = $pool; | |
970 | cfs_write_file("user.cfg", $usercfg); | |
971 | } | |
972 | }; | |
973 | ||
974 | lock_user_config($addVMtoPoolFn, "can't add VM $vmid to pool '$pool'"); | |
975 | } | |
976 | ||
977 | sub remove_vm_from_pool { | |
978 | my ($vmid) = @_; | |
979 | ||
980 | my $delVMfromPoolFn = sub { | |
981 | my $usercfg = cfs_read_file("user.cfg"); | |
982 | if (my $pool = $usercfg->{vms}->{$vmid}) { | |
983 | if (my $data = $usercfg->{pools}->{$pool}) { | |
984 | delete $data->{vms}->{$vmid}; | |
985 | delete $usercfg->{vms}->{$vmid}; | |
986 | cfs_write_file("user.cfg", $usercfg); | |
987 | } | |
988 | } | |
989 | }; | |
990 | ||
991 | lock_user_config($delVMfromPoolFn, "pool cleanup for VM $vmid failed"); | |
992 | } | |
993 | ||
2c3a6c0a | 994 | 1; |