17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
37c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
47c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
57c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
87c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
97c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
107c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
137c478bd9Sstevel@tonic-gate  * March 31, 1998.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
167c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
177c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
187c478bd9Sstevel@tonic-gate  * Rights Reserved.
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * Contributor(s):
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate #include "ldap-int.h"
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate /* ldap_create_proxyauth_control
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate    Create a "version 1" proxied authorization control.
277c478bd9Sstevel@tonic-gate 
28*1da57d55SToomas Soome    Parameters are
297c478bd9Sstevel@tonic-gate 
30*1da57d55SToomas Soome    ld              LDAP pointer to the desired connection
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate    dn		   The dn used in the proxy auth
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate    ctl_iscritical  Indicates whether the control is critical of not. If
357c478bd9Sstevel@tonic-gate                    this field is non-zero, the operation will only be car-
367c478bd9Sstevel@tonic-gate                    ried out if the control is recognized by the server
377c478bd9Sstevel@tonic-gate                    and/or client
387c478bd9Sstevel@tonic-gate 
39*1da57d55SToomas Soome    ctrlp           the address of a place to put the constructed control
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate int
437c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_create_proxyauth_control(LDAP * ld,const char * dn,const char ctl_iscritical,LDAPControl ** ctrlp)447c478bd9Sstevel@tonic-gate ldap_create_proxyauth_control (
45*1da57d55SToomas Soome      LDAP *ld,
46*1da57d55SToomas Soome      const char *dn,
477c478bd9Sstevel@tonic-gate      const char ctl_iscritical,
48*1da57d55SToomas Soome      LDAPControl **ctrlp
497c478bd9Sstevel@tonic-gate )
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	BerElement		*ber;
527c478bd9Sstevel@tonic-gate 	int				rc;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
557c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
567c478bd9Sstevel@tonic-gate 	}
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	if (  ctrlp == NULL ) {
597c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
607c478bd9Sstevel@tonic-gate 		return ( LDAP_PARAM_ERROR );
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 	if (NULL == dn)
637c478bd9Sstevel@tonic-gate 	{
647c478bd9Sstevel@tonic-gate 	    dn = "";
657c478bd9Sstevel@tonic-gate 	}
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	/* create a ber package to hold the controlValue */
687c478bd9Sstevel@tonic-gate 	if ( ( nsldapi_alloc_ber_with_options( ld, &ber ) ) != LDAP_SUCCESS ) {
697c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
707c478bd9Sstevel@tonic-gate 		return( LDAP_NO_MEMORY );
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 
75*1da57d55SToomas Soome         if ( LBER_ERROR == ber_printf( ber,
76*1da57d55SToomas Soome                                        "{s}",
77*1da57d55SToomas Soome                                        dn ) )
787c478bd9Sstevel@tonic-gate         {
797c478bd9Sstevel@tonic-gate             LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
807c478bd9Sstevel@tonic-gate             ber_free( ber, 1 );
817c478bd9Sstevel@tonic-gate             return( LDAP_ENCODING_ERROR );
827c478bd9Sstevel@tonic-gate         }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	rc = nsldapi_build_control( LDAP_CONTROL_PROXYAUTH, ber, 1,
857c478bd9Sstevel@tonic-gate 	    ctl_iscritical, ctrlp );
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
887c478bd9Sstevel@tonic-gate 	return( rc );
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /* ldap_create_proxiedauth_control
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate    Create a "version 2" proxied authorization control.
967c478bd9Sstevel@tonic-gate 
97*1da57d55SToomas Soome    Parameters are
987c478bd9Sstevel@tonic-gate 
99*1da57d55SToomas Soome    ld              LDAP pointer to the desired connection
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate    authzid		   The authorization identity used in the proxy auth,
1027c478bd9Sstevel@tonic-gate                    e.g., dn:uid=bjensen,dc=example,dc=com
1037c478bd9Sstevel@tonic-gate 
104*1da57d55SToomas Soome    ctrlp           the address of a place to put the constructed control
1057c478bd9Sstevel@tonic-gate */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate int
1087c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_create_proxiedauth_control(LDAP * ld,const char * authzid,LDAPControl ** ctrlp)1097c478bd9Sstevel@tonic-gate ldap_create_proxiedauth_control (
110*1da57d55SToomas Soome      LDAP *ld,
111*1da57d55SToomas Soome      const char *authzid,
112*1da57d55SToomas Soome      LDAPControl **ctrlp
1137c478bd9Sstevel@tonic-gate )
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	BerElement		*ber;
1167c478bd9Sstevel@tonic-gate 	int				rc;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
1197c478bd9Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (  ctrlp == NULL || authzid == NULL ) {
1237c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
1247c478bd9Sstevel@tonic-gate 		return ( LDAP_PARAM_ERROR );
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/* create a ber package to hold the controlValue */
1287c478bd9Sstevel@tonic-gate 	if ( ( nsldapi_alloc_ber_with_options( ld, &ber ) ) != LDAP_SUCCESS ) {
1297c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
1307c478bd9Sstevel@tonic-gate 		return( LDAP_NO_MEMORY );
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 
135*1da57d55SToomas Soome         if ( LBER_ERROR == ber_printf( ber,
136*1da57d55SToomas Soome                                        "s",
137*1da57d55SToomas Soome                                        authzid ) )
1387c478bd9Sstevel@tonic-gate         {
1397c478bd9Sstevel@tonic-gate             LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
1407c478bd9Sstevel@tonic-gate             ber_free( ber, 1 );
1417c478bd9Sstevel@tonic-gate             return( LDAP_ENCODING_ERROR );
1427c478bd9Sstevel@tonic-gate         }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	rc = nsldapi_build_control( LDAP_CONTROL_PROXIEDAUTH, ber, 1, 1, ctrlp );
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
1477c478bd9Sstevel@tonic-gate 	return( rc );
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate }
150