c2b484cf7c87bd3ff554aef88a10bfdcdf39e5cc
[clearscm.git] / clients / HP / bin / mkpass.prenis
1 #!/bin/ksh
2 ################################################################################
3 #
4 # File:         mkpass
5 # Description:  Mother of All (MoA) passwd administration script
6 # Author:       Cory Chan (cory@cup.hp.com)
7 # Language:     Korn Shell
8 # Modified:     11/18/1994 Ryan Fong (fong@cup.hp.com) Modified for 10.0
9 #               07/26/1995 Andrew DeFaria (defaria@cup.hp.com) Revamped to use
10 #               NFS mount point to avoid rcp. Script now works for both 9.0
11 #               and 10.0.
12 #               08/21/1995 Andrew DeFaria (defaria@cup.hp.com) Revamped mail
13 #               message sending.
14 #               03/25/98 Michael Coulter (coulter) Changed "ch.apollo" to
15 #               "che.hp.com" because of a domain name change for Chelmsford.
16 #
17 # (c) Copyright 2001, Andrew@DeFaria.com, all rights reserved
18 #
19 ################################################################################
20 # First source the appserver script
21 if [ -x /app/appserver ]; then
22   . /app/appserver
23 fi
24
25 # Set ADMIN_ROOT
26 ADMIN_ROOT=/app/admin
27
28 # Check for force flag
29 FORCE="False"
30
31 if [ "$1" = "-f" ]; then
32   FORCE="True"
33 fi
34
35 # Whom to notify of problems. 
36 NOTIFY=root@$(hostname)
37
38 # Determine OS level
39 OS=`uname -r | cut -c 3-4`
40
41 MASTER_PASSWD=$ADMIN_ROOT/lib/master_passwd
42 MASTER_PASSWD_MLL=$ADMIN_ROOT/lib/master_passwd.mll
43 LOCAL_PASSWD=/etc/passwd.loc
44 EXCLUDED_PASSWD=/etc/passwd.exc
45 MARKER_FILE=/etc/pass.time
46 PASSWD_OLD=/etc/passwd.old
47 PASSWD=/etc/passwd
48 TMP_PASSWD=/tmp/passwd.$$
49 TMP_PASSWD2=/tmp/passwd2.$$
50
51 # Log and save old messages if there were any problems
52 MESSAGE_FILE=$ADMIN_ROOT/log/mkpass.$(uname -n)
53
54 # Set file attribute
55 umask 022                               
56 # Check for existance of $MASTER_PASSWD file. If missing send message and
57 # abort.
58 if [[ ! -f $MASTER_PASSWD ]]; then
59   mailx -s "mkpass: $MASTER_PASSWD file is missing!" $NOTIFY < /dev/null
60   exit 1
61 fi
62
63 # Check existence of necessary files; make when necessary.  
64 if [[ ! -f $MARKER_FILE ]]; then
65   # make time marker if not exists
66   touch $MARKER_FILE
67 fi
68
69 if [[ ! -f $EXCLUDED_PASSWD ]]; then
70   echo "# one login per line, no space/tab/null line#" > $EXCLUDED_PASSWD
71 fi
72
73 if [[ ! -f $PASSWD_OLD ]]; then
74   # make old passwd file if not exists
75   cp $PASSWD $PASSWD_OLD                
76 fi
77
78 if [[ ! -f $LOCAL_PASSWD ]]; then               
79   # no local file, notify and exit
80   cat > $MESSAGE_FILE <<!EOM
81 There was no $LOCAL_PASSWD file found on `uname -n`. This file must exist
82 and have root as its first entry. Please correct the problem.
83
84 For more information see:
85
86 http://cllweb/productivity/SysAdmin/Passwords.html#root
87
88 !EOM
89   mailx -s "mkpass: $LOCAL_PASSWD missing!" $NOTIFY < $MESSAGE_FILE
90   exit 0
91 fi
92
93 if [ $FORCE = "False" ]; then
94   # Check if passwd was changed directly since last check, if so mail the
95   # differences to $NOTIFY
96   if [[ $PASSWD -nt $MARKER_FILE ]] ; then
97     cat > $MESSAGE_FILE <<!EOM
98 $PASSWD on `uname -n` changed without using merge script! This is not the 
99 proper way to update passwords. For help regarding what you should do see:
100
101 http://cllweb/productivity/SysAdmin/Passwords.html#email
102
103 Here are the differences between the files:
104
105 (< = /etc/passwd > = /etc/passwd.old)
106 -----------------------------------------------------------------------------
107 !EOM
108     diff $PASSWD $PASSWD_OLD >> $MESSAGE_FILE
109     mailx -s "mkpass: $PASSWD incorrectly changed" $NOTIFY < $MESSAGE_FILE
110     exit 0
111   fi
112 fi
113
114 # Check to see if $LOCAL_PASSWD, $MASTER_PASSWD or $EXCLUDED_PASSWD is newer
115 # than $PASSWD. If so, combine $LOCAL_PASSWD and $MASTER_PASSWD (excluding 
116 # entries from $EXCLUDED_PASSWD) to form new $PASSWD
117 if [[ $FORCE             =   "True"  ||
118       $LOCAL_PASSWD      -nt $PASSWD || 
119       $MASTER_PASSWD     -nt $PASSWD || 
120       $MASTER_PASSWD_MLL -nt $PASSWD || 
121       $EXCLUDED_PASSWD   -nt $PASSWD ]]; then
122
123   # If only the $MASTER_PASSWD changed then make a note not to send email
124   if [[ $LOCAL_PASSWD    -nt $PASSWD || 
125         $EXCLUDED_PASSWD -nt $PASSWD ]]; then
126     NOTIFY_OF_CHANGE=True
127   else
128     NOTIFY_OF_CHANGE=False
129   fi
130
131   # Save an old copy around
132   cp $PASSWD $PASSWD_OLD
133
134   # Check root entry in $LOCAL_PASSWD
135   if grep -v "^#" $LOCAL_PASSWD | head -n 1 | grep "^root:" > /dev/null; then
136     # 1st entry root OKAY in $LOCAL_PASSWD
137     :
138   else
139     # 1st entry NOT root in passwd.loc
140     cat > $MESSAGE_FILE <<!EOM
141 The first entry of $LOCAL_PASSWD on `uname -n` should be for root. 
142 Please correct this problem. 
143
144 For more information see:
145
146 http://cllweb/productivity/SysaAdmin/Passwords.html#root
147
148 !EOM
149     mailx -s "mkpass: Missing first root in $LOCAL_PASSWD" $NOTIFY < $MESSAGE_FILE
150     rm -f $TMP_PASSWD
151     exit 0
152   fi
153
154   # Make new $PASSWD
155   rm -f $TMP_PASSWD2
156
157   # MLL gives us a passwd file that does not qualify the machine name portion
158   # of the home directory. This code fixes that up and also adds "_MoA" to the
159   # geos field.
160   #
161   # Some engineers also use a local copy of tcsh, therefore we must also check
162   # shell and add on .che.hp.com to the shell path.
163   IFS=:
164   while read user pass uid gid geos home shell; do
165     first_component_home=$(print $home | cut -f2 -d/)
166     machine_component_home=$(print $home | cut -f3 -d/)
167     rest_home=$(print $home | cut -f4- -d/)
168     home=/$first_component_home/$machine_component_home.che.hp.com/$rest_home
169     first_component_shell=$(print $shell | cut -f2 -d/)
170     machine_component_shell=$(print $shell | cut -f3 -d/)
171     rest_shell=$(print $shell | cut -f4- -d/)
172     if [ $first_component_shell = "net" ]; then
173       shell=\
174 /$first_component_shell/$machine_component_shell.che.hp.com/$rest_shell
175     fi
176     print "$user:$pass:$uid:$gid:$geos,_MoA_:$home:$shell" >> $TMP_PASSWD2
177   done < $MASTER_PASSWD_MLL
178
179   cat $LOCAL_PASSWD $MASTER_PASSWD $TMP_PASSWD2 > $TMP_PASSWD
180
181   # Do exclusion
182   grep -v "^#" $EXCLUDED_PASSWD |\
183    grep -vf $EXCLUDED_PASSWD $TMP_PASSWD > $TMP_PASSWD2
184
185   # Transform password file to 10.0 format
186   if [ $OS = "10" ]; then
187     sed -e 's/:\/nfs/:\/net/' -e 's/:\/bin/:\/usr\/bin/' \
188      $TMP_PASSWD2 > $TMP_PASSWD
189     rm -f $TMP_PASSWD2
190   else
191     mv $TMP_PASSWD2 $TMP_PASSWD
192   fi
193
194   if [ -s $TMP_PASSWD ]; then
195     mv $TMP_PASSWD $PASSWD
196     chmod 444 $PASSWD
197   else
198     rm -f $TMP_PASSWD
199     mailx -s "mkpass: Error: Zero length passwd file resulted!" $NOTIFY <<!EOM
200 For some reason mkpass resulted in a zero length passwd file. Please
201 investigate this.
202 !EOM
203   fi
204  
205   if [[ $NOTIFY_OF_CHANGE = "True" ]]; then
206     # Notify... (This could be improved by implementing a logging facility
207     # instead of tons of email).
208     cat > $MESSAGE_FILE <<!EOM
209 MoA has updated the $PASSWD file on `uname -n`. Here were the differences
210 before the update occured. Unless you feel that these updates were made
211 incorrectly you can probably safely ignore this message.
212
213 (< = /etc/passwd > = /etc/passwd.old)
214 -----------------------------------------------------------------------------
215 !EOM
216     diff $PASSWD $PASSWD_OLD >> $MESSAGE_FILE
217     mailx -s "mkpass: Made new $PASSWD" $NOTIFY < $MESSAGE_FILE
218   fi
219 fi
220
221 # Update marker file
222 touch -ma $MARKER_FILE
223
224 # Update log file
225 echo "$PASSWD on `uname -n` is up to date as of `date`" > $MESSAGE_FILE
226
227 exit 0