getvalues.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2024 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/ctype.h>
  22. #include <ac/socket.h>
  23. #include <ac/string.h>
  24. #include <ac/time.h>
  25. #include "ldap-int.h"
  26. char **
  27. ldap_get_values( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
  28. {
  29. BerElement ber;
  30. char *attr;
  31. int found = 0;
  32. char **vals;
  33. assert( ld != NULL );
  34. assert( LDAP_VALID( ld ) );
  35. assert( entry != NULL );
  36. assert( target != NULL );
  37. Debug0( LDAP_DEBUG_TRACE, "ldap_get_values\n" );
  38. ber = *entry->lm_ber;
  39. /* skip sequence, dn, sequence of, and snag the first attr */
  40. if ( ber_scanf( &ber, "{x{{a" /*}}}*/, &attr ) == LBER_ERROR ) {
  41. ld->ld_errno = LDAP_DECODING_ERROR;
  42. return( NULL );
  43. }
  44. if ( strcasecmp( target, attr ) == 0 )
  45. found = 1;
  46. /* break out on success, return out on error */
  47. while ( ! found ) {
  48. LDAP_FREE(attr);
  49. attr = NULL;
  50. if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) {
  51. ld->ld_errno = LDAP_DECODING_ERROR;
  52. return( NULL );
  53. }
  54. if ( strcasecmp( target, attr ) == 0 )
  55. break;
  56. }
  57. LDAP_FREE(attr);
  58. attr = NULL;
  59. /*
  60. * if we get this far, we've found the attribute and are sitting
  61. * just before the set of values.
  62. */
  63. if ( ber_scanf( &ber, "[v]", &vals ) == LBER_ERROR ) {
  64. ld->ld_errno = LDAP_DECODING_ERROR;
  65. return( NULL );
  66. }
  67. return( vals );
  68. }
  69. struct berval **
  70. ldap_get_values_len( LDAP *ld, LDAPMessage *entry, LDAP_CONST char *target )
  71. {
  72. BerElement ber;
  73. char *attr;
  74. int found = 0;
  75. struct berval **vals;
  76. assert( ld != NULL );
  77. assert( LDAP_VALID( ld ) );
  78. assert( entry != NULL );
  79. assert( target != NULL );
  80. Debug0( LDAP_DEBUG_TRACE, "ldap_get_values_len\n" );
  81. ber = *entry->lm_ber;
  82. /* skip sequence, dn, sequence of, and snag the first attr */
  83. if ( ber_scanf( &ber, "{x{{a" /* }}} */, &attr ) == LBER_ERROR ) {
  84. ld->ld_errno = LDAP_DECODING_ERROR;
  85. return( NULL );
  86. }
  87. if ( strcasecmp( target, attr ) == 0 )
  88. found = 1;
  89. /* break out on success, return out on error */
  90. while ( ! found ) {
  91. LDAP_FREE( attr );
  92. attr = NULL;
  93. if ( ber_scanf( &ber, /*{*/ "x}{a" /*}*/, &attr ) == LBER_ERROR ) {
  94. ld->ld_errno = LDAP_DECODING_ERROR;
  95. return( NULL );
  96. }
  97. if ( strcasecmp( target, attr ) == 0 )
  98. break;
  99. }
  100. LDAP_FREE( attr );
  101. attr = NULL;
  102. /*
  103. * if we get this far, we've found the attribute and are sitting
  104. * just before the set of values.
  105. */
  106. if ( ber_scanf( &ber, "[V]", &vals ) == LBER_ERROR ) {
  107. ld->ld_errno = LDAP_DECODING_ERROR;
  108. return( NULL );
  109. }
  110. return( vals );
  111. }
  112. int
  113. ldap_count_values( char **vals )
  114. {
  115. int i;
  116. if ( vals == NULL )
  117. return( 0 );
  118. for ( i = 0; vals[i] != NULL; i++ )
  119. ; /* NULL */
  120. return( i );
  121. }
  122. int
  123. ldap_count_values_len( struct berval **vals )
  124. {
  125. return( ldap_count_values( (char **) vals ) );
  126. }
  127. void
  128. ldap_value_free( char **vals )
  129. {
  130. LDAP_VFREE( vals );
  131. }
  132. void
  133. ldap_value_free_len( struct berval **vals )
  134. {
  135. ber_bvecfree( vals );
  136. }
  137. char **
  138. ldap_value_dup( char *const *vals )
  139. {
  140. char **new;
  141. int i;
  142. if( vals == NULL ) {
  143. return NULL;
  144. }
  145. for( i=0; vals[i]; i++ ) {
  146. ; /* Count the number of values */
  147. }
  148. if( i == 0 ) {
  149. return NULL;
  150. }
  151. new = LDAP_MALLOC( (i+1)*sizeof(char *) ); /* Alloc array of pointers */
  152. if( new == NULL ) {
  153. return NULL;
  154. }
  155. for( i=0; vals[i]; i++ ) {
  156. new[i] = LDAP_STRDUP( vals[i] ); /* Dup each value */
  157. if( new[i] == NULL ) {
  158. LDAP_VFREE( new );
  159. return NULL;
  160. }
  161. }
  162. new[i] = NULL;
  163. return new;
  164. }