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