热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Batchmodeandexpiredpasswords_MySQL

Batchmodeandexpiredpasswords
Aseriesofrelateddiscussionstriggered by difficulty in setting passwords via scripts using the mysql command-line client when an account has anexpired passwordcaused me to look into the interaction between expired passwords and batch mode, and this blog post resulted. I hope it’s a useful explanation of the behavior and the workaround to those troubled by it, and amplifies the excellent documentation in the user manual.

The ability to flag accounts as having expired passwords first appeared in MySQL 5.6, with furtherimprovements made in MySQL 5.7. When an account is flagged with an expired password, it enters what the manual refers to as a “sandbox mode.” Connections are allowed, but operations are restricted until the SET PASSWORD statement is issued. It’s important to realize that this security feature is entirely focused on password maintenance – it isnot an appropriate mechanism for temporarily locking out users. While the sandbox mode shares certain characteristics with a locked account, in that users are unable to accomplish real work, it can be immediately bypassed by the affected user by issuing SET PASSWORD. That there are no controls to prohibit users from simply issuing SET PASSWORD = PASSWORD(‘same password’) – or even setting a blank password – is a topic for another discussion, but serves to highlight that the expired password mechanism is wholly unsuitable for general account locking.

The sandbox mode is clearly targeted at interactive users – somebody who can process the error messages related to expired passwords on an established connection and know how to resolve them:

mysql> SELECT 1; ERROR 1820 (HY000): You must SET PASSWORD before executing this statement mysql> SET PASSWORD = PASSWORD('testpwd'); Query OK, 0 rows affected (0.00 sec) mysql> SELECT 1; +---+ | 1 | +---+ | 1 | +---+1 row in set (0.00 sec) mysql>

This sandbox mode can create all sorts of problems for non-interactive clients. For example, a connection pool library may establish a connection and assume the absence of errors in creating the connection indicates a valid connection to hand out to application threads. A batch job may not have adequate error checking. Consequently, the sandbox mode only applies to clients which indicate they can deal with it. On the protocol level, this is done by setting the client capability flag, CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS. Different connectors/APIs will have different ways to set this capability flag:

  • In the C API, this is done by setting the MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS option usingmysql_options()
  • PHP, via mysqli,honors the same option
  • Using Connector/JAVA, setting thedisconnectOnExpiredPasswords propertyto false
  • Connector/ODBC has acan_handle_exp_pwd option

Most applications won’t be able to deal with expired password sandbox mode, and the default is universally to not set this flag. The resulting behavior is that the server authenticates the user, but sends an error message in response and terminates the connection. This is also the expected behavior when using older (pre-5.6.10) clients such as mysql – they are unaware of the new client capabilities flag, and do not indicate they can support password expiration sandbox mode.

The mysql client differentiates between batch and interactive mode when determining whether to set the capabilities flag. Notably for the discussion which prompted this blog post, if you use the -e option to specify a statement to execute, the client connects in batch mode (from client/mysql.cc):

case 'e':status.batch= 1;status.add_to_history= 0;

This is also set with the -B option.

When the connection is initialized, we find this code:

my_bool handle_expired= (opt_connect_expired_password || !status.batch) ?TRUE : FALSE;

The result is that trying to connect using the mysql client and -e with an account having an expired password will produce an error:

R:/ade/mysql-5.6.19-winx64>bin/mysql -uexptest -P3307 -e"SELECT 1;"ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.

This can be circumvented by explicitly indicating expired passwords can be handled, using the–connect-expired-password option:

R:/ade/mysql-5.6.19-winx64>bin/mysql -uexptest -P3307 /-e"SET PASSWORD='';" --connect-expired-password

The documentation also notes how other standard clients determine whether to set the capability flag or not:

MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDSis enabled formysqltestunconditionally, formysqlin interactive mode, and formysqladminif the first command ispassword.

If you disagree with the decision to block connections as they are established when the capability flag is not set, you can control this on the server side using the –disconnect_on_expired_password option (set it to OFF; default is ON). This causes connections to be established in sandbox mode regardless of the capabilities indicated by the client:

R:/ade/mysql-5.6.19-winx64>bin/mysql -uexptest -P3307 -e"SELECT 1;"ERROR 1820 (HY000) at line 1: You must SET PASSWORD before executing this statement

As seen in the example above, the sandbox mode still applies – so your application code will need to be prepared to deal with it.

The blog posts cited at the beginning of this post highlight where this may be most frequently observed – following an RPM installation of MySQL Server 5.6. Because RPMs limit interactivity, it’s not feasible to prompt users for a root password. Consequentially, the RPM installation assigns random passwords to the root accounts and flags them with expired passwords, prompting users to change the password on first use. This is described in themanual page on RPM installations:

As of MySQL 5.6.8, new RPM install operations (not upgrades) invokemysql_install_dbwith the--random-passwordsoption that provides for more secure MySQL installation. Invokingmysql_install_dbwith--random-passwordscauses it to assign a random password to the MySQLrootaccounts, set the“password expired”flag for those accounts, and not create anonymous-user MySQL accounts. It will be necessary after installation to start the server, connect asrootusing the password written to the$HOME/.mysql_secretfile, and assign a newrootpassword. Until this is done,rootcannot do anything else.

Users scripting RPM deployments of MySQL may want to script password updates as a post-install step. Those doing so should use mysqladmin, or may use mysql –connect-expired-password -e.

推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • 本文描述了作者第一次参加比赛的经历和感受。作者是小学六年级时参加比赛的唯一选手,感到有些紧张。在比赛期间,作者与学长学姐一起用餐,在比赛题目中遇到了一些困难,但最终成功解决。作者还尝试了一款游戏,在回程的路上感到晕车。最终,作者以110分的成绩取得了省一会的资格,并坚定了继续学习的决心。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 给定一个二叉树,要求随机选择树上的一个节点。解法:遍历树的过程中,随机选择一个节点即可。具体做法参看:从输入 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
author-avatar
樊赜思
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有