1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2021 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 
20 #include "portable.h"
21 
22 #include <stdio.h>
23 #include <ac/stdlib.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
26 
27 #include "ldap-int.h"
28 
29 /*
30  * LDAP Who Am I? (Extended) Operation <draft-zeilenga-ldap-authzid-xx.txt>
31  */
32 
ldap_parse_whoami(LDAP * ld,LDAPMessage * res,struct berval ** authzid)33 int ldap_parse_whoami(
34 	LDAP *ld,
35 	LDAPMessage *res,
36 	struct berval **authzid )
37 {
38 	int rc;
39 	char *retoid = NULL;
40 
41 	assert( ld != NULL );
42 	assert( LDAP_VALID( ld ) );
43 	assert( res != NULL );
44 	assert( authzid != NULL );
45 
46 	*authzid = NULL;
47 
48 	rc = ldap_parse_extended_result( ld, res, &retoid, authzid, 0 );
49 
50 	if( rc != LDAP_SUCCESS ) {
51 		ldap_perror( ld, "ldap_parse_whoami" );
52 		return rc;
53 	}
54 
55 	ber_memfree( retoid );
56 	return rc;
57 }
58 
59 int
ldap_whoami(LDAP * ld,LDAPControl ** sctrls,LDAPControl ** cctrls,int * msgidp)60 ldap_whoami( LDAP *ld,
61 	LDAPControl		**sctrls,
62 	LDAPControl		**cctrls,
63 	int				*msgidp )
64 {
65 	int rc;
66 
67 	assert( ld != NULL );
68 	assert( LDAP_VALID( ld ) );
69 	assert( msgidp != NULL );
70 
71 	rc = ldap_extended_operation( ld, LDAP_EXOP_WHO_AM_I,
72 		NULL, sctrls, cctrls, msgidp );
73 
74 	return rc;
75 }
76 
77 int
ldap_whoami_s(LDAP * ld,struct berval ** authzid,LDAPControl ** sctrls,LDAPControl ** cctrls)78 ldap_whoami_s(
79 	LDAP *ld,
80 	struct berval **authzid,
81 	LDAPControl **sctrls,
82 	LDAPControl **cctrls )
83 {
84 	int		rc;
85 	int		msgid;
86 	LDAPMessage	*res;
87 
88 	rc = ldap_whoami( ld, sctrls, cctrls, &msgid );
89 	if ( rc != LDAP_SUCCESS ) return rc;
90 
91 	if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res ) {
92 		return ld->ld_errno;
93 	}
94 
95 	rc = ldap_parse_whoami( ld, res, authzid );
96 	if( rc != LDAP_SUCCESS ) {
97 		ldap_msgfree( res );
98 		return rc;
99 	}
100 
101 	return( ldap_result2error( ld, res, 1 ) );
102 }
103