assertion.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #include "portable.h"
  16. #include <stdio.h>
  17. #include <ac/stdlib.h>
  18. #include <ac/string.h>
  19. #include <ac/time.h>
  20. #include "ldap-int.h"
  21. int
  22. ldap_create_assertion_control_value(
  23. LDAP *ld,
  24. char *assertion,
  25. struct berval *value )
  26. {
  27. BerElement *ber = NULL;
  28. int err;
  29. ld->ld_errno = LDAP_SUCCESS;
  30. if ( assertion == NULL || assertion[ 0 ] == '\0' ) {
  31. ld->ld_errno = LDAP_PARAM_ERROR;
  32. return ld->ld_errno;
  33. }
  34. if ( value == NULL ) {
  35. ld->ld_errno = LDAP_PARAM_ERROR;
  36. return ld->ld_errno;
  37. }
  38. BER_BVZERO( value );
  39. ber = ldap_alloc_ber_with_options( ld );
  40. if ( ber == NULL ) {
  41. ld->ld_errno = LDAP_NO_MEMORY;
  42. return ld->ld_errno;
  43. }
  44. err = ldap_pvt_put_filter( ber, assertion );
  45. if ( err < 0 ) {
  46. ld->ld_errno = LDAP_ENCODING_ERROR;
  47. goto done;
  48. }
  49. err = ber_flatten2( ber, value, 1 );
  50. if ( err < 0 ) {
  51. ld->ld_errno = LDAP_NO_MEMORY;
  52. goto done;
  53. }
  54. done:;
  55. if ( ber != NULL ) {
  56. ber_free( ber, 1 );
  57. }
  58. return ld->ld_errno;
  59. }
  60. int
  61. ldap_create_assertion_control(
  62. LDAP *ld,
  63. char *assertion,
  64. int iscritical,
  65. LDAPControl **ctrlp )
  66. {
  67. struct berval value;
  68. if ( ctrlp == NULL ) {
  69. ld->ld_errno = LDAP_PARAM_ERROR;
  70. return ld->ld_errno;
  71. }
  72. ld->ld_errno = ldap_create_assertion_control_value( ld,
  73. assertion, &value );
  74. if ( ld->ld_errno == LDAP_SUCCESS ) {
  75. ld->ld_errno = ldap_control_create( LDAP_CONTROL_ASSERT,
  76. iscritical, &value, 0, ctrlp );
  77. if ( ld->ld_errno != LDAP_SUCCESS ) {
  78. LDAP_FREE( value.bv_val );
  79. }
  80. }
  81. return ld->ld_errno;
  82. }