deltachat/
transport.rs

1//! # Message transport.
2//!
3//! A transport represents a single IMAP+SMTP configuration
4//! that is known to work at least once in the past.
5//!
6//! Transports are stored in the `transports` SQL table.
7//! Each transport is uniquely identified by its email address.
8//! The table stores both the login parameters entered by the user
9//! and configured list of connection candidates.
10
11use std::fmt;
12
13use anyhow::{Context as _, Result, bail, format_err};
14use deltachat_contact_tools::{EmailAddress, addr_normalize};
15use serde::{Deserialize, Serialize};
16
17use crate::config::Config;
18use crate::configure::server_params::{ServerParams, expand_param_vector};
19use crate::constants::{DC_LP_AUTH_FLAGS, DC_LP_AUTH_OAUTH2};
20use crate::context::Context;
21use crate::events::EventType;
22use crate::login_param::EnteredLoginParam;
23use crate::net::load_connection_timestamp;
24use crate::provider::{Protocol, Provider, Socket, UsernamePattern, get_provider_by_id};
25use crate::sql::Sql;
26use crate::sync::{RemovedTransportData, SyncData, TransportData};
27
28#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
29pub(crate) enum ConnectionSecurity {
30    /// Implicit TLS.
31    Tls,
32
33    // STARTTLS.
34    Starttls,
35
36    /// Plaintext.
37    Plain,
38}
39
40impl fmt::Display for ConnectionSecurity {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        match self {
43            Self::Tls => write!(f, "tls")?,
44            Self::Starttls => write!(f, "starttls")?,
45            Self::Plain => write!(f, "plain")?,
46        }
47        Ok(())
48    }
49}
50
51impl TryFrom<Socket> for ConnectionSecurity {
52    type Error = anyhow::Error;
53
54    fn try_from(socket: Socket) -> Result<Self> {
55        match socket {
56            Socket::Automatic => Err(format_err!("Socket security is not configured")),
57            Socket::Ssl => Ok(Self::Tls),
58            Socket::Starttls => Ok(Self::Starttls),
59            Socket::Plain => Ok(Self::Plain),
60        }
61    }
62}
63
64/// Values saved into `imap_certificate_checks`.
65#[derive(
66    Copy, Clone, Debug, Display, FromPrimitive, ToPrimitive, PartialEq, Eq, Serialize, Deserialize,
67)]
68#[repr(u32)]
69#[strum(serialize_all = "snake_case")]
70pub(crate) enum ConfiguredCertificateChecks {
71    /// Use configuration from the provider database.
72    /// If there is no provider database setting for certificate checks,
73    /// accept invalid certificates.
74    ///
75    /// Must not be saved by new versions.
76    ///
77    /// Previous Delta Chat versions before core 1.133.0
78    /// stored this in `configured_imap_certificate_checks`
79    /// if Automatic configuration
80    /// was selected, configuration with strict TLS checks failed
81    /// and configuration without strict TLS checks succeeded.
82    OldAutomatic = 0,
83
84    /// Ensure that TLS certificate is valid for the server hostname.
85    Strict = 1,
86
87    /// Accept certificates that are expired, self-signed
88    /// or otherwise not valid for the server hostname.
89    AcceptInvalidCertificates = 2,
90
91    /// Accept certificates that are expired, self-signed
92    /// or otherwise not valid for the server hostname.
93    ///
94    /// Alias to `AcceptInvalidCertificates` for compatibility.
95    AcceptInvalidCertificates2 = 3,
96
97    /// Use configuration from the provider database.
98    /// If there is no provider database setting for certificate checks,
99    /// apply strict checks to TLS certificates.
100    Automatic = 4,
101}
102
103#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
104pub(crate) struct ConnectionCandidate {
105    /// Server hostname or IP address.
106    pub host: String,
107
108    /// Server port.
109    pub port: u16,
110
111    /// Transport layer security.
112    pub security: ConnectionSecurity,
113}
114
115impl fmt::Display for ConnectionCandidate {
116    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117        write!(f, "{}:{}:{}", &self.host, self.port, self.security)?;
118        Ok(())
119    }
120}
121
122#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
123pub(crate) struct ConfiguredServerLoginParam {
124    pub connection: ConnectionCandidate,
125
126    /// Username.
127    pub user: String,
128}
129
130impl fmt::Display for ConfiguredServerLoginParam {
131    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132        write!(f, "{}:{}", self.connection, &self.user)?;
133        Ok(())
134    }
135}
136
137pub(crate) async fn prioritize_server_login_params(
138    sql: &Sql,
139    params: &[ConfiguredServerLoginParam],
140    alpn: &str,
141) -> Result<Vec<ConfiguredServerLoginParam>> {
142    let mut res: Vec<(Option<i64>, ConfiguredServerLoginParam)> = Vec::with_capacity(params.len());
143    for param in params {
144        let timestamp = load_connection_timestamp(
145            sql,
146            alpn,
147            &param.connection.host,
148            param.connection.port,
149            None,
150        )
151        .await?;
152        res.push((timestamp, param.clone()));
153    }
154    res.sort_by_key(|(ts, _param)| std::cmp::Reverse(*ts));
155    Ok(res.into_iter().map(|(_ts, param)| param).collect())
156}
157
158/// Login parameters saved to the database
159/// after successful configuration.
160#[derive(Debug, Clone, PartialEq, Eq)]
161pub(crate) struct ConfiguredLoginParam {
162    /// `From:` address that was used at the time of configuration.
163    pub addr: String,
164
165    pub imap: Vec<ConfiguredServerLoginParam>,
166
167    // Custom IMAP user.
168    //
169    // This overwrites autoconfig from the provider database
170    // if non-empty.
171    pub imap_user: String,
172
173    pub imap_password: String,
174
175    pub smtp: Vec<ConfiguredServerLoginParam>,
176
177    // Custom SMTP user.
178    //
179    // This overwrites autoconfig from the provider database
180    // if non-empty.
181    pub smtp_user: String,
182
183    pub smtp_password: String,
184
185    pub provider: Option<&'static Provider>,
186
187    /// TLS options: whether to allow invalid certificates and/or
188    /// invalid hostnames
189    pub certificate_checks: ConfiguredCertificateChecks,
190
191    /// If true, login via OAUTH2 (not recommended anymore)
192    pub oauth2: bool,
193}
194
195/// JSON representation of ConfiguredLoginParam
196/// for the database and sync messages.
197#[derive(Debug, Serialize, Deserialize)]
198pub(crate) struct ConfiguredLoginParamJson {
199    pub addr: String,
200    pub imap: Vec<ConfiguredServerLoginParam>,
201    pub imap_user: String,
202    pub imap_password: String,
203    pub smtp: Vec<ConfiguredServerLoginParam>,
204    pub smtp_user: String,
205    pub smtp_password: String,
206    pub provider_id: Option<String>,
207    pub certificate_checks: ConfiguredCertificateChecks,
208    pub oauth2: bool,
209}
210
211impl fmt::Display for ConfiguredLoginParam {
212    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
213        let addr = &self.addr;
214        let provider_id = match self.provider {
215            Some(provider) => provider.id,
216            None => "none",
217        };
218        let certificate_checks = self.certificate_checks;
219        write!(f, "{addr} imap:[")?;
220        let mut first = true;
221        for imap in &self.imap {
222            if !first {
223                write!(f, ", ")?;
224            }
225            write!(f, "{imap}")?;
226            first = false;
227        }
228        write!(f, "] smtp:[")?;
229        let mut first = true;
230        for smtp in &self.smtp {
231            if !first {
232                write!(f, ", ")?;
233            }
234            write!(f, "{smtp}")?;
235            first = false;
236        }
237        write!(f, "] provider:{provider_id} cert_{certificate_checks}")?;
238        Ok(())
239    }
240}
241
242impl ConfiguredLoginParam {
243    /// Load configured account settings from the database.
244    ///
245    /// Returns transport ID and configured parameters
246    /// of the current primary transport.
247    /// Returns `None` if account is not configured.
248    pub(crate) async fn load(context: &Context) -> Result<Option<(u32, Self)>> {
249        let Some(self_addr) = context.get_config(Config::ConfiguredAddr).await? else {
250            return Ok(None);
251        };
252
253        let Some((id, json)) = context
254            .sql
255            .query_row_optional(
256                "SELECT id, configured_param FROM transports WHERE addr=?",
257                (&self_addr,),
258                |row| {
259                    let id: u32 = row.get(0)?;
260                    let json: String = row.get(1)?;
261                    Ok((id, json))
262                },
263            )
264            .await?
265        else {
266            bail!("Self address {self_addr} doesn't have a corresponding transport");
267        };
268        Ok(Some((id, Self::from_json(&json)?)))
269    }
270
271    /// Loads configured login parameters for all transports.
272    ///
273    /// Returns a vector of all transport IDs
274    /// paired with the configured parameters for the transports.
275    pub(crate) async fn load_all(context: &Context) -> Result<Vec<(u32, Self)>> {
276        context
277            .sql
278            .query_map_vec("SELECT id, configured_param FROM transports", (), |row| {
279                let id: u32 = row.get(0)?;
280                let json: String = row.get(1)?;
281                let param = Self::from_json(&json)?;
282                Ok((id, param))
283            })
284            .await
285    }
286
287    /// Loads legacy configured param. Only used for tests and the migration.
288    pub(crate) async fn load_legacy(context: &Context) -> Result<Option<Self>> {
289        if !context.get_config_bool(Config::Configured).await? {
290            return Ok(None);
291        }
292
293        let addr = context
294            .get_config(Config::ConfiguredAddr)
295            .await?
296            .unwrap_or_default()
297            .trim()
298            .to_string();
299
300        let certificate_checks: ConfiguredCertificateChecks = if let Some(certificate_checks) =
301            context
302                .get_config_parsed::<i32>(Config::ConfiguredImapCertificateChecks)
303                .await?
304        {
305            num_traits::FromPrimitive::from_i32(certificate_checks)
306                .context("Invalid configured_imap_certificate_checks value")?
307        } else {
308            // This is true for old accounts configured using C core
309            // which did not check TLS certificates.
310            ConfiguredCertificateChecks::OldAutomatic
311        };
312
313        let send_pw = context
314            .get_config(Config::ConfiguredSendPw)
315            .await?
316            .context("SMTP password is not configured")?;
317        let mail_pw = context
318            .get_config(Config::ConfiguredMailPw)
319            .await?
320            .context("IMAP password is not configured")?;
321
322        let server_flags = context
323            .get_config_parsed::<i32>(Config::ConfiguredServerFlags)
324            .await?
325            .unwrap_or_default();
326        let oauth2 = matches!(server_flags & DC_LP_AUTH_FLAGS, DC_LP_AUTH_OAUTH2);
327
328        let provider = context
329            .get_config(Config::ConfiguredProvider)
330            .await?
331            .and_then(|cfg| get_provider_by_id(&cfg));
332
333        let imap;
334        let smtp;
335
336        let mail_user = context
337            .get_config(Config::ConfiguredMailUser)
338            .await?
339            .unwrap_or_default();
340        let send_user = context
341            .get_config(Config::ConfiguredSendUser)
342            .await?
343            .unwrap_or_default();
344
345        if let Some(provider) = provider {
346            let parsed_addr = EmailAddress::new(&addr).context("Bad email-address")?;
347            let addr_localpart = parsed_addr.local;
348
349            if provider.server.is_empty() {
350                let servers = vec![
351                    ServerParams {
352                        protocol: Protocol::Imap,
353                        hostname: context
354                            .get_config(Config::ConfiguredMailServer)
355                            .await?
356                            .unwrap_or_default(),
357                        port: context
358                            .get_config_parsed::<u16>(Config::ConfiguredMailPort)
359                            .await?
360                            .unwrap_or_default(),
361                        socket: context
362                            .get_config_parsed::<i32>(Config::ConfiguredMailSecurity)
363                            .await?
364                            .and_then(num_traits::FromPrimitive::from_i32)
365                            .unwrap_or_default(),
366                        username: mail_user.clone(),
367                    },
368                    ServerParams {
369                        protocol: Protocol::Smtp,
370                        hostname: context
371                            .get_config(Config::ConfiguredSendServer)
372                            .await?
373                            .unwrap_or_default(),
374                        port: context
375                            .get_config_parsed::<u16>(Config::ConfiguredSendPort)
376                            .await?
377                            .unwrap_or_default(),
378                        socket: context
379                            .get_config_parsed::<i32>(Config::ConfiguredSendSecurity)
380                            .await?
381                            .and_then(num_traits::FromPrimitive::from_i32)
382                            .unwrap_or_default(),
383                        username: send_user.clone(),
384                    },
385                ];
386                let servers = expand_param_vector(servers, &addr, &parsed_addr.domain);
387                imap = servers
388                    .iter()
389                    .filter_map(|params| {
390                        let Ok(security) = params.socket.try_into() else {
391                            return None;
392                        };
393                        if params.protocol == Protocol::Imap {
394                            Some(ConfiguredServerLoginParam {
395                                connection: ConnectionCandidate {
396                                    host: params.hostname.clone(),
397                                    port: params.port,
398                                    security,
399                                },
400                                user: params.username.clone(),
401                            })
402                        } else {
403                            None
404                        }
405                    })
406                    .collect();
407                smtp = servers
408                    .iter()
409                    .filter_map(|params| {
410                        let Ok(security) = params.socket.try_into() else {
411                            return None;
412                        };
413                        if params.protocol == Protocol::Smtp {
414                            Some(ConfiguredServerLoginParam {
415                                connection: ConnectionCandidate {
416                                    host: params.hostname.clone(),
417                                    port: params.port,
418                                    security,
419                                },
420                                user: params.username.clone(),
421                            })
422                        } else {
423                            None
424                        }
425                    })
426                    .collect();
427            } else {
428                imap = provider
429                    .server
430                    .iter()
431                    .filter_map(|server| {
432                        if server.protocol != Protocol::Imap {
433                            return None;
434                        }
435
436                        let Ok(security) = server.socket.try_into() else {
437                            return None;
438                        };
439
440                        Some(ConfiguredServerLoginParam {
441                            connection: ConnectionCandidate {
442                                host: server.hostname.to_string(),
443                                port: server.port,
444                                security,
445                            },
446                            user: if !mail_user.is_empty() {
447                                mail_user.clone()
448                            } else {
449                                match server.username_pattern {
450                                    UsernamePattern::Email => addr.to_string(),
451                                    UsernamePattern::Emaillocalpart => addr_localpart.clone(),
452                                }
453                            },
454                        })
455                    })
456                    .collect();
457                smtp = provider
458                    .server
459                    .iter()
460                    .filter_map(|server| {
461                        if server.protocol != Protocol::Smtp {
462                            return None;
463                        }
464
465                        let Ok(security) = server.socket.try_into() else {
466                            return None;
467                        };
468
469                        Some(ConfiguredServerLoginParam {
470                            connection: ConnectionCandidate {
471                                host: server.hostname.to_string(),
472                                port: server.port,
473                                security,
474                            },
475                            user: if !send_user.is_empty() {
476                                send_user.clone()
477                            } else {
478                                match server.username_pattern {
479                                    UsernamePattern::Email => addr.to_string(),
480                                    UsernamePattern::Emaillocalpart => addr_localpart.clone(),
481                                }
482                            },
483                        })
484                    })
485                    .collect();
486            }
487        } else if let (Some(configured_mail_servers), Some(configured_send_servers)) = (
488            context.get_config(Config::ConfiguredImapServers).await?,
489            context.get_config(Config::ConfiguredSmtpServers).await?,
490        ) {
491            imap = serde_json::from_str(&configured_mail_servers)
492                .context("Failed to parse configured IMAP servers")?;
493            smtp = serde_json::from_str(&configured_send_servers)
494                .context("Failed to parse configured SMTP servers")?;
495        } else {
496            // Load legacy settings storing a single IMAP and single SMTP server.
497            let mail_server = context
498                .get_config(Config::ConfiguredMailServer)
499                .await?
500                .unwrap_or_default();
501            let mail_port = context
502                .get_config_parsed::<u16>(Config::ConfiguredMailPort)
503                .await?
504                .unwrap_or_default();
505
506            let mail_security: Socket = context
507                .get_config_parsed::<i32>(Config::ConfiguredMailSecurity)
508                .await?
509                .and_then(num_traits::FromPrimitive::from_i32)
510                .unwrap_or_default();
511
512            let send_server = context
513                .get_config(Config::ConfiguredSendServer)
514                .await?
515                .context("SMTP server is not configured")?;
516            let send_port = context
517                .get_config_parsed::<u16>(Config::ConfiguredSendPort)
518                .await?
519                .unwrap_or_default();
520            let send_security: Socket = context
521                .get_config_parsed::<i32>(Config::ConfiguredSendSecurity)
522                .await?
523                .and_then(num_traits::FromPrimitive::from_i32)
524                .unwrap_or_default();
525
526            imap = vec![ConfiguredServerLoginParam {
527                connection: ConnectionCandidate {
528                    host: mail_server,
529                    port: mail_port,
530                    security: mail_security.try_into()?,
531                },
532                user: mail_user.clone(),
533            }];
534            smtp = vec![ConfiguredServerLoginParam {
535                connection: ConnectionCandidate {
536                    host: send_server,
537                    port: send_port,
538                    security: send_security.try_into()?,
539                },
540                user: send_user.clone(),
541            }];
542        }
543
544        Ok(Some(ConfiguredLoginParam {
545            addr,
546            imap,
547            imap_user: mail_user,
548            imap_password: mail_pw,
549            smtp,
550            smtp_user: send_user,
551            smtp_password: send_pw,
552            certificate_checks,
553            provider,
554            oauth2,
555        }))
556    }
557
558    pub(crate) async fn save_to_transports_table(
559        self,
560        context: &Context,
561        entered_param: &EnteredLoginParam,
562        timestamp: i64,
563    ) -> Result<()> {
564        save_transport(context, entered_param, &self.into(), timestamp).await?;
565        Ok(())
566    }
567
568    pub(crate) fn from_json(json: &str) -> Result<Self> {
569        let json: ConfiguredLoginParamJson = serde_json::from_str(json)?;
570
571        let provider = json.provider_id.and_then(|id| get_provider_by_id(&id));
572
573        Ok(ConfiguredLoginParam {
574            addr: json.addr,
575            imap: json.imap,
576            imap_user: json.imap_user,
577            imap_password: json.imap_password,
578            smtp: json.smtp,
579            smtp_user: json.smtp_user,
580            smtp_password: json.smtp_password,
581            provider,
582            certificate_checks: json.certificate_checks,
583            oauth2: json.oauth2,
584        })
585    }
586
587    pub(crate) fn into_json(self) -> Result<String> {
588        let json: ConfiguredLoginParamJson = self.into();
589        Ok(serde_json::to_string(&json)?)
590    }
591
592    pub(crate) fn strict_tls(&self, connected_through_proxy: bool) -> bool {
593        let provider_strict_tls = self.provider.map(|provider| provider.opt.strict_tls);
594        match self.certificate_checks {
595            ConfiguredCertificateChecks::OldAutomatic => {
596                provider_strict_tls.unwrap_or(connected_through_proxy)
597            }
598            ConfiguredCertificateChecks::Automatic => provider_strict_tls.unwrap_or(true),
599            ConfiguredCertificateChecks::Strict => true,
600            ConfiguredCertificateChecks::AcceptInvalidCertificates
601            | ConfiguredCertificateChecks::AcceptInvalidCertificates2 => false,
602        }
603    }
604}
605
606impl From<ConfiguredLoginParam> for ConfiguredLoginParamJson {
607    fn from(configured_login_param: ConfiguredLoginParam) -> Self {
608        Self {
609            addr: configured_login_param.addr,
610            imap: configured_login_param.imap,
611            imap_user: configured_login_param.imap_user,
612            imap_password: configured_login_param.imap_password,
613            smtp: configured_login_param.smtp,
614            smtp_user: configured_login_param.smtp_user,
615            smtp_password: configured_login_param.smtp_password,
616            provider_id: configured_login_param.provider.map(|p| p.id.to_string()),
617            certificate_checks: configured_login_param.certificate_checks,
618            oauth2: configured_login_param.oauth2,
619        }
620    }
621}
622
623/// Saves transport to the database.
624pub(crate) async fn save_transport(
625    context: &Context,
626    entered_param: &EnteredLoginParam,
627    configured: &ConfiguredLoginParamJson,
628    add_timestamp: i64,
629) -> Result<()> {
630    let addr = addr_normalize(&configured.addr);
631    let configured_addr = context.get_config(Config::ConfiguredAddr).await?;
632
633    context
634        .sql
635        .execute(
636            "INSERT INTO transports (addr, entered_param, configured_param, add_timestamp)
637             VALUES (?, ?, ?, ?)
638             ON CONFLICT (addr)
639             DO UPDATE SET entered_param=excluded.entered_param,
640                           configured_param=excluded.configured_param,
641                           add_timestamp=excluded.add_timestamp",
642            (
643                &addr,
644                serde_json::to_string(entered_param)?,
645                serde_json::to_string(configured)?,
646                add_timestamp,
647            ),
648        )
649        .await?;
650
651    if configured_addr.is_none() {
652        // If there is no transport yet, set the new transport as the primary one
653        context
654            .sql
655            .set_raw_config(Config::ConfiguredAddr.as_ref(), Some(&addr))
656            .await?;
657    }
658    Ok(())
659}
660
661/// Sends a sync message to synchronize transports across devices.
662pub(crate) async fn send_sync_transports(context: &Context) -> Result<()> {
663    info!(context, "Sending transport synchronization message.");
664
665    // Synchronize all transport configurations.
666    //
667    // Transport with ID 1 is never synchronized
668    // because it can only be created during initial configuration.
669    // This also guarantees that credentials for the first
670    // transport are never sent in sync messages,
671    // so this is not worse than when not using multi-transport.
672    // If transport ID 1 is reconfigured,
673    // likely because the password has changed,
674    // user has to reconfigure it manually on all devices.
675    let transports = context
676        .sql
677        .query_map_vec(
678            "SELECT entered_param, configured_param, add_timestamp
679             FROM transports WHERE id>1",
680            (),
681            |row| {
682                let entered_json: String = row.get(0)?;
683                let entered: EnteredLoginParam = serde_json::from_str(&entered_json)?;
684                let configured_json: String = row.get(1)?;
685                let configured: ConfiguredLoginParamJson = serde_json::from_str(&configured_json)?;
686                let timestamp: i64 = row.get(2)?;
687                Ok(TransportData {
688                    configured,
689                    entered,
690                    timestamp,
691                })
692            },
693        )
694        .await?;
695    let removed_transports = context
696        .sql
697        .query_map_vec(
698            "SELECT addr, remove_timestamp FROM removed_transports",
699            (),
700            |row| {
701                let addr: String = row.get(0)?;
702                let timestamp: i64 = row.get(1)?;
703                Ok(RemovedTransportData { addr, timestamp })
704            },
705        )
706        .await?;
707    context
708        .add_sync_item(SyncData::Transports {
709            transports,
710            removed_transports,
711        })
712        .await?;
713    context.scheduler.interrupt_inbox().await;
714
715    Ok(())
716}
717
718/// Process received data for transport synchronization.
719pub(crate) async fn sync_transports(
720    context: &Context,
721    transports: &[TransportData],
722    removed_transports: &[RemovedTransportData],
723) -> Result<()> {
724    for TransportData {
725        configured,
726        entered,
727        timestamp,
728    } in transports
729    {
730        save_transport(context, entered, configured, *timestamp).await?;
731    }
732
733    context
734        .sql
735        .transaction(|transaction| {
736            for RemovedTransportData { addr, timestamp } in removed_transports {
737                transaction.execute(
738                    "DELETE FROM transports
739                     WHERE addr=? AND add_timestamp<=?",
740                    (addr, timestamp),
741                )?;
742                transaction.execute(
743                    "INSERT INTO removed_transports (addr, remove_timestamp)
744                     VALUES (?, ?)
745                     ON CONFLICT (addr) DO
746                     UPDATE SET remove_timestamp = excluded.remove_timestamp
747                     WHERE excluded.remove_timestamp > remove_timestamp",
748                    (addr, timestamp),
749                )?;
750            }
751            Ok(())
752        })
753        .await?;
754
755    context.emit_event(EventType::TransportsModified);
756    Ok(())
757}
758
759#[cfg(test)]
760mod tests {
761    use super::*;
762    use crate::log::LogExt as _;
763    use crate::provider::get_provider_by_id;
764    use crate::test_utils::TestContext;
765    use crate::tools::time;
766
767    #[test]
768    fn test_configured_certificate_checks_display() {
769        use std::string::ToString;
770
771        assert_eq!(
772            "accept_invalid_certificates".to_string(),
773            ConfiguredCertificateChecks::AcceptInvalidCertificates.to_string()
774        );
775    }
776
777    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
778    async fn test_save_load_login_param() -> Result<()> {
779        let t = TestContext::new().await;
780
781        let param = ConfiguredLoginParam {
782            addr: "alice@example.org".to_string(),
783            imap: vec![ConfiguredServerLoginParam {
784                connection: ConnectionCandidate {
785                    host: "imap.example.com".to_string(),
786                    port: 123,
787                    security: ConnectionSecurity::Starttls,
788                },
789                user: "alice".to_string(),
790            }],
791            imap_user: "".to_string(),
792            imap_password: "foo".to_string(),
793            smtp: vec![ConfiguredServerLoginParam {
794                connection: ConnectionCandidate {
795                    host: "smtp.example.com".to_string(),
796                    port: 456,
797                    security: ConnectionSecurity::Tls,
798                },
799                user: "alice@example.org".to_string(),
800            }],
801            smtp_user: "".to_string(),
802            smtp_password: "bar".to_string(),
803            provider: None,
804            certificate_checks: ConfiguredCertificateChecks::Strict,
805            oauth2: false,
806        };
807
808        param
809            .clone()
810            .save_to_transports_table(&t, &EnteredLoginParam::default(), time())
811            .await?;
812        let expected_param = r#"{"addr":"alice@example.org","imap":[{"connection":{"host":"imap.example.com","port":123,"security":"Starttls"},"user":"alice"}],"imap_user":"","imap_password":"foo","smtp":[{"connection":{"host":"smtp.example.com","port":456,"security":"Tls"},"user":"alice@example.org"}],"smtp_user":"","smtp_password":"bar","provider_id":null,"certificate_checks":"Strict","oauth2":false}"#;
813        assert_eq!(
814            t.sql
815                .query_get_value::<String>("SELECT configured_param FROM transports", ())
816                .await?
817                .unwrap(),
818            expected_param
819        );
820        assert_eq!(t.is_configured().await?, true);
821        let (_transport_id, loaded) = ConfiguredLoginParam::load(&t).await?.unwrap();
822        assert_eq!(param, loaded);
823
824        // Legacy ConfiguredImapCertificateChecks config is ignored
825        t.set_config(Config::ConfiguredImapCertificateChecks, Some("999"))
826            .await?;
827        assert!(ConfiguredLoginParam::load(&t).await.is_ok());
828
829        // Test that we don't panic on unknown ConfiguredImapCertificateChecks values.
830        let wrong_param = expected_param.replace("Strict", "Stricct");
831        assert_ne!(expected_param, wrong_param);
832        t.sql
833            .execute("UPDATE transports SET configured_param=?", (wrong_param,))
834            .await?;
835        assert!(ConfiguredLoginParam::load(&t).await.is_err());
836
837        Ok(())
838    }
839
840    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
841    async fn test_posteo_alias() -> Result<()> {
842        let t = TestContext::new().await;
843
844        let user = "alice@posteo.de";
845
846        // Alice has old config with "alice@posteo.at" address
847        // and "alice@posteo.de" username.
848        t.set_config(Config::Configured, Some("1")).await?;
849        t.set_config(Config::ConfiguredProvider, Some("posteo"))
850            .await?;
851        t.sql
852            .set_raw_config(Config::ConfiguredAddr.as_ref(), Some("alice@posteo.at"))
853            .await?;
854        t.set_config(Config::ConfiguredMailServer, Some("posteo.de"))
855            .await?;
856        t.set_config(Config::ConfiguredMailPort, Some("993"))
857            .await?;
858        t.set_config(Config::ConfiguredMailSecurity, Some("1"))
859            .await?; // TLS
860        t.set_config(Config::ConfiguredMailUser, Some(user)).await?;
861        t.set_config(Config::ConfiguredMailPw, Some("foobarbaz"))
862            .await?;
863        t.set_config(Config::ConfiguredImapCertificateChecks, Some("1"))
864            .await?; // Strict
865        t.set_config(Config::ConfiguredSendServer, Some("posteo.de"))
866            .await?;
867        t.set_config(Config::ConfiguredSendPort, Some("465"))
868            .await?;
869        t.set_config(Config::ConfiguredSendSecurity, Some("1"))
870            .await?; // TLS
871        t.set_config(Config::ConfiguredSendUser, Some(user)).await?;
872        t.set_config(Config::ConfiguredSendPw, Some("foobarbaz"))
873            .await?;
874        t.set_config(Config::ConfiguredSmtpCertificateChecks, Some("1"))
875            .await?; // Strict
876        t.set_config(Config::ConfiguredServerFlags, Some("0"))
877            .await?;
878
879        let param = ConfiguredLoginParam {
880            addr: "alice@posteo.at".to_string(),
881            imap: vec![
882                ConfiguredServerLoginParam {
883                    connection: ConnectionCandidate {
884                        host: "posteo.de".to_string(),
885                        port: 993,
886                        security: ConnectionSecurity::Tls,
887                    },
888                    user: user.to_string(),
889                },
890                ConfiguredServerLoginParam {
891                    connection: ConnectionCandidate {
892                        host: "posteo.de".to_string(),
893                        port: 143,
894                        security: ConnectionSecurity::Starttls,
895                    },
896                    user: user.to_string(),
897                },
898            ],
899            imap_user: "alice@posteo.de".to_string(),
900            imap_password: "foobarbaz".to_string(),
901            smtp: vec![
902                ConfiguredServerLoginParam {
903                    connection: ConnectionCandidate {
904                        host: "posteo.de".to_string(),
905                        port: 465,
906                        security: ConnectionSecurity::Tls,
907                    },
908                    user: user.to_string(),
909                },
910                ConfiguredServerLoginParam {
911                    connection: ConnectionCandidate {
912                        host: "posteo.de".to_string(),
913                        port: 587,
914                        security: ConnectionSecurity::Starttls,
915                    },
916                    user: user.to_string(),
917                },
918            ],
919            smtp_user: "alice@posteo.de".to_string(),
920            smtp_password: "foobarbaz".to_string(),
921            provider: get_provider_by_id("posteo"),
922            certificate_checks: ConfiguredCertificateChecks::Strict,
923            oauth2: false,
924        };
925
926        let loaded = ConfiguredLoginParam::load_legacy(&t).await?.unwrap();
927        assert_eq!(loaded, param);
928
929        migrate_configured_login_param(&t).await;
930        let (_transport_id, loaded) = ConfiguredLoginParam::load(&t).await?.unwrap();
931        assert_eq!(loaded, param);
932
933        Ok(())
934    }
935
936    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
937    async fn test_empty_server_list_legacy() -> Result<()> {
938        // Find a provider that does not have server list set.
939        //
940        // There is at least one such provider in the provider database.
941        let (domain, provider) = crate::provider::data::PROVIDER_DATA
942            .iter()
943            .find(|(_domain, provider)| provider.server.is_empty())
944            .unwrap();
945
946        let t = TestContext::new().await;
947
948        let addr = format!("alice@{domain}");
949
950        t.set_config(Config::Configured, Some("1")).await?;
951        t.set_config(Config::ConfiguredProvider, Some(provider.id))
952            .await?;
953        t.sql
954            .set_raw_config(Config::ConfiguredAddr.as_ref(), Some(&addr))
955            .await?;
956        t.set_config(Config::ConfiguredMailPw, Some("foobarbaz"))
957            .await?;
958        t.set_config(Config::ConfiguredImapCertificateChecks, Some("1"))
959            .await?; // Strict
960        t.set_config(Config::ConfiguredSendPw, Some("foobarbaz"))
961            .await?;
962        t.set_config(Config::ConfiguredSmtpCertificateChecks, Some("1"))
963            .await?; // Strict
964        t.set_config(Config::ConfiguredServerFlags, Some("0"))
965            .await?;
966
967        let loaded = ConfiguredLoginParam::load_legacy(&t).await?.unwrap();
968        assert_eq!(loaded.provider, Some(*provider));
969        assert_eq!(loaded.imap.is_empty(), false);
970        assert_eq!(loaded.smtp.is_empty(), false);
971
972        migrate_configured_login_param(&t).await;
973
974        let (_transport_id, loaded) = ConfiguredLoginParam::load(&t).await?.unwrap();
975        assert_eq!(loaded.provider, Some(*provider));
976        assert_eq!(loaded.imap.is_empty(), false);
977        assert_eq!(loaded.smtp.is_empty(), false);
978
979        Ok(())
980    }
981
982    async fn migrate_configured_login_param(t: &TestContext) {
983        t.sql.execute("DROP TABLE transports;", ()).await.unwrap();
984        t.sql.set_raw_config_int("dbversion", 130).await.unwrap();
985        t.sql.run_migrations(t).await.log_err(t).ok();
986    }
987
988    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
989    async fn test_empty_server_list() -> Result<()> {
990        // Find a provider that does not have server list set.
991        //
992        // There is at least one such provider in the provider database.
993        let (domain, provider) = crate::provider::data::PROVIDER_DATA
994            .iter()
995            .find(|(_domain, provider)| provider.server.is_empty())
996            .unwrap();
997
998        let t = TestContext::new().await;
999
1000        let addr = format!("alice@{domain}");
1001
1002        ConfiguredLoginParam {
1003            addr: addr.clone(),
1004            imap: vec![ConfiguredServerLoginParam {
1005                connection: ConnectionCandidate {
1006                    host: "example.org".to_string(),
1007                    port: 100,
1008                    security: ConnectionSecurity::Tls,
1009                },
1010                user: addr.clone(),
1011            }],
1012            imap_user: addr.clone(),
1013            imap_password: "foobarbaz".to_string(),
1014            smtp: vec![ConfiguredServerLoginParam {
1015                connection: ConnectionCandidate {
1016                    host: "example.org".to_string(),
1017                    port: 100,
1018                    security: ConnectionSecurity::Tls,
1019                },
1020                user: addr.clone(),
1021            }],
1022            smtp_user: addr.clone(),
1023            smtp_password: "foobarbaz".to_string(),
1024            provider: Some(provider),
1025            certificate_checks: ConfiguredCertificateChecks::Automatic,
1026            oauth2: false,
1027        }
1028        .save_to_transports_table(&t, &EnteredLoginParam::default(), time())
1029        .await?;
1030
1031        let (_transport_id, loaded) = ConfiguredLoginParam::load(&t).await?.unwrap();
1032        assert_eq!(loaded.provider, Some(*provider));
1033        assert_eq!(loaded.imap.is_empty(), false);
1034        assert_eq!(loaded.smtp.is_empty(), false);
1035        assert_eq!(t.get_configured_provider().await?, Some(*provider));
1036
1037        Ok(())
1038    }
1039}