| 1247 | |
| 1248 | # 设置ROOT密码 |
| 1249 | def SetupPassword(self, get): |
| 1250 | password = get['password'].strip() |
| 1251 | |
| 1252 | if not password: return public.returnMsg(False, 'root密码不能为空') |
| 1253 | re_list = re.findall("[,。?!;:“”‘’()【】《》¥&\u4e00-\u9fa5]+", password) |
| 1254 | if re_list: return public.returnMsg(False, f'数据库密码不能包含中文和特殊字符 {" ".join(re_list)}') |
| 1255 | |
| 1256 | try: |
| 1257 | if not password: return public.returnMsg(False, 'root密码不能为空') |
| 1258 | if password.find("'") != -1 or password.find('"') != -1: return public.returnMsg(False, "数据库密码不能包含引号") |
| 1259 | if re.search(r"[\u4e00-\u9fa5]+", password): return public.returnMsg(False, '数据库密码不能包含中文') |
| 1260 | sid = get.get('sid/d', 0) |
| 1261 | # 修改MYSQL |
| 1262 | mysql_obj = public.get_mysql_obj_by_sid(sid) |
| 1263 | if not mysql_obj: return public.returnMsg(False, '连接指定数据库失败') |
| 1264 | result = mysql_obj.query("show databases") |
| 1265 | isError = self.IsSqlError(result) |
| 1266 | is_modify = True |
| 1267 | if isError != None and not sid: |
| 1268 | # 尝试使用新密码 |
| 1269 | public.M('config').where("id=?", (1,)).setField('mysql_root', password) |
| 1270 | result = mysql_obj.query("show databases") |
| 1271 | isError = self.IsSqlError(result) |
| 1272 | if isError != None: |
| 1273 | public.ExecShell("cd /www/server/panel && " + public.get_python_bin() + " tools.py root \"" + password + "\"") |
| 1274 | is_modify = False |
| 1275 | |
| 1276 | if is_modify: |
| 1277 | admin_user = 'root' |
| 1278 | m_version = public.readFile(public.GetConfigValue('setup_path') + '/mysql/version.pl') |
| 1279 | if sid: |
| 1280 | admin_user = mysql_obj._USER |
| 1281 | m_version = mysql_obj.query('select version();')[0][0] |
| 1282 | |
| 1283 | if any(mysql_version in m_version for mysql_version in ['5.7', '8.0', '8.4', '9.0', '9.7']): |
| 1284 | accept = self.map_to_list(mysql_obj.query("select Host from mysql.user where User='{}'".format(admin_user))) |
| 1285 | for my_host in accept: |
| 1286 | mysql_obj.execute("UPDATE mysql.user SET authentication_string='' WHERE User='{}' and Host='{}'".format(admin_user, my_host[0])) |
| 1287 | mysql_obj.execute("ALTER USER `%s`@`%s` IDENTIFIED BY '%s'" % (admin_user, my_host[0], password)) |
| 1288 | elif any(mariadb_ver in m_version for mariadb_ver in ['10.5.', '10.4.', '10.6.', '10.7.', '10.11.', '11.3.']): |
| 1289 | accept = self.map_to_list(mysql_obj.query("select Host from mysql.user where User='{}'".format(admin_user))) |
| 1290 | for my_host in accept: |
| 1291 | mysql_obj.execute("ALTER USER `%s`@`%s` IDENTIFIED BY '%s'" % (admin_user, my_host[0], password)) |
| 1292 | else: |
| 1293 | result = mysql_obj.execute("update mysql.user set Password=password('" + password + "') where User='{}'".format(admin_user)) |
| 1294 | mysql_obj.execute("flush privileges") |
| 1295 | |
| 1296 | msg = public.getMsg('DATABASE_ROOT_SUCCESS') |
| 1297 | # 修改SQLITE |
| 1298 | if sid: |
| 1299 | public.M('database_servers').where("id=? AND LOWER(db_type)=LOWER('mysql')", sid).setField('db_password', password) |
| 1300 | public.WriteLog("TYPE_DATABASE", "修改远程MySQL服务器管理员密码") |
| 1301 | else: |
| 1302 | public.M('config').where("id=?", (1,)).setField('mysql_root', password) |
| 1303 | public.WriteLog("TYPE_DATABASE", "DATABASE_ROOT_SUCCESS") |
| 1304 | session['config']['mysql_root'] = password |
| 1305 | return public.returnMsg(True, msg) |
| 1306 | except Exception as ex: |