Friday, December 24, 2010

How to create Certificate Authority using OpenSSL

The Certificate Authority (CA) is used to verify the authenticity of a certificate. Start by installing openssl package:
apt-get install openssl

Create Private Certificate Authority

  1. OpenSSL (version 0.9.8) is installed to path /usr/lib/ssl. The CA.sh script is not in search path, we are going to add it for just current session.
    export PATH=$PATH:/usr/lib/ssl/misc
    
  2. Let customize a bit configuration file (/usr/lib/ssl/openssl.cnf) that is used for certificate creation, but first make a backup copy. Make the following changes:
    ...
    [ req ]
    default_bits    = 2048
    ...
    [ req_distinguished_name ]
    countryName_default             = UA
    stateOrProvinceName_default     = LV
    0.organizationName_default      = XYZ Co
    ...
    
  3. Create a directory for all certificates (it can be any directory, we will create in home):
    mkdir ~/ca && cd ~/ca
    
  4. Answer few questions (hit enter to create a new when prompted for CA filename):
    ldap1:~/ca# CA.sh -newca
    CA certificate filename (or enter to create)
    
    Making CA certificate ...
    Generating a 2048 bit RSA private key
    ............+++
    ........+++
    writing new private key to './demoCA/private/./cakey.pem'
    Enter PEM pass phrase: **************
    Verifying - Enter PEM pass phrase: **************
    ...
    Country Name (2 letter code) [UA]:
    State or Province Name (full name) [LV]:
    Locality Name (eg, city) []:Lviv
    Organization Name (eg, company) [XYZ Co]:
    Organizational Unit Name (eg, section) []:
    Common Name (eg, YOUR name) []:XYZ Root CA
    Email Address []:
    
    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:
    An optional company name []:
    Using configuration from /usr/lib/ssl/openssl.cnf
    Enter pass phrase for ./demoCA/private/./cakey.pem: *****
    Check that the request matches the signature
    Signature ok
    Certificate Details:
    ...
    Write out database with 1 new entries
    Data Base Updated
    
  5. Secure Certificate Authority:
    chmod -R go-rwx ~/ca
    
Your Certificate Authority file is cacert.pem (it is located in ~/ca/demoCA directory).

Thursday, December 23, 2010

How to create a new user in OpenLDAP

We are going create a new account for John Smith. Here are few simple steps:
  1. We need create a template for a new user account jsmith (file add-user.ldif):
    # User primary group
    dn: cn=jsmith,ou=groups,dc=dev,dc=local
    cn: jsmith
    objectClass: top
    objectClass: posixGroup
    gidNumber: 10000
    
    # User account
    dn: uid=jsmith,ou=people,dc=dev,dc=local
    cn: John Smith
    givenName: John
    sn: Smith
    uid: jsmith
    uidNumber: 10000
    gidNumber: 10000
    homeDirectory: /home/jsmith
    mail: jsmith@dev.local
    objectClass: top
    objectClass: posixAccount
    objectClass: shadowAccount
    objectClass: inetOrgPerson
    objectClass: organizationalPerson
    objectClass: person
    loginShell: /bin/bash
    userPassword: {CRYPT}*
    
  2. Load user to ldap:
    ldapadd -cxWD cn=admin,dc=dev,dc=local -f add-user.ldif
    
    or if you are authenticated by Kerberos:
    ldapadd -f add-user.ldif
    
  3. Try to find it:
    ldapsearch -x uid=jsmith
    
  4. Set user password (consider store user password in kerberos instead):
    ldappasswd -xWD cn=admin,dc=dev,dc=local -S uid=jsmith,ou=people,dc=dev,dc=local
    
Read more about openldap here.

Debian OpenLDAP

OpenLDAP is a free, open source implementation of the Lightweight Directory Access Protocol (LDAP).

Install OpenLDAP Server

  1. Ensure the host name is FQDN:
    ldap1:~# hostname 
    ldap1.dev.local
    
    If it is not, issue the following:
    echo "ldap1.dev.local" > /etc/hostname
    hostname -F /etc/hostname
    
  2. Install necessary packages (during a package configuration phase set admin password and accept all default options):
    apt-get -y install rsyslog slapd ldap-utils
    
  3. Setup system-wide defaults for LDAP clients (file /etc/ldap/ldap.conf):
    BASE    dc=dev,dc=local
    URI     ldap://ldap1.dev.local
    
  4. Disable ipv6 support for slapd (file /etc/default/slapd):
    # Additional options to pass to slapd
    SLAPD_OPTIONS="-4"
    
    Restart slapd:
    /etc/init.d/slapd restart
    netstat -tunlp | grep slapd
    
    Output:
    tcp        0      0 0.0.0.0:389             0.0.0.0:*               LISTEN      1557/slapd
    

Logging

  1. Create a file that enable ldap logging (file log-stats.ldif):
    # Enable LDAP logging
    dn: cn=config
    changetype: modify
    replace: olcLogLevel
    olcLogLevel: stats
    
  2. ... disable ldap logging (file log-none.ldif):
    # Disable LDAP logging
    dn: cn=config
    changetype: modify
    replace: olcLogLevel
    olcLogLevel: none
    
  3. And here is a command (changes are applied immediately, no need to restart slapd):
    ldapmodify -QY EXTERNAL -H ldapi:/// -f log-stats.ldif
    

What to index

  1. Create indexes to match the actual filter terms used in search queries. Read more here. We are going to add the following indexes: uid, cn. So here is our index file (file db-index.ldif):
    dn: olcDatabase={1}hdb,cn=config
    changetype: modify
    add: olcDbIndex
    olcDbIndex: uid eq
    -
    add: olcDbIndex
    olcDbIndex: cn eq
    -
    add: olcDbIndex
    olcDbIndex: ou eq
    -
    add: olcDbIndex
    olcDbIndex: dc eq
    -
    add: olcDbIndex
    olcDbIndex: uniqueMember eq
    -
    add: olcDbIndex
    olcDbIndex: uidNumber eq
    -
    add: olcDbIndex
    olcDbIndex: gidNumber eq
    
    Apply changes:
    ldapmodify -QY EXTERNAL -H ldapi:/// -f db-index.ldif
    

Reindex database

  1. Here is a simple script to reindex database (file /usr/local/sbin/slap-reindex). You do not need to run it often, that is depends how big is your database and how many changes occur, consider run it monthly:
    #!/bin/sh
    /etc/init.d/slapd stop > /dev/null
    su openldap -c "slapindex"
    /etc/init.d/slapd start > /dev/null
    

Simple tree structure

  1. Here is our simple structure:
    dev.local
    |--people
    `--groups
    
  2. It correspond to the following (file init-tree.ldif):
    dn: ou=people,dc=dev,dc=local
    ou: people
    objectClass: organizationalUnit
    
    dn: ou=groups,dc=dev,dc=local
    ou: groups
    objectClass: organizationalUnit
    
  3. Add it to ldap:
    ldapadd -cxWD cn=admin,dc=dev,dc=local -f init-tree.ldif
    
  4. Test if we can find it:
    ldapsearch -x ou=people
    
    Here is search result:
    # extended LDIF
    #
    # LDAPv3
    # base  (default) with scope subtree
    # filter: ou=people
    # requesting: ALL
    #
    
    # people, dev.local
    dn: ou=people,dc=dev,dc=local
    ou: people
    objectClass: organizationalUnit
    
    # search result
    search: 2
    result: 0 Success
    
    # numResponses: 2
    # numEntries: 1
    

Wednesday, December 22, 2010

Debian Kerberos Slave

Slave KDCs provide an additional source of Kerberos ticket-granting services in the event of inaccessibility of the master KDC. It recommended that your KDCs have a predefined set of CNAME records (DNS hostname aliases), such as krb for the master KDC and kdc1, kdc2, ... for the slave KDCs. This way, if you need to swap a machine, you only need to change a DNS entry, rather than having to change hostnames.

Master (Primary) Kerberos Server

  1. Add a new slave (kdc2.dev.local) to file /etc/krb5.conf (for the master and any other slaves):
    [realms]
            DEV.LOCAL = {
                    kdc = kdc1.dev.local
                    kdc = kdc2.dev.local
                    admin_server = krb.dev.local
            }
    
    Alternatively (preferred way) consider setup DNS discovery. Read here how.
  2. Add slave host principal:
    kadmin.local -q "addprinc -randkey host/kdc2.dev.local"
    
    kadmin.local -q "ktadd host/kdc2.dev.local"
    
  3. Create database propagation host list (file /etc/krb5kdc/kpropd.acl):
    host/kdc1.dev.local@DEV.LOCAL
    host/kdc2.dev.local@DEV.LOCAL
    
  4. Create a dump of the kerberos database (that is a default path for kprop utility):
    kdb5_util dump /var/lib/krb5kdc/slave_datatrans
    

Secondary (Slave, Read-Only) Kerberos Server

  1. Install Kerberos Server and xinetd (to be used for database propagation):
    apt-get install krb5-kdc xinetd
    
  2. Copy (a) realm configuration (file /etc/krb5.conf), (b) database propagation list (file /etc/krb5kdc/kpropd.acl), (c) keytab (file /etc/krb5.keytab), (d) logrotate settings from master, e.g. using ssh copy:
    scp kdc1:/etc/krb5.conf /etc
    scp kdc1:/etc/krb5kdc/kpropd.acl /etc/krb5kdc
    scp kdc1:/etc/krb5.keytab /etc
    scp kdc1:/etc/logrotate.d/krb5 /etc/logrotate.d
    mkdir /var/log/krb5
    
  3. Setup database propagation service (file /etc/xinetd.d/krb_prop):
    service krb_prop
    {
            disable         = no
            socket_type     = stream
            protocol        = tcp
            user            = root
            wait            = no
            server          = /usr/sbin/kpropd
    }
    
    Restart xinetd service:
    /etc/init.d/xinetd restart
    

Propagate database

  1. Propagate database from Master to Slave
    kdc1:~# kprop kdc2.dev.local
    Database propagation to kdc2.dev.local: SUCCEEDED
    
  2. Create database stash key on slave
    kdb5_util stash
    
  3. Start Kerberos Slave service:
    /etc/init.d/krb5-kdc start
    

Automate database propagation

  1. Here is a script that populates master database to all slaves (run on master, file /usr/local/sbin/krb5-prop):
    #!/bin/sh
    
    #slaves="kdc2.dev.local kdc3.dev.local"
    slaves="kdc2.dev.local"
    
    /usr/sbin/kdb5_util dump /var/lib/krb5kdc/slave_datatrans
    error=$?
    if [ $error -ne 0 ]; then
      echo "Kerberos database dump failed."
      exit 1
    fi
    
    for slave in $slaves; do
      /usr/sbin/kprop $slave > /dev/null
      error=$?
      if [ $error -ne 0 ]; then
        echo "Kerberos propagation to host $slave failed."
      fi
    done
    exit 0
    
    Ensure the file is executable:
    chmod +x /usr/local/sbin/krb5-prop
    
  2. Schedule a cron job (/usr/local/sbin/cron-krb5-prop):
    #
    # Regular cron job for Kerberos database propagation
    #
    PATH=/usr/local/sbin
    HOME=/
    LOG=/dev/null
    
    # Every 53 minutes
    53 * * * * root test -x /usr/local/sbin/krb5-prop && krb5-prop >> $LOG
    
    .. and let cron know about it:
    ln -s /usr/local/sbin/cron-krb5-prop /etc/cron.d/cron-krb5-prop
    
Finally here is how to test it is working:
  1. Stop Master Kerberos server:
    /etc/init.d/krb5-kdc stop
    
  2. Open log file on Slave:
    tail -f /var/log/krb5/kdc.log
    
  3. Login to kerberos client:
    ssh user1@deby01
    
  4. Watch the log on Slave, you should see authentication messages.
Read more about kerberos here.

Tuesday, December 21, 2010

How to setup Kerberos DNS discovery

Kerberos DNS discovery can simplify the client hosts setup. The following need to be added to zone file.
$ORIGIN dev.local.
_kerberos-adm._tcp      SRV     0 0 749 kdc1
$ORIGIN _udp.dev.local.
_kerberos               SRV     10 0 88 kdc1.dev.local.
_kerberos               SRV     20 0 88 kdc2.dev.local.
_kerberos-master        SRV     0 0 88 kdc1.dev.local.
_kpasswd                SRV     0 0 464 kdc1.dev.local.
The client configuration can now look like this (file /etc/krb5.conf):
[libdefaults]
        default_realm = DEV.LOCAL
# ...
[realms]
        DEV.LOCAL = {
        }

[domain_realm]
Let test this:
deby01:~$ host -t SRV _kerberos._udp
_kerberos._udp.dev.local has SRV record 10 0 88 kdc1.dev.local.