| 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.client.opers; |
| 39 | |
| 40 | import java.io.UnsupportedEncodingException; |
| 41 | |
| 42 | import org.ietf.ldap.ber.stream.BERElement; |
| 43 | import org.ietf.ldap.ber.stream.BERInteger; |
| 44 | import org.ietf.ldap.ber.stream.BEROctetString; |
| 45 | import org.ietf.ldap.ber.stream.BERSequence; |
| 46 | import org.ietf.ldap.ber.stream.BERTag; |
| 47 | |
| 48 | /** |
| 49 | * This class implements the bind request. This object is |
| 50 | * sent to the ldap server. |
| 51 | * <pre> |
| 52 | * BindRequest ::= [APPLICATION 0] SEQUENCE { |
| 53 | * version INTEGER(1..127) , |
| 54 | * name LDAPDN, |
| 55 | * authentication CHOICE { |
| 56 | * simple [0] OCTET STRING, |
| 57 | * krbv42LDAP [1] OCTET STRING, |
| 58 | * krbv42DSA [2] OCTET STRING |
| 59 | * } |
| 60 | * } |
| 61 | * </pre> |
| 62 | * |
| 63 | * Note that LDAPv3 bind reuqest is structured as follows: |
| 64 | * <pre> |
| 65 | * BindRequest ::= [APPLICATION 0] SEQUENCE { |
| 66 | * version INTEGER (1..127) |
| 67 | * name LDAPDN, |
| 68 | * authentication AuthenticationChoice |
| 69 | * } |
| 70 | * AuthenticationChoice ::= CHOICE { |
| 71 | * simple [0] OCTET STRING, |
| 72 | * -- 1 and 2 reserved |
| 73 | * sasl [3] SaslCredentials |
| 74 | * } |
| 75 | * SaslCredentials ::= SEQUENCE { |
| 76 | * mechanism LDAPString, |
| 77 | * credentials OCTET STRING |
| 78 | * } |
| 79 | * </pre> |
| 80 | * |
| 81 | * @version 1.0 |
| 82 | */ |
| 83 | public class JDAPBindRequest implements JDAPProtocolOp { |
| 84 | /** |
| 85 | * Internal variables |
| 86 | */ |
| 87 | protected int m_version; |
| 88 | protected String m_name = null; |
| 89 | protected byte[] m_password = null; |
| 90 | protected String m_mechanism = null; |
| 91 | |
| 92 | /** |
| 93 | * Constructs anonymous or simple bind request. |
| 94 | * @param version version |
| 95 | * @param name distinguished name |
| 96 | * @param password password |
| 97 | */ |
| 98 | public JDAPBindRequest(int version, String name, String password) { |
| 99 | m_version = version; |
| 100 | m_name = name; |
| 101 | try { |
| 102 | m_password = password.getBytes("UTF8"); |
| 103 | } catch ( UnsupportedEncodingException e ) { |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Constructs anonymous or simple bind request. |
| 109 | * @param version version |
| 110 | * @param name distinguished name |
| 111 | * @param password password |
| 112 | */ |
| 113 | public JDAPBindRequest(int version, String name, byte[] password) { |
| 114 | m_version = version; |
| 115 | m_name = name; |
| 116 | m_password = password; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Constructs a LDAP v3.0 SaslCredentials bind request. |
| 121 | * @param version version |
| 122 | * @param name distinguished name |
| 123 | * @param mechanism mechanism (must not be null) |
| 124 | * @param credentials credientials |
| 125 | */ |
| 126 | public JDAPBindRequest(int version, String name, String mechanism, |
| 127 | byte credentials[]) { |
| 128 | m_version = version; |
| 129 | m_name = name; |
| 130 | m_mechanism = mechanism; |
| 131 | m_password = credentials; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Retrieves the protocol operation type. |
| 136 | * @return protocol type |
| 137 | */ |
| 138 | public int getType() { |
| 139 | return JDAPProtocolOp.BIND_REQUEST; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Retrieves the ber representation of the request. |
| 144 | * @return ber representation |
| 145 | */ |
| 146 | public BERElement getBERElement() { |
| 147 | /* anonymous bind |
| 148 | * [*] umich-ldap-v3.3: |
| 149 | * 0x60 0x07 (implicit [Application 0] Sequence) |
| 150 | * 0x02 0x01 0x02 (Integer) |
| 151 | * 0x04 0x00 (OctetString) |
| 152 | * 0x80 0x00 (implicit OctetString) |
| 153 | * [*] zoomit server v1.0: |
| 154 | * 0x60 0x0b |
| 155 | * 0x30 0x09 (sequece) |
| 156 | * 0x02 0x01 0x02 |
| 157 | * 0x04 0x00 |
| 158 | * 0xa0 0x02 (explicit tag) |
| 159 | * 0x04 0x00 |
| 160 | * simple bind with "cn=root,o=ncware,c=ca", "password" |
| 161 | * [*] umich-ldap-v3.3: |
| 162 | * 0x60 0x24 ([APPLICATION 0]) |
| 163 | * 0x02 0x01 0x02 (version - Integer) |
| 164 | * 0x04 0x15 c n = r o o t , o = n c w a r e , |
| 165 | * c = c a |
| 166 | * 0x80 0x08 p a s s w o r d |
| 167 | */ |
| 168 | BERSequence seq = new BERSequence(); |
| 169 | seq.addElement(new BERInteger(m_version)); |
| 170 | seq.addElement(new BEROctetString(m_name)); |
| 171 | BERTag auth = null; |
| 172 | if (m_mechanism == null) { |
| 173 | auth = new BERTag(BERTag.CONTEXT, new BEROctetString(m_password), |
| 174 | true); |
| 175 | } else { |
| 176 | BERSequence sasl = new BERSequence(); |
| 177 | sasl.addElement(new BEROctetString(m_mechanism)); |
| 178 | if (m_password == null) { |
| 179 | sasl.addElement(new BEROctetString((byte[])null)); |
| 180 | } else { |
| 181 | sasl.addElement(new BEROctetString(m_password, 0, |
| 182 | m_password.length)); |
| 183 | } |
| 184 | auth = new BERTag(BERTag.SASLCONTEXT|3, /* SaslCredentials */ |
| 185 | sasl, true); |
| 186 | } |
| 187 | seq.addElement(auth); |
| 188 | BERTag element = new BERTag(BERTag.APPLICATION|BERTag.CONSTRUCTED|0, |
| 189 | seq, true); |
| 190 | return element; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Retrieves the string representation of the request parameters. |
| 195 | * @return string representation parameters |
| 196 | */ |
| 197 | public String getParamString() { |
| 198 | return "{version=" + m_version + ", name=" + m_name + |
| 199 | ", mechanism=" + m_mechanism + |
| 200 | ", authentication=" + |
| 201 | ((m_password == null) ? "null" : /*"********"*/new String(m_password)) + "}"; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Retrieves the string representation of the request. |
| 206 | * @return string representation |
| 207 | */ |
| 208 | public String toString() { |
| 209 | return "BindRequest " + getParamString(); |
| 210 | } |
| 211 | } |