#!/bin/sh # # Copyright (c) AT&T 1998. All rights reserved. # @(#)setdomainname.sh 1.9 3/20/98 # # # These variables may be used immediately # FATAL_EXIT_CODE=1 textdomaindir=/opt/lanman/lib/locale textdomain='setdomainname' # # Function to run gettext with TEXTDOMAINDIR set. # TEXTDOMAINDIR is not set in the exported environment so subprocesses # do not inadvertently use it. # pgettext () { gttext=gettext #Trick to fool shgettext TEXTDOMAINDIR=${textdomaindir} ${gttext} ${textdomain} "$1" } # Used in messages Progname=`basename $0` # The product name COMMENT="SunLink Server" # initialze the common shell variables LMPATHS="`/bin/dirname $0`/../lib/lmpaths" if [ -f ${LMPATHS} ] then . ${LMPATHS} else . lmpaths fi # # if not root then exit # id | grep root > $DEV_NULL_PATH 2>&1 if [ $? != 0 ] then eval echo "\"`pgettext '\nYou must be root to run ${Progname}.\n'`\"" exit ${FATAL_EXIT_CODE} fi #---------------------------------------------------------------------------- # Routines #---------------------------------------------------------------------------- # # Ask the user a question, return true(YES) if yes, false(NO) if no # NO=255 YES=0 NO_DEFAULT=2 yorn () { def=$1 shift while [ 1 ] do if [ $def = $NO ] then echo "$* [y/n]? n\b\c" elif [ $def = $YES ] then echo "$* [y/n]? y\b\c" else echo "$* [y/n]? \c" fi case `line` in y|Y|[yY][eE][sS]) return $YES ;; n|N|[nN][oO]) return $NO ;; '') if [ $def != $NO_DEFAULT ] then return $def fi pgettext "\tPlease respond with y(es) or n(o)\n" ;; *) pgettext "\tPlease respond with y(es) or n(o)\n" ;; esac done } #---------------------------------------------------------------------------- # # Usage - display more info for bdc than pdc # Globals: references Progname # usage () { eval echo "\"`pgettext 'Usage: ${Progname} [-n NewDomainName] [-s] \nwhen -s is specified the server is not automatically started'`\"" exit 1 } #---------------------------------------------------------------------------- # # Check for a legal name. # Return 0 if ok, 1 otherwise. # This routine takes one argument, the name to validate. # check_name () { name=$1 if [ "$name" = "" ] then pgettext "You must enter a domain name.\n" return 1 fi # if can't be uname if [ "$name" = "`uname -n`" ] then pgettext "The domain name cannot be the system name.\n" pgettext "You must select a unique name.\n" return 1 fi # Domain name can't be server name # test must be case insensitive. use sort since it honors locale locale="`$SRVCONFIG_PATH -g lmxserver,lang`" count=`echo "$name\n$Servername" | LC_CTYPE="$locale" sort -fu | wc -l` if [ "$count" -eq 1 ] then pgettext "The domain name cannot be the Server name.\n" pgettext "You must select a unique name.\n" return 1 fi # check name length namelen=`echo $name | wc -c` namelen=`expr $namelen - 1` if [ $namelen -gt 15 ] then pgettext "Domain names must be 15 characters or less.\n" return 1 fi # check for valid character set len=`expr "$name" : "[$VALIDCH]*"` if [ "$len" -ne "$namelen" ] then pgettext "The domain name contains invalid characters.\n" eval echo "\"`pgettext 'The valid characters are: ${VALIDCH}'`\"" return 1 fi # is the name the current name? if [ "$name" = "$Current_domainname" ] then pgettext "The new name is the same as the current domain name.\n" return 1 fi return 0 } #---------------------------------------------------------------------------- # # Check the command line options. # Return 0 if no options (interactive mode), or 1 for non-interactive. # If invalid options or arguments, call usage( ). # # This routine takes the command line arguments specified on the invocation of # the shell script. All args have values and are of the form arg:val # Usage: check_commandline arg1 arg2 arg3 [arg4] # # Globals: modifies NewDomain # check_commandline () { # are there any commandline arguments? if [ "$1" = "" ] then return 0 else while getopts 'n:s?' c do case $c in n) NewDomain=$OPTARG;; s) Autostart_flag=no;; \?) usage;; esac done check_name $NewDomain if [ $? -eq 1 ] then usage fi return 1 fi } #---------------------------------------------------------------------------- # # Initialize variables, perform startup checks. # Sets ulimit, Servername, Listenname, Current_domainname, Role. # Checks if server is running and sets Serverup. # initialize () { # set global variables # Valid characters for server name VALIDCH="--.a-zA-Z0-9~!#$%^&()_{}" # Owner and Group for datafiles OWNER=lanman GROUP=DOS---- # The variables needed to set the name NewDomain="" Listenname=`$SRVCONFIG_PATH -g "server,listenname"` if [ "$Listenname" = "" ] then # no entry was entered, name must be uname.serve ListenExtension=`$SRVCONFIG_PATH -g "lmxserver,listenextension" | \ tr '[A-Z]' '[a-z]'` if [ -z "$ListenExtension" ] then ListenExtension=".serve" fi len=`echo $ListenExtension | wc -c` len=`expr 16 - $len` Servername="`uname -n | cut -c1-${len}`${ListenExtension}" else Servername=$Listenname fi # The current domain name Current_domainname=`$SRVCONFIG_PATH -g "workstation,domain"` # Role is used to issue role-specific messages, etc. Role=`${GETROLE_PATH} -r | tr -d "\012"` # The accounts database may be VERY large; # Never run out of ulimit space ulimit 1000000 # is server running? ps -e | grep $LMXCTRL_NAME > $DEV_NULL_PATH 2>&1 if [ $? = 0 ] then Serverup=yes else Serverup=no fi # This flag set to no if command line arg (-s) is specified. # The server will be started if the flag is yes in non-interactive mode. Autostart_flag=yes return } #---------------------------------------------------------------------------- savedatabase () { # save a copy of the SAM database rm -rf $SAMDIR_PATH/.tmp > $DEV_NULL_PATH 2>&1 mkdir $SAMDIR_PATH/.tmp > $DEV_NULL_PATH 2>&1 cp $SAMDIR_PATH/$Current_domainname $SAMDIR_PATH/.tmp> $DEV_NULL_PATH 2>&1 cp $LSA_PATH $LSA_PATH.t > $DEV_NULL_PATH 2>&1 } #---------------------------------------------------------------------------- restoredatabase () { cp -r $SAMDIR_PATH/.tmp/* $SAMDIR_PATH > $DEV_NULL_PATH 2>&1 rm -f $SAMDIR_PATH/.tmp/* > $DEV_NULL_PATH 2>&1 rmdir $SAMDIR_PATH/.tmp > $DEV_NULL_PATH 2>&1 rm -f $SAMDIR_PATH/$NewDomain > $DEV_NULL_PATH 2>&1 mv $LSA_PATH.t $LSA_PATH > $DEV_NULL_PATH 2>&1 # restore group/owner chgrp $GROUP $SAMDIR_PATH/* $LSA_PATH/* > $DEV_NULL_PATH 2>&1 chown $OWNER $SAMDIR_PATH/* $LSA_PATH/* > $DEV_NULL_PATH 2>&1 } #---------------------------------------------------------------------------- # # Get the new domain name. # Globals: referenced: Servername, Role, Current_domainname # modified: NewDomain # getdomain_name () { eval echo "\"`pgettext 'This server, ${Servername}, is a ${Role} domain controller in the \ndomain ${Current_domainname}.'`\"" eval yorn $YES "\"`pgettext 'Would you like to change the domain name now'`\"" if [ $? = $NO ] then eval echo "\"`pgettext 'This server's domain ${Current_domainname} will not be changed.'`\"" restorestate fi newdomain="" while [ 1 ] do newdomain="" pgettext "Enter the new domain name (15 characters or less): " read newdomain # Make sure domain name is OK check_name "$newdomain" if [ $? -eq 1 ] then continue fi break done NewDomain=$newdomain return } #---------------------------------------------------------------------------- # # Restore Original_servername, replace accounts database file, # start the server if it had been running. # Exits with FATAL_EXIT_CODE # restorestate() { trap 2 3 15 pgettext "Restoring server to its previous state ... \n" stty echo if [ "$Current_domainname" != "" ] then $SRVCONFIG_PATH -s "workstation,domain=$Current_domainname" \ > $DEV_NULL_PATH 2>&1 fi if [ -d $SAMDIR_PATH/.tmp ] then restoredatabase fi pgettext "Success\n" if [ $Serverup = yes ] then eval echo "\"`pgettext 'Restarting the ${COMMENT} ... '`\"" $NET_PATH start server > $DEV_NULL_PATH 2>&1 if [ $? -ne 0 ] then pgettext "Failed to start\n" else pgettext "Started successfully\n" fi fi exit ${FATAL_EXIT_CODE} } #---------------------------------------------------------------------------- # # Setname: this is the worker routine. It is called in both interactive and # non-interactive modes. # All the required information has been specified on the command line and # checked. # setname () { # set the lanman.ini keyword $SRVCONFIG_PATH -s "workstation,domain=$NewDomain" #set the new domain name in the LSA database $SETDOMAIN_PATH -n $NewDomain if [ $? != 0 ] then restorestate else pgettext "Domain name changed successfully\n" fi # rename the domains blob file # first, make sure it exists if [ ! -f $SAMDIR_PATH/$Current_domainname ] then restorestate fi cp $SAMDIR_PATH/$Current_domainname $SAMDIR_PATH/$NewDomain rm -f $SAMDIR_PATH/$Current_domainname # set the owner/group on the new filename chgrp $GROUP $SAMDIR_PATH/* chown $OWNER $SAMDIR_PATH/* # check the database $SAMCHECK_PATH -sv > $DEV_NULL_PATH 2>&1 return } #---------------------------------------------------------------------------- # # # changename () { # # Interactive = no, command line has been used, just do it! # if [ $Interactive = yes ] then getdomain_name fi # do the actual name update setname return } #---------------------------------------------------------------------------- # # Prompt to stop the server # Globals: references Serverup, Interactive # stop_the_server () { # See if the server is running and then stop it. if [ $Serverup = yes ] then if [ $Interactive = yes ] then pgettext "The server is running.\n" pgettext "The domain name cannot be changed while the server is running.\n" eval yorn $YES "\"`pgettext 'Do you want to stop the server and continue'`\"" if [ $? != $YES ] then exit ${FATAL_EXIT_CODE} fi fi pgettext "Stopping the server ... \n" $NET_PATH stop server /y > $DEV_NULL_PATH 2>&1 if [ $? = 0 ] then pgettext "Success\n" else pgettext "Error: Unable to stop the server.\n" exit ${FATAL_EXIT_CODE} fi fi } #---------------------------------------------------------------------------- # # Start_the_server - determine whether to automatically start, prompt to start, # or not start the server. # Globals: references Serverup, Interactive # start_the_server () { # # If the server was not up AND we are running interactively, # then prompt to restart; # else do not start if the flag is set to not start; # all other times automatically restart # if [ $Serverup = no -a $Interactive = yes ] then eval yorn $YES "\"`pgettext 'Do you want to start the server now'`\"" startserver=$? elif [ $Autostart_flag = no ] then startserver=$NO else startserver=$YES fi if [ $startserver = $YES ] then eval echo "\"`pgettext 'Starting the ${COMMENT} ... '`\"" $NET_PATH start server > $DEV_NULL_PATH 2>&1 if [ $? -ne 0 ] then pgettext "Unable to start the server.\n" # restorestate else pgettext "Server started successfully \n\nNote: If the replicator service is running on this server, it may have \nto be reconfigured. Use the Properties function of Server Manager to check \nthe replication configuration.\n" fi fi } #---------------------------------------------------------------------------- #---------------------------------------------------------------------------- # # Beginning of main function of setdomainname # # # perform initialization, startup checks # initialize # is there are command line? should we run interactively? check_commandline $* if [ $? -eq 0 ] then Interactive=yes else Interactive=no fi if [ $Interactive = yes ] then eval echo "\"`pgettext 'This ${COMMENT} utility (${Progname})\nallows you to change the domain name on a server. If the domain\nname of this server is changed, the domain name must be changed on every\nserver in this domain. This utility must be run on each of these servers.\nThe server will be stopped each time you run ${Progname}.'`\"" eval yorn $NO "\"`pgettext 'Do you want to continue'`\"" if [ $? = $NO ] then exit ${FATAL_EXIT_CODE} fi fi savedatabase stop_the_server trap 'restorestate' 2 3 15 # # main routine to change the name # changename start_the_server trap 2 3 15 #remove saved files rm -f $SAMDIR_PATH/.tmp/* > $DEV_NULL_PATH 2>&1 rmdir $SAMDIR_PATH/.tmp > $DEV_NULL_PATH 2>&1 rm -f $LSA_PATH.t > $DEV_NULL_PATH 2>&1