| 1 | #!/bin/bash |
| 2 | # setnew-password.sh: For demonstration purposes only. |
| 3 | # Not a good idea to actually run this script. |
| 4 | # This script must be run as root. |
| 5 | |
| 6 | ROOT_UID=0 # Root has $UID 0. |
| 7 | E_WRONG_USER=65 # Not root? |
| 8 | |
| 9 | E_NOSUCHUSER=70 |
| 10 | SUCCESS=0 |
| 11 | |
| 12 | |
| 13 | if [ "$UID" -ne "$ROOT_UID" ] |
| 14 | then |
| 15 | echo; echo "Only root can run this script."; echo |
| 16 | exit $E_WRONG_USER |
| 17 | else |
| 18 | echo |
| 19 | echo "You should know better than to run this script, root." |
| 20 | echo "Even root users get the blues... " |
| 21 | echo |
| 22 | fi |
| 23 | |
| 24 | |
| 25 | username=bozo |
| 26 | NEWPASSWORD=security_violation |
| 27 | |
| 28 | # Check if bozo lives here. |
| 29 | grep -q "$username" /etc/passwd |
| 30 | if [ $? -ne $SUCCESS ] |
| 31 | then |
| 32 | echo "User $username does not exist." |
| 33 | echo "No password changed." |
| 34 | exit $E_NOSUCHUSER |
| 35 | fi |
| 36 | |
| 37 | echo "$NEWPASSWORD" | passwd --stdin "$username" |
| 38 | # The '--stdin' option to 'passwd' permits |
| 39 | #+ getting a new password from stdin (or a pipe). |
| 40 | |
| 41 | echo; echo "User $username's password changed!" |
| 42 | |
| 43 | # Using the 'passwd' command in a script is dangerous. |
| 44 | |
| 45 | exit 0 |