--- /opt/tclrad/lib/tcllib1.19/ftp/ftp.tcl 2017-08-08 01:35:08.000000000 +0200 +++ ./ftp.tcl 2018-11-06 11:28:44.438296964 +0100 @@ -319,6 +319,9 @@ # still readable, still eof). [SF Tcllib Bug 15822535]. set rc 421 + if { [info exists ftp(Tls)] } { + catch {tls::unimport $ftp(CtrlSock)} + } catch {close $ftp(CtrlSock)} catch {unset ftp(CtrlSock)} catch {unset ftp(state.data)} @@ -361,9 +364,17 @@ if {$VERBOSE} { DisplayMsg $s StateBegin////////($ftp(State)) } switch -exact -- $ftp(State) { + tls { + PutsCtrlSock $s "AUTH TLS" + set ftp(State) user + Command $ftp(Command) tls + } user { - switch -exact -- $rc { + switch -exact -- $rc { 2 { + if { [info exists ftp(Tls)] } { + tls::import $ftp(CtrlSock) + } PutsCtrlSock $s "USER $ftp(User)" set ftp(State) passwd Command $ftp(Command) user @@ -380,10 +391,14 @@ 2 { set complete_with 1 Command $ftp(Command) password - } + } 3 { PutsCtrlSock $s "PASS $ftp(Passwd)" - set ftp(State) connect + if { [info exists ftp(Tls)] } { + set ftp(State) opts + } else { + set ftp(State) connect + } Command $ftp(Command) password } default { @@ -393,6 +408,42 @@ } } } + opts { + switch -exact -- $rc { + 2 { + PutsCtrlSock $s "OPTS UTF8 ON" + set ftp(State) pbsz + } default { + set errmsg "Error connecting! $msgtext" + set complete_with 0 + Command $ftp(Command) error $msgtext + } + } + } + pbsz { + switch -exact -- $rc { + 2 { + PutsCtrlSock $s "PBSZ 0" + set ftp(State) prot + } default { + set errmsg "Error connecting! $msgtext" + set complete_with 0 + Command $ftp(Command) error $msgtext + } + } + } + prot { + switch -exact -- $rc { + 2 { + PutsCtrlSock $s "PROT P" + set ftp(State) connect + } default { + set errmsg "Error connecting! $msgtext" + set complete_with 0 + Command $ftp(Command) error $msgtext + } + } + } connect { switch -exact -- $rc { 2 { @@ -552,6 +603,9 @@ switch -exact -- $rc { 1 - 2 { + if { [info exists ftp(Tls)] } { + tls::handshake $ftp(DataSock) + } set ftp(State) list_close } default { @@ -801,6 +855,9 @@ switch -exact -- $rc { 1 - 2 { + if { [info exists ftp(Tls)] } { + tls::handshake $ftp(DataSock) + } set ftp(State) put_close } default { @@ -879,6 +936,9 @@ append_sent { switch -exact -- $rc { 1 { + if { [info exists ftp(Tls)] } { + tls::handshake $ftp(DataSock) + } set ftp(State) append_close } default { @@ -969,6 +1029,9 @@ reget_sent { switch -exact -- $rc { 1 { + if { [info exists ftp(Tls)] } { + tls::handshake $ftp(DataSock) + } set ftp(State) reget_close } default { @@ -1043,6 +1106,9 @@ get_sent { switch -exact -- $rc { 1 { + if { [info exists ftp(Tls)] } { + tls::handshake $ftp(DataSock) + } set ftp(State) get_close } default { @@ -2392,6 +2458,9 @@ # wait for synchronization WaitOrTimeout $s + if { [info exists ftp(Tls)] } { + catch {tls::unimport $ftp(CtrlSock)} + } catch {close $ftp(CtrlSock)} catch {unset ftp} return 1 @@ -2495,7 +2564,7 @@ set ftp(state.control) "" # Get and set possible options - set options {-blocksize -timeout -mode -port -progress -output -command} + set options {-blocksize -timeout -mode -port -progress -output -command -tls} foreach {option value} $args { if { [lsearch -exact $options $option] != "-1" } { if { $DEBUG } { @@ -2517,6 +2586,10 @@ return $connections($ftp(User),$ftp(Passwd),$ftp(RemoteHost)) } + if { [info exists ftp(Tls)] } { + uplevel #0 [list package require tls] + set ftp(State) tls + } # No call of StateHandler is required at this time. # StateHandler at first time is called automatically @@ -2602,8 +2675,14 @@ # from the caller. Closing it is not our responsibility in that case. if {![info exists ftp(get:channel)]} { + if { [info exists ftp(Tls)] } { + catch {tls::unimport $ftp(DestCI)} + } catch {close $ftp(DestCI)} } + if { [info exists ftp(Tls)] } { + catch {tls::unimport $ftp(SourceCI)} + } catch {close $ftp(SourceCI)} catch {unset ftp(state.data)} DisplayMsg $s $error error @@ -2613,8 +2692,14 @@ # from the caller. Closing it is not our responsibility in that case. if {![info exists ftp(get:channel)]} { + if { [info exists ftp(Tls)] } { + catch {tls::unimport $ftp(DestCI)} + } close $ftp(DestCI) } + if { [info exists ftp(Tls)] } { + catch {tls::unimport $ftp(SourceCI)} + } close $ftp(SourceCI) catch {unset ftp(state.data)} if { $VERBOSE } { @@ -2732,6 +2817,9 @@ set ftp(List) [append ftp(List) $buffer] } } else { + if { [info exists ftp(Tls)] } { + catch {tls::unimport $sock} + } close $sock catch {unset ftp(state.data)} if { $VERBOSE } { @@ -2766,6 +2854,9 @@ incr ftp(Total) [string length $buffer] } } else { + if { [info exists ftp(Tls)] } { + catch {tls::unimport $sock} + } close $sock catch {unset ftp(state.data)} if { $VERBOSE } { @@ -2797,6 +2888,9 @@ set substr [string range $ftp(PutData) $ftp(Total) \ [expr {$ftp(Total) + $ftp(Blocksize)}]] if {[catch {puts -nonewline $sock "$substr"} result]} { + if { [info exists ftp(Tls)] } { + catch {tls::unimport $sock} + } close $sock catch {unset ftp(state.data)} if { $VERBOSE } { @@ -2807,6 +2901,9 @@ } } else { fileevent $sock writable {} + if { [info exists ftp(Tls)] } { + catch {tls::unimport $sock} + } close $sock catch {unset ftp(state.data)} if { $VERBOSE } { @@ -2842,9 +2939,21 @@ catch { unset ftp(AC) } catch {after cancel $ftp(Wait)} catch {fileevent $ftp(DataSock) readable {}} + if { [info exists ftp(Tls)] } { + catch {tls::unimport $ftp(DataSock)} + } catch {close $ftp(DataSock); unset ftp(DataSock)} + if { [info exists ftp(Tls)] } { + catch {tls::unimport $ftp(DestCI)} + } catch {close $ftp(DestCI); unset ftp(DestCI)} + if { [info exists ftp(Tls)] } { + catch {tls::unimport $ftp(SourceCI)} + } catch {close $ftp(SourceCI); unset ftp(SourceCI)} + if { [info exists ftp(Tls)] } { + catch {tls::unimport $ftp(DummySock)} + } catch {close $ftp(DummySock); unset ftp(DummySock)} return } @@ -2867,6 +2976,7 @@ upvar ::ftp::ftp$s ftp variable VERBOSE + if { $VERBOSE } { DisplayMsg $s "D: New Connection from $addr:$port" data DisplayMsg $s "D: Sequencer state $ftp(State)" data @@ -2930,6 +3040,9 @@ DisplayMsg $s "D: ... Connection from $addr:$port ... initialized" data } + if { [info exists ftp(Tls)] } { + tls::import $sock + } # Marker for WaitDataConn set ftp(AC) 1 return @@ -3096,7 +3209,7 @@ } # configure control channel - + fconfigure $ftp(CtrlSock) -buffering line -blocking $block -translation {auto crlf} fileevent $ftp(CtrlSock) readable [list [namespace current]::StateHandler $s $ftp(CtrlSock)] @@ -3105,7 +3218,7 @@ if { $VERBOSE } { DisplayMsg $s "C: Connection to $ftp(RemoteHost):$ftp(Port)" control } - + return 1 }