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