selftest: Improve error handling and perl style when setting up users in Samba4.pm
authorAndrew Bartlett <abartlet@samba.org>
Sun, 17 Oct 2021 22:55:14 +0000 (11:55 +1300)
committerStefan Metzmacher <metze@samba.org>
Wed, 20 Oct 2021 12:02:33 +0000 (12:02 +0000)
This catches errors and avoids using global varibles (the old
style file handles are global).

BUG: https://bugzilla.samba.org/show_bug.cgi?id=14869

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
selftest/target/Samba4.pm

index abb387b7527c89b60ce7b8e338088591d70fd741..4b302aa19de5975d102014e167d60a7145d60df2 100755 (executable)
@@ -240,12 +240,19 @@ sub wait_for_start($$)
 
 sub write_ldb_file($$$)
 {
-       my ($self, $file, $ldif) = @_;
+       my ($self, $file, $ldif_in) = @_;
 
        my $ldbadd =  Samba::bindir_path($self, "ldbadd");
-       open(LDIF, "|$ldbadd -H $file >/dev/null");
-       print LDIF $ldif;
-       return(close(LDIF));
+       open(my $ldif, "|$ldbadd -H $file > /dev/null")
+           or die "Failed to run $ldbadd: $!";
+       print $ldif $ldif_in;
+       close($ldif);
+
+       unless ($? == 0) {
+           warn("$ldbadd failed: $?");
+           return undef;
+       }
+       return 1;
 }
 
 sub add_wins_config($$)
@@ -954,6 +961,8 @@ sub provision_raw_step2($$$)
 {
        my ($self, $ctx, $ret) = @_;
 
+       my $ldif;
+
        my $provision_cmd = join(" ", @{$ctx->{provision_options}});
        unless (system($provision_cmd) == 0) {
                warn("Unable to provision: \n$provision_cmd\n");
@@ -999,17 +1008,23 @@ sub provision_raw_step2($$$)
 
        my $user_dn = "cn=$testallowed_account,cn=users,$base_dn";
        $testallowed_account = "testallowed account";
-       open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
-       print LDIF "dn: $user_dn
+       open($ldif, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb")
+           or die "Failed to run $ldbmodify: $!";
+       print $ldif "dn: $user_dn
 changetype: modify
 replace: samAccountName
 samAccountName: $testallowed_account
 -
 ";
-       close(LDIF);
+       close($ldif);
+       unless ($? == 0) {
+           warn("$ldbmodify failed: $?");
+           return undef;
+       }
 
-       open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
-       print LDIF "dn: $user_dn
+       open($ldif, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb")
+            or die "Failed to run $ldbmodify: $!";
+       print $ldif "dn: $user_dn
 changetype: modify
 replace: userPrincipalName
 userPrincipalName: testallowed upn\@$ctx->{realm}
@@ -1017,7 +1032,11 @@ replace: servicePrincipalName
 servicePrincipalName: host/testallowed
 -          
 ";
-       close(LDIF);
+       close($ldif);
+       unless ($? == 0) {
+           warn("$ldbmodify failed: $?");
+           return undef;
+       }
 
        $samba_tool_cmd = ${cmd_env};
        $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
@@ -1028,14 +1047,19 @@ servicePrincipalName: host/testallowed
        }
 
        $user_dn = "cn=testdenied,cn=users,$base_dn";
-       open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
-       print LDIF "dn: $user_dn
+        open($ldif, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb")
+            or die "Failed to run $ldbmodify: $!";
+        print $ldif "dn: $user_dn
 changetype: modify
 replace: userPrincipalName
 userPrincipalName: testdenied_upn\@$ctx->{realm}.upn
 -          
 ";
-       close(LDIF);
+       close($ldif);
+       unless ($? == 0) {
+           warn("$ldbmodify failed: $?");
+           return undef;
+       }
 
        $samba_tool_cmd = ${cmd_env};
        $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
@@ -1046,8 +1070,9 @@ userPrincipalName: testdenied_upn\@$ctx->{realm}.upn
        }
 
        $user_dn = "cn=testupnspn,cn=users,$base_dn";
-       open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
-       print LDIF "dn: $user_dn
+        open($ldif, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb")
+            or die "Failed to run $ldbmodify: $!";
+        print $ldif "dn: $user_dn
 changetype: modify
 replace: userPrincipalName
 userPrincipalName: http/testupnspn.$ctx->{dnsname}\@$ctx->{realm}
@@ -1055,7 +1080,11 @@ replace: servicePrincipalName
 servicePrincipalName: http/testupnspn.$ctx->{dnsname}
 -
 ";
-       close(LDIF);
+       close($ldif);
+       unless ($? == 0) {
+           warn("$ldbmodify failed: $?");
+           return undef;
+       }
 
        $samba_tool_cmd = ${cmd_env};
        $samba_tool_cmd .= Samba::bindir_path($self, "samba-tool")
@@ -1118,14 +1147,19 @@ servicePrincipalName: http/testupnspn.$ctx->{dnsname}
        # Change the userPrincipalName for jane
        $user_dn = "cn=jane,cn=users,$base_dn";
 
-       open(LDIF, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb");
-       print LDIF "dn: $user_dn
+       open($ldif, "|$ldbmodify -H $ctx->{privatedir}/sam.ldb")
+            or die "Failed to run $ldbmodify: $!";
+        print $ldif "dn: $user_dn
 changetype: modify
 replace: userPrincipalName
 userPrincipalName: jane.doe\@$ctx->{realm}
 -
 ";
-       close(LDIF);
+       close($ldif);
+       unless ($? == 0) {
+           warn("$ldbmodify failed: $?");
+           return undef;
+       }
 
        return $ret;
 }