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