Validar y autenticar Red Hat 6 (RHEL 6) con Active Directory 2003

Últimamente me he encontrado en una situación curiosa al tener que validar contra un AD 2003 y posterior mente soportar la migración al AD 2008 R2. Después de un poco de investigación y tiempo he encontrado un par de opciones viables.

Requisitos previos:

1. NTP. Nuestro servidor de NTP debe ser el mismo que el de los Domain Controllers de Windows a los que nos vamos a conectar.

2. FQDN. El comando "hostname -f" debe devolvernos el mismo dominio que el controlado por los Domain Controllers, en otras palabras deben usar sus mismos DNS, si son internos al dominio, algo bastante común, pues debe darse de alta en el.

3. GROUPS. No podemos tener el mismo nombre en los grupos de Windows y los grupos Unix. Bueno si podemos pero tenemos que tener claro que si coinciden en nombre prevalecerán los permisos de Unix. Quiere decir que si el usuario no existe en el grupo de Unix que se llama igual que el de Windows, dará igual que este en el Windows o no.

4. MAYÚSCULAS. El script que presento tiene mayúsculas y minúsculas, poned vuestros datos con el mismo orden. Kerberos es "case sesitive" y no funcionara si no lo respetáis.

5. SYSADMINX. Con este script solo permitimos a los que pertenecen al grupo "sysadminx" entrar al sistema. Esta como requisito en la configuración de pam. Esto puede ser cambiado por cualquier grupo de nuestra elección o simplemente eliminar la línea para permitir a cualquiera acceder. 

Script:

 

#!/bin/bash
#————————————————————+
# Purpose  :Script for connect to Active Directory 2003
# Shell    :Bash
# OS       :RHEL 6
# Version  :1.0
#       Revision :0
# Author   :JJ Vidanez
#————————————————————+

WORKGR=EXAMPLE
DOMAIN=EXAMPLE.LOCAL
KRBREALM=EXAMPLE.LOCAL
DC1=dc1.example.local
DC2=dc2.example.local

[[ -n "$1" ]] || { echo "Parameter AD admin account needed"; exit 0 ; }

echo "[*] Installing required authentication packages"
for package in samba-common pam_krb5 sudo authconfig samba-winbind samba-winbind-clients krb5-libs krb5-workstation
do
  rpm -q ${package} >/dev/null 2>&1
  if [[ $? -ne 0 ]]
  then
    echo "… installing ${package}"
    yum -y install ${package} >/dev/null 2>&1
    if [[ $? -ne 0 ]]
    then
      echo "${package} installation failed"
      exit 1
    fi
  fi
done


echo "[*] Configuring Kerberos, samba and winbind"

authconfig \
–disablecache \
–disablesssd \
–disablesssdauth \
–disableldapauth \
–disableldap \
–disablefingerprintauth \
–disablefingerprint \
–enablewinbind \
–enablewinbindauth \
–smbsecurity=ads \
–smbworkgroup=${WORKGR} \
–smbrealm=${DOMAIN} \
–enablewinbindusedefaultdomain \
–winbindtemplatehomedir=/export/home/%U \
–winbindtemplateshell=/bin/bash \
–enablekrb5 –krb5realm=${KRBREALM} \
–enablekrb5kdcdns \
–enablekrb5realmdns \
–enablelocauthorize \
–enablemkhomedir \
–enablepamaccess \
–updateall

echo "[*] Saving originals krb5.conf and smb.conf"
mv /etc/samba/smb.conf /etc/samba/smb.conf.orig
mv /etc/krb5.conf /etc/krb5.conf.orig

echo "[*] Introducing new /etc/krb5.conf"
cat <<EOF> /etc/krb5.conf
[logging]
default = FILE:/var/log/krb5libs.log
kdc = FILE:/var/log/krb5kdc.log
admin_server = FILE:/var/log/kadmind.log

[libdefaults]
default_realm = EXAMPLE.LOCAL
dns_lookup_realm = true
dns_lookup_kdc = false
ticket_lifetime = 24h
renew_lifetime = 7d
forwardable = true

[realms]

EXAMPLE.LOCAL = {
  kdc = dc1.example.local
  kdc = dc2.example.local
  admin_server = dc1.example.local
  admin_server = dc2.example.local
}

[domain_realm]
example.local = EXAMPLE.LOCAL
.example.local = EXAMPLE.LOCAL

[kdc]
profile = /var/kerberos/krb5kdc/kdc.conf

[appdefaults]
pam = {
     debug = false
     ticket_lifetime = 36000
     renew_lifetime = 36000
     forwardable = true
     krb4_convert = false
}
EOF

echo "[*] Introducing new /etc/samba/smb.conf"
cat <<EOF> /etc/samba/smb.conf
[global]
workgroup = EXAMPLE
realm = EXAMPLE.LOCAL
security = ads
idmap uid = 16777216-33554431
idmap gid = 16777216-33554431
winbind separator = +
template homedir = /export/home/%U
template shell = /bin/bash
winbind use default domain = true
winbind offline logon = false
server signing = auto
server string = Samba Server
winbind enum users = yes
winbind enum groups = yes
idmap config EXAMPLE:backend = rid
idmap config EXAMPLE:base_rid = 500
idmap config EXAMPLE:range = 90000-1000000

[homes]
comment = AD User Home Directories
browseable = no
writable = yes
valid users = %D+%S
EOF


echo "[*] Modifying SSHD to support PAM authentication"
grep -e '^UsePAM.*' /etc/ssh/sshd_config >/dev/null 2>&1
if [[ $? -ne 0 ]]
then
cat << EOF >> /etc/ssh/sshd_config
UsePAM yes
EOF
else
perl -pi -e 's|^UsePAM.*|UsePAM yes|' /etc/ssh/sshd_config
fi

echo "[*] Modifying pam for mkhomedir arguments and winbind"
mv /etc/pam.d/password-auth /etc/pam.d/password-auth.orig
mv /etc/pam.d/system-auth /etc/pam.d/system-auth.orig
cat <<EOF> /etc/pam.d/password-auth
#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth        required      pam_env.so
auth        sufficient    pam_unix.so nullok try_first_pass
auth        requisite     pam_succeed_if.so user ingroup sysadminx debug
auth        requisite     pam_succeed_if.so uid >= 500 quiet
auth        sufficient    pam_krb5.so use_first_pass
auth        sufficient    pam_winbind.so use_first_pass
auth        required      pam_deny.so

account     required      pam_access.so
account     required      pam_unix.so broken_shadow
account     sufficient    pam_localuser.so
account     sufficient    pam_succeed_if.so uid < 500 quiet
account     [default=bad success=ok user_unknown=ignore] pam_krb5.so
account     [default=bad success=ok user_unknown=ignore] pam_winbind.so
account     required      pam_permit.so

password    requisite     pam_cracklib.so try_first_pass retry=3 type=
password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok
password    sufficient    pam_krb5.so use_authtok
password    sufficient    pam_winbind.so use_authtok
password    required      pam_deny.so

session     optional      pam_keyinit.so revoke
session     required      pam_limits.so
session     optional      pam_mkhomedir.so umask=0077
session     [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
session     required      pam_unix.so
session     optional      pam_krb5.so
EOF

cp -p /etc/pam.d/password-auth /etc/pam.d/system-auth

echo "[] Join to the domain"
net ads join -U $1

echo "[*] Restarting services"
service winbind restart



##############################################################################
### Vidanez.com is not responsible for the
### contents, use or the code enclosed.
###
### Copyright 2012 vidanez.com, Inc. ALL RIGHTS RESERVED
### Use of this software is under your own responsability
### http://www.vidanez.com/responsabilidad-del-contenido/
##############################################################################

 

LINK to the script AD2003

Por ultimo en caso de que no os funcione os recomiendo que le echéis un vistazo a los logs de kerberos y de "/var/logs/secure" que os dirá exactamente donde esta el fallo.


Leave a Reply

Your email address will not be published. Required fields are marked *