| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 1999 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap.controls; |
| 39 | |
| 40 | import org.ietf.ldap.LDAPControl; |
| 41 | import org.ietf.ldap.ber.stream.BEROctetString; |
| 42 | import org.ietf.ldap.ber.stream.BERSequence; |
| 43 | |
| 44 | /** |
| 45 | * Represents an LDAP v3 server control that specifies that you want |
| 46 | * the server to use the specified DN's identity for this operation. |
| 47 | * (The OID for this control is 2.16.840.1.113730.3.4.12.) |
| 48 | * <P> |
| 49 | * |
| 50 | * You can include the control in any request by constructing |
| 51 | * an <CODE>LDAPSearchConstraints</CODE> object and calling the |
| 52 | * <CODE>setControls</CODE> method. You can then pass this |
| 53 | * <CODE>LDAPSearchConstraints</CODE> object to the <CODE>search</CODE> |
| 54 | * or other request method of an <CODE>LDAPConnection</CODE> object. |
| 55 | * <P> |
| 56 | * |
| 57 | * For example: |
| 58 | * <PRE> |
| 59 | * ... |
| 60 | * LDAPConnection ld = new LDAPConnection(); |
| 61 | * try { |
| 62 | * // Connect to server. |
| 63 | * ld.connect( 3, hostname, portnumber, "", "" ); |
| 64 | * |
| 65 | * // Create a "critical" proxied auth server control using |
| 66 | * // the DN "uid=charlie,ou=people,o=acme.com". |
| 67 | * LDAPProxiedAuthControl ctrl = |
| 68 | * new LDAPProxiedAuthControl( "uid=charlie,ou=people,o=acme.com", |
| 69 | * true ); |
| 70 | * |
| 71 | * // Create search constraints to use that control. |
| 72 | * LDAPSearchConstraints cons = new LDAPSearchConstraints(); |
| 73 | * cons.setControls( sortCtrl ); |
| 74 | * |
| 75 | * // Send the search request. |
| 76 | * LDAPSearchResults res = ld.search( "o=Airius.com", |
| 77 | * LDAPConnection.SCOPE_SUB, "(cn=Barbara*)", null, false, cons ); |
| 78 | * |
| 79 | * ... |
| 80 | * |
| 81 | * </PRE> |
| 82 | * |
| 83 | * <P> |
| 84 | * |
| 85 | * @version 1.0 |
| 86 | * @see org.ietf.ldap.LDAPControl |
| 87 | * @see org.ietf.ldap.LDAPConstraints |
| 88 | * @see org.ietf.ldap.LDAPSearchConstraints |
| 89 | * @see org.ietf.ldap.LDAPConstraints#setControls(LDAPControl) |
| 90 | */ |
| 91 | public class LDAPProxiedAuthControl extends LDAPControl { |
| 92 | public final static String PROXIEDAUTHREQUEST = |
| 93 | "2.16.840.1.113730.3.4.12"; |
| 94 | |
| 95 | private String _dn; |
| 96 | |
| 97 | /** |
| 98 | * Constructs an <CODE>LDAPProxiedAuthControl</CODE> object with a |
| 99 | * DN to use as identity. |
| 100 | * @param dn DN to use as identity for execution of a request |
| 101 | * @param critical <CODE>true</CODE> if the LDAP operation should be |
| 102 | * discarded when the server does not support this control (in other |
| 103 | * words, this control is critical to the LDAP operation) |
| 104 | * @see org.ietf.ldap.LDAPControl |
| 105 | */ |
| 106 | public LDAPProxiedAuthControl( String dn, |
| 107 | boolean critical) { |
| 108 | super( PROXIEDAUTHREQUEST, critical, null ); |
| 109 | _value = createSpecification( _dn = dn ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Create a "flattened" BER encoding of the requested contents, |
| 114 | * and return it as a byte array. |
| 115 | * @param dn the DN to use as an identity |
| 116 | * @return the byte array of encoded data. |
| 117 | */ |
| 118 | private byte[] createSpecification( String dn ) { |
| 119 | /* A sequence */ |
| 120 | BERSequence ber = new BERSequence(); |
| 121 | /* Add the DN as a string value */ |
| 122 | ber.addElement( new BEROctetString( dn ) ); |
| 123 | /* Suck out the data and return it */ |
| 124 | return flattenBER( ber ); |
| 125 | } |
| 126 | |
| 127 | public String toString() { |
| 128 | StringBuffer sb = new StringBuffer("{ProxiedAuthCtrl:"); |
| 129 | |
| 130 | sb.append(" isCritical="); |
| 131 | sb.append(isCritical()); |
| 132 | |
| 133 | sb.append(" dn="); |
| 134 | sb.append(_dn); |
| 135 | |
| 136 | sb.append("}"); |
| 137 | |
| 138 | return sb.toString(); |
| 139 | } |
| 140 | } |
| 141 |