xref: /titanic_44/usr/src/lib/libldap5/sources/ldap/common/proxyauthctrl.c (revision 5151fb1220e0ceafdc172203863c73da4285c170)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 
3 /*
4  * The contents of this file are subject to the Netscape Public
5  * License Version 1.1 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.mozilla.org/NPL/
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * The Original Code is Mozilla Communicator client code, released
15  * March 31, 1998.
16  *
17  * The Initial Developer of the Original Code is Netscape
18  * Communications Corporation. Portions created by Netscape are
19  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
20  * Rights Reserved.
21  *
22  * Contributor(s):
23  */
24 #include "ldap-int.h"
25 
26 /* ldap_create_proxyauth_control
27 
28    Create a "version 1" proxied authorization control.
29 
30    Parameters are
31 
32    ld              LDAP pointer to the desired connection
33 
34    dn		   The dn used in the proxy auth
35 
36    ctl_iscritical  Indicates whether the control is critical of not. If
37                    this field is non-zero, the operation will only be car-
38                    ried out if the control is recognized by the server
39                    and/or client
40 
41    ctrlp           the address of a place to put the constructed control
42 */
43 
44 int
45 LDAP_CALL
46 ldap_create_proxyauth_control (
47      LDAP *ld,
48      const char *dn,
49      const char ctl_iscritical,
50      LDAPControl **ctrlp
51 )
52 {
53 	BerElement		*ber;
54 	int				rc;
55 
56 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
57 		return( LDAP_PARAM_ERROR );
58 	}
59 
60 	if (  ctrlp == NULL ) {
61 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
62 		return ( LDAP_PARAM_ERROR );
63 	}
64 	if (NULL == dn)
65 	{
66 	    dn = "";
67 	}
68 
69 	/* create a ber package to hold the controlValue */
70 	if ( ( nsldapi_alloc_ber_with_options( ld, &ber ) ) != LDAP_SUCCESS ) {
71 		LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
72 		return( LDAP_NO_MEMORY );
73 	}
74 
75 
76 
77         if ( LBER_ERROR == ber_printf( ber,
78                                        "{s}",
79                                        dn ) )
80         {
81             LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
82             ber_free( ber, 1 );
83             return( LDAP_ENCODING_ERROR );
84         }
85 
86 	rc = nsldapi_build_control( LDAP_CONTROL_PROXYAUTH, ber, 1,
87 	    ctl_iscritical, ctrlp );
88 
89 	LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
90 	return( rc );
91 
92 }
93 
94 
95 /* ldap_create_proxiedauth_control
96 
97    Create a "version 2" proxied authorization control.
98 
99    Parameters are
100 
101    ld              LDAP pointer to the desired connection
102 
103    authzid		   The authorization identity used in the proxy auth,
104                    e.g., dn:uid=bjensen,dc=example,dc=com
105 
106    ctrlp           the address of a place to put the constructed control
107 */
108 
109 int
110 LDAP_CALL
111 ldap_create_proxiedauth_control (
112      LDAP *ld,
113      const char *authzid,
114      LDAPControl **ctrlp
115 )
116 {
117 	BerElement		*ber;
118 	int				rc;
119 
120 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
121 		return( LDAP_PARAM_ERROR );
122 	}
123 
124 	if (  ctrlp == NULL || authzid == NULL ) {
125 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
126 		return ( LDAP_PARAM_ERROR );
127 	}
128 
129 	/* create a ber package to hold the controlValue */
130 	if ( ( nsldapi_alloc_ber_with_options( ld, &ber ) ) != LDAP_SUCCESS ) {
131 		LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
132 		return( LDAP_NO_MEMORY );
133 	}
134 
135 
136 
137         if ( LBER_ERROR == ber_printf( ber,
138                                        "s",
139                                        authzid ) )
140         {
141             LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
142             ber_free( ber, 1 );
143             return( LDAP_ENCODING_ERROR );
144         }
145 
146 	rc = nsldapi_build_control( LDAP_CONTROL_PROXIEDAUTH, ber, 1, 1, ctrlp );
147 
148 	LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
149 	return( rc );
150 
151 }
152