Issue 8229 - ldapsearch: report failed search results to stderr even when ldif == 0
Summary: ldapsearch: report failed search results to stderr even when ldif == 0
Status: UNCONFIRMED
Alias: None
Product: OpenLDAP
Classification: Unclassified
Component: client tools (show other issues)
Version: unspecified
Hardware: All All
: --- normal
Target Milestone: 3.0.0
Assignee: OpenLDAP project
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-08-28 21:13 UTC by Ryan Tandy
Modified: 2021-07-07 15:50 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Ryan Tandy 2015-08-28 21:13:20 UTC
Full_Name: Ryan Tandy
Version: master
OS: Debian
URL: 
Submission from: (NULL) (24.68.37.4)
Submitted by: ryan


Forwarded from a Debian bug report: <https://bugs.debian.org/474021>

ldapsearch's error reporting currently does this:

$ ldapsearch -x -b dc=nonexistent > /dev/null
$ ldapsearch -x -b dc=nonexistent -L > /dev/null
No such object (32)
$ ldapsearch -x -b dc=nonexistent -LL > /dev/null
No such object (32)
$ ldapsearch -x -b dc=nonexistent -LLL > /dev/null
No such object (32)

The submitter would like it to report the failure to stderr in the default case
as well. He suggested this change:

-	if( !ldif ) {
-		printf( "result: %d %s\n", err, ldap_err2string(err) );

-	} else if ( err != LDAP_SUCCESS ) {
-		fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
+	if ( err != LDAP_SUCCESS %2%7{
+		fprintf( stderr, "Search failed: %s (%d)\n", ldap_err2string(err), err );
+	}
+
+	if ( !ldif ) {
+		printf( "result: %d %s\n", err, ldap_err2string(err) );
 	}

I would probably just flip the cases, and keep the "else" (i.e. not duplicate
the output):

-       if( !ldif ) {
-               printf( _("result: %d %s\n"), err, ldap_err2string(err) );
-
-       } else if ( err != LDAP_SUCCESS ) {
+       if ( err != LDAP_SUCCESS ) {
                fprintf( stderr, "%s (%d)\n", ldap_r2ststring(err), err );
+       } else if( !ldif ) {
+               printf( _("result: %d %s\n"), err, ldap_err2string(err) );
        }

Does either of these changes sound appropriate?

The submitter also suggested sending the LDAP_SYNC Cancelled message to stderr:

                                if ( cancel_msgid != -1 &&
                                                cancel_msgid == ldap_msgid( msg
) ) {
                                        printf(_("Cancelled \n"));
                                        printf(_("cancel_msgid = %d\n"),
cancel_msgid);

I don't know that area very well, but at a glance it seems like stdout is
probably more appropriate (so no change needed).

Thanks.
Comment 1 Ryan Tandy 2015-08-28 21:43:17 UTC
On Fri, Aug 28, 2015 at 11:23:50PM +0200, Hallvard Breien Furuseth wrote:
>We should be wary of changing ldapsearch output, since
>people parse it in scripts.

Good point. Adding a stderr message to the existing stdout message would 
safer. Still not as safe as no change, of course.

>I do find the output quirky and would have liked it different.
>Maybe yet another option could clean it up, or an -o flag similar
>to ldif-wrap.

Yet another option... :)

Comment 2 Quanah Gibson-Mount 2017-04-12 16:45:41 UTC
moved from Incoming to Software Bugs
Comment 3 Quanah Gibson-Mount 2021-02-22 18:09:52 UTC
Fix for 2.6
Comment 4 Quanah Gibson-Mount 2021-07-07 15:50:35 UTC
Pushing to 3.0 as any changes could break existing script behavior. 3.0 is open for significant behavior changes.