You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

passwd-expect 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #
  2. # This scripts changes a password on the local system or a remote host.
  3. # Connections to the remote (this can also be localhost) are made by ssh, rsh,
  4. # telnet or rlogin.
  5. # @author Gaudenz Steinlin <gaudenz@soziologie.ch>
  6. # For sudo support alter sudoers (using visudo) so that it contains the
  7. # following information (replace 'apache' if your webserver runs under another
  8. # user):
  9. # -----
  10. # # Needed for Horde's passwd module
  11. # Runas_Alias REGULARUSERS = ALL, !root
  12. # apache ALL=(REGULARUSERS) NOPASSWD:/usr/bin/passwd
  13. # -----
  14. # @stdin The username, oldpassword, newpassword (in this order)
  15. # will be taken from stdin
  16. # @param -prompt regexp for the shell prompt
  17. # @param -password regexp password prompt
  18. # @param -oldpassword regexp for the old password
  19. # @param -newpassword regexp for the new password
  20. # @param -verify regexp for verifying the password
  21. # @param -success regexp for success changing the password
  22. # @param -login regexp for the telnet prompt for the loginname
  23. # @param -host hostname to be connected
  24. # @param -timeout timeout for each step
  25. # @param -log file for writing error messages
  26. # @param -output file for loging the output
  27. # @param -telnet use telnet
  28. # @param -ssh use ssh (default)
  29. # @param -rlogin use rlogin
  30. # @param -slogin use slogin
  31. # @param -sudo use sudo
  32. # @param -program command for changing passwords
  33. #
  34. # @return 0 on success, 1 on failure
  35. #
  36. # default values
  37. set host "localhost"
  38. set login "ssh"
  39. set program "passwd"
  40. set prompt_string "(%|\\\$|>)"
  41. set fingerprint_string "The authenticity of host.* can't be established.*\nRSA key fingerprint is.*\nAre you sure you want to continue connecting.*"
  42. set password_string "(P|p)assword.*"
  43. set oldpassword_string "((O|o)ld|login|\\\(current\\\) UNIX) (P|p)assword.*"
  44. set newpassword_string "(N|n)ew.* (P|p)assword.*"
  45. set badoldpassword_string "(Authentication token manipulation error).*"
  46. set badpassword_string "((passwd|BAD PASSWORD).*|(passwd|Bad:).*\r)"
  47. set verify_string "((R|r)e-*enter.*(P|p)assword|Retype new( UNIX)? password|(V|v)erification|(V|v)erify|(A|a)gain).*"
  48. set success_string "((P|p)assword.* changed|successfully)"
  49. set login_string "(((L|l)ogin|(U|u)sername).*)"
  50. set timeout 20
  51. set log "/tmp/passwd.out"
  52. set output false
  53. set output_file "/tmp/passwd.log"
  54. # read input from stdin
  55. fconfigure stdin -blocking 1
  56. gets stdin user
  57. gets stdin password(old)
  58. gets stdin password(new)
  59. # alternative: read input from command line
  60. #if {$argc < 3} {
  61. # send_user "Too few arguments: Usage $argv0 username oldpass newpass"
  62. # exit 1
  63. #}
  64. #set user [lindex $argv 0]
  65. #set password(old) [lindex $argv 1]
  66. #set password(new) [lindex $argv 2]
  67. # no output to the user
  68. log_user 0
  69. # read in other options
  70. for {set i 0} {$i<$argc} {incr i} {
  71. set arg [lindex $argv $i]
  72. switch -- $arg "-prompt" {
  73. incr i
  74. set prompt_string [lindex $argv $i]
  75. continue
  76. } "-password" {
  77. incr i
  78. set password_string [lindex $argv $i]
  79. continue
  80. } "-oldpassword" {
  81. incr i
  82. set oldpassword_string [lindex $argv $i]
  83. continue
  84. } "-newpassword" {
  85. incr i
  86. set newpassword_string [lindex $argv $i]
  87. continue
  88. } "-verify" {
  89. incr i
  90. set verify_string [lindex $argv $i]
  91. continue
  92. } "-success" {
  93. incr i
  94. set success_string [lindex $argv $i]
  95. continue
  96. } "-login" {
  97. incr i
  98. set login_string [lindex $argv $i]
  99. continue
  100. } "-host" {
  101. incr i
  102. set host [lindex $argv $i]
  103. continue
  104. } "-timeout" {
  105. incr i
  106. set timeout [lindex $argv $i]
  107. continue
  108. } "-log" {
  109. incr i
  110. set log [lindex $argv $i]
  111. continue
  112. } "-output" {
  113. incr i
  114. set output_file [lindex $argv $i]
  115. set output true
  116. continue
  117. } "-telnet" {
  118. set login "telnet"
  119. continue
  120. } "-ssh" {
  121. set login "ssh"
  122. continue
  123. } "-ssh-exec" {
  124. set login "ssh-exec"
  125. continue
  126. } "-rlogin" {
  127. set login "rlogin"
  128. continue
  129. } "-slogin" {
  130. set login "slogin"
  131. continue
  132. } "-sudo" {
  133. set login "sudo"
  134. continue
  135. } "-program" {
  136. incr i
  137. set program [lindex $argv $i]
  138. continue
  139. }
  140. }
  141. # log session
  142. if {$output} {
  143. log_file $output_file
  144. }
  145. set err [open $log "w" "0600"]
  146. # start remote session
  147. if {[string match $login "rlogin"]} {
  148. set pid [spawn rlogin $host -l $user]
  149. } elseif {[string match $login "slogin"]} {
  150. set pid [spawn slogin $host -l $user]
  151. } elseif {[string match $login "ssh"]} {
  152. set pid [spawn ssh $host -l $user]
  153. } elseif {[string match $login "ssh-exec"]} {
  154. set pid [spawn ssh $host -l $user $program]
  155. } elseif {[string match $login "sudo"]} {
  156. set pid [spawn sudo -u $user $program]
  157. } elseif {[string match $login "telnet"]} {
  158. set pid [spawn telnet $host]
  159. expect -re $login_string {
  160. sleep .5
  161. send "$user\r"
  162. }
  163. } else {
  164. puts $err "Invalid login mode. Valid modes: rlogin, slogin, ssh, telnet, sudo\n"
  165. close $err
  166. exit 1
  167. }
  168. set old_password_notentered true
  169. if {![string match $login "sudo"]} {
  170. # log in
  171. expect {
  172. -re $fingerprint_string {sleep .5
  173. send yes\r
  174. exp_continue}
  175. -re $password_string {sleep .5
  176. send $password(old)\r}
  177. timeout {puts $err "Could not login to system (no password prompt)\n"
  178. close $err
  179. exit 1}
  180. }
  181. # start password changing program
  182. expect {
  183. -re $prompt_string {sleep .5
  184. send $program\r}
  185. # The following is for when passwd is the login shell or ssh-exec is used
  186. -re $oldpassword_string {sleep .5
  187. send $password(old)\r
  188. set old_password_notentered false}
  189. timeout {puts $err "Could not login to system (bad old password?)\n"
  190. close $err
  191. exit 1}
  192. }
  193. }
  194. # send old password
  195. if {$old_password_notentered} {
  196. expect {
  197. -re $oldpassword_string {sleep .5
  198. send $password(old)\r}
  199. timeout {puts $err "Could not start passwd program (no old password prompt)\n"
  200. close $err
  201. exit 1}
  202. }
  203. }
  204. # send new password
  205. expect {
  206. -re $newpassword_string {sleep .5
  207. send $password(new)\r}
  208. -re $badoldpassword_string {puts $err "Old password is incorrect\n"
  209. close $err
  210. exit 1}
  211. timeout {puts "Could not change password (bad old password?)\n"
  212. close $err
  213. exit 1}
  214. }
  215. # send new password again
  216. expect {
  217. -re $badpassword_string {puts $err "$expect_out(0,string)"
  218. close $err
  219. send \003
  220. sleep .5
  221. exit 1}
  222. -re $verify_string {sleep .5
  223. send $password(new)\r}
  224. timeout {puts $err "New password not valid (too short, bad password, too similar, ...)\n"
  225. close $err
  226. send \003
  227. sleep .5
  228. exit 1}
  229. }
  230. # check response
  231. expect {
  232. -re $success_string {sleep .5
  233. send exit\r}
  234. -re $badpassword_string {puts $err "$expect_out(0,string)"
  235. close $err
  236. exit 1}
  237. timeout {puts $err "Could not change password.\n"
  238. close $err
  239. exit 1}
  240. }
  241. # exit succsessfully
  242. expect {
  243. eof {close $err
  244. exit 0}
  245. }
  246. close $err