From 993fdc1a62fac9b6906bc133d40b723f0fc716a5 Mon Sep 17 00:00:00 2001
From: Mark Reynolds <mreynolds@redhat.com>
Date: Tue, 29 Jan 2013 13:41:05 -0500
Subject: [PATCH] Ticket 433 - multiple bugs in start-dirsrv, stop-dirsrv, restart-dirsrv scripts

Bug Description:  These scripts check /etc/sysconfig/ to for DS instances.  The
                  issue is that when you install 389-admin it creates a file
                  called dirsrv-admin.  Then these scripts report errors when
                  trying to start/stop/restart.

Fix Description:  First, ignore instances that use "admin" for the indentifier.
                  Second, don't allow setup-ds.pl to use "admin" as the ID.  I
                  also needed to add extra checks for silent installations(which
                  do not use the DSDialogs.pm).  Also ensured the correct/detailed
                  error is displayed and logged when there is a failure.

https://fedorahosted.org/389/ticket/433

Reviewed by: ?
---
 ldap/admin/src/scripts/DSCreate.pm.in    |    4 +++-
 ldap/admin/src/scripts/DSDialogs.pm      |    6 +++++-
 ldap/admin/src/scripts/DSUtil.pm.in      |    7 ++++++-
 ldap/admin/src/scripts/restart-dirsrv.in |    4 ++++
 ldap/admin/src/scripts/setup-ds.pl.in    |    8 +++++++-
 ldap/admin/src/scripts/setup-ds.res.in   |    2 ++
 ldap/admin/src/scripts/start-dirsrv.in   |    4 ++++
 ldap/admin/src/scripts/stop-dirsrv.in    |    4 ++++
 8 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/ldap/admin/src/scripts/DSCreate.pm.in b/ldap/admin/src/scripts/DSCreate.pm.in
index 30de40e..736da17 100644
--- a/ldap/admin/src/scripts/DSCreate.pm.in
+++ b/ldap/admin/src/scripts/DSCreate.pm.in
@@ -122,7 +122,9 @@ sub sanityCheckParams {
         }
     }
 
-    if (!isValidServerID($inf->{slapd}->{ServerIdentifier})) {
+    if($inf->{slapd}->{ServerIdentifier} eq "admin"){
+        return ('error_invalid_serverid - "admin" is reserved for the Admininstration Server, please choose a different server identifier');
+    } elsif (!isValidServerID($inf->{slapd}->{ServerIdentifier})) {
         return ('error_invalid_serverid', $inf->{slapd}->{ServerIdentifier});
     } elsif (-d $inf->{slapd}->{config_dir}) {
         return ('error_server_already_exists', $inf->{slapd}->{config_dir});
diff --git a/ldap/admin/src/scripts/DSDialogs.pm b/ldap/admin/src/scripts/DSDialogs.pm
index 08c8b2b..00765e0 100644
--- a/ldap/admin/src/scripts/DSDialogs.pm
+++ b/ldap/admin/src/scripts/DSDialogs.pm
@@ -99,7 +99,11 @@ my $dsserverid = new Dialog (
         my $res = $DialogManager::SAME;
         my $path = $self->{manager}->{setup}->{configdir} . "/slapd-" . $ans;
         if (!isValidServerID($ans)) {
-            $self->{manager}->alert("error_invalid_serverid", $ans);
+            if($ans eq "admin"){
+                $self->{manager}->alert("error_reserved_serverid", $ans);
+            } else {
+                $self->{manager}->alert("error_invalid_serverid", $ans);
+            }
         } elsif (-d $path) {
             $self->{manager}->alert("error_server_already_exists", $path);
         } else {
diff --git a/ldap/admin/src/scripts/DSUtil.pm.in b/ldap/admin/src/scripts/DSUtil.pm.in
index 0f72571..70df23a 100644
--- a/ldap/admin/src/scripts/DSUtil.pm.in
+++ b/ldap/admin/src/scripts/DSUtil.pm.in
@@ -137,7 +137,12 @@ sub isValidDN {
 sub isValidServerID {
     my $servid = shift;
     my $validchars = '#%:\w@_-';
-    return $servid =~ /^[$validchars]+$/o;
+    if($servid eq "admin"){
+        # "admin" is reserved for the admin server
+        return 0;
+    } else {
+        return $servid =~ /^[$validchars]+$/o;
+    }
 }
 
 # we want the name of the effective user id of this process e.g. if someone did
diff --git a/ldap/admin/src/scripts/restart-dirsrv.in b/ldap/admin/src/scripts/restart-dirsrv.in
index 76fb176..74dc1cf 100644
--- a/ldap/admin/src/scripts/restart-dirsrv.in
+++ b/ldap/admin/src/scripts/restart-dirsrv.in
@@ -50,6 +50,10 @@ if [ "$#" -eq 0 ]; then
     for i in $initconfig_dir/@package_name@-*; do
         regex=s,$initconfig_dir/@package_name@-,,g
         inst=`echo $i | sed -e $regex`
+        # check for admin server ID used by 389-admin pkg, and ignore it
+        if [ "$inst" = "admin" ]; then
+            continue
+        fi
         echo Restarting instance \"$inst\"
         restart_instance $inst
         if [ "$?" -ne 0 ]; then
diff --git a/ldap/admin/src/scripts/setup-ds.pl.in b/ldap/admin/src/scripts/setup-ds.pl.in
index 7613ed8..044444b 100644
--- a/ldap/admin/src/scripts/setup-ds.pl.in
+++ b/ldap/admin/src/scripts/setup-ds.pl.in
@@ -88,8 +88,14 @@ if (@errs) {
     if ($setup->{update}) {
         $setup->msg($FATAL, 'error_updating');
     } else {
-        $setup->msg($FATAL, 'error_creating_dsinstance',
+        if($setup->{inf}->{slapd}->{ServerIdentifier} eq "admin"){
+            # 'admin' is reserved for the admin server - log the correct error 
+            $setup->msg($FATAL, 'error_creating_dsinstance_adminid',
                     $setup->{inf}->{slapd}->{ServerIdentifier});
+        } else {
+            $setup->msg($FATAL, 'error_creating_dsinstance',
+                    $setup->{inf}->{slapd}->{ServerIdentifier});
+        }
     }
     $setup->doExit(1);
 } else {
diff --git a/ldap/admin/src/scripts/setup-ds.res.in b/ldap/admin/src/scripts/setup-ds.res.in
index a8a0c00..401722e 100644
--- a/ldap/admin/src/scripts/setup-ds.res.in
+++ b/ldap/admin/src/scripts/setup-ds.res.in
@@ -88,6 +88,7 @@ error_creating_suffix = Could not create the suffix '%s'.  Error: %s\n\n
 
 setup_exiting = Exiting . . .\nLog file is '%s'\n\n
 error_creating_dsinstance = Error: Could not create directory server instance '%s'.\n
+error_creating_dsinstance_adminid = Error: Could not create directory server instance '%s', instance name 'admin' reserved for the Administration Server.\n
 created_dsinstance = Your new DS instance '%s' was successfully created.\n
 no_mapvalue_for_key = The map value '%s' for key '%s' did not map to a value in any of the given information files.\n
 error_opening_ldiftmpl = Could not open the LDIF template file '%s'.  Error: %s\n
@@ -104,6 +105,7 @@ program, or low port restriction.  Please choose another value for\
 ServerPort.  Error: $!\n
 error_invalid_serverid = The ServerIdentifier '%s' contains invalid characters.  It must\
 contain only alphanumeric characters and the following: #%:@_-\n\n
+error_reserved_serverid = The ServerIdentifier '%s" is reserved for the Administration Server, please choose a different server identifier.\n
 error_opening_scripttmpl = Could not open the script template file '%s'.  Error: %s\n
 error_creating_directory = Could not create directory '%s'.  Error: %s\n
 error_chowning_directory = Could not change ownership of directory '%s' to userid '%s': Error: %s\n
diff --git a/ldap/admin/src/scripts/start-dirsrv.in b/ldap/admin/src/scripts/start-dirsrv.in
index 291c821..cfd6477 100755
--- a/ldap/admin/src/scripts/start-dirsrv.in
+++ b/ldap/admin/src/scripts/start-dirsrv.in
@@ -127,6 +127,10 @@ if [ "$#" -eq 0 ]; then
     for i in $initconfig_dir/@package_name@-*; do
         regex=s,$initconfig_dir/@package_name@-,,g
         inst=`echo $i | sed -e $regex`
+        # check for admin server ID used by 389-admin pkg, and ignore it
+        if [ "$inst" = "admin" ]; then
+            continue
+        fi
         echo Starting instance \"$inst\"
         start_instance $inst
         if [ "$?" -ne 0 ]; then
diff --git a/ldap/admin/src/scripts/stop-dirsrv.in b/ldap/admin/src/scripts/stop-dirsrv.in
index 4d88585..28adde5 100755
--- a/ldap/admin/src/scripts/stop-dirsrv.in
+++ b/ldap/admin/src/scripts/stop-dirsrv.in
@@ -75,6 +75,10 @@ if [ "$#" -eq 0 ]; then
     for i in $initconfig_dir/@package_name@-*; do
         regex=s,$initconfig_dir/@package_name@-,,g
         inst=`echo $i | sed -e $regex`
+        # check for admin server ID used by 389-admin pkg, and ignore it
+        if [ "$inst" = "admin" ]; then
+            continue
+        fi
         echo Stopping instance \"$inst\"
         stop_instance $inst
         if [ "$?" -ne 0 ]; then
-- 
1.7.1

