Components | All | New | MacOS | Windows | Linux | iOS | ||||
Examples | Mac & Win | Server | Client | Guides | Statistic | FMM | Blog | Deprecated | Old |
LDAP.Connect
Initializes an LDAP connection.
Component | Version | macOS | Windows | Linux | Server | iOS SDK |
LDAP | 6.0 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ❌ No |
Parameters
Parameter | Description | Example |
---|---|---|
IP | The IP or domain name of the server. If prefixed with ldap:// or ldaps://, the plugin will remove that prefix. If prefix is ldaps://, the plugin will assume SSL is enabled. |
"192.168.2.222" |
SSL | Whether to use SSL. 1 to enable or 0 to not enable. If you start with 0 here, you can later use LDAP.StartTLS to enable TLS. |
1 |
Port | The port to use. If zero or undefined, the default port is used. e.g. 636 for LDAPS or 389 for LDAP. |
3889 |
Result
Returns reference number or error.
Description
Initializes an LDAP connection.Returns the references number which you pass to other LDAP functions.
Please call LDAP.Release later to close connection.
Before connecting, you can use EnvironmentVariables.SetValue function to set various flags for LDAP library, e.g. LDAPREFERRALS option. Please check LDAP documentation for which environment variables can be set.
Examples
Connect to local LDAP server:
MBS( "LDAP.Connect"; "localhost"; 0; 389 )
Connect to LDAP server:
MBS( "LDAP.Connect"; "ldap.internal"; 1)
Connect via IP and special port:
MBS( "LDAP.Connect"; "192.168.1.123"; 1; 3636)
Connect using URL:
MBS( "LDAP.Connect"; "ldaps://192.168.1.123")
Connect and Query:
Delete All Records [No dialog]
#Connect
Set Variable [$r; Value:MBS("LDAP.Connect"; LDAP Query::Server; LDAP Query::SSL; LDAP Query::Port)]
If [MBS("IsError")]
Show Custom Dialog ["LDAP error"; "Failed to connect." & ¶ & $r]
Exit Script []
Else
Set Variable [$ldap; Value:$r]
#Login
Set Variable [$r; Value:MBS("LDAP.Bind"; $ldap; LDAP Query::UserName; LDAP Query::Password; LDAP Query::AuthMethod)]
If [MBS("IsError")]
Show Custom Dialog ["LDAP error"; "Failed to authenticate." & ¶ & $r]
Else
#Search
Set Variable [$r; Value:MBS("LDAP.Search"; $ldap; LDAP Query::Base; LDAP Query::Scope; LDAP Query::Filter; ""; 0; 20; 999)]
#Check results
Set Variable [$EntryCount; Value:MBS("LDAP.SearchResult.Count"; $ldap)]
#Walk over all entries
Set Field [LDAP Query::Entry Count; $EntryCount]
If [$EntryCount > 0]
Set Variable [$EntryIndex; Value:0]
Loop
Set Variable [$EntryName; Value:MBS("LDAP.SearchResult.DistinguishedName"; $ldap; $EntryIndex)]
#Walk over all attributes
Set Variable [$AttributeCount; Value:MBS("LDAP.SearchResult.AttributeCount"; $ldap; $EntryIndex)]
If [$AttributeCount]
Set Variable [$AttributeIndex; Value:0]
Loop
#Check attribute name and value:
Set Variable [$AttributeName; Value:MBS("LDAP.SearchResult.AttributeName"; $ldap; $EntryIndex; $AttributeIndex)]
Set Variable [$AttributeValues; Value:MBS("LDAP.SearchResult.AttributeValues"; $ldap; $EntryIndex; $AttributeIndex; 1)]
#Store in a record:
New Record/Request
Set Field [LDAP Query::Entry; $EntryName]
Set Field [LDAP Query::Attribute; $AttributeName]
Set Field [LDAP Query::Values; $AttributeValues]
Commit Records/Requests [No dialog]
#next attribute
Set Variable [$AttributeIndex; Value:$AttributeIndex + 1]
Exit Loop If [$AttributeIndex = $AttributeCount]
End Loop
End If
#next entry
Set Variable [$EntryIndex; Value:$EntryIndex + 1]
Exit Loop If [$EntryIndex = $EntryCount]
End Loop
End If
End If
#Cleanup
Set Variable [$r; Value:MBS("LDAP.Release"; $ldap)]
End If
Example script to connect and bind:
# ========================================
# Purpose:
# Common routine to bind to the LDAP server
# Returns:
# $error = Error code if unsuccessful
# $error = 0 for success
# $resultText = Text summary of the success or error
# Parameters:
# $serverName
# $serverDomain
# Called from:
# (script) All "worker" LDAP scripts
# Author:
# John Munro (HJM) from Deutsche Schule Tokyo Yokohama
# Notes:
# none
# History:
# 2021-05-20 HJM - created
# ========================================
#
Set Variable [ $! ; Value: #Assign ( Get ( ScriptParameter ) ) ]
Set Variable [ $bindUsername ; Value: "filemakerbind" ]
Set Variable [ $bindPassword ; Value: “xxxxxxxxx” ]
#
Set Error Capture [ On ]
#
# If debugging these parameters will be empty so fill with test data
If [ $serverName = "" ]
Set Variable [ $serverName ; Value: "sys-xxx” ]
End If
If [ $serverDomain = "" ]
Set Variable [ $serverDomain ; Value: “xxx.com” ]
End If
#
Set Variable [ $serverFQDN ; Value: $serverName & "." & $serverDomain ]
#
# Connect
Set Variable [ $ssl ; Value: 1 ]
Set Variable [ $port ; Value: 636 ]
Set Variable [ $result ; Value: MBS( "LDAP.Connect" ; $serverFQDN ; $ssl ; $port ) ]
If [ MBS( "IsError" ) ]
// Show Custom Dialog [ "LDAP Error" ; "Failed to connect to Domain Controller." & ¶ & $result ]
Exit Script [ Text Result: # ( "error" ; $result ) & # ( "resultText" ; "LDAP error. Failed to connect to Domain Controller." & ¶ & $result ) ]
End If
#
# Login
Set Variable [ $ldap ; Value: $result ]
Set Variable [ $result ; Value: MBS("LDAP.Bind"; $ldap; $bindUsername & "@" & $serverDomain ; $bindPassword ; "simple") ]
If [ MBS( "IsError" ) ]
// Show Custom Dialog [ "LDAP Error" ; "Failed to authenticate." & ¶ & $result ]
Exit Script [ Text Result: # ( "error" ; $result ) & # ( "resultText" ; "LDAP error. Failed to authenticate." & ¶ & $result ) ]
End If
#
# Return error free result
Exit Script [ Text Result: # ( "error" ; 0 ) & # ( "resultText" ; "Bind successful" ) & # ( "ldap" ; $ldap ) ]
See also
- EnvironmentVariables.SetValue
- IsError
- LDAP.AddJSON
- LDAP.Bind
- LDAP.JSON
- LDAP.Search
- LDAP.SearchResult.Count
- LDAP.SearchResult.DistinguishedName
- LDAP.StartTLS
- TAPI.Connect
Example Databases
Blog Entries
This function checks for a license.
Created 15th December 2015, last changed 3th January 2024