]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-common.c
fix previous (obviously broken) commit
[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 int ignore_server[ TESTER_SERVER_LAST ];
44 static int 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, ignore = 1;
130
131         if ( strcmp( err, "ALL" ) == 0 ) {
132                 for ( i = 0; ignore_str2err[ i ].name != NULL; i++ ) {
133                         int     err = ignore_str2err[ i ].err;
134
135                         if ( err > 0 ) {
136                                 ignore_server[ err ] = 1;
137
138                         } else if ( err < 0 ) {
139                                 ignore_client[ -err ] = 1;
140                         }
141                 }
142
143                 return 0;
144         }
145
146         if ( err[ 0 ] == '!' ) {
147                 ignore = 0;
148                 err++;
149
150         } else if ( err[ 0 ] == '*' ) {
151                 ignore = -1;
152                 err++;
153         }
154
155         for ( i = 0; ignore_str2err[ i ].name != NULL; i++ ) {
156                 if ( strcmp( err, ignore_str2err[ i ].name ) == 0 ) {
157                         int     err = ignore_str2err[ i ].err;
158
159                         if ( err > 0 ) {
160                                 ignore_server[ err ] = ignore;
161
162                         } else if ( err < 0 ) {
163                                 ignore_client[ -err ] = ignore;
164                         }
165
166                         return err;
167                 }
168         }
169
170         return UNKNOWN_ERR;
171 }
172
173 int
174 tester_ignore_str2errlist( const char *err )
175 {
176         int     i;
177         char    **errs = ldap_str2charray( err, "," );
178
179         for ( i = 0; errs[ i ] != NULL; i++ ) {
180                 /* TODO: allow <err>:<prog> to ignore <err> only when <prog> */
181                 (void)tester_ignore_str2err( errs[ i ] );
182         }
183
184         ldap_charray_free( errs );
185
186         return 0;
187 }
188
189 int
190 tester_ignore_err( int err )
191 {
192         int rc = 1;
193
194         if ( err > 0 ) {
195                 if ( err < TESTER_SERVER_LAST ) {
196                         rc = ignore_server[ err ];
197                         if ( rc > 0 ) {
198                                 ignore_server[ err ]++;
199
200                         } else if ( rc < 0 ) {
201                                 ignore_server[ err ]--;
202                         }
203                 }
204
205         } else if ( err < 0 ) {
206                 if ( -err < TESTER_CLIENT_LAST ) {
207                         rc = ignore_client[ -err ];
208                         if ( rc > 0 ) {
209                                 ignore_client[ -err ]++;
210
211                         } else if ( rc < 0 ) {
212                                 ignore_server[ err ]--;
213                         }
214                 }
215         }
216
217         /* SUCCESS is always "ignored" */
218         return rc;
219 }
220
221 void
222 tester_init( const char *pname, tester_t ptype )
223 {
224         pid = getpid();
225         srand( pid );
226         snprintf( progname, sizeof( progname ), "%s PID=%d", pname, pid );
227         progtype = ptype;
228 }
229
230 char *
231 tester_uri( char *uri, char *host, int port )
232 {
233         static char     uribuf[ BUFSIZ ];
234
235         if ( uri != NULL ) {
236                 return uri;
237         }
238
239         snprintf( uribuf, sizeof( uribuf ), "ldap://%s:%d", host, port );
240
241         return uribuf;
242 }
243
244 void
245 tester_ldap_error( LDAP *ld, const char *fname, const char *msg )
246 {
247         int             err;
248         char            *text = NULL;
249         LDAPControl     **ctrls = NULL;
250
251         ldap_get_option( ld, LDAP_OPT_RESULT_CODE, (void *)&err );
252         if ( err != LDAP_SUCCESS ) {
253                 ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void *)&text );
254         }
255
256         fprintf( stderr, "%s: %s: %s (%d) %s %s\n",
257                 progname, fname, ldap_err2string( err ), err,
258                 text == NULL ? "" : text,
259                 msg ? msg : "" );
260
261         if ( text ) {
262                 ldap_memfree( text );
263                 text = NULL;
264         }
265
266         ldap_get_option( ld, LDAP_OPT_MATCHED_DN, (void *)&text );
267         if ( text != NULL ) {
268                 if ( text[ 0 ] != '\0' ) {
269                         fprintf( stderr, "\tmatched: %s\n", text );
270                 }
271                 ldap_memfree( text );
272                 text = NULL;
273         }
274
275         ldap_get_option( ld, LDAP_OPT_SERVER_CONTROLS, (void *)&ctrls );
276         if ( ctrls != NULL ) {
277                 int     i;
278
279                 fprintf( stderr, "\tcontrols:\n" );
280                 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
281                         fprintf( stderr, "\t\t%s\n", ctrls[ i ]->ldctl_oid );
282                 }
283                 ldap_controls_free( ctrls );
284                 ctrls = NULL;
285         }
286
287         if ( err == LDAP_REFERRAL ) {
288                 char **refs = NULL;
289
290                 ldap_get_option( ld, LDAP_OPT_REFERRAL_URLS, (void *)&refs );
291
292                 if ( refs ) {
293                         int     i;
294
295                         fprintf( stderr, "\treferral:\n" );
296                         for ( i = 0; refs[ i ] != NULL; i++ ) {
297                                 fprintf( stderr, "\t\t%s\n", refs[ i ] );
298                         }
299
300                         ber_memvfree( (void **)refs );
301                 }
302         }
303 }
304
305 void
306 tester_perror( const char *fname, const char *msg )
307 {
308         int     save_errno = errno;
309         char    buf[ BUFSIZ ];
310
311         fprintf( stderr, "%s: %s: (%d) %s %s\n",
312                         progname, fname, save_errno,
313                         AC_STRERROR_R( save_errno, buf, sizeof( buf ) ),
314                         msg ? msg : "" );
315 }
316
317 void
318 tester_error( const char *msg )
319 {
320         fprintf( stderr, "%s: %s\n", progname, msg );
321 }
322