Debian Codegraph @

src/connection/establish.rs

raw symbols SCIP
1 use bytes::buf::Buf;
2 use bytes::Bytes;
3
4 use crate::collation::{CharSet, Collation};
5 use crate::common::StatementCache;
6 use crate::connection::{tls, MySqlConnectionInner, MySqlStream, MAX_PACKET_SIZE};
7 use crate::error::Error;
8 use crate::net::{Socket, WithSocket};
9 use crate::protocol::connect::{
10 AuthSwitchRequest, AuthSwitchResponse, Handshake, HandshakeResponse,
11 };
12 use crate::protocol::Capabilities;
13 use crate::{MySqlConnectOptions, MySqlConnection, MySqlSslMode};
14
15 impl MySqlConnection {
16 pub(crate) async fn establish(options: &MySqlConnectOptions) -> Result<Self, Error> {
17 let do_handshake = DoHandshake::new(options)?;
18
19 let handshake = match &options.socket {
20 Some(path) => crate::net::connect_uds(path, do_handshake).await?,
21 None => crate::net::connect_tcp(&options.host, options.port, do_handshake).await?,
22 };
23
24 let stream = handshake?;
25
26 Ok(Self {
27 inner: Box::new(MySqlConnectionInner {
28 stream,
29 transaction_depth: 0,
30 cache_statement: StatementCache::new(options.statement_cache_capacity),
31 log_settings: options.log_settings.clone(),
32 }),
33 })
34 }
35 }
36
37 struct DoHandshake<'a> {
38 options: &'a MySqlConnectOptions,
39 charset: CharSet,
40 collation: Collation,
41 }
42
43 impl<'a> DoHandshake<'a> {
44 fn new(options: &'a MySqlConnectOptions) -> Result<Self, Error> {
45 let charset: CharSet = options.charset.parse()?;
46 let collation: Collation = options
47 .collation
48 .as_deref()
49 .map(|collation| collation.parse())
50 .transpose()?
51 .unwrap_or_else(|| charset.default_collation());
52
53 if options.enable_cleartext_plugin
54 && matches!(
55 options.ssl_mode,
56 MySqlSslMode::Disabled | MySqlSslMode::Preferred
57 )
58 {
59 log::warn!("Security warning: sending cleartext passwords without requiring SSL");
60 }
61
62 Ok(Self {
63 options,
64 charset,
65 collation,
66 })
67 }
68
69 async fn do_handshake<S: Socket>(self, socket: S) -> Result<MySqlStream, Error> {
70 let DoHandshake {
71 options,
72 charset,
73 collation,
74 } = self;
75
76 let mut stream = MySqlStream::with_socket(charset, collation, options, socket);
77
78 // https://dev.mysql.com/doc/internals/en/connection-phase.html
79 // https://mariadb.com/kb/en/connection/
80
81 let handshake: Handshake = stream.recv_packet().await?.decode()?;
82
83 let mut plugin = handshake.auth_plugin;
84 let nonce = handshake.auth_plugin_data;
85
86 // FIXME: server version parse is a bit ugly
87 // expecting MAJOR.MINOR.PATCH
88
89 let mut server_version = handshake.server_version.split('.');
90
91 let server_version_major: u16 = server_version
92 .next()
93 .unwrap_or_default()
94 .parse()
95 .unwrap_or(0);
96
97 let server_version_minor: u16 = server_version
98 .next()
99 .unwrap_or_default()
100 .parse()
101 .unwrap_or(0);
102
103 let server_version_patch: u16 = server_version
104 .next()
105 .unwrap_or_default()
106 .parse()
107 .unwrap_or(0);
108
109 stream.server_version = (
110 server_version_major,
111 server_version_minor,
112 server_version_patch,
113 );
114
115 stream.capabilities &= handshake.server_capabilities;
116 stream.capabilities |= Capabilities::PROTOCOL_41;
117
118 let mut stream = tls::maybe_upgrade(stream, self.options).await?;
119
120 let auth_response = if let (Some(plugin), Some(password)) = (plugin, &options.password) {
121 Some(plugin.scramble(&mut stream, password, &nonce).await?)
122 } else {
123 None
124 };
125
126 stream.write_packet(HandshakeResponse {
127 collation: stream.collation as u8,
128 max_packet_size: MAX_PACKET_SIZE,
129 username: &options.username,
130 database: options.database.as_deref(),
131 auth_plugin: plugin,
132 auth_response: auth_response.as_deref(),
133 })?;
134
135 stream.flush().await?;
136
137 loop {
138 let packet = stream.recv_packet().await?;
139 match packet[0] {
140 0x00 => {
141 let _ok = packet.ok()?;
142
143 break;
144 }
145
146 0xfe => {
147 let switch: AuthSwitchRequest =
148 packet.decode_with(self.options.enable_cleartext_plugin)?;
149
150 plugin = Some(switch.plugin);
151 let nonce = switch.data.chain(Bytes::new());
152
153 let response = switch
154 .plugin
155 .scramble(
156 &mut stream,
157 options.password.as_deref().unwrap_or_default(),
158 &nonce,
159 )
160 .await?;
161
162 stream.write_packet(AuthSwitchResponse(response))?;
163 stream.flush().await?;
164 }
165
166 id => {
167 if let (Some(plugin), Some(password)) = (plugin, &options.password) {
168 if plugin.handle(&mut stream, packet, password, &nonce).await? {
169 // plugin signaled authentication is ok
170 break;
171 }
172
173 // plugin signaled to continue authentication
174 } else {
175 return Err(err_protocol!(
176 "unexpected packet 0x{:02x} during authentication",
177 id
178 ));
179 }
180 }
181 }
182 }
183
184 Ok(stream)
185 }
186 }
187
188 impl<'a> WithSocket for DoHandshake<'a> {
189 type Output = Result<MySqlStream, Error>;
190
191 async fn with_socket<S: Socket>(self, socket: S) -> Self::Output {
192 self.do_handshake(socket).await
193 }
194 }