passwd.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. /* ACKNOWLEDGEMENTS:
  16. * This program was originally developed by Kurt D. Zeilenga for inclusion in
  17. * OpenLDAP Software.
  18. */
  19. #include "portable.h"
  20. #include <stdio.h>
  21. #include <ac/stdlib.h>
  22. #include <ac/string.h>
  23. #include <ac/time.h>
  24. #include "ldap-int.h"
  25. /*
  26. * LDAP Password Modify (Extended) Operation (RFC 3062)
  27. */
  28. int ldap_parse_passwd(
  29. LDAP *ld,
  30. LDAPMessage *res,
  31. struct berval *newpasswd )
  32. {
  33. int rc;
  34. struct berval *retdata = NULL;
  35. assert( ld != NULL );
  36. assert( LDAP_VALID( ld ) );
  37. assert( res != NULL );
  38. assert( newpasswd != NULL );
  39. newpasswd->bv_val = NULL;
  40. newpasswd->bv_len = 0;
  41. rc = ldap_parse_extended_result( ld, res, NULL, &retdata, 0 );
  42. if ( rc != LDAP_SUCCESS ) {
  43. return rc;
  44. }
  45. if ( retdata != NULL ) {
  46. ber_tag_t tag;
  47. BerElement *ber = ber_init( retdata );
  48. if ( ber == NULL ) {
  49. rc = ld->ld_errno = LDAP_NO_MEMORY;
  50. goto done;
  51. }
  52. /* we should check the tag */
  53. tag = ber_scanf( ber, "{o}", newpasswd );
  54. ber_free( ber, 1 );
  55. if ( tag == LBER_ERROR ) {
  56. rc = ld->ld_errno = LDAP_DECODING_ERROR;
  57. }
  58. }
  59. done:;
  60. ber_bvfree( retdata );
  61. return rc;
  62. }
  63. int
  64. ldap_passwd( LDAP *ld,
  65. struct berval *user,
  66. struct berval *oldpw,
  67. struct berval *newpw,
  68. LDAPControl **sctrls,
  69. LDAPControl **cctrls,
  70. int *msgidp )
  71. {
  72. int rc;
  73. struct berval bv = BER_BVNULL;
  74. BerElement *ber = NULL;
  75. assert( ld != NULL );
  76. assert( LDAP_VALID( ld ) );
  77. assert( msgidp != NULL );
  78. if( user != NULL || oldpw != NULL || newpw != NULL ) {
  79. /* build change password control */
  80. ber = ber_alloc_t( LBER_USE_DER );
  81. if( ber == NULL ) {
  82. ld->ld_errno = LDAP_NO_MEMORY;
  83. return ld->ld_errno;
  84. }
  85. ber_printf( ber, "{" /*}*/ );
  86. if( user != NULL ) {
  87. ber_printf( ber, "tO",
  88. LDAP_TAG_EXOP_MODIFY_PASSWD_ID, user );
  89. }
  90. if( oldpw != NULL ) {
  91. ber_printf( ber, "tO",
  92. LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, oldpw );
  93. }
  94. if( newpw != NULL ) {
  95. ber_printf( ber, "tO",
  96. LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, newpw );
  97. }
  98. ber_printf( ber, /*{*/ "N}" );
  99. rc = ber_flatten2( ber, &bv, 0 );
  100. if( rc < 0 ) {
  101. ld->ld_errno = LDAP_ENCODING_ERROR;
  102. return ld->ld_errno;
  103. }
  104. }
  105. rc = ldap_extended_operation( ld, LDAP_EXOP_MODIFY_PASSWD,
  106. bv.bv_val ? &bv : NULL, sctrls, cctrls, msgidp );
  107. ber_free( ber, 1 );
  108. return rc;
  109. }
  110. int
  111. ldap_passwd_s(
  112. LDAP *ld,
  113. struct berval *user,
  114. struct berval *oldpw,
  115. struct berval *newpw,
  116. struct berval *newpasswd,
  117. LDAPControl **sctrls,
  118. LDAPControl **cctrls )
  119. {
  120. int rc;
  121. int msgid;
  122. LDAPMessage *res;
  123. rc = ldap_passwd( ld, user, oldpw, newpw, sctrls, cctrls, &msgid );
  124. if ( rc != LDAP_SUCCESS ) {
  125. return rc;
  126. }
  127. if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res ) {
  128. return ld->ld_errno;
  129. }
  130. rc = ldap_parse_passwd( ld, res, newpasswd );
  131. if( rc != LDAP_SUCCESS ) {
  132. ldap_msgfree( res );
  133. return rc;
  134. }
  135. return( ldap_result2error( ld, res, 1 ) );
  136. }