xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/whoami.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: whoami.c,v 1.3 2021/08/14 16:14:56 christos Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2021 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.3 2021/08/14 16:14:56 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 
ldap_parse_whoami(LDAP * ld,LDAPMessage * res,struct berval ** authzid)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
ldap_whoami(LDAP * ld,LDAPControl ** sctrls,LDAPControl ** cctrls,int * msgidp)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
ldap_whoami_s(LDAP * ld,struct berval ** authzid,LDAPControl ** sctrls,LDAPControl ** cctrls)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