#include "common.h"
+#if !LDAP_DEPRECATED
+/*
+ * NOTE: we declare it here only because we want to keep supporting
+ * (how long?) ancient, deprecated LDAP_AUTH_KRB* auth methods
+ */
+LDAP_F( int )
+ldap_bind LDAP_P(( /* deprecated */
+ LDAP *ld,
+ LDAP_CONST char *who,
+ LDAP_CONST char *passwd,
+ int authmethod ));
+#endif
int authmethod = -1;
char *binddn = NULL;
}
void tool_perror(
- char *func,
+ const char *func,
int err,
- char *extra,
- char *matched,
- char *info,
+ const char *extra,
+ const char *matched,
+ const char *info,
char **refs )
{
fprintf( stderr, "%s: %s (%d)%s\n",
exit( EXIT_FAILURE );
}
- if ( use_tls &&
- ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ))
- {
- ldap_perror( ld, "ldap_start_tls" );
- if ( use_tls > 1 ) {
- exit( EXIT_FAILURE );
+ if ( use_tls ) {
+ rc = ldap_start_tls_s( ld, NULL, NULL );
+ if ( rc != LDAP_SUCCESS ) {
+ tool_perror( "ldap_start_tls", rc, NULL, NULL, NULL, NULL );
+ if ( use_tls > 1 ) {
+ exit( EXIT_FAILURE );
+ }
}
}
}
lutil_sasl_freedefs( defaults );
if( rc != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
+ tool_perror( "ldap_sasl_interactive_bind_s",
+ rc, NULL, NULL, NULL, NULL );
exit( EXIT_FAILURE );
}
#else
exit( EXIT_FAILURE );
#endif
} else {
- int msgid, err;
+ int msgid, err, rc;
LDAPMessage *result;
LDAPControl **ctrls;
char msgbuf[256];
msgid = ldap_bind( ld, binddn, passwd.bv_val, authmethod );
if ( msgid == -1 ) {
- ldap_perror( ld, "ldap_bind" );
+ tool_perror( "ldap_bind", -1, NULL, NULL, NULL, NULL );
exit( EXIT_FAILURE );
}
if ( ldap_result( ld, msgid, 1, NULL, &result ) == -1 ) {
- ldap_perror( ld, "ldap_result" );
+ tool_perror( "ldap_result", -1, NULL, NULL, NULL, NULL );
exit( EXIT_FAILURE );
}
- if ( ldap_parse_result( ld, result, &err, &matched, &info, &refs,
- &ctrls, 1 ) != LDAP_SUCCESS )
- {
- ldap_perror( ld, "ldap_bind parse result" );
+ rc = ldap_parse_result( ld, result, &err, &matched, &info, &refs,
+ &ctrls, 1 );
+ if ( rc != LDAP_SUCCESS ) {
+ tool_perror( "ldap_bind parse result", rc, NULL, NULL, NULL, NULL );
exit( EXIT_FAILURE );
}
return -1;
case LDAP_REQ_ABANDON:
- rc = ldap_abandon( ld, msgid );
+ rc = ldap_abandon_ext( ld, msgid, NULL, NULL );
fprintf( stderr, "got interrupt, abandon got %d: %s\n",
rc, ldap_err2string( rc ) );
return -1;
void tool_server_controls LDAP_P(( LDAP *, LDAPControl *, int ));
int tool_check_abandon LDAP_P(( LDAP *ld, int msgid ));
void tool_perror LDAP_P((
- char *func,
+ const char *func,
int err,
- char *extra,
- char *matched,
- char *info,
+ const char *extra,
+ const char *matched,
+ const char *info,
char **refs ));
LDAP_END_DECL
rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
if ( rc < 0 ) {
- ldap_perror( ld, "ldapcompare: ldap_result" );
+ tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
return rc;
}
rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
if ( rc < 0 ) {
- ldap_perror( ld, "ldapdelete: ldap_result" );
+ tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
return rc;
}
rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
NULL, NULL, NULL, -1, &res );
if ( rc != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_search" );
+ tool_perror( "ldap_search", rc, NULL, NULL, NULL, NULL );
return( rc );
}
char *dn = ldap_get_dn( ld, e );
if( dn == NULL ) {
- ldap_perror( ld, "ldap_prune" );
ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
+ tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
ber_memfree( dn );
return rc;
}
rc = deletechildren( ld, dn );
if ( rc == -1 ) {
- ldap_perror( ld, "ldap_prune" );
+ tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
ber_memfree( dn );
return rc;
}
printf( _("\tremoving %s\n"), dn );
}
- rc = ldap_delete_s( ld, dn );
+ rc = ldap_delete_ext_s( ld, dn, NULL, NULL );
if ( rc == -1 ) {
- ldap_perror( ld, "ldap_delete" );
+ tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
ber_memfree( dn );
return rc;
rc = ldap_search_ext_s( ld, dn, LDAP_SCOPE_ONELEVEL, NULL, attrs, 1,
ctrls, NULL, NULL, -1, &res_se );
if ( rc != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_search" );
+ tool_perror( "ldap_search", rc, NULL, NULL, NULL, NULL );
return( rc );
}
ber_free( ber, 1 );
char *dn = ldap_get_dn( ld, e );
if( dn == NULL ) {
- ldap_perror( ld, "ldap_prune" );
ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &rc );
+ tool_perror( "ldap_prune", rc, NULL, NULL, NULL, NULL );
ber_memfree( dn );
return rc;
}
printf( _("\tremoving %s\n"), dn );
}
- rc = ldap_delete_s( ld, dn );
+ rc = ldap_delete_ext_s( ld, dn, NULL, NULL );
if ( rc == -1 ) {
- ldap_perror( ld, "ldap_delete" );
+ tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
ber_memfree( dn );
return rc;
static int process_response(
LDAP *ld,
int msgid,
- const char *opstr,
+ int res,
const char *dn );
static char *read_one_record LDAP_P(( FILE *fp ));
/* create transaction */
rc = ldap_txn_create_s( ld, &txnCookiep, NULL, NULL );
if( rc != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_txn_create_s" );
+ tool_perror( "ldap_txn_create_s", rc, NULL, NULL, NULL, NULL );
if( txn > 2 ) return EXIT_FAILURE;
txn = 0;
}
/* create transaction */
rc = ldap_txn_end_s( ld, &txnCookie, !txnabort, NULL, NULL );
if( rc != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_txn_create_s" );
+ tool_perror( "ldap_txn_create_s", rc, NULL, NULL, NULL, NULL );
if( txn > 2 ) return EXIT_FAILURE;
txn = 0;
}
if ( rc != LDAP_SUCCESS ) {
/* print error message about failed update including DN */
fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
- ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
+ tool_perror( newentry ? "ldap_add" : "ldap_modify", rc, NULL, NULL, NULL, NULL );
goto done;
} else if ( verbose ) {
printf( _("modify complete\n") );
}
rc = process_response( ld, msgid,
- newentry ? "ldap_add" : "ldap_modify", dn );
+ newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn );
} else {
rc = LDAP_SUCCESS;
rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
- ldap_perror( ld, "ldap_delete" );
+ tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
goto done;
} else if ( verbose ) {
printf( _("delete complete") );
}
- rc = process_response( ld, msgid, "ldap_delete", dn );
+ rc = process_response( ld, msgid, LDAP_RES_DELETE, dn );
} else {
rc = LDAP_SUCCESS;
pctrls, NULL, &msgid );
if ( rc != LDAP_SUCCESS ) {
fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
- ldap_perror( ld, "ldap_modrdn" );
+ tool_perror( "ldap_modrdn", rc, NULL, NULL, NULL, NULL );
goto done;
} else {
printf( _("modrdn completed\n") );
}
- rc = process_response( ld, msgid, "ldap_rename", dn );
+ rc = process_response( ld, msgid, LDAP_RES_RENAME, dn );
} else {
rc = LDAP_SUCCESS;
return( rc );
}
+static const char *
+res2str( int res ) {
+ switch ( res ) {
+ case LDAP_RES_ADD:
+ return "ldap_add";
+ case LDAP_RES_DELETE:
+ return "ldap_delete";
+ case LDAP_RES_MODIFY:
+ return "ldap_modify";
+ case LDAP_RES_MODRDN:
+ return "ldap_modrdn";
+ default:
+ assert( 0 );
+ }
+
+ return "ldap_unknown";
+}
+
static int process_response(
LDAP *ld,
int msgid,
- const char *opstr,
+ int op,
const char *dn )
{
LDAPMessage *res;
- int rc = LDAP_OTHER;
+ int rc = LDAP_OTHER,
+ msgtype;
struct timeval tv = { 0, 0 };
for ( ; ; ) {
}
}
- if ( ldap_msgtype( res ) != LDAP_RES_INTERMEDIATE ) {
- rc = ldap_result2error( ld, res, 1 );
- if( rc != LDAP_SUCCESS ) ldap_perror( ld, opstr );
+ msgtype = ldap_msgtype( res );
+ if ( msgtype != LDAP_RES_INTERMEDIATE ) {
+ int err;
+ char *text = NULL, *matched = NULL, **refs = NULL;
+
+ rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, NULL, 1 );
+ if ( rc == LDAP_SUCCESS ) {
+ rc = err;
+ }
+ if ( rc != LDAP_SUCCESS ) {
+ tool_perror( res2str( op ), rc, NULL, matched, text, refs );
+ } else if ( msgtype != op ) {
+ fprintf( stderr, "%s: msgtype: expected %d got %d\n",
+ res2str( op ), op, msgtype );
+ rc = LDAP_OTHER;
+ }
+ if ( text ) {
+ ldap_memfree( text );
+ }
+ if ( matched ) {
+ ldap_memfree( matched );
+ }
+ if ( text ) {
+ ber_memvfree( (void **)refs );
+ }
return rc;
}
rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
if ( rc < 0 ) {
- ldap_perror( ld, "ldapmodrdn: ldap_result" );
+ tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
return rc;
}
ber_free( ber, 1 );
if( rc != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_extended_operation" );
+ tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
rc = EXIT_FAILURE;
goto done;
}
rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
if ( rc < 0 ) {
- ldap_perror( ld, "ldappasswd: ldap_result" );
+ tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
return rc;
}
rc = ldap_parse_result( ld, res,
&code, &matcheddn, &text, &refs, NULL, 0 );
if( rc != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_parse_result" );
+ tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL );
rc = EXIT_FAILURE;
goto done;
}
rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
if( rc != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_parse_extended_result" );
+ tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
rc = EXIT_FAILURE;
goto done;
}
#include "common.h"
+#if !LDAP_DEPRECATED
+/*
+ * NOTE: we use this deprecated function only because
+ * we want ldapsearch to provide some client-side sorting
+ * capability.
+ */
+/* from ldap.h */
+typedef int (LDAP_SORT_AD_CMP_PROC) LDAP_P(( /* deprecated */
+ LDAP_CONST char *left,
+ LDAP_CONST char *right ));
+
+LDAP_F( int ) /* deprecated */
+ldap_sort_entries LDAP_P(( LDAP *ld,
+ LDAPMessage **chain,
+ LDAP_CONST char *attr,
+ LDAP_SORT_AD_CMP_PROC *cmp ));
+#endif
static int scope = LDAP_SCOPE_SUBTREE;
static int deref = -1;
done:
if ( rc == -1 ) {
- ldap_perror( ld, "ldap_result" );
+ tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
return( rc );
}
rc = ldap_get_entry_controls( ld, entry, &ctrls );
if( rc != LDAP_SUCCESS ) {
fprintf(stderr, _("print_entry: %d\n"), rc );
- ldap_perror( ld, "ldap_get_entry_controls" );
+ tool_perror( "ldap_get_entry_controls", rc, NULL, NULL, NULL, NULL );
exit( EXIT_FAILURE );
}
rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
if( rc != LDAP_SUCCESS ) {
- ldap_perror(ld, "ldap_parse_reference");
+ tool_perror( "ldap_parse_reference", rc, NULL, NULL, NULL, NULL );
exit( EXIT_FAILURE );
}
&retoid, &retdata, 0 );
if( rc != LDAP_SUCCESS ) {
- ldap_perror(ld, "ldap_parse_extended_result");
+ tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
exit( EXIT_FAILURE );
}
&retoid, &retdata, &ctrls, 0 );
if( rc != LDAP_SUCCESS ) {
- ldap_perror(ld, "ldap_parse_intermediate");
+ tool_perror( "ldap_parse_intermediate", rc, NULL, NULL, NULL, NULL );
exit( EXIT_FAILURE );
}
&err, &matcheddn, &text, &refs, &ctrls, 0 );
if( rc != LDAP_SUCCESS ) {
- ldap_perror(ld, "ldap_parse_result");
+ tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL );
exit( EXIT_FAILURE );
}
&err, NULL, NULL, NULL, &ctrl, 0 );
if( rc != LDAP_SUCCESS ) {
- ldap_perror(ld, "ldap_parse_result");
+ tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL );
exit( EXIT_FAILURE );
}
rc = ldap_whoami( ld, NULL, NULL, &id );
if( rc != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_extended_operation" );
+ tool_perror( "ldap_extended_operation", rc, NULL, NULL, NULL, NULL );
rc = EXIT_FAILURE;
goto skip;
}
rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, &tv, &res );
if ( rc < 0 ) {
- ldap_perror( ld, "ldapwhoami: ldap_result" );
+ tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
return rc;
}
rc = ldap_parse_result( ld, res,
&code, &matcheddn, &text, &refs, NULL, 0 );
- if( rc != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_parse_result" );
+ if ( rc == LDAP_SUCCESS ) {
+ rc = code;
+ }
+
+ if ( rc != LDAP_SUCCESS ) {
+ tool_perror( "ldap_parse_result", rc, NULL, matcheddn, text, refs );
rc = EXIT_FAILURE;
goto skip;
}
rc = ldap_parse_extended_result( ld, res, &retoid, &retdata, 1 );
if( rc != LDAP_SUCCESS ) {
- ldap_perror( ld, "ldap_parse_result" );
+ tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
rc = EXIT_FAILURE;
goto skip;
}