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