#author("2018-08-22T11:33:11+09:00","","")

[[CentOS]]

#author("2018-08-22T11:33:17+09:00","","")

[[CentOS7]]


*OpenLDAPサーバーのインストール [#vf91ec98]
各サーバー間でユーザーのアカウント情報を共有できるように LDAP サーバーを構築。

**OpenLDAP をインストールします。 [#bab260fa]
|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# yum -y install openldap-servers openldap-clients|
|[root@localhost ~]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG|
|[root@localhost ~]# chown ldap. /var/lib/ldap/DB_CONFIG|
|[root@localhost ~]# systemctl start slapd|
|[root@localhost ~]# systemctl enable slapd|

**OpenLDAP の管理者パスワードを設定します。 [#yd0f1bc6]

|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# slappasswd &color(lightpink){←管理者パスワード生成};|
|New password: &br; Re-enter new password: &br; {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx|
|[root@localhost ~]# vi chrootpw.ldif|
|&color(lightpink){# olcRootPW に生成した管理者パスワードを指定する}; &br; dn: olcDatabase={0}config,cn=config &br; changetype: modify &br; add: olcRootPW &br; olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx|
|[root@localhost ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f chrootpw.ldif|
|SASL/EXTERNAL authentication started &br; SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth &br; SASL SSF: 0 &br; modifying entry "olcDatabase={0}config,cn=config"|

**基本的なスキーマを読み込んでおきます。 [#kff9ed0b]
|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif|
|SASL/EXTERNAL authentication started &br; SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth &br; SASL SSF: 0 &br; adding new entry "cn=cosine,cn=schema,cn=config"|
|[root@localhost ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif|
|SASL/EXTERNAL authentication started &br; SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth &br; SASL SSF: 0 &br; adding new entry "cn=nis,cn=schema,cn=config"|
|[root@localhost ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif|
|SASL/EXTERNAL authentication started &br; SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth &br; SASL SSF: 0 &br; adding new entry "cn=inetorgperson,cn=schema,cn=config"|

**自身のドメイン名を設定します。 [#pac8cf53]
|BGCOLOR(black):COLOR(white):|c
|&color(lightpink){# ディレクトリマネージャーのパスワード生成};|
|[root@localhost ~]# slappasswd|
|New password: &br; Re-enter new password: &br; {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx|
|[root@localhost ~]# vi chdomain.ldif|
|&color(lightpink){# dc=***,dc=*** は自身のドメイン名に置き換え &br; # (以下はドメイン名「hoge.com」の場合の例) &br; # olcRootPW に生成したディレクトリマネージャーのパスワードを指定する};|
|dn: olcDatabase={1}monitor,cn=config &br; changetype: modify &br; replace: olcAccess &br; olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" &br;   read by dn.base="cn=Manager,dc=hoge,dc=com" read by * none &br;  &br; dn: olcDatabase={2}hdb,cn=config &br; changetype: modify &br; replace: olcSuffix &br; olcSuffix: dc=hoge,dc=com &br;  &br; dn: olcDatabase={2}hdb,cn=config &br; changetype: modify &br; replace: olcRootDN &br; olcRootDN: cn=Manager,dc=hoge,dc=com &br;  &br; dn: olcDatabase={2}hdb,cn=config &br; changetype: modify &br; add: olcRootPW &br; olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx &br;  &br; dn: olcDatabase={2}hdb,cn=config &br; changetype: modify &br; add: olcAccess &br; olcAccess: {0}to attrs=userPassword,shadowLastChange by &br;   dn="cn=Manager,dc=hoge,dc=com" write by anonymous auth by self write by * none &br; olcAccess: {1}to dn.base="" by * read &br; olcAccess: {2}to * by dn="cn=Manager,dc=hoge,dc=com" write by * read|
|[root@localhost ~]# ldapmodify -Y EXTERNAL -H ldapi:/// -f chdomain.ldif|
|SASL/EXTERNAL authentication started &br; SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth &br; SASL SSF: 0 &br; modifying entry "olcDatabase={1}monitor,cn=config" &br;  &br; modifying entry "olcDatabase={2}hdb,cn=config" &br;  &br; modifying entry "olcDatabase={2}hdb,cn=config" &br;  &br; modifying entry "olcDatabase={2}hdb,cn=config"|
|[root@localhost ~]# vi basedomain.ldif|
|&color(lightpink){# dc=***,dc=*** は自身のドメイン名に置き換え &br; # (以下はドメイン名「hoge.com」の場合の例) &br; # 「dc: hoge」の箇所は自身のドメイン名の先頭部分に置き換え}; &br; dn: dc=hoge,dc=com &br; objectClass: top &br; objectClass: dcObject &br; objectclass: organization &br; o: Hoge Com &br; dc: hoge &br;  &br; dn: cn=Manager,dc=hoge,dc=com &br; objectClass: organizationalRole &br; cn: Manager &br; description: Directory Manager &br;  &br; dn: ou=People,dc=hoge,dc=com &br; objectClass: organizationalUnit &br; ou: People &br;  &br; dn: ou=Group,dc=hoge,dc=com &br; objectClass: organizationalUnit &br; ou: Group|
|[root@localhost ~]# ldapadd -x -D cn=Manager,dc=hoge,dc=com -W -f basedomain.ldif |
|Enter LDAP Password: &color(lightpink){# ディレクトリマネージャーのパスワード}; &br; adding new entry "dc=hoge,dc=com" &br;  &br; adding new entry "cn=Manager,dc=hoge,dc=com" &br;  &br; adding new entry "ou=People,dc=hoge,dc=com" &br;  &br; adding new entry "ou=Group,dc=hoge,dc=com"|

**Firewalld を有効にしている場合は、LDAPサービスの許可が必要です。なお、LDAP は 389/TCP を使用します。 [#v6f7b311]

|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# firewall-cmd --add-service=ldap --permanent|
|success|
|[root@localhost ~]# firewall-cmd --reload|
|success|


-----


*OpenLDAP ユーザーアカウントを追加する [#k632bec0]
''LDAP'' サーバーにユーザーアカウントを追加します。


**ユーザーを個別に追加します [#la8090f3]
|BGCOLOR(black):COLOR(white):|c
|&color(lightpink){# パスワード生成};|
|[root@localhost ~]# slappasswd|
|New password: &br; Re-enter new password: &br; {SSHA}xxxxxxxxxxxxxxxxx|
|[root@localhost ~]# vi ldapuser.ldif|
|&color(lightpink){# 新規作成}; &br; &color(lightpink){# dc=***,dc=*** は自身のドメイン名に置き換える}; &br; &color(lightpink){# (以下はドメイン名「hoge.com」の場合の例)};|
|dn: uid=cent,ou=People,dc=hoge,dc=com &br; objectClass: inetOrgPerson &br; objectClass: posixAccount &br; objectClass: shadowAccount &br; cn: Cent &br; sn: Linux &br; userPassword: {SSHA}xxxxxxxxxxxxxxxxx &br; loginShell: /bin/bash &br; uidNumber: 1000 &br; gidNumber: 1000 &br; homeDirectory: /home/cent &br;  &br; dn: cn=cent,ou=Group,dc=hoge,dc=com &br; objectClass: posixGroup &br; cn: Cent &br; gidNumber: 1000 &br; memberUid: cent|
|[root@localhost ~]# ldapadd -x -D cn=Manager,dc=hoge,dc=com -W -f ldapuser.ldif|
|Enter LDAP Password: &br; adding new entry "uid=cent,ou=People,dc=hoge,dc=com" &br;  &br; adding new entry "cn=cent,ou=Group,dc=hoge,dc=com"|


**ローカル ''passwd/group'' に登録されているユーザー/グループを ''LDAP'' ディレクトリに一括登録します [#x2070a41]
|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# vi ldapuser.sh|
|&color(lightpink){# ローカルの ''UID'' 番号が ''1000-9999'' のユーザーとそれらが所属するグループを抽出 &br; # ''SUFFIX=***'' は自身のドメイン名に置き換え &br; # 一例ですのでご自由に改変してください};|

 #!/bin/bash
 
 SUFFIX='dc=hoge,dc=com'
 LDIF='ldapuser.ldif'
 
 echo -n > $LDIF
 GROUP_IDS=()
 grep "x:[1-9][0-9][0-9][0-9]:" /etc/passwd | (while read TARGET_USER
 do
     USER_ID="$(echo "$TARGET_USER" | cut -d':' -f1)"
 
     USER_NAME="$(echo "$TARGET_USER" | cut -d':' -f5 | cut -d' ' -f1,2)"
     [ ! "$USER_NAME" ] && USER_NAME="$USER_ID"
 
     LDAP_SN="$(echo "$USER_NAME" | cut -d' ' -f2)"
     [ ! "$LDAP_SN" ] && LDAP_SN="$USER_NAME"
 
     LASTCHANGE_FLAG="$(grep "${USER_ID}:" /etc/shadow | cut -d':' -f3)"
     [ ! "$LASTCHANGE_FLAG" ] && LASTCHANGE_FLAG="0"
 
     SHADOW_FLAG="$(grep "${USER_ID}:" /etc/shadow | cut -d':' -f9)"
     [ ! "$SHADOW_FLAG" ] && SHADOW_FLAG="0"
 
     GROUP_ID="$(echo "$TARGET_USER" | cut -d':' -f4)"
     [ ! "$(echo "${GROUP_IDS[@]}" | grep "$GROUP_ID")" ] && GROUP_IDS=("${GROUP_IDS[@]}" "$GROUP_ID")
 
     echo "dn: uid=$USER_ID,ou=People,$SUFFIX" >> $LDIF
     echo "objectClass: inetOrgPerson" >> $LDIF
     echo "objectClass: posixAccount" >> $LDIF
     echo "objectClass: shadowAccount" >> $LDIF
     echo "sn: $LDAP_SN" >> $LDIF
     echo "givenName: $(echo "$USER_NAME" | awk '{print $1}')" >> $LDIF
     echo "cn: $USER_NAME" >> $LDIF
     echo "displayName: $USER_NAME" >> $LDIF
     echo "uidNumber: $(echo "$TARGET_USER" | cut -d':' -f3)" >> $LDIF
     echo "gidNumber: $(echo "$TARGET_USER" | cut -d':' -f4)" >> $LDIF
     echo "userPassword: {crypt}$(grep "${USER_ID}:" /etc/shadow | cut -d':' -f2)" >> $LDIF
     echo "gecos: $USER_NAME" >> $LDIF
     echo "loginShell: $(echo "$TARGET_USER" | cut -d':' -f7)" >> $LDIF
     echo "homeDirectory: $(echo "$TARGET_USER" | cut -d':' -f6)" >> $LDIF
     echo "shadowExpire: $(passwd -S "$USER_ID" | awk '{print $7}')" >> $LDIF
     echo "shadowFlag: $SHADOW_FLAG" >> $LDIF
     echo "shadowWarning: $(passwd -S "$USER_ID" | awk '{print $6}')" >> $LDIF
     echo "shadowMin: $(passwd -S "$USER_ID" | awk '{print $4}')" >> $LDIF
     echo "shadowMax: $(passwd -S "$USER_ID" | awk '{print $5}')" >> $LDIF
     echo "shadowLastChange: $LASTCHANGE_FLAG" >> $LDIF
     echo >> $LDIF
 done
 
 for TARGET_GROUP_ID in "${GROUP_IDS[@]}"
 do
     LDAP_CN="$(grep ":${TARGET_GROUP_ID}:" /etc/group | cut -d':' -f1)"
 
     echo "dn: cn=$LDAP_CN,ou=Group,$SUFFIX" >> $LDIF
     echo "objectClass: posixGroup" >> $LDIF
     echo "cn: $LDAP_CN" >> $LDIF
     echo "gidNumber: $TARGET_GROUP_ID" >> $LDIF
 
     for MEMBER_UID in $(grep ":${TARGET_GROUP_ID}:" /etc/passwd | cut -d':' -f1,3)
     do
         UID_NUM=$(echo "$MEMBER_UID" | cut -d':' -f2)
         [ $UID_NUM -ge 1000 -a $UID_NUM -le 9999 ] && echo "memberUid: $(echo "$MEMBER_UID" | cut -d':' -f1)" >> $LDIF
     done
     echo >> $LDIF
 done
 )


|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# sh ldapuser.sh|
|[root@localhost ~]# ldapadd -x -D cn=Manager,dc=hoge,dc=com -W -f ldapuser.ldif|
|Enter LDAP Password: &br; adding new entry "uid=cent,ou=People,dc=hoge,dc=com" &br; adding new entry "uid=redhat,ou=People,dc=hoge,dc=com" &br; adding new entry "uid=ubuntu,ou=People,dc=hoge,dc=com" &br; adding new entry "uid=debian,ou=People,dc=hoge,dc=com" &br; adding new entry "cn=cent,ou=Group,dc=hoge,dc=com" &br; adding new entry "cn=redhat,ou=Group,dc=hoge,dc=com" &br; adding new entry "cn=ubuntu,ou=Group,dc=hoge,dc=com" &br; adding new entry "cn=debian,ou=Group,dc=hoge,dc=com"|


**登録したユーザーとグループを削除する場合は以下のようにします [#md34ada2]

|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# ldapdelete -x -W -D 'cn=Manager,dc=hoge,dc=com' "uid=cent,ou=People,dc=hoge,dc=com"|
|Enter LDAP Password:|
|[root@localhost ~]# ldapdelete -x -W -D 'cn=Manager,dc=hoge,dc=com' "cn=cent,ou=Group,dc=hoge,dc=com"|
|Enter LDAP Password:|


-----


*OpenLDAP LDAPクライアントの設定 [#z890674f]
LDAP サーバーのユーザーアカウント情報を共有できるように LDAP クライアントとしての設定をします。

**OpenLDAP クライアントをインストールして設定します。 [#x2a584b0]

|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# yum -y install openldap-clients nss-pam-ldapd|
|&color(lightpink){# ldapserver=(自身のLDAPサーバーのホスト名またはIPアドレス) &br; # ldapbasedn="dc=(自身のドメイン名)"};|
|[root@localhost ~]# authconfig --enableldap --enableldapauth --ldapserver=centos.hoge.com --ldapbasedn="dc=hoge,dc=com" --enablemkhomedir --update|
|[root@localhost ~]# exit|
|logout &br;  &br; CentOS Linux 7 (Core) &br; Kernel 3.10.0-123.20.1.el7.x86_64 on an x86_64 &br;  &br; localhost login: &color(lime){redhat}; &color(lightpink){# LDAPユーザー}; &br; Password:&color(lightpink){# パスワード}; &br; Creating directory '/home/redhat'.|
|[redhat@localhost ~]$ &color(lightpink){# ログインできた};|
|[redhat@localhost ~]$ passwd &color(lightpink){# パスワード変更は通常通り};|
|Changing password for user redhat. &br; Enter login(LDAP) password: &color(lightpink){# 現在のパスワード}; &br; New password: &color(lightpink){# 新しいパスワード}; &br; Retype new password: &br; LDAP password information changed for redhat &br; passwd: all authentication tokens updated successfully.|

**SELinux を有効にしている場合、ホームディレクトリがない場合に自動生成する mkhomedir が SELinux によって拒否されるため、許可ルールの追加が必要です。 [#ocd66d8e]

|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# vi mkhomedir.te|
|&color(lightpink){# 以下の内容で新規作成};|
 module mkhomedir 1.0;
 
 require {
         type unconfined_t;
         type oddjob_mkhomedir_exec_t;
         class file entrypoint;
 }
 
 #============= unconfined_t ==============
 allow unconfined_t oddjob_mkhomedir_exec_t:file entrypoint;

|BGCOLOR(black):COLOR(white):|c
|&color(lightpink){# 中間ファイル生成};|
|[root@localhost ~]# checkmodule -m -M -o mkhomedir.mod mkhomedir.te|
|checkmodule: loading policy configuration from mkhomedir.te &br; checkmodule: policy configuration loaded &br; checkmodule: writing binary representation (version 17) to mkhomedir.mod &br; &color(lightpink){# 中間ファイルからモジュール生成};|
|[root@localhost ~]# semodule_package --outfile mkhomedir.pp --module mkhomedir.mod|
|&color(lightpink){# モジュールインストール};|
|[root@localhost ~]# semodule -i mkhomedir.pp|


-----


*OpenLDAP LDAP over SSL/TLS [#a7ba3fab]

LDAP over TLS を設定し、LDAP サーバーとクライアント間の通信をよりセキュアにします。

**まずは SSL 証明書を作成しておきます。 [#n57906e4]

**LDAP サーバーの設定です。 [#o4fa2d78]

|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# cp /etc/pki/tls/certs/server.key /etc/pki/tls/certs/server.crt /etc/pki/tls/certs/ca-bundle.crt /etc/openldap/certs/|
|[root@localhost ~]# chown ldap. /etc/openldap/certs/server.key /etc/openldap/certs/server.crt /etc/openldap/certs/ca-bundle.crt|
|[root@localhost ~]# vi mod_ssl.ldif|
|&color(lightpink){# 新規作成};|

 dn: cn=config
 changetype: modify
 add: olcTLSCACertificateFile
 olcTLSCACertificateFile: /etc/openldap/certs/ca-bundle.crt
 -
 replace: olcTLSCertificateFile
 olcTLSCertificateFile: /etc/openldap/certs/server.crt
 -
 replace: olcTLSCertificateKeyFile
 olcTLSCertificateKeyFile: /etc/openldap/certs/server.key

|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# ldapmodify -Y EXTERNAL -H ldapi:/// -f mod_ssl.ldif|

 SASL/EXTERNAL authentication started
 SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
 SASL SSF: 0
 modifying entry "cn=config"

|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# vi /etc/sysconfig/slapd|
|&color(lightpink){# 9行目:追記};  &br; SLAPD_URLS="ldapi:/// ldap:/// ldaps:///"|
|[root@localhost ~]# systemctl restart slapd|

**LDAP クライアントの設定です [#ncc6f3eb]

|BGCOLOR(black):COLOR(white):|c
|[root@localhost ~]# echo "TLS_REQCERT allow" >> /etc/openldap/ldap.conf|
|[root@localhost ~]# echo "tls_reqcert allow" >> /etc/nslcd.conf|
|[root@localhost ~]# authconfig --enableldaptls --update|
|getsebool: SELinux is disabled|
|[root@localhost ~]# exit|
|logout &br;  &br; CentOS Linux 7 (Core) &br; Kernel 3.10.0-123.20.1.el7.x86_64 on an x86_64 &br;  &br; localhost login: redhat &br; Password: &br; Last login: Tue Aug 19 19:55:52 on ttyS0|
|[redhat@localhost ~]$ &color(lightpink){# ログインできた};|

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS