| 1 | use bytes::{Buf, Bytes}; |
| 2 | |
| 3 | use crate::error::Error; |
| 4 | use crate::io::ProtocolEncode; |
| 5 | use crate::io::{BufExt, ProtocolDecode}; |
| 6 | use crate::protocol::auth::AuthPlugin; |
| 7 | use crate::protocol::Capabilities; |
| 8 | |
| 9 | // https://dev.mysql.com/doc/dev/mysql-server/8.0.12/page_protocol_connection_phase_packets_protocol_auth_switch_request.html |
| 10 | |
| 11 | #[derive(Debug)] |
| 12 | pub struct AuthSwitchRequest { |
| 13 | pub plugin: AuthPlugin, |
| 14 | pub data: Bytes, |
| 15 | } |
| 16 | |
| 17 | impl ProtocolDecode<'_, bool> for AuthSwitchRequest { |
| 18 | fn decode_with(mut buf: Bytes, enable_cleartext_plugin: bool) -> Result<Self, Error> { |
| 19 | let header = buf.get_u8(); |
| 20 | if header != 0xfe { |
| 21 | return Err(err_protocol!( |
| 22 | "expected 0xfe (AUTH_SWITCH) but found 0x{:x}", |
| 23 | header |
| 24 | )); |
| 25 | } |
| 26 | |
| 27 | let plugin = buf.get_str_nul()?.parse()?; |
| 28 | |
| 29 | if matches!(plugin, AuthPlugin::MySqlClearPassword) && !enable_cleartext_plugin { |
| 30 | return Err(err_protocol!("mysql_cleartext_plugin disabled")); |
| 31 | } |
| 32 | |
| 33 | if matches!(plugin, AuthPlugin::MySqlClearPassword) && buf.is_empty() { |
| 34 | // Contrary to the MySQL protocol, AWS Aurora with IAM sends |
| 35 | // no data. That is fine because the mysql_clear_password says to |
| 36 | // ignore any data sent. |
| 37 | // See: https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_connection_phase_authentication_methods_clear_text_password.html |
| 38 | return Ok(Self { |
| 39 | plugin, |
| 40 | data: Bytes::new(), |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | // See: https://github.com/mysql/mysql-server/blob/ea7d2e2d16ac03afdd9cb72a972a95981107bf51/sql/auth/sha2_password.cc#L942 |
| 45 | if buf.len() != 21 { |
| 46 | return Err(err_protocol!( |
| 47 | "expected 21 bytes but found {} bytes", |
| 48 | buf.len() |
| 49 | )); |
| 50 | } |
| 51 | let data = buf.get_bytes(20); |
| 52 | buf.advance(1); // NUL-terminator |
| 53 | |
| 54 | Ok(Self { plugin, data }) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | #[derive(Debug)] |
| 59 | pub struct AuthSwitchResponse(pub Vec<u8>); |
| 60 | |
| 61 | impl ProtocolEncode<'_, Capabilities> for AuthSwitchResponse { |
| 62 | fn encode_with(&self, buf: &mut Vec<u8>, _: Capabilities) -> Result<(), Error> { |
| 63 | buf.extend_from_slice(&self.0); |
| 64 | Ok(()) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | #[test] |
| 69 | fn test_decode_auth_switch_packet_data() { |
| 70 | const AUTH_SWITCH_NO_DATA: &[u8] = b"\xfecaching_sha2_password\x00abcdefghijabcdefghij\x00"; |
| 71 | |
| 72 | let p = AuthSwitchRequest::decode_with(AUTH_SWITCH_NO_DATA.into(), true).unwrap(); |
| 73 | |
| 74 | assert!(matches!(p.plugin, AuthPlugin::CachingSha2Password)); |
| 75 | assert_eq!(p.data, &b"abcdefghijabcdefghij"[..]); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn test_decode_auth_switch_cleartext_disabled() { |
| 80 | const AUTH_SWITCH_CLEARTEXT: &[u8] = b"\xfemysql_clear_password\x00abcdefghijabcdefghij\x00"; |
| 81 | |
| 82 | let e = AuthSwitchRequest::decode_with(AUTH_SWITCH_CLEARTEXT.into(), false).unwrap_err(); |
| 83 | |
| 84 | let e_str = e.to_string(); |
| 85 | |
| 86 | let expected = "encountered unexpected or invalid data: mysql_cleartext_plugin disabled"; |
| 87 | |
| 88 | assert!( |
| 89 | // Don't want to assert the full string since it contains the module path now. |
| 90 | e_str.starts_with(expected), |
| 91 | "expected error string to start with {expected:?}, got {e_str:?}" |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn test_decode_auth_switch_packet_no_data() { |
| 97 | const AUTH_SWITCH_NO_DATA: &[u8] = b"\xfemysql_clear_password\x00"; |
| 98 | |
| 99 | let p = AuthSwitchRequest::decode_with(AUTH_SWITCH_NO_DATA.into(), true).unwrap(); |
| 100 | |
| 101 | assert!(matches!(p.plugin, AuthPlugin::MySqlClearPassword)); |
| 102 | assert_eq!(p.data, Bytes::new()); |
| 103 | } |