Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 38 ยท Directory Concepts

Distinguished Names and RDNs

Learn how LDAP names entries with distinguished names and relative distinguished names, the escaping rules of RFC 4514, and common DN mistakes.

Every entry in a directory is identified by its distinguished name (DN). A DN is the entry's full path in the tree, written as a comma-separated list of components. If you can read and write DNs correctly, half of LDAP is already easy. If you cannot, you will fight confusing errors forever.

This lesson explains the structure of a DN, the relative distinguished name (RDN), the escaping rules for special characters, how DNs are compared and what happens when an entry is renamed or moved. You will write a small parser and escaper in Python to see the rules in action.

DN structure

Here is a DN:

TEXT
uid=asmith,ou=People,dc=example,dc=com

Read it right to left to walk down from the root:

  1. dc=com
  2. dc=example under it
  3. ou=People under that
  4. uid=asmith, the entry itself

Each comma-separated component, such as uid=asmith, is an RDN (relative distinguished name). The leftmost RDN names the entry within its parent. Everything after the first comma is the parent DN. Joining an RDN to a parent DN gives the full DN.

One DN, many parts

RDN uid=asmith, the leftmost part.
Parent DN ou=People,dc=example,dc=com.
DN RDN plus parent, unique in the tree.

Read right to left from the root

1 / 2

What a DN is not

A DN is not a path you can type any way you like. It must exist exactly as stored (apart from case and spacing normalization). It is also not a stable identifier: if the entry is renamed or moved, the DN changes. Systems that need a permanent identifier use an attribute such as entryUUID in OpenLDAP or objectGUID in Active Directory, which never change.

RDNs

The RDN is one or more attribute=value assertions. Almost always it is one:

TEXT
cn=Alice Smith
uid=asmith
ou=Engineering

An RDN can be multi-valued, joined with +, which is useful when two people share a common name:

TEXT
cn=Alice Smith+uid=asmith

The attribute used in the RDN must also appear as an attribute in the entry. If an entry has RDN uid=asmith, it must have uid: asmith. Siblings must have distinct RDNs, which guarantees that each DN is unique.

Choosing an RDN attribute

Choice Pros Cons
uid=asmith Unique, stable login name Changes if the login is renamed
cn=Alice Smith Human friendly (the Active Directory default) Two Alice Smiths collide; renames when someone marries
employeeNumber=1042 Never changes Unfriendly to read
Multi-valued cn+uid Avoids collisions Awkward to type

Active Directory users are normally named by cn (the display name), for example CN=Alice Smith,OU=Engineering,DC=corp,DC=example,DC=test. Because two users in the same container cannot share a cn, you will meet names like Alice Smith 2.

Special characters and escaping

RFC 4514 defines the DN string format. A few characters have meaning in it and must be escaped with a backslash when they appear inside a value:

Character Why it is special Escaped form
, separates RDNs \,
+ joins multi-valued RDNs \+
" quoting \"
\ the escape character \\
< and > reserved \< and \>
; legacy separator \;
= optional, allowed escaped \=
# at the start marks a hex-encoded value \#
leading or trailing space would be trimmed \
NUL byte not representable \00

Any character can also be written as \ followed by two hex digits (\2c is a comma). So a user named Smith, Alice is stored as cn=Smith\, Alice,ou=People,dc=example,dc=com.

When you build a DN from user input in code, always escape the value. An unescaped comma lets an attacker inject extra RDNs, a form of LDAP injection. Use your library's helper (for example ldap3.utils.dn.escape_rdn in Python) rather than writing your own, except for learning.

Case and whitespace

Attribute types (cn, CN) are case-insensitive. Attribute values are compared with the attribute's matching rule, which for cn, uid and ou is usually case-insensitive. Spaces around commas and equals signs are ignored on input (uid=asmith, ou=People), but good style is to write DNs with no spaces. Servers normalize DNs before comparing them, so UID=ASmith,OU=people,DC=Example,DC=com and uid=asmith,ou=People,dc=example,dc=com refer to the same entry.

Parsing and escaping (Python stand-in)

The first stand-in splits a DN into RDNs, honouring backslash escapes. The second escapes a value and shows the injection problem.

Python Parse a DN

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Escape a value and see the injection risk

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the unescaped version, a second RDN appears out of nowhere. The escaped version keeps Smith, Alice as one value.

Renaming and moving entries

LDAP has a special operation, ModifyDN (also called "rename"), that changes an entry's RDN, moves it to a new parent, or both. Because DNs change, anything that stored the old DN, such as member attributes of groups, can break unless the server maintains the reference (OpenLDAP's refint overlay and Active Directory both update links in some cases). Plan for this when choosing the tree layout, as discussed in The Directory Information Tree.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the DN uid=asmith,ou=People,dc=example,dc=com, which part is the RDN?

    The RDN is the leftmost component that names the entry within its parent.
  2. 2. How is a comma inside a value written in a DN?

    RFC 4514 escapes special characters with a backslash, or as a hex pair such as \2c.
  3. 3. Why should applications not store DNs as permanent identifiers?

    Use a stable attribute like entryUUID or objectGUID for permanent references.
  4. 4. What is the risk of building a DN by concatenating unescaped user input?

    Special characters in input can change the structure of the DN, so always escape values.

Summary

  • A DN is the full path of an entry, read from the root on the right to the entry on the left.
  • The leftmost component is the RDN; the rest is the parent DN. Siblings need unique RDNs.
  • Special characters such as ,, +, \ and " must be escaped per RFC 4514.
  • Compare DNs case-insensitively after normalization, and never rely on a DN as a permanent ID.
  • ModifyDN renames or moves an entry, changing its DN and possibly breaking references.

Next: LDIF: The LDAP Data Interchange Format