getentry.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2022 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 the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. /* Portions Copyright (c) 1990 Regents of the University of Michigan.
  16. * All rights reserved.
  17. */
  18. #include "portable.h"
  19. #include <stdio.h>
  20. #include <ac/stdlib.h>
  21. #include <ac/socket.h>
  22. #include <ac/string.h>
  23. #include <ac/time.h>
  24. #include "ldap-int.h"
  25. /* ARGSUSED */
  26. LDAPMessage *
  27. ldap_first_entry( LDAP *ld, LDAPMessage *chain )
  28. {
  29. assert( ld != NULL );
  30. assert( LDAP_VALID( ld ) );
  31. assert( chain != NULL );
  32. return chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY
  33. ? chain
  34. : ldap_next_entry( ld, chain );
  35. }
  36. LDAPMessage *
  37. ldap_next_entry( LDAP *ld, LDAPMessage *entry )
  38. {
  39. assert( ld != NULL );
  40. assert( LDAP_VALID( ld ) );
  41. assert( entry != NULL );
  42. for(
  43. entry = entry->lm_chain;
  44. entry != NULL;
  45. entry = entry->lm_chain )
  46. {
  47. if( entry->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
  48. return( entry );
  49. }
  50. }
  51. return( NULL );
  52. }
  53. int
  54. ldap_count_entries( LDAP *ld, LDAPMessage *chain )
  55. {
  56. int i;
  57. assert( ld != NULL );
  58. assert( LDAP_VALID( ld ) );
  59. for ( i = 0; chain != NULL; chain = chain->lm_chain ) {
  60. if( chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY ) {
  61. i++;
  62. }
  63. }
  64. return( i );
  65. }
  66. int
  67. ldap_get_entry_controls(
  68. LDAP *ld,
  69. LDAPMessage *entry,
  70. LDAPControl ***sctrls )
  71. {
  72. int rc;
  73. BerElement be;
  74. assert( ld != NULL );
  75. assert( LDAP_VALID( ld ) );
  76. assert( entry != NULL );
  77. assert( sctrls != NULL );
  78. if ( entry->lm_msgtype != LDAP_RES_SEARCH_ENTRY ) {
  79. return LDAP_PARAM_ERROR;
  80. }
  81. /* make a local copy of the BerElement */
  82. AC_MEMCPY(&be, entry->lm_ber, sizeof(be));
  83. if ( ber_scanf( &be, "{xx" /*}*/ ) == LBER_ERROR ) {
  84. rc = LDAP_DECODING_ERROR;
  85. goto cleanup_and_return;
  86. }
  87. rc = ldap_pvt_get_controls( &be, sctrls );
  88. cleanup_and_return:
  89. if( rc != LDAP_SUCCESS ) {
  90. ld->ld_errno = rc;
  91. if( ld->ld_matched != NULL ) {
  92. LDAP_FREE( ld->ld_matched );
  93. ld->ld_matched = NULL;
  94. }
  95. if( ld->ld_error != NULL ) {
  96. LDAP_FREE( ld->ld_error );
  97. ld->ld_error = NULL;
  98. }
  99. }
  100. return rc;
  101. }