home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume5 / vipw < prev    next >
Internet Message Format  |  1989-02-03  |  6KB

  1. Path: xanth!nic.MR.NET!hal!ncoast!allbery
  2. From: greywolf@unisoft.UUCP (The Grey Wolf)
  3. Newsgroups: comp.sources.misc
  4. Subject: v05i017: portable (mostly, if not completely) vipw shell script.
  5. Keywords: vipw with no proprietary code.
  6. Message-ID: <1308@unisoft.UUCP>
  7. Date: 28 Oct 88 02:58:57 GMT
  8. Sender: allbery@ncoast.UUCP
  9. Reply-To: greywolf@unisoft.UUCP (The Grey Wolf)
  10. Lines: 187
  11. Approved: allbery@ncoast.UUCP
  12.  
  13. Posting-number: Volume 5, Issue 17
  14. Submitted-by: "The Grey Wolf" <greywolf@unisoft.UUCP>
  15. Archive-name: vipw
  16.  
  17. [Some comments:
  18.  
  19. (1) some shells break or slow down when the leading character on lines in
  20.     a here document is the same as the leading character of the "EOF word".
  21.     This has got that bug!  (I fixed it, just in case.)
  22.  
  23. (2) The code is in "debug mode"; rearrange the comments a bit to make a
  24.     working version.
  25.  
  26. (3) I hate to tell you, greywolf, but this thing is just as useful under
  27.     Xenix or System V; neither of which are guaranteed to mount /usr.
  28.     Stupid parochialisms are why this critter's needed in the first place!
  29.  
  30. ++bsa]
  31.  
  32. After seeing the questions on comp.unix.questions about a vipw program,
  33. I thought I would submit one.  It seems to me to be completely portable;
  34. if not, the tweaks required are likely minimal.  It includes sanity
  35. checking for a uid 0 entry (verification of shell, home directory, password);
  36. and produces appropriate messages explaining what happened.
  37. Enjoy.  Comments?  Flames?  -> ...!{sun,ucbvax,well,uunet}!unisoft!greywolf
  38. -----cut here----------cut here----------cut here----------cut here-----
  39. #!/bin/sh -
  40. # This is a shell archive, meaning:  
  41. #    1: delete everything above the #! /bin/sh - line.
  42. #    2: use sh (NOT csh) to unpack the archive.
  43. #
  44. # This file contains:
  45. #    vipw.sh (3832 bytes) 
  46. #
  47. # Packed by greywolf@unisoft.UniSoft (The Grey Wolf) 30 September 1988
  48. sed 's/^FOO_//g' << '_BAR_' > vipw.sh
  49. FOO_#! /bin/sh -
  50. FOO_# vipw: edit passwd file with locking.
  51. FOO_# unicom!greywolf
  52. FOO_
  53. FOO_# PATH and IFS are here for paranoia's sake
  54. FOO_# The path may be modified as necessary.
  55. FOO_IFS='     
  56. FOO_'
  57. FOO_PATH=/etc:/bin:/usr/bin:/usr/ucb:/usr/etc:/usr/local/bin export PATH IFS
  58. FOO_
  59. FOO_EDITOR=${EDITOR-vi}
  60. FOO_
  61. FOO_# PASSWD=/etc/passwd PTMP=/etc/ptmp
  62. FOO_PASSWD=./passwd PTMP=./ptmp
  63. FOO_
  64. FOO_# if the temp file exists, tell 'em to come back later.
  65. FOO_
  66. FOO_if [ -f $PTMP ]
  67. FOO_then    echo "vipw: temp file busy, try again later."
  68. FOO_    exit 0
  69. FOO_fi
  70. FOO_
  71. FOO_# simplify cleanup
  72. FOO_trap "/bin/rm -f $PTMP ; exit 0" 0 1 2 3 15
  73. FOO_
  74. FOO_cp $PASSWD $PTMP        # so we don't have the real thing.
  75. FOO_if chmod 600 $PTMP        # This could be something like 644...
  76. FOO_then    :
  77. FOO_else    exit $?
  78. FOO_fi
  79. FOO_
  80. FOO_# check if /usr is mounted; if we're single-user, it likely isn't, at least
  81. FOO_# on any SANE implementation of a hierarchy.  (Sun 4.0 is not a sane hier-
  82. FOO_# archy in my opinion.)
  83. FOO_
  84. FOO_if mount | grep "/usr " > /dev/null
  85. FOO_then    mount=0
  86. FOO_else    mount=1            # set a flag to umount /usr later
  87. FOO_    if mount /usr
  88. FOO_    then  :
  89. FOO_    else  echo "mounting /usr failed"
  90. FOO_          exit $?    # if it failed, we have problems -- better leave
  91. FOO_    fi
  92. FOO_fi
  93. FOO_
  94. FOO_# edit the thing.
  95. FOO_
  96. FOO_$EDITOR $PTMP
  97. FOO_
  98. FOO_# was it changed?  don't shuffle files unnecessarily.
  99. FOO_
  100. FOO_if cmp -s $PTMP $PASSWD
  101. FOO_then     echo "No changes appear to have been made."
  102. FOO_    exit 0
  103. FOO_fi
  104. FOO_
  105. FOO_#
  106. FOO_# check the super-user password entry.  There MUST be at least one user with
  107. FOO_# uid 0.  If not, then we exit.  If the password is not of the 13 char
  108. FOO_# length, then it cannot be decrypted, so we exit.  If the password has an
  109. FOO_# asterisk in it, it cannot be decrypted, so we exit.  If the ptmp file gets
  110. FOO_# deleted/trashed, we don't even bother moving it.  If root's login shell is
  111. FOO_# not executable, then we don't allow it, and we exit.  If root's passwd is
  112. FOO_# zero-length, then we issue a warning only.
  113. FOO_# Replacing root's (bad) shell with /bin/sh is tricky without another tmp
  114. FOO_# file, and that's getting sloppy.
  115. FOO_
  116. FOO_# we don't demand "root" as the first uid 0 entry.  I use "wizard" on some
  117. FOO_# machines.
  118. FOO_
  119. FOO_echo "verifying superuser password entry..."
  120. FOO_
  121. FOO_# length of a minimal superuser password entry when echoed portably is
  122. FOO_# 14 chars:
  123. FOO_# "root::0:0::/:" + the newline that echo prints.
  124. FOO_
  125. FOO_if [ `cat $PTMP | wc -c` -lt 14 ]
  126. FOO_then    echo "panic: zero-length file!
  127. FOO_passwd file unchanged."
  128. FOO_    exit 1
  129. FOO_fi
  130. FOO_
  131. FOO_# look for a uid 0 entry -- this MUST be first on the line or this will fail.
  132. FOO_if pwd=`head -1 $PTMP | egrep '^[^:]*:[^:]*:0:[0-9^:]*:[^:]*:[^:]*:.*[^:]$'`
  133. FOO_then    pwd="`echo $pwd | 
  134. FOO_          sed -e 's/::/:NULL:/g' \
  135. FOO_          -e 's/:.*\*[^:]*:/:STAR:/g' -e 's,:$,:/bin/sh,'`"
  136. FOO_else    echo "panic: basic super-user entry missing
  137. FOO_passwd file unchanged."
  138. FOO_    exit 1
  139. FOO_fi
  140. FOO_
  141. FOO_# split the root entry for easy access
  142. FOO_set `echo "$pwd" | 
  143. FOO_     awk -F: '{ printf "%s %s %d %d %s %s",$1,$2,$3,$4,$6,$7 }'`
  144. FOO_
  145. FOO_pw_name="$1" pw_passwd="$2" pw_uid="$3" pw_gid="$4"
  146. FOO_pw_dir="$5" pw_shell="$6"
  147. FOO_
  148. FOO_
  149. FOO_not_ok=0        # everything is ok until we find otherwise
  150. FOO_
  151. FOO_# The length of a password will be 13 chars; when echoed portably it will be
  152. FOO_# 14 chars due to the newline returned by echo
  153. FOO_
  154. FOO_if [ `echo "$pw_passwd" | wc -c` -lt 14 ]
  155. FOO_then    if [ "$pw_passwd" = "NULL" ]
  156. FOO_    then    echo "warning: null super-user passwd."
  157. FOO_        not_ok=1
  158. FOO_    else
  159. FOO_        echo "panic: impossible super-user password.
  160. FOO_passwd file unchanged."
  161. FOO_        exit 1
  162. FOO_    fi
  163. FOO_fi
  164. FOO_
  165. FOO_# root MUST log into / as home directory.  Why?  Enforcement of convention.
  166. FOO_# if you don't like it, change it.
  167. FOO_
  168. FOO_if [ ! "$pw_dir" = "/" ]
  169. FOO_then    echo "panic: bad super-user directory.
  170. FOO_passwd file unchanged."
  171. FOO_    exit 1
  172. FOO_fi
  173. FOO_
  174. FOO_# We have to have an existing executable shell, or else root's login/su
  175. FOO_# attempts will fail miserably.
  176. FOO_# Why doesn't test have a -x option?!?
  177. FOO_
  178. FOO_if [ ! -f "$pw_shell" ]
  179. FOO_then    echo "panic: can't find shell $pw_shell!
  180. FOO_passwd file unchanged."
  181. FOO_    exit 1
  182. FOO_fi
  183. FOO_
  184. FOO_if [ $not_ok -eq 0 ] 
  185. FOO_then    echo ok.
  186. FOO_fi
  187. FOO_
  188. FOO_# make passwd file read-only to discourage direct use of an editor on the
  189. FOO_# file.
  190. FOO_chmod 444 $PTMP
  191. FOO_mv -f $PTMP $PASSWD
  192. FOO_echo done.
  193. FOO_exit 0
  194. _BAR_
  195. if [ `cat vipw.sh` -ne 3832 ]
  196. then
  197.     echo "vipw.sh unpacked with wrong size -- should be 3832 bytes
  198. fi
  199. # end of shar file.
  200.