whoami.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /* 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 Who Am I? (Extended) Operation <draft-zeilenga-ldap-authzid-xx.txt>
  27. */
  28. int ldap_parse_whoami(
  29. LDAP *ld,
  30. LDAPMessage *res,
  31. struct berval **authzid )
  32. {
  33. int rc;
  34. char *retoid = NULL;
  35. assert( ld != NULL );
  36. assert( LDAP_VALID( ld ) );
  37. assert( res != NULL );
  38. assert( authzid != NULL );
  39. *authzid = NULL;
  40. rc = ldap_parse_extended_result( ld, res, &retoid, authzid, 0 );
  41. if( rc != LDAP_SUCCESS ) {
  42. ldap_perror( ld, "ldap_parse_whoami" );
  43. return rc;
  44. }
  45. ber_memfree( retoid );
  46. return rc;
  47. }
  48. int
  49. ldap_whoami( LDAP *ld,
  50. LDAPControl **sctrls,
  51. LDAPControl **cctrls,
  52. int *msgidp )
  53. {
  54. int rc;
  55. assert( ld != NULL );
  56. assert( LDAP_VALID( ld ) );
  57. assert( msgidp != NULL );
  58. rc = ldap_extended_operation( ld, LDAP_EXOP_WHO_AM_I,
  59. NULL, sctrls, cctrls, msgidp );
  60. return rc;
  61. }
  62. int
  63. ldap_whoami_s(
  64. LDAP *ld,
  65. struct berval **authzid,
  66. LDAPControl **sctrls,
  67. LDAPControl **cctrls )
  68. {
  69. int rc;
  70. int msgid;
  71. LDAPMessage *res;
  72. rc = ldap_whoami( ld, sctrls, cctrls, &msgid );
  73. if ( rc != LDAP_SUCCESS ) return rc;
  74. if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res ) {
  75. return ld->ld_errno;
  76. }
  77. rc = ldap_parse_whoami( ld, res, authzid );
  78. if( rc != LDAP_SUCCESS ) {
  79. ldap_msgfree( res );
  80. return rc;
  81. }
  82. return( ldap_result2error( ld, res, 1 ) );
  83. }