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

Enable domain and local part substitution in Search Filter #52

Open
wants to merge 1 commit 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
111 changes: 92 additions & 19 deletions src/auth-ldap.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,42 +124,115 @@
return (result);
}

static TRString *createSearchFilter(TRString *template, const char *username) {
TRString *templateString;
TRString *result, *part;
TRString *quotedName;
const char userFormat[] = "%u";
static TRString *createSearchFilter(TRString *template, const char *userName) {
TRString *templateString, *tempResult, part;
TRString *result;
TRString *login, *local, *domain;
TRString *quotedLogin, *quotedLocal, *quotedDomain;
const char loginFormat[] = "%u";
const char localFormat[] = "%n";
const char domainFormat[] = "%d";
const char separator[] = "@";
TRAutoreleasePool *pool = [[TRAutoreleasePool alloc] init];

/* Copy the template */
/* Initialize the working areas */
templateString = [[[TRString alloc] initWithString: template] autorelease];

/* Initialize the result */
tempResult = [[[TRString alloc] init] autorelease];
login = [[[TRString alloc] initWithCString: userName] autorelease];
local = [[[TRString alloc] init] autorelease];
domain = [[[TRString alloc] init] autorelease];
result = [[TRString alloc] init];

/* Split login into local and domain part, if possible */
if (local = [login substringToCString: separator] == NULL) {
local = login;
} else {
domain = [login substringFromCString: separator];
}

/* Quote the username */
quotedName = quoteForSearch(username);
/* Quote the different parts */
quotedLogin = quoteForSearch(login cString);
quotedLocal = quoteForSearch(local cString);
quotedDomain = quoteForSearch(domain cString);

while ((part = [templateString substringToCString: userFormat]) != NULL) {
TRString *temp;

/* Replace %u with login */
while ((part = [templateString substringToCString: loginFormat]) != NULL) {
TRString *tempLogin;

/* Append everything until the first %u */
[result appendString: part];
[tempResult appendString: part];

/* Append the username */
[result appendString: quotedName];
/* Append the login */
[tempResult appendString: quotedLogin];

/* Move templateString past the %u */
temp = [templateString substringFromCString: userFormat];
templateString = temp;
tempLogin = [templateString substringFromCString: loginFormat];
templateString = tempLogin;
}

[quotedLogin release];

/* Append the remainder, if any */
if (templateString) {
[tempResult appendString: templateString];
}

templateString = tempResult;
tempResult = "";


/* Replace %n with local part */
while ((part = [templateString substringToCString: localFormat]) != NULL) {
TRString *tempLocal;

/* Append everything until the first %n */
[tempResult appendString: part];

/* Append the local part */
[tempResult appendString: quotedLocal];

/* Move templateString past the %n */
tempLocal = [templateString substringFromCString: localFormat];
templateString = tempLocal;
}

[quotedName release];
[quotedLocal release];

/* Append the remainder, if any */
if (templateString) {
[result appendString: templateString];
[tempResult appendString: templateString];
}

templateString = tempResult;
tempResult = "";


/* Replace %d with domain part */
while ((part = [templateString substringToCString: domainFormat]) != NULL) {
TRString *tempDomain;

/* Append everything until the first %d */
[tempResult appendString: part];

/* Append the domain part */
[tempResult appendString: quotedDomain];

/* Move templateString past the %d */
tempDomain = [templateString substringFromCString: domainFormat];
templateString = tempDomain;
}

[quotedDomain release];

/* Append the remainder, if any */
if (templateString) {
[tempResult appendString: templateString];
}

result = tempResult;
tempResult = "";


[pool release];

Expand Down