2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2010 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/stdlib.h>
22 #include <ac/socket.h>
23 #include <ac/string.h>
28 void ldap_int_error_init( void ) {
32 ldap_err2string( int err )
36 Debug( LDAP_DEBUG_TRACE, "ldap_err2string\n", 0, 0, 0 );
39 # define C(code, message) case code: m = message; break
41 /* LDAPv3 (RFC 4511) codes */
42 C(LDAP_SUCCESS, N_("Success"));
43 C(LDAP_OPERATIONS_ERROR, N_("Operations error"));
44 C(LDAP_PROTOCOL_ERROR, N_("Protocol error"));
45 C(LDAP_TIMELIMIT_EXCEEDED, N_("Time limit exceeded"));
46 C(LDAP_SIZELIMIT_EXCEEDED, N_("Size limit exceeded"));
47 C(LDAP_COMPARE_FALSE, N_("Compare False"));
48 C(LDAP_COMPARE_TRUE, N_("Compare True"));
49 C(LDAP_STRONG_AUTH_NOT_SUPPORTED,N_("Authentication method not supported"));
50 C(LDAP_STRONG_AUTH_REQUIRED, N_("Strong(er) authentication required"));
52 C(LDAP_REFERRAL, N_("Referral"));
53 C(LDAP_ADMINLIMIT_EXCEEDED, N_("Administrative limit exceeded"));
54 C(LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
55 N_("Critical extension is unavailable"));
56 C(LDAP_CONFIDENTIALITY_REQUIRED,N_("Confidentiality required"));
57 C(LDAP_SASL_BIND_IN_PROGRESS, N_("SASL bind in progress"));
59 C(LDAP_NO_SUCH_ATTRIBUTE, N_("No such attribute"));
60 C(LDAP_UNDEFINED_TYPE, N_("Undefined attribute type"));
61 C(LDAP_INAPPROPRIATE_MATCHING, N_("Inappropriate matching"));
62 C(LDAP_CONSTRAINT_VIOLATION, N_("Constraint violation"));
63 C(LDAP_TYPE_OR_VALUE_EXISTS, N_("Type or value exists"));
64 C(LDAP_INVALID_SYNTAX, N_("Invalid syntax"));
66 C(LDAP_NO_SUCH_OBJECT, N_("No such object"));
67 C(LDAP_ALIAS_PROBLEM, N_("Alias problem"));
68 C(LDAP_INVALID_DN_SYNTAX, N_("Invalid DN syntax"));
70 C(LDAP_ALIAS_DEREF_PROBLEM, N_("Alias dereferencing problem"));
72 C(LDAP_INAPPROPRIATE_AUTH, N_("Inappropriate authentication"));
73 C(LDAP_INVALID_CREDENTIALS, N_("Invalid credentials"));
74 C(LDAP_INSUFFICIENT_ACCESS, N_("Insufficient access"));
75 C(LDAP_BUSY, N_("Server is busy"));
76 C(LDAP_UNAVAILABLE, N_("Server is unavailable"));
77 C(LDAP_UNWILLING_TO_PERFORM, N_("Server is unwilling to perform"));
78 C(LDAP_LOOP_DETECT, N_("Loop detected"));
80 C(LDAP_NAMING_VIOLATION, N_("Naming violation"));
81 C(LDAP_OBJECT_CLASS_VIOLATION, N_("Object class violation"));
82 C(LDAP_NOT_ALLOWED_ON_NONLEAF, N_("Operation not allowed on non-leaf"));
83 C(LDAP_NOT_ALLOWED_ON_RDN, N_("Operation not allowed on RDN"));
84 C(LDAP_ALREADY_EXISTS, N_("Already exists"));
85 C(LDAP_NO_OBJECT_CLASS_MODS, N_("Cannot modify object class"));
87 C(LDAP_AFFECTS_MULTIPLE_DSAS, N_("Operation affects multiple DSAs"));
89 /* Virtual List View draft */
90 C(LDAP_VLV_ERROR, N_("Virtual List View error"));
92 C(LDAP_OTHER, N_("Other (e.g., implementation specific) error"));
94 /* LDAPv2 (RFC 1777) codes */
95 C(LDAP_PARTIAL_RESULTS, N_("Partial results and referral received"));
96 C(LDAP_IS_LEAF, N_("Entry is a leaf"));
98 /* Connection-less LDAP (CLDAP - RFC 1798) code */
99 C(LDAP_RESULTS_TOO_LARGE, N_("Results too large"));
101 /* Cancel Operation (RFC 3909) codes */
102 C(LDAP_CANCELLED, N_("Cancelled"));
103 C(LDAP_NO_SUCH_OPERATION, N_("No Operation to Cancel"));
104 C(LDAP_TOO_LATE, N_("Too Late to Cancel"));
105 C(LDAP_CANNOT_CANCEL, N_("Cannot Cancel"));
107 /* Assert Control (RFC 4528 and old internet-draft) codes */
108 C(LDAP_ASSERTION_FAILED, N_("Assertion Failed"));
109 C(LDAP_X_ASSERTION_FAILED, N_("Assertion Failed (X)"));
111 /* Proxied Authorization Control (RFC 4370 and I-D) codes */
112 C(LDAP_PROXIED_AUTHORIZATION_DENIED, N_("Proxied Authorization Denied"));
113 C(LDAP_X_PROXY_AUTHZ_FAILURE, N_("Proxy Authorization Failure (X)"));
115 /* Content Sync Operation (RFC 4533 and I-D) codes */
116 C(LDAP_SYNC_REFRESH_REQUIRED, N_("Content Sync Refresh Required"));
117 C(LDAP_X_SYNC_REFRESH_REQUIRED, N_("Content Sync Refresh Required (X)"));
119 /* No-Op Control (draft-zeilenga-ldap-noop) code */
120 C(LDAP_X_NO_OPERATION, N_("No Operation (X)"));
122 /* Client Update Protocol (RFC 3928) codes */
123 C(LDAP_CUP_RESOURCES_EXHAUSTED, N_("LCUP Resources Exhausted"));
124 C(LDAP_CUP_SECURITY_VIOLATION, N_("LCUP Security Violation"));
125 C(LDAP_CUP_INVALID_DATA, N_("LCUP Invalid Data"));
126 C(LDAP_CUP_UNSUPPORTED_SCHEME, N_("LCUP Unsupported Scheme"));
127 C(LDAP_CUP_RELOAD_REQUIRED, N_("LCUP Reload Required"));
130 /* Codes related to LDAP Transactions (draft-zeilenga-ldap-txn) */
131 C(LDAP_X_TXN_SPECIFY_OKAY, N_("TXN specify okay"));
132 C(LDAP_X_TXN_ID_INVALID, N_("TXN ID is invalid"));
135 /* API codes - renumbered since draft-ietf-ldapext-ldap-c-api */
136 C(LDAP_SERVER_DOWN, N_("Can't contact LDAP server"));
137 C(LDAP_LOCAL_ERROR, N_("Local error"));
138 C(LDAP_ENCODING_ERROR, N_("Encoding error"));
139 C(LDAP_DECODING_ERROR, N_("Decoding error"));
140 C(LDAP_TIMEOUT, N_("Timed out"));
141 C(LDAP_AUTH_UNKNOWN, N_("Unknown authentication method"));
142 C(LDAP_FILTER_ERROR, N_("Bad search filter"));
143 C(LDAP_USER_CANCELLED, N_("User cancelled operation"));
144 C(LDAP_PARAM_ERROR, N_("Bad parameter to an ldap routine"));
145 C(LDAP_NO_MEMORY, N_("Out of memory"));
146 C(LDAP_CONNECT_ERROR, N_("Connect error"));
147 C(LDAP_NOT_SUPPORTED, N_("Not Supported"));
148 C(LDAP_CONTROL_NOT_FOUND, N_("Control not found"));
149 C(LDAP_NO_RESULTS_RETURNED, N_("No results returned"));
150 C(LDAP_MORE_RESULTS_TO_RETURN, N_("More results to return"));
151 C(LDAP_CLIENT_LOOP, N_("Client Loop"));
152 C(LDAP_REFERRAL_LIMIT_EXCEEDED, N_("Referral Limit Exceeded"));
156 m = (LDAP_API_ERROR(err) ? N_("Unknown API error")
157 : LDAP_E_ERROR(err) ? N_("Unknown (extension) error")
158 : LDAP_X_ERROR(err) ? N_("Unknown (private extension) error")
159 : N_("Unknown error"));
168 ldap_perror( LDAP *ld, LDAP_CONST char *str )
172 assert( ld != NULL );
173 assert( LDAP_VALID( ld ) );
174 assert( str != NULL );
176 fprintf( stderr, "%s: %s (%d)\n",
177 str ? str : "ldap_perror",
178 ldap_err2string( ld->ld_errno ),
181 if ( ld->ld_matched != NULL && ld->ld_matched[0] != '\0' ) {
182 fprintf( stderr, _("\tmatched DN: %s\n"), ld->ld_matched );
185 if ( ld->ld_error != NULL && ld->ld_error[0] != '\0' ) {
186 fprintf( stderr, _("\tadditional info: %s\n"), ld->ld_error );
189 if ( ld->ld_referrals != NULL && ld->ld_referrals[0] != NULL) {
190 fprintf( stderr, _("\treferrals:\n") );
191 for (i=0; ld->ld_referrals[i]; i++) {
192 fprintf( stderr, _("\t\t%s\n"), ld->ld_referrals[i] );
201 ldap_result2error( LDAP *ld, LDAPMessage *r, int freeit )
205 rc = ldap_parse_result( ld, r, &err,
206 NULL, NULL, NULL, NULL, freeit );
208 return err != LDAP_SUCCESS ? err : rc;
212 * Parse LDAPResult Messages:
214 * LDAPResult ::= SEQUENCE {
215 * resultCode ENUMERATED,
217 * errorMessage LDAPString,
218 * referral [3] Referral OPTIONAL }
220 * including Bind results:
222 * BindResponse ::= [APPLICATION 1] SEQUENCE {
223 * COMPONENTS OF LDAPResult,
224 * serverSaslCreds [7] OCTET STRING OPTIONAL }
226 * and ExtendedOp results:
228 * ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
229 * COMPONENTS OF LDAPResult,
230 * responseName [10] LDAPOID OPTIONAL,
231 * response [11] OCTET STRING OPTIONAL }
242 LDAPControl ***serverctrls,
246 ber_int_t errcode = LDAP_SUCCESS;
251 Debug( LDAP_DEBUG_TRACE, "ldap_parse_result\n", 0, 0, 0 );
253 assert( ld != NULL );
254 assert( LDAP_VALID( ld ) );
257 if(errcodep != NULL) *errcodep = LDAP_SUCCESS;
258 if(matcheddnp != NULL) *matcheddnp = NULL;
259 if(errmsgp != NULL) *errmsgp = NULL;
260 if(referralsp != NULL) *referralsp = NULL;
261 if(serverctrls != NULL) *serverctrls = NULL;
263 #ifdef LDAP_R_COMPILE
264 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
266 /* Find the result, last msg in chain... */
267 lm = r->lm_chain_tail;
268 /* FIXME: either this is not possible (assert?)
269 * or it should be handled */
271 switch ( lm->lm_msgtype ) {
272 case LDAP_RES_SEARCH_ENTRY:
273 case LDAP_RES_SEARCH_REFERENCE:
274 case LDAP_RES_INTERMEDIATE:
284 ld->ld_errno = LDAP_NO_RESULTS_RETURNED;
285 #ifdef LDAP_R_COMPILE
286 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
291 if ( ld->ld_error ) {
292 LDAP_FREE( ld->ld_error );
295 if ( ld->ld_matched ) {
296 LDAP_FREE( ld->ld_matched );
297 ld->ld_matched = NULL;
299 if ( ld->ld_referrals ) {
300 LDAP_VFREE( ld->ld_referrals );
301 ld->ld_referrals = NULL;
306 ber = ber_dup( lm->lm_ber );
308 if ( ld->ld_version < LDAP_VERSION2 ) {
309 tag = ber_scanf( ber, "{iA}",
310 &ld->ld_errno, &ld->ld_error );
315 tag = ber_scanf( ber, "{iAA" /*}*/,
316 &ld->ld_errno, &ld->ld_matched, &ld->ld_error );
318 if( tag != LBER_ERROR ) {
319 /* peek for referrals */
320 if( ber_peek_tag(ber, &len) == LDAP_TAG_REFERRAL ) {
321 tag = ber_scanf( ber, "v", &ld->ld_referrals );
325 /* need to clean out misc items */
326 if( tag != LBER_ERROR ) {
327 if( lm->lm_msgtype == LDAP_RES_BIND ) {
328 /* look for sasl result creditials */
329 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SASL_RES_CREDS ) {
331 tag = ber_scanf( ber, "x" );
334 } else if( lm->lm_msgtype == LDAP_RES_EXTENDED ) {
335 /* look for exop result oid or value */
336 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_EXOP_RES_OID ) {
338 tag = ber_scanf( ber, "x" );
341 if ( tag != LBER_ERROR &&
342 ber_peek_tag( ber, &len ) == LDAP_TAG_EXOP_RES_VALUE )
345 tag = ber_scanf( ber, "x" );
350 if( tag != LBER_ERROR ) {
351 int rc = ldap_pvt_get_controls( ber, serverctrls );
353 if( rc != LDAP_SUCCESS ) {
358 if( tag != LBER_ERROR ) {
359 tag = ber_scanf( ber, /*{*/"}" );
363 if ( tag == LBER_ERROR ) {
364 ld->ld_errno = errcode = LDAP_DECODING_ERROR;
372 if( errcodep != NULL ) {
373 *errcodep = ld->ld_errno;
375 if ( errcode == LDAP_SUCCESS ) {
376 if( matcheddnp != NULL ) {
377 if ( ld->ld_matched )
379 *matcheddnp = LDAP_STRDUP( ld->ld_matched );
382 if( errmsgp != NULL ) {
385 *errmsgp = LDAP_STRDUP( ld->ld_error );
389 if( referralsp != NULL) {
390 *referralsp = ldap_value_dup( ld->ld_referrals );
397 #ifdef LDAP_R_COMPILE
398 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );