diff --git a/acl/CHANGELOG b/acl/CHANGELOG index 53b781692..86f0681d2 100644 --- a/acl/CHANGELOG +++ b/acl/CHANGELOG @@ -47,3 +47,5 @@ Fixed the display of modules granted to groups. Added a per-user option to opt out of forced password changes after a certain number of days. A human-readable description of the password restrictions regular expression can be entered, for use in error messages. Webmin users can now be given temporary passwords, which they are forced to change at the next login. Thanks to GE Medical Systems for supporting this feature. +---- Changes since 1.410 ---- +Added an API function to allow easier anonymous module access setup. diff --git a/acl/acl-lib.pl b/acl/acl-lib.pl index f2c4e6928..e3065f9e9 100644 --- a/acl/acl-lib.pl +++ b/acl/acl-lib.pl @@ -889,5 +889,54 @@ local $hash = &hash_session_id($sid); return $sessiondb{$hash} ? $hash : $sid; } +# setup_anonymous_access(path, module) +# Grants anonymous access to some path. By default, the user for other anonymous +# access will be used, or if there is none, a user named 'anonymous' will be +# created and granted access to the module. +sub setup_anonymous_access +{ +local ($path, $mod) = @_; + +# Find out what users and paths we grant access to currently +local %miniserv; +&get_miniserv_config(\%miniserv); +local @anon = split(/\s+/, $miniserv{'anonymous'}); +local $found = 0; +local $user; +foreach my $a (@anon) { + local ($p, $u) = split(/=/, $a); + $found++ if ($p eq $path); + $user = $u; + } +return 1 if ($found); # Already setup + +if (!$user) { + # Create a user if need be + $user = "anonymous"; + local $uinfo = { 'name' => $user, + 'pass' => '*LK*', + 'modules' => [ $mod ], + }; + &create_user($uinfo); + } +else { + # Make sure the user has the module + local ($uinfo) = grep { $_->{'name'} eq $user } &list_users(); + if ($uinfo && &indexof($mod, @{$uinfo->{'modules'}}) < 0) { + push(@{$uinfo->{'modules'}}, $mod); + &modify_user($uinfo->{'name'}, $uinfo); + } + else { + print STDERR "Anonymous access is granted to user $user, but he doesn't exist!\n"; + } + } + +# Grant access to the user and path +push(@anon, "$path=$user"); +$miniserv{'anonymous'} = join(" ", @anon); +&put_miniserv_config(\%miniserv); +&reload_miniserv(); +} + 1;