Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function to escape illegal chars in LDAP DN. #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 132 additions & 38 deletions src/LyncConnector/Scripts/7. ImportScript-Lync.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,89 @@ param(

Set-StrictMode -Version "2.0"

$commonModule = (Join-Path -Path $ScriptDir -ChildPath $ConfigParameters["Common Module Script Name (with extension)"].Value)
Function Escape-IllegalCharacters {

param(
[parameter(Mandatory = $true)]
[string]$DN
)

$l = $DN.Length
$i = 0
$parts = New-Object System.Collections.ArrayList
$delims = @("OU=","DC=","CN=")
$illegalChars = ',\/#+<>;"='.ToCharArray()
$charArr = $DN.ToCharArray()
$tempString = ""
$newDN = ""

do {

if ($charArr[$i] -eq ",") {
# Check if end of part base on next 3 chars
$endOfPart=$false
if ($i+3 -lt $l) {
#not at end of string
$nextThree = ($charArr[$i+1] + $charArr[$i+2] + $charArr[$i+3]).ToUpper()
if ($delims.Contains($nextThree)) {
$endOfPart = $true
}
}
if ($endOfPart) {
$parts.Add($tempString) | out-null
$tempString = ""
$i++
}
else {
$tempString += $charArr[$i]
$i++
}
}
else {
# Add to tempString
$tempString += $charArr[$i]
if ($i -eq $l-1) {
$parts.Add($tempString) | out-null
}
$i++
}


}
while ($i -lt $l)

foreach ($part in $parts) {
$type = $part.Substring(0,3)
$value = $part.Substring(3)
$newValue = ""
foreach ($char in $value.ToCharArray()) {
if ($illegalChars.Contains($char)) {
$newValue += ("\"+$char)
}
else {
$newValue+=$char
}
}
$part = $type + $newValue
if ($newDN -ne "") {
$newDN += ","
}
$newDN+=$part
}

$newDN
}

if (!(Get-Module -Name (Get-Item $commonModule).BaseName)) { Import-Module -Name $commonModule }

Enter-Script -ScriptType "Import" -ErrorObject $Error
Function Write-MIMLog ([string]$m) {
$logFile = Join-Path -Path ([Microsoft.MetadirectoryServices.MAUtils]::MAFolder) -ChildPath "PSMA.txt"
Write-Verbose $m
$logOn = $true
if ($logOn) {
$d = [string](Get-Date -Format s)
($d + " " + $m) | Out-File $logFile -Append
}
}

function Import-CSEntries
{
Expand Down Expand Up @@ -343,7 +421,7 @@ function Get-OrganizationalUnits

foreach ($includedNode in $openImportConnectionRunStep.InclusionHierarchyNodes)
{
$rootDN = $includedNode.DN
$rootDN = Escape-IllegalCharacters -DN $includedNode.DN

Write-Debug "Enumerating Inclusion OrganizationalUnit Hierarchy for $rootDN"

Expand All @@ -353,7 +431,7 @@ function Get-OrganizationalUnits
$excludedNodeOUs = @()
foreach ($exludedNode in $openImportConnectionRunStep.ExclusionHierarchyNodes)
{
$rootDN = $includedNode.DN
$rootDN = Escape-IllegalCharacters -DN $includedNode.DN

Write-Debug "Enumerating Exclusion OrganizationalUnit Hierarchy for $rootDN"

Expand Down Expand Up @@ -454,46 +532,62 @@ function Get-OrganizationalUnitHierarchy
return $organizationalUnits
}

$userPages = Get-ConfigParameter -ConfigParameters $ConfigParameters -ParameterName "UserPages"
try {

if ([string]::IsNullOrEmpty($userPages))
{
$userPages = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9".Split(",")
}
else
{
$userPages = $userPages.Split(",")
}


$commonModule = (Join-Path -Path $ScriptDir -ChildPath $ConfigParameters["Common Module Script Name (with extension)"].Value)

$ouPages = Get-ConfigParameter -ConfigParameters $ConfigParameters -ParameterName "OrganizationalUnitPages"
if ([string]::IsNullOrEmpty($ouPages))
{
$ouPages = @("")
}
else
{
$ouPages = $ouPages.Split(",")
}
if (!(Get-Module -Name (Get-Item $commonModule).BaseName)) { Import-Module -Name $commonModule }

$lastRunDateTimeOffset = Get-ConfigParameter -ConfigParameters $ConfigParameters -ParameterName "LastRunDateTimeOffsetMinutes"
Enter-Script -ScriptType "Import" -ErrorObject $Error

if ([string]::IsNullOrEmpty($lastRunDateTimeOffset))
{
$lastRunDateTimeOffset = 30 # in minutes
}
else
{
$lastRunDateTimeOffset = $lastRunDateTimeOffset.Trim()
}

$deltaImport = $openImportConnectionRunStep.ImportType -eq "Delta"
$customData = [xml]$getImportEntriesRunStep.CustomData
$userPages = Get-ConfigParameter -ConfigParameters $ConfigParameters -ParameterName "UserPages"

$preferredDomainController = $customData.WaterMark.PreferredDomainController
if ([string]::IsNullOrEmpty($userPages))
{
$userPages = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9".Split(",")
}
else
{
$userPages = $userPages.Split(",")
}

Write-Debug ("GetImportEntriesRunStep.CustomData received is: {0}" -f $customData.InnerXml)
$ouPages = Get-ConfigParameter -ConfigParameters $ConfigParameters -ParameterName "OrganizationalUnitPages"
if ([string]::IsNullOrEmpty($ouPages))
{
$ouPages = @("")
}
else
{
$ouPages = $ouPages.Split(",")
}

Import-CSEntries
$lastRunDateTimeOffset = Get-ConfigParameter -ConfigParameters $ConfigParameters -ParameterName "LastRunDateTimeOffsetMinutes"

Exit-Script -ScriptType "Import" -ErrorObject $Error
if ([string]::IsNullOrEmpty($lastRunDateTimeOffset))
{
$lastRunDateTimeOffset = 30 # in minutes
}
else
{
$lastRunDateTimeOffset = $lastRunDateTimeOffset.Trim()
}

$deltaImport = $openImportConnectionRunStep.ImportType -eq "Delta"
$customData = [xml]$getImportEntriesRunStep.CustomData

$preferredDomainController = $customData.WaterMark.PreferredDomainController

Write-Debug ("GetImportEntriesRunStep.CustomData received is: {0}" -f $customData.InnerXml)

Import-CSEntries

Exit-Script -ScriptType "Import" -ErrorObject $Error
}

catch {
$ErrorMessage = $_.Exception.Message
Write-MIMLog "ERROR!! Exception caught: $ErrorMessage"
}