]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-common.c
prefix '*' to only log first occurrence of ignored error
[openldap] / tests / progs / slapd-common.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2009 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 file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* ACKNOWLEDGEMENTS:
16  * This work was initially developed by Howard Chu for inclusion
17  * in OpenLDAP Software.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include "ac/stdlib.h"
25 #include "ac/unistd.h"
26 #include "ac/string.h"
27 #include "ac/errno.h"
28
29 #include "ldap.h"
30
31 #include "ldap_pvt.h"
32 #include "slapd-common.h"
33
34 /* global vars */
35 pid_t pid;
36
37 /* static vars */
38 static char progname[ BUFSIZ ];
39 tester_t progtype;
40
41 #define TESTER_SERVER_LAST      (LDAP_OTHER + 1)
42 #define TESTER_CLIENT_LAST      (- LDAP_REFERRAL_LIMIT_EXCEEDED + 1)
43 static unsigned ignore_server[ TESTER_SERVER_LAST ];
44 static unsigned ignore_client[ TESTER_CLIENT_LAST ];
45
46 static struct {
47         char    *name;
48         int     err;
49 } ignore_str2err[] = {
50         { "OPERATIONS_ERROR",           LDAP_OPERATIONS_ERROR },
51         { "PROTOCOL_ERROR",             LDAP_PROTOCOL_ERROR },
52         { "TIMELIMIT_EXCEEDED",         LDAP_TIMELIMIT_EXCEEDED },
53         { "SIZELIMIT_EXCEEDED",         LDAP_SIZELIMIT_EXCEEDED },
54         { "COMPARE_FALSE",              LDAP_COMPARE_FALSE },
55         { "COMPARE_TRUE",               LDAP_COMPARE_TRUE },
56         { "AUTH_METHOD_NOT_SUPPORTED",  LDAP_AUTH_METHOD_NOT_SUPPORTED },
57         { "STRONG_AUTH_NOT_SUPPORTED",  LDAP_STRONG_AUTH_NOT_SUPPORTED },
58         { "STRONG_AUTH_REQUIRED",       LDAP_STRONG_AUTH_REQUIRED },
59         { "STRONGER_AUTH_REQUIRED",     LDAP_STRONGER_AUTH_REQUIRED },
60         { "PARTIAL_RESULTS",            LDAP_PARTIAL_RESULTS },
61
62         { "REFERRAL",                   LDAP_REFERRAL },
63         { "ADMINLIMIT_EXCEEDED",        LDAP_ADMINLIMIT_EXCEEDED },
64         { "UNAVAILABLE_CRITICAL_EXTENSION", LDAP_UNAVAILABLE_CRITICAL_EXTENSION },
65         { "CONFIDENTIALITY_REQUIRED",   LDAP_CONFIDENTIALITY_REQUIRED },
66         { "SASL_BIND_IN_PROGRESS",      LDAP_SASL_BIND_IN_PROGRESS },
67
68         { "NO_SUCH_ATTRIBUTE",          LDAP_NO_SUCH_ATTRIBUTE },
69         { "UNDEFINED_TYPE",             LDAP_UNDEFINED_TYPE },
70         { "INAPPROPRIATE_MATCHING",     LDAP_INAPPROPRIATE_MATCHING },
71         { "CONSTRAINT_VIOLATION",       LDAP_CONSTRAINT_VIOLATION },
72         { "TYPE_OR_VALUE_EXISTS",       LDAP_TYPE_OR_VALUE_EXISTS },
73         { "INVALID_SYNTAX",             LDAP_INVALID_SYNTAX },
74
75         { "NO_SUCH_OBJECT",             LDAP_NO_SUCH_OBJECT },
76         { "ALIAS_PROBLEM",              LDAP_ALIAS_PROBLEM },
77         { "INVALID_DN_SYNTAX",          LDAP_INVALID_DN_SYNTAX },
78         { "IS_LEAF",                    LDAP_IS_LEAF },
79         { "ALIAS_DEREF_PROBLEM",        LDAP_ALIAS_DEREF_PROBLEM },
80
81         /* obsolete */
82         { "PROXY_AUTHZ_FAILURE",        LDAP_X_PROXY_AUTHZ_FAILURE },
83         { "INAPPROPRIATE_AUTH",         LDAP_INAPPROPRIATE_AUTH },
84         { "INVALID_CREDENTIALS",        LDAP_INVALID_CREDENTIALS },
85         { "INSUFFICIENT_ACCESS",        LDAP_INSUFFICIENT_ACCESS },
86
87         { "BUSY",                       LDAP_BUSY },
88         { "UNAVAILABLE",                LDAP_UNAVAILABLE },
89         { "UNWILLING_TO_PERFORM",       LDAP_UNWILLING_TO_PERFORM },
90         { "LOOP_DETECT",                LDAP_LOOP_DETECT },
91
92         { "NAMING_VIOLATION",           LDAP_NAMING_VIOLATION },
93         { "OBJECT_CLASS_VIOLATION",     LDAP_OBJECT_CLASS_VIOLATION },
94         { "NOT_ALLOWED_ON_NONLEAF",     LDAP_NOT_ALLOWED_ON_NONLEAF },
95         { "NOT_ALLOWED_ON_RDN",         LDAP_NOT_ALLOWED_ON_RDN },
96         { "ALREADY_EXISTS",             LDAP_ALREADY_EXISTS },
97         { "NO_OBJECT_CLASS_MODS",       LDAP_NO_OBJECT_CLASS_MODS },
98         { "RESULTS_TOO_LARGE",          LDAP_RESULTS_TOO_LARGE },
99         { "AFFECTS_MULTIPLE_DSAS",      LDAP_AFFECTS_MULTIPLE_DSAS },
100
101         { "OTHER",                      LDAP_OTHER },
102
103         { "SERVER_DOWN",                LDAP_SERVER_DOWN },
104         { "LOCAL_ERROR",                LDAP_LOCAL_ERROR },
105         { "ENCODING_ERROR",             LDAP_ENCODING_ERROR },
106         { "DECODING_ERROR",             LDAP_DECODING_ERROR },
107         { "TIMEOUT",                    LDAP_TIMEOUT },
108         { "AUTH_UNKNOWN",               LDAP_AUTH_UNKNOWN },
109         { "FILTER_ERROR",               LDAP_FILTER_ERROR },
110         { "USER_CANCELLED",             LDAP_USER_CANCELLED },
111         { "PARAM_ERROR",                LDAP_PARAM_ERROR },
112         { "NO_MEMORY",                  LDAP_NO_MEMORY },
113         { "CONNECT_ERROR",              LDAP_CONNECT_ERROR },
114         { "NOT_SUPPORTED",              LDAP_NOT_SUPPORTED },
115         { "CONTROL_NOT_FOUND",          LDAP_CONTROL_NOT_FOUND },
116         { "NO_RESULTS_RETURNED",        LDAP_NO_RESULTS_RETURNED },
117         { "MORE_RESULTS_TO_RETURN",     LDAP_MORE_RESULTS_TO_RETURN },
118         { "CLIENT_LOOP",                LDAP_CLIENT_LOOP },
119         { "REFERRAL_LIMIT_EXCEEDED",    LDAP_REFERRAL_LIMIT_EXCEEDED },
120
121         { NULL }
122 };
123
124 #define UNKNOWN_ERR     (1234567890)
125
126 static int
127 tester_ignore_str2err( const char *err )
128 {
129         int             i;
130         unsigned        ignore = 1;
131
132         if ( strcmp( err, "ALL" ) == 0 ) {
133                 for ( i = 0; ignore_str2err[ i ].name != NULL; i++ ) {
134                         int     err = ignore_str2err[ i ].err;
135
136                         if ( err > 0 ) {
137                                 ignore_server[ err ] = 1;
138
139                         } else if ( err < 0 ) {
140                                 ignore_client[ -err ] = 1;
141                         }
142                 }
143
144                 return 0;
145         }
146
147         if ( err[ 0 ] == '!' ) {
148                 ignore = 0;
149                 err++;
150
151         } else if ( err[ 0 ] == '*' ) {
152                 ignore = -1;
153                 err++;
154         }
155
156         for ( i = 0; ignore_str2err[ i ].name != NULL; i++ ) {
157                 if ( strcmp( err, ignore_str2err[ i ].name ) == 0 ) {
158                         int     err = ignore_str2err[ i ].err;
159
160                         if ( err > 0 ) {
161                                 ignore_server[ err ] = ignore;
162
163                         } else if ( err < 0 ) {
164                                 ignore_client[ -err ] = ignore;
165                         }
166
167                         return err;
168                 }
169         }
170
171         return UNKNOWN_ERR;
172 }
173
174 int
175 tester_ignore_str2errlist( const char *err )
176 {
177         int     i;
178         char    **errs = ldap_str2charray( err, "," );
179
180         for ( i = 0; errs[ i ] != NULL; i++ ) {
181                 /* TODO: allow <err>:<prog> to ignore <err> only when <prog> */
182                 (void)tester_ignore_str2err( errs[ i ] );
183         }
184
185         ldap_charray_free( errs );
186
187         return 0;
188 }
189
190 unsigned
191 tester_ignore_err( int err )
192 {
193         unsigned        rc = 1;
194
195         if ( err > 0 ) {
196                 if ( err < TESTER_SERVER_LAST ) {
197                         rc = ignore_server[ err ];
198                         if ( rc > 0 ) {
199                                 ignore_server[ err ]++;
200
201                         } else if ( rc < 0 ) {
202                                 ignore_server[ err ]--;
203                         }
204                 }
205
206         } else if ( err < 0 ) {
207                 if ( -err < TESTER_CLIENT_LAST ) {
208                         rc = ignore_client[ -err ];
209                         if ( rc > 0 ) {
210                                 ignore_client[ -err ]++;
211
212                         } else if ( rc < 0 ) {
213                                 ignore_server[ err ]--;
214                         }
215                 }
216         }
217
218         /* SUCCESS is always "ignored" */
219         return rc;
220 }
221
222 void
223 tester_init( const char *pname, tester_t ptype )
224 {
225         pid = getpid();
226         srand( pid );
227         snprintf( progname, sizeof( progname ), "%s PID=%d", pname, pid );
228         progtype = ptype;
229 }
230
231 char *
232 tester_uri( char *uri, char *host, int port )
233 {
234         static char     uribuf[ BUFSIZ ];
235
236         if ( uri != NULL ) {
237                 return uri;
238         }
239
240         snprintf( uribuf, sizeof( uribuf ), "ldap://%s:%d", host, port );
241
242         return uribuf;
243 }
244
245 void
246 tester_ldap_error( LDAP *ld, const char *fname, const char *msg )
247 {
248         int             err;
249         char            *text = NULL;
250         LDAPControl     **ctrls = NULL;
251
252         ldap_get_option( ld, LDAP_OPT_RESULT_CODE, (void *)&err );
253         if ( err != LDAP_SUCCESS ) {
254                 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void *)&text );
255         }
256
257         fprintf( stderr, "%s: %s: %s (%d) %s %s\n",
258                 progname, fname, ldap_err2string( err ), err,
259                 text == NULL ? "" : text,
260                 msg ? msg : "" );
261
262         if ( text ) {
263                 ldap_memfree( text );
264                 text = NULL;
265         }
266
267         ldap_get_option( ld, LDAP_OPT_MATCHED_DN, (void *)&text );
268         if ( text != NULL ) {
269                 if ( text[ 0 ] != '\0' ) {
270                         fprintf( stderr, "\tmatched: %s\n", text );
271                 }
272                 ldap_memfree( text );
273                 text = NULL;
274         }
275
276         ldap_get_option( ld, LDAP_OPT_SERVER_CONTROLS, (void *)&ctrls );
277         if ( ctrls != NULL ) {
278                 int     i;
279
280                 fprintf( stderr, "\tcontrols:\n" );
281                 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
282                         fprintf( stderr, "\t\t%s\n", ctrls[ i ]->ldctl_oid );
283                 }
284                 ldap_controls_free( ctrls );
285                 ctrls = NULL;
286         }
287
288         if ( err == LDAP_REFERRAL ) {
289                 char **refs = NULL;
290
291                 ldap_get_option( ld, LDAP_OPT_REFERRAL_URLS, (void *)&refs );
292
293                 if ( refs ) {
294                         int     i;
295
296                         fprintf( stderr, "\treferral:\n" );
297                         for ( i = 0; refs[ i ] != NULL; i++ ) {
298                                 fprintf( stderr, "\t\t%s\n", refs[ i ] );
299                         }
300
301                         ber_memvfree( (void **)refs );
302                 }
303         }
304 }
305
306 void
307 tester_perror( const char *fname, const char *msg )
308 {
309         int     save_errno = errno;
310         char    buf[ BUFSIZ ];
311
312         fprintf( stderr, "%s: %s: (%d) %s %s\n",
313                         progname, fname, save_errno,
314                         AC_STRERROR_R( save_errno, buf, sizeof( buf ) ),
315                         msg ? msg : "" );
316 }
317
318 void
319 tester_error( const char *msg )
320 {
321         fprintf( stderr, "%s: %s\n", progname, msg );
322 }
323