1 /*  Copyright (c) 2015, 2021, Oracle and/or its affiliates.
2 
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License, version 2.0,
5     as published by the Free Software Foundation.
6 
7     This program is also distributed with certain software (including
8     but not limited to OpenSSL) that is licensed under separate terms,
9     as designated in a particular file or component or in included license
10     documentation.  The authors of MySQL hereby grant you an additional
11     permission to link the program and your derivative works with the
12     separately licensed software that they have included with MySQL.
13 
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License, version 2.0, for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #include "m_ctype.h"  /* my_charset_utf8_bin */
24 #include "my_dbug.h"   /* assert */
25 #include <mysqld_error.h> /* To get ER_NOT_VALID_PASSWORD */
26 #include <mysql/plugin_validate_password.h> /* validate_password plugin */
27 
28 #include "strfunc.h"
29 #include "sql_string.h"
30 #include "sql_plugin.h"
31 
32 
33 LEX_CSTRING validate_password_plugin= {
34   C_STRING_WITH_LEN("validate_password")
35 };
36 
37 /**
38   Validate the input password based on defined policies.
39 
40   @param password        password which needs to be validated against the
41                          defined policies
42   @param password_len    length of password
43 
44   @retval 0 ok
45   @retval 1 ERROR;
46 */
47 
my_validate_password_policy(const char * password,unsigned int password_len)48 int my_validate_password_policy(const char *password, unsigned int password_len)
49 {
50   plugin_ref plugin;
51   String password_str;
52 
53   if (password)
54   {
55     String tmp_str(password, password_len, &my_charset_utf8_bin);
56     password_str= tmp_str;
57   }
58   plugin= my_plugin_lock_by_name(0, validate_password_plugin,
59                                  MYSQL_VALIDATE_PASSWORD_PLUGIN);
60   if (plugin)
61   {
62     st_mysql_validate_password *password_validate=
63                       (st_mysql_validate_password *) plugin_decl(plugin)->info;
64 
65     if (!password_validate->validate_password(&password_str))
66     {
67       my_error(ER_NOT_VALID_PASSWORD, MYF(0));
68       plugin_unlock(0, plugin);
69       return (1);
70     }
71     plugin_unlock(0, plugin);
72   }
73   return (0);
74 }
75 
76 
77 /* called when new user is created or exsisting password is changed */
my_calculate_password_strength(const char * password,unsigned int password_len)78 int my_calculate_password_strength(const char *password, unsigned int password_len)
79 {
80   int res= 0;
81   assert(password != NULL);
82 
83   String password_str;
84   if (password)
85     password_str.set(password, password_len, &my_charset_utf8_bin);
86   plugin_ref plugin= my_plugin_lock_by_name(0, validate_password_plugin,
87                                             MYSQL_VALIDATE_PASSWORD_PLUGIN);
88   if (plugin)
89   {
90     st_mysql_validate_password *password_strength=
91                       (st_mysql_validate_password *) plugin_decl(plugin)->info;
92 
93     res= password_strength->get_password_strength(&password_str);
94     plugin_unlock(0, plugin);
95   }
96   return(res);
97 }
98