From 7f7f83cbf68f4ce88c00ad203889272812accd0e Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Mar 22 2013 19:52:55 +0000 Subject: Ticket #47299 - allow cmdline scripts to work with non-root user https://fedorahosted.org/389/ticket/47299 Reviewed by: mreynolds (Thanks!) Branch: master Fix Description: If running as non-root, look for the initconfig scripts/files in ~/.dirsrv. If the INITCONFIGDIR env. var. is set, use that and do not look anywhere else. Cannot depend on the value of $USER - must use id to determine the effective user id. Also fixed a problem with return value handling - the if test changes the value of $?, so have to save the value just after starting the instance. Platforms tested: RHEL6 x86_64 Flag Day: no Doc impact: no --- diff --git a/ldap/admin/src/scripts/DSSharedLib.in b/ldap/admin/src/scripts/DSSharedLib.in index 17079d7..1a66e28 100644 --- a/ldap/admin/src/scripts/DSSharedLib.in +++ b/ldap/admin/src/scripts/DSSharedLib.in @@ -20,13 +20,38 @@ get_server_id() inst_count=0 instances="" rc=0 - - for i in `ls $dir/dirsrv-* 2>/dev/null` + + # convert + # uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),..... + # to + # 0 + # this is the only portable, secure way to determine the id number + userid=`id | awk -F'[=(]+' '{print $2}'` + if [ "$userid" -ne 0 ] ; then + # magic - see start-dirsrv, DSCreate.pm::get_initconfigdir, etc. + dir=$HOME/.@package_name@ + fi + if [ -n "$INITCONFIGDIR" ] ; then + dir=$INITCONFIGDIR + fi + + # look first in user provided INITCONFIGDIR, then in the system/build location, + # then in the users home dir - cases + # 1. system install but running as non-root user + # in this case, we want to use the instance from $dir - it will + # fallback to $homedir in that case, and if that is a problem, the user will + # just have to temporarily move $homedir/dirsrv-sysinstancename out of the way + # while working on the system instance + # 2. prefix/non-system install + # in this case, we want to use $homedir - if for some reason there is a system + # instance in $dir with the same name, the user can use INITCONFIGDIR to + # override that and force the use of the one from $homedir + for i in `ls $dir/@package_name@-* 2>/dev/null` do - if [ $i != "$dir/dirsrv-admin" ] + if [ $i != "$dir/@package_name@-admin" ] then inst_count=`expr $inst_count + 1` - id=$(expr "$i" : ".*dirsrv-\([^)]*\).*") + id=$(expr "$i" : ".*@package_name@-\([^)]*\).*") if [ $first == "yes" ] then instances=$id @@ -52,11 +77,11 @@ get_server_id() elif [ $servid == slapd-* ] then servid=`echo "$servid" | sed -e 's/slapd-//'` - elif [ $servid == dirsrv-* ] + elif [ $servid == @package_name@-* ] then - servid=`echo "$servid" | sed -e 's/dirsrv-//'` + servid=`echo "$servid" | sed -e 's/@package_name@-//'` fi - if ! [ -a "$dir/dirsrv-$servid" ] + if ! [ -a "$dir/@package_name@-$servid" ] then # invalid instance name, return the "valid" instance names servid=$instances diff --git a/ldap/admin/src/scripts/DSUtil.pm.in b/ldap/admin/src/scripts/DSUtil.pm.in index e587671..b69cbb5 100644 --- a/ldap/admin/src/scripts/DSUtil.pm.in +++ b/ldap/admin/src/scripts/DSUtil.pm.in @@ -1287,13 +1287,21 @@ sub get_server_id { my $instances = ""; my $name; my $file; + + if (getLogin ne 'root') { + $dir = "$ENV{HOME}/.@package_name@"; + } + + if (defined $ENV{INITCONFIGDIR}) { + $dir = $ENV{INITCONFIGDIR}; + } opendir(DIR, "$dir"); my @files = readdir(DIR); foreach $file (@files){ - if($file =~ /^dirsrv-/ && $file ne "dirsrv-admin"){ + if($file =~ /^@package_name@-/ && $file ne "@package_name@-admin"){ $instance_count++; - if($file =~ /dirsrv-(.*)/){ + if($file =~ /@package_name@-(.*)/){ if($first eq "yes"){ $instances=$1; $first = "no"; @@ -1312,17 +1320,17 @@ sub get_server_id { print "Available instances: $instances\n"; exit (1); } - } elsif ($servid =~ /^dirsrv-/){ - # strip off "dirsrv-" - $servid =~ s/^dirsrv-//; + } elsif ($servid =~ /^@package_name@-/){ + # strip off "@package_name@-" + $servid =~ s/^@package_name@-//; } elsif ($servid =~ /^slapd-/){ # strip off "slapd-" $servid =~ s/^slapd-//; } - unless ( -e "$dir/dirsrv-$servid" ){ + unless ( -e "$dir/@package_name@-$servid" ){ print (STDERR "Invalid server identifer: $servid\n"); - print (STDERR "Available instances: $instances\n"); + print (STDERR "Available instances in $dir: $instances\n"); exit (1); } diff --git a/ldap/admin/src/scripts/restart-dirsrv.in b/ldap/admin/src/scripts/restart-dirsrv.in index 74dc1cf..c5825b9 100644 --- a/ldap/admin/src/scripts/restart-dirsrv.in +++ b/ldap/admin/src/scripts/restart-dirsrv.in @@ -37,7 +37,13 @@ done shift $(($OPTIND-1)) if [ "$initconfig_dir" = "" ]; then - if [ $USER = root ] ; then + # convert + # uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),..... + # to + # 0 + # this is the only portable, secure way to determine the id number + userid=`id | awk -F'[=(]+' '{print $2}'` + if [ "$userid" -eq 0 ] ; then initconfig_dir=@initconfigdir@ else initconfig_dir=$HOME/.@package_name@ @@ -56,8 +62,9 @@ if [ "$#" -eq 0 ]; then fi echo Restarting instance \"$inst\" restart_instance $inst - if [ "$?" -ne 0 ]; then - ret=$? + rv=$? + if [ "$rv" -ne 0 ]; then + ret=$rv fi done exit $ret diff --git a/ldap/admin/src/scripts/start-dirsrv.in b/ldap/admin/src/scripts/start-dirsrv.in index b5f45f9..7864ad9 100755 --- a/ldap/admin/src/scripts/start-dirsrv.in +++ b/ldap/admin/src/scripts/start-dirsrv.in @@ -115,7 +115,13 @@ done shift $(($OPTIND-1)) if [ "$initconfig_dir" = "" ]; then - if [ $USER = root ] ; then + # convert + # uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),..... + # to + # 0 + # this is the only portable, secure way to determine the id number + userid=`id | awk -F'[=(]+' '{print $2}'` + if [ "$userid" -eq 0 ] ; then initconfig_dir=@initconfigdir@ else initconfig_dir=$HOME/.@package_name@ @@ -134,8 +140,9 @@ if [ "$#" -eq 0 ]; then fi echo Starting instance \"$inst\" start_instance $inst - if [ "$?" -ne 0 ]; then - ret=$? + rv=$? + if [ "$rv" -ne 0 ]; then + ret=$rv fi done exit $ret diff --git a/ldap/admin/src/scripts/stop-dirsrv.in b/ldap/admin/src/scripts/stop-dirsrv.in index 220fe92..cec65d9 100755 --- a/ldap/admin/src/scripts/stop-dirsrv.in +++ b/ldap/admin/src/scripts/stop-dirsrv.in @@ -67,7 +67,13 @@ done shift $(($OPTIND-1)) if [ "$initconfig_dir" = "" ]; then - if [ $USER = root ] ; then + # convert + # uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),..... + # to + # 0 + # this is the only portable, secure way to determine the id number + userid=`id | awk -F'[=(]+' '{print $2}'` + if [ "$userid" -eq 0 ] ; then initconfig_dir=@initconfigdir@ else initconfig_dir=$HOME/.@package_name@ @@ -86,8 +92,9 @@ if [ "$#" -eq 0 ]; then fi echo Stopping instance \"$inst\" stop_instance $inst - if [ "$?" -ne 0 ]; then - ret=$? + rv=$? + if [ "$rv" -ne 0 ]; then + ret=$rv fi done exit $ret