home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Utilities / defaultuser / defaultuser.sh < prev   
Encoding:
Linux/UNIX/POSIX Shell Script  |  1997-12-14  |  5.6 KB  |  240 lines

  1. #!/bin/sh
  2.  
  3. PATH=/usr/local/gnu/bin:/usr/local/bin:/bin:/usr/ucb:/usr/bin:/usr/etc:/usr:/etc
  4.  
  5. #####################################################################
  6. # Date:     13 Sept 1997
  7. # Author:     TjL <luomat+next@luomat.peak.org>
  8. # Version:     2.0 (previously released as `fiddlepass'
  9. #
  10. #####################################################################
  11. #
  12. #
  13. # Warning: do NOT make root the DefaultUser... it just 
  14. # isn't a good idea
  15. #
  16. #
  17. #####################################################################
  18. #
  19. #  This script is to remove and restore the password for the user
  20. #  known as the ``DefaultUser'' 
  21. #
  22. #  This supercedes the script I formerly wrote called `fiddlepass' 
  23. #  to do roughly the same thing, thou not nearly as well.
  24. #
  25. #  Thanks to Luke Howard <lukeh@xedoc.com.au> and 
  26. #            Christian Limpach <chris@nice.ch> 
  27. #  but they are not responsible for this script....
  28. #
  29. #  ... in fact, neither am I... Use entirely at your own risk
  30. #
  31. #
  32. #  USAGE: pretty simple.  It takes one of two arguments:
  33. #
  34. #    Use ``removepass'' to remove the password.
  35. #     It will be stored (encrypted) in root's dwrites.
  36. #
  37. #     Use  ``restorepass'' to restore the password
  38. #  How to use:
  39. #  I'd try putting something like this in /etc/rc.local:
  40. #
  41. #  defaultuser.sh removepass
  42. #  (sleep 60 && defaultuser.sh restorepass) &
  43. #
  44. #  That will remove the password, wait 60 seconds, and then
  45. #  restore the password.... that should be plenty of time
  46. #  to complete the boot action and have the default user
  47. #  get logged in.... you might want to make it less time
  48. #  since this account will be open during that interval...
  49. #  
  50. #
  51. #
  52. ####################################################################
  53. # setting VERBOSE=yes gets some extra info during processing
  54. VERBOSE=yes
  55.  
  56. name=`basename $0`
  57.  
  58.  
  59.  
  60. ####################################################################
  61. if [ "`/usr/ucb/whoami`" != "root" ]
  62. then
  63.     echo "Must be root to run this program"
  64.     exit 1
  65. fi
  66. ####################################################################
  67.  
  68. NIUTIL=/usr/bin/niutil
  69.  
  70. if [ ! -x $NIUTIL ]; then
  71.     echo "Oops... can't find $NIUTIL!"
  72.     exit 1
  73. fi
  74.  
  75. ####################################################################
  76.  
  77.  
  78.  
  79.  
  80. ###################################################################
  81. # get the username of the `DefaultUser' from root's dwrite
  82. # database
  83. USER=`/usr/bin/dread loginwindow DefaultUser |awk '{print $NF}'`
  84.  
  85. #########################################
  86. # if there is no DefaultUser, exit
  87. if [ "$USER" = "" ]
  88. then
  89.     echo "No default user found, try 'dwrite loginwindow DefaultUser username' to set a Default User"
  90.     exit 1
  91. fi
  92.  
  93.  
  94.  
  95. if [ "$VERBOSE" = "yes" ]; then
  96.     # OK, tell them who the user is
  97.     echo "DefaultUser is ${USER}"
  98. fi    
  99.  
  100. ###################################################################
  101. # set the NetInfo Directory
  102. NIDIR=/users/${USER}
  103.  
  104. if [ "$VERBOSE" = "yes" ]; then
  105.     # OK, tell them who the user is
  106.     echo "NIDIR=/users/${USER}"
  107. fi    
  108.  
  109.  
  110. ####################################################################
  111.  
  112. case $1 in
  113.  
  114.     removepass)
  115.  
  116.     # we try to get the current password from the 
  117.     # NetInfo database
  118.     PASSWD=`$NIUTIL -readprop . $NIDIR passwd`
  119.  
  120.     # if that exited NOT zero, something went wrong
  121.     if [ "$?" != "0" ]
  122.     then
  123.         echo "FATAL: Can't find local password for user $USER"
  124.         exit 1
  125.     fi
  126.     
  127.     if [ "$VERBOSE" = "yes" ]; then
  128.         echo "Good: found a local passwd for $USER"
  129.     fi
  130.  
  131.     #########################################
  132.     # store the password (encrypted) in
  133.     # root's dwrite database
  134.     /usr/bin/dwrite loginwindow DefaultUserPassword $PASSWD
  135.  
  136.     #########################################
  137.     # then remove the password
  138.     $NIUTIL -destroyprop . $NIDIR passwd 
  139.  
  140.  
  141.     ;;
  142.     #
  143.     # The end of the `removepass' code
  144.     #
  145.  
  146.  
  147.  
  148.     restorepass)
  149.  
  150.  
  151.     if [ "$VERBOSE" = "yes" ]; then
  152.         echo "Trying to reestablish password for $USER"
  153.     fi
  154.     
  155.     PASS=`/usr/bin/dread loginwindow DefaultUserPassword |\
  156.     awk '{print $NF}'`
  157.     
  158.     ########################################################
  159.     if [ "$PASS" = "" ]
  160.     then
  161.  
  162.         if [ "$VERBOSE" = "yes" ]; then
  163.             echo "Drat.... can't find old password for $USER... trying to grab root's"
  164.         fi
  165.  
  166.  
  167.         # eek, no password found... let's use
  168.         # root's password so we don't have an account 
  169.         # with no password at all
  170.         
  171.         PASS=`$NIUTIL -readprop . /users/root passwd`
  172.  
  173.         ###########################################
  174.         if [ "$PASS" = "" ]
  175.         then
  176.             if [ "$VERBOSE" = "yes" ]; then
  177.                 echo "Very bad... no root password "
  178.             fi
  179.  
  180.  
  181.  
  182.             # goodness, there's no root password
  183.             # this is bad news
  184.             
  185.             echo "Please check the local domain on `hostname` since there does not seem to be a password for $USER or root... logins are disabled for $USER" | /usr/ucb/Mail -s "($name) WARNING: " root $USER
  186.             
  187.             
  188.             # well, since we can't find a password
  189.             # we'll use "*" to disable logins
  190.             # to we'll be secure at least
  191.             PASS="*"
  192.             
  193.  
  194.         #########################################
  195.         else
  196.  
  197.         if [ "$VERBOSE" = "yes" ]; then
  198.             echo "well, at least we can use root's password rather than $USER's"
  199.         fi
  200.  
  201.             echo "Could not find a password for $USER, so I made it the same as root's from the local domain on machine `hostname`" | /usr/ucb/Mail -s "($name) WARNING: " root $USER
  202.         
  203.                                     
  204.         fi        
  205.         #########################################
  206.  
  207.     fi
  208. ###################################################################
  209.     
  210.     # now we recreate the password
  211.     $NIUTIL -createprop . $NIDIR passwd $PASS
  212.     
  213.     if [ "$?" != "0" ]
  214.     then
  215.         echo "Something went wrong trying to restore password for $USER"
  216.         exit 1
  217.     fi    
  218.  
  219.  
  220.     ;; 
  221.     # 
  222.     # the end of the ``restorepass' code
  223.     #    
  224.     
  225.     
  226.     *) echo "Use either:"
  227.         echo "    $name restorepass"
  228.         echo "or"
  229.         echo "    $name removepass"
  230.     
  231.     ;;
  232.     
  233.     
  234. esac    
  235. ###################################################################
  236. exit 0
  237. ###################################################################
  238.