|
| 1 | +// Copyright © 2012, Oracle and/or its affiliates. All rights reserved. |
| 2 | +// |
| 3 | +// MySQL Connector/NET is licensed under the terms of the GPLv2 |
| 4 | +// <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most |
| 5 | +// MySQL Connectors. There are special exceptions to the terms and |
| 6 | +// conditions of the GPLv2 as it is applied to this software, see the |
| 7 | +// FLOSS License Exception |
| 8 | +// <http://www.mysql.com/about/legal/licensing/foss-exception.html>. |
| 9 | +// |
| 10 | +// This program is free software; you can redistribute it and/or modify |
| 11 | +// it under the terms of the GNU General Public License as published |
| 12 | +// by the Free Software Foundation; version 2 of the License. |
| 13 | +// |
| 14 | +// This program is distributed in the hope that it will be useful, but |
| 15 | +// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | +// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 | +// for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License along |
| 20 | +// with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | +// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | + |
| 23 | +using System.IO; |
| 24 | +using System; |
| 25 | +using MySql.Data.MySqlClient.Properties; |
| 26 | +using MySql.Data.Common; |
| 27 | +using System.Text; |
| 28 | +using System.Diagnostics; |
| 29 | + |
| 30 | +namespace MySql.Data.MySqlClient.Authentication |
| 31 | +{ |
| 32 | + public abstract class MySqlAuthenticationPlugin |
| 33 | + { |
| 34 | + private NativeDriver driver; |
| 35 | + protected byte[] AuthenticationData; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// This is a factory method that is used only internally. It creates an auth plugin based on the method type |
| 39 | + /// </summary> |
| 40 | + /// <param name="method"></param> |
| 41 | + /// <param name="flags"></param> |
| 42 | + /// <param name="settings"></param> |
| 43 | + /// <returns></returns> |
| 44 | + internal static MySqlAuthenticationPlugin GetPlugin(string method, NativeDriver driver, byte[] authData) |
| 45 | + { |
| 46 | + if (method == "mysql_old_password") |
| 47 | + { |
| 48 | + driver.Close(true); |
| 49 | + throw new MySqlException(Resources.OldPasswordsNotSupported); |
| 50 | + } |
| 51 | + MySqlAuthenticationPlugin plugin = AuthenticationPluginManager.GetPlugin(method); |
| 52 | + if (plugin == null) |
| 53 | + throw new MySqlException(String.Format(Resources.UnknownAuthenticationMethod, method)); |
| 54 | + |
| 55 | + plugin.driver = driver; |
| 56 | + plugin.SetAuthData(authData); |
| 57 | + return plugin; |
| 58 | + } |
| 59 | + |
| 60 | + protected MySqlConnectionStringBuilder Settings |
| 61 | + { |
| 62 | + get { return driver.Settings; } |
| 63 | + } |
| 64 | + |
| 65 | + protected Version ServerVersion |
| 66 | + { |
| 67 | + get { return new Version(driver.Version.Major, driver.Version.Minor, driver.Version.Build); } |
| 68 | + } |
| 69 | + |
| 70 | + internal ClientFlags Flags |
| 71 | + { |
| 72 | + get { return driver.Flags; } |
| 73 | + } |
| 74 | + |
| 75 | + protected Encoding Encoding |
| 76 | + { |
| 77 | + get { return driver.Encoding; } |
| 78 | + } |
| 79 | + |
| 80 | + protected virtual void SetAuthData(byte[] data) |
| 81 | + { |
| 82 | + AuthenticationData = data; |
| 83 | + } |
| 84 | + |
| 85 | + protected virtual void CheckConstraints() |
| 86 | + { |
| 87 | + } |
| 88 | + |
| 89 | + protected virtual void AuthenticationFailed(Exception ex) |
| 90 | + { |
| 91 | + string msg = String.Format(Resources.AuthenticationFailed, Settings.Server, GetUsername(), PluginName, ex.Message); |
| 92 | + throw new MySqlException(msg, ex); |
| 93 | + } |
| 94 | + |
| 95 | + protected virtual void AuthenticationSuccessful() |
| 96 | + { |
| 97 | + } |
| 98 | + |
| 99 | + protected virtual byte[] MoreData(byte[] data) |
| 100 | + { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + internal void Authenticate(bool reset) |
| 105 | + { |
| 106 | + CheckConstraints(); |
| 107 | + |
| 108 | + MySqlPacket packet = driver.Packet; |
| 109 | + |
| 110 | + // send auth response |
| 111 | + packet.WriteString(GetUsername()); |
| 112 | + |
| 113 | + // now write the password |
| 114 | + WritePassword(packet); |
| 115 | + |
| 116 | + if ((Flags & ClientFlags.CONNECT_WITH_DB) != 0 || reset) |
| 117 | + { |
| 118 | + if (!String.IsNullOrEmpty(Settings.Database)) |
| 119 | + packet.WriteString(Settings.Database); |
| 120 | + } |
| 121 | + |
| 122 | + if (reset) |
| 123 | + packet.WriteInteger(8, 2); |
| 124 | + |
| 125 | + if ((Flags & ClientFlags.PLUGIN_AUTH) != 0) |
| 126 | + packet.WriteString(PluginName); |
| 127 | + |
| 128 | + driver.SetConnectAttrs(); |
| 129 | + driver.SendPacket(packet); |
| 130 | + //read server response |
| 131 | + packet = ReadPacket(); |
| 132 | + byte[] b = packet.Buffer; |
| 133 | + if (b[0] == 0xfe) |
| 134 | + { |
| 135 | + if (packet.IsLastPacket) |
| 136 | + { |
| 137 | + driver.Close(true); |
| 138 | + throw new MySqlException( Resources.OldPasswordsNotSupported ); |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + HandleAuthChange(packet); |
| 143 | + } |
| 144 | + } |
| 145 | + driver.ReadOk(false); |
| 146 | + AuthenticationSuccessful(); |
| 147 | + } |
| 148 | + |
| 149 | + private void WritePassword(MySqlPacket packet) |
| 150 | + { |
| 151 | + bool secure = (Flags & ClientFlags.SECURE_CONNECTION) != 0; |
| 152 | + object password = GetPassword(); |
| 153 | + if (password is string) |
| 154 | + { |
| 155 | + if (secure) |
| 156 | + packet.WriteLenString((string)password); |
| 157 | + else |
| 158 | + packet.WriteString((string)password); |
| 159 | + } |
| 160 | + else if (password == null) |
| 161 | + packet.WriteByte(0); |
| 162 | + else if (password is byte[]) |
| 163 | + packet.Write(password as byte[]); |
| 164 | + else throw new MySqlException("Unexpected password format: " + password.GetType()); |
| 165 | + } |
| 166 | + |
| 167 | + private MySqlPacket ReadPacket() |
| 168 | + { |
| 169 | + try |
| 170 | + { |
| 171 | + MySqlPacket p = driver.ReadPacket(); |
| 172 | + return p; |
| 173 | + } |
| 174 | + catch (MySqlException ex) |
| 175 | + { |
| 176 | + // make sure this is an auth failed ex |
| 177 | + AuthenticationFailed(ex); |
| 178 | + return null; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private void HandleAuthChange(MySqlPacket packet) |
| 183 | + { |
| 184 | + byte b = packet.ReadByte(); |
| 185 | + Debug.Assert(b == 0xfe); |
| 186 | + |
| 187 | + string method = packet.ReadString(); |
| 188 | + byte[] authData = new byte[packet.Length - packet.Position]; |
| 189 | + Array.Copy(packet.Buffer, packet.Position, authData, 0, authData.Length); |
| 190 | + |
| 191 | + MySqlAuthenticationPlugin plugin = MySqlAuthenticationPlugin.GetPlugin(method, driver, authData); |
| 192 | + plugin.AuthenticationChange(); |
| 193 | + } |
| 194 | + |
| 195 | + private void AuthenticationChange() |
| 196 | + { |
| 197 | + MySqlPacket packet = driver.Packet; |
| 198 | + packet.Clear(); |
| 199 | + byte[] moreData = MoreData(null); |
| 200 | + while (moreData != null && moreData.Length > 0) |
| 201 | + { |
| 202 | + packet.Clear(); |
| 203 | + packet.Write(moreData); |
| 204 | + driver.SendPacket(packet); |
| 205 | + |
| 206 | + packet = ReadPacket(); |
| 207 | + byte prefixByte = packet.Buffer[0]; |
| 208 | + if (prefixByte != 1) return; |
| 209 | + |
| 210 | + // a prefix of 0x01 means need more auth data |
| 211 | + byte[] responseData = new byte[packet.Length - 1]; |
| 212 | + Array.Copy(packet.Buffer, 1, responseData, 0, responseData.Length); |
| 213 | + moreData = MoreData(responseData); |
| 214 | + } |
| 215 | + // we get here if MoreData returned null but the last packet read was a more data packet |
| 216 | + ReadPacket(); |
| 217 | + } |
| 218 | + |
| 219 | + public abstract string PluginName { get; } |
| 220 | + |
| 221 | + public virtual string GetUsername() |
| 222 | + { |
| 223 | + return Settings.UserID; |
| 224 | + } |
| 225 | + |
| 226 | + public virtual object GetPassword() |
| 227 | + { |
| 228 | + return null; |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments