Home | History | Annotate | Line # | Download | only in libldap
      1 /*	$NetBSD: whoami.c,v 1.4 2025/09/05 21:16:22 christos Exp $	*/
      2 
      3 /* $OpenLDAP$ */
      4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      5  *
      6  * Copyright 1998-2024 The OpenLDAP Foundation.
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted only as authorized by the OpenLDAP
     11  * Public License.
     12  *
     13  * A copy of this license is available in the file LICENSE in the
     14  * top-level directory of the distribution or, alternatively, at
     15  * <http://www.OpenLDAP.org/license.html>.
     16  */
     17 /* ACKNOWLEDGEMENTS:
     18  * This program was originally developed by Kurt D. Zeilenga for inclusion in
     19  * OpenLDAP Software.
     20  */
     21 
     22 #include <sys/cdefs.h>
     23 __RCSID("$NetBSD: whoami.c,v 1.4 2025/09/05 21:16:22 christos Exp $");
     24 
     25 #include "portable.h"
     26 
     27 #include <stdio.h>
     28 #include <ac/stdlib.h>
     29 #include <ac/string.h>
     30 #include <ac/time.h>
     31 
     32 #include "ldap-int.h"
     33 
     34 /*
     35  * LDAP Who Am I? (Extended) Operation <draft-zeilenga-ldap-authzid-xx.txt>
     36  */
     37 
     38 int ldap_parse_whoami(
     39 	LDAP *ld,
     40 	LDAPMessage *res,
     41 	struct berval **authzid )
     42 {
     43 	int rc;
     44 	char *retoid = NULL;
     45 
     46 	assert( ld != NULL );
     47 	assert( LDAP_VALID( ld ) );
     48 	assert( res != NULL );
     49 	assert( authzid != NULL );
     50 
     51 	*authzid = NULL;
     52 
     53 	rc = ldap_parse_extended_result( ld, res, &retoid, authzid, 0 );
     54 
     55 	if( rc != LDAP_SUCCESS ) {
     56 		ldap_perror( ld, "ldap_parse_whoami" );
     57 		return rc;
     58 	}
     59 
     60 	ber_memfree( retoid );
     61 	return rc;
     62 }
     63 
     64 int
     65 ldap_whoami( LDAP *ld,
     66 	LDAPControl		**sctrls,
     67 	LDAPControl		**cctrls,
     68 	int				*msgidp )
     69 {
     70 	int rc;
     71 
     72 	assert( ld != NULL );
     73 	assert( LDAP_VALID( ld ) );
     74 	assert( msgidp != NULL );
     75 
     76 	rc = ldap_extended_operation( ld, LDAP_EXOP_WHO_AM_I,
     77 		NULL, sctrls, cctrls, msgidp );
     78 
     79 	return rc;
     80 }
     81 
     82 int
     83 ldap_whoami_s(
     84 	LDAP *ld,
     85 	struct berval **authzid,
     86 	LDAPControl **sctrls,
     87 	LDAPControl **cctrls )
     88 {
     89 	int		rc;
     90 	int		msgid;
     91 	LDAPMessage	*res;
     92 
     93 	rc = ldap_whoami( ld, sctrls, cctrls, &msgid );
     94 	if ( rc != LDAP_SUCCESS ) return rc;
     95 
     96 	if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res ) {
     97 		return ld->ld_errno;
     98 	}
     99 
    100 	rc = ldap_parse_whoami( ld, res, authzid );
    101 	if( rc != LDAP_SUCCESS ) {
    102 		ldap_msgfree( res );
    103 		return rc;
    104 	}
    105 
    106 	return( ldap_result2error( ld, res, 1 ) );
    107 }
    108