Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

imap4 and tls/twapi::tls_socket

53 views
Skip to first unread message

JonoK

unread,
May 22, 2019, 12:51:53 AM5/22/19
to
I've been trying to connect to an exchange server via IMAP using the imap4 package.


I confirmed that IMAP is working by connecting using thunderbird ... settings say it is connecting using:
---
Server name: mail.gtaus.com.au
Connection security: STARTTLS
Authentication method: Normal password
---
This is on a windows 10 matching and I'm using the latest magicsplat distribution - tcl 8.6.9 - and as I can't access any of the core.tcl.tk pages, I cant' get to TLS, so I'm trying to use twapi::tls_socket instead ...

I've changed imap4::open to use tls_socket ... thus

# Open a new IMAP connection and initalize the handler.
proc open {hostname {port 0}} {
variable info
variable debug
variable use_ssl
if {$debug} {
puts "I: open $hostname $port (SSL=$use_ssl)"
}

if {$use_ssl} {
# if {[info procs ::tls::socket] eq ""} {
# error "Package TLS must be loaded for secure connections."
# }
if {!$port} {
set port 993
}
# set chan [::tls::socket $hostname $port]
set chan [::twapi::tls_socket $hostname $port]
} else {
if {!$port} {
set port 143
}
set chan [socket $hostname $port]
}
fconfigure $chan -encoding binary -translation binary
# Intialize the connection state array
initinfo $chan
# Get the banner
processline $chan
# Save the banner
set info($chan,banner) [lastline $chan]
return $chan
}

then in my program ...

---
package require imap4
package require twapi

set ::imap4::use_ssl 1

set server "mail.gtaus.com.au"
set port "143"
puts "server is $server port is $port"

set imap [::imap4::open $server $port]
---
and it crashes with this error

L:\>c:\Tcl\bin\tclsh.exe tcl\imap.tcl
server is mail.gtaus.com.au port is 143
The token supplied to the function is invalid
while executing
"InitializeSecurityContext $Credentials $Handle $Target $Inattr 0 $Datarep $inbuflist 0"
(procedure "sspi_step" line 16)
invoked from within
"sspi_step $SspiContext $indata"
(procedure "rethrow" line 2)
invoked from within
"rethrow"
invoked from within
"trap {
lassign [sspi_step $SspiContext $indata] status outdata leftover
} onerror {} {
debuglog "sspi_step returned error: [trapresult]"
close $Socket..."
(procedure "_blocking_negotiate_loop" line 18)
invoked from within
"_blocking_negotiate_loop $chan"
(procedure "_client_blocking_negotiate" line 8)
invoked from within
"_client_blocking_negotiate $chan"
(procedure "rethrow" line 6)
invoked from within
"rethrow"
invoked from within
"trap {
set so [socket {*}$socket_args {*}$args]
_init $chan $type $so $credentials $peersubject $requestclientcert [lrange $verifier 0 end] $server
if..."
(procedure "twapi::tls::_socket" line 50)
invoked from within
"::twapi::tls_socket $hostname $port"
(procedure "::imap4::open" line 17)
invoked from within
"::imap4::open $server $port"
invoked from within
"set imap [::imap4::open $server $port]"
(file "tcl\imap.tcl" line 12)

Any ideas on what's going on greatly appreciated (in advance :).

Jonathan.

JonoK

unread,
May 22, 2019, 2:46:58 AM5/22/19
to
As is the case ... post and find the answer.
I changed the port to 993 and it just worked. :)

Jonathan.

Harald Oehlmann

unread,
May 22, 2019, 3:29:13 AM5/22/19
to
Am 22.05.2019 um 08:46 schrieb JonoK:
> As is the case ... post and find the answer.
> I changed the port to 993 and it just worked. :)

Jonathan,

I would appreciate your patch for tcl-lib. An auto-sense for loaded
tls/twapi package may be used to switch between the two packages.

Thank you,
Harald

Jonathan Kelly

unread,
May 22, 2019, 8:56:54 PM5/22/19
to
Hi Harald,

that's really a job for someone else - I'm only an occassional TCL
hacker - though if someone could do the magic package loading part I
can update the rest of the script and test.

Jonathan.

Harald Oehlmann

unread,
May 27, 2019, 4:29:56 PM5/27/19
to
Well, from your initial program:

# Open a new IMAP connection and initalize the handler.
proc open {hostname {port 0}} {
variable info
variable debug
variable use_ssl
if {$debug} {
puts "I: open $hostname $port (SSL=$use_ssl)"
}

if {$use_ssl} {
if {[info procs ::tls::socket] eq ""} {
if {[info procs ::twapi::tls_socket] eq ""} {
error "Package TLS or TWAPI must be loaded for
secure connections."
} else {
set socket_cmd ::twapi::tls_socket
}
} else {
set socket_cmd ::tls::socket
}
if {!$port} {
set port 993
}
set chan [$socket_cmd $hostname $port]
} else {
if {!$port} {
set port 143
}
set chan [socket $hostname $port]
}
fconfigure $chan -encoding binary -translation binary
# Intialize the connection state array
initinfo $chan
# Get the banner
processline $chan
# Save the banner
set info($chan,banner) [lastline $chan]
return $chan
}

If this would work for you, you may send it as a patch to:
https://core.tcl.tk/tcllib/

Thank you,
Harald
0 new messages