Back to index

openldap  2.4.31
slapd-common.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1999-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* ACKNOWLEDGEMENTS:
00016  * This work was initially developed by Howard Chu for inclusion
00017  * in OpenLDAP Software.
00018  */
00019 
00020 #include "portable.h"
00021 
00022 #include <stdio.h>
00023 
00024 #include "ac/stdlib.h"
00025 #include "ac/unistd.h"
00026 #include "ac/string.h"
00027 #include "ac/errno.h"
00028 
00029 #include "ldap.h"
00030 
00031 #include "ldap_pvt.h"
00032 #include "slapd-common.h"
00033 
00034 /* global vars */
00035 pid_t pid;
00036 
00037 /* static vars */
00038 static char progname[ BUFSIZ ];
00039 tester_t progtype;
00040 
00041 /*
00042  * ignore_count[] is indexed by result code:
00043  * negative for OpenLDAP client-side errors, positive for protocol codes.
00044  */
00045 #define       TESTER_CLIENT_FIRST  LDAP_REFERRAL_LIMIT_EXCEEDED /* negative */
00046 #define       TESTER_SERVER_LAST   LDAP_OTHER
00047 static int ignore_base      [ -TESTER_CLIENT_FIRST + TESTER_SERVER_LAST + 1 ];
00048 #define    ignore_count     (ignore_base - TESTER_CLIENT_FIRST)
00049 
00050 static const struct {
00051        const char *name;
00052        int    err;
00053 } ignore_str2err[] = {
00054        { "OPERATIONS_ERROR",              LDAP_OPERATIONS_ERROR },
00055        { "PROTOCOL_ERROR",         LDAP_PROTOCOL_ERROR },
00056        { "TIMELIMIT_EXCEEDED",            LDAP_TIMELIMIT_EXCEEDED },
00057        { "SIZELIMIT_EXCEEDED",            LDAP_SIZELIMIT_EXCEEDED },
00058        { "COMPARE_FALSE",          LDAP_COMPARE_FALSE },
00059        { "COMPARE_TRUE",           LDAP_COMPARE_TRUE },
00060        { "AUTH_METHOD_NOT_SUPPORTED",     LDAP_AUTH_METHOD_NOT_SUPPORTED },
00061        { "STRONG_AUTH_NOT_SUPPORTED",     LDAP_STRONG_AUTH_NOT_SUPPORTED },
00062        { "STRONG_AUTH_REQUIRED",   LDAP_STRONG_AUTH_REQUIRED },
00063        { "STRONGER_AUTH_REQUIRED", LDAP_STRONGER_AUTH_REQUIRED },
00064        { "PARTIAL_RESULTS",        LDAP_PARTIAL_RESULTS },
00065 
00066        { "REFERRAL",               LDAP_REFERRAL },
00067        { "ADMINLIMIT_EXCEEDED",    LDAP_ADMINLIMIT_EXCEEDED },
00068        { "UNAVAILABLE_CRITICAL_EXTENSION", LDAP_UNAVAILABLE_CRITICAL_EXTENSION },
00069        { "CONFIDENTIALITY_REQUIRED",      LDAP_CONFIDENTIALITY_REQUIRED },
00070        { "SASL_BIND_IN_PROGRESS",  LDAP_SASL_BIND_IN_PROGRESS },
00071 
00072        { "NO_SUCH_ATTRIBUTE",             LDAP_NO_SUCH_ATTRIBUTE },
00073        { "UNDEFINED_TYPE",         LDAP_UNDEFINED_TYPE },
00074        { "INAPPROPRIATE_MATCHING", LDAP_INAPPROPRIATE_MATCHING },
00075        { "CONSTRAINT_VIOLATION",   LDAP_CONSTRAINT_VIOLATION },
00076        { "TYPE_OR_VALUE_EXISTS",   LDAP_TYPE_OR_VALUE_EXISTS },
00077        { "INVALID_SYNTAX",         LDAP_INVALID_SYNTAX },
00078 
00079        { "NO_SUCH_OBJECT",         LDAP_NO_SUCH_OBJECT },
00080        { "ALIAS_PROBLEM",          LDAP_ALIAS_PROBLEM },
00081        { "INVALID_DN_SYNTAX",             LDAP_INVALID_DN_SYNTAX },
00082        { "IS_LEAF",                LDAP_IS_LEAF },
00083        { "ALIAS_DEREF_PROBLEM",    LDAP_ALIAS_DEREF_PROBLEM },
00084 
00085        /* obsolete */
00086        { "PROXY_AUTHZ_FAILURE",    LDAP_X_PROXY_AUTHZ_FAILURE },
00087        { "INAPPROPRIATE_AUTH",            LDAP_INAPPROPRIATE_AUTH },
00088        { "INVALID_CREDENTIALS",    LDAP_INVALID_CREDENTIALS },
00089        { "INSUFFICIENT_ACCESS",    LDAP_INSUFFICIENT_ACCESS },
00090 
00091        { "BUSY",                   LDAP_BUSY },
00092        { "UNAVAILABLE",            LDAP_UNAVAILABLE },
00093        { "UNWILLING_TO_PERFORM",   LDAP_UNWILLING_TO_PERFORM },
00094        { "LOOP_DETECT",            LDAP_LOOP_DETECT },
00095 
00096        { "NAMING_VIOLATION",              LDAP_NAMING_VIOLATION },
00097        { "OBJECT_CLASS_VIOLATION", LDAP_OBJECT_CLASS_VIOLATION },
00098        { "NOT_ALLOWED_ON_NONLEAF", LDAP_NOT_ALLOWED_ON_NONLEAF },
00099        { "NOT_ALLOWED_ON_RDN",            LDAP_NOT_ALLOWED_ON_RDN },
00100        { "ALREADY_EXISTS",         LDAP_ALREADY_EXISTS },
00101        { "NO_OBJECT_CLASS_MODS",   LDAP_NO_OBJECT_CLASS_MODS },
00102        { "RESULTS_TOO_LARGE",             LDAP_RESULTS_TOO_LARGE },
00103        { "AFFECTS_MULTIPLE_DSAS",  LDAP_AFFECTS_MULTIPLE_DSAS },
00104 
00105        { "OTHER",                  LDAP_OTHER },
00106 
00107        { "SERVER_DOWN",            LDAP_SERVER_DOWN },
00108        { "LOCAL_ERROR",            LDAP_LOCAL_ERROR },
00109        { "ENCODING_ERROR",         LDAP_ENCODING_ERROR },
00110        { "DECODING_ERROR",         LDAP_DECODING_ERROR },
00111        { "TIMEOUT",                LDAP_TIMEOUT },
00112        { "AUTH_UNKNOWN",           LDAP_AUTH_UNKNOWN },
00113        { "FILTER_ERROR",           LDAP_FILTER_ERROR },
00114        { "USER_CANCELLED",         LDAP_USER_CANCELLED },
00115        { "PARAM_ERROR",            LDAP_PARAM_ERROR },
00116        { "NO_MEMORY",                     LDAP_NO_MEMORY },
00117        { "CONNECT_ERROR",          LDAP_CONNECT_ERROR },
00118        { "NOT_SUPPORTED",          LDAP_NOT_SUPPORTED },
00119        { "CONTROL_NOT_FOUND",             LDAP_CONTROL_NOT_FOUND },
00120        { "NO_RESULTS_RETURNED",    LDAP_NO_RESULTS_RETURNED },
00121        { "MORE_RESULTS_TO_RETURN", LDAP_MORE_RESULTS_TO_RETURN },
00122        { "CLIENT_LOOP",            LDAP_CLIENT_LOOP },
00123        { "REFERRAL_LIMIT_EXCEEDED",       LDAP_REFERRAL_LIMIT_EXCEEDED },
00124 
00125        { NULL }
00126 };
00127 
00128 #define UNKNOWN_ERR  (1234567890)
00129 
00130 static int
00131 tester_ignore_str2err( const char *err )
00132 {
00133        int           i, ignore = 1;
00134 
00135        if ( strcmp( err, "ALL" ) == 0 ) {
00136               for ( i = 0; ignore_str2err[ i ].name != NULL; i++ ) {
00137                      ignore_count[ ignore_str2err[ i ].err ] = 1;
00138               }
00139               ignore_count[ LDAP_SUCCESS ] = 0;
00140 
00141               return 0;
00142        }
00143 
00144        if ( err[ 0 ] == '!' ) {
00145               ignore = 0;
00146               err++;
00147 
00148        } else if ( err[ 0 ] == '*' ) {
00149               ignore = -1;
00150               err++;
00151        }
00152 
00153        for ( i = 0; ignore_str2err[ i ].name != NULL; i++ ) {
00154               if ( strcmp( err, ignore_str2err[ i ].name ) == 0 ) {
00155                      int    err = ignore_str2err[ i ].err;
00156 
00157                      if ( err != LDAP_SUCCESS ) {
00158                             ignore_count[ err ] = ignore;
00159                      }
00160 
00161                      return err;
00162               }
00163        }
00164 
00165        return UNKNOWN_ERR;
00166 }
00167 
00168 int
00169 tester_ignore_str2errlist( const char *err )
00170 {
00171        int    i;
00172        char   **errs = ldap_str2charray( err, "," );
00173 
00174        for ( i = 0; errs[ i ] != NULL; i++ ) {
00175               /* TODO: allow <err>:<prog> to ignore <err> only when <prog> */
00176               (void)tester_ignore_str2err( errs[ i ] );
00177        }
00178 
00179        ldap_charray_free( errs );
00180 
00181        return 0;
00182 }
00183 
00184 int
00185 tester_ignore_err( int err )
00186 {
00187        int rc = 1;
00188 
00189        if ( err && TESTER_CLIENT_FIRST <= err && err <= TESTER_SERVER_LAST ) {
00190               rc = ignore_count[ err ];
00191               if ( rc != 0 ) {
00192                      ignore_count[ err ] = rc + (rc > 0 ? 1 : -1);
00193               }
00194        }
00195 
00196        /* SUCCESS is always "ignored" */
00197        return rc;
00198 }
00199 
00200 void
00201 tester_init( const char *pname, tester_t ptype )
00202 {
00203        pid = getpid();
00204        srand( pid );
00205        snprintf( progname, sizeof( progname ), "%s PID=%d", pname, pid );
00206        progtype = ptype;
00207 }
00208 
00209 char *
00210 tester_uri( char *uri, char *host, int port )
00211 {
00212        static char   uribuf[ BUFSIZ ];
00213 
00214        if ( uri != NULL ) {
00215               return uri;
00216        }
00217 
00218        snprintf( uribuf, sizeof( uribuf ), "ldap://%s:%d", host, port );
00219 
00220        return uribuf;
00221 }
00222 
00223 void
00224 tester_ldap_error( LDAP *ld, const char *fname, const char *msg )
00225 {
00226        int           err;
00227        char          *text = NULL;
00228        LDAPControl   **ctrls = NULL;
00229 
00230        ldap_get_option( ld, LDAP_OPT_RESULT_CODE, (void *)&err );
00231        if ( err != LDAP_SUCCESS ) {
00232               ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void *)&text );
00233        }
00234 
00235        fprintf( stderr, "%s: %s: %s (%d) %s %s\n",
00236               progname, fname, ldap_err2string( err ), err,
00237               text == NULL ? "" : text,
00238               msg ? msg : "" );
00239 
00240        if ( text ) {
00241               ldap_memfree( text );
00242               text = NULL;
00243        }
00244 
00245        ldap_get_option( ld, LDAP_OPT_MATCHED_DN, (void *)&text );
00246        if ( text != NULL ) {
00247               if ( text[ 0 ] != '\0' ) {
00248                      fprintf( stderr, "\tmatched: %s\n", text );
00249               }
00250               ldap_memfree( text );
00251               text = NULL;
00252        }
00253 
00254        ldap_get_option( ld, LDAP_OPT_SERVER_CONTROLS, (void *)&ctrls );
00255        if ( ctrls != NULL ) {
00256               int    i;
00257 
00258               fprintf( stderr, "\tcontrols:\n" );
00259               for ( i = 0; ctrls[ i ] != NULL; i++ ) {
00260                      fprintf( stderr, "\t\t%s\n", ctrls[ i ]->ldctl_oid );
00261               }
00262               ldap_controls_free( ctrls );
00263               ctrls = NULL;
00264        }
00265 
00266        if ( err == LDAP_REFERRAL ) {
00267               char **refs = NULL;
00268 
00269               ldap_get_option( ld, LDAP_OPT_REFERRAL_URLS, (void *)&refs );
00270 
00271               if ( refs ) {
00272                      int    i;
00273 
00274                      fprintf( stderr, "\treferral:\n" );
00275                      for ( i = 0; refs[ i ] != NULL; i++ ) {
00276                             fprintf( stderr, "\t\t%s\n", refs[ i ] );
00277                      }
00278 
00279                      ber_memvfree( (void **)refs );
00280               }
00281        }
00282 }
00283 
00284 void
00285 tester_perror( const char *fname, const char *msg )
00286 {
00287        int    save_errno = errno;
00288        char   buf[ BUFSIZ ];
00289 
00290        fprintf( stderr, "%s: %s: (%d) %s %s\n",
00291                      progname, fname, save_errno,
00292                      AC_STRERROR_R( save_errno, buf, sizeof( buf ) ),
00293                      msg ? msg : "" );
00294 }
00295 
00296 void
00297 tester_error( const char *msg )
00298 {
00299        fprintf( stderr, "%s: %s\n", progname, msg );
00300 }