|
| 1 | +#!/bin/bash |
| 2 | +# This library contains some functions to use and setup 389 |
| 3 | +# directory server ( https://www.port389.org/index.html ) |
| 4 | +# which is an "enterprise-class Open Source LDAP server for Linux.". |
| 5 | +# SPDX-License-Identifier: GPL-3.0-only or GPL-3.0-or-later |
| 6 | +# |
| 7 | +# Copyright (C) 2024 Raul Mahiques |
| 8 | +# |
| 9 | +# This program is free software: you can redistribute it and/or modify |
| 10 | +# it under the terms of the GNU General Public License as published by |
| 11 | +# the Free Software Foundation, either version 3 of the License, or |
| 12 | +# (at your option) any later version. |
| 13 | +# |
| 14 | +# This program is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +# GNU General Public License for more details. |
| 18 | +# |
| 19 | +# For more details find a copy of the license here: |
| 20 | +# https://www.gnu.org/licenses/gpl-3.0.txt |
| 21 | +# |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +####################################### |
| 26 | +# DS389 - restrict permissions: |
| 27 | +# - prevent normal users from reading the whole directory |
| 28 | +# Arguments: |
| 29 | +# 1 - _ldap_uri |
| 30 | +# 2 - _ldap_basedn |
| 31 | +# 3 - _admin_user |
| 32 | +# 4 - _admin_pwd |
| 33 | +# Examples: |
| 34 | +# ds389_restrict_permissions "<_ldap_uri>" "<_ldap_basedn>" "<_admin_user>" "<_admin_pwd>" |
| 35 | +####################################### |
| 36 | +function ds389_restrict_permissions() { |
| 37 | + local _ldap_uri="$1" |
| 38 | + local _ldap_basedn="$2" |
| 39 | + local _admin_user="$3" |
| 40 | + local _admin_pwd="$4" |
| 41 | + ldapmodify -D "${_admin_user}" -w "${_admin_pwd}" -x -H "${_ldap_uri}" << EOL |
| 42 | +dn: ou=people,${_ldap_basedn} |
| 43 | +changetype: modify |
| 44 | +delete: aci |
| 45 | +aci: (targetattr="objectClass || description || nsUniqueId || uid || displayName || loginShell || uidNumber || gidNumber || gecos || homeDirectory || cn || memberOf || mail || nsSshPublicKey || nsAccountLock || userCertificate")(targetfilter="(objectClass=posixaccount)")(version 3.0; acl "Enable anyone user read"; allow (read, search, compare)(userdn="ldap:///anyone");) |
| 46 | + |
| 47 | +dn: ou=people,${_ldap_basedn} |
| 48 | +changetype: modify |
| 49 | +add: aci |
| 50 | +aci: (targetattr="objectClass || description || nsUniqueId || uid || displayName || loginShell || uidNumber || gidNumber || gecos || homeDirectory || cn || memberOf || mail || nsSshPublicKey || nsAccountLock || userCertificate")(targetfilter="(objectClass=posixaccount)")(version 3.0; acl "Enable self user read"; allow (read, search, compare)(userdn="ldap:///self");) |
| 51 | +EOL |
| 52 | +} |
| 53 | + |
| 54 | +####################################### |
| 55 | +# DS389 - Grant user privileges to read the whole directory |
| 56 | +# Arguments: |
| 57 | +# 1 - _ldap_uri |
| 58 | +# 2 - _ldap_basedn |
| 59 | +# 3 - _admin_user |
| 60 | +# 4 - _admin_pwd |
| 61 | +# 5 - Username (Default: ldap_user) |
| 62 | +# Examples: |
| 63 | +# ds389_user_private_read "ldap://ldap.mydemo.lab:389" "dc=mydemo,dc=lab" "cn=Directory Manager" "secret" "ldap_user" |
| 64 | +####################################### |
| 65 | +function ds389_ldap_user_user_private_read() { |
| 66 | + local _ldap_uri="$1" |
| 67 | + local _ldap_basedn="$2" |
| 68 | + local _admin_user="$3" |
| 69 | + local _admin_pwd="$4" |
| 70 | + local ldap_user="$5" |
| 71 | + ldapmodify -D "${_admin_user}" -w "${_admin_pwd}" -x -H "${_ldap_uri}" << EOL |
| 72 | +dn: cn=user_private_read,ou=permissions,${_ldap_basedn} |
| 73 | +changetype: modify |
| 74 | +add: member |
| 75 | +member: uid=${ldap_user},ou=people,${_ldap_basedn} |
| 76 | +EOL |
| 77 | +} |
| 78 | + |
| 79 | +####################################### |
| 80 | +# DS389 - Verify user has access |
| 81 | +# Arguments: |
| 82 | +# 1 - ldap user DN |
| 83 | +# 2 - ldap user pwd |
| 84 | +# 3 - _ldap_uri |
| 85 | +# 4 - _ldap_basedn |
| 86 | +# Examples: |
| 87 | +# ds389_ldap_user_access_check "cn=Directory Manager" "secret" "uid=ldap_user,ou=people,dc=mydemo,dc=lab" "mypassword" |
| 88 | +####################################### |
| 89 | +function ds389_ldap_user_access_check() { |
| 90 | + local _ldap_user_dn="${1}" |
| 91 | + local _ldap_user_pwd="${2}" |
| 92 | + local _ldap_uri="${3}" |
| 93 | + local _ldap_basedn="${4}" |
| 94 | + ldapsearch -x -D "${_ldap_user_dn}" -w "${_ldap_user_pwd}" -H "${_ldap_uri}" -b "${_ldap_basedn}" |
| 95 | +} |
| 96 | + |
| 97 | +####################################### |
| 98 | +# DS389 - Install 389 Directory server |
| 99 | +# Arguments: |
| 100 | +# 1 - _ldap_uri |
| 101 | +# 2 - _ldap_basedn |
| 102 | +# 3 - _admin_user |
| 103 | +# 4 - _admin_pwd |
| 104 | +# Examples: |
| 105 | +# ds389_install "ldap://ldap.mydemo.lab:389" "dc=mydemo,dc=lab" "cn=Directory Manager" "secret" |
| 106 | +####################################### |
| 107 | +function ds389_install() { |
| 108 | + local _ldap_uri="${1}" |
| 109 | + local _ldap_basedn="${2}" |
| 110 | + local _admin_user="${3}" |
| 111 | + local _admin_pwd="${4}" |
| 112 | + # add the repo |
| 113 | + helm repo add suse-lab-setup https://opensource.suse.com/lab-setup |
| 114 | + helm repo update |
| 115 | + # installs the chart with default parameters |
| 116 | + if [[ -f values.yaml ]] |
| 117 | + then |
| 118 | + helm upgrade --install ds389 --namespace ds389 suse-lab-setup/ds389 -f values.yaml |
| 119 | + else |
| 120 | + helm upgrade --install ds389 --namespace ds389 suse-lab-setup/ds389 |
| 121 | + fi |
| 122 | + sleep 60 |
| 123 | + ds389_restrict_permissions "${_ldap_uri}" "${_ldap_basedn}" "${_admin_user}" "${_admin_pwd}" |
| 124 | + ds389_ldap_user_user_private_read "${_ldap_uri}" "${_ldap_basedn}" "${_admin_user}" "${_admin_pwd}" "ldap_user" |
| 125 | +} |
| 126 | + |
| 127 | +####################################### |
| 128 | +# DS389 - Uninstall 389 Directory server |
| 129 | +# Examples: |
| 130 | +# ds389_uninstall |
| 131 | +####################################### |
| 132 | +function ds389_uninstall() { |
| 133 | + helm uninstall ds389 |
| 134 | + sleep 15 |
| 135 | +} |
0 commit comments