Advertisement
Guest User

Untitled

a guest
Mar 29th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.35 KB | None | 0 0
  1. sub askSqlRootUser
  2. {
  3.     my ($dialog) = @_;
  4.  
  5.     my $host = $main::imscpConfig{'SQL_SERVER'} eq 'remote_server' ? '' : 'localhost';
  6.     my $port = 3306;
  7.     my $user = 'root';
  8.     my $pwd = '';
  9.     my ($rs, $msg) = (0, '');
  10.  
  11.     if($host eq 'localhost') {
  12.         # If authentication is made through unix socket, password is normally not required.
  13.         # We try a connect without password with 'root' as user and we return on success
  14.         return 0 unless tryDbConnect($host, $port, $user, $pwd);
  15.     }
  16.  
  17.     do {
  18.         ($rs, $host) = $dialog->inputbox(<<"EOF", $host, 'utf-8') unless $host eq 'localhost';
  19.  
  20. Please enter your SQL server hostname or IP address:$msg
  21. EOF
  22.         $msg = $host eq '' ? "\n\n\\Z1SQL server hostname or IP address is required.\\Zn\n\nPlease try again:" : '';
  23.     } while($rs < 30 && $msg ne '');
  24.  
  25.     return $rs if $rs >= 30;
  26.  
  27.     do {
  28.         ($rs, $port) = $dialog->inputbox(<<"EOF", $port) unless $host eq 'localhost';
  29.  
  30. Please enter your SQL server port:$msg
  31. EOF
  32.         $msg = $port eq '' ? "\n\n\\Z1A SQL server port is required.\\Zn\n\nPlease try again:" : '';
  33.     } while($rs < 30 && $msg ne '');
  34.  
  35.     return $rs if $rs >= 30;
  36.  
  37.     do {
  38.         ($rs, $user) = $dialog->inputbox(<<"EOF", $user);
  39.  
  40. Please enter your SQL root username:$msg
  41.  
  42. Note that this user must have full privileges on the SQL server.
  43. i-MSCP only uses that user while installation or reconfiguration.
  44. EOF
  45.         $msg = $user eq '' ? "\n\n\\Z1A SQL root user is required.\\Zn\n\nPlease try again:" : '';
  46.     } while($rs < 30 && $msg) ne '';
  47.  
  48.     return $rs if $rs >= 30;
  49.  
  50.     do {
  51.         ($rs, $pwd) = $dialog->passwordbox(<<"EOF");
  52.  
  53. Please enter your SQL root user password:$msg
  54. EOF
  55.         $msg = $pwd eq '' ? "\n\n\\Z1SQL root user password is required.\\Zn\n\nPlease try again:" : '';
  56.     } while($rs < 30 && $msg);
  57.  
  58.     return $rs if $rs >= 30;
  59.  
  60.     if(my $connectError = tryDbConnect($host, $port, $user, $pwd)) {
  61.         $rs = $dialog->msgbox(<<"EOF");
  62.  
  63. \\Z1Connection to SQL server failed\\Zn
  64.  
  65. i-MSCP installer could not connect to SQL server using the following data:
  66.  
  67. \\Z4Host:\\Zn $host
  68. \\Z4Port:\\Zn $port
  69. \\Z4Username:\\Zn $user
  70. \\Z4Password:\\Zn $pwd
  71.  
  72. Error was: \\Z1$connectError\\Zn
  73.  
  74. Please try again.
  75. EOF
  76.         goto &{askSqlRootUser} unless $rs >= 30;
  77.     }
  78.  
  79.     if($rs < 30) {
  80.         setupSetQuestion('DATABASE_TYPE', 'mysql');
  81.         setupSetQuestion('DATABASE_HOST', $host);
  82.         setupSetQuestion('DATABASE_PORT', $port);
  83.     }
  84.  
  85.     $rs;
  86. }
  87.  
  88. sub askMasterSqlUser
  89. {
  90.     my $dialog = shift;
  91.  
  92.     my $host = setupGetQuestion('DATABASE_HOST');
  93.     my $port = setupGetQuestion('DATABASE_PORT');
  94.     my $user = setupGetQuestion('DATABASE_USER', 'imscp_user');
  95.     my $pwd = setupGetQuestion('DATABASE_PASSWORD');
  96.     my ($rs, $msg) = (0, '');
  97.  
  98.     $pwd = decryptBlowfishCBC($main::imscpDBKey, $main::imscpDBiv, $pwd) unless $pwd eq '' || iMSCP::Getopt->preseed;
  99.  
  100.     if(grep($_ eq $main::reconfigure, ( 'sql', 'servers', 'all', 'forced' ))
  101.         || $host eq '' || $port eq '' || $user eq '' || $user eq 'root' || $pwd eq ''
  102.         || tryDbConnect($host, $port, $user, $pwd)
  103.     ) {
  104.         $rs = askSqlRootUser($dialog);
  105.         return $rs if $rs >= 30;
  106.  
  107.         do {
  108.             ($rs, $user) = $dialog->inputbox( <<"EOF", $msg eq '' ? $user : '' );
  109.  
  110. Please enter a username for the master i-MSCP SQL username:$msg
  111. EOF
  112.             if ($user eq 'root') {
  113.                 $msg = "\n\n\\Z1Usage of SQL root user is disallowed. \\Zn\n\nPlease try again:";
  114.             } elsif (length $user > 16) {
  115.                 $msg = "\n\n\\Username can be up to 16 characters long.\\Zn\n\nPlease try again:";
  116.             } elsif (length $user < 6) {
  117.                 $msg = "\n\n\\Z1Username must be at least 6 characters long.\\Zn\n\nPlease try again:";
  118.             } elsif ($user !~ /^[\x21-\x5b\x5d-\x7e]+$/) {
  119.                 $msg = "\n\n\\Z1Only printable ASCII characters (excepted space and backslash) are allowed.\\Zn\n\nPlease try again:";
  120.             } else {
  121.                 $msg = '';
  122.             }
  123.         } while ($rs < 30 && $msg ne '');
  124.  
  125.         if ($rs < 30) {
  126.             $pwd = '';
  127.             do {
  128.                 ($rs, $pwd) = $dialog->passwordbox( <<"EOF", $msg eq '' ? $pwd : '');
  129. Please enter a password for the master i-MSCP SQL user (blank for autogenerate):$msg
  130. EOF
  131.                 if ($pwd ne '' && length $pwd < 6) {
  132.                     $msg = "\n\n\\Z1Password must be at least 6 characters long.\\Zn\n\nPlease try again:";
  133.                 } elsif ($pwd =~ /[^\x21-\x5b\x5d-\x7e]/) {
  134.                     $msg = "\n\n\\Z1Only printable ASCII characters (excepted space and backslash) are allowed.\\Zn\n\nPlease try again:";
  135.                 } else {
  136.                     $msg = '';
  137.                 }
  138.             } while ($rs < 30 && $msg ne '');
  139.  
  140.             if ($rs < 30) {
  141.                 unless ($pwd) {
  142.                     my @allowedChr = map { chr } (0x21 .. 0x5b, 0x5d .. 0x7e);
  143.                     $pwd = '';
  144.                     $pwd .= $allowedChr[rand @allowedChr] for 1 .. 16;
  145.                 }
  146.  
  147.                 $dialog->msgbox( <<"EOF" );
  148.  
  149. Password for master i-MSCP SQL user set to: $pwd
  150. EOF
  151.             }
  152.         }
  153.     }
  154.  
  155.     if($rs < 30) {
  156.         setupSetQuestion('DATABASE_USER', $user);
  157.         setupSetQuestion('DATABASE_PASSWORD', $pwd)
  158.     }
  159.  
  160.     $rs;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement