Merge commit 'a058d1cc571af5fbcfe7f1d719df1abbfdb722f3' into merges
[unleashed.git] / usr / src / cmd / ypcmd / ypinit.sh
blob5a4793178ab59d16c586e5c9cd8b601d4715a79a
1 #!/sbin/sh
3 # CDDL HEADER START
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.
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.
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]
20 # CDDL HEADER END
22 # Copyright 2014 Nexenta Systems, Inc. All rights reserved.
24 # Copyright 2007 Sun Microsystems, Inc. All rights reserved.
25 # Use is subject to license terms.
27 # Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
28 # All Rights Reserved
30 # Portions of this source code were derived from Berkeley 4.3 BSD
31 # under license from the Regents of the University of California.
33 # set -xv
34 YPXFR=/usr/lib/netsvc/yp/ypxfr
35 MAKEPATH=/usr/bin
36 maps="publickey publickey.byname"
37 yproot_dir=/var/yp
38 yproot_exe=/usr/sbin/yp
39 hf=/var/run/ypservers.$$
40 XFR=${YPXFR}
42 hosts_file=/etc/hosts
43 hosts6_file=/etc/inet/ipnodes
44 clientp=F
45 masterp=F
46 slavep=F
47 host=""
48 def_dom=""
49 master=""
50 got_host_list=F
51 first_time=T
52 non_interactive=F
53 exit_on_error=F
54 errors_in_setup=F
56 enable_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"
64 enable_this_boot () {
65 /usr/sbin/svcadm enable $1
66 [ $? = 0 ] || echo "ypinit: unable to enable $1"
69 is_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}'`"
81 PATH=/bin:/usr/bin:/usr/etc:/usr/sbin:$yproot_exe:$MAKEPATH:$PATH
82 export PATH
84 # To do cleanup
85 trap '/usr/bin/rm -f $hf' 0 1 2 3 15
87 # Check out total number of arguments
88 case $# in
89 1) case $1 in
90 -c) clientp=T;;
91 -m) masterp=T;;
92 *) echo 'usage:'
93 echo ' ypinit -c [server_name...]'
94 echo ' ypinit -m'
95 echo ' ypinit -s master_server'
96 echo ""
97 echo "\
98 where -c is used to set up a yp client, -m is used to build a master "
99 echo "\
100 yp server data base, and -s is used for a slave data base."
101 echo "\
102 master_server must be an existing reachable yp server."
103 exit 1;;
104 esac;;
106 2) case $1 in
107 -s) slavep=T; master=$2;
108 if ( grep $master $hosts_file $hosts6_file > /dev/null )
109 then
110 echo ""
111 else
112 echo "server not found in $hosts_file or $hosts6_file"
113 exit 1
114 fi;;
116 # the case with more than one argument with the '-c' option
117 # is a subject to enter non-interactive mode
118 -c) clientp=T; non_interactive=T;
120 *) echo 'usage:'
121 echo ' ypinit -c [server_name...]'
122 echo ' ypinit -m'
123 echo ' ypinit -s master_server'
124 echo ""
125 echo "\
126 where -c is used to set up a yp client, -m is used to build a master "
127 echo "\
128 yp server data base, and -s is used for a slave data base."
129 echo "\
130 master_server must be an existing reachable yp server."
131 exit 1;;
132 esac;;
133 *) case $1 in
135 # the case with more than one argument with the '-c' option
136 # is a subject to enter non-interactive mode
137 -c) clientp=T; non_interactive=T;
139 *) echo 'usage:'
140 echo ' ypinit -c [server_name...]'
141 echo ' ypinit -m'
142 echo ' ypinit -s master_server'
143 echo ""
144 echo "\
145 where -c is used to set up a yp client, -m is used to build a master "
146 echo "\
147 yp server data base, and -s is used for a slave data base."
148 echo "\
149 master_server must be an existing reachable yp server."
150 exit 1;;
151 esac;;
152 esac
154 if [ $? -ne 0 ]
155 then
156 echo "\
157 \aYou have to be the superuser to run this. Please log in as root."
158 exit 1
161 host=`uname -n`
163 if [ $? -ne 0 ]
164 then
165 echo "\aCan't get local host's name. Please check your path."
166 exit 1
169 if [ -z "$host" ]
170 then
171 echo "\aThe local host's name hasn't been set. Please set it."
172 exit 1
175 def_dom=`domainname`
177 if [ $? -ne 0 ]
178 then
179 echo "\aCan't get local host's domain name. Please check your path."
180 exit 1
183 if [ -z "$def_dom" ]
184 then
185 echo "\aThe local host's domain name hasn't been set. Please set it."
186 exit 1
189 domainname $def_dom
190 real_def_dom=$def_dom
191 #def_dom=`ypalias -d $def_dom`
192 ypservers_map=`ypalias ypservers`
193 domain_dir="$yproot_dir""/""$def_dom"
194 binding_dir="$yproot_dir""/binding/""$def_dom"
195 binding_file="$yproot_dir""/binding/""$def_dom""/ypservers"
197 if [ ! -d $yproot_dir -o -f $yproot_dir ]
198 then
199 echo "\
200 \aThe directory $yproot_dir doesn't exist. Restore it from the distribution."
201 exit 1
204 # add domainname and ypservers aliases to aliases file
205 echo ypservers $ypservers_map >> $yproot_dir/aliases
206 echo $real_def_dom $def_dom >> $yproot_dir/aliases
207 sort $yproot_dir/aliases | uniq > /var/run/.ypaliases; mv /var/run/.ypaliases $yproot_dir/aliases
209 if [ ! -d "$yproot_dir"/binding ]
210 then
211 mkdir "$yproot_dir"/binding
214 if [ ! -d $binding_dir ]
215 then
216 mkdir "$binding_dir"
219 if [ $slavep = F ]
220 then
221 if [ $non_interactive = F ]
222 then
223 while [ $got_host_list = F ]; do
224 touch $hf # make sure file exists
225 echo ""
226 echo "\
227 In order for NIS to operate sucessfully, we have to construct a list of the "
228 echo "\
229 NIS servers. Please continue to add the names for YP servers in order of"
230 echo "\
231 preference, one per line. When you are done with the list, type a <control D>"
232 echo "\
233 or a return on a line by itself."
234 if [ $masterp = T ]
235 then
236 echo $host > $hf
237 echo "\tnext host to add: $host"
238 elif [ -f $binding_file ]
239 then
240 if [ $first_time = T ]
241 then
242 for h in `cat $binding_file`
244 echo $h >> $hf
245 echo "\tnext host to add: $h"
246 done
250 echo "\tnext host to add: \c"
252 while read h ; test -n "$h"
255 # Host should be in the v4 or v6 hosts file or
256 # reasonably resemble an IP address. We'll do a
257 # sanity check that a v4 addr is one word consisting
258 # of only numbers and the "." character,
259 # which should guard against fully qualified
260 # hostnames and most malformed entries. IPv6
261 # addresses can be numbers, hex letters, and have
262 # at least one ":" character and possibly one or
263 # more "." characters for embedded v4 addresses
265 if ( grep $h $hosts_file $hosts6_file > /dev/null ) || \
266 ( test $clientp = T && `is_valid_ipaddr $h` )
267 then
268 echo $h >> $hf
269 echo "\tnext host to add: \c"
270 else
271 echo "host $h not found in $hosts_file or" \
272 "$hosts6_file.\nNot added to the list."
273 echo ""
274 echo "Do you wish to abort [y/n: y] \c"
275 read cont_ok
277 case $cont_ok in
278 n*) echo "\tnext host to add: \c";;
279 N*) echo "\tnext host to add: \c";;
280 *) exit 1;;
281 esac
283 done
285 echo ""
286 if [ -s $hf ]
287 then
288 echo "The current list of yp servers looks like this:"
289 echo ""
290 cat $hf
291 echo ""
292 echo "Is this correct? [y/n: y] \c"
293 else
294 echo "You have not added any server information."
295 echo ""
296 echo "Do you still wish to exit? [y/n: y] \c"
299 read hlist_ok
301 case $hlist_ok in
302 n*) got_host_list=F
303 first_time=F
304 rm $hf
305 echo "Let's try the whole thing again...";;
306 N*) got_host_list=F
307 first_time=F
308 rm $hf
309 echo "Let's try the whole thing again...";;
310 *) got_host_list=T;;
311 esac
312 done
313 else
314 shift
315 > $hf
316 while [[ $# > 0 ]]; do
317 if ( grep $1 $hosts_file $hosts6_file > /dev/null ) || \
318 ( `is_valid_ipaddr $1` )
319 then
320 echo $1 >> $hf
321 else
322 echo "host $1 not found in $hosts_file or" \
323 "$hosts6_file.\nNot added to the list."
324 echo ""
326 shift
327 done
330 if [ -s $hf ]
331 then
332 cp $hf $binding_file
337 # Start client service on next boot, unless we're establishing a slave
338 # server, in which case the binding is needed now (or should be
339 # preserved).
341 if [ $slavep = T ]
342 then
343 enable_this_boot network/nis/client:default
344 else
345 enable_next_boot network/nis/client:default
349 # As a client, our configuration is correct once a binding file is
350 # established, and so we can exit (making sure we're no longer a server,
351 # of course).
353 if [ $clientp = T ]
354 then
355 rm $hf
356 /usr/sbin/svcadm disable network/nis/server:default
357 /usr/sbin/svcadm disable network/nis/xfr:default
358 /usr/sbin/svcadm disable network/nis/passwd:default
359 /usr/sbin/svcadm disable network/nis/update:default
360 exit 0
363 if [ $slavep = T ]
364 then
365 if [ $host = $master ]
366 then
367 echo "\
368 \aThe host specified should be a running master yp server, not this machine."
369 exit 1
372 maps=`ypwhich -m | egrep $master$| awk '{ printf("%s ",$1) }' -`
373 if [ -z "$maps" ]
374 then
375 echo "\aCan't enumerate maps from $master. Please check that it is running."
376 exit 1
380 echo ""
382 echo "Installing the YP database will require that you answer a few questions."
383 echo "Questions will all be asked at the beginning of the procedure."
384 echo ""
385 echo "Do you want this procedure to quit on non-fatal errors? [y/n: n] \c"
386 read doexit
388 case $doexit in
389 y*) exit_on_error=T;;
390 Y*) exit_on_error=T;;
391 *) echo "\
392 OK, please remember to go back and redo manually whatever fails. If you"
393 echo "\
394 don't, some part of the system (perhaps the yp itself) won't work.";;
395 esac
397 echo "The yp domain directory is $yproot_dir""/""$def_dom"
399 for dir in $yproot_dir/$def_dom
402 if [ -d $dir ]; then
403 echo "Can we destroy the existing $dir and its contents? [y/n: n] \c"
404 read kill_old_dir
406 case $kill_old_dir in
407 y*) rm -r -f $dir
409 if [ $? -ne 0 ]
410 then
411 echo "\aCan't clean up old directory $dir. Fatal error."
412 exit 1
413 fi;;
415 Y*) rm -r -f $dir
417 if [ $? -ne 0 ]
418 then
419 echo "\aCan't clean up old directory $dir. Fatal error."
420 exit 1
421 fi;;
423 *) echo "OK, please clean it up by hand and start again. Bye"
424 exit 0;;
425 esac
428 mkdir $dir
430 if [ $? -ne 0 ]
431 then
432 echo "\aCan't make new directory $dir. Fatal error."
433 exit 1
436 done
438 if [ $slavep = T ]
439 then
440 echo "\
441 There will be no further questions. The remainder of the procedure should take"
442 echo "a few minutes, to copy the data bases from $master."
444 for dom in $real_def_dom
446 for map in $maps
448 echo "Transferring $map..."
449 $XFR -h $master -c -d $dom $map
451 if [ $? -ne 0 ]
452 then
453 errors_in_setup=T
455 if [ $exit_on_error = T ]
456 then
457 exit 1
460 done
461 done
463 echo ""
464 echo "${host}'s nis data base has been set up\n"
466 if [ $errors_in_setup = T ]
467 then
468 echo " with errors. Please remember"
469 echo "to figure out what went wrong, and fix it."
470 else
471 echo " without any errors."
474 # enable slave services
475 enable_this_boot network/nis/server:default
477 enable_this_boot network/nis/client:default
479 exit 0
480 else
482 rm -f $yproot_dir/*.time
484 echo "\
485 There will be no further questions. The remainder of the procedure should take"
486 echo "5 to 10 minutes."
488 echo "Building $yproot_dir/$def_dom/ypservers..."
489 makedbm $hf $yproot_dir/$def_dom/$ypservers_map
491 if [ $? -ne 0 ]
492 then
493 echo "\
494 \aCouldn't build yp data base $yproot_dir/$def_dom/$ypservers_map."
495 errors_in_setup=T
497 if [ $exit_on_error = T ]
498 then
499 exit 1
503 rm $hf
505 in_pwd=`pwd`
506 cd $yproot_dir
507 echo "Running \c"
508 echo $yproot_dir "\c"
509 echo "/Makefile..."
510 make NOPUSH=1
512 if [ $? -ne 0 ]
513 then
514 echo "\
515 \aError running Makefile."
516 errors_in_setup=T
518 if [ $exit_on_error = T ]
519 then
520 exit 1
524 cd $in_pwd
525 echo ""
526 echo "\
527 $host has been set up as a yp master server\c"
529 if [ $errors_in_setup = T ]
530 then
531 echo " with errors. Please remember"
532 echo "to figure out what went wrong, and fix it."
533 else
534 echo " without any errors."
537 echo ""
538 echo "\
539 If there are running slave yp servers, run yppush now for any data bases"
540 echo "\
541 which have been changed. If there are no running slaves, run ypinit on"
542 echo "\
543 those hosts which are to be slave servers."
545 # enable master services
546 enable_this_boot network/nis/server:default
547 enable_this_boot network/nis/xfr:default
548 enable_this_boot network/nis/passwd:default
549 enable_this_boot network/nis/update:default
551 enable_this_boot network/nis/client:default