Handle hostnames with upper-case letters
[webmin.git] / postgresql / delete_grants.cgi
1 #!/usr/local/bin/perl
2 # Reset privilege grants on some table/view/index
3
4 require './postgresql-lib.pl';
5 &ReadParse();
6 &error_setup($text{'dgrant_err'});
7 $access{'users'} || &error($text{'grant_ecannot'});
8 @d = split(/\0/, $in{'d'});
9 @d || &error($text{'dgrant_enone'});
10
11 foreach $d (@d) {
12         ($db, $table, $ns, $type) = split(/\//, $d, -1);
13         $ss = &supports_schemas($db);
14
15         # Get old privileges
16         if ($ss) {
17                 $s = &execute_sql($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');
18                 $ss = 1;
19                 }
20         else {
21                 $s = &execute_sql($db, 'select relname, relacl, \'public\' from pg_class where (relkind = \'r\' OR relkind = \'S\') and relname !~ \'^pg_\' order by relname');
22                 $ss = 0;
23                 }
24         foreach $g (@{$s->{'data'}}) {
25                 if ($g->[0] eq $table &&
26                     $g->[2] eq $ns) {
27                         $g->[1] =~ s/^\{//; $g->[1] =~ s/\}$//;
28                         @grant = map { /^"(.*)=(.*)"$/ || /^(.*)=(.*)$/; [ $1, $2 ] }
29                                      split(/,/, $g->[1]);
30                         }
31                 }
32
33         # Revoke them
34         $qt = $ss ? &quote_table($ns.".".$table)
35                   : &quote_table($table);
36         foreach $g (@grant) {
37                 next if (!$g->[1]);
38                 if ($g->[0] eq '') {
39                         $who = "public";
40                         }
41                 elsif ($g->[0] =~ /group\s+(\S+)/) {
42                         $who = "group \"$1\"";
43                         }
44                 else {
45                         $who = "\"$g->[0]\"";
46                         }
47                 &execute_sql_logged($db, "revoke all on $qt from $who");
48                 }
49         }
50
51 &webmin_log("degrant", undef, scalar(@d));
52 &redirect("list_grants.cgi?search=".&urlize($in{'search'}));
53