1 /* $NetBSD: search.c,v 1.1.1.6 2018/02/06 01:53:15 christos Exp $ */ 2 3 /* $OpenLDAP$ */ 4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 5 * 6 * Copyright 1998-2017 The OpenLDAP Foundation. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted only as authorized by the OpenLDAP 11 * Public License. 12 * 13 * A copy of this license is available in the file LICENSE in the 14 * top-level directory of the distribution or, alternatively, at 15 * <http://www.OpenLDAP.org/license.html>. 16 */ 17 /* Portions Copyright (c) 1995 Regents of the University of Michigan. 18 * All rights reserved. 19 * 20 * Redistribution and use in source and binary forms are permitted 21 * provided that this notice is preserved and that due credit is given 22 * to the University of Michigan at Ann Arbor. The name of the University 23 * may not be used to endorse or promote products derived from this 24 * software without specific prior written permission. This software 25 * is provided ``as is'' without express or implied warranty. 26 */ 27 28 #include <sys/cdefs.h> 29 __RCSID("$NetBSD: search.c,v 1.1.1.6 2018/02/06 01:53:15 christos Exp $"); 30 31 #include "portable.h" 32 33 #include <stdio.h> 34 35 #include <ac/string.h> 36 #include <ac/socket.h> 37 38 #include "lutil.h" 39 #include "slap.h" 40 41 int 42 do_search( 43 Operation *op, /* info about the op to which we're responding */ 44 SlapReply *rs /* all the response data we'll send */ ) 45 { 46 struct berval base = BER_BVNULL; 47 ber_len_t siz, off, i; 48 49 Debug( LDAP_DEBUG_TRACE, "%s do_search\n", 50 op->o_log_prefix, 0, 0 ); 51 /* 52 * Parse the search request. It looks like this: 53 * 54 * SearchRequest := [APPLICATION 3] SEQUENCE { 55 * baseObject DistinguishedName, 56 * scope ENUMERATED { 57 * baseObject (0), 58 * singleLevel (1), 59 * wholeSubtree (2), 60 * subordinate (3) -- OpenLDAP extension 61 * }, 62 * derefAliases ENUMERATED { 63 * neverDerefaliases (0), 64 * derefInSearching (1), 65 * derefFindingBaseObj (2), 66 * alwaysDerefAliases (3) 67 * }, 68 * sizelimit INTEGER (0 .. 65535), 69 * timelimit INTEGER (0 .. 65535), 70 * attrsOnly BOOLEAN, 71 * filter Filter, 72 * attributes SEQUENCE OF AttributeType 73 * } 74 */ 75 76 /* baseObject, scope, derefAliases, sizelimit, timelimit, attrsOnly */ 77 if ( ber_scanf( op->o_ber, "{miiiib" /*}*/, 78 &base, &op->ors_scope, &op->ors_deref, &op->ors_slimit, 79 &op->ors_tlimit, &op->ors_attrsonly ) == LBER_ERROR ) 80 { 81 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" ); 82 rs->sr_err = SLAPD_DISCONNECT; 83 goto return_results; 84 } 85 86 if ( op->ors_tlimit < 0 || op->ors_tlimit > SLAP_MAX_LIMIT ) { 87 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid time limit" ); 88 goto return_results; 89 } 90 91 if ( op->ors_slimit < 0 || op->ors_slimit > SLAP_MAX_LIMIT ) { 92 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid size limit" ); 93 goto return_results; 94 } 95 96 switch( op->ors_scope ) { 97 case LDAP_SCOPE_BASE: 98 case LDAP_SCOPE_ONELEVEL: 99 case LDAP_SCOPE_SUBTREE: 100 case LDAP_SCOPE_SUBORDINATE: 101 break; 102 default: 103 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid scope" ); 104 goto return_results; 105 } 106 107 switch( op->ors_deref ) { 108 case LDAP_DEREF_NEVER: 109 case LDAP_DEREF_FINDING: 110 case LDAP_DEREF_SEARCHING: 111 case LDAP_DEREF_ALWAYS: 112 break; 113 default: 114 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "invalid deref" ); 115 goto return_results; 116 } 117 118 rs->sr_err = dnPrettyNormal( NULL, &base, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx ); 119 if( rs->sr_err != LDAP_SUCCESS ) { 120 Debug( LDAP_DEBUG_ANY, "%s do_search: invalid dn: \"%s\"\n", 121 op->o_log_prefix, base.bv_val, 0 ); 122 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" ); 123 goto return_results; 124 } 125 126 Debug( LDAP_DEBUG_ARGS, "SRCH \"%s\" %d %d", 127 base.bv_val, op->ors_scope, op->ors_deref ); 128 Debug( LDAP_DEBUG_ARGS, " %d %d %d\n", 129 op->ors_slimit, op->ors_tlimit, op->ors_attrsonly); 130 131 /* filter - returns a "normalized" version */ 132 rs->sr_err = get_filter( op, op->o_ber, &op->ors_filter, &rs->sr_text ); 133 if( rs->sr_err != LDAP_SUCCESS ) { 134 if( rs->sr_err == SLAPD_DISCONNECT ) { 135 rs->sr_err = LDAP_PROTOCOL_ERROR; 136 send_ldap_disconnect( op, rs ); 137 rs->sr_err = SLAPD_DISCONNECT; 138 } else { 139 send_ldap_result( op, rs ); 140 } 141 goto return_results; 142 } 143 filter2bv_x( op, op->ors_filter, &op->ors_filterstr ); 144 145 Debug( LDAP_DEBUG_ARGS, " filter: %s\n", 146 !BER_BVISEMPTY( &op->ors_filterstr ) ? op->ors_filterstr.bv_val : "empty", 0, 0 ); 147 148 /* attributes */ 149 siz = sizeof(AttributeName); 150 off = offsetof(AttributeName,an_name); 151 if ( ber_scanf( op->o_ber, "{M}}", &op->ors_attrs, &siz, off ) == LBER_ERROR ) { 152 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding attrs error" ); 153 rs->sr_err = SLAPD_DISCONNECT; 154 goto return_results; 155 } 156 for ( i=0; i<siz; i++ ) { 157 const char *dummy; /* ignore msgs from bv2ad */ 158 op->ors_attrs[i].an_desc = NULL; 159 op->ors_attrs[i].an_oc = NULL; 160 op->ors_attrs[i].an_flags = 0; 161 if ( slap_bv2ad( &op->ors_attrs[i].an_name, 162 &op->ors_attrs[i].an_desc, &dummy ) != LDAP_SUCCESS ) 163 { 164 if ( slap_bv2undef_ad( &op->ors_attrs[i].an_name, 165 &op->ors_attrs[i].an_desc, &dummy, 166 SLAP_AD_PROXIED|SLAP_AD_NOINSERT ) ) 167 { 168 struct berval *bv = &op->ors_attrs[i].an_name; 169 170 /* RFC 4511 LDAPv3: All User Attributes */ 171 if ( bvmatch( bv, slap_bv_all_user_attrs ) ) { 172 continue; 173 } 174 175 /* RFC 3673 LDAPv3: All Operational Attributes */ 176 if ( bvmatch( bv, slap_bv_all_operational_attrs ) ) { 177 continue; 178 } 179 180 /* RFC 4529 LDAP: Requesting Attributes by Object Class */ 181 if ( bv->bv_len > 1 && bv->bv_val[0] == '@' ) { 182 /* FIXME: check if remaining is valid oc name? */ 183 continue; 184 } 185 186 /* add more "exceptions" to RFC 4511 4.5.1.8. */ 187 188 /* invalid attribute description? remove */ 189 if ( ad_keystring( bv ) ) { 190 /* NOTE: parsed in-place, don't modify; 191 * rather add "1.1", which must be ignored */ 192 BER_BVSTR( &op->ors_attrs[i].an_name, LDAP_NO_ATTRS ); 193 } 194 195 /* otherwise leave in place... */ 196 } 197 } 198 } 199 200 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) { 201 Debug( LDAP_DEBUG_ANY, "%s do_search: get_ctrls failed\n", 202 op->o_log_prefix, 0, 0 ); 203 goto return_results; 204 } 205 206 Debug( LDAP_DEBUG_ARGS, " attrs:", 0, 0, 0 ); 207 208 if ( siz != 0 ) { 209 for ( i = 0; i<siz; i++ ) { 210 Debug( LDAP_DEBUG_ARGS, " %s", op->ors_attrs[i].an_name.bv_val, 0, 0 ); 211 } 212 } 213 214 Debug( LDAP_DEBUG_ARGS, "\n", 0, 0, 0 ); 215 216 if ( StatslogTest( LDAP_DEBUG_STATS ) ) { 217 char abuf[BUFSIZ/2], *ptr = abuf; 218 unsigned len = 0, alen; 219 220 sprintf(abuf, "scope=%d deref=%d", op->ors_scope, op->ors_deref); 221 Statslog( LDAP_DEBUG_STATS, 222 "%s SRCH base=\"%s\" %s filter=\"%s\"\n", 223 op->o_log_prefix, op->o_req_dn.bv_val, abuf, 224 op->ors_filterstr.bv_val, 0 ); 225 226 for ( i = 0; i<siz; i++ ) { 227 alen = op->ors_attrs[i].an_name.bv_len; 228 if (alen >= sizeof(abuf)) { 229 alen = sizeof(abuf)-1; 230 } 231 if (len && (len + 1 + alen >= sizeof(abuf))) { 232 Statslog( LDAP_DEBUG_STATS, "%s SRCH attr=%s\n", 233 op->o_log_prefix, abuf, 0, 0, 0 ); 234 len = 0; 235 ptr = abuf; 236 } 237 if (len) { 238 *ptr++ = ' '; 239 len++; 240 } 241 ptr = lutil_strncopy(ptr, op->ors_attrs[i].an_name.bv_val, alen); 242 len += alen; 243 *ptr = '\0'; 244 } 245 if (len) { 246 Statslog( LDAP_DEBUG_STATS, "%s SRCH attr=%s\n", 247 op->o_log_prefix, abuf, 0, 0, 0 ); 248 } 249 } 250 251 op->o_bd = frontendDB; 252 rs->sr_err = frontendDB->be_search( op, rs ); 253 254 return_results:; 255 if ( !BER_BVISNULL( &op->o_req_dn ) ) { 256 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx ); 257 } 258 if ( !BER_BVISNULL( &op->o_req_ndn ) ) { 259 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx ); 260 } 261 if ( !BER_BVISNULL( &op->ors_filterstr ) ) { 262 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx ); 263 } 264 if ( op->ors_filter != NULL) { 265 filter_free_x( op, op->ors_filter, 1 ); 266 } 267 if ( op->ors_attrs != NULL ) { 268 op->o_tmpfree( op->ors_attrs, op->o_tmpmemctx ); 269 } 270 271 return rs->sr_err; 272 } 273 274 int 275 fe_op_search( Operation *op, SlapReply *rs ) 276 { 277 BackendDB *bd = op->o_bd; 278 279 if ( op->ors_scope == LDAP_SCOPE_BASE ) { 280 Entry *entry = NULL; 281 282 if ( BER_BVISEMPTY( &op->o_req_ndn ) ) { 283 #ifdef LDAP_CONNECTIONLESS 284 /* Ignore LDAPv2 CLDAP Root DSE queries */ 285 if (op->o_protocol == LDAP_VERSION2 && op->o_conn->c_is_udp) { 286 goto return_results; 287 } 288 #endif 289 /* check restrictions */ 290 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) { 291 send_ldap_result( op, rs ); 292 goto return_results; 293 } 294 295 rs->sr_err = root_dse_info( op->o_conn, &entry, &rs->sr_text ); 296 297 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) { 298 /* check restrictions */ 299 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) { 300 send_ldap_result( op, rs ); 301 goto return_results; 302 } 303 304 rs->sr_err = schema_info( &entry, &rs->sr_text ); 305 } 306 307 if( rs->sr_err != LDAP_SUCCESS ) { 308 send_ldap_result( op, rs ); 309 goto return_results; 310 311 } else if ( entry != NULL ) { 312 if ( get_assert( op ) && 313 ( test_filter( op, entry, get_assertion( op )) != LDAP_COMPARE_TRUE )) { 314 rs->sr_err = LDAP_ASSERTION_FAILED; 315 goto fail1; 316 } 317 318 rs->sr_err = test_filter( op, entry, op->ors_filter ); 319 320 if( rs->sr_err == LDAP_COMPARE_TRUE ) { 321 /* note: we set no limits because either 322 * no limit is specified, or at least 1 323 * is specified, and we're going to return 324 * at most one entry */ 325 op->ors_slimit = SLAP_NO_LIMIT; 326 op->ors_tlimit = SLAP_NO_LIMIT; 327 328 rs->sr_entry = entry; 329 rs->sr_attrs = op->ors_attrs; 330 rs->sr_operational_attrs = NULL; 331 rs->sr_flags = 0; 332 send_search_entry( op, rs ); 333 rs->sr_entry = NULL; 334 rs->sr_operational_attrs = NULL; 335 } 336 rs->sr_err = LDAP_SUCCESS; 337 fail1: 338 entry_free( entry ); 339 send_ldap_result( op, rs ); 340 goto return_results; 341 } 342 } 343 344 if( BER_BVISEMPTY( &op->o_req_ndn ) && !BER_BVISEMPTY( &default_search_nbase ) ) { 345 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx ); 346 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx ); 347 348 ber_dupbv_x( &op->o_req_dn, &default_search_base, op->o_tmpmemctx ); 349 ber_dupbv_x( &op->o_req_ndn, &default_search_nbase, op->o_tmpmemctx ); 350 } 351 352 /* 353 * We could be serving multiple database backends. Select the 354 * appropriate one, or send a referral to our "referral server" 355 * if we don't hold it. 356 */ 357 358 op->o_bd = select_backend( &op->o_req_ndn, 1 ); 359 if ( op->o_bd == NULL ) { 360 rs->sr_ref = referral_rewrite( default_referral, 361 NULL, &op->o_req_dn, op->ors_scope ); 362 363 if (!rs->sr_ref) rs->sr_ref = default_referral; 364 rs->sr_err = LDAP_REFERRAL; 365 op->o_bd = bd; 366 send_ldap_result( op, rs ); 367 368 if (rs->sr_ref != default_referral) 369 ber_bvarray_free( rs->sr_ref ); 370 rs->sr_ref = NULL; 371 goto return_results; 372 } 373 374 /* check restrictions */ 375 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) { 376 send_ldap_result( op, rs ); 377 goto return_results; 378 } 379 380 /* check for referrals */ 381 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) { 382 goto return_results; 383 } 384 385 if ( SLAP_SHADOW(op->o_bd) && get_dontUseCopy(op) ) { 386 /* don't use shadow copy */ 387 BerVarray defref = op->o_bd->be_update_refs 388 ? op->o_bd->be_update_refs : default_referral; 389 390 if( defref != NULL ) { 391 rs->sr_ref = referral_rewrite( defref, 392 NULL, &op->o_req_dn, op->ors_scope ); 393 if( !rs->sr_ref) rs->sr_ref = defref; 394 rs->sr_err = LDAP_REFERRAL; 395 send_ldap_result( op, rs ); 396 397 if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref ); 398 399 } else { 400 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM, 401 "copy not used; no referral information available" ); 402 } 403 404 } else if ( op->o_bd->be_search ) { 405 if ( limits_check( op, rs ) == 0 ) { 406 /* actually do the search and send the result(s) */ 407 (op->o_bd->be_search)( op, rs ); 408 } 409 /* else limits_check() sends error */ 410 411 } else { 412 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM, 413 "operation not supported within namingContext" ); 414 } 415 416 return_results:; 417 op->o_bd = bd; 418 return rs->sr_err; 419 } 420 421