| 1 |
/****************************************************************************** |
| 2 |
* The MIT License |
| 3 |
* Copyright (c) 2003 Novell Inc. www.novell.com |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
* of this software and associated documentation files (the Software), to deal |
| 7 |
* in the Software without restriction, including without limitation the rights |
| 8 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the Software is |
| 10 |
* furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in |
| 13 |
* all copies or substantial portions of the Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
* SOFTWARE. |
| 22 |
*******************************************************************************/ |
| 23 |
// |
| 24 |
// Novell.Directory.Ldap.Rfc2251.RfcLdapMessage.cs |
| 25 |
// |
| 26 |
// Author: |
| 27 |
// Sunil Kumar ([email protected]) |
| 28 |
// |
| 29 |
// (C) 2003 Novell, Inc (http://www.novell.com) |
| 30 |
// |
| 31 |
|
| 32 |
using System; |
| 33 |
using LdapException = Novell.Directory.Ldap.LdapException; |
| 34 |
using LdapMessage = Novell.Directory.Ldap.LdapMessage; |
| 35 |
using Novell.Directory.Ldap.Asn1; |
| 36 |
|
| 37 |
namespace Novell.Directory.Ldap.Rfc2251 |
| 38 |
{ |
| 39 |
|
| 40 |
/// <summary> Represents an Ldap Message. |
| 41 |
/// |
| 42 |
/// <pre> |
| 43 |
/// LdapMessage ::= SEQUENCE { |
| 44 |
/// messageID MessageID, |
| 45 |
/// protocolOp CHOICE { |
| 46 |
/// bindRequest BindRequest, |
| 47 |
/// bindResponse BindResponse, |
| 48 |
/// unbindRequest UnbindRequest, |
| 49 |
/// searchRequest SearchRequest, |
| 50 |
/// searchResEntry SearchResultEntry, |
| 51 |
/// searchResDone SearchResultDone, |
| 52 |
/// searchResRef SearchResultReference, |
| 53 |
/// modifyRequest ModifyRequest, |
| 54 |
/// modifyResponse ModifyResponse, |
| 55 |
/// addRequest AddRequest, |
| 56 |
/// addResponse AddResponse, |
| 57 |
/// delRequest DelRequest, |
| 58 |
/// delResponse DelResponse, |
| 59 |
/// modDNRequest ModifyDNRequest, |
| 60 |
/// modDNResponse ModifyDNResponse, |
| 61 |
/// compareRequest CompareRequest, |
| 62 |
/// compareResponse CompareResponse, |
| 63 |
/// abandonRequest AbandonRequest, |
| 64 |
/// extendedReq ExtendedRequest, |
| 65 |
/// extendedResp ExtendedResponse }, |
| 66 |
/// controls [0] Controls OPTIONAL } |
| 67 |
/// </pre> |
| 68 |
/// |
| 69 |
/// |
| 70 |
/// Note: The creation of a MessageID should be hidden within the creation of |
| 71 |
/// an RfcLdapMessage. The MessageID needs to be in sequence, and has an |
| 72 |
/// upper and lower limit. There is never a case when a user should be |
| 73 |
/// able to specify the MessageID for an RfcLdapMessage. The MessageID() |
| 74 |
/// constructor should be package protected. (So the MessageID value |
| 75 |
/// isn't arbitrarily run up.) |
| 76 |
/// </summary> |
| 77 |
public class RfcLdapMessage:Asn1Sequence |
| 78 |
{ |
| 79 |
/// <summary> Returns this RfcLdapMessage's messageID as an int.</summary> |
| 80 |
virtual public int MessageID |
| 81 |
{ |
| 82 |
get |
| 83 |
{ |
| 84 |
return ((Asn1Integer) get_Renamed(0)).intValue(); |
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
/// <summary> Returns this RfcLdapMessage's message type</summary> |
| 89 |
virtual public int Type |
| 90 |
{ |
| 91 |
get |
| 92 |
{ |
| 93 |
return get_Renamed(1).getIdentifier().Tag; |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
/// <summary> Returns the response associated with this RfcLdapMessage. |
| 98 |
/// Can be one of RfcLdapResult, RfcBindResponse, RfcExtendedResponse |
| 99 |
/// all which extend RfcResponse. It can also be |
| 100 |
/// RfcSearchResultEntry, or RfcSearchResultReference |
| 101 |
/// </summary> |
| 102 |
virtual public Asn1Object Response |
| 103 |
{ |
| 104 |
get |
| 105 |
{ |
| 106 |
return get_Renamed(1); |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
/// <summary> Returns the optional Controls for this RfcLdapMessage.</summary> |
| 111 |
virtual public RfcControls Controls |
| 112 |
{ |
| 113 |
get |
| 114 |
{ |
| 115 |
if (size() > 2) |
| 116 |
return (RfcControls) get_Renamed(2); |
| 117 |
return null; |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
/// <summary> Returns the dn of the request, may be null</summary> |
| 122 |
virtual public System.String RequestDN |
| 123 |
{ |
| 124 |
get |
| 125 |
{ |
| 126 |
return ((RfcRequest) op).getRequestDN(); |
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
/// <summary> returns the original request in this message |
| 131 |
/// |
| 132 |
/// </summary> |
| 133 |
/// <returns> the original msg request for this response |
| 134 |
/// </returns> |
| 135 |
/// <summary> sets the original request in this message |
| 136 |
/// |
| 137 |
/// </summary> |
| 138 |
/// <param name="msg">the original request for this response |
| 139 |
/// </param> |
| 140 |
virtual public LdapMessage RequestingMessage |
| 141 |
{ |
| 142 |
get |
| 143 |
{ |
| 144 |
return requestMessage; |
| 145 |
} |
| 146 |
|
| 147 |
set |
| 148 |
{ |
| 149 |
requestMessage = value; |
| 150 |
return ; |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
private Asn1Object op; |
| 156 |
private RfcControls controls; |
| 157 |
private LdapMessage requestMessage = null; |
| 158 |
|
| 159 |
/// <summary> Create an RfcLdapMessage by copying the content array |
| 160 |
/// |
| 161 |
/// </summary> |
| 162 |
/// <param name="origContent">the array list to copy |
| 163 |
/// </param> |
| 164 |
/* package */ |
| 165 |
internal RfcLdapMessage(Asn1Object[] origContent, RfcRequest origRequest, System.String dn, System.String filter, bool reference):base(origContent, origContent.Length) |
| 166 |
{ |
| 167 |
|
| 168 |
set_Renamed(0, new RfcMessageID()); // MessageID has static counter |
| 169 |
|
| 170 |
RfcRequest req = (RfcRequest) origContent[1]; |
| 171 |
RfcRequest newreq = req.dupRequest(dn, filter, reference); |
| 172 |
op = (Asn1Object) newreq; |
| 173 |
set_Renamed(1, (Asn1Object) newreq); |
| 174 |
|
| 175 |
return ; |
| 176 |
} |
| 177 |
|
| 178 |
/// <summary> Create an RfcLdapMessage using the specified Ldap Request.</summary> |
| 179 |
public RfcLdapMessage(RfcRequest op):this(op, null) |
| 180 |
{ |
| 181 |
return ; |
| 182 |
} |
| 183 |
|
| 184 |
/// <summary> Create an RfcLdapMessage request from input parameters.</summary> |
| 185 |
public RfcLdapMessage(RfcRequest op, RfcControls controls):base(3) |
| 186 |
{ |
| 187 |
|
| 188 |
this.op = (Asn1Object) op; |
| 189 |
this.controls = controls; |
| 190 |
|
| 191 |
add(new RfcMessageID()); // MessageID has static counter |
| 192 |
add((Asn1Object) op); |
| 193 |
if (controls != null) |
| 194 |
{ |
| 195 |
add(controls); |
| 196 |
} |
| 197 |
return ; |
| 198 |
} |
| 199 |
|
| 200 |
/// <summary> Create an RfcLdapMessage using the specified Ldap Response.</summary> |
| 201 |
public RfcLdapMessage(Asn1Sequence op):this(op, null) |
| 202 |
{ |
| 203 |
return ; |
| 204 |
} |
| 205 |
|
| 206 |
/// <summary> Create an RfcLdapMessage response from input parameters.</summary> |
| 207 |
public RfcLdapMessage(Asn1Sequence op, RfcControls controls):base(3) |
| 208 |
{ |
| 209 |
|
| 210 |
this.op = op; |
| 211 |
this.controls = controls; |
| 212 |
|
| 213 |
add(new RfcMessageID()); // MessageID has static counter |
| 214 |
add(op); |
| 215 |
if (controls != null) |
| 216 |
{ |
| 217 |
add(controls); |
| 218 |
} |
| 219 |
return ; |
| 220 |
} |
| 221 |
|
| 222 |
/// <summary> Will decode an RfcLdapMessage directly from an InputStream.</summary> |
| 223 |
[CLSCompliantAttribute(false)] |
| 224 |
public RfcLdapMessage(Asn1Decoder dec, System.IO.Stream in_Renamed, int len):base(dec, in_Renamed, len) |
| 225 |
{ |
| 226 |
|
| 227 |
sbyte[] content; |
| 228 |
System.IO.MemoryStream bais; |
| 229 |
|
| 230 |
// Decode implicitly tagged protocol operation from an Asn1Tagged type |
| 231 |
// to its appropriate application type. |
| 232 |
Asn1Tagged protocolOp = (Asn1Tagged) get_Renamed(1); |
| 233 |
Asn1Identifier protocolOpId = protocolOp.getIdentifier(); |
| 234 |
content = ((Asn1OctetString) protocolOp.taggedValue()).byteValue(); |
| 235 |
bais = new System.IO.MemoryStream(SupportClass.ToByteArray(content)); |
| 236 |
|
| 237 |
switch (protocolOpId.Tag) |
| 238 |
{ |
| 239 |
|
| 240 |
case LdapMessage.SEARCH_RESPONSE: |
| 241 |
set_Renamed(1, new RfcSearchResultEntry(dec, bais, content.Length)); |
| 242 |
break; |
| 243 |
|
| 244 |
case LdapMessage.SEARCH_RESULT: |
| 245 |
set_Renamed(1, new RfcSearchResultDone(dec, bais, content.Length)); |
| 246 |
break; |
| 247 |
|
| 248 |
case LdapMessage.SEARCH_RESULT_REFERENCE: |
| 249 |
set_Renamed(1, new RfcSearchResultReference(dec, bais, content.Length)); |
| 250 |
break; |
| 251 |
|
| 252 |
case LdapMessage.ADD_RESPONSE: |
| 253 |
set_Renamed(1, new RfcAddResponse(dec, bais, content.Length)); |
| 254 |
break; |
| 255 |
|
| 256 |
case LdapMessage.BIND_RESPONSE: |
| 257 |
set_Renamed(1, new RfcBindResponse(dec, bais, content.Length)); |
| 258 |
break; |
| 259 |
|
| 260 |
case LdapMessage.COMPARE_RESPONSE: |
| 261 |
set_Renamed(1, new RfcCompareResponse(dec, bais, content.Length)); |
| 262 |
break; |
| 263 |
|
| 264 |
case LdapMessage.DEL_RESPONSE: |
| 265 |
set_Renamed(1, new RfcDelResponse(dec, bais, content.Length)); |
| 266 |
break; |
| 267 |
|
| 268 |
case LdapMessage.EXTENDED_RESPONSE: |
| 269 |
set_Renamed(1, new RfcExtendedResponse(dec, bais, content.Length)); |
| 270 |
break; |
| 271 |
|
| 272 |
case LdapMessage.INTERMEDIATE_RESPONSE: |
| 273 |
set_Renamed(1, new RfcIntermediateResponse(dec, bais, content.Length)); |
| 274 |
break; |
| 275 |
|
| 276 |
case LdapMessage.MODIFY_RESPONSE: |
| 277 |
set_Renamed(1, new RfcModifyResponse(dec, bais, content.Length)); |
| 278 |
break; |
| 279 |
|
| 280 |
case LdapMessage.MODIFY_RDN_RESPONSE: |
| 281 |
set_Renamed(1, new RfcModifyDNResponse(dec, bais, content.Length)); |
| 282 |
break; |
| 283 |
|
| 284 |
default: |
| 285 |
throw new System.SystemException("RfcLdapMessage: Invalid tag: " + protocolOpId.Tag); |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
// decode optional implicitly tagged controls from Asn1Tagged type to |
| 290 |
// to RFC 2251 types. |
| 291 |
if (size() > 2) |
| 292 |
{ |
| 293 |
Asn1Tagged controls = (Asn1Tagged) get_Renamed(2); |
| 294 |
// Asn1Identifier controlsId = protocolOp.getIdentifier(); |
| 295 |
// we could check to make sure we have controls here.... |
| 296 |
|
| 297 |
content = ((Asn1OctetString) controls.taggedValue()).byteValue(); |
| 298 |
bais = new System.IO.MemoryStream(SupportClass.ToByteArray(content)); |
| 299 |
set_Renamed(2, new RfcControls(dec, bais, content.Length)); |
| 300 |
} |
| 301 |
return ; |
| 302 |
} |
| 303 |
|
| 304 |
//************************************************************************* |
| 305 |
// Accessors |
| 306 |
//************************************************************************* |
| 307 |
|
| 308 |
/// <summary> Returns the request associated with this RfcLdapMessage. |
| 309 |
/// Throws a class cast exception if the RfcLdapMessage is not a request. |
| 310 |
/// </summary> |
| 311 |
public RfcRequest getRequest() |
| 312 |
{ |
| 313 |
return (RfcRequest) get_Renamed(1); |
| 314 |
} |
| 315 |
|
| 316 |
public virtual bool isRequest() |
| 317 |
{ |
| 318 |
return get_Renamed(1) is RfcRequest; |
| 319 |
} |
| 320 |
|
| 321 |
/// <summary> Duplicate this message, replacing base dn, filter, and scope if supplied |
| 322 |
/// |
| 323 |
/// </summary> |
| 324 |
/// <param name="dn">the base dn |
| 325 |
/// |
| 326 |
/// </param> |
| 327 |
/// <param name="filter">the filter |
| 328 |
/// |
| 329 |
/// </param> |
| 330 |
/// <param name="reference">true if a search reference |
| 331 |
/// |
| 332 |
/// </param> |
| 333 |
/// <returns> the object representing the new message |
| 334 |
/// </returns> |
| 335 |
public System.Object dupMessage(System.String dn, System.String filter, bool reference) |
| 336 |
{ |
| 337 |
if ((op == null)) |
| 338 |
{ |
| 339 |
throw new LdapException("DUP_ERROR", LdapException.LOCAL_ERROR, (System.String) null); |
| 340 |
} |
| 341 |
|
| 342 |
RfcLdapMessage newMsg = new RfcLdapMessage(toArray(), (RfcRequest) get_Renamed(1), dn, filter, reference); |
| 343 |
return newMsg; |
| 344 |
} |
| 345 |
} |
| 346 |
} |