xref: /titanic_44/usr/src/cmd/ypcmd/ypinit.sh (revision 99ebb4ca412cb0a19d77a3899a87c055b9c30fa8)
1#!/sbin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License, Version 1.0 only
7# (the "License").  You may not use this file except in compliance
8# with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23#
24# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26
27#	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
28#	  All Rights Reserved
29
30# Portions of this source code were derived from Berkeley 4.3 BSD
31# under license from the Regents of the University of California.
32
33#ident	"%Z%%M%	%I%	%E% SMI"
34
35# set -xv
36YPXFR=/usr/lib/netsvc/yp/ypxfr
37MAKEPATH=/usr/ccs/bin
38maps="publickey publickey.byname"
39yproot_dir=/var/yp
40yproot_exe=/usr/sbin/yp
41hf=/var/run/ypservers.$$
42XFR=${YPXFR}
43
44hosts_file=/etc/hosts
45hosts6_file=/etc/inet/ipnodes
46clientp=F
47masterp=F
48slavep=F
49host=""
50def_dom=""
51master=""
52got_host_list=F
53first_time=T
54exit_on_error=F
55errors_in_setup=F
56
57enable_next_boot () {
58	/usr/sbin/svcadm disable -t $1
59	[ $? = 0 ] || echo "ypinit: unable to temporarily disable $1"
60	/usr/sbin/svccfg -s $1 \
61	    setprop general/enabled = true
62	[ $? = 0 ] || echo "ypinit: unable to enable $1 for next boot"
63}
64
65enable_this_boot () {
66	/usr/sbin/svcadm enable $1
67	[ $? = 0 ] || echo "ypinit: unable to enable $1"
68}
69
70PATH=/bin:/usr/bin:/usr/etc:/usr/sbin:$yproot_exe:$MAKEPATH:$PATH
71export PATH
72
73# To do cleanup
74trap '/usr/bin/rm -f $hf' 0 1 2 3 15
75
76case $# in
771)	case $1 in
78	-c)	clientp=T;;
79	-m)	masterp=T;;
80	*)	echo 'usage:'
81		echo '	ypinit -c'
82		echo '	ypinit -m'
83		echo '	ypinit -s master_server'
84		echo ""
85		echo "\
86where -c is used to set up a yp client, -m is used to build a master "
87                echo "\
88yp server data base, and -s is used for a slave data base."
89		echo "\
90master_server must be an existing reachable yp server."
91		exit 1;;
92	esac;;
93
942)	case $1 in
95	-s)	slavep=T; master=$2;
96		if ( grep $master $hosts_file $hosts6_file > /dev/null )
97		then
98			echo ""
99		else
100			echo "server not found in $hosts_file or $hosts6_file"
101			exit 1
102		fi;;
103
104	*)	echo 'usage:'
105		echo '	ypinit -c'
106		echo '	ypinit -m'
107		echo '	ypinit -s master_server'
108		echo ""
109		echo "\
110where -c is used to set up a yp client, -m is used to build a master "
111                echo "\
112yp server data base, and -s is used for a slave data base."
113		echo "\
114master_server must be an existing reachable yp server."
115		exit 1;;
116	esac;;
1173)	case $1 in
118	-c)	clientp=T;;
119	*)	echo 'usage:'
120		echo '	ypinit -c'
121		echo '	ypinit -m'
122		echo '	ypinit -s master_server'
123		echo ""
124		echo "\
125where -c is used to set up a yp client, -m is used to build a master "
126                echo "\
127yp server data base, and -s is used for a slave data base."
128		echo "\
129master_server must be an existing reachable yp server."
130		exit 1;;
131	esac;;
132
133*)	echo 'usage:'
134	echo '	ypinit -c'
135	echo '	ypinit -m'
136	echo '	ypinit -s master_server'
137	echo ""
138	echo "\
139where -c is used to set up a yp client, -m is used to build a master "
140	echo "\
141yp server data base, and -s is used for a slave data base."
142	echo "\
143master_server must be an existing reachable yp server."
144	exit 1;;
145esac
146
147if [ $? -ne 0 ]
148then
149	echo "\
150You have to be the superuser to run this.  Please log in as root."
151	exit 1
152fi
153
154host=`uname -n`
155
156if [ $? -ne 0 ]
157then
158	echo "Can't get local host's name.  Please check your path."
159	exit 1
160fi
161
162if [ -z "$host" ]
163then
164	echo "The local host's name hasn't been set.  Please set it."
165	exit 1
166fi
167
168def_dom=`domainname`
169
170if [ $? -ne 0 ]
171then
172	echo "Can't get local host's domain name.  Please check your path."
173	exit 1
174fi
175
176if [ -z "$def_dom" ]
177then
178	echo "The local host's domain name hasn't been set.  Please set it."
179	exit 1
180fi
181
182domainname $def_dom
183real_def_dom=$def_dom
184#def_dom=`ypalias -d $def_dom`
185ypservers_map=`ypalias ypservers`
186domain_dir="$yproot_dir""/""$def_dom"
187binding_dir="$yproot_dir""/binding/""$def_dom"
188binding_file="$yproot_dir""/binding/""$def_dom""/ypservers"
189
190if [ ! -d $yproot_dir -o -f $yproot_dir ]
191then
192    echo "\
193The directory $yproot_dir doesn't exist.  Restore it from the distribution."
194	exit 1
195fi
196
197# add domainname and ypservers aliases to aliases file
198echo ypservers $ypservers_map >> $yproot_dir/aliases
199echo $real_def_dom $def_dom >> $yproot_dir/aliases
200sort $yproot_dir/aliases | uniq > /var/run/.ypaliases; mv /var/run/.ypaliases $yproot_dir/aliases
201
202if [ ! -d "$yproot_dir"/binding ]
203then
204	mkdir "$yproot_dir"/binding
205fi
206
207if [ ! -d  $binding_dir ]
208then
209	mkdir  "$binding_dir"
210fi
211
212if [ $slavep = F ]
213then
214	while [ $got_host_list = F ]; do
215		touch $hf    # make sure file exists
216		echo ""
217		echo "\
218In order for NIS to operate sucessfully, we have to construct a list of the "
219		echo "\
220NIS servers.  Please continue to add the names for YP servers in order of"
221		echo "\
222preference, one per line.  When you are done with the list, type a <control D>"
223		echo "\
224or a return on a line by itself."
225		if [ $masterp = T ]
226		then
227			echo $host > $hf
228			echo "\tnext host to add:  $host"
229		elif [ -f $binding_file ]
230		then
231			if [ $first_time = T ]
232			then
233				for h in `cat $binding_file`
234				do
235					echo $h >> $hf
236					echo "\tnext host to add:  $h"
237				done
238			fi
239		fi
240
241		echo  "\tnext host to add:  \c"
242
243		while read h ; test -n "$h"
244		do
245			if ( grep $h $hosts_file $hosts6_file > /dev/null )
246			then
247				echo $h >> $hf
248				echo  "\tnext host to add:  \c"
249			else
250				echo "host $h not found in $hosts_file or $hosts6_file. Not added to the list"
251				echo ""
252				echo  "Do you wish to abort [y/n: y]  \c"
253				read cont_ok
254
255				case $cont_ok in
256				n*)	echo "\tnext host to add:  \c";;
257				N*)	echo "\tnext host to add:  \c";;
258				*)	exit 1;;
259				esac
260			fi
261
262		done
263
264		echo ""
265		if [ -s $hf ]
266		then
267			echo "The current list of yp servers looks like this:"
268			echo ""
269			cat $hf
270			echo ""
271			echo "Is this correct?  [y/n: y]  \c"
272		else
273			echo "You have not added any server information."
274			echo ""
275			echo "Do you still wish to exit? [y/n: y]  \c"
276		fi
277
278		read hlist_ok
279
280		case $hlist_ok in
281		n*)	got_host_list=F
282			first_time=F
283			rm $hf
284			echo "Let's try the whole thing again...";;
285		N*)	got_host_list=F
286			first_time=F
287			rm $hf
288			echo "Let's try the whole thing again...";;
289		*)	got_host_list=T;;
290		esac
291	done
292
293	if [ -s $hf ]
294	then
295		cp  $hf $binding_file
296	fi
297fi
298
299#
300# Start client service on next boot, unless we're establishing a slave
301# server, in which case the binding is needed now (or should be
302# preserved).
303#
304if [ $slavep = T ]
305then
306	enable_this_boot network/nis/client:default
307else
308	enable_next_boot network/nis/client:default
309fi
310
311#
312# As a client, our configuration is correct once a binding file is
313# established, and so we can exit (making sure we're no longer a server,
314# of course).
315#
316if [ $clientp = T ]
317then
318	rm $hf
319	/usr/sbin/svcadm disable network/nis/server:default
320	/usr/sbin/svcadm disable network/nis/xfr:default
321	/usr/sbin/svcadm disable network/nis/passwd:default
322	/usr/sbin/svcadm disable network/nis/update:default
323	exit 0
324fi
325
326if [ $slavep = T ]
327then
328	if [ $host = $master ]
329	then
330		echo "\
331The host specified should be a running master yp server, not this machine."
332		exit 1
333	fi
334
335	maps=`ypwhich -m | egrep $master$| awk '{ printf("%s ",$1) }' -`
336	if [ -z "$maps" ]
337	then
338		echo "Can't enumerate maps from $master. Please check that it is running."
339		exit 1
340	fi
341fi
342
343echo ""
344
345echo "Installing the YP database will require that you answer a few questions."
346echo "Questions will all be asked at the beginning of the procedure."
347echo ""
348echo "Do you want this procedure to quit on non-fatal errors? [y/n: n]  \c"
349read doexit
350
351case $doexit in
352y*)	exit_on_error=T;;
353Y*)	exit_on_error=T;;
354*)	echo "\
355OK, please remember to go back and redo manually whatever fails.  If you"
356	echo "\
357don't, some part of the system (perhaps the yp itself) won't work.";;
358esac
359
360echo "The yp domain directory is $yproot_dir""/""$def_dom"
361
362for dir in $yproot_dir/$def_dom
363do
364
365	if [ -d $dir ]; then
366		echo  "Can we destroy the existing $dir and its contents? [y/n: n]  \c"
367		read kill_old_dir
368
369		case $kill_old_dir in
370		y*)	rm -r -f $dir
371
372			if [ $?  -ne 0 ]
373			then
374			echo "Can't clean up old directory $dir.  Fatal error."
375				exit 1
376			fi;;
377
378		Y*)	rm -r -f $dir
379
380			if [ $?  -ne 0 ]
381			then
382			echo "Can't clean up old directory $dir.  Fatal error."
383				exit 1
384			fi;;
385
386		*)    echo "OK, please clean it up by hand and start again.  Bye"
387			exit 0;;
388		esac
389	fi
390
391	mkdir $dir
392
393	if [ $?  -ne 0 ]
394	then
395		echo "Can't make new directory $dir.  Fatal error."
396		exit 1
397	fi
398
399done
400
401if [ $slavep = T ]
402then
403	echo "\
404There will be no further questions. The remainder of the procedure should take"
405	echo "a few minutes, to copy the data bases from $master."
406
407	for dom in  $real_def_dom
408	do
409		for map in $maps
410		do
411			echo "Transferring $map..."
412			$XFR -h $master -c -d $dom $map
413
414			if [ $?  -ne 0 ]
415			then
416				errors_in_setup=T
417
418				if [ $exit_on_error = T ]
419				then
420					exit 1
421				fi
422			fi
423		done
424	done
425
426	echo ""
427	echo  "${host}'s nis data base has been set up\n"
428
429	if [ $errors_in_setup = T ]
430	then
431		echo " with errors.  Please remember"
432		echo "to figure out what went wrong, and fix it."
433	else
434		echo " without any errors."
435	fi
436
437	# enable slave services
438	enable_this_boot network/nis/server:default
439
440	enable_this_boot network/nis/client:default
441
442	exit 0
443else
444
445	rm -f $yproot_dir/*.time
446
447	echo "\
448There will be no further questions. The remainder of the procedure should take"
449	echo "5 to 10 minutes."
450
451	echo "Building $yproot_dir/$def_dom/ypservers..."
452	makedbm $hf $yproot_dir/$def_dom/$ypservers_map
453
454	if [ $?  -ne 0 ]
455	then
456		echo "\
457Couldn't build yp data base $yproot_dir/$def_dom/$ypservers_map."
458		errors_in_setup=T
459
460		if [ $exit_on_error = T ]
461		then
462			exit 1
463		fi
464	fi
465
466	rm $hf
467
468	in_pwd=`pwd`
469	cd $yproot_dir
470	echo  "Running \c"
471	echo  $yproot_dir "\c"
472	echo "/Makefile..."
473	make NOPUSH=1
474
475	if [ $?  -ne 0 ]
476	then
477		echo "\
478Error running Makefile."
479		errors_in_setup=T
480
481		if [ $exit_on_error = T ]
482		then
483			exit 1
484		fi
485	fi
486
487	cd $in_pwd
488	echo ""
489	echo  "\
490$host has been set up as a yp master server\c"
491
492	if [ $errors_in_setup = T ]
493	then
494		echo " with errors.  Please remember"
495		echo "to figure out what went wrong, and fix it."
496	else
497		echo " without any errors."
498	fi
499
500	echo ""
501	echo "\
502If there are running slave yp servers, run yppush now for any data bases"
503	echo "\
504which have been changed.  If there are no running slaves, run ypinit on"
505	echo "\
506those hosts which are to be slave servers."
507
508	# enable master services
509	enable_this_boot network/nis/server:default
510	enable_this_boot network/nis/xfr:default
511	enable_this_boot network/nis/passwd:default
512	enable_this_boot network/nis/update:default
513
514	enable_this_boot network/nis/client:default
515fi
516