Handle hostnames with upper-case letters
[webmin.git] / mysql / save_cpriv.cgi
1 #!/usr/local/bin/perl
2 # save_cpriv.cgi
3 # Save, update or delete field permissions
4
5 require './mysql-lib.pl';
6 &ReadParse();
7 $access{'perms'} || &error($text{'perms_ecannot'});
8
9 if ($in{'delete'}) {
10         # Delete some permissions
11         $access{'perms'} == 1 || &can_edit_db($in{'olddb'}) ||
12                 &error($text{'perms_edb'});
13         &execute_sql_logged($master_db,
14                 "delete from columns_priv where user = '$in{'olduser'}' ".
15                 "and host = '$in{'oldhost'}' and db = '$in{'olddb'}' ".
16                 "and table_name = '$in{'oldtable'}' ".
17                 "and column_name = '$in{'oldfield'}'");
18         }
19 else {
20         # Validate inputs
21         &error_setup($text{'cpriv_err'});
22         $in{'field'} || &error($text{'cpriv_efield'});
23         $in{'user_def'} || $in{'user'} =~ /^\S+$/ ||
24                 &error($text{'cpriv_euser'});
25         $in{'host_def'} || $in{'host'} =~ /^\S+$/ ||
26                 &error($text{'cpriv_ehost'});
27         $in{'perms'} =~ s/\0/,/g;
28
29         if ($in{'table'}) {
30                 # Create new column permissions
31                 ($d, $t) = split(/\./, $in{'table'});
32                 $access{'perms'} == 1 || &can_edit_db($d) ||
33                         &error($text{'perms_edb'});
34                 $sql = sprintf "insert into columns_priv values ('%s', '%s', ".
35                                "'%s', '%s', '%s', NULL, '%s')",
36                                 $in{'host_def'} ? '%' : $in{'host'}, $d,
37                                 $in{'user_def'} ? '' : $in{'user'},
38                                 $t, $in{'field'}, $in{'perms'};
39                 }
40         else {
41                 # Update existing column permissions
42                 $access{'perms'} == 1 || &can_edit_db($in{'olddb'}) ||
43                         &error($text{'perms_edb'});
44                 $sql = sprintf "update columns_priv set host = '%s', ".
45                                "user = '%s', column_name = '%s', ".
46                                "column_priv = '%s' where host = '%s' ".
47                                "and db = '%s' and user = '%s' ".
48                                "and table_name = '%s' and column_name = '%s'",
49                                 $in{'host_def'} ? '%' : $in{'host'},
50                                 $in{'user_def'} ? '' : $in{'user'},
51                                 $in{'field'}, $in{'perms'},
52                                 $in{'oldhost'}, $in{'olddb'},
53                                 $in{'olduser'}, $in{'oldtable'},
54                                 $in{'oldfield'};
55                 }
56         &execute_sql_logged($master_db, $sql);
57         }
58 &execute_sql_logged($master_db, 'flush privileges');
59 if ($in{'delete'}) {
60         &webmin_log("delete", "cpriv", $in{'oldtable'},
61                     { 'user' => $in{'olduser'}, 'host' => $in{'oldhost'},
62                       'db' => $in{'olddb'}, 'table' => $in{'oldtable'},
63                       'field' => $in{'oldfield'} } );
64         }
65 elsif ($in{'table'}) {
66         &webmin_log("create", "cpriv", $in{'table'},
67                     { 'user' => $in{'user_def'} ? '' : $in{'user'},
68                       'host' => $in{'host_def'} ? '%' : $in{'host'},
69                       'db' => $d, 'table' => $t, 'field' => $in{'field'} } );
70         }
71 else {
72         &webmin_log("modify", "cpriv", $in{'table'},
73                     { 'user' => $in{'user_def'} ? '' : $in{'user'},
74                       'host' => $in{'host_def'} ? '%' : $in{'host'},
75                       'db' => $in{'olddb'}, 'table' => $in{'oldtable'},
76                       'field' => $in{'field'} } );
77         }
78 &redirect("list_cprivs.cgi");
79