xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/error.c (revision 6cf6fe02a981b55727c49c3d37b0d8191a98c0ee)
1 /*	$NetBSD: error.c,v 1.1.1.4 2014/05/28 09:58:41 tron Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2014 The OpenLDAP Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 
18 #include "portable.h"
19 
20 #include <stdio.h>
21 
22 #include <ac/stdlib.h>
23 
24 #include <ac/socket.h>
25 #include <ac/string.h>
26 #include <ac/time.h>
27 
28 #include "ldap-int.h"
29 
30 void ldap_int_error_init( void ) {
31 }
32 
33 char *
34 ldap_err2string( int err )
35 {
36 	char *m;
37 
38 	Debug( LDAP_DEBUG_TRACE, "ldap_err2string\n", 0, 0, 0 );
39 
40 	switch ( err ) {
41 #	define C(code, message) case code: m = message; break
42 
43 	/* LDAPv3 (RFC 4511) codes */
44 	C(LDAP_SUCCESS,					N_("Success"));
45 	C(LDAP_OPERATIONS_ERROR, 		N_("Operations error"));
46 	C(LDAP_PROTOCOL_ERROR, 			N_("Protocol error"));
47 	C(LDAP_TIMELIMIT_EXCEEDED,		N_("Time limit exceeded"));
48 	C(LDAP_SIZELIMIT_EXCEEDED, 		N_("Size limit exceeded"));
49 	C(LDAP_COMPARE_FALSE, 			N_("Compare False"));
50 	C(LDAP_COMPARE_TRUE, 			N_("Compare True"));
51 	C(LDAP_STRONG_AUTH_NOT_SUPPORTED,N_("Authentication method not supported"));
52 	C(LDAP_STRONG_AUTH_REQUIRED,	N_("Strong(er) authentication required"));
53 
54 	C(LDAP_REFERRAL,				N_("Referral"));
55 	C(LDAP_ADMINLIMIT_EXCEEDED,		N_("Administrative limit exceeded"));
56 	C(LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
57 									N_("Critical extension is unavailable"));
58 	C(LDAP_CONFIDENTIALITY_REQUIRED,N_("Confidentiality required"));
59 	C(LDAP_SASL_BIND_IN_PROGRESS,	N_("SASL bind in progress"));
60 
61 	C(LDAP_NO_SUCH_ATTRIBUTE, 		N_("No such attribute"));
62 	C(LDAP_UNDEFINED_TYPE, 			N_("Undefined attribute type"));
63 	C(LDAP_INAPPROPRIATE_MATCHING, 	N_("Inappropriate matching"));
64 	C(LDAP_CONSTRAINT_VIOLATION, 	N_("Constraint violation"));
65 	C(LDAP_TYPE_OR_VALUE_EXISTS, 	N_("Type or value exists"));
66 	C(LDAP_INVALID_SYNTAX, 			N_("Invalid syntax"));
67 
68 	C(LDAP_NO_SUCH_OBJECT, 			N_("No such object"));
69 	C(LDAP_ALIAS_PROBLEM, 			N_("Alias problem"));
70 	C(LDAP_INVALID_DN_SYNTAX,		N_("Invalid DN syntax"));
71 
72 	C(LDAP_ALIAS_DEREF_PROBLEM,	 	N_("Alias dereferencing problem"));
73 
74 	C(LDAP_INAPPROPRIATE_AUTH, 		N_("Inappropriate authentication"));
75 	C(LDAP_INVALID_CREDENTIALS, 	N_("Invalid credentials"));
76 	C(LDAP_INSUFFICIENT_ACCESS, 	N_("Insufficient access"));
77 	C(LDAP_BUSY, 					N_("Server is busy"));
78 	C(LDAP_UNAVAILABLE, 			N_("Server is unavailable"));
79 	C(LDAP_UNWILLING_TO_PERFORM, 	N_("Server is unwilling to perform"));
80 	C(LDAP_LOOP_DETECT, 			N_("Loop detected"));
81 
82 	C(LDAP_NAMING_VIOLATION, 		N_("Naming violation"));
83 	C(LDAP_OBJECT_CLASS_VIOLATION, 	N_("Object class violation"));
84 	C(LDAP_NOT_ALLOWED_ON_NONLEAF, 	N_("Operation not allowed on non-leaf"));
85 	C(LDAP_NOT_ALLOWED_ON_RDN,	 	N_("Operation not allowed on RDN"));
86 	C(LDAP_ALREADY_EXISTS, 			N_("Already exists"));
87 	C(LDAP_NO_OBJECT_CLASS_MODS, 	N_("Cannot modify object class"));
88 
89 	C(LDAP_AFFECTS_MULTIPLE_DSAS,	N_("Operation affects multiple DSAs"));
90 
91 	/* Virtual List View draft */
92 	C(LDAP_VLV_ERROR,				N_("Virtual List View error"));
93 
94 	C(LDAP_OTHER, N_("Other (e.g., implementation specific) error"));
95 
96 	/* LDAPv2 (RFC 1777) codes */
97 	C(LDAP_PARTIAL_RESULTS, N_("Partial results and referral received"));
98 	C(LDAP_IS_LEAF, 				N_("Entry is a leaf"));
99 
100 	/* Connection-less LDAP (CLDAP - RFC 1798) code */
101 	C(LDAP_RESULTS_TOO_LARGE,		N_("Results too large"));
102 
103 	/* Cancel Operation (RFC 3909) codes */
104 	C(LDAP_CANCELLED,				N_("Cancelled"));
105 	C(LDAP_NO_SUCH_OPERATION,		N_("No Operation to Cancel"));
106 	C(LDAP_TOO_LATE,				N_("Too Late to Cancel"));
107 	C(LDAP_CANNOT_CANCEL,			N_("Cannot Cancel"));
108 
109 	/* Assert Control (RFC 4528 and old internet-draft) codes */
110 	C(LDAP_ASSERTION_FAILED,		N_("Assertion Failed"));
111 	C(LDAP_X_ASSERTION_FAILED,		N_("Assertion Failed (X)"));
112 
113 	/* Proxied Authorization Control (RFC 4370 and I-D) codes */
114 	C(LDAP_PROXIED_AUTHORIZATION_DENIED, N_("Proxied Authorization Denied"));
115 	C(LDAP_X_PROXY_AUTHZ_FAILURE,	N_("Proxy Authorization Failure (X)"));
116 
117 	/* Content Sync Operation (RFC 4533 and I-D) codes */
118 	C(LDAP_SYNC_REFRESH_REQUIRED,	N_("Content Sync Refresh Required"));
119 	C(LDAP_X_SYNC_REFRESH_REQUIRED,	N_("Content Sync Refresh Required (X)"));
120 
121 	/* No-Op Control (draft-zeilenga-ldap-noop) code */
122 	C(LDAP_X_NO_OPERATION,			N_("No Operation (X)"));
123 
124 	/* Client Update Protocol (RFC 3928) codes */
125 	C(LDAP_CUP_RESOURCES_EXHAUSTED,	N_("LCUP Resources Exhausted"));
126 	C(LDAP_CUP_SECURITY_VIOLATION,	N_("LCUP Security Violation"));
127 	C(LDAP_CUP_INVALID_DATA,		N_("LCUP Invalid Data"));
128 	C(LDAP_CUP_UNSUPPORTED_SCHEME,	N_("LCUP Unsupported Scheme"));
129 	C(LDAP_CUP_RELOAD_REQUIRED,		N_("LCUP Reload Required"));
130 
131 #ifdef LDAP_X_TXN
132 	/* Codes related to LDAP Transactions (draft-zeilenga-ldap-txn) */
133 	C(LDAP_X_TXN_SPECIFY_OKAY,		N_("TXN specify okay"));
134 	C(LDAP_X_TXN_ID_INVALID,		N_("TXN ID is invalid"));
135 #endif
136 
137 	/* API codes - renumbered since draft-ietf-ldapext-ldap-c-api */
138 	C(LDAP_SERVER_DOWN,				N_("Can't contact LDAP server"));
139 	C(LDAP_LOCAL_ERROR,				N_("Local error"));
140 	C(LDAP_ENCODING_ERROR,			N_("Encoding error"));
141 	C(LDAP_DECODING_ERROR,			N_("Decoding error"));
142 	C(LDAP_TIMEOUT,					N_("Timed out"));
143 	C(LDAP_AUTH_UNKNOWN,			N_("Unknown authentication method"));
144 	C(LDAP_FILTER_ERROR,			N_("Bad search filter"));
145 	C(LDAP_USER_CANCELLED,			N_("User cancelled operation"));
146 	C(LDAP_PARAM_ERROR,				N_("Bad parameter to an ldap routine"));
147 	C(LDAP_NO_MEMORY,				N_("Out of memory"));
148 	C(LDAP_CONNECT_ERROR,			N_("Connect error"));
149 	C(LDAP_NOT_SUPPORTED,			N_("Not Supported"));
150 	C(LDAP_CONTROL_NOT_FOUND,		N_("Control not found"));
151 	C(LDAP_NO_RESULTS_RETURNED,		N_("No results returned"));
152 	C(LDAP_MORE_RESULTS_TO_RETURN,	N_("More results to return"));
153 	C(LDAP_CLIENT_LOOP,				N_("Client Loop"));
154 	C(LDAP_REFERRAL_LIMIT_EXCEEDED,	N_("Referral Limit Exceeded"));
155 #	undef C
156 
157 	default:
158 		m = (LDAP_API_ERROR(err) ? N_("Unknown API error")
159 			 : LDAP_E_ERROR(err) ? N_("Unknown (extension) error")
160 			 : LDAP_X_ERROR(err) ? N_("Unknown (private extension) error")
161 			 : N_("Unknown error"));
162 		break;
163 	}
164 
165 	return _(m);
166 }
167 
168 /* deprecated */
169 void
170 ldap_perror( LDAP *ld, LDAP_CONST char *str )
171 {
172     int i;
173 
174 	assert( ld != NULL );
175 	assert( LDAP_VALID( ld ) );
176 	assert( str != NULL );
177 
178 	fprintf( stderr, "%s: %s (%d)\n",
179 		str ? str : "ldap_perror",
180 		ldap_err2string( ld->ld_errno ),
181 		ld->ld_errno );
182 
183 	if ( ld->ld_matched != NULL && ld->ld_matched[0] != '\0' ) {
184 		fprintf( stderr, _("\tmatched DN: %s\n"), ld->ld_matched );
185 	}
186 
187 	if ( ld->ld_error != NULL && ld->ld_error[0] != '\0' ) {
188 		fprintf( stderr, _("\tadditional info: %s\n"), ld->ld_error );
189 	}
190 
191 	if ( ld->ld_referrals != NULL && ld->ld_referrals[0] != NULL) {
192 		fprintf( stderr, _("\treferrals:\n") );
193 		for (i=0; ld->ld_referrals[i]; i++) {
194 			fprintf( stderr, _("\t\t%s\n"), ld->ld_referrals[i] );
195 		}
196 	}
197 
198 	fflush( stderr );
199 }
200 
201 /* deprecated */
202 int
203 ldap_result2error( LDAP *ld, LDAPMessage *r, int freeit )
204 {
205 	int rc, err;
206 
207 	rc = ldap_parse_result( ld, r, &err,
208 		NULL, NULL, NULL, NULL, freeit );
209 
210 	return err != LDAP_SUCCESS ? err : rc;
211 }
212 
213 /*
214  * Parse LDAPResult Messages:
215  *
216  *   LDAPResult ::= SEQUENCE {
217  *     resultCode      ENUMERATED,
218  *     matchedDN       LDAPDN,
219  *     errorMessage    LDAPString,
220  *     referral        [3] Referral OPTIONAL }
221  *
222  * including Bind results:
223  *
224  *   BindResponse ::= [APPLICATION 1] SEQUENCE {
225  *     COMPONENTS OF LDAPResult,
226  *     serverSaslCreds  [7] OCTET STRING OPTIONAL }
227  *
228  * and ExtendedOp results:
229  *
230  *   ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
231  *     COMPONENTS OF LDAPResult,
232  *     responseName     [10] LDAPOID OPTIONAL,
233  *     response         [11] OCTET STRING OPTIONAL }
234  *
235  */
236 int
237 ldap_parse_result(
238 	LDAP			*ld,
239 	LDAPMessage		*r,
240 	int				*errcodep,
241 	char			**matcheddnp,
242 	char			**errmsgp,
243 	char			***referralsp,
244 	LDAPControl		***serverctrls,
245 	int				freeit )
246 {
247 	LDAPMessage	*lm;
248 	ber_int_t errcode = LDAP_SUCCESS;
249 
250 	ber_tag_t tag;
251 	BerElement	*ber;
252 
253 	Debug( LDAP_DEBUG_TRACE, "ldap_parse_result\n", 0, 0, 0 );
254 
255 	assert( ld != NULL );
256 	assert( LDAP_VALID( ld ) );
257 	assert( r != NULL );
258 
259 	if(errcodep != NULL) *errcodep = LDAP_SUCCESS;
260 	if(matcheddnp != NULL) *matcheddnp = NULL;
261 	if(errmsgp != NULL) *errmsgp = NULL;
262 	if(referralsp != NULL) *referralsp = NULL;
263 	if(serverctrls != NULL) *serverctrls = NULL;
264 
265 	LDAP_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 */
270 	if ( lm != NULL ) {
271 		switch ( lm->lm_msgtype ) {
272 		case LDAP_RES_SEARCH_ENTRY:
273 		case LDAP_RES_SEARCH_REFERENCE:
274 		case LDAP_RES_INTERMEDIATE:
275 			lm = NULL;
276 			break;
277 
278 		default:
279 			break;
280 		}
281 	}
282 
283 	if( lm == NULL ) {
284 		errcode = ld->ld_errno = LDAP_NO_RESULTS_RETURNED;
285 		LDAP_MUTEX_UNLOCK( &ld->ld_res_mutex );
286 	    goto done;
287 	}
288 
289 	if ( ld->ld_error ) {
290 		LDAP_FREE( ld->ld_error );
291 		ld->ld_error = NULL;
292 	}
293 	if ( ld->ld_matched ) {
294 		LDAP_FREE( ld->ld_matched );
295 		ld->ld_matched = NULL;
296 	}
297 	if ( ld->ld_referrals ) {
298 		LDAP_VFREE( ld->ld_referrals );
299 		ld->ld_referrals = NULL;
300 	}
301 
302 	/* parse results */
303 
304 	ber = ber_dup( lm->lm_ber );
305 
306 	if ( ld->ld_version < LDAP_VERSION2 ) {
307 		tag = ber_scanf( ber, "{iA}",
308 			&ld->ld_errno, &ld->ld_error );
309 
310 	} else {
311 		ber_len_t len;
312 
313 		tag = ber_scanf( ber, "{iAA" /*}*/,
314 			&ld->ld_errno, &ld->ld_matched, &ld->ld_error );
315 
316 		if( tag != LBER_ERROR ) {
317 			/* peek for referrals */
318 			if( ber_peek_tag(ber, &len) == LDAP_TAG_REFERRAL ) {
319 				tag = ber_scanf( ber, "v", &ld->ld_referrals );
320 			}
321 		}
322 
323 		/* need to clean out misc items */
324 		if( tag != LBER_ERROR ) {
325 			if( lm->lm_msgtype == LDAP_RES_BIND ) {
326 				/* look for sasl result creditials */
327 				if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SASL_RES_CREDS ) {
328 					/* skip 'em */
329 					tag = ber_scanf( ber, "x" );
330 				}
331 
332 			} else if( lm->lm_msgtype == LDAP_RES_EXTENDED ) {
333 				/* look for exop result oid or value */
334 				if ( ber_peek_tag( ber, &len ) == LDAP_TAG_EXOP_RES_OID ) {
335 					/* skip 'em */
336 					tag = ber_scanf( ber, "x" );
337 				}
338 
339 				if ( tag != LBER_ERROR &&
340 					ber_peek_tag( ber, &len ) == LDAP_TAG_EXOP_RES_VALUE )
341 				{
342 					/* skip 'em */
343 					tag = ber_scanf( ber, "x" );
344 				}
345 			}
346 		}
347 
348 		if( tag != LBER_ERROR ) {
349 			int rc = ldap_pvt_get_controls( ber, serverctrls );
350 
351 			if( rc != LDAP_SUCCESS ) {
352 				tag = LBER_ERROR;
353 			}
354 		}
355 
356 		if( tag != LBER_ERROR ) {
357 			tag = ber_scanf( ber, /*{*/"}" );
358 		}
359 	}
360 
361 	if ( tag == LBER_ERROR ) {
362 		ld->ld_errno = errcode = LDAP_DECODING_ERROR;
363 	}
364 
365 	if( ber != NULL ) {
366 		ber_free( ber, 0 );
367 	}
368 
369 	/* return */
370 	if( errcodep != NULL ) {
371 		*errcodep = ld->ld_errno;
372 	}
373 	if ( errcode == LDAP_SUCCESS ) {
374 		if( matcheddnp != NULL ) {
375 			if ( ld->ld_matched )
376 			{
377 				*matcheddnp = LDAP_STRDUP( ld->ld_matched );
378 			}
379 		}
380 		if( errmsgp != NULL ) {
381 			if ( ld->ld_error )
382 			{
383 				*errmsgp = LDAP_STRDUP( ld->ld_error );
384 			}
385 		}
386 
387 		if( referralsp != NULL) {
388 			*referralsp = ldap_value_dup( ld->ld_referrals );
389 		}
390 	}
391 	LDAP_MUTEX_UNLOCK( &ld->ld_res_mutex );
392 
393 done:
394 	if ( freeit ) {
395 		ldap_msgfree( r );
396 	}
397 
398 	return errcode;
399 }
400