Back to index

openldap  2.4.31
search.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 1998-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 the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
00016  * All rights reserved.
00017  *
00018  * Redistribution and use in source and binary forms are permitted
00019  * provided that this notice is preserved and that due credit is given
00020  * to the University of Michigan at Ann Arbor. The name of the University
00021  * may not be used to endorse or promote products derived from this
00022  * software without specific prior written permission. This software
00023  * is provided ``as is'' without express or implied warranty.
00024  */
00025 
00026 #include "portable.h"
00027 
00028 #include <stdio.h>
00029 
00030 #include <ac/string.h>
00031 #include <ac/socket.h>
00032 
00033 #include "lutil.h"
00034 #include "slap.h"
00035 
00036 int
00037 do_search(
00038     Operation *op,   /* info about the op to which we're responding */
00039     SlapReply *rs    /* all the response data we'll send */ )
00040 {
00041        struct berval base = BER_BVNULL;
00042        ber_len_t     siz, off, i;
00043 
00044        Debug( LDAP_DEBUG_TRACE, "%s do_search\n",
00045               op->o_log_prefix, 0, 0 );
00046        /*
00047         * Parse the search request.  It looks like this:
00048         *
00049         *     SearchRequest := [APPLICATION 3] SEQUENCE {
00050         *            baseObject    DistinguishedName,
00051         *            scope         ENUMERATED {
00052         *                   baseObject    (0),
00053         *                   singleLevel   (1),
00054         *                   wholeSubtree (2),
00055         *          subordinate (3)  -- OpenLDAP extension
00056         *            },
00057         *            derefAliases  ENUMERATED {
00058         *                   neverDerefaliases    (0),
00059         *                   derefInSearching     (1),
00060         *                   derefFindingBaseObj  (2),
00061         *                   alwaysDerefAliases   (3)
00062         *            },
00063         *            sizelimit     INTEGER (0 .. 65535),
00064         *            timelimit     INTEGER (0 .. 65535),
00065         *            attrsOnly     BOOLEAN,
00066         *            filter        Filter,
00067         *            attributes    SEQUENCE OF AttributeType
00068         *     }
00069         */
00070 
00071        /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */
00072        if ( ber_scanf( op->o_ber, "{miiiib" /*}*/,
00073               &base, &op->ors_scope, &op->ors_deref, &op->ors_slimit,
00074            &op->ors_tlimit, &op->ors_attrsonly ) == LBER_ERROR )
00075        {
00076               send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
00077               rs->sr_err = SLAPD_DISCONNECT;
00078               goto return_results;
00079        }
00080 
00081        if ( op->ors_tlimit < 0 || op->ors_tlimit > SLAP_MAX_LIMIT ) {
00082               send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid time limit" );
00083               goto return_results;
00084        }
00085 
00086        if ( op->ors_slimit < 0 || op->ors_slimit > SLAP_MAX_LIMIT ) {
00087               send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid size limit" );
00088               goto return_results;
00089        }
00090 
00091        switch( op->ors_scope ) {
00092        case LDAP_SCOPE_BASE:
00093        case LDAP_SCOPE_ONELEVEL:
00094        case LDAP_SCOPE_SUBTREE:
00095        case LDAP_SCOPE_SUBORDINATE:
00096               break;
00097        default:
00098               send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid scope" );
00099               goto return_results;
00100        }
00101 
00102        switch( op->ors_deref ) {
00103        case LDAP_DEREF_NEVER:
00104        case LDAP_DEREF_FINDING:
00105        case LDAP_DEREF_SEARCHING:
00106        case LDAP_DEREF_ALWAYS:
00107               break;
00108        default:
00109               send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid deref" );
00110               goto return_results;
00111        }
00112 
00113        rs->sr_err = dnPrettyNormal( NULL, &base, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
00114        if( rs->sr_err != LDAP_SUCCESS ) {
00115               Debug( LDAP_DEBUG_ANY, "%s do_search: invalid dn: \"%s\"\n",
00116                      op->o_log_prefix, base.bv_val, 0 );
00117               send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
00118               goto return_results;
00119        }
00120 
00121        Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d",
00122               base.bv_val, op->ors_scope, op->ors_deref );
00123        Debug( LDAP_DEBUG_ARGS, "    %d %d %d\n",
00124               op->ors_slimit, op->ors_tlimit, op->ors_attrsonly);
00125 
00126        /* filter - returns a "normalized" version */
00127        rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text );
00128        if( rs->sr_err != LDAP_SUCCESS ) {
00129               if( rs->sr_err == SLAPD_DISCONNECT ) {
00130                      rs->sr_err = LDAP_PROTOCOL_ERROR;
00131                      send_ldap_disconnect( op, rs );
00132                      rs->sr_err = SLAPD_DISCONNECT;
00133               } else {
00134                      send_ldap_result( op, rs );
00135               }
00136               goto return_results;
00137        }
00138        filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
00139        
00140        Debug( LDAP_DEBUG_ARGS, "    filter: %s\n",
00141               !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 );
00142 
00143        /* attributes */
00144        siz = sizeof(AttributeName);
00145        off = offsetof(AttributeName,an_name);
00146        if ( ber_scanf( op->o_ber, "{M}}", &op->ors_attrs, &siz, off ) == LBER_ERROR ) {
00147               send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding attrs error" );
00148               rs->sr_err = SLAPD_DISCONNECT;
00149               goto return_results;
00150        }
00151        for ( i=0; i<siz; i++ ) {
00152               const char *dummy;   /* ignore msgs from bv2ad */
00153               op->ors_attrs[i].an_desc = NULL;
00154               op->ors_attrs[i].an_oc = NULL;
00155               op->ors_attrs[i].an_flags = 0;
00156               if ( slap_bv2ad( &op->ors_attrs[i].an_name,
00157                      &op->ors_attrs[i].an_desc, &dummy ) != LDAP_SUCCESS )
00158               {
00159                      if ( slap_bv2undef_ad( &op->ors_attrs[i].an_name,
00160                             &op->ors_attrs[i].an_desc, &dummy,
00161                             SLAP_AD_PROXIED|SLAP_AD_NOINSERT ) )
00162                      {
00163                             struct berval *bv = &op->ors_attrs[i].an_name;
00164 
00165                             /* RFC 4511 LDAPv3: All User Attributes */
00166                             if ( bvmatch( bv, slap_bv_all_user_attrs ) ) {
00167                                    continue;
00168                             }
00169 
00170                             /* RFC 3673 LDAPv3: All Operational Attributes */
00171                             if ( bvmatch( bv, slap_bv_all_operational_attrs ) ) {
00172                                    continue;
00173                             }
00174 
00175                             /* RFC 4529 LDAP: Requesting Attributes by Object Class */
00176                             if ( bv->bv_len > 1 && bv->bv_val[0] == '@' ) {
00177                                    /* FIXME: check if remaining is valid oc name? */
00178                                    continue;
00179                             }
00180 
00181                             /* add more "exceptions" to RFC 4511 4.5.1.8. */
00182 
00183                             /* invalid attribute description? remove */
00184                             if ( ad_keystring( bv ) ) {
00185                                    /* NOTE: parsed in-place, don't modify;
00186                                     * rather add "1.1", which must be ignored */
00187                                    BER_BVSTR( &op->ors_attrs[i].an_name, LDAP_NO_ATTRS );
00188                             }
00189 
00190                             /* otherwise leave in place... */
00191                      }
00192               }
00193        }
00194 
00195        if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
00196               Debug( LDAP_DEBUG_ANY, "%s do_search: get_ctrls failed\n",
00197                      op->o_log_prefix, 0, 0 );
00198               goto return_results;
00199        }
00200 
00201        Debug( LDAP_DEBUG_ARGS, "    attrs:", 0, 0, 0 );
00202 
00203        if ( siz != 0 ) {
00204               for ( i = 0; i<siz; i++ ) {
00205                      Debug( LDAP_DEBUG_ARGS, " %s", op->ors_attrs[i].an_name.bv_val, 0, 0 );
00206               }
00207        }
00208 
00209        Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 );
00210 
00211        if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
00212               char abuf[BUFSIZ/2], *ptr = abuf;
00213               unsigned len = 0, alen;
00214 
00215               sprintf(abuf, "scope=%d deref=%d", op->ors_scope, op->ors_deref);
00216               Statslog( LDAP_DEBUG_STATS,
00217                       "%s SRCH base=\"%s\" %s filter=\"%s\"\n",
00218                       op->o_log_prefix, op->o_req_dn.bv_val, abuf,
00219                       op->ors_filterstr.bv_val, 0 );
00220 
00221               for ( i = 0; i<siz; i++ ) {
00222                      alen = op->ors_attrs[i].an_name.bv_len;
00223                      if (alen >= sizeof(abuf)) {
00224                             alen = sizeof(abuf)-1;
00225                      }
00226                      if (len && (len + 1 + alen >= sizeof(abuf))) {
00227                             Statslog( LDAP_DEBUG_STATS, "%s SRCH attr=%s\n",
00228                                 op->o_log_prefix, abuf, 0, 0, 0 );
00229                             len = 0;
00230                             ptr = abuf;
00231                      }
00232                      if (len) {
00233                             *ptr++ = ' ';
00234                             len++;
00235                      }
00236                      ptr = lutil_strncopy(ptr, op->ors_attrs[i].an_name.bv_val, alen);
00237                      len += alen;
00238                      *ptr = '\0';
00239               }
00240               if (len) {
00241                      Statslog( LDAP_DEBUG_STATS, "%s SRCH attr=%s\n",
00242                             op->o_log_prefix, abuf, 0, 0, 0 );
00243               }
00244        }
00245 
00246        op->o_bd = frontendDB;
00247        rs->sr_err = frontendDB->be_search( op, rs );
00248 
00249 return_results:;
00250        if ( !BER_BVISNULL( &op->o_req_dn ) ) {
00251               slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
00252        }
00253        if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
00254               slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
00255        }
00256        if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
00257               op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
00258        }
00259        if ( op->ors_filter != NULL) {
00260               filter_free_x( op, op->ors_filter, 1 );
00261        }
00262        if ( op->ors_attrs != NULL ) {
00263               op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx );
00264        }
00265 
00266        return rs->sr_err;
00267 }
00268 
00269 int
00270 fe_op_search( Operation *op, SlapReply *rs )
00271 {
00272        BackendDB            *bd = op->o_bd;
00273 
00274        if ( op->ors_scope == LDAP_SCOPE_BASE ) {
00275               Entry *entry = NULL;
00276 
00277               if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
00278 #ifdef LDAP_CONNECTIONLESS
00279                      /* Ignore LDAPv2 CLDAP Root DSE queries */
00280                      if (op->o_protocol == LDAP_VERSION2 && op->o_conn->c_is_udp) {
00281                             goto return_results;
00282                      }
00283 #endif
00284                      /* check restrictions */
00285                      if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
00286                             send_ldap_result( op, rs );
00287                             goto return_results;
00288                      }
00289 
00290                      rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text );
00291 
00292               } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
00293                      /* check restrictions */
00294                      if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
00295                             send_ldap_result( op, rs );
00296                             goto return_results;
00297                      }
00298 
00299                      rs->sr_err = schema_info( &entry, &rs->sr_text );
00300               }
00301 
00302               if( rs->sr_err != LDAP_SUCCESS ) {
00303                      send_ldap_result( op, rs );
00304                      goto return_results;
00305 
00306               } else if ( entry != NULL ) {
00307                      if ( get_assert( op ) &&
00308                             ( test_filter( op, entry, get_assertion( op )) != LDAP_COMPARE_TRUE )) {
00309                             rs->sr_err = LDAP_ASSERTION_FAILED;
00310                             goto fail1;
00311                      }
00312 
00313                      rs->sr_err = test_filter( op, entry, op->ors_filter );
00314 
00315                      if( rs->sr_err == LDAP_COMPARE_TRUE ) {
00316                             /* note: we set no limits because either
00317                              * no limit is specified, or at least 1
00318                              * is specified, and we're going to return
00319                              * at most one entry */                   
00320                             op->ors_slimit = SLAP_NO_LIMIT;
00321                             op->ors_tlimit = SLAP_NO_LIMIT;
00322 
00323                             rs->sr_entry = entry;
00324                             rs->sr_attrs = op->ors_attrs;
00325                             rs->sr_operational_attrs = NULL;
00326                             rs->sr_flags = 0;
00327                             send_search_entry( op, rs );
00328                             rs->sr_entry = NULL;
00329                             rs->sr_operational_attrs = NULL;
00330                      }
00331                      rs->sr_err = LDAP_SUCCESS;
00332 fail1:
00333                      entry_free( entry );
00334                      send_ldap_result( op, rs );
00335                      goto return_results;
00336               }
00337        }
00338 
00339        if( BER_BVISEMPTY( &op->o_req_ndn ) && !BER_BVISEMPTY( &default_search_nbase ) ) {
00340               slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
00341               slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
00342 
00343               ber_dupbv_x( &op->o_req_dn, &default_search_base, op->o_tmpmemctx );
00344               ber_dupbv_x( &op->o_req_ndn, &default_search_nbase, op->o_tmpmemctx );
00345        }
00346 
00347        /*
00348         * We could be serving multiple database backends.  Select the
00349         * appropriate one, or send a referral to our "referral server"
00350         * if we don't hold it.
00351         */
00352 
00353        op->o_bd = select_backend( &op->o_req_ndn, 1 );
00354        if ( op->o_bd == NULL ) {
00355               rs->sr_ref = referral_rewrite( default_referral,
00356                      NULL, &op->o_req_dn, op->ors_scope );
00357 
00358               if (!rs->sr_ref) rs->sr_ref = default_referral;
00359               rs->sr_err = LDAP_REFERRAL;
00360               op->o_bd = bd;
00361               send_ldap_result( op, rs );
00362 
00363               if (rs->sr_ref != default_referral)
00364               ber_bvarray_free( rs->sr_ref );
00365               rs->sr_ref = NULL;
00366               goto return_results;
00367        }
00368 
00369        /* check restrictions */
00370        if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
00371               send_ldap_result( op, rs );
00372               goto return_results;
00373        }
00374 
00375        /* check for referrals */
00376        if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
00377               goto return_results;
00378        }
00379 
00380        if ( SLAP_SHADOW(op->o_bd) && get_dontUseCopy(op) ) {
00381               /* don't use shadow copy */
00382               BerVarray defref = op->o_bd->be_update_refs
00383                      ? op->o_bd->be_update_refs : default_referral;
00384 
00385               if( defref != NULL ) {
00386                      rs->sr_ref = referral_rewrite( defref,
00387                             NULL, &op->o_req_dn, op->ors_scope );
00388                      if( !rs->sr_ref) rs->sr_ref = defref;
00389                      rs->sr_err = LDAP_REFERRAL;
00390                      send_ldap_result( op, rs );
00391 
00392                      if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
00393 
00394               } else {
00395                      send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
00396                             "copy not used; no referral information available" );
00397               }
00398 
00399        } else if ( op->o_bd->be_search ) {
00400               if ( limits_check( op, rs ) == 0 ) {
00401                      /* actually do the search and send the result(s) */
00402                      (op->o_bd->be_search)( op, rs );
00403               }
00404               /* else limits_check() sends error */
00405 
00406        } else {
00407               send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
00408                      "operation not supported within namingContext" );
00409        }
00410 
00411 return_results:;
00412        op->o_bd = bd;
00413        return rs->sr_err;
00414 }
00415