From: Andrew Bartlett Date: Sun, 17 Oct 2021 22:55:14 +0000 (+1300) Subject: selftest: Improve error handling and perl style when setting up users in Samba4.pm X-Git-Tag: ldb-2.5.0~373 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=459200caba04fd83ed650b9cdfe5b158cf9a149f;p=thirdparty%2Fsamba.git selftest: Improve error handling and perl style when setting up users in Samba4.pm 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 Reviewed-by: Stefan Metzmacher --- diff --git a/selftest/target/Samba4.pm b/selftest/target/Samba4.pm index abb387b7527..4b302aa19de 100755 --- a/selftest/target/Samba4.pm +++ b/selftest/target/Samba4.pm @@ -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; }