Skip to content

Commit 6ee5ac5

Browse files
author
Alexander Krotov
committed
Replace DC_LP_..._SOCKET_FLAGS with DC_SOCKET_...
This change moves socket security settings flom server_flags to separate configuration variables for IMAP and SMTP, similar to what Delta Chat Desktop already uses. This allows to completely separate IMAP and SMTP server configuration.
1 parent a398d71 commit 6ee5ac5

File tree

12 files changed

+176
-170
lines changed

12 files changed

+176
-170
lines changed

deltachat-ffi/deltachat.h

+27-38
Original file line numberDiff line numberDiff line change
@@ -4169,86 +4169,75 @@ int64_t dc_lot_get_timestamp (const dc_lot_t* lot);
41694169

41704170

41714171
/**
4172-
* @}
4173-
*/
4174-
4175-
4176-
/**
4177-
* @defgroup DC_LP DC_LP
4172+
* @defgroup DC_SOCKET DC_SOCKET
41784173
*
4179-
* Flags for configuring IMAP and SMTP servers.
4180-
* These flags are optional
4181-
* and may be set together with the username, password etc.
4182-
* via dc_set_config() using the key "server_flags".
4174+
* These constants configure socket security.
41834175
*
4184-
* @addtogroup DC_LP
4176+
* @addtogroup DC_SOCKET
41854177
* @{
41864178
*/
41874179

4188-
41894180
/**
4190-
* Force OAuth2 authorization. This flag does not skip automatic configuration.
4191-
* Before calling dc_configure() with DC_LP_AUTH_OAUTH2 set,
4192-
* the user has to confirm access at the URL returned by dc_get_oauth2_url().
4181+
* @}
41934182
*/
4194-
#define DC_LP_AUTH_OAUTH2 0x2
4195-
41964183

41974184
/**
4198-
* Force NORMAL authorization, this is the default.
4199-
* If this flag is set, automatic configuration is skipped.
4185+
* Connect via SSL/TLS.
42004186
*/
4201-
#define DC_LP_AUTH_NORMAL 0x4
4187+
#define DC_SOCKET_SSL 1
42024188

42034189

42044190
/**
4205-
* Connect to IMAP via STARTTLS.
4191+
* Connect via STARTTLS.
42064192
* If this flag is set, automatic configuration is skipped.
42074193
*/
4208-
#define DC_LP_IMAP_SOCKET_STARTTLS 0x100
4194+
#define DC_SOCKET_STARTTLS 2
42094195

42104196

42114197
/**
4212-
* Connect to IMAP via SSL.
4213-
* If this flag is set, automatic configuration is skipped.
4198+
* Connect unencrypted, this should not be used.
42144199
*/
4215-
#define DC_LP_IMAP_SOCKET_SSL 0x200
4200+
#define DC_SOCKET_PLAIN 3
42164201

42174202

42184203
/**
4219-
* Connect to IMAP unencrypted, this should not be used.
4220-
* If this flag is set, automatic configuration is skipped.
4204+
* @}
42214205
*/
4222-
#define DC_LP_IMAP_SOCKET_PLAIN 0x400
42234206

42244207

42254208
/**
4226-
* Connect to SMTP via STARTTLS.
4227-
* If this flag is set, automatic configuration is skipped.
4209+
* @defgroup DC_LP DC_LP
4210+
*
4211+
* Flags for configuring IMAP and SMTP servers.
4212+
* These flags are optional
4213+
* and may be set together with the username, password etc.
4214+
* via dc_set_config() using the key "server_flags".
4215+
*
4216+
* @addtogroup DC_LP
4217+
* @{
42284218
*/
4229-
#define DC_LP_SMTP_SOCKET_STARTTLS 0x10000
42304219

42314220

42324221
/**
4233-
* Connect to SMTP via SSL.
4234-
* If this flag is set, automatic configuration is skipped.
4222+
* Force OAuth2 authorization. This flag does not skip automatic configuration.
4223+
* Before calling dc_configure() with DC_LP_AUTH_OAUTH2 set,
4224+
* the user has to confirm access at the URL returned by dc_get_oauth2_url().
42354225
*/
4236-
#define DC_LP_SMTP_SOCKET_SSL 0x20000
4226+
#define DC_LP_AUTH_OAUTH2 0x2
42374227

42384228

42394229
/**
4240-
* Connect to SMTP unencrypted, this should not be used.
4230+
* Force NORMAL authorization, this is the default.
42414231
* If this flag is set, automatic configuration is skipped.
42424232
*/
4243-
#define DC_LP_SMTP_SOCKET_PLAIN 0x40000 ///<
4233+
#define DC_LP_AUTH_NORMAL 0x4
4234+
42444235

42454236
/**
42464237
* @}
42474238
*/
42484239

42494240
#define DC_LP_AUTH_FLAGS (DC_LP_AUTH_OAUTH2|DC_LP_AUTH_NORMAL) // if none of these flags are set, the default is chosen
4250-
#define DC_LP_IMAP_SOCKET_FLAGS (DC_LP_IMAP_SOCKET_STARTTLS|DC_LP_IMAP_SOCKET_SSL|DC_LP_IMAP_SOCKET_PLAIN) // if none of these flags are set, the default is chosen
4251-
#define DC_LP_SMTP_SOCKET_FLAGS (DC_LP_SMTP_SOCKET_STARTTLS|DC_LP_SMTP_SOCKET_SSL|DC_LP_SMTP_SOCKET_PLAIN) // if none of these flags are set, the default is chosen
42524241

42534242
/**
42544243
* @defgroup DC_CERTCK DC_CERTCK

src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ pub enum Config {
2424
MailUser,
2525
MailPw,
2626
MailPort,
27+
MailSecurity,
2728
ImapCertificateChecks,
2829
SendServer,
2930
SendUser,
3031
SendPw,
3132
SendPort,
33+
SendSecurity,
3234
SmtpCertificateChecks,
3335
ServerFlags,
3436

src/configure/auto_mozilla.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
//! Documentation: https://developer.mozilla.org/en-US/docs/Mozilla/Thunderbird/Autoconfiguration */
44
use quick_xml::events::{BytesEnd, BytesStart, BytesText};
55

6-
use crate::constants::*;
76
use crate::context::Context;
87
use crate::login_param::LoginParam;
8+
use crate::provider::Socket;
99

1010
use super::read_url::read_url;
1111
use super::Error;
@@ -136,13 +136,13 @@ fn moz_autoconfigure_text_cb<B: std::io::BufRead>(
136136
MozConfigTag::Sockettype => {
137137
let val_lower = val.to_lowercase();
138138
if val_lower == "ssl" {
139-
moz_ac.out.server_flags |= DC_LP_IMAP_SOCKET_SSL as i32
139+
moz_ac.out.mail_security = Socket::SSL;
140140
}
141141
if val_lower == "starttls" {
142-
moz_ac.out.server_flags |= DC_LP_IMAP_SOCKET_STARTTLS as i32
142+
moz_ac.out.mail_security = Socket::STARTTLS;
143143
}
144144
if val_lower == "plain" {
145-
moz_ac.out.server_flags |= DC_LP_IMAP_SOCKET_PLAIN as i32
145+
moz_ac.out.mail_security = Socket::Plain;
146146
}
147147
}
148148
_ => {}
@@ -154,13 +154,13 @@ fn moz_autoconfigure_text_cb<B: std::io::BufRead>(
154154
MozConfigTag::Sockettype => {
155155
let val_lower = val.to_lowercase();
156156
if val_lower == "ssl" {
157-
moz_ac.out.server_flags |= DC_LP_SMTP_SOCKET_SSL as i32
157+
moz_ac.out.send_security = Socket::SSL;
158158
}
159159
if val_lower == "starttls" {
160-
moz_ac.out.server_flags |= DC_LP_SMTP_SOCKET_STARTTLS as i32
160+
moz_ac.out.send_security = Socket::STARTTLS;
161161
}
162162
if val_lower == "plain" {
163-
moz_ac.out.server_flags |= DC_LP_SMTP_SOCKET_PLAIN as i32
163+
moz_ac.out.send_security = Socket::Plain;
164164
}
165165
}
166166
_ => {}

src/configure/auto_outlook.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
33
use quick_xml::events::BytesEnd;
44

5-
use crate::constants::*;
65
use crate::context::Context;
76
use crate::login_param::LoginParam;
7+
use crate::provider::Socket;
88

99
use super::read_url::read_url;
1010
use super::Error;
@@ -146,19 +146,19 @@ fn outlk_autodiscover_endtag_cb(event: &BytesEnd, outlk_ad: &mut OutlookAutodisc
146146
std::mem::replace(&mut outlk_ad.config_server, String::new());
147147
outlk_ad.out.mail_port = port;
148148
if ssl_on {
149-
outlk_ad.out.server_flags |= DC_LP_IMAP_SOCKET_SSL as i32
149+
outlk_ad.out.mail_security = Socket::SSL
150150
} else if ssl_off {
151-
outlk_ad.out.server_flags |= DC_LP_IMAP_SOCKET_PLAIN as i32
151+
outlk_ad.out.mail_security = Socket::Plain
152152
}
153153
outlk_ad.out_imap_set = true
154154
} else if type_ == "smtp" && !outlk_ad.out_smtp_set {
155155
outlk_ad.out.send_server =
156156
std::mem::replace(&mut outlk_ad.config_server, String::new());
157157
outlk_ad.out.send_port = outlk_ad.config_port;
158158
if ssl_on {
159-
outlk_ad.out.server_flags |= DC_LP_SMTP_SOCKET_SSL as i32
159+
outlk_ad.out.send_security = Socket::SSL
160160
} else if ssl_off {
161-
outlk_ad.out.server_flags |= DC_LP_SMTP_SOCKET_PLAIN as i32
161+
outlk_ad.out.send_security = Socket::Plain
162162
}
163163
outlk_ad.out_smtp_set = true
164164
}

src/configure/mod.rs

+24-57
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::imap::Imap;
1616
use crate::login_param::{CertificateChecks, LoginParam, LoginParamNew, ServerParams};
1717
use crate::message::Message;
1818
use crate::oauth2::*;
19+
use crate::provider::Socket;
1920
use crate::smtp::Smtp;
2021
use crate::{chat, e2ee, provider};
2122

@@ -184,9 +185,11 @@ async fn configure(ctx: &Context, param: &mut LoginParam) -> Result<()> {
184185
// all other values are always NULL when entering autoconfig
185186
param.mail_server = cfg.mail_server.clone();
186187
param.mail_port = cfg.mail_port;
188+
param.mail_security = cfg.mail_security;
187189
param.send_server = cfg.send_server.clone();
188190
param.send_port = cfg.send_port;
189191
param.send_user = cfg.send_user.clone();
192+
param.send_security = cfg.send_security;
190193
param.server_flags = cfg.server_flags;
191194
// although param_autoconfig's data are no longer needed from,
192195
// it is used to later to prevent trying variations of port/server/logins
@@ -201,12 +204,6 @@ async fn configure(ctx: &Context, param: &mut LoginParam) -> Result<()> {
201204
if param.send_pw.is_empty() {
202205
param.send_pw = param.mail_pw.clone()
203206
}
204-
if !dc_exactly_one_bit_set(param.server_flags & DC_LP_IMAP_SOCKET_FLAGS as i32) {
205-
param.server_flags &= !(DC_LP_IMAP_SOCKET_FLAGS as i32);
206-
}
207-
if !dc_exactly_one_bit_set(param.server_flags & (DC_LP_SMTP_SOCKET_FLAGS as i32)) {
208-
param.server_flags &= !(DC_LP_SMTP_SOCKET_FLAGS as i32);
209-
}
210207
if !dc_exactly_one_bit_set(param.server_flags & DC_LP_AUTH_FLAGS as i32) {
211208
param.server_flags &= !(DC_LP_AUTH_FLAGS as i32);
212209
param.server_flags |= DC_LP_AUTH_NORMAL as i32
@@ -393,22 +390,14 @@ pub fn loginparam_new_to_old(context: &Context, servers: &LoginParamNew) -> Opti
393390
p.mail_server = imap.hostname.to_string();
394391
p.mail_user = imap.apply_username_pattern(addr.clone());
395392
p.mail_port = imap.port as i32;
393+
p.mail_security = imap.socket;
396394
p.imap_certificate_checks = CertificateChecks::AcceptInvalidCertificates;
397-
p.server_flags |= match imap.socket {
398-
provider::Socket::Plain => DC_LP_SMTP_SOCKET_PLAIN,
399-
provider::Socket::STARTTLS => DC_LP_IMAP_SOCKET_STARTTLS,
400-
provider::Socket::SSL => DC_LP_IMAP_SOCKET_SSL,
401-
};
402395

403396
p.send_server = smtp.hostname.to_string();
404397
p.send_user = smtp.apply_username_pattern(addr.clone());
405398
p.send_port = smtp.port as i32;
399+
p.send_security = smtp.socket;
406400
p.smtp_certificate_checks = CertificateChecks::AcceptInvalidCertificates;
407-
p.server_flags |= match smtp.socket {
408-
provider::Socket::Plain => DC_LP_SMTP_SOCKET_PLAIN,
409-
provider::Socket::STARTTLS => DC_LP_SMTP_SOCKET_STARTTLS,
410-
provider::Socket::SSL => DC_LP_SMTP_SOCKET_SSL,
411-
};
412401

413402
info!(context, "offline autoconfig found: {}", p);
414403
return Some(p);
@@ -423,12 +412,7 @@ pub fn loginparam_old_to_new(p: LoginParam) -> LoginParamNew {
423412
addr: p.addr.clone(),
424413
imap: vec![ServerParams {
425414
protocol: Protocol::IMAP,
426-
socket: match p.server_flags & DC_LP_IMAP_SOCKET_FLAGS as i32 {
427-
DC_LP_IMAP_SOCKET_PLAIN => provider::Socket::Plain,
428-
DC_LP_IMAP_SOCKET_STARTTLS => provider::Socket::STARTTLS,
429-
DC_LP_IMAP_SOCKET_SSL => provider::Socket::SSL,
430-
_ => Default::default(),
431-
},
415+
socket: p.mail_security,
432416
port: p.mail_port as u16,
433417
hostname: p.mail_server,
434418
username_pattern: if p.mail_user.contains('@') {
@@ -439,12 +423,7 @@ pub fn loginparam_old_to_new(p: LoginParam) -> LoginParamNew {
439423
}],
440424
smtp: vec![ServerParams {
441425
protocol: Protocol::SMTP,
442-
socket: match p.server_flags & DC_LP_SMTP_SOCKET_FLAGS as i32 {
443-
DC_LP_SMTP_SOCKET_PLAIN => provider::Socket::Plain,
444-
DC_LP_SMTP_SOCKET_STARTTLS => provider::Socket::STARTTLS,
445-
DC_LP_SMTP_SOCKET_SSL => provider::Socket::SSL,
446-
_ => Default::default(),
447-
},
426+
socket: p.send_security,
448427
port: p.send_port as u16,
449428
hostname: p.send_server,
450429
username_pattern: if p.send_user.contains('@') {
@@ -493,10 +472,10 @@ async fn try_imap_ports(
493472
) -> Result<LoginParam> {
494473
// Try to infer port from socket security.
495474
if param.mail_port == 0 {
496-
if 0 != param.server_flags & DC_LP_IMAP_SOCKET_SSL {
475+
if param.mail_security == Socket::SSL {
497476
param.mail_port = 993
498477
}
499-
if 0 != param.server_flags & (DC_LP_IMAP_SOCKET_STARTTLS | DC_LP_IMAP_SOCKET_PLAIN) {
478+
if param.mail_security == Socket::STARTTLS || param.mail_security == Socket::Plain {
500479
param.mail_port = 143
501480
}
502481
}
@@ -507,29 +486,25 @@ async fn try_imap_ports(
507486
// Try common secure combinations.
508487

509488
// Try TLS over port 993
510-
param.server_flags &= !(DC_LP_IMAP_SOCKET_FLAGS as i32);
511-
param.server_flags |= DC_LP_IMAP_SOCKET_SSL as i32;
489+
param.mail_security = Socket::SSL;
512490
param.mail_port = 993;
513491
if let Ok(login_param) = try_imap_usernames(context, param.clone(), imap).await {
514492
return Ok(login_param);
515493
}
516494

517495
// Try STARTTLS over port 143
518-
param.server_flags &= !(DC_LP_IMAP_SOCKET_FLAGS as i32);
519-
param.server_flags |= DC_LP_IMAP_SOCKET_STARTTLS as i32;
496+
param.mail_security = Socket::STARTTLS;
520497
param.mail_port = 143;
521498
try_imap_usernames(context, param, imap).await
522-
} else if 0 == param.server_flags & DC_LP_SMTP_SOCKET_FLAGS as i32 {
499+
} else if param.mail_security == Socket::Automatic {
523500
// Try TLS over user-provided port.
524-
param.server_flags &= !(DC_LP_IMAP_SOCKET_FLAGS as i32);
525-
param.server_flags |= DC_LP_IMAP_SOCKET_SSL as i32;
501+
param.mail_security = Socket::SSL;
526502
if let Ok(login_param) = try_imap_usernames(context, param.clone(), imap).await {
527503
return Ok(login_param);
528504
}
529505

530506
// Try STARTTLS over user-provided port.
531-
param.server_flags &= !(DC_LP_IMAP_SOCKET_FLAGS as i32);
532-
param.server_flags |= DC_LP_IMAP_SOCKET_STARTTLS as i32;
507+
param.mail_security = Socket::STARTTLS;
533508
try_imap_usernames(context, param, imap).await
534509
} else {
535510
try_imap_usernames(context, param, imap).await
@@ -614,15 +589,11 @@ async fn try_smtp_ports(
614589
) -> Result<LoginParam> {
615590
// Try to infer port from socket security.
616591
if param.send_port == 0 {
617-
if 0 != param.server_flags & DC_LP_SMTP_SOCKET_STARTTLS as i32 {
618-
param.send_port = 587;
619-
}
620-
if 0 != param.server_flags & DC_LP_SMTP_SOCKET_PLAIN as i32 {
621-
param.send_port = 25;
622-
}
623-
if 0 != param.server_flags & DC_LP_SMTP_SOCKET_SSL as i32 {
624-
param.send_port = 465;
625-
}
592+
param.send_port = match param.send_security {
593+
Socket::Automatic => 0,
594+
Socket::STARTTLS | Socket::Plain => 587,
595+
Socket::SSL => 465,
596+
};
626597
}
627598

628599
if param.send_port == 0 {
@@ -631,29 +602,25 @@ async fn try_smtp_ports(
631602
// Try common secure combinations.
632603

633604
// Try TLS over port 465.
634-
param.server_flags &= !(DC_LP_SMTP_SOCKET_FLAGS as i32);
635-
param.server_flags |= DC_LP_SMTP_SOCKET_SSL as i32;
605+
param.send_security = Socket::SSL;
636606
param.send_port = 465;
637607
if let Ok(login_param) = try_smtp_usernames(context, param.clone(), smtp).await {
638608
return Ok(login_param);
639609
}
640610

641611
// Try STARTTLS over port 587.
642-
param.server_flags &= !(DC_LP_SMTP_SOCKET_FLAGS as i32);
643-
param.server_flags |= DC_LP_SMTP_SOCKET_STARTTLS as i32;
612+
param.send_security = Socket::STARTTLS;
644613
param.send_port = 587;
645614
try_smtp_usernames(context, param, smtp).await
646-
} else if 0 == param.server_flags & DC_LP_SMTP_SOCKET_FLAGS as i32 {
615+
} else if param.send_security == Socket::Automatic {
647616
// Try TLS over user-provided port.
648-
param.server_flags &= !(DC_LP_SMTP_SOCKET_FLAGS as i32);
649-
param.server_flags |= DC_LP_SMTP_SOCKET_SSL as i32;
617+
param.send_security = Socket::SSL;
650618
if let Ok(param) = try_smtp_usernames(context, param.clone(), smtp).await {
651619
return Ok(param);
652620
}
653621

654622
// Try STARTTLS over user-provided port.
655-
param.server_flags &= !(DC_LP_SMTP_SOCKET_FLAGS as i32);
656-
param.server_flags |= DC_LP_SMTP_SOCKET_STARTTLS as i32;
623+
param.send_security = Socket::STARTTLS;
657624
try_smtp_usernames(context, param, smtp).await
658625
} else {
659626
try_smtp_usernames(context, param, smtp).await

0 commit comments

Comments
 (0)