Labs / Identity & Access / Central logins

Lab 3 of 3 · Central logins

LDAP: one account for every server

Keep every account in one directory, and use one LDAP login on every server.

Start this lab 5 steps

The idea

Pinecone Games, a small game studio, keeps every account in one LDAP directory. Look people up in it, log in to it, point a server at it, and on someone's last day, close every door with one change.

A DN is a path up the tree
A DN is a path up the tree
Same bind, two ways to travel
Same bind, two ways to travel
Search for the name, then bind as it
Search for the name, then bind as it
Two checks, one after the other
Two checks, one after the other
One change, every server
One change, every server

Steps

  1. Look someone up

    Pinecone's directory runs on directory, with OpenLDAP's server, slapd. A directory is a database of people and groups, built to be read far more often than it's written.

    Everything in it is an entry, and each entry has a DN, a distinguished name, that spells out where it sits in the tree. Read uid=sanne,ou=people,dc=pinecone,dc=lab from the right: the studio (dc=pinecone,dc=lab, its domain name a domain component at a time), its people (ou=people, an organizational unit), and Sanne (uid=sanne, her user ID). Each piece is one step down the tree.

    An entry holds attributes: cn (common name) for the full name, title, mail. Its objectClass lines say which attributes it may have: inetOrgPerson for someone at a company, posixAccount for someone who can log in to a Linux machine.

    To read the directory you search: from a base DN, for the entries that match a filter in brackets, like (uid=sanne). Sanne, the studio's build engineer, leaves on Friday, so start with her, and then with the groups she's in. They live under ou=groups.

    laptop:~# ldapsearch -x -H ldap://directory.lab -b dc=pinecone,dc=lab "(uid=sanne)"
    laptop:~# ldapsearch -x -LLL -H ldap://directory.lab -b dc=pinecone,dc=lab "(&(objectClass=posixGroup)(memberUid=sanne))" cn
  2. Log in to the directory

    Logging in to a directory is called a bind. A simple bind sends a DN and a password, and the directory checks the password against that entry's userPassword, which holds only a salted hash of it. The search that follows runs as that person, with what they may read.

    -D names the DN to bind as, and -w the password. Noor, an engine programmer, lends you hers: green-kiln-4.

    The address says how it all travels. ldap:// is plain LDAP, on TCP port 389. ldaps:// is port 636, and wraps the connection in TLS before LDAP says a word, checking the server's certificate on the way in.

    You'll search twice, once each way, while directory records the traffic.

    laptop:~# ldapsearch -x -LLL -H ldap://directory.lab -D uid=noor,ou=people,dc=pinecone,dc=lab -w green-kiln-4 -b dc=pinecone,dc=lab "(uid=sanne)" mail
    laptop:~# ldapsearch -x -LLL -H ldaps://directory.lab -D uid=noor,ou=people,dc=pinecone,dc=lab -w green-kiln-4 -b dc=pinecone,dc=lab "(uid=sanne)" mail
  3. Let the build server check passwords with the directory

    buildServer has a line in /etc/passwd for each person, but no passwords. An SSH login there goes through PAM, the Linux library that runs each login past a stack of modules, in two phases: auth, is the password right, and account, may this person log in now.

    Here both phases go to pam_ldap, which hands the question to nslcd, a small program that asks the directory: ssh, then PAM, then pam_ldap, then nslcd, then directory.

    nslcd's file, /etc/nslcd.conf, still has the package's settings: a directory on the server itself, for a company called example. It needs two lines changed and one added:

    • uri: where the directory is, ldaps://directory.lab, so passwords travel inside TLS.
    • base: where in the tree to look for accounts, dc=pinecone,dc=lab.
    • tls_cacertfile: who vouches for the directory's certificate. That's the studio's certificate authority, in /usr/local/share/ca-certificates/lab-ca.crt.

    wikiServer was set up this way last year, and its logins already work.

    laptop:~# sshpass -p green-kiln-4 ssh noor@buildServer.lab hostname
    directory:~# grep -A1 'BIND dn="uid=noor' /var/log/slapd.log
  4. Only engineers on the build server

    Any account in the directory gets onto buildServer now, Bea's included. She's a character artist, and the build server holds the keys the studio signs its games with.

    Checking Bea's password is authentication: is this Bea? Deciding whether Bea may use this server is authorization, a separate question with its own answer. That's two checks, not one.

    nslcd asks the second one with pam_authz_search: a search that must find at least one entry for the login to go on. In its filter, $username stands for the name being logged in. Pinecone's filter finds the engineers group, if it lists that person:

    (&(objectClass=posixGroup)(cn=engineers)(memberUid=$username))

    laptop:~# sshpass -p ochre-fox-17 ssh bea@buildServer.lab hostname
    laptop:~# sshpass -p ochre-fox-17 ssh bea@buildServer.lab hostname
    buildServer:~# tail -n 3 /var/log/auth.log
    laptop:~# sshpass -p green-kiln-4 ssh noor@buildServer.lab hostname
  5. Sanne's last day

    It's Friday, Sanne's last day. With an account on each server, someone would have to remember every one. Here it's one change, in one place.

    Changes to a directory are written in LDIF too, as one file of changes: a block for each entry, its dn: line first, then a changetype line that says what to do, and a blank line between blocks. Sanne needs two:

    • Delete her entry, uid=sanne,ou=people,dc=pinecone,dc=lab, with changetype: delete.
    • Take her out of the engineers group: changetype: modify, then delete: memberUid and memberUid: sanne. With that value line, only her name goes. Without it, every memberUid would go, and the group with them.

    Changes are made bound as the directory's administrator, cn=admin,dc=pinecone,dc=lab, whose password is tidy-desk-31. Write them in /root/sanne-leaves.ldif on laptop, then send them with ldapmodify.

    laptop:~# ldapmodify -x -H ldaps://directory.lab -D cn=admin,dc=pinecone,dc=lab -w tidy-desk-31 -f /root/sanne-leaves.ldif
    laptop:~# sshpass -p late-train-6 ssh sanne@buildServer.lab hostname
    laptop:~# sshpass -p late-train-6 ssh sanne@wikiServer.lab hostname

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab