]> git.sur5r.net Git - openldap/blob - libraries/libldap/error.c
Minor adjustments to the LBER_VFREE() & LDAP_VFREE macros.
[openldap] / libraries / libldap / error.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5
6 #include "portable.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include <ac/socket.h>
12 #include <ac/string.h>
13 #include <ac/time.h>
14
15 #include "ldap-int.h"
16
17 struct ldaperror {
18         int     e_code;
19         char    *e_reason;
20 };
21
22 static const struct ldaperror ldap_errlist[] = {
23         {LDAP_SUCCESS,                                  "Success" },
24         {LDAP_OPERATIONS_ERROR,                 "Operations error" },
25         {LDAP_PROTOCOL_ERROR,                   "Protocol error" },
26         {LDAP_TIMELIMIT_EXCEEDED,               "Time limit exceeded" },
27         {LDAP_SIZELIMIT_EXCEEDED,               "Size limit exceeded" },
28         {LDAP_COMPARE_FALSE,                    "Compare false" },
29         {LDAP_COMPARE_TRUE,                     "Compare true" },
30         {LDAP_STRONG_AUTH_NOT_SUPPORTED, "Strong authentication not supported" },
31         {LDAP_STRONG_AUTH_REQUIRED,     "Strong authentication required" },
32         {LDAP_PARTIAL_RESULTS,                  "Partial results and referral received" },
33
34         {LDAP_REFERRAL,                                 "Referral"},
35         {LDAP_ADMINLIMIT_EXCEEDED,              "Administrative limit exceeded"},
36         {LDAP_UNAVAILABLE_CRITICIAL_EXTENSION,
37                                                                         "Criticial extension is unavailable"},
38         {LDAP_CONFIDENTIALITY_REQUIRED, "Confidentiality required"},
39         {LDAP_SASL_BIND_IN_PROGRESS,    "SASL bind in progress"},
40
41         {LDAP_NO_SUCH_ATTRIBUTE,                "No such attribute" },
42         {LDAP_UNDEFINED_TYPE,                   "Undefined attribute type" },
43         {LDAP_INAPPROPRIATE_MATCHING,   "Inappropriate matching" },
44         {LDAP_CONSTRAINT_VIOLATION,     "Constraint violation" },
45         {LDAP_TYPE_OR_VALUE_EXISTS,     "Type or value exists" },
46         {LDAP_INVALID_SYNTAX,                   "Invalid syntax" },
47
48         {LDAP_NO_SUCH_OBJECT,                   "No such object" },
49         {LDAP_ALIAS_PROBLEM,                    "Alias problem" },
50         {LDAP_INVALID_DN_SYNTAX,                "Invalid DN syntax" },
51         {LDAP_IS_LEAF,                                  "Object is a leaf" },
52         {LDAP_ALIAS_DEREF_PROBLEM,              "Alias dereferencing problem" },
53
54         {LDAP_INAPPROPRIATE_AUTH,               "Inappropriate authentication" },
55         {LDAP_INVALID_CREDENTIALS,              "Invalid credentials" },
56         {LDAP_INSUFFICIENT_ACCESS,              "Insufficient access" },
57         {LDAP_BUSY,                                     "DSA is busy" },
58         {LDAP_UNAVAILABLE,                              "DSA is unavailable" },
59         {LDAP_UNWILLING_TO_PERFORM,     "DSA is unwilling to perform" },
60         {LDAP_LOOP_DETECT,                              "Loop detected" },
61
62         {LDAP_NAMING_VIOLATION,                 "Naming violation" },
63         {LDAP_OBJECT_CLASS_VIOLATION,   "Object class violation" },
64         {LDAP_NOT_ALLOWED_ON_NONLEAF,   "Operation not allowed on nonleaf" },
65         {LDAP_NOT_ALLOWED_ON_RDN,               "Operation not allowed on RDN" },
66         {LDAP_ALREADY_EXISTS,                   "Already exists" },
67         {LDAP_NO_OBJECT_CLASS_MODS,     "Cannot modify object class" },
68         {LDAP_RESULTS_TOO_LARGE,                "Results too large" },
69         {LDAP_AFFECTS_MULTIPLE_DSAS,    "Operation affects multiple DSAs" },
70
71         {LDAP_OTHER,                                    "Unknown error" },
72         {LDAP_SERVER_DOWN,                              "Can't contact LDAP server" },
73         {LDAP_LOCAL_ERROR,                              "Local error" },
74         {LDAP_ENCODING_ERROR,                   "Encoding error" },
75         {LDAP_DECODING_ERROR,                   "Decoding error" },
76         {LDAP_TIMEOUT,                                  "Timed out" },
77         {LDAP_AUTH_UNKNOWN,                             "Unknown authentication method" },
78         {LDAP_FILTER_ERROR,                             "Bad search filter" },
79         {LDAP_USER_CANCELLED,                   "User cancelled operation" },
80         {LDAP_PARAM_ERROR,                              "Bad parameter to an ldap routine" },
81         {LDAP_NO_MEMORY,                                "Out of memory" },
82
83         {LDAP_CONNECT_ERROR,                    "Connect error" },
84         {LDAP_NOT_SUPPORTED,                    "Not Supported" },
85         {LDAP_CONTROL_NOT_FOUND,                "Control not found" },
86         {LDAP_NO_RESULTS_RETURNED,              "No results returned" },
87         {LDAP_MORE_RESULTS_TO_RETURN,   "More results to return" },
88         {LDAP_CLIENT_LOOP,                              "Client Loop" },
89         {LDAP_REFERRAL_LIMIT_EXCEEDED,  "Referral Limit Exceeded" },
90
91         {-1, 0 }
92 };
93
94 static struct ldaperror *ldap_int_error( int err )
95 {
96         int     i;
97
98         for ( i = 0; ldap_errlist[i].e_code != -1; i++ ) {
99                 if ( err == ldap_errlist[i].e_code )
100                         return (struct ldaperror *) &ldap_errlist[i];
101         }
102
103         return NULL;
104 }
105
106 char *
107 ldap_err2string( int err )
108 {
109         struct ldaperror *e;
110         
111         Debug( LDAP_DEBUG_TRACE, "ldap_err2string\n", 0, 0, 0 );
112
113         e = ldap_int_error( err );
114
115         return ( e != NULL ) ? e->e_reason : "Unknown error";
116 }
117
118 /* deprecated */
119 void
120 ldap_perror( LDAP *ld, LDAP_CONST char *str )
121 {
122         char *s;
123         struct ldaperror *e;
124         Debug( LDAP_DEBUG_TRACE, "ldap_perror\n", 0, 0, 0 );
125
126         assert( ld != NULL );
127         assert( LDAP_VALID( ld ) );
128         assert( s );
129
130         s = ( str != NULL ) ? (char *) str : "ldap_perror";
131
132         if ( ld == NULL ) {
133                 perror( s );
134                 return;
135         }
136
137         e = ldap_int_error( ld->ld_errno );
138
139         if ( e != NULL ) {
140                 fprintf( stderr, "%s: %s\n",
141                         s, e->e_reason );
142         } else {
143                 fprintf( stderr, "%s: unknown LDAP error number %d\n",
144                         s, ld->ld_errno );
145         }
146
147         if ( ld->ld_matched != NULL ) {
148                 fprintf( stderr, "\tmatched: \"%s\"\n",
149                         ld->ld_matched );
150         }
151
152         if ( ld->ld_error != NULL ) {
153                 fprintf( stderr, "\tadditional info: %s\n",
154                     ld->ld_error );
155         }
156
157         fflush( stderr );
158 }
159
160 int
161 ldap_result2error( LDAP *ld, LDAPMessage *r, int freeit )
162 {
163         LDAPMessage     *lm;
164         BerElement      ber;
165         long            along;
166         unsigned long   rc;
167
168         Debug( LDAP_DEBUG_TRACE, "ldap_result2error\n", 0, 0, 0 );
169
170         assert( ld != NULL );
171         assert( LDAP_VALID( ld ) );
172         assert( r != NULL );
173
174         if ( ld == NULL || r == NULLMSG )
175                 return( LDAP_PARAM_ERROR );
176
177         for ( lm = r; lm->lm_chain != NULL; lm = lm->lm_chain )
178                 ;       /* NULL */
179
180         if ( ld->ld_error ) {
181                 LDAP_FREE( ld->ld_error );
182                 ld->ld_error = NULL;
183         }
184         if ( ld->ld_matched ) {
185                 LDAP_FREE( ld->ld_matched );
186                 ld->ld_matched = NULL;
187         }
188
189         ber = *(lm->lm_ber);
190
191         if ( ld->ld_version < LDAP_VERSION2 ) {
192                 rc = ber_scanf( &ber, "{ia}", &along, &ld->ld_error );
193         } else {
194                 rc = ber_scanf( &ber, "{iaa}", &along, &ld->ld_matched,
195                     &ld->ld_error );
196         }
197
198         if ( rc == LBER_ERROR ) {
199                 ld->ld_errno = LDAP_DECODING_ERROR;
200         } else {
201                 ld->ld_errno = (int) along;
202         }
203
204         if ( freeit )
205                 ldap_msgfree( r );
206
207         return( ld->ld_errno );
208 }