- 基礎環境準備
- 服務端配置
- 客戶端配置
- 客戶端驗證
- 附:sudo常見屬性介紹
- 常見錯誤分析
本文接文章openldap服務端安裝配置shell
導入sudo schema數據庫
openldap默認schema中不包含sudo所須要的數據結構,這時須要咱們自行導入sudo schema文件。操做以下後端
[root@mldap01 tmp]# rpm -ql sudo-1.8.6p3-27.el6.x86_64 | grep schema /usr/share/doc/sudo-1.8.6p3/schema.ActiveDirectory /usr/share/doc/sudo-1.8.6p3/schema.OpenLDAP /usr/share/doc/sudo-1.8.6p3/schema.iPlanet [root@mldap01 tmp]# cp -a /usr/share/doc/sudo-1.8.6p3/schema.OpenLDAP /etc/openldap/schema/sudo.schema
[root@mldap01 slapd.d]# slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d config file testing succeeded [root@mldap01 slapd.d]# chown ldap.ldap -R /etc/openldap/slapd.d [root@mldap01 slapd.d]# /etc/init.d/slapd restart Stopping slapd: [ OK ] Starting slapd: [ OK ]
驗證openldap是否支持sudoapi
查看OpenLDAP數據庫目錄中schema所產生的文件
bash
查看sudo schema所支持的對象類型
shell [root@mldap01 ~]# ldapsearch -LLLY EXTERNAL -H ldapi:/// -b cn={12}sudo,cn=schema,cn=config | grep NAME | awk '{print $4,$5}' | sort SASL/EXTERNAL authentication started SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth SASL SSF: 0 NAME 'sudoCommand' NAME 'sudoHost' NAME 'sudoNotAfter' NAME 'sudoNotBefore' NAME 'sudoOption' NAME 'sudoOrder' NAME 'sudoRole' NAME 'sudoRunAs' NAME 'sudoRunAsGroup' NAME 'sudoRunAsUser' NAME 'sudoUser'
服務器
定義sudo規則條目及sudo組數據結構
sudoers的配置信息存放在ou=suders的子樹中,默認OpenLDAP用戶並無指定sudo規則,openldap首先在目錄樹子樹中尋找條目cn=defaults
, 若是找到,那麼全部的sudoOption屬性都會被解析爲全局默認值, 這相似於系統sudo(/etc/sudoers)文件中Defaults語句。oracle
當用戶到OpenLDAP服務端中查詢一個sudo用戶權限時通常有兩到三次查詢。第一次查詢解析全局配置,第二次查詢匹配用戶名或者用戶所在的組(特殊標籤ALL也在這次查詢中匹配),若是沒有找到相關匹配項,則發出第三次查詢,這次查詢返回全部包含用戶組的條目並檢查該用戶是否存在於這些組中。接下來建立openldap的sudoers子樹。具體命令以下。app
[root@mldap01 ~]# cat << EOF | ldapadd -x -D "cn=Manager,dc=gdy,dc=com" -W > dn: ou=sudoers,dc=gdy,dc=com > objectClass: organizationalUnit > objectClass: top > ou: sudoers > > dn: cn=default,ou=sudoers,dc=gdy,dc=com > objectClass: sudoRole > cn: default > description: Default sudoOption's go here > sudoOption: requiretty > sudoOption: !visiblepw > sudoOption: always_set_home > sudoOption: env_reset > sudoOption: env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS" > sudoOption: env_keep+="MALL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE" > sudoOption: env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES" > sudoOption: env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHOME" > sudoOption: env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY" > sudoOption: secure_path=/sbin:/bin:/usr/sbin:/usr/bin > > dn: cn=%dba,ou=sudoers,dc=gdy,dc=com > objectClass: sudoRole > cn: %dba > sudoUser: %dba > sudoRunAsUser: oracle > sudoRunAsUser: grid > sudoOption: !authenticate > sudoCommand: /bin/bash > > dn: cn=%app,ou=sudoers,dc=gdy,dc=com > objectClass: sudoRole > cn: %app > sudoUser: %app > sudoHost: ALL > sudoRunAsUser: appman > sudoOption: !authenticate > sudoCommand: /bin/bash > > dn: cn=%admin,ou=sudoers,dc=gdy,dc=com > objectClass: sudoRole > cn: %admin > sudoUser: %admin > sudoHost: ALL > sudoOption: authenticate > sudoCommand: /bin/rm > sudoCommand: /bin/rmdir > sudoCommand: /bin/chmod > sudoCommand: /bin/chown > sudoCommand: /bin/dd > sudoCommand: /bin/mv > sudoCommand: /bin/cp > sudoCommand: /sbin/fsck* > sudoCommand: /sbin/*remove > sudoCommand: /usr/bin/chattr > sudoCommand: /sbin/mkfs* > sudoCommand: !/usr/bin/passwd > sudoOrder: 0 > > dn: cn=%limit,ou=sudoers,dc=gdy,dc=com > objectClass: top > objectClass: sudoRole > cn: %limit > sudoCommand: /usr/bin/chattr > sudoHost: limit.gdy.com > sudoOption: !authenticate > sudoRunAsUser: ALL > sudoUser: %limit > EOF Enter LDAP Password: adding new entry "ou=sudoers,dc=gdy,dc=com" adding new entry "cn=default,ou=sudoers,dc=gdy,dc=com" adding new entry "cn=%dba,ou=sudoers,dc=gdy,dc=com" adding new entry "cn=%app,ou=sudoers,dc=gdy,dc=com" adding new entry "cn=%admin,ou=sudoers,dc=gdy,dc=com" adding new entry "cn=%limit,ou=sudoers,dc=gdy,dc=com"
以上操做中,讀者不難發現app組能夠經過sudo切換到系統appman用戶下,並且也不須要輸入驗證密碼,同理admin組裏面的用戶只能經過sudo執行容許的命令,其餘越權命令不容許執行。dba組裏面的用戶能夠經過sudo命令切換到系統oracle和grid用戶下,且不須要輸入驗證密碼。limit組裏面的用戶只容許在limit.gdy.com機器上經過sudo執行一條命令,且不須要提供驗證密碼,其餘任何機器都不能使用sudo命令執行命令。manager組裏面的用戶能夠在任何主機上執行sudo命令,沒有任何限制,若是要求OpenLDAP用戶提示輸入密碼,只須要將!authenticate
中的感嘆號去掉便可。ssh
添加OpenLDAP用戶到app組和manager組中
[root@mldap01 ~]# cat << EOF | ldapadd -D "cn=Manager,dc=gdy,dc=com" -c -x -W dn: cn=app,ou=Group,dc=gdy,dc=com objectClass: posixGroup cn: app gidNumber: 10005 dn: cn=manager,ou=Group,dc=gdy,dc=com objectClass: posixGroup cn: manager gidNumber: 10006 dn: uid=jboss,ou=People,dc=gdy,dc=com objectClass: account objectClass: posixAccount objectClass: shadowAccount cn: jboss uid: jboss uidNumber: 20006 gidNumber: 10005 userPassword: {CRYPT}$6$eRYFmeDr$siXSlXkO8OEhzHsLvlslFJZpmLnW1ajWU3wG3b4DVAdP6jYJDpUwwJW2juEir3wbpjU6t2h7sSWfRARb2xJ.G1 homeDirectory: /home/jboss loginShell: /bin/bash dn: uid=manager01,ou=People,dc=gdy,dc=com objectClass: account objectClass: posixAccount objectClass: shadowAccount cn: manager01 uid: manager01 uidNumber: 20007 gidNumber: 10006 userPassword: {CRYPT}$6$yTg3xJfA$VpThv3kvPG1uFw2.n/VgTnk8W05PED9CdEq.RXbz7SdjGd30z51lyuiBUFfd4F8HGQvWV3pWlplqATQh90fpL. homeDirectory: /home/manager01 loginShell: /bin/bash EOF Enter LDAP Password: adding new entry "cn=app,ou=Group,dc=gdy,dc=com" adding new entry "cn=manager,ou=Group,dc=gdy,dc=com" adding new entry "uid=jboss,ou=People,dc=gdy,dc=com" adding new entry "uid=manager01,ou=People,dc=gdy,dc=com"
經過ldapsearch指令查看jboss用戶及app組相關信息
```shell
[root@mldap01 ~]# ldapsearch -x -LLL uid=jboss
dn: uid=jboss,ou=people,dc=gdy,dc=com
objectClass: account
objectClass: posixAccount
objectClass: shadowAccount
cn: jboss
uid: jboss
uidNumber: 20006
gidNumber: 10005
homeDirectory: /home/jboss
loginShell: /bin/bash
[root@mldap01 ~]# ldapsearch -x -LLL cn=app
dn: cn=app,ou=Group,dc=gdy,dc=com
objectClass: posixGroup
cn: app
gidNumber: 10005
```
查看當前sudo版本是否支持openldap的sudo規則,不支持的版本須要進行升級
[user1@test01 openldap]$ rpm -qi sudo | grep -i version Version : 1.8.6p3 Vendor: CentOS // 若是sudo版本爲1.7.4p5則須要對sudo進行升級,不然沒法支持openldap服務器sudo規則的匹配 [user1@test01 openldap]$ sudo -V Sudo version 1.8.6p3 Sudoers policy plugin version 1.8.6p3 Sudoers file grammar version 42 Sudoers I/O plugin version 1.8.6p3 //sudo版本的信息
客戶端加入OpenLDAP服務端
本文略,具體參考openldap客戶端安裝
修改nsswitch.conf配置文件,添加sudo查找順序,命令以下
[root@mldap01 ~]# cat >> /etc/nsswitch.conf << EOF > sudoers: ldap files > EOF [root@mldap01 ~]# cat /etc/nsswitch.conf | grep -i sudoers sudoers: ldap files
修改sudo-ldap.conf配置文件,添加支持後端openldap驗證sudo的參數,命令以下:
[root@mldap01 ~]# cat >> /etc/sudo-ldap.conf << EOF > SUDOERS_BASE ou=Sudoers,dc=gdy,dc=com > EOF [root@mldap01 ~]# cat /etc/sudo-ldap.conf | grep -i Sudoers ## SUDOERS_BASE base ## Multiple SUDOERS_BASE lines may be specified, in which #sudoers_base ou=SUDOers,dc=example,dc=com ## SUDOERS_DEBUG debug_level #sudoers_debug 1 uri ldap://mldap01.gdy.com SUDOERS_BASE ou=sudoers,dc=gdy,dc=com
驗證OpenLDAP賬號經過sudo提取系統用戶權限
```shell ldif格式的appman用戶及組 dn: uid=appman,ou=people,dc=gdy,dc=com uid: appman cn: appman objectClass: account objectClass: posixAccount objectClass: top objectClass: shadowAccount userPassword: {CRYPT}$6$2v5NKeJj$e1G81HKtTa0fnNX1nqhX1ym/63/X0lHEnobGd0ZBHiwUhcy9SlF8RC.6bPH.CtEbfEVSnn.rRBkRWCAisUrgn/ shadowLastChange: 17675 shadowMin: 0 shadowMax: 99999 shadowWarning: 7 loginShell: /bin/bash uidNumber: 30000 gidNumber: 30000 homeDirectory: /home/appman dn: cn=appman,ou=Group,dc=gdy,dc=com objectClass: posixGroup objectClass: top cn: appman userPassword: {crypt}x gidNumber: 30000 [root@test01 ~]# ssh jboss@127.0.0.1 jboss@127.0.0.1's password: Last login: Thu May 24 13:49:07 2018 from localhost [jboss@test01 ~]$ sudo -l Matching Defaults entries for jboss on this host: requiretty, !visiblepw, always_set_home, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS", env_keep+="MALL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHOME", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY", secure_path=/sbin:/bin:/usr/sbin:/usr/bin, !visiblepw, always_set_home, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin User jboss may run the following commands on this host: (appman) NOPASSWD: /bin/bash [jboss@test01 ~]$ sudo -i -u appman [appman@test01 ~]$ # 切換成功 ```
sudo常見的屬性有如下幾個
例1,報錯/home/appman: No such file or directory
[jboss@test01 ~]$ sudo -i -u appman sudo: unable to change directory to /home/appman: No such file or directory sudo: unable to execute /bin/bash: No such file or directory
解決方法:在本機建立appman的家目錄就能夠了