admin scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.0 KiB

  1. #!/usr/bin/perl -w
  2. #
  3. # mysql_add
  4. #
  5. # Adds a new mysql user and database, generates random password and
  6. # emails it to user. If database/user already exists, will reset
  7. # password instead.
  8. #
  9. # $Id: mysql_add,v 1.3 2003/06/11 10:43:59 dizer Exp dizer $
  10. #
  11. use DBI;
  12. use strict;
  13. # Ask a question. Arguments: (default value, "question").
  14. # Will not accept null (empty) answers when no default is provided.
  15. # If default set to "OPT", null answers will be accepted.
  16. #
  17. sub ask($$)
  18. {
  19. my ($def, $msg) = (shift, shift);
  20. my $ans;
  21. while (!$ans) {
  22. print "$msg\n[" . (defined($def) ? ($def eq 'OPT' ? "OPTIONAL" : $def) : "no default") . "] >> ";
  23. $ans = <STDIN>;
  24. chomp($ans);
  25. if ($def and !$ans) {
  26. $ans = $def;
  27. }
  28. print "\n";
  29. }
  30. return $ans eq 'OPT' ? undef : $ans;
  31. }
  32. # Ask a yes/no question. Arguments: (default value, "question").
  33. # Will not accept null (empty) answers when no default is provided.
  34. # Returns boolean value.
  35. #
  36. sub yesno($$)
  37. {
  38. my ($def, $msg) = (shift, shift);
  39. my $ans;
  40. my $val = -1;
  41. if (defined($def)) {
  42. $def = $def ? 'yes' : 'no';
  43. }
  44. while ($val < 0) {
  45. print "$msg\n[" . ($def ? "$def" : "no default") . "] (Y/N) >> ";
  46. $ans = <STDIN>;
  47. chomp($ans);
  48. if ($def and !$ans) {
  49. $ans = $def;
  50. }
  51. if ($ans =~ /^[yY].*$/) {
  52. $val = 1;
  53. } elsif ($ans =~ /^[nN].*$/) {
  54. $val = 0;
  55. }
  56. print "\n";
  57. }
  58. return $val;
  59. }
  60. sub mkpasswd()
  61. {
  62. my @passchars = qw ( a b c d e f g h i j k m n p q r s t u v w x y z A B C D E F G H J K L M N P Q R S T U V W X Y Z 2 3 4 5 6 7 8 9 );
  63. my ($c, $password) = (0, "");
  64. for ($c = 0; $c < 8; $c++) {
  65. my $rnd = int rand ($#passchars + 1);
  66. $password .= $passchars[$rnd];
  67. }
  68. return $password;
  69. }
  70. my ($admpassword, $dbh, $dbname, $username, $passwd, $email, $sql, $dbexists, $userexists, $dbuserexists, @results);
  71. system("stty -echo");
  72. print "Password for user root to connect to MySQL: ";
  73. $admpassword = <STDIN>;
  74. chomp($admpassword);
  75. system("stty echo");
  76. print "\n";
  77. $dbh = DBI->connect("DBI:mysql:mysql:host=mysql.internal", "root", $admpassword, {PrintError => 0}) or die("Can't make a connection to the mysql server.\n The error: $DBI::errstr");
  78. if ($ARGV[0]) {
  79. $dbname = $username = $ARGV[0];
  80. $email = "$username\@redbrick.dcu.ie";
  81. } else {
  82. $dbname = ask(undef, "Database name");
  83. $username = ask($dbname, "Username");
  84. $email = ask("$username\@redbrick.dcu.ie", "Email password to");
  85. }
  86. $passwd = mkpasswd();
  87. $sql = $dbh->prepare("show databases") or die($dbh->errstr);
  88. $sql->execute() or die($dbh->errstr);
  89. while (@results = $sql->fetchrow_array()) {
  90. if ($results[0] eq $dbname) {
  91. print "Database already exists, skipping database creation.\n";
  92. $dbexists = 1;
  93. last;
  94. }
  95. }
  96. $sql->finish();
  97. if (! $dbexists) {
  98. $dbh->do("create database $dbname") or die($dbh->errstr);
  99. }
  100. $sql = $dbh->prepare("select * from user where host = ? and user = ?") or die($dbh->errstr);
  101. #$sql = $dbh->prepare("show grants for ?@?");
  102. $sql->execute('%.internal',$username) or die($dbh->errstr);
  103. $sql->finish();
  104. if ($sql->rows() == 1) {
  105. $userexists = 1;
  106. }
  107. if (! $userexists) {
  108. $sql = $dbh->prepare('GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, EXECUTE, CREATE ROUTINE, ALTER ROUTINE ON `' . $dbname . '`.* TO \'' . $username . '\'@\'%.internal\' IDENTIFIED BY ? WITH GRANT OPTION');
  109. $sql->execute($passwd);
  110. $sql->finish();
  111. # if ($sql->rows() != 1) {
  112. # die("failed to add host/user/password combination");
  113. # }
  114. } else {
  115. if (yesno(undef, "host/user/password combination already exists, reset password")) {
  116. $sql = $dbh->prepare("update user set password = password(?) where user = ? and host = ?") or die($dbh->errstr);
  117. $sql->execute($passwd, $username, '%.internal') or die($dbh->errstr);
  118. $sql->finish();
  119. if ($sql->rows() != 1) {
  120. die("failed to update host/user/password combination, possible reason:" . $dbh->errstr);
  121. }
  122. }
  123. }
  124. #$sql = $dbh->prepare("select * from db where host = ? and db = ? and user = ?") or die($dbh->errstr);
  125. #$sql->execute('%.internal', $dbname, $username) or die($dbh->errstr);
  126. #$sql->finish();
  127. #if ($sql->rows() == 1) {
  128. # print "host/user/database combination already exists, skipping creation.\n";
  129. # $dbuserexists = 1;
  130. #}
  131. #if (! $dbuserexists) {
  132. # $sql = $dbh->prepare("insert into db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Grant_priv,References_priv,Index_priv,Alter_priv) values ('' , ?, ?, 'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y')");
  133. # $sql->execute($dbname, $username) or die($dbh->errstr);
  134. # $sql->finish();
  135. #}
  136. #if ($sql->rows() != 1) {
  137. # die("failed to add host/user/database combination");
  138. #}
  139. $dbh->do("flush privileges") or die("Can't flush privileges\n$dbh->errstr");
  140. $dbh->disconnect();
  141. print <<"EOF";
  142. hostname: mysql.internal
  143. database: $dbname
  144. username: $username
  145. password: $passwd
  146. EOF
  147. open(PIPE, "| /usr/sbin/sendmail -t");
  148. print PIPE <<"EOF";
  149. From: RedBrick Admin Team <elected-admins\@redbrick.dcu.ie>
  150. Subject: Your RedBrick MySQL Account
  151. To: $email
  152. Your MySQL account details are:
  153. hostname: mysql.internal
  154. database: $dbname
  155. username: $username
  156. password: $passwd
  157. EOF