home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume05 / vipw < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  6.4 KB

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