Handle hostnames with upper-case letters
[webmin.git] / postgresql / save_grant.cgi
1 #!/usr/local/bin/perl
2 # save_grant.cgi
3 # Update privilege grants on some table/view/index
4
5 require './postgresql-lib.pl';
6 &ReadParse();
7 $access{'users'} || &error($text{'grant_ecannot'});
8
9 # Remove old privileges on object
10 if (&supports_schemas($in{'db'})) {
11         $s = &execute_sql($in{'db'}, 'select relname, relacl, nspname from pg_class, pg_namespace where relnamespace = pg_namespace.oid and (relkind = \'r\' OR relkind = \'S\') and relname !~ \'^pg_\' order by relname');
12         $ss = 1;
13         }
14 else {
15         $s = &execute_sql($in{'db'}, 'select relname, relacl, \'public\' from pg_class where (relkind = \'r\' OR relkind = \'S\') and relname !~ \'^pg_\' order by relname');
16         $ss = 0;
17         }
18 foreach $g (@{$s->{'data'}}) {
19         if ($g->[0] eq $in{'table'} &&
20             $g->[2] eq $in{'ns'}) {
21                 $g->[1] =~ s/^\{//; $g->[1] =~ s/\}$//;
22                 @grant = map { /^"(.*)=(.*)"$/ || /^(.*)=(.*)$/; [ $1, $2 ] }
23                              split(/,/, $g->[1]);
24                 }
25         }
26 $qt = $ss ? &quote_table($in{'ns'}.".".$in{'table'})
27           : &quote_table($in{'table'});
28 foreach $g (@grant) {
29         next if (!$g->[1]);
30         if ($g->[0] eq '') {
31                 $who = "public";
32                 }
33         elsif ($g->[0] =~ /group\s+(\S+)/) {
34                 $who = "group \"$1\"";
35                 }
36         else {
37                 $who = "\"$g->[0]\"";
38                 }
39         &execute_sql_logged($in{'db'}, "revoke all on $qt from $who");
40         }
41
42 # Grant new privileges
43 for($i=0; defined($in{"user_$i"}); $i++) {
44         @what = split(/\0/, $in{"what_$i"});
45         next if (!$in{"user_$i"} || !@what);
46         if ($in{"user_$i"} eq "public") {
47                 $who = "public";
48                 }
49         elsif ($in{"user_$i"} =~ /^group\s+(\S+)$/) {
50                 $who = "group \"$1\"";
51                 }
52         else {
53                 $who = "\"".$in{"user_$i"}."\"";
54                 }
55         &execute_sql_logged($in{'db'}, "grant ".join(",", @what)." on ".
56                                        "$qt to $who");
57         }
58
59 &webmin_log("grant", undef, $in{'table'}, \%in);
60 &redirect("list_grants.cgi?search=".&urlize($in{'search'}));
61