Skip to content

Instantly share code, notes, and snippets.

@cowlike
Created March 21, 2019 18:03
Show Gist options
  • Save cowlike/0ccc45019db82706830c9f8b4131b1cd to your computer and use it in GitHub Desktop.
Save cowlike/0ccc45019db82706830c9f8b4131b1cd to your computer and use it in GitHub Desktop.
wrap Novell.Directory.Ldap
module Ldap
open System
open TMon
open Novell.Directory.Ldap
open Novell.Directory.Ldap.Utilclass
type AttrValue =
String of string
| StringArray of string []
| ByteArray of byte []
| ByteArrayArray of byte [] []
type AttrName = string
type Attr = {
name: AttrName
value: AttrValue }
type Entry = Attributes of Attr []
let b64Encode (b: byte []) = Base64.Encode b
let private toStr = function
| String s -> s
| StringArray sa -> String.Join("\n\t", sa)
| ByteArray b -> Base64.Encode b
| ByteArrayArray ba -> String.Join("\n\t", Array.map b64Encode ba)
let private binaryList = [
"photo"
"personalSignature"
"audio"
"jpegPhoto"
"javaSerializedData"
"thumbnailPhoto"
"thumbnailLogo"
"userPassword"
"userCertificate"
"cACertificate"
"authorityRevocationList"
"certificateRevocationList"
"crossCertificatePair"
"x500UniqueIdentifier"
"msExchMailboxGuid"
"objectSid"
"objectGUID" ]
let private toAttr (a:LdapAttribute) =
{ name = a.Name
value =
if List.contains a.Name binaryList then
if a.Size() = 1 then ByteArray a.ByteValue
else ByteArrayArray a.ByteValueArray
else
if a.Size() = 1 then String a.StringValue
else StringArray a.StringValueArray }
let getConn host port user pw =
try
let conn = new LdapConnection()
conn.SecureSocketLayer <- true
conn.Connect(host, port)
conn.Bind(user, pw)
Success conn
with e -> Fail e.Message
let private allAttributes (entry: LdapEntry) =
entry.GetAttributeSet().ToArray()
|> Array.map (fun (x: obj) -> x :?> LdapAttribute |> toAttr)
let showEntry (Attributes attributes) =
attributes
|> Array.map (fun a -> sprintf "%s = %s" a.name (toStr a.value))
let search (conn: LdapConnection) searchBase searchTerm attrs =
conn.Search(searchBase, LdapConnection.ScopeSub, searchTerm, attrs, false)
|> seq
|> Seq.map (allAttributes >> Attributes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment