|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Bart Visscher <[email protected]> |
|
6
|
|
|
* @author Joas Schilling <[email protected]> |
|
7
|
|
|
* @author Michael Göhler <[email protected]> |
|
8
|
|
|
* @author Robin McCorkell <[email protected]> |
|
9
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
10
|
|
|
* @author Stefan Weil <[email protected]> |
|
11
|
|
|
* @author Thomas Müller <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
* @license AGPL-3.0 |
|
14
|
|
|
* |
|
15
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
16
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
17
|
|
|
* as published by the Free Software Foundation. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU Affero General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
namespace OC\Setup; |
|
29
|
|
|
|
|
30
|
|
|
use OC\DB\ConnectionFactory; |
|
31
|
|
|
use OCP\IDBConnection; |
|
32
|
|
|
|
|
33
|
|
|
class MySQL extends AbstractDatabase { |
|
34
|
|
|
public $dbprettyname = 'MySQL/MariaDB'; |
|
35
|
|
|
|
|
36
|
|
|
public function setupDatabase($username) { |
|
37
|
|
|
//check if the database user has admin right |
|
38
|
|
|
$connection = $this->connect(); |
|
39
|
|
|
|
|
40
|
|
|
$this->createSpecificUser($username, $connection); |
|
41
|
|
|
|
|
42
|
|
|
//create the database |
|
43
|
|
|
$this->createDatabase($connection); |
|
44
|
|
|
|
|
45
|
|
|
//fill the database if needed |
|
46
|
|
|
$query='select count(*) from information_schema.tables where table_schema=? AND table_name = ?'; |
|
47
|
|
|
$result = $connection->executeQuery($query, [$this->dbName, $this->tablePrefix.'users']); |
|
48
|
|
|
$row = $result->fetch(); |
|
49
|
|
|
if (!$row or $row['count(*)'] === '0') { |
|
50
|
|
|
\OC_DB::createDbFromStructure($this->dbDefinitionFile); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param \OC\DB\Connection $connection |
|
56
|
|
|
*/ |
|
57
|
|
|
private function createDatabase($connection) { |
|
58
|
|
|
try{ |
|
59
|
|
|
$name = $this->dbName; |
|
60
|
|
|
$user = $this->dbUser; |
|
61
|
|
|
//we can't use OC_BD functions here because we need to connect as the administrative user. |
|
62
|
|
|
$query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET utf8 COLLATE utf8_bin;"; |
|
63
|
|
|
$connection->executeUpdate($query); |
|
64
|
|
|
} catch (\Exception $ex) { |
|
65
|
|
|
$this->logger->error('Database creation failed: {error}', [ |
|
66
|
|
|
'app' => 'mysql.setup', |
|
67
|
|
|
'error' => $ex->getMessage() |
|
68
|
|
|
]); |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
|
|
//this query will fail if there aren't the right permissions, ignore the error |
|
74
|
|
|
$query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'"; |
|
75
|
|
|
$connection->executeUpdate($query); |
|
76
|
|
|
} catch (\Exception $ex) { |
|
77
|
|
|
$this->logger->debug('Could not automatically grant privileges, this can be ignored if database user already had privileges: {error}', [ |
|
78
|
|
|
'app' => 'mysql.setup', |
|
79
|
|
|
'error' => $ex->getMessage() |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param IDBConnection $connection |
|
86
|
|
|
* @throws \OC\DatabaseSetupException |
|
87
|
|
|
*/ |
|
88
|
|
|
private function createDBUser($connection) { |
|
89
|
|
|
$name = $this->dbUser; |
|
90
|
|
|
$password = $this->dbPassword; |
|
91
|
|
|
// we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one, |
|
92
|
|
|
// the anonymous user would take precedence when there is one. |
|
93
|
|
|
$query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'"; |
|
94
|
|
|
$connection->executeUpdate($query); |
|
95
|
|
|
$query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'"; |
|
96
|
|
|
$connection->executeUpdate($query); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param $username |
|
101
|
|
|
* @param IDBConnection $connection |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
private function createSpecificUser($username, $connection) { |
|
105
|
|
|
try { |
|
106
|
|
|
//user already specified in config |
|
107
|
|
|
$oldUser = $this->config->getSystemValue('dbuser', false); |
|
108
|
|
|
|
|
109
|
|
|
//we don't have a dbuser specified in config |
|
110
|
|
|
if ($this->dbUser !== $oldUser) { |
|
111
|
|
|
//add prefix to the admin username to prevent collisions |
|
112
|
|
|
$adminUser = substr('oc_' . $username, 0, 16); |
|
113
|
|
|
|
|
114
|
|
|
$i = 1; |
|
115
|
|
|
while (true) { |
|
116
|
|
|
//this should be enough to check for admin rights in mysql |
|
117
|
|
|
$query = 'SELECT user FROM mysql.user WHERE user=?'; |
|
118
|
|
|
$result = $connection->executeQuery($query, [$adminUser]); |
|
119
|
|
|
|
|
120
|
|
|
//current dbuser has admin rights |
|
121
|
|
|
if ($result) { |
|
122
|
|
|
$data = $result->fetchAll(); |
|
123
|
|
|
//new dbuser does not exist |
|
124
|
|
|
if (count($data) === 0) { |
|
125
|
|
|
//use the admin login data for the new database user |
|
126
|
|
|
$this->dbUser = $adminUser; |
|
127
|
|
|
|
|
128
|
|
|
//create a random password so we don't need to store the admin password in the config file |
|
129
|
|
|
$this->dbPassword = $this->random->generate(30); |
|
130
|
|
|
|
|
131
|
|
|
$this->createDBUser($connection); |
|
132
|
|
|
|
|
133
|
|
|
break; |
|
134
|
|
|
} else { |
|
135
|
|
|
//repeat with different username |
|
136
|
|
|
$length = strlen((string)$i); |
|
137
|
|
|
$adminUser = substr('oc_' . $username, 0, 16 - $length) . $i; |
|
138
|
|
|
$i++; |
|
139
|
|
|
} |
|
140
|
|
|
} else { |
|
141
|
|
|
break; |
|
142
|
|
|
} |
|
143
|
|
|
}; |
|
144
|
|
|
} |
|
145
|
|
|
} catch (\Exception $ex) { |
|
146
|
|
|
$this->logger->error('Specific user creation failed: {error}', [ |
|
147
|
|
|
'app' => 'mysql.setup', |
|
148
|
|
|
'error' => $ex->getMessage() |
|
149
|
|
|
]); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$this->config->setSystemValues([ |
|
153
|
|
|
'dbuser' => $this->dbUser, |
|
154
|
|
|
'dbpassword' => $this->dbPassword, |
|
155
|
|
|
]); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|