lightweight directory access protocol (ldap)

52
1 Internet Based Applications Internet Based Applications Lightweight Lightweight Directory Access Directory Access Protocol (LDAP) Protocol (LDAP) Piotr Wierzejewski

Upload: razi

Post on 23-Jan-2016

53 views

Category:

Documents


0 download

DESCRIPTION

Lightweight Directory Access Protocol (LDAP). Piotr Wierzejewski. Directory Services. A "directory" service is a network accessible database: Small amount of information in each request/reply. Limited functionality (as compared to a complete database system) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lightweight Directory Access Protocol (LDAP)

1

Internet Based ApplicationsInternet Based Applications

Lightweight Directory Lightweight Directory Access Protocol Access Protocol

(LDAP)(LDAP)

Lightweight Directory Lightweight Directory Access Protocol Access Protocol

(LDAP)(LDAP)

Piotr Wierzejewski

Page 2: Lightweight Directory Access Protocol (LDAP)

2http://www.man.poznan.pl/~wierzej/

Directory ServicesDirectory ServicesDirectory ServicesDirectory Services

• A "directory" service is a network accessible database:

Small amount of information in each request/reply.Limited functionality (as compared to a complete database system)Updates (changes) are much less frequent than queries.

Page 3: Lightweight Directory Access Protocol (LDAP)

3http://www.man.poznan.pl/~wierzej/

DirectoriesDirectoriesDirectoriesDirectories

• Some typical examples include:telephone directorieslists of addresses (email, network, P.O., etc)

• Each record is referenced by a unique key:given a name, look up a phone numbergiven a name, look up an email address

Page 4: Lightweight Directory Access Protocol (LDAP)

4http://www.man.poznan.pl/~wierzej/

ApplicationsApplicationsApplicationsApplications

• Some applications simply provide a front-end to a directory service.

Electronic phone book.

• Some applications use a directory service to store configuration information, auxiliary databases,etc.

Page 5: Lightweight Directory Access Protocol (LDAP)

5http://www.man.poznan.pl/~wierzej/

Information StructureInformation StructureInformation StructureInformation Structure

• Typically, the information in a directory is structured hierarchically (but it doesn't have to be).

• The structure of the data (the hierarchy) is often useful in finding data and provides some (minimal) relationship between records.

Page 6: Lightweight Directory Access Protocol (LDAP)

6http://www.man.poznan.pl/~wierzej/

Example: DNSExample: DNSExample: DNSExample: DNS

The Domain Name System is an example of a directory:

• hierarchical structure

• for each item there is a unique key (the hostname) and a number of attributes:

IP addressMail exchangerHost informationetc...

Page 7: Lightweight Directory Access Protocol (LDAP)

7http://www.man.poznan.pl/~wierzej/

X.500X.500X.500X.500

• X.500 is a Directory Service that has been used for a while:

Based on O.S.I. Protocol Stack

requires upper layers (above transport) of the OSI Stack

Heavyweight service (protocol).

Page 8: Lightweight Directory Access Protocol (LDAP)

8http://www.man.poznan.pl/~wierzej/

LDAPLDAPLDAPLDAP

• A number of lightweight front-ends to X.500 have been developed - the most recent is LDAP:

Lightweight Directory Access ProtocolBased on TCP (but can be mapped to other protocols).90% of the functionality of X.50010% of the cost

Page 9: Lightweight Directory Access Protocol (LDAP)

9http://www.man.poznan.pl/~wierzej/

LDAP & U. of MichiganLDAP & U. of MichiganLDAP & U. of MichiganLDAP & U. of Michigan

• LDAP originated at the University of Michigan.

• LDAP can be used as a front-end to X.500 or stand-alone.

• LDAP is now available commercially from a number of sources (including Netscape)

Page 10: Lightweight Directory Access Protocol (LDAP)

10http://www.man.poznan.pl/~wierzej/

LDAP definitionLDAP definitionLDAP definitionLDAP definition

• RFC 1777: data representation schemedefined operations and mapping to requests/response protocol.

• RFC 1823: Application Programming Interface (has become a standard)

API provided – no sockets programming required!

Page 11: Lightweight Directory Access Protocol (LDAP)

11http://www.man.poznan.pl/~wierzej/

LDAP Data RepresentationLDAP Data RepresentationLDAP Data RepresentationLDAP Data Representation

• Each record has a unique key called a distinguished name (dn for short).

• A distinguished name (RFC 1779) is meant to be used by humans (not just computers).

• Each dn is a sequence of components.Each component is a string containing an attribute=value pair.

Page 12: Lightweight Directory Access Protocol (LDAP)

12http://www.man.poznan.pl/~wierzej/

Example DNExample DNExample DNExample DN

CN=Dave Hollinger,

OU=Computer Science,

O=Rensselaer Polytechnic Institute,

C=US

Typically written all on one line.

Page 13: Lightweight Directory Access Protocol (LDAP)

13http://www.man.poznan.pl/~wierzej/

HierarchyHierarchyHierarchyHierarchy

• Like Domain Names, the name can be interpreted as part of a hierarchy.

• The last component of the dn is at the highest level in the hierarchy.

CN=Joe Integral, OU=Math, O=RPI, C=US

Page 14: Lightweight Directory Access Protocol (LDAP)

14http://www.man.poznan.pl/~wierzej/

Sample HierarchySample HierarchySample HierarchySample Hierarchy

C=US

O=RPIO=MIT

OU=Computer ScienceOU=Math

CN=Dave Hollinger

Page 15: Lightweight Directory Access Protocol (LDAP)

15http://www.man.poznan.pl/~wierzej/

Component NamesComponent NamesComponent NamesComponent Names

• The components can be anything, but there is a standard hierarchy used (for a global LDAP namespace):

C country nameO organization nameOU organizational unitCN common nameL locality nameST state or provinceSTREET street address

Page 16: Lightweight Directory Access Protocol (LDAP)

16http://www.man.poznan.pl/~wierzej/

Relative DNsRelative DNsRelative DNsRelative DNs

• Relative Distinguished Names are the individual components of a Distinguished Name (interpreted as relative to some position in the hierarchy).

• For example, the RDN "ou=Math" falls in the hierarchy below "o=RPI, c=US".

Page 17: Lightweight Directory Access Protocol (LDAP)

17http://www.man.poznan.pl/~wierzej/

DN usageDN usageDN usageDN usage

• A distinguished name is a key used to access a record.

• Each record can contain multiple attribute/value pairs. Examples of attributes:

phone number email addresstitle home pagepublic key project 3 grade

Page 18: Lightweight Directory Access Protocol (LDAP)

18http://www.man.poznan.pl/~wierzej/

ObjectClassObjectClassObjectClassObjectClass

• A commonly used attribute is "objectClass".

• Each record represents an object, and the attributes associated with that object are defined according to it's objectClass

The value of the objectClass attribute.

Page 19: Lightweight Directory Access Protocol (LDAP)

19http://www.man.poznan.pl/~wierzej/

Object Type examplesObject Type examplesObject Type examplesObject Type examples

• Examples of objectClass:organization (needs a name and address)person (needs name, email, phone & address)course (needs a CRN, instructor, mascot)cookie (needs name, cost & taste index)

Page 20: Lightweight Directory Access Protocol (LDAP)

20http://www.man.poznan.pl/~wierzej/

Defining ObjectClass typesDefining ObjectClass typesDefining ObjectClass typesDefining ObjectClass types

• You can define what attributes are required for objects with a specific value for the objectclass attribute.

• You can also define what attributes are allowed.

• New records must adhere to these settings!

Page 21: Lightweight Directory Access Protocol (LDAP)

21http://www.man.poznan.pl/~wierzej/

Multiple ValuesMultiple ValuesMultiple ValuesMultiple Values

• Each attribute can have multiple values, for example we could have the following record:

DN: cn=Dave Hollinger, O=RPI, C=USCN: Dave HollingerCN: David HollingerEmail: [email protected]: [email protected]: [email protected]

Page 22: Lightweight Directory Access Protocol (LDAP)

22http://www.man.poznan.pl/~wierzej/

LDAP ServicesLDAP ServicesLDAP ServicesLDAP Services

• Add, Delete, Change entry

• Change entry name (dn).

• Searching (the primary operation)Search some portion of the directory for entries that match some criteria.

Page 23: Lightweight Directory Access Protocol (LDAP)

23http://www.man.poznan.pl/~wierzej/

AuthenticationAuthenticationAuthenticationAuthentication

• LDAP authentication can be based on simple passwords (cleartext) or Kerberos.

• LDAP V3 includes support for other authentication techniques including reliance on public keys.

Page 24: Lightweight Directory Access Protocol (LDAP)

24http://www.man.poznan.pl/~wierzej/

LDAP RequestsLDAP RequestsLDAP RequestsLDAP Requests

• bind/unbind (authentication)

• search

• modify

• add

• delete

• compare

Page 25: Lightweight Directory Access Protocol (LDAP)

25http://www.man.poznan.pl/~wierzej/

LDAP Protocol DefinitionLDAP Protocol DefinitionLDAP Protocol DefinitionLDAP Protocol Definition

• The protocol is defined in RFC 1777 using ASN.1 (abstract syntax notation) and encoding is based on BER (Basic Encoding Rules) - all very formal.

• All requests/responses are packaged in an "envelope" (headers) and include a messageID field.

Page 26: Lightweight Directory Access Protocol (LDAP)

26http://www.man.poznan.pl/~wierzej/

Example - LDAP bind requestExample - LDAP bind requestExample - LDAP bind requestExample - LDAP bind request

Bind request must be the first request.

BindRequest =

[Application 0] SEQUENCE {

version INTEGER (1…127),

name LDAPDN,

authentication CHOICE {

simple [0] OCTET STRING,

krbv42LDAP [1] OCTET STRING,

krbv42DSA [2] OCTET STRING

}

}

Page 27: Lightweight Directory Access Protocol (LDAP)

27http://www.man.poznan.pl/~wierzej/

Other RequestsOther RequestsOther RequestsOther Requests

• Search/modify/delete/change requests can include maximum time limits (and size limits in the case of search).

• There can be multiple pending requests (each with unique messageID).

Asynchronous replies (each includes messageID of request).

Page 28: Lightweight Directory Access Protocol (LDAP)

28http://www.man.poznan.pl/~wierzej/

Search Request ParametersSearch Request ParametersSearch Request ParametersSearch Request Parameters

base scope

size time

attributes attrsonly

search_filter

Page 29: Lightweight Directory Access Protocol (LDAP)

29http://www.man.poznan.pl/~wierzej/

Search Parameter: BaseSearch Parameter: BaseSearch Parameter: BaseSearch Parameter: Base

• The base is the DN of root of the search.

• A server typically serves only below some

subtree of the global DN namespace.

You can ask the server to restrict the

search to a subtree of what it serves.

Page 30: Lightweight Directory Access Protocol (LDAP)

30http://www.man.poznan.pl/~wierzej/

Search Parameter: ScopeSearch Parameter: ScopeSearch Parameter: ScopeSearch Parameter: Scope

• base – search only the base element.

• onelevel – search all elements that are children of the base.

• subtree – search everything in the subtree base

Page 31: Lightweight Directory Access Protocol (LDAP)

31http://www.man.poznan.pl/~wierzej/

Search Parameter: TimeSearch Parameter: TimeSearch Parameter: TimeSearch Parameter: Time

Limit on number of seconds the

search can take.

Value of 0 means “no limit”.

Page 32: Lightweight Directory Access Protocol (LDAP)

32http://www.man.poznan.pl/~wierzej/

Search Parameter: SizeSearch Parameter: SizeSearch Parameter: SizeSearch Parameter: Size

Limit on the number of entries to return from the search.

A value of 0 means no limit.

Page 33: Lightweight Directory Access Protocol (LDAP)

33http://www.man.poznan.pl/~wierzej/

Search Parameter: AttributesSearch Parameter: AttributesSearch Parameter: AttributesSearch Parameter: Attributes

A list of attributes that should be returned for each matched entry.

NULL mean “all attributes”

Attribute names are strings.

Page 34: Lightweight Directory Access Protocol (LDAP)

34http://www.man.poznan.pl/~wierzej/

Search Parameter: AttrsonlySearch Parameter: AttrsonlySearch Parameter: AttrsonlySearch Parameter: Attrsonly

a flag that indicates whether values should be returned

TRUE: return both attributes and values.FALSE: return just list of attributes.

Page 35: Lightweight Directory Access Protocol (LDAP)

35http://www.man.poznan.pl/~wierzej/

Search Parameter: FilterSearch Parameter: FilterSearch Parameter: FilterSearch Parameter: Filter

a search filter that defines the conditions that constitute a match.

Filters are text strings.

There is an entire RFC that describes the syntax of LDAP filters. (RFC 1558)

Page 36: Lightweight Directory Access Protocol (LDAP)

36http://www.man.poznan.pl/~wierzej/

Search FiltersSearch FiltersSearch FiltersSearch Filters

• Restrict the search to those records that have specific attributes, or those whose attributes have restricted values.

"objectclass=*" match all records

"cn=*dave*" matches any record with "dave" in the value of cn

Page 37: Lightweight Directory Access Protocol (LDAP)

37http://www.man.poznan.pl/~wierzej/

Complex FiltersComplex FiltersComplex FiltersComplex Filters

• You can combine simple filters with boolean &, | and !:

(&(cn=*da)(email=*hotmail*))

(&(!(age>=18))(drinks=yes))

(|(grade>=90)(cookies>10))

Page 38: Lightweight Directory Access Protocol (LDAP)

38http://www.man.poznan.pl/~wierzej/

Search ReplySearch ReplySearch ReplySearch Reply

• Each search can generate a sequence of Search Response records:

Distinguished Name for recordlist of attributes, possibly with list of values for each attribute.Result code

• LDAP includes an extensive error/status reporting facility.

Page 39: Lightweight Directory Access Protocol (LDAP)

39http://www.man.poznan.pl/~wierzej/

Other Requests/ResponsesOther Requests/ResponsesOther Requests/ResponsesOther Requests/Responses

• The other requests and responses are detailed in RFC1777.

• However, to write a client we don't need to know the details of the protocol, there is an API (RFC 1823) and library available!

yippie!

Page 40: Lightweight Directory Access Protocol (LDAP)

40http://www.man.poznan.pl/~wierzej/

LDAP APILDAP APILDAP APILDAP API

• There are actually a couple of well-established APIs:

the original (RFC 1823) from U. of Michigan.Netscape has one.

• In both cases we are spared the details of the protocol, we just call some subroutines.

• The socket stuff is handled for us.

Page 41: Lightweight Directory Access Protocol (LDAP)

41http://www.man.poznan.pl/~wierzej/

Writing a clientWriting a clientWriting a clientWriting a client

1. Open connection with a server

2. Authenticate (or authentificate if you must).

3. Do some searches/modification/deletions.

4. Close the connection

Page 42: Lightweight Directory Access Protocol (LDAP)

42http://www.man.poznan.pl/~wierzej/

Opening a connectionOpening a connectionOpening a connectionOpening a connection

int ldap_bind(

LDAP *ld, connection handle

char *dn, who you are (your dn)

char *cred, your credentials

int method) which kind of authenticaton

return value is LDAP_SUCCESS on success or else ldap_errno is set to indicate the error.

Page 43: Lightweight Directory Access Protocol (LDAP)

43http://www.man.poznan.pl/~wierzej/

Simple bindSimple bindSimple bindSimple bindThere are actually a bunch of ldap_bind functions, this is the

simplest:

int ldap_simple_bind(

LDAP *ld, connection handle

char *dn, who you are (your dis. name)

char *passwd) your ldap password

The sample LDAP server on monte.cs.rpi.edu is set up so you don't need a password (or dn) to do anything. :

ldap_simple_bind(l,NULL,NULL);

Page 44: Lightweight Directory Access Protocol (LDAP)

44http://www.man.poznan.pl/~wierzej/

Synchronous vs. Synchronous vs. AsynchronousAsynchronous

Synchronous vs. Synchronous vs. AsynchronousAsynchronous

• Synchronous calls all end in "_s"

ldap_simple_bind_s(l,NULL,NULL);

• Easier to use (returns the result right away).

Page 45: Lightweight Directory Access Protocol (LDAP)

45http://www.man.poznan.pl/~wierzej/

Simple Search QuerySimple Search QuerySimple Search QuerySimple Search Query

int ldap_search_s(

LDAP *ld, connection handle

char *base, dn of the base of the search

int scope, scope of the search

char *filter, search filter

char *attrs[], list of attributes to return

int attrsonly, flag - return no values?

LDAPMessage **res) result of query

Page 46: Lightweight Directory Access Protocol (LDAP)

46http://www.man.poznan.pl/~wierzej/

Search ScopeSearch ScopeSearch ScopeSearch Scope• LDAP_SCOPE_BASE: search only the base for

a match.

• LDAP_SCOPE_ONELEVEL: search only one level below the base.

• LDAP_SCOPE_SUBTREE: search the entire subtree below the base.

Page 47: Lightweight Directory Access Protocol (LDAP)

47http://www.man.poznan.pl/~wierzej/

Search FiltersSearch FiltersSearch FiltersSearch Filters

• LDAP search filters are described in RFC 1960.

attribute=value pairs with support for boolean connectives and relational operators

• Examples:"(objectclass=*)"

"(&(objectclass=Cookie)(tasteindex>=30))"

Page 48: Lightweight Directory Access Protocol (LDAP)

48http://www.man.poznan.pl/~wierzej/

Example SearchExample SearchExample SearchExample Search

ldap_search_s(l,

"course=Netprog, school=RPI",

LDAP_SCOPE_SUBTREE,

"(cn=Joe Student)", NULL,0,&mesg);

On success, mesg is a pointer to the result. To access the records in the result you have to use more of the LDAP library.

Page 49: Lightweight Directory Access Protocol (LDAP)

49http://www.man.poznan.pl/~wierzej/

Search ResultsSearch ResultsSearch ResultsSearch Results

The result is a list of records - you do something like this to scan the list:

LDAPMessage *p; char *dn;

for (p=ldap_first_entry(l,msg);

p != NULL;

p=ldap_next_entry(l,p)) {

dn = ldap_get_dn(l,p);

printf("dn: %d\n",dn);

}

Page 50: Lightweight Directory Access Protocol (LDAP)

50http://www.man.poznan.pl/~wierzej/

Attributes of each entryAttributes of each entryAttributes of each entryAttributes of each entry

• Extracting the attributes (and values) from each entry is similar - step through a list of attributes using:

ldap_first_attribute()ldap_next_attribute()

• Example code in RFC 1823!!!

Page 51: Lightweight Directory Access Protocol (LDAP)

51http://www.man.poznan.pl/~wierzej/

PerLDAP: Searching for an PerLDAP: Searching for an entryentry

#!/usr/bin/perl5use Mozilla::LDAP::Conn;#setup global parameters, using Getopt# …

$c = new Mozilla::LDAP::Conn(\%ld);$entry = $c->search($base, $sc, $srch);while ($entry) { $entry->printLDIF(); $entry = $c->nextEntry();}$c->close();

Page 52: Lightweight Directory Access Protocol (LDAP)

52http://www.man.poznan.pl/~wierzej/

PerLDAP: Updating an entryPerLDAP: Updating an entry

#!/usr/bin/perl5use Mozilla::LDAP::Conn;#setup global parameters, using Getopt# …

$c = new Mozilla::LDAP::Conn(\%ld);$entry = $c->search($base, $sc, $srch);while ($entry) { $entry->setValue(“mailhost”)=[$host]; $c->update($entry); $entry = $c->nextEntry();}$c->close();