2
## Emacs, this is -*- perl -*- mode? :-)
4
## Permission setter for MySQL
6
## mady by Luuk de Boer (luuk@wxs.nl) 1998.
7
## it's made under GPL ...:-))
10
############################################################################
13
## 1.0 first start of the program
14
## 1.1 some changes from monty and after that
15
## initial release in mysql 3.22.10 (nov 1998)
16
## 1.2 begin screen now in a loop + quit is using 0 instead of 9
17
## after ideas of Paul DuBois.
18
## 1.2a Add Grant, References, Index and Alter privilege handling (Monty)
19
## 1.3 Applied patch provided by Martin Mokrejs <mmokrejs@natur.cuni.cz>
20
## (General code cleanup, use the GRANT statement instead of updating
21
## the privilege tables directly, added option to revoke privileges)
22
## 1.4 Remove option 6 which attempted to erroneously grant global privileges
26
# empty ... suggestions ... mail them to me ...
34
use vars qw($dbh $sth $hostname $opt_user $opt_password $opt_help $opt_host
35
$opt_socket $opt_port $host $version);
40
$dbh=$host=$opt_user= $opt_password= $opt_help= $opt_host= $opt_socket= "";
43
read_my_cnf(); # Read options from ~/.my.cnf
45
GetOptions("user=s","password=s","help","host=s","socket=s","port=i");
47
usage() if ($opt_help); # the help function
51
$sqlhost = "localhost";
58
# ask for a password if no password is set already
59
if ($opt_password eq '')
62
print "Password for user $opt_user to connect to MySQL: ";
63
$opt_password = <STDIN>;
70
# make the connection to MySQL
71
$dbh= DBI->connect("DBI:mysql:mysql:host=$sqlhost:port=$opt_port:mysql_socket=$opt_socket",$opt_user,$opt_password, {PrintError => 0}) ||
72
die("Can't make a connection to the mysql server.\n The error: $DBI::errstr");
74
# the start of the program
79
# below all subroutines of the program
83
# the beginning of the program
85
sub q1 { # first question ...
90
print "## Welcome to the permission setter $version for MySQL.\n";
91
print "## made by Luuk de Boer\n";
94
print "What would you like to do:\n";
95
print " 1. Set password for an existing user.\n";
96
print " 2. Create a database + user privilege for that database\n";
97
print " and host combination (user can only do SELECT)\n";
98
print " 3. Create/append user privilege for an existing database\n";
99
print " and host combination (user can only do SELECT)\n";
100
print " 4. Create/append broader user privileges for an existing\n";
101
print " database and host combination\n";
102
print " (user can do SELECT,INSERT,UPDATE,DELETE)\n";
103
print " 5. Create/append quite extended user privileges for an\n";
104
print " existing database and host combination (user can do\n";
105
print " SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX,\n";
106
print " LOCK TABLES,CREATE TEMPORARY TABLES)\n";
107
print " 6. Create/append full privileges for an existing database\n";
108
print " and host combination (user has FULL privilege)\n";
109
print " 7. Remove all privileges for for an existing database and\n";
110
print " host combination.\n";
111
print " (user will have all permission fields set to N)\n";
112
print " 0. exit this program\n";
113
print "\nMake your choice [1,2,3,4,5,6,7,0]: ";
117
if ($answer =~ /^[1234567]$/) {
120
} elsif ($answer =~ /^[234567]$/) {
123
print "Sorry, something went wrong. With such option number you should not get here.\n\n";
126
} elsif ($answer == 0) {
127
print "We hope we can help you next time \n\n";
130
print "Your answer was $answer\n";
131
print "and that's wrong .... Try again\n";
139
# set a password for a user
143
my ($user,$pass,$host) = "";
144
print "\n\nSetting a (new) password for a user.\n";
147
$pass = newpass($user);
148
$host = hosts($user);
152
print "That was it ... here is an overview of what you gave to me:\n";
153
print "The username : $user\n";
154
# print "The password : $pass\n";
155
print "The host : $host\n";
158
print "Are you pretty sure you would like to implement this [yes/no]: ";
163
print "Okay .. that was it then ... See ya\n\n";
168
print "Okay ... let's go then ...\n\n";
170
$user = $dbh->quote($user);
171
$host = $dbh->quote($host);
178
$pass = "PASSWORD(". $dbh->quote($pass) . ")";
180
my $sth = $dbh->prepare("update user set Password=$pass where User = $user and Host = $host") || die $dbh->errstr;
181
$sth->execute || die $dbh->errstr;
183
print "The password is set for user $user.\n\n";
188
# all things which will be added are done here
192
my ($answer,$good,$db,$user,$pass,$host,$priv);
201
$pass = newpass("$user");
206
print "That was it ... here is an overview of what you gave to me:\n";
207
print "The database name : $db\n";
208
print "The username : $user\n";
209
# print "The password : $pass\n";
210
print "The host(s) : $host\n";
213
print "Are you pretty sure you would like to implement this [yes/no]: ";
217
print "Okay .. that was it then ... See ya\n\n";
220
print "Okay ... let's go then ...\n\n";
224
# create the database
226
my $sth = $dbh->do("CREATE DATABASE $db") || $dbh->errstr;
228
print STDERR "What do you want? You wanted to create new database and add new user, right?\n";
229
die "But then specify databasename, please\n";
233
if ( ( !$todo ) or not ( $todo =~ m/^[2-7]$/ ) ) {
234
print STDERR "Sorry, select option $todo isn't known inside the program .. See ya\n";
238
my @hosts = split(/,/,$host);
240
die "username not specified: $user\n";
243
die "databasename is not specified nor *\n";
245
foreach $host (@hosts) {
246
# user privileges: SELECT
247
if (($todo == 2) || ($todo == 3)) {
248
$sth = $dbh->do("GRANT SELECT ON $db.* TO $user@\"$host\" IDENTIFIED BY \'$pass\'") || die $dbh->errstr;
249
} elsif ($todo == 4) {
250
# user privileges: SELECT,INSERT,UPDATE,DELETE
251
$sth = $dbh->do("GRANT SELECT,INSERT,UPDATE,DELETE ON $db.* TO $user@\"$host\" IDENTIFIED BY \'$pass\'") || die $dbh->errstr;
252
} elsif ($todo == 5) {
253
# user privileges: SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX,LOCK TABLES,CREATE TEMPORARY TABLES
254
$sth = $dbh->do("GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,INDEX,LOCK TABLES,CREATE TEMPORARY TABLES ON $db.* TO $user@\"$host\" IDENTIFIED BY \'$pass\'") || die $dbh->errstr;
255
} elsif ($todo == 6) {
257
$sth = $dbh->do("GRANT ALL ON $db.* TO \'$user\'\@\'$host\' IDENTIFIED BY \'$pass\'") || die $dbh->errstr;
258
} elsif ($todo == 7) {
259
# all privileges set to N
260
$sth = $dbh->do("REVOKE ALL ON *.* FROM \'$user\'\@\'$host\'") || die $dbh->errstr;
263
$dbh->do("FLUSH PRIVILEGES") || print STDERR "Can't flush privileges\n";
264
print "Everything is inserted and mysql privileges have been reloaded.\n\n";
268
# ask for a new database name
271
my ($answer,$good,$db);
272
print "\n\nWhich database would you like to add: ";
278
my $sth = $dbh->prepare("SHOW DATABASES") || die $dbh->errstr;
279
$sth->execute || die $dbh->errstr;
280
while (my @r = $sth->fetchrow_array) {
281
if ($r[0] eq $answer) {
282
print "\n\nSorry, this database name is already in use; try something else: ";
287
print "You must type something ...\nTry again: ";
290
last if ($good == 0);
293
print "The new database $db will be created\n";
301
my ($answer,$good,$db);
302
print "\n\nWhich database from existing databases would you like to select: \n";
303
print "You can choose from: \n";
304
my $sth = $dbh->prepare("show databases") || die $dbh->errstr;
305
$sth->execute || die $dbh->errstr;
306
while (my @r = $sth->fetchrow_array) {
309
print "Which database will it be (case sensitive). Type * for any: \n";
315
if ($answer eq "*") {
316
print "OK, the user entry will NOT be limited to any database";
319
my $sth = $dbh->prepare("show databases") || die $dbh->errstr;
320
$sth->execute || die $dbh->errstr;
321
while (my @r = $sth->fetchrow_array) {
322
if ($r[0] eq $answer) {
329
print "Type either database name or * meaning any databasename. That means";
330
print " any of those above but also any which will be created in future!";
331
print " This option gives a user chance to operate on databse mysql, which";
332
print " contains privilege settings. That is really risky!\n";
338
print "You must select one from the list.\nTry again: ";
342
print "The database $db will be used.\n";
347
# ask for a new username
354
print "\nWhat username is to be created: ";
365
print "You must type something ...\nTry again: ";
370
print "Username = $user\n";
375
# ask for a user which is already in the user table
381
print "\nFor which user do you want to specify a password: ";
388
my $sth = $dbh->prepare("select User from user where User = '$answer'") || die $dbh->errstr;
389
$sth->execute || die $dbh->errstr;
390
my @r = $sth->fetchrow_array;
397
print "Sorry, user $answer isn't known in the user table.\nTry again: ";
403
print "You must type something ...\nTry again: ";
408
print "Username = $user\n";
413
# ask for a new password
418
my ($pass,$answer,$good,$yes);
420
print "Would you like to set a password for $user [y/n]: ";
426
print "What password do you want to specify for $user: ";
436
print "Type the password again: ";
437
my $second = <STDIN>;
441
if ($answer ne $second)
443
print "Passwords aren't the same; we begin from scratch again.\n";
445
print "Password please: ";
455
print "You must type something ...\nTry again: ";
460
# print "The password for $user is $pass.\n";
464
print "We won't set a password so the user doesn't have to use it\n";
475
my ($host,$answer,$good);
477
print "We now need to know from what host(s) the user will connect.\n";
478
print "Keep in mind that % means 'from any host' ...\n";
479
print "The host please: ";
487
print "Would you like to add another host [yes/no]: ";
492
print "Okay, give us the host please: ";
497
print "Okay we keep it with this ...\n";
502
print "You must type something ...\nTry again: ";
508
print "The following host(s) will be used: $host.\n";
513
# ask for a host which is already in the user table
518
my ($answer,$good,$host);
520
print "We now need to know which host for $user we have to change.\n";
521
print "Choose from the following hosts: \n";
522
$user = $dbh->quote($user);
523
my $sth = $dbh->prepare("select Host,User from user where User = $user") || die $dbh->errstr;
524
$sth->execute || die $dbh->errstr;
525
while (my @r = $sth->fetchrow_array)
529
print "The host please (case sensitive): ";
536
$sth = $dbh->prepare("select Host,User from user where Host = '$answer' and User = $user") || die $dbh->errstr;
537
$sth->execute || die $dbh->errstr;
538
my @r = $sth->fetchrow_array;
546
print "You have to select a host from the list ...\nTry again: ";
552
print "You have to type something ...\nTry again: ";
557
print "The following host will be used: $host.\n";
562
# a nice quit (first disconnect and then exit
571
# Read variables password, port and socket from .my.cnf under the client
577
open(TMP,$ENV{'HOME'} . "/.my.cnf") || return 1;
580
if (/^\[(client|perl)\]/i)
582
while ((defined($_=<TMP>)) && !/^\[\w+\]/)
585
if (/^host\s*=\s*(\S+)/i)
589
elsif (/^user\s*=\s*(\S+)/i)
593
elsif (/^password\s*=\s*(\S+)/i)
597
elsif (/^port\s*=\s*(\S+)/i)
601
elsif (/^socket\s*=\s*(\S+)/i)
617
----------------------------------------------------------------------
618
The permission setter for MySQL.
621
made by: Luuk de Boer <luuk\@wxs.nl>
622
----------------------------------------------------------------------
624
The permission setter is a little program which can help you add users
625
or databases or change passwords in MySQL. Keep in mind that we don't
626
check permissions which already been set in MySQL. So if you can't
627
connect to MySQL using the permission you just added, take a look at
628
the permissions which have already been set in MySQL.
630
The permission setter first reads your .my.cnf file in your Home
631
directory if it exists.
633
Options for the permission setter:
635
--help : print this help message and exit.
637
The options shown below are used for making the connection to the MySQL
638
server. Keep in mind that the permissions for the user specified via
639
these options must be sufficient to add users / create databases / set
642
--user : is the username to connect with.
643
--password : the password of the username.
644
--host : the host to connect to.
645
--socket : the socket to connect to.
646
--port : the port number of the host to connect to.
648
If you don't give a password and no password is set in your .my.cnf
649
file, then the permission setter will ask for a password.