git-gui / lib / sshkey.tclon commit Merge branch 'jn/branch-move-to-self' (b2dd021)
   1# git-gui about git-gui dialog
   2# Copyright (C) 2006, 2007 Shawn Pearce
   3
   4proc find_ssh_key {} {
   5        foreach name {~/.ssh/id_dsa.pub ~/.ssh/id_rsa.pub ~/.ssh/identity.pub} {
   6                if {[file exists $name]} {
   7                        set fh    [open $name r]
   8                        set cont  [read $fh]
   9                        close $fh
  10                        return [list $name $cont]
  11                }
  12        }
  13
  14        return {}
  15}
  16
  17proc do_ssh_key {} {
  18        global sshkey_title have_tk85 sshkey_fd use_ttk NS
  19
  20        set w .sshkey_dialog
  21        if {[winfo exists $w]} {
  22                raise $w
  23                return
  24        }
  25
  26        Dialog $w
  27        wm transient $w .
  28
  29        set finfo [find_ssh_key]
  30        if {$finfo eq {}} {
  31                set sshkey_title [mc "No keys found."]
  32                set gen_state   normal
  33        } else {
  34                set sshkey_title [mc "Found a public key in: %s" [lindex $finfo 0]]
  35                set gen_state   disabled
  36        }
  37
  38        ${NS}::frame $w.header
  39        ${NS}::label $w.header.lbl -textvariable sshkey_title -anchor w
  40        ${NS}::button $w.header.gen -text [mc "Generate Key"] \
  41                -command [list make_ssh_key $w] -state $gen_state
  42        pack $w.header.lbl -side left -expand 1 -fill x
  43        pack $w.header.gen -side right
  44        pack $w.header -fill x -pady 5 -padx 5
  45
  46        text $w.contents -width 60 -height 10 -wrap char -relief sunken
  47        pack $w.contents -fill both -expand 1
  48        if {$have_tk85} {
  49                set clr darkblue
  50                if {$use_ttk} { set clr [ttk::style lookup . -selectbackground] }
  51                $w.contents configure -inactiveselectbackground $clr
  52        }
  53
  54        ${NS}::frame $w.buttons
  55        ${NS}::button $w.buttons.close -text [mc Close] \
  56                -default active -command [list destroy $w]
  57        pack $w.buttons.close -side right
  58        ${NS}::button $w.buttons.copy -text [mc "Copy To Clipboard"] \
  59                -command [list tk_textCopy $w.contents]
  60        pack $w.buttons.copy -side left
  61        pack $w.buttons -side bottom -fill x -pady 5 -padx 5
  62
  63        if {$finfo ne {}} {
  64                $w.contents insert end [lindex $finfo 1] sel
  65        }
  66        $w.contents configure -state disabled
  67
  68        bind $w <Visibility> "grab $w; focus $w.buttons.close"
  69        bind $w <Key-Escape> "destroy $w"
  70        bind $w <Key-Return> "destroy $w"
  71        bind $w <Destroy> kill_sshkey
  72        wm title $w [mc "Your OpenSSH Public Key"]
  73        tk::PlaceWindow $w widget .
  74        tkwait window $w
  75}
  76
  77proc make_ssh_key {w} {
  78        global sshkey_title sshkey_output sshkey_fd
  79
  80        set sshkey_title [mc "Generating..."]
  81        $w.header.gen configure -state disabled
  82
  83        set cmdline [list sh -c {echo | ssh-keygen -q -t rsa -f ~/.ssh/id_rsa 2>&1}]
  84
  85        if {[catch { set sshkey_fd [_open_stdout_stderr $cmdline] } err]} {
  86                error_popup [mc "Could not start ssh-keygen:\n\n%s" $err]
  87                return
  88        }
  89
  90        set sshkey_output {}
  91        fconfigure $sshkey_fd -blocking 0
  92        fileevent $sshkey_fd readable [list read_sshkey_output $sshkey_fd $w]
  93}
  94
  95proc kill_sshkey {} {
  96        global sshkey_fd
  97        if {![info exists sshkey_fd]} return
  98        catch { kill_file_process $sshkey_fd }
  99        catch { close $sshkey_fd }
 100}
 101
 102proc read_sshkey_output {fd w} {
 103        global sshkey_fd sshkey_output sshkey_title
 104
 105        set sshkey_output "$sshkey_output[read $fd]"
 106        if {![eof $fd]} return
 107
 108        fconfigure $fd -blocking 1
 109        unset sshkey_fd
 110
 111        $w.contents configure -state normal
 112        if {[catch {close $fd} err]} {
 113                set sshkey_title [mc "Generation failed."]
 114                $w.contents insert end $err
 115                $w.contents insert end "\n"
 116                $w.contents insert end $sshkey_output
 117        } else {
 118                set finfo [find_ssh_key]
 119                if {$finfo eq {}} {
 120                        set sshkey_title [mc "Generation succeeded, but no keys found."]
 121                        $w.contents insert end $sshkey_output
 122                } else {
 123                        set sshkey_title [mc "Your key is in: %s" [lindex $finfo 0]]
 124                        $w.contents insert end [lindex $finfo 1] sel
 125                }
 126        }
 127        $w.contents configure -state disable
 128}