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