Thursday, 14 December 2017

LDAP

LDAP

Installation

apt-get install slapd
apt-get install ldap-utils
apt-get install ldapvi

Status/start/stop LDAP service

systemctl status slapd
systemctl start slapd
systemctl stop slapd

Configuration

After the packages are installed und slapd is started it is possible to make a first connection test:

ldapsearch -H ldap://127.0.0.1 -x -LLL -b "" -s base +

This command outputs the RootDSE (directory system [agent] entry). By the way scope base is required here otherwise ldapsearch will not return data. The "+" sign means that operational attributes are displayed that normally are hidden.

If this works one can test ldapvi. Ldapvi is a vi based tool to watch, modify and delete LDAP data.

ldapvi -h ldap://127.0.0.1 --bind simple -b "" -s base '(objectclass=*)' +
ldapvi -h ldap://127.0.0.1 --bind simple --discover


The local host address can be omitted:

ldapvi -h ldap:// --bind simple -b "" -s base '(objectclass=*)' + 

Here are explicit commands to get naming contexts and configuration context:

ldapsearch -H ldap:// -x -s base -b "" -LLL "namingContexts"
ldapsearch -H ldap:// -x -s base -b "" -LLL "configContext"


Here are two commands to access the config DN:

ldapsearch -H ldapi:// -Y EXTERNAL -b "cn=config" -LLL -Q
ldapvi -h ldapi:// -Y EXTERNAL -b "cn=config" 


Here is a different URL used. "ldapi" indicates that a unix socket should be used to access the ldapserver with unix sockets the LDAP server is able to identify the user (root of course). The EXTERNAL mechanism utilizes authentication services provided by lower level network services (unix domain sockets).

Command to explore the default domain context:

ldapvi -h ldapi:// -Y EXTERNAL -b "dc=nodomain"

Root Password for the config tree

Create a password hash with slappasswd. Enter the new passwort in the config tree with:

ldapvi -h ldapi:// -Y EXTERNAL -b "cn=config"

Search for "cn=admin,cn=config".  Add or update attribute olcRootPW with the result of slappasswd.

After that it is possible to access the ldap config tree with simple bind (password authentication):

ldapvi -h ldap:// -D "cn=admin,cn=config" -b "cn=config"

Add Schema

Enter the following commands:

ldapadd -x -H ldap:// -D "cn=admin,cn=config" -W -f /etc/ldap/schema/core.ldif 
ldapadd -x -H ldap:// -D "cn=admin,cn=config" -W -f /etc/ldap/schema/cosine.ldif
ldapadd -x -H ldap:// -D "cn=admin,cn=config" -W -f /etc/ldap/schema/inetorgperson.ldif
ldapadd -x -H ldap:// -D "cn=admin,cn=config" -W -f /etc/ldap/schema/inetorgperson.ldif

ldapadd -x -H ldap:// -D "cn=admin,cn=config" -W -f /etc/ldap/schema/nis.ldif

It seems this schemata are already known by Debians LDAP server.

Object classes

To get a list of all defined object classes one can enter this command: 
ldapsearch -H ldap:// -D "cn=admin,cn=config" -W -s base -b "cn=subschema" objectclasses

Rename domain

Openldap was intialized with a domain name (cn=nodomain) which ist not favorable. It can be displayed with this command:

ldapsearch -H ldap:// -D "cn=admin,dc=nodomain" -W -b dc=nodomain -s sub
I was able to delete the manager object within the "nodmain" sub tree but I was not able to delete the domain node itself. I tried it with this commands:

ldapdelete -H ldap:// -D "cn=admin,dc=nodomain" -W dc=nodomain

Or this one. Ldapmodify with an LDIF file:

version: 1
# to apply these changes using ldapvi, run:
#   ldapvi --ldapmodify -h ldap:// ,ldapvi-lamobo-11313.ldif

dn: dc=nodomain
changetype: delete

ldapmodify -H ldap:// -D "cn=admin,dc=nodomain" -W -f ,ldapvi-lamobo-11313.ldif
Here is an explanation why I was not successful.

Next I renamed the suffix as explained here:
olcRootDN: cn=admin,dc=bd,dc=local
olcSuffix: dc=bd,dc=local

I added the top level nodes for my new domain with the following LDIF file:

dn: dc=bd,dc=local
changetype: add
objectClass: top
objectClass: dcObject
objectClass: organization
o: bd.local
dc: bd

dn: cn=admin,dc=bd,dc=local
changetype: add
objectClass: simpleSecurityObject
objectClass: organizationalRole
cn: admin
description: LDAP administrator


ldapmodify -x -W -D cn=admin,dc=bd,dc=local -H ldapi:/// -f slapd_setup_basic.ldif
 
After that it is able to access the new DIT with:

ldapvi -h ldap:// -D "cn=admin,dc=bd,dc=local" -b "dc=bd,dc=local" 

Add groupOfEntries class

With this class it is possible to create empty groups. The standard object classes for groups  groupOfNames and groupOfUniqueNames can't do that.

dn: cn=groupofentries,cn=schema,cn=config
objectClass: olcSchemaConfig
cn:groupofentries 
olcobjectclasses:( 1.2.826.0.1.3458854.2.1.1.1 NAME 'groupOfEntries' SUP top STRUCTURAL MUST ( cn ) MAY ( member $ businessCategory $ seeAlso $ owner $ ou $ o $ description ))


ldapadd -x -H ldap:// -D "cn=admin,cn=config" -W -f /tmp/groupOfEntries.ldif

Overlay memberof

The memberof overlay adds automatically the memberOf attribut to users that are members of a groupOfEntries.

dn: cn=module,cn=config
cn: module
objectClass: olcModuleList
olcModuleLoad: memberof
olcModulePath: /usr/lib/ldap

dn: olcOverlay=memberof,olcDatabase={1}mdb,cn=config
changetype: add
objectClass: olcOverlayConfig
objectClass: olcMemberOf
olcOverlay: memberof
olcMemberOfGroupOC: groupOfEntries
olcMemberOfMemberAD: member
olcMemberOfMemberOfAD: memberOf

ldapadd -x -H ldap:// -D "cn=admin,cn=config" -W -f /tmp/memberOf.ldif

Groups and Users

Now we can add some groups and users.

dn: ou=groups,dc=bd,dc=local
changetype: add
objectClass: top
objectClass: organizationalUnit
ou: groups

dn: cn=admins,ou=groups,dc=bd,dc=local
changetype: add
objectClass: top
objectClass: groupOfEntries

dn: cn=users,ou=groups,dc=bd,dc=local
changetype: add
objectClass: top
objectClass: groupOfEntries

dn: ou=people,dc=bd,dc=local
changetype: add
objectClass: top
objectClass: organizationalUnit
ou: people

dn: cn=bdonauba,ou=people,dc=bd,dc=local
changetype: add
objectClass: top
objectClass: person
sn: MyName

ldapadd -x -H ldap:// -D "cn=admin,cn=config" -W -f /tmp/groupUsers.ldif

Attention! The memberOf attribute is operational so one has to use the plus sign with ldapsearch or ldapvi to see it.

Unix Attributes

Of course we want to use openldap as directory for users und groups. To do so we have to enhance the groups with the posixGroup class. For the posixGroup class the gidNumber attribute is mandatory.

cn=admins,ou=groups,dc=bd,dc=local
cn: admins
gidNumber: 10001
objectClass: groupOfEntries
objectClass: posixGroup
objectClass: top


At the moment this is not possible because both groupOfEntries and possixGroup are of type structural, so we change the type of possixGroup to auxiliary.

olcObjectClasses: {2}( 1.3.6.1.1.1.2.2 NAME 'posixGroup' DESC 'Abstraction of a group of accounts' SUP top AUXILIARY MUST ( cn $ gidNumber ) MAY ( userPassword $ memberUid $ description ) )

After this change one can add the posixGroup object class.

Now we can define our first user account.

cn=bdonauba,ou=people,dc=bd,dc=local
sn: Donaubauer
cn: bdonauba
gidNumber: 10001
homeDirectory: /home/bdonauba
loginShell: /bin/bash
objectClass: person
objectClass: posixAccount
objectClass: top
uid: bdonauba
uidNumber: 10001



slapcat -F /etc/ldap/slapd.d/ -H ldap://

dn: dc=bd,dc=l


Wednesday, 10 May 2017

Postgresql

Postgresql

Here is a IMHO nice tutorial: http://www.postgresqltutorial.com

Installation

apt-cache search postgresql
apt-get install postgresql-9.4

Start/Stop

systemctl stop postgresql
systemctl start postgresql
systemctl status postgresql

Configuration

 Login as Postgresql Admin user:
 su - postgres

Move Data Directory

Show the data directory path in pgsql:
SHOW data_directory;
Shut down postgresql DBMS as root:
systemctl stop postgresql
Copy data to hd:
rsync -av /var/lib/postgresql /mnt/data/postgresql
Rename old data directory:
mv /var/lib/postgresql  /var/lib/postgresql.bak
Change  data_directory parameter in postgresql configuration file:
vi /etc/postgresql/9.4/main/postgresql.conf

Set data_directory to the new location:

 data_directory = '/mnt/data/postgresql/9.4/main'
Start postgresql DBMS as root:
systemctl start postgresq
 Change the home directory of user postgres to:
/mnt/data/postgresql

psql

psql commands

List psql commands:
\?
List databases:
\l
 List databases with tablespace: 
\l+
Connect to database:
\c dbname
List tables in the database: 
\dt
Show settings:
show data_directory;
The show command displays the content of configuration parameter. In this case the data directory is displayed.
/mnt/data/postgresql/9.4/main
List sql commands:
\h

Create test database

Start psql an enter this command:
create database mytestdb;
Connect to the new db:
\c mytestdb
Now the database mytestdb is available. In order to create a table enter a create table command:
create table mytesttable (id integer primary key, data char(32));