Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / adm / bin / configure_machine
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         configure_machine
5 # Description:  A script to set up the "admin" environment
6 # Author:       Andrew@DeFaria.com
7 # Created:      Tue Apr 15 14:20:02 PDT 1997
8 # Modified:
9 # Language:     Korn Shell
10 #
11 # (c) Copyright 2001, Andrew@DeFaria.com, all rights reserved
12 #
13 ################################################################################
14 me=$(basename $0)
15
16 # Set adm_base
17 adm_base=${adm_base:-//sonscentral/Corporate/Software/adm}
18
19 if [ ! -d /adm ]; then
20   if [ ! -d "$adm_base" ]; then
21     print -u2 "$me: Error: Unable to find \"adm\" path! - Exiting"
22     exit 1
23   fi
24 fi
25
26 # Set adm_fpath
27 adm_fpath=${adm_fpath:-$adm_base/functions}
28
29 # Source functions
30 . $adm_fpath/common
31
32 # Source in tmpfiles function
33 tmpprefix=${TMPDIR:-/tmp}/configure_machine.$$
34 . $adm_fpath/tmpfiles
35 arm_trap
36
37 verbose=
38 debug=
39 mode=check
40
41 function usage {
42   info "$ME [-v|verbose] [-d|debug] [-usage]"
43   info "        -v|verbose:     Turns on verbose mode"
44   info "        -d|debug:       Turns on debug mode"
45   info "        -f|ix:          Turns on fix mode"
46   info "        -c|heck:        Turns on check mode (default)"
47   info "        -usage:         Print this usage message\n"
48
49   error "$1" 1
50 } # usage
51
52 function symlink {
53   debug "ENTER symlink"
54   from="$1"
55   to="$2"
56
57   if [ ! -h "$from" ]; then
58     if [ $mode = "fix" ]; then
59       verbose "Setting up symlink from $from -> $to"
60       ln -s "$to" "$from"
61     else
62       warning "$from link is not setup properly" 0
63     fi
64   else
65     verbose "$from link is OK"
66   fi
67
68   debug "EXIT symlink"
69 } # symlink
70
71 function setup_symlinks {
72   debug "ENTER setup_symlinks"
73
74   symlinks="\
75 /adm            $adm_base\
76 "
77   print $symlinks | while read from to; do
78     symlink $from $to
79   done
80
81   debug "EXIT setup_symlinks"
82 } # setup_symlinks
83
84 function check_and_replace_file {
85   debug "ENTER check_and_replace_file"
86   master_file="$1"
87   check_file="$2"
88   permissions="$3"
89   owner_group="$4"
90
91   if ! cmp -s $master_file $check_file; then
92     if [ $mode = "fix" ]; then
93       verbose "Fixing $check_file"
94       cp $master_file $check_file
95       chmod $permissions $check_file
96       chown $owner_group $check_file
97     else
98       warning "$check_file is not setup properly" 0
99     fi
100   else
101     verbose "$check_file is OK"
102   fi
103
104   debug "EXIT check_and_replace_file"
105 } # check_and_replace_file
106
107 function check_nsswitch_conf {
108   debug "ENTER check_nsswitch_conf"
109
110   sed 's/automount:     files nis/automount:    nis files/' \
111     /etc/nsswitch.conf > $tmpprefix.nsswitch.conf
112
113   check_and_replace_file $tmpprefix.nsswitch.conf /etc/nsswitch.conf 444
114 root:adm
115
116   debug "EXIT check_nsswitch_conf"
117 } # check_nsswitch_conf
118
119 function check_automount_maps {
120   debug "ENTER check_automount_maps"
121
122   for map in master home direct indirect; do
123     check_and_replace_file /adm/etc/auto_$map /etc/auto_$map 444 root:adm
124   done
125
126   debug "EXIT check_automount_maps"
127 } # check_automount_maps
128
129 function setup_rcfiles {
130   debug "ENTER setup_rcfiles"
131   local_src=$ADM_PATH/etc/init.d/local
132   local_dest=/etc/init.d/local
133   local_symlink=/etc/rc3.d/S99local
134
135   if [ ! -x $local_dest ]; then
136     if [ $mode = "fix" ]; then
137       verbose "Creating $local_dest"
138       cp "$local_src" $local_dest
139       chown root:adm $local_dest
140       chmod 555 $local_dest
141     else
142       warning "$local_dest does not exist!" 0
143     fi
144   else
145     verbose "$local_dest is OK"
146   fi
147
148   if [ ! -h $local_symlink ]; then
149     if [ $mode = "fix" ]; then
150       verbose "Setting up $local_symlink"
151       ln -s $local_dest $local_symlink
152     else
153       warning "$local_symlink does not exist!" 0
154     fi
155   else
156     verbose "$local_symlink is OK"
157   fi
158
159   if [ ! -d /etc/Startup ]; then
160     if [ $mode = "fix" ]; then
161       verbose "Creating /etc/Startup directory"
162       mkdir /etc/Startup
163       chown root:adm /etc/Startup
164       # Note: Security would be better if this was 775...
165       chmod 777 /etc/Startup
166     else
167       warning "/etc/Startup does not exist!" 0
168     fi
169   else
170     verbose "/etc/Startup is OK"
171   fi
172
173   start_views_src=$ADM_PATH/clearcase/start_views
174   start_views_dest=/etc/Startup/start_views
175
176   if [ ! -x $start_views_dest ]; then
177     if [ $mode = "fix" ]; then
178       verbose "Creating $start_views_dest"
179       cp "$start_views_src" $start_views_dest
180       chown root:adm $start_views_dest
181       chmod 555 $start_views_dest
182     else
183       warning "$start_views_dest does not exist!" 0
184     fi
185   else
186     verbose "$start_views_dest is OK"
187   fi
188
189   views_to_start_src=$ADM_PATH/clearcase/views_to_start
190   views_to_start_dest=/etc/views_to_start
191
192   if [ ! -f $views_to_start_dest ]; then
193     if [ $mode = "fix" ]; then
194       verbose "Creating $views_to_start_dest"
195       cp "$views_to_start_src" $views_to_start_dest
196       chown root:adm $views_to_start_dest
197       chmod 555 $views_to_start_dest
198     else
199       warning "$views_to_start_dest does not exist!" 0
200     fi
201   else
202     verbose "$views_to_start_dest is OK"
203   fi
204
205   debug "EXIT setup_rcfiles"
206 } # setup_rcfiles
207
208 function setup_root_rhosts {
209   debug "ENTER setup_root_rhosts"
210   root_rhosts_src="$ADM_PATH/etc/root_rhosts"
211   root_rhosts_dest="/.rhosts"
212
213   if [ ! -f $root_rhosts_dest ]; then
214     if [ $mode = "fix" ]; then
215       verbose "Creating $root_rhosts_dest"
216       cp "$root_rhosts_src" $root_rhosts_dest
217       chown root:adm $root_rhosts_dest
218       chmod 400 $root_rhosts_dest
219     else
220       warning "$root_rhosts_dest does not exist!" 0
221     fi
222   else
223     if is_root; then
224       if ! cmp -s $root_rhosts_src $root_rhosts_dest; then
225         if [ $mode = "fix" ]; then
226           verbose "Updating $root_rhosts_dest"
227           cp "$root_rhosts_src" $root_rhosts_dest
228           chown root:adm $root_rhosts_dest
229           chmod 400 $root_rhosts_dest
230         else
231           warning "Contents of $root_rhosts_dest is non standard!" 0
232         fi
233       fi
234     else
235       verbose "$root_rhosts_dest is present"
236       verbose "Contents not check since you're not running as root"
237     fi
238   fi
239
240   debug "EXIT setup_root_rhosts"
241 } # setup_root_rhosts
242
243 function setup_root_profile {
244   debug "ENTER setup_root_profile"
245   if [ "$VENDOR" = "HP" ]; then
246     debug "RETURN setup_root_profile (Skip HP machines)"
247     return
248   fi
249   root_profile_src="$ADM_PATH/etc/root_profile"
250   root_profile_dest="/.profile"
251
252   if [ ! -f $root_profile_dest ]; then
253     if [ $mode = "fix" ]; then
254       verbose "Creating $root_profile_dest"
255       cp "$root_profile_src" $root_profile_dest
256       chown root:adm $root_profile_dest
257       chmod 444 $root_profile_dest
258     else
259       warning "$root_profile_dest does not exist!" 0
260     fi
261   else
262     if is_root; then
263       if ! cmp -s $root_profile_src $root_profile_dest; then
264         if [ $mode = "fix" ]; then
265           verbose "Updating $root_profile_dest"
266           cp "$root_profile_src" $root_profile_dest
267           chown root:adm $root_profile_dest
268           chmod 400 $root_profile_dest
269         else
270           warning "Contents of $root_profile_dest is non standard!" 0
271         fi
272       fi
273     else
274       verbose "$root_profile_dest is present"
275       verbose "Contents not check since you're not running as root"
276     fi
277   fi
278
279   debug "EXIT setup_root_profile"
280 } # setup_root_rhosts
281
282 # Get parameters
283 while [ $# -ge 1 ]; do
284   case "$1" in
285     -usage)
286       usage
287     ;;
288
289     -v|-verbose)
290       verbose=yes
291     ;;
292
293     -d|-debug)
294       debug=yes
295     ;;
296
297     -f|-fix)
298       mode=fix
299     ;;
300
301     -c|-check)
302       mode=check
303     ;;
304
305     *)
306       usage "Unrecognized parameter $1"
307     ;;
308   esac
309   shift
310 done
311
312 if [ "$mode" = "fix" ]; then
313   if ! is_root; then
314     error "Must be root to execute this command in fix mode!" 2
315   fi
316 fi
317
318 verbose "Starting $me..."
319 setup_symlinks
320 check_nsswitch_conf
321 check_automount_maps
322 setup_rcfiles
323 setup_root_rhosts
324 setup_root_profile
325 verbose "$me completed"