home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March B / SCO_CASTOR4RRT.iso / expect / root / usr / bin / tkpasswd / tkpasswd~
Text File  |  1998-08-19  |  16KB  |  630 lines

  1. #!/usr/bin/expectk -f
  2. # tkpasswd - Change passwords using Expectk
  3. # Author: Don Libes, NIST, October 1, 1993
  4. # Version: 1.8 - Added support for Tk 4.1
  5.  
  6. # There is no man page.  However, there is some on-line help when you run
  7. # the program.  Technical details and insights are described in the
  8. # O'Reilly book "Exploring Expect".
  9.  
  10. proc prog_exists {prog} {
  11.     global env
  12.  
  13.     foreach dir [split $env(PATH) :] {
  14.         if [file executable $dir/$prog] {
  15.             return 1
  16.         }
  17.     }
  18.     return 0
  19. }
  20.  
  21. frame .type -relief raised -bd 1
  22.  
  23. radiobutton .passwd -text passwd -variable passwd_cmd \
  24.         -value {passwd {cat /etc/passwd}} \
  25.         -anchor w -command get_users -relief flat
  26. pack .passwd -in .type -fill x
  27.  
  28. if [prog_exists yppasswd] {
  29.     radiobutton .yppasswd -text yppasswd -variable passwd_cmd \
  30.         -value {yppasswd {ypcat passwd}} \
  31.         -anchor w -command get_users -relief flat
  32.     pack .yppasswd -in .type -fill x
  33. }
  34.  
  35. if [prog_exists nispasswd] {
  36.     radiobutton .nispasswd -text nispasswd -variable passwd_cmd \
  37.         -value {nispasswd {niscat passwd}} \
  38.         -anchor w -command get_users -relief flat
  39.     pack .nispasswd -in .type -fill x 
  40. }
  41. pack .type -fill x
  42.  
  43. frame .sort -relief raised -bd 1
  44. radiobutton .unsorted -text unsorted -variable sort_cmd -value " " \
  45.         -anchor w -relief flat -command get_users
  46. radiobutton .name -text name -variable sort_cmd -value "| sort" \
  47.         -anchor w -relief flat -command get_users
  48. radiobutton .uid -text uid -variable sort_cmd -value "| sort -t: -n +2" \
  49.          -anchor w -relief flat -command get_users
  50. pack .unsorted .name .uid -in .sort -fill x
  51. pack .sort -fill x
  52.  
  53. frame .users -relief raised -bd 1
  54. # has to be wide enough for 8+1+5=14
  55. text .names -yscrollcommand ".scroll set" -width 14 -height 1 \
  56.         -font "*-bold-o-normal-*-120-*-m-*" -setgrid 1
  57. .names tag configure nopassword -relief raised
  58. .names tag configure selection -relief raised
  59.  
  60. set iscolor 0
  61. if {[winfo depth .] > 1} {
  62.     set iscolor 1
  63. }
  64.  
  65. if {$iscolor} {
  66.     .names tag configure nopassword -background red
  67.     .names tag configure selection -background green
  68. } else {
  69.     .names tag configure nopassword -background  black -foreground white
  70.     .names tag configure selection -background white -foreground black
  71. }
  72. scrollbar .scroll -command ".names yview" -relief raised
  73. pack .scroll -in .users -side left -fill y
  74. pack .names  -in .users -side left -fill y
  75. pack .users -expand 1 -fill y
  76.  
  77. wm minsize . 14 1
  78. wm maxsize . 14 999
  79. wm geometry . 14x10
  80.  
  81. frame .password_frame -relief raised -bd 1
  82. entry .password -textvar password -relief sunken -width 1
  83. focus .password
  84. bind .password <Return> password_set
  85. label .prompt -text "Password:" -bd 0
  86. button .password_set -text "set" -command password_set
  87. button .generate_button -text "generate" -command password_generate
  88. pack .prompt .password -in .password_frame -fill x -padx 2 -pady 2
  89. pack .password_set .generate_button -in .password_frame -side left -expand 1 -fill x -padx 2 -pady 2
  90. pack .password_frame -fill x
  91.  
  92. set dict_loaded 0
  93. checkbutton .dict -text "test dictionary" -variable dict_check \
  94.         -command {if !$dict_loaded load_dict} \
  95.         -anchor w
  96. pack .dict -fill x -padx 2 -pady 2
  97.  
  98.  
  99. button .quit -text quit -command exit
  100. button .help_button -text help -command help
  101. pack .quit .help_button -side left -expand 1 -fill x -padx 2 -pady 2
  102.  
  103. proc help {} {
  104.     if [catch {toplevel .help}] return
  105.     message .help.text -text \
  106. "tkpasswd - written by Don Libes, NIST, 10/1/93.
  107.  
  108. Click on passwd (local users) or yppasswd (NIS users).\
  109. Select user using mouse (or keys - see below).\
  110. Enter password or press ^G to generate a random password.\
  111. (Press ^A to adjust the generation parameters.)\
  112. Press return to set the password.\
  113. If the dictionary is enabled and the password is in it,\
  114. the password is rejected.
  115.  
  116. You must be root to set local passwords besides your own.\
  117. If you are not root, you must also enter an old password\
  118. when requested.
  119.  
  120. You do not have to move mouse into password field(s) to enter password.\
  121. ^U clears password field.\
  122. ^N and ^P select next/previous user.\
  123. M-n and M-p select next/previous user with no password.\
  124. (Users with no passwords are highlighted.)"
  125.  
  126.     button .help.ok -text "ok" -command {destroy .help}
  127.     pack .help.text
  128.     pack .help.ok -fill x -padx 2 -pady 2
  129. }
  130.  
  131. # get list of local users
  132. proc get_users {} {
  133.     global sort_cmd passwd_cmd
  134.     global nopasswords    ;# line numbers of entries with no passwords
  135.     global last_line    ;# last line of text box
  136.     global selection_line
  137.  
  138.     .names delete 1.0 end
  139.  
  140.     set file [open "|[lindex $passwd_cmd 1] $sort_cmd"]
  141.     set last_line 1
  142.     set nopasswords {}
  143.     while {[gets $file buf] != -1} {
  144.         set buf [split $buf :]
  145.         if [llength $buf]>2 {
  146.             # normal password entry
  147.             .names insert end "[format "%-8.8s %5d" [lindex $buf 0] [lindex $buf 2]]\n"
  148.             if 0==[string compare [lindex $buf 1] ""] {
  149.                 .names tag add nopassword \
  150.                         {end - 2 line linestart} \
  151.                         {end - 2 line lineend}
  152.                 lappend nopasswords $last_line
  153.             }
  154.         } else {
  155.             # +name style entry
  156.             .names insert end "$buf\n"
  157.         }
  158.         incr last_line
  159.     }
  160.     incr last_line -1
  161.     close $file
  162.     set selection_line 0
  163. }
  164.  
  165. proc feedback {msg} {
  166.     global password
  167.  
  168.     set password $msg
  169.     .password select from 0
  170.     .password select to end
  171.     update
  172. }
  173.  
  174. proc load_dict {} {
  175.     global dict dict_loaded
  176.  
  177.     feedback "loading dictionary..."
  178.  
  179.     if 0==[catch {open /usr/dict/words} file] {
  180.         rename set s
  181.         foreach w [split [read $file] "\n"] {s dict($w) ""}
  182.         close $file
  183.         rename s set
  184.         set dict_loaded 1
  185.         feedback "dictionary loaded"
  186.     } else {
  187.         feedback "dictionary missing"
  188.         .dict deselect
  189.     }
  190. }
  191.  
  192. # put whatever security checks you like in here
  193. proc weak_password {password} {
  194.     global dict dict_check
  195.  
  196.     if $dict_check {
  197.         feedback "checking password"
  198.  
  199.         if [info exists dict($password)] {
  200.             feedback "sorry - in dictionary"
  201.             return 1
  202.         }
  203.     }
  204.     return 0
  205. }
  206.  
  207. proc password_set {} {
  208.     global password passwd_cmd selection_line
  209.  
  210.     set new_password $password
  211.  
  212.     if {$selection_line==0} {
  213.         feedback "select a user first"
  214.         return
  215.     }
  216.     set user [lindex [.names get selection.first selection.last] 0]
  217.  
  218.     if [weak_password $password] return
  219.  
  220.     feedback "setting password . . ."
  221.  
  222.     set cmd [lindex $passwd_cmd 0]
  223.     spawn -noecho $cmd $user
  224.     log_user 0
  225.     set last_msg "error in $cmd"
  226.     while 1 {
  227.         expect {
  228.             -nocase "old password:" {
  229.                 exp_send "[get_old_password]\r"
  230.             } "assword*:" {
  231.                 exp_send "$new_password\r"
  232.             } -re "(.*)\r\n" {
  233.                 set last_msg $expect_out(1,string)
  234.             } eof break
  235.         }
  236.     }
  237.     set status [wait]
  238.     if [lindex $status 3]==0 {
  239.         feedback "set successfully"
  240.     } else {
  241.         feedback $last_msg
  242.     }
  243. }
  244.  
  245. # defaults for generating passwords
  246. set length 9
  247. set minnum 2
  248. set minlower 5
  249. set minupper 2
  250. set distribute 0
  251.  
  252. proc parameter_filename {} {
  253.     set file .tkpasswd.rc
  254.     if [info exists env(DOTDIR)] {
  255.         set file "$env(DOTDIR)/$file"
  256.     }
  257.     return ~/$file
  258. }
  259.  
  260. catch {source [parameter_filename]}
  261.  
  262. # save parameters in a file
  263. proc save_parameters {} {
  264.     global minnum minlower minupper length
  265.  
  266.     if [catch {open [parameter_filename] w} f] {
  267.         # should never happen, so don't bother with window code
  268.         puts "tkpasswd: could not write [parameter_filename]"
  269.         return
  270.     }
  271.     puts $f "# This is the .tkpasswd.rc file.  Do not edit it by hand as"
  272.     puts $f "# it is automatically maintained by tkpasswd.  Any manual"
  273.     puts $f "# modifications will be lost."
  274.     puts $f ""
  275.     puts $f "set length $length"
  276.     puts $f "set minnum $minnum"
  277.     puts $f "set minupper $minupper"
  278.     puts $f "set minlower $minlower"
  279.     close $f
  280. }
  281.  
  282. # insert char into password at a random position
  283. proc insert {pvar char} {
  284.     upvar $pvar p
  285.  
  286.     set p [linsert $p [rand [expr 1+[llength $p]]] $char]
  287. }
  288.  
  289. # given a size, distribute between left and right hands
  290. # taking into account where we left off
  291. proc psplit {max lvar rvar} {
  292.     upvar $lvar left $rvar right
  293.     global isleft
  294.  
  295.     if {$isleft} {
  296.         set right [expr $max/2]
  297.         set left [expr $max-$right]
  298.         set isleft [expr !($max%2)]
  299.     } else {
  300.         set left [expr $max/2]
  301.         set right [expr $max-$left]
  302.         set isleft [expr $max%2]
  303.     }
  304. }
  305.  
  306. proc password_generate {} {
  307.     global password length minnum minlower minupper
  308.     global lpass rpass initially_left isleft
  309.     global distribute
  310.  
  311.     if {$distribute} {
  312.        set lkeys {q w e r t a s d f g z x c v b}
  313.        set rkeys {y u i o p h j k l n m}
  314.        set lnums {1 2 3 4 5 6}
  315.        set rnums {7 8 9 0}
  316.     } else {
  317.        set lkeys {a b c d e f g h i j k l m n o p q r s t u v w x y z}
  318.        set rkeys {a b c d e f g h i j k l m n o p q r s t u v w x y z}
  319.        set lnums {0 1 2 3 4 5 6 7 8 9}
  320.        set rnums {0 1 2 3 4 5 6 7 8 9}
  321.     }
  322.     set lkeys_length [llength $lkeys]
  323.     set rkeys_length [llength $rkeys]
  324.     set lnums_length [llength $lnums]
  325.     set rnums_length [llength $rnums]
  326.  
  327.     # if there is any underspecification, use additional lowercase letters
  328.     set minlower [expr $length - ($minnum + $minupper)]
  329.  
  330.  
  331.     set lpass ""        ;# password chars typed by left hand
  332.     set rpass ""        ;# password chars typed by right hand
  333.     set password ""        ;# merged password
  334.  
  335.     # choose left or right starting hand
  336.     set initially_left [set isleft [rand 2]]
  337.  
  338.     psplit $minnum left right
  339.     for {set i 0} {$i<$left} {incr i} {
  340.         insert lpass [lindex $lnums [rand $lnums_length]]
  341.     }
  342.     for {set i 0} {$i<$right} {incr i} {
  343.         insert rpass [lindex $rnums [rand $rnums_length]]
  344.     }
  345.  
  346.     psplit $minlower left right
  347.     for {set i 0} {$i<$left} {incr i} {
  348.         insert lpass [lindex $lkeys [rand $lkeys_length]]
  349.     }
  350.     for {set i 0} {$i<$right} {incr i} {
  351.         insert rpass [lindex $rkeys [rand $rkeys_length]]
  352.     }
  353.  
  354.     psplit $minupper left right
  355.     for {set i 0} {$i<$left} {incr i} {
  356.         insert lpass [string toupper [lindex $lkeys [rand $lkeys_length]]]
  357.     }
  358.     for {set i 0} {$i<$right} {incr i} {
  359.         insert rpass [string toupper [lindex $rkeys [rand $rkeys_length]]]
  360.     }
  361.  
  362.     # merge results together
  363.     if {$initially_left} {
  364.         regexp "(\[^ ]*) *(.*)" "$lpass" x password lpass
  365.         while {[llength $lpass]} {
  366.             regexp "(\[^ ]*) *(.*)" "$password$rpass" x password rpass
  367.             regexp "(\[^ ]*) *(.*)" "$password$lpass" x password lpass
  368.         }
  369.         if {[llength $rpass]} {
  370.             append password $rpass
  371.         }        
  372.     } else {
  373.         regexp "(\[^ ]*) *(.*)" "$rpass" x password rpass
  374.         while {[llength $rpass]} {
  375.             regexp "(\[^ ]*) *(.*)" "$password$lpass" x password lpass
  376.             regexp "(\[^ ]*) *(.*)" "$password$rpass" x password rpass
  377.         }
  378.         if {[llength $lpass]} {
  379.             append password $lpass
  380.         }        
  381.     }
  382. }
  383.  
  384. set _ran [pid]
  385. proc rand {m} {
  386.     global _ran
  387.  
  388.     set period 259200
  389.     set _ran [expr ($_ran*7141 + 54773) % $period]
  390.     expr int($m*($_ran/double($period)))
  391. }
  392.  
  393. proc gen_bad_args {msg} {
  394.     if ![llength [info commands .parameters.errmsg]] {
  395.         message .parameters.errmsg -aspect 300
  396.         pack .parameters.errmsg
  397.     }
  398.     .parameters.errmsg configure -text "$msg\
  399. Please adjust the password generation arguments."
  400. }
  401.  
  402.  
  403. # tell tab what window to move between
  404. set parm_tabList {}
  405.  
  406. # The procedure below is invoked in response to tabs in the entry
  407. # windows.  It moves the focus to the next window in the tab list.
  408. # Arguments:
  409. #
  410. # list -    Ordered list of windows to receive focus
  411.  
  412. proc Tab {list} {
  413.     set i [lsearch $list [focus]]
  414.     if {$i < 0} {
  415.     set i 0
  416.     } else {
  417.     incr i
  418.     if {$i >= [llength $list]} {
  419.         set i 0
  420.     }
  421.     }
  422.     focus [lindex $list $i]
  423. }
  424.  
  425. # adjust args used in password generation
  426. proc adjust_parameters {} {
  427.     global parm_tabList
  428.     set parm_tabList {}
  429.  
  430.     toplevel [set w .parameters]
  431.  
  432. #    wm title $w ""
  433. #    wm iconname $w ""
  434.  
  435.     message $w.text -aspect 300 -text \
  436. "These parameters control generation of random passwords.
  437.  
  438. It is not necessary to move the mouse into this window to operate it.\
  439. Press <tab> to move to the next entry.\
  440. Press <return> or click the <ok> button when you are done."
  441.  
  442.     foreach desc {
  443.       {length {total length}}
  444.       {minnum {minimum number of digits}}
  445.       {minupper {minimum number of uppercase letters}}
  446.       {minlower {minimum number of lowercase letters}}} {
  447.         set name [lindex $desc 0]
  448.         set text [lindex $desc 1]
  449.         frame $w.$name -bd 1
  450.         entry $w.$name.entry -relief sunken -width 2 -textvar $name
  451.         bind $w.$name.entry <Tab> "Tab \$parm_tabList"
  452.         bind $w.$name.entry <Return> "destroy_parm_window"
  453.         label $w.$name.text -text $text
  454.         pack $w.$name.entry -side left
  455.         pack $w.$name.text -side left
  456.         lappend parm_tabList $w.$name.entry
  457.     }
  458.     frame $w.2 -bd 1
  459.     checkbutton $w.2.cb -text "alternate characters across hands" \
  460.         -relief flat -variable distribute
  461.     pack $w.2.cb -side left
  462.  
  463.     button $w.ok -text "ok" -command "destroy_parm_window"
  464.     pack $w.text -expand 1 -fill x
  465.     pack $w.length $w.minnum $w.minupper $w.minlower $w.2 -expand 1 -fill x
  466.     pack $w.ok -side left -fill x -expand 1 -padx 2 -pady 2
  467.  
  468. #strace 10
  469.     set oldfocus [focus]
  470. #    $w.length.entry icursor end
  471.     tkwait visibility $w.length.entry
  472.     focus $w.length.entry
  473. #    grab $w
  474.     tkwait window $w
  475. #    grab release $w
  476.     focus $oldfocus
  477.  
  478. #strace 0
  479.  
  480.     save_parameters    
  481. }
  482.  
  483. proc isnumber {n} {
  484.     regexp "^\[0-9\]+$" $n
  485. }
  486.  
  487. # destroy parm window IF all values are legal
  488. proc destroy_parm_window {} {
  489.     global minnum minlower minupper length
  490.  
  491.     set mustbe "must be a number greater than or equal to zero."
  492.  
  493.     # check all variables
  494.     if {![isnumber $length]} {
  495.         gen_bad_args "The total length $mustbe"
  496.         return
  497.     }
  498.     if {![isnumber $minlower]} {
  499.         gen_bad_args "The minimum number of lowercase characters $mustbe"
  500.         return
  501.     }
  502.     if {![isnumber $minupper]} {
  503.         gen_bad_args "The minimum number of uppercase characters $mustbe"
  504.         return
  505.     }
  506.     if {![isnumber $minnum]} {
  507.         gen_bad_args "The minimum number of digits $mustbe"
  508.         return
  509.     }
  510.  
  511.     # check constraints
  512.     if {$minnum + $minlower + $minupper > $length} {
  513.         gen_bad_args \
  514. "It is impossible to generate a $length-character password with\
  515. $minnum number[pluralize $minnum],\
  516. $minlower lowercase letter[pluralize $minlower], and\
  517. $minupper uppercase letter[pluralize $minupper]."
  518.         return
  519.     }
  520.  
  521.     destroy .parameters
  522. }
  523.  
  524. # return appropriate ending for a count of "n" nouns
  525. proc pluralize {n} {
  526.     expr $n!=1?"s":""
  527. }
  528.  
  529.  
  530. proc get_old_password {} {
  531.     global old
  532.  
  533.     toplevel .old
  534.     label .old.label -text "Old password:"
  535.     catch {unset old}
  536.     entry .old.entry -textvar old -relief sunken -width 1
  537.  
  538.     pack .old.label
  539.     pack .old.entry -fill x -padx 2 -pady 2
  540.  
  541.     bind .old.entry <Return> {destroy .old}
  542.     set oldfocus [focus]
  543.     focus .old.entry
  544.     tkwait visibility .old
  545.     grab .old
  546.     tkwait window .old
  547.     focus $oldfocus
  548.     return $old
  549. }
  550.  
  551. .unsorted select
  552. .passwd invoke
  553.  
  554. proc make_selection {} {
  555.     global selection_line last_line
  556.  
  557.     .names tag remove selection 0.0 end
  558.  
  559.     # don't let selection go off top of screen
  560.     if {$selection_line < 1} {
  561.         set selection_line $last_line
  562.     } elseif {$selection_line > $last_line} {
  563.         set selection_line 1
  564.     }
  565.     .names yview -pickplace [expr $selection_line-1]
  566.     .names tag add selection $selection_line.0 [expr 1+$selection_line].0
  567. }
  568.  
  569. proc select_next_nopassword {direction} {
  570.     global selection_line last_line
  571.     global nopasswords
  572.     
  573.     if 0==[llength $nopasswords] {
  574.         feedback "no null passwords"
  575.         return
  576.     }
  577.  
  578.     if $direction==1 {
  579.         # is there a better way to get last element of list?
  580.         if $selection_line>=[lindex $nopasswords [expr [llength $nopasswords]-1]] {
  581.             set selection_line 0
  582.         }
  583.         foreach i $nopasswords {
  584.             if $selection_line<$i break
  585.         }
  586.     } else {
  587.         if $selection_line<=[lindex $nopasswords 0] {
  588.             set selection_line $last_line
  589.         }
  590.         set j [expr [llength $nopasswords]-1]
  591.         for {} {$j>=0} {incr j -1} {
  592.             set i [lindex $nopasswords $j]
  593.             if $selection_line>$i break
  594.         }
  595.     }
  596.     set selection_line $i
  597.     make_selection
  598. }
  599.  
  600. proc select {w coords} {
  601.     global selection_line
  602.  
  603.     $w mark set insert "@$coords linestart"
  604.     $w mark set anchor insert
  605.     set first [$w index "anchor linestart"]
  606.     set last [$w index "insert lineend + 1c"]
  607.     scan $first %d selection_line
  608.  
  609.     $w tag remove selection 0.0 end
  610.     $w tag add selection $first $last
  611. }
  612.  
  613. bind Text <1> {select %W %x,%y}
  614. bind Text <Double-1> {select %W %x,%y}
  615. bind Text <Triple-1> {select %W %x,%y}
  616. bind Text <2> {select %W %x,%y}
  617. bind Text <3> {select %W %x,%y}
  618. bind Text <B1-Motion> {}
  619. bind Text <Shift-1> {}
  620. bind Text <Shift-B1-Motion> {}
  621. bind Text <B2-Motion> {}
  622.  
  623. bind .password <Control-n>    {incr selection_line 1;    make_selection}
  624. bind .password <Control-p>    {incr selection_line -1;make_selection}
  625. bind .password <Meta-n>    {select_next_nopassword 1}
  626. bind .password <Meta-p>    {select_next_nopassword -1}
  627. bind .password <Control-g>    {password_generate}
  628. bind .password <Control-a>    {adjust_parameters}
  629. bind Entry <Control-c>        {exit}
  630.