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