Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / adm / bin / nisclient-9.x
1 #!/bin/bash
2 ###############################################################################
3 # This script sets a machine up to be an NIS client.
4 # Kevin Lister 10.28.98
5
6 ###############################################################################
7 # Variables
8 logfile=/nisclient.log
9 me=${0##*/}
10
11 admin=cll-support@cup.hp.com
12 nisdomain=cll
13 domainname=
14 netnfsrc=/etc/netnfsrc
15 nsswitch=/etc/nsswitch.conf
16 pwfile=/etc/passwd
17 lclpwfile=/etc/passwd.loc
18 nispwfile=/etc/passwd.nis
19 crontab=/usr/spool/cron/crontabs/root
20 newcrontab=/tmp/root-crontab
21 null=/dev/null
22 ypdir=/usr/etc/yp
23
24 # Functions
25 function error {
26   print -u2 "$me: Error: $1"
27   /usr/bin/mailx -s "$(uname -n): NIS setup error: $1" $admin < $null
28   exit 255;
29 } # error
30
31 function warning {
32   print -u2 "$me: Warning: $1"
33 } # warning
34
35 function display {
36   print "$1"
37 } # display
38
39 function info {
40   display "$me: Info: $1"
41 } # info
42
43 ##
44 ########## Main
45 ##
46
47 ##
48 ########## Check the status of the system before proceeding
49 ##
50
51 # Must be root to run this
52 if [ $(id -u) -ne 0 ]; then
53         error "Must be root to execute this command... Exiting."
54 fi
55
56 # Check to see if this script has already been ran
57 if [ -a $logfile ]; then
58         error "$me has already been ran on $(uname -n)... Exiting"
59 fi
60
61 # Check to see if domainname has been set
62 domainname=`/bin/domainname`
63 if [ "_$domainname" != "_" ]; then
64         error "NIS Domain name is already set to -> $domainname... Exiting."
65 fi
66
67 # Are we already running NIS?
68 /usr/bin/ypwhich >> $logfile 2>&1
69 if [ $? -eq 0 ]; then
70         error "This system is already running NIS... Exiting."
71 fi
72
73 # Check to see if there is a name service switch config file
74 if [ -a $nsswitch ]; then
75         error "This system has a name service switch config file...
76 Exiting."
77 fi
78
79 # Check to see if /etc/yp exists
80 if [ ! -a $ypdir ]; then
81         error "Directory /etc/yp does not exist... Exiting"
82 fi
83
84 ##
85 ########## System checks out. Set up the files and start NIS.
86 ##
87
88 # Set the NIS domain name - This is probably not needed
89 /bin/domainname $nisdomain >> $logfile 2>&1
90 if [ $? -ne 0 ]; then
91         error "Could not set NIS domain name... Exiting."
92 fi
93
94 # Setup the /etc/netnfsrc file
95 mv $netnfsrc $netnfsrc.preNIS
96 cp $netnfsrc.preNIS $netnfsrc
97 sed -e "s/^NIS_CLIENT=0/NIS_CLIENT=1/"  \
98     -e "s/^NISDOMAIN=/NISDOMAIN=cll/" \
99     < $netnfsrc > $netnfsrc-tmp
100 mv $netnfsrc-tmp $netnfsrc
101 chmod 544 $netnfsrc
102 chown bin:bin $netnfsrc
103
104 # Email us if there are local passwd entries other than root
105 if [ $( /bin/wc -l $lclpwfile | /usr/bin/cut -d " " -f 1) != "1"  ]; then
106         /usr/bin/mailx -s "$(uname -n): passwd.loc has more than 1 line"
107 $admin < $lclpwfile
108 fi
109
110 # Create NIS passwd file
111 if [ -a $lclpwfile ]; then
112         /bin/grep "^root" $lclpwfile > $nispwfile
113 else
114         /bin/grep "^root" $pwfile > $nispwfile
115 fi
116
117 /bin/cat >> $nispwfile <<:END
118 adm:*:4:4:,_MoA_:/usr/adm:/bin/sh
119 anon:*:21:5:placeholder for future,_MoA_:/:/bin/sync
120 bin:*:2:2:,_MoA_:/bin:/bin/sh
121 daemon:*:1:5:,_MoA_:/:/bin/sh
122 ftp:*:24:5:Anonymous &,,,_LcL_:/home/ftp:/bin/sync
123 ftp:*:500:10:anonymous ftp,_MoA_:/home/ftp:/bin/false
124 lp:*:9:7:[Line Printer],,,,_MoA_:/usr/spool/lp:/bin/sh
125 nuucp:*:6:1:0000-uucp(0000),_MoA_:/usr/spool/uucppublic:/usr/lib/uucp/uucico
126 sync:*:20:1:,_MoA_:/:/bin/sync
127 tftp:*:510:1:Trivial FTP user,_MoA_:/usr/tftpdir:/bin/false
128 uucp:*:5:3:,_MoA_:/usr/spool/uucppublic:/usr/lib/uucp/uucico
129 who:*:90:1:,_MoA_:/:/bin/who
130 :END
131
132 /bin/grep -v "^root" $lclpwfile >> $nispwfile
133
134 /bin/cat >> $nispwfile <<:END2
135 -@dangerous-users
136 +@sysadmin
137 +@site-ux
138 +:
139 :END2
140
141 mv $pwfile $pwfile.preNIS
142 mv $nispwfile $pwfile
143 chmod 444 $pwfile
144 chown root:other $pwfile
145
146 # Remove MoA from the root crontab
147 sed -e "/MoA/d" -e "/mkpass/d" < $crontab > $newcrontab
148 mv $crontab $crontab-preNIS
149 /usr/bin/crontab $newcrontab
150 chmod 444 $crontab
151 chown root:other $crontab
152
153 # Start the NIS Client daemons
154 /etc/ypbind -ypset >> $logfile 2>&1
155 sleep 10
156 /usr/bin/ypwhich >> $logfile 2>&1
157 if [ $? -ne 0 ]; then
158         error "Problem starting the NIS client daemons... Exiting."
159 fi
160
161 # Create /net symlink if automounter is NOT running
162 /bin/ps -ef | /bin/grep automount | /bin/grep -v grep >> $logfile 2>&1
163 if [ $? -ne 0 ]; then
164         /bin/ln -s /nfs /net >> $logfile 2>&1
165 fi
166
167 # Create symlinks to shells so new passwd file doesn't blow up
168 /bin/ln -s /bin/sh /usr/bin/sh
169 /bin/ln -s /bin/csh /usr/bin/csh
170 /bin/ln -s /bin/ksh /usr/bin/ksh
171
172 # Create symlinks to ypdir, not required, just friendlier
173 /bin/ln -s /usr/etc/yp /var/yp
174
175 # Email us that a machine was updated
176 /usr/bin/mailx -s "$(uname -n): NIS setup complete" $admin < $null