Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / adm / bin / restrict_passwd
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         restrict_passwd
5 # Description:  This script will convert /etc/passwd to a "restricted" passwd 
6 #               file. There is a list of "special" users such as system users 
7 #               like anon, ftp, etc as well as some administrative engineers. 
8 #               These users have their passwd entries passed through 
9 #               unmodified. All other users get either a restricted shell 
10 #               (sorry just rksh) and are homed to /home/vumover (this was 
11 #               done for the purposes of migrating views to the new view 
12 #               servers and may be changed at some time in the future). 
13 #
14 #               If -mailmode is specified then the only change to the original
15 #               passwd line is that shell is set to /bin/false. This is to
16 #               prevent user logins to the mail server.
17 #
18 #               Note that all su users are conciously not written to the new 
19 #               passwd file unless they appear in the special users list. 
20 #
21 #               Finally, users from MLL are skipped.
22 #                       
23 # Author:       Andrew DeFaria, California Language Labs
24 # Created:      Fri Oct 17 09:06:46 PDT 1997
25 # Modified:     
26 # Language:     Korn Shell
27 #
28 # (c) Copyright 2001, Andrew@DeFaria.com, all rights reserved
29 #
30 ################################################################################
31 me=$(basename $0)
32
33 function error {
34   print -u2 "$me: Error: $1"
35 } # error
36
37 function info {
38   print -u2 "$1"
39 }
40
41 function verbose {
42   if [ ! -z "$VERBOSE" ]; then
43     info "$1"
44   fi
45 } # verbose
46
47 function debug {
48   if [ ! -z "$DEBUG" ]; then
49     info "$1"
50   fi
51 } # debug
52
53 function usage {
54   info "$me [-v] [-d] [-m] [-o file] [-u]"
55   info "        -v:             Turns on verbose mode"
56   info "        -d:             Turns on debug mode"
57   info "        -m:             Generate passwd file for mail server"
58   info "        -o file:        Specify file to place output into"
59   info "        -u:             Print this usage message"
60   info ""
61   info "$me reads password entries from stdin and writes a restricted version"
62   info "of the password entry to stdout. Some users are special and are"
63   info "unaltered. Root (su) users are skipped and not written out."
64   exit
65 } # usage
66
67 ## Main body
68
69 # Get parameters
70 while getopts ":dmo:vu" options; do
71   case $options in
72     u)
73       usage
74       ;;
75
76     v)
77       VERBOSE=yes
78       ;;    
79
80     d)
81       DEBUG=yes
82       ;;    
83
84     m)
85       mailmode=yes
86       ;;
87
88     o)
89       outfile=$OPTARG
90       ;;
91
92     *)
93       usage
94       ;;
95   esac
96 done
97 shift $(($OPTIND - 1))
98
99 IFS=: 
100
101 while read user pass uid gid geos home shell; do
102   case "$user" in 
103 anon|\
104 dts|\
105 bin|\
106 daemon|\
107 ftp|\
108 gridmgr0|\
109 gridmgr1|\
110 jun|\
111 junsu|\
112 lp|\
113 stevew|\
114 stevewsu|\
115 sync|\
116 tftp|\
117 uucp|\
118 vobadm|\
119 root)
120       verbose "****> User $user is \"special\""
121       print "$user:$pass:$uid:$gid:$geos:$home:$shell"
122       ;;
123
124     *)
125       # In mail mode change shell to /bin/false. Otherwise use a restricted
126       # shell and also set the home directory to /home/vumover.
127       if [ ! -z "$mailmode" ]; then
128         shell=/bin/false
129       else
130         shell=/usr/bin/rksh
131         home=/home/vumover
132       fi
133
134       # Change MoA marking to Restricted
135       geos="$(print "$geos" | sed 's/_MoA_/_Restricted_/')"
136
137       # Allow no other uid 0 users other than those listed above
138       if [ $uid -eq 0 ]; then
139         verbose "****> User $user is in uid 0 - skipping..."
140         continue
141       fi
142
143       # Skip users from MLL
144       print $home | grep "\.ch\.apollo" > /dev/null 2>&1 
145       if [ $? -eq 0 ]; then
146         verbose "****> User $user is from Apollo - skipping..."
147         continue
148       fi
149
150       print "$user:$pass:$uid:$gid:$geos:$home:$shell"
151       ;;
152   esac
153 done < /etc/passwd > /tmp/passwd.$$
154
155 if [ "_$outfile" = "_" ]; then
156   mv /etc/passwd /etc/passwd.old
157   mv /tmp/passwd.$$ /etc/passwd
158   chmod 444 /etc/passwd 
159 else
160   mv /tmp/passwd.$$ $outfile
161 fi