Преглед изворни кода

Bug 538525 - Ability to create instance as non-root user

https://bugzilla.redhat.com/show_bug.cgi?id=538525
Resolves: bug 538525
Bug Description: Ability to create instance as non-root user
Branch: HEAD
Fix Description: By default for root user the init config file
will be stored in /etc/sysconfig and for non-root user it will
be stored in $HOME/.dirsrv folder.

A new parameter [slapd] initconfig_dir is added to the .inf file
to specify a non-default folder for the init config folder. This
folder must exist prior to running the DS tools. The folder can
also be specified via setup-ds.pl command-line parameter
slapd.initconfig_dir.

A new parameter -d is added to the start-dirsrv, restart-dirsrv,
and stop-dirsrv to specify the non-default init config folder.

A new parameter --initconfig_dir is added to remove-ds.pl to
specify the non-default init config folder.

The templates for start-slapd, restart-slapd, stop-slapd scripts
have been modified to specify the init config folder.
Endi S. Dewata пре 15 година
родитељ
комит
7701c56266

+ 64 - 8
ldap/admin/src/scripts/DSCreate.pm.in

@@ -260,6 +260,17 @@ sub createInstanceScripts {
     my $perlexec = "@perlexec@" || "/usr/bin/env perl";
     my $myperl = "!$perlexec";
     my $mydevnull = (-f "/dev/null" ? " /dev/null " : " NUL ");
+
+    # determine initconfig_dir
+    my $initconfig_dir = $inf->{slapd}->{initconfig_dir};
+    if (!$initconfig_dir) {
+        if ($ENV{USER} eq 'root') {
+            $initconfig_dir = "$inf->{General}->{prefix}@initconfigdir@";
+        } else {
+            $initconfig_dir = "$ENV{HOME}/.@package_name@";
+        }
+    }
+
     my %maptable = (
         "DS-ROOT" => $inf->{General}->{prefix},
         "SEP" => "/", # works on all platforms
@@ -273,6 +284,7 @@ sub createInstanceScripts {
         "BAK-DIR" => $inf->{slapd}->{bak_dir},
         "SERVER-DIR" => $inf->{General}->{ServerRoot},
         "CONFIG-DIR" => $inf->{slapd}->{config_dir},
+        "INITCONFIG-DIR" => $initconfig_dir,
         "INST-DIR" => $inf->{slapd}->{inst_dir},
         "RUN-DIR" => $inf->{slapd}->{run_dir},
         "PRODUCT-NAME" => "slapd",
@@ -458,9 +470,21 @@ sub makeOtherConfigFiles {
         return @errs;
     }
 
+    # determine initconfig_dir
+    my $initconfig_dir = $inf->{slapd}->{initconfig_dir};
+    if (!$initconfig_dir) {
+        if ($ENV{USER} eq 'root') {
+            $initconfig_dir = "$inf->{General}->{prefix}@initconfigdir@";
+        } else {
+            $initconfig_dir = "$ENV{HOME}/.@package_name@";
+            mkpath $initconfig_dir unless -d $initconfig_dir;
+        }
+    }
+
     # install instance specific initconfig script
     $src = "$inf->{General}->{prefix}@configdir@/template-initconfig";
-    $dest = "$inf->{General}->{prefix}@initconfigdir@/@package_name@-$inf->{slapd}->{ServerIdentifier}";
+    $dest = "$initconfig_dir/@package_name@-$inf->{slapd}->{ServerIdentifier}";
+
     $! = 0; # clear errno
 
     if (!open(SRC, "< $src")) {
@@ -1026,10 +1050,42 @@ sub stopServer {
 sub removeDSInstance {
     my $inst = shift;
     my $force = shift;
+    my $initconfig_dir = shift;
     my $baseconfigdir = $ENV{DS_CONFIG_DIR} || "@instconfigdir@";
     my $instname = "slapd-$inst";
-    my $configdir = "$baseconfigdir/$instname";
+    my $configdir;
+    my $rundir;
+    my $product_name;
     my @errs;
+
+    # determine initconfig_dir
+    if (!$initconfig_dir) {
+        if ($ENV{USER} eq 'root') {
+            $initconfig_dir = "@initconfigdir@";
+        } else {
+            $initconfig_dir = "$ENV{HOME}/.@package_name@";
+        }
+    }
+
+    my $initconfig = "$initconfig_dir/@package_name@-$inst";
+
+    # Get the configdir, rundir and product_name from the instance initconfig script.
+    unless(open(INFILE, $initconfig)) {
+        return ( [ 'error_no_such_instance', $instname, $! ] );
+    }
+
+    my $line;
+    while($line = <INFILE>) {
+        if ($line =~ /CONFIG_DIR=(.*) ; export CONFIG_DIR/) {
+            $configdir = $1;
+        } elsif ($line =~ /RUN_DIR=(.*) ; export INST_DIR/) {
+            $rundir = $1;
+        } elsif ($line =~ /PRODUCT_NAME=(.*) ; export PRODUCT_NAME/) {
+            $product_name = $1;
+        }
+    }
+    close(INFILE);
+
     if ( ! -d $configdir )
     {
         debug(1, "Error: $configdir does not exist: $!\n");
@@ -1097,8 +1153,8 @@ sub removeDSInstance {
     if ( -d $instdir && $instdir =~ /$instname/ )
     {
         # clean up pid files (if any)
-        remove_pidfile("STARTPIDFILE", $instdir, $instname);
-        remove_pidfile("PIDFILE", $instdir, $instname);
+        remove_pidfile("STARTPIDFILE", $inst, $instdir, $instname, $rundir, $product_name);
+        remove_pidfile("PIDFILE", $inst, $instdir, $instname, $rundir, $product_name);
 
         my $rc = rmtree($instdir);
         if ( 0 == $rc )
@@ -1111,12 +1167,12 @@ sub removeDSInstance {
     push @errs, remove_tree($entry, "nsslapd-schemadir", $instname, 1, "\.db\$");
 
     # Remove the instance specific initconfig script
-    if ( -f "@initconfigdir@/@package_name@-$inst" ) {
-        my $rc = unlink("@initconfigdir@/@package_name@-$inst");
+    if ( -f $initconfig ) {
+        my $rc = unlink($initconfig);
         if ( 0 == $rc )
         {
-            push @errs, [ 'error_removing_path', "@initconfigdir@/@package_name@-$inst", $! ];
-            debug(1, "Warning: @initconfigdir@/@package_name@-$inst was not removed. Error: $!\n");
+            push @errs, [ 'error_removing_path', $initconfig, $! ];
+            debug(1, "Warning: $initconfig was not removed. Error: $!\n");
         }
     }
 

+ 1 - 37
ldap/admin/src/scripts/DSUtil.pm.in

@@ -947,45 +947,9 @@ sub remove_tree
 
 sub remove_pidfile
 {
-    my ($type, $instdir, $instname) = @_;
-    my $serv_id;
-    my $run_dir;
-    my $product_name;
+    my ($type, $serv_id, $instdir, $instname, $run_dir, $product_name) = @_;
     my $pidfile;
 
-    # Get the serv_id from the start-slapd script.
-    unless(open(INFILE,"$instdir/start-slapd")) {
-        print("Cannot open start-slapd file for reading "); return 0;
-    }
-    my $line;
-    while($line = <INFILE>) {
-        if ($line =~ /start-dirsrv /g) {
-            my @servline=split(/start-dirsrv /, $line);
-            @servline=split(/\s+/, $servline[1]);
-            $serv_id=$servline[0];
-        }
-    }
-    close(INFILE);
-
-    # Get the run_dir and product_name from the instance initconfig script.
-    unless(open(INFILE,"@initconfigdir@/@package_name@-$serv_id")) {
-        print("Couldn't open @initconfigdir@/@package_name@-$serv_id "); return 0;
-    }
-    while($line = <INFILE>) {
-        if ($line =~ /RUN_DIR=/g) {
-            my @rundir_line=split(/RUN_DIR=+/, $line);
-            @rundir_line=split(/;/, $rundir_line[1]);
-            $run_dir = $rundir_line[0];
-            chop($run_dir);
-        } elsif ($line =~ /PRODUCT_NAME=/g) {
-            my @product_line=split(/PRODUCT_NAME=+/, $line);
-            @product_line=split(/;/, $product_line[1]);
-            $product_name = $product_line[0];
-            chop($product_name);
-        }
-    }
-    close(INFILE);
-
     # Construct the pidfile name as follows:
     #     PIDFILE=$RUN_DIR/$PRODUCT_NAME-$SERV_ID.pid
     #     STARTPIDFILE=$RUN_DIR/$PRODUCT_NAME-$SERV_ID.startpid

+ 12 - 16
ldap/admin/src/scripts/remove-ds.pl.in

@@ -23,10 +23,14 @@ use strict;
 
 use File::Basename;
 use File::Path;
+use Getopt::Long;
 use DSUtil;
 use Resource;
 use DSCreate qw(removeDSInstance);
 
+# process command line options
+Getopt::Long::Configure(qw(bundling)); # bundling allows -ddddd
+
 my $res = new Resource("@propertydir@/setup-ds.res");
 
 sub usage {
@@ -36,24 +40,16 @@ sub usage {
         print(STDERR "       -d            - turn on debugging output\n");
 }
 
-my $i = 0;
 my $force = "";
 my $instname = "";
+my $initconfig_dir = "";
 
-# load args from the command line
-while ($i <= $#ARGV) {
-    if ( "$ARGV[$i]" eq "-f" ) { 
-        $force = 1;
-    } elsif ("$ARGV[$i]" eq "-i") {
-        $i++;
-        $instname = $ARGV[$i];
-    } elsif ("$ARGV[$i]" eq "-d") {
-        $DSUtil::debuglevel++;
-    } else {
-        &usage; exit(1);
-    }
-    $i++;
-}
+GetOptions('help|h|?' => sub { &usage; exit(1); },
+           'debug|d+' => \$DSUtil::debuglevel,
+           'instance|i=s' => \$instname,
+           'initconfig_dir|c=s' => \$initconfig_dir,
+           'force|f' => \$force
+           );
 
 # Make sure the instance name option was provided.
 unless ($instname) {
@@ -67,7 +63,7 @@ unless ($inst) {
     exit 1;
 }
 
-my @errs = removeDSInstance($inst, $force);
+my @errs = removeDSInstance($inst, $force, $initconfig_dir);
 if (@errs) {
     print STDERR "The following errors occurred during removal:\n";
     for (@errs) {

+ 21 - 4
ldap/admin/src/scripts/restart-dirsrv.in

@@ -11,7 +11,7 @@ restart_instance() {
     SERV_ID=$1
 
     server_already_stopped=0
-    @sbindir@/stop-dirsrv $SERV_ID
+    @sbindir@/stop-dirsrv -d $initconfig_dir $SERV_ID
     status=$?
     if [ $status -eq 1 ] ; then
         return 3;
@@ -20,7 +20,7 @@ restart_instance() {
             server_already_stopped=1
        fi
     fi
-    @sbindir@/start-dirsrv $SERV_ID
+    @sbindir@/start-dirsrv -d $initconfig_dir $SERV_ID
     status=$?
     if [ $server_already_stopped -eq 1 ] && [ $status -eq 0 ] ; then
         return 2;
@@ -28,11 +28,28 @@ restart_instance() {
     return $status
 }
 
+while getopts "d:" flag
+do
+    case "$flag" in
+        d) initconfig_dir="$OPTARG";;
+    esac
+done
+shift $(($OPTIND-1))
+
+if [ "$initconfig_dir" = "" ]; then
+    if [ $USER = root ] ; then
+        initconfig_dir=@initconfigdir@
+    else
+        initconfig_dir=$HOME/.@package_name@
+    fi
+fi
+
 if [ "$#" -eq 0 ]; then
     # We're restarting all instances.
     ret=0
-    for i in @initconfigdir@/@package_name@-*; do
-        inst=`echo $i | sed -e 's,@initconfigdir@/@package_name@-,,g'`
+    for i in $initconfig_dir/@package_name@-*; do
+        regex=s,$initconfig_dir/@package_name@-,,g
+        inst=`echo $i | sed -e $regex`
         echo Restarting instance \"$inst\"
         restart_instance $inst
         if [ "$?" -ne 0 ]; then

+ 22 - 5
ldap/admin/src/scripts/start-dirsrv.in

@@ -14,8 +14,8 @@ start_instance() {
     shift
 
     # source env. for this instance
-    if [ -f @initconfigdir@/@package_name@-$SERV_ID ] ; then
-        . @initconfigdir@/@package_name@-$SERV_ID
+    if [ -f $initconfig_dir/@package_name@-$SERV_ID ] ; then
+        . $initconfig_dir/@package_name@-$SERV_ID
     else
         echo Instance $SERV_ID not found.
         return 1
@@ -93,11 +93,28 @@ start_instance() {
 # source env. for all instances
 [ -f @initconfigdir@/@package_name@ ] && . @initconfigdir@/@package_name@
 
+while getopts "d:" flag
+do
+    case "$flag" in
+        d) initconfig_dir="$OPTARG";;
+    esac
+done
+shift $(($OPTIND-1))
+
+if [ "$initconfig_dir" = "" ]; then
+    if [ $USER = root ] ; then
+        initconfig_dir=@initconfigdir@
+    else
+        initconfig_dir=$HOME/.@package_name@
+    fi
+fi
+
 if [ "$#" -eq 0 ]; then
     # We're starting all instances.
     ret=0
-    for i in @initconfigdir@/@package_name@-*; do
-        inst=`echo $i | sed -e 's,@initconfigdir@/@package_name@-,,g'`
+    for i in $initconfig_dir/@package_name@-*; do
+        regex=s,$initconfig_dir/@package_name@-,,g
+        inst=`echo $i | sed -e $regex`
         echo Starting instance \"$inst\"
         start_instance $inst
         if [ "$?" -ne 0 ]; then
@@ -107,6 +124,6 @@ if [ "$#" -eq 0 ]; then
     exit $ret
 else
     # We're starting a single instance.
-    start_instance $*
+    start_instance $@
     exit $?
 fi

+ 22 - 5
ldap/admin/src/scripts/stop-dirsrv.in

@@ -10,8 +10,8 @@ stop_instance() {
     SERV_ID=$1
 
     # source env. for this instance
-    if [ -f @initconfigdir@/@package_name@-$SERV_ID ]; then
-        . @initconfigdir@/@package_name@-$SERV_ID
+    if [ -f $initconfig_dir/@package_name@-$SERV_ID ] ; then
+        . $initconfig_dir/@package_name@-$SERV_ID
     else
         echo Instance $SERV_ID not found.
         return 1
@@ -53,11 +53,28 @@ stop_instance() {
     return 1
 }
 
+while getopts "d:" flag
+do
+    case "$flag" in
+        d) initconfig_dir="$OPTARG";;
+    esac
+done
+shift $(($OPTIND-1))
+
+if [ "$initconfig_dir" = "" ]; then
+    if [ $USER = root ] ; then
+        initconfig_dir=@initconfigdir@
+    else
+        initconfig_dir=$HOME/.@package_name@
+    fi
+fi
+
 if [ "$#" -eq 0 ]; then
     # We're stopping all instances.
     ret=0
-    for i in @initconfigdir@/@package_name@-*; do
-        inst=`echo $i | sed -e 's,@initconfigdir@/@package_name@-,,g'`
+    for i in $initconfig_dir/@package_name@-*; do
+        regex=s,$initconfig_dir/@package_name@-,,g
+        inst=`echo $i | sed -e $regex`
         echo Stopping instance \"$inst\"
         stop_instance $inst
         if [ "$?" -ne 0 ]; then
@@ -67,6 +84,6 @@ if [ "$#" -eq 0 ]; then
     exit $ret
 else
     # We're stopping a single instance.
-    stop_instance $*
+    stop_instance $@
     exit $?
 fi

+ 1 - 1
ldap/admin/src/scripts/template-restart-slapd.in

@@ -7,5 +7,5 @@
 #       2: Server started successfully (was not running)
 #       3: Server could not be stopped
 
-@sbindir@/restart-dirsrv {{SERV-ID}} "$@"
+@sbindir@/restart-dirsrv -d {{INITCONFIG-DIR}} {{SERV-ID}} "$@"
 exit $?

+ 1 - 1
ldap/admin/src/scripts/template-start-slapd.in

@@ -6,5 +6,5 @@
 #       1: Server could not be started
 #       2: Server already running
 
-@sbindir@/start-dirsrv {{SERV-ID}} "$@"
+@sbindir@/start-dirsrv -d {{INITCONFIG-DIR}} {{SERV-ID}} "$@"
 exit $?

+ 1 - 1
ldap/admin/src/scripts/template-stop-slapd.in

@@ -6,5 +6,5 @@
 #       1: Server could not be stopped
 #       2: Server was not running
 
-@sbindir@/stop-dirsrv {{SERV-ID}} "$@"
+@sbindir@/stop-dirsrv -d {{INITCONFIG-DIR}} {{SERV-ID}} "$@"
 exit $?