sábado, 2 de abril de 2016

LINUX-CENTOS-LAB-comprendiendo ACL con ejercicios

ACL o access control list es un paquete de utilidades para administrar privilegios sobre los directorios de trabajo.

Honestamente, no pude determinar una característica diferenciadora entre usar ACL para controlar los privilegios a user/groups, versus las tecnicas anteriormente estudiadas con chown, chgrop y chmod.

Sin embargo, la implementación de ACL sugiere que el sysadmin se encuentra en un nivel avanzado, por lo que en este post vamos a estudiar sus fundamentos, configuración y algunos ejercicios para comprenderlo mejor.



Implementación y configuración,

Para implementar ACL se requiere tener instalado el pakage acl.x86_64 y libacl.x86_64. Los utilitarios a utilizar son "setfacl" y "getfacl"

[root@localhost ~]# yum list *acl
Installed Packages
acl.x86_64                                 2.2.51-12.el7                              @anaconda
libacl.x86_64                              2.2.51-12.el7                              @anaconda
[root@localhost ~]#

[root@localhost ~]# yum info acl
Installed Packages
Name        : acl
Arch        : x86_64
Version     : 2.2.51
Release     : 12.el7
Size        : 196 k
Repo        : installed
From repo   : anaconda
Summary     : Access control list utilities
URL         : http://acl.bestbits.at/
License     : GPLv2+
Description : This package contains the getfacl and setfacl utilities needed for
            : manipulating access control lists.

[root@localhost ~]# 
[root@localhost ~]# which setfacl 
/usr/bin/setfacl
[root@localhost ~]# which getfacl
/usr/bin/getfacl
[root@localhost ~]#

Para que cualquier directorio implemente ACL,  su correspondiente Filesystem debe tener indicada la opción de ACL.
Verificar si el volumen filesystem cuenta con ACL

[root@localhost ~]# tune2fs -l /dev/mapper/centos-home | grep mount
Last mounted on:          /home
Default mount options:    user_xattr acl
Last mount time:          Wed Mar 30 21:22:37 2016
Maximum mount count:      -1
[root@localhost ~]#

De esta forma, el filesystem /home tiene indicado la implementación de ACL. 
En caso de que no no lo tuviese, se debe indicar durante el arranque del sistema operativo. 

En el siguiente ejemplo, se va a indicar al filesystem /home que implemente acl

Verificar si el FS tiene implementado acl al montar en el arranque del sistema:
 [root@localhost ~]# mount | grep home
/dev/mapper/centos-home on /home type ext4 (rw,relatime,data=ordered)

Asociar acl en la partición para que lo considere en el arranque del sistema. 

[root@localhost ~]# vi /etc/fstab 
[root@localhost ~]# grep "home" /etc/fstab 
/dev/mapper/centos-home /home                   ext4    defaults,acl        1 2
[root@localhost ~]# 


Configurar ACL

Para configurar ACL se utiliza el comando setfacl

Al observar los parametros permitidos para el uso de este comando, se tiene:
[root@localhost /]# ls -ltr | grep home
drwxr-xr-x.  13 root root  4096 mar  8 17:45 home
[root@localhost /]# 

crear un directorio y verificar el estado de sus privilegios ACL.
[root@localhost ~]# mkdir -p /home/shared
[root@localhost ~]# ls -la /home/ | grep shared
drwxr-xr-x   2 root     root      4096 mar 31 14:58 shared
[root@localhost ~]# 
[root@localhost ~]# getfacl /home/shared
getfacl: Removing leading '/' from absolute path names
# file: home/shared
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
[root@localhost ~]# 

En el ejemplo anterior, no hay pivilegios a otros groups/users sobre el directorio.

Se Verifica los usuarios pertenecientes al group 1007

[root@localhost ~]# cat /etc/passwd | grep 1007
user101:x:1005:1007::/home/user101:/bin/bash
[root@localhost ~]# 

Seting de privileges ACL sólo read para el directorio. 
Donde:
-m indica que se modifica los privileges
g:1007:r indica group:id group:reading
/home/shared indica el nombre del directorio

[root@localhost ~]# setfacl -m g:1007:r /home/shared
[root@localhost ~]# getfacl /home/shared/
getfacl: Removing leading '/' from absolute path names
# file: home/shared/
# owner: root
# group: root
user::rwx
group::r-x
group:user101:r--
mask::r-x
other::r-x
[root@localhost ~]# 

Cuando un directorio posee ACL se anexa un símbolo plus(+) tal como se puede apreciar en el siguiente ejemplo:
[user101@localhost ~]$ ls -ltr /home | grep shared
drwxr-xr-x+ 2 root     root      4096 Mar 31 15:09 shared
[user101@localhost ~]$ 

El user root que sigue siendo owner del directorio, puede añadir archivos.
[root@localhost ~]# date > /home/shared/root.date.txt
[root@localhost ~]# cat /home/shared/root.date.txt 
jue mar 31 15:10:13 CLT 2016
[root@localhost ~]# 

Sin embargo, el usuario perteneciente al grupo, puede sólo leer el directorio, pero no los archivos que se encuentran dentro de él.
[root@localhost ~]# su - user101
Last login: jue mar 31 15:32:24 CLT 2016 on pts/0
[user101@localhost ~]$ ls -ltr /home/shared/
ls: cannot access /home/shared/root.date.txt: Permission denied
total 0
-????????? ? ? ? ?            ? root.date.txt
[user101@localhost ~]$ 

Para eliminar los cambios introducidos por ACL se ejecuta el comando con la opción "-b", indicando el directorio.
[root@localhost ~]# setfacl -b /home/shared
[root@localhost ~]# getfacl /home/shared
getfacl: Removing leading '/' from absolute path names
# file: home/shared
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
[root@localhost ~]# ls -ltr /home | grep shared
drwxr-xr-x  2 root     root      4096 mar 31 15:09 shared
[root@localhost ~]# 

Nuevamente el user "user101" puede revisar el estado de los archivos contenidos dentro del directorio.
[user101@localhost ~]$ ls -ltr /home/shared/
total 4
-rw-r--r-- 1 root root 29 Mar 31 15:10 root.date.txt
[user101@localhost ~]$ 

En el siguiente caso, se añade la opción read y write al directorio.
[root@localhost ~]# setfacl -m g:1007:rw /home/shared
[root@localhost ~]# getfacl /home/shared/
getfacl: Removing leading '/' from absolute path names
# file: home/shared/
# owner: root
# group: root
user::rwx
group::r-x
group:user101:rw-
mask::rwx
other::r-x
[root@localhost ~]# 

Sin embargo, para el user101 aun no es posible ver los archivos creados por otros usuarios. Tampoco crear archivos. 

[user101@localhost ~]$ ls -ltr /home/shared/
ls: cannot access /home/shared/root.date.txt: Permission denied
total 0
-????????? ? ? ? ?            ? root.date.txt
[user101@localhost ~]$ date > /home/shared/user101.login
-bash: /home/shared/user101.login: Permission denied
[user101@localhost ~]$ 

Cuando se añade los 3 privileges, read, write and execute, ACL permite realizar las operaciones antes mencionadas.
[root@localhost ~]# setfacl -m g:1007:rwx /home/shared
[root@localhost ~]# getfacl /home/shared/
getfacl: Removing leading '/' from absolute path names
# file: home/shared/
# owner: root
# group: root
user::rwx
group::r-x
group:user101:rwx
mask::rwx
other::r-x
[root@localhost ~]# 

[user101@localhost ~]$ date > /home/shared/user101.login
[user101@localhost ~]$ cat /home/shared/user101.login 
Thu Mar 31 15:48:46 CLT 2016
[user101@localhost ~]$ ls -ltr /home/shared/
total 8
-rw-r--r-- 1 root    root    29 Mar 31 15:10 root.date.txt
-rw-rw-r-- 1 user101 user101 29 Mar 31 15:48 user101.login
[user101@localhost ~]$


Sin embargo, Si se añade los params "d" (default) , ACL se aplica para todos los nuevos archivos a crear.

[root@localhost ~]# setfacl -d  -m g:1007:rwx /home/shared
[root@localhost ~]# getfacl /home/shared/
getfacl: Removing leading '/' from absolute path names
# file: home/shared/
# owner: root
# group: root
user::rwx
group::r-x
group:user101:rwx
mask::rwx
other::r-x
default:user::rwx
default:group::r-x
default:group:user101:rwx
default:mask::rwx
default:other::r-x
[root@localhost ~]#

Ejercicios de Lab

Req 1: Otorgar todos los permisos sólo al usuario user101

[root@localhost ~]# setfacl -dm u:user101:rwx /home/shared 
[root@localhost ~]# getfacl /home/shared/
getfacl: Removing leading '/' from absolute path names
# file: home/shared/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:user:user101:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[root@localhost ~]# 

El user owner puede crear y eliminar archivos

[root@localhost ~]# date > /home/shared/user101.date.5.txt
[root@localhost ~]# ls -ltr /home/shared | grep user101.date.5.txt
-rw-rw-rw-+ 1 root    root    29 mar 31 18:01 user101.date.5.txt
[root@localhost ~]# ls -ltr /home/shared | grep user101.date.5.txt
-rw-rw-rw-+ 1 root    root    29 mar 31 18:02 user101.date.5.txt
[root@localhost ~]# 


El user "user101" puede leer y modificar el archivo creado por el user root:
[user101@localhost ~]$ date > /home/shared/user101.date.5.txt
[user101@localhost ~]$ echo "write by user101" >>/home/shared/user101.date.5.txt
[user101@localhost ~]$ cat /home/shared/user101.date.5.txt
Thu Mar 31 18:02:06 CLT 2016
write by user101
[user101@localhost ~]$ 

Los usuarios de todos los grupos pueden escribir sobre el directorio

[root@localhost ~]# setfacl -d -m o::rwx /home/shared
[root@localhost ~]# getfacl 
Usage: getfacl [-aceEsRLPtpndvh] file ...
Try `getfacl --help' for more information.
[root@localhost ~]# getfacl /home/shared/
getfacl: Removing leading '/' from absolute path names
# file: home/shared/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:user:user101:rwx
default:group::r-x
default:mask::rwx
default:other::rwx
[root@localhost ~]# 
[root@localhost ~]# ls -ltr /home | grep shared
drwxr-xr-x+ 2 root     root      4096 mar 31 18:01 shared
[root@localhost ~]# 


Req 2: otorgar privileges read, write and exec, y todos los archivos y subdirectorios, para el group user101

[root@localhost ~]# setfacl -dRm g:user101:rwx /home/shared
[root@localhost ~]# getfacl /home/shared/
getfacl: Removing leading '/' from absolute path names
# file: home/shared/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:group::r-x
default:group:user101:rwx
default:mask::rwx
default:other::r-x

[root@localhost ~]# 
[root@localhost ~]# getfacl /home/shared/*
getfacl: Removing leading '/' from absolute path names
# file: home/shared/root.date.txt
# owner: root
# group: root
user::rw-
group::r--
other::r--

# file: home/shared/user101.date.3.txt
# owner: user101
# group: user101
user::rw-
group::rw-
other::r--

# file: home/shared/user101.date.5.txt
# owner: root
# group: root
user::rw-
user:user101:rwx #effective:rw-
group::r-x #effective:r--
mask::rw-
other::rw-

# file: home/shared/user101.login
# owner: user101
# group: user101
user::rw-
group::rw-
other::r--

[root@localhost ~]# 

Req 3: Otorgar acceso total a un user en específico, 

En el siguiente ejemplo, el user davis pertenece a un group distinto al group que tiene acceso al directorio. 
[root@localhost ~]# cat /etc/passwd | grep davis
davis:x:3000:10502::/home/davis:/bin/bash
[root@localhost ~]# 
[root@localhost ~]# setfacl -m u:davis:rwx /home/shared/
[root@localhost ~]# getfacl /home/shared/
getfacl: Removing leading '/' from absolute path names
# file: home/shared/
# owner: root
# group: root
user::rwx
user:davis:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:group::r-x
default:group:user101:rwx
default:mask::rwx
default:other::r-x
[root@localhost ~]# 

Req 4: Modificar el acceso al user davis para sólo read. 

[root@localhost ~]# setfacl -m u:davis:r /home/shared/
[root@localhost ~]# getfacl /home/shared/
getfacl: Removing leading '/' from absolute path names
# file: home/shared/
# owner: root
# group: root
user::rwx
user:davis:r--
group::r-x
mask::r-x
other::r--
default:user::rwx
default:group::r-x
default:group:user101:rwx
default:mask::rwx
default:other::r-x
[root@localhost ~]# 

Quitar  todos los privileges del user davis. 
[root@localhost ~]# setfacl -x u:davis /home/shared/
[root@localhost ~]# getfacl /home/shared/
getfacl: Removing leading '/' from absolute path names
# file: home/shared/
# owner: root
# group: root
user::rwx
group::r-x
mask::r-x
other::r--
default:user::rwx
default:group::r-x
default:group:user101:rwx
default:mask::rwx
default:other::r-x
[root@localhost ~]# 

Fuentes

https://bencane.com/2012/05/27/acl-using-access-control-lists-on-linux/

No hay comentarios:

Publicar un comentario