#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Check policies"
## exposure: dangerous
## packages:
## - univention-s4-connector
## roles:
## - domaincontroller_master
## - domaincontroller_backup
## bugs:
##  - 49838

# shellcheck source=../../lib/base.sh
. "$TESTLIBPATH/base.sh" || exit 137
# shellcheck source=../../lib/udm.sh
. "$TESTLIBPATH/udm.sh" || exit 137
# shellcheck source=../../lib/random.sh
. "$TESTLIBPATH/random.sh" || exit 137
. "s4connector.sh" || exit 137

RETVAL=100

test -n "$connector_s4_ldap_host" || exit 137
connector_running_on_this_host || exit 137
SYNCMODE="$(ad_get_sync_mode)"
ad_set_sync_mode "sync"

ucr set connector/s4/mapping/msgpwl?no

eval "$(ucr shell)"

ucr set connector/s4/mapping/msgpwl=yes

service univention-s4-connector restart

policy_name="{E1E20275-CBF3-46D4-B926-$(random_chars 12 "ABCDEF0123456789")}"
policy_base="CN=$policy_name,CN=Policies,CN=System,$samba4_ldap_base"
policy_base_ucs="CN=$policy_name,CN=Policies,CN=System,$ldap_base"

echo -e "
dn: CN=$policy_name,CN=Policies,CN=System,$samba4_ldap_base
objectClass: top
objectClass: container
objectClass: groupPolicyContainer
cn: $policy_name
displayName: Neues Gruppenrichtlinienobjekt
gPCFileSysPath: \\\\$domainname\\SysVol\\$domainname\\Policies\\$policy_name
gPCFunctionalityVersion: 2
flags: 0
gPCMachineExtensionNames: [{0ACDD40C-75AC-47AB-BAA0-BF6DE7E7FE63}{2DA6AA7F-8C88-4194-A558-0D36E7FD3E64}][{B587E2B1-4D59-4E7E-AED9-22B9DF11D053}{06993B16-A5C7-47EB-B61C-B1CB7EE600AC}][{C6DC5466-785A-11D2-84D0-00C04FB169F7}{942A8E4F-A261-11D1-A760-00C04FB9603F}]
versionNumber: 10

dn: CN=Machine,CN=$policy_name,CN=Policies,CN=System,$samba4_ldap_base
objectClass: top
objectClass: container
cn: Machine

dn: CN=Microsoft,CN=Machine,CN=$policy_name,CN=Policies,CN=System,$samba4_ldap_base
objectClass: top
objectClass: container
cn: Microsoft

dn: CN=Windows,CN=Microsoft,CN=Machine,CN=$policy_name,CN=Policies,CN=System,$samba4_ldap_base
objectClass: top
objectClass: container
cn: Windows

dn: CN=IEEE80211,CN=Windows,CN=Microsoft,CN=Machine,CN=$policy_name,CN=Policies,CN=System,$samba4_ldap_base
objectClass: top
objectClass: container
cn: IEEE80211

dn: CN=Neue Drahtlosnetzwerkrichtlinie,CN=IEEE80211,CN=Windows,CN=Microsoft,CN=Machine,CN=$policy_name,CN=Policies,CN=System,$samba4_ldap_base
objectClass: top
objectClass: ms-net-ieee-80211-GroupPolicy
cn: Neue Drahtlosnetzwerkrichtlinie
description: Beispielbeschreibung
ms-net-ieee-80211-GP-PolicyGUID: {70463c70-ba51-4f25-a711-0d7d2abf2c82}
ms-net-ieee-80211-GP-PolicyData:: [encoded data, 4800 characters]

dn: CN=IEEE8023,CN=Windows,CN=Microsoft,CN=Machine,CN=$policy_name,CN=Policies,CN=System,$samba4_ldap_base
objectClass: top
objectClass: container
cn: IEEE8023

dn: CN=Neue Richtlinie für Kabelnetzwerke,CN=IEEE8023,CN=Windows,CN=Microsoft,CN=Machine,CN=$policy_name,CN=Policies,CN=System,$samba4_ldap_base
objectClass: top
objectClass: ms-net-ieee-8023-GroupPolicy
cn:: TmV1ZSBSaWNodGxpbmllIGbDvHIgS2FiZWxuZXR6d2Vya2U=
description: Beispielbeschreibung
ms-net-ieee-8023-GP-PolicyGUID: {8705b171-9380-4e17-b553-86f3edb7cadb}
ms-net-ieee-8023-GP-PolicyData:: PD94bWwgdmVyc2lvbj0iMS4wIj8+DQo8TEFOUG9saWN5IHhtbG5zPSJodHRwOi8vd3d3Lm1pY3Jvc29mdC5jb20vbmV0d29ya2luZy9MQU4vcG9saWN5L3YxIj4NCgk8bmFtZT5OZXVlIFJpY2h0bGluaWUgZsO8ciBLYWJlbG5ldHp3ZXJrZTwvbmFtZT4NCgk8ZGVzY3JpcHRpb24+QmVpc3BpZWxiZXNjaHJlaWJ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1TZXJ2ZXJWYWxpZGF0aW9uIHhtbG5zPSJodHRwOi8vd3d3Lm1pY3Jvc29mdC5jb20vcHJvdmlzaW9uaW5nL01zUGVhcENvbm5lY3Rpb25Qcm9wZXJ0aWVzVjIiPnRydWU8L1BlcmZvcm1TZXJ2ZXJWYWxpZGF0aW9uPjxBY2NlcHRTZXJ2ZXJOYW1lIHhtbG5zPSJodHRwOi8vd3d3Lm1pY3Jvc29mdC5jb20vcHJvdmlzaW9uaW5nL01zUGVhcENvbm5lY3Rpb25Qcm9wZXJ0aWVzVjIiPmZhbHNlPC9BY2NlcHRTZXJ2ZXJOYW1lPjwvUGVhcEV4dGVuc2lvbnM+PC9FYXBUeXBlPjwvRWFwPjwvQ29uZmlnPjwvRWFwSG9zdENvbmZpZz48L0VBUENvbmZpZz4NCgkJCQkJPC9PbmVYPg0KCQkJCTwvc2VjdXJpdHk+DQoJCQk8L01TTT4NCgkJPC9MQU5Qcm9maWxlPg0KCTwvcHJvZmlsZUxpc3Q+DQo8L0xBTlBvbGljeT4NCg==

dn: CN=Wireless,CN=Windows,CN=Microsoft,CN=Machine,CN=$policy_name,CN=Policies,CN=System,$samba4_ldap_base
objectClass: top
objectClass: container
cn: Wireless

dn: CN=Neue XP-Drahtlosnetzwerkrichtlinie,CN=Wireless,CN=Windows,CN=Microsoft,CN=Machine,CN=$policy_name,CN=Policies,CN=System,$samba4_ldap_base
objectClass: top
objectClass: msieee80211-Policy
cn: Neue XP-Drahtlosnetzwerkrichtlinie
description: Beispielbeschreibung
msieee80211-DataType: 256
msieee80211-ID: {2d403346-3931-4ebe-aea4-014fedf15b69}
msieee80211-Data:: AwAAAPgAAAAwKgAAAAAAAAEAAAABAAAAAQAAAOQAAABOAEUAVwBTAFMASQBEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABwAAAAMAAAAAAAAABQAAAAEAAAACAAAAAQAAAAIAAAAZAAAAAAAAAAEAAAABAAAAAAAAAAMAAAAFAAAAEgAAAAEAAAAUAAAAQgBlAGkAcwBwAGkAZQBsAGIAZQBzAGMAaAByAGUAaQBiAHUAbgBnAAAAAAABAAAAAAAAAAEAAAADAAAAAQAAAAEAAAABAAAAAgAAAIAAAADAqAAAAAAAAAIAAAAUAAAAMCoAAAAAAAABAAAAAQAAAAAAAAABAAAAFAAAADAqAAAAAAAAAQAAAAEAAAAAAAAA

" | ldbadd --verbose -H /var/lib/samba/private/sam.ldb || fail_test 110

ad_wait_for_synchronization; fail_bool 0 110

univention-ldapsearch -b "$policy_base_ucs" -s base dn -LLL  || fail_test 110
univention-ldapsearch -b "CN=Neue Drahtlosnetzwerkrichtlinie,  CN=IEEE80211,CN=Windows,CN=Microsoft,CN=Machine,$policy_base_ucs" -s base dn -LLL || fail_test 110
univention-ldapsearch -b "CN=Neue Richtlinie für Kabelnetzwerke,CN=IEEE8023,CN=Windows,CN=Microsoft,CN=Machine,$policy_base_ucs" -s base dn -LLL || fail_test 110
univention-ldapsearch -b "CN=Neue XP-Drahtlosnetzwerkrichtlinie,CN=Wireless,CN=Windows,CN=Microsoft,CN=Machine,$policy_base_ucs" -s base dn -LLL || fail_test 110

ldbdel -H /var/lib/samba/private/sam.ldb "CN=Neue XP-Drahtlosnetzwerkrichtlinie,CN=Wireless,CN=Windows,CN=Microsoft,CN=Machine,$policy_base" || fail_test 110
ldbdel -H /var/lib/samba/private/sam.ldb "CN=Neue Richtlinie für Kabelnetzwerke,CN=IEEE8023,CN=Windows,CN=Microsoft,CN=Machine,$policy_base" || fail_test 110
ldbdel -H /var/lib/samba/private/sam.ldb "CN=Neue Drahtlosnetzwerkrichtlinie,CN=IEEE80211,CN=Windows,CN=Microsoft,CN=Machine,$policy_base" || fail_test 110

ad_wait_for_synchronization; fail_bool 0 110

univention-ldapsearch -b "CN=Neue Drahtlosnetzwerkrichtlinie,CN=IEEE80211,CN=Windows,CN=Microsoft,CN=Machine,$policy_base_ucs" -s base dn -LLL && fail_test 110
univention-ldapsearch -b "CN=Neue Richtlinie für Kabelnetzwerke,CN=IEEE8023,CN=Windows,CN=Microsoft,CN=Machine,$policy_base_ucs" -s base dn -LLL && fail_test 110
univention-ldapsearch -b "CN=Neue XP-Drahtlosnetzwerkrichtlinie,CN=Wireless,CN=Windows,CN=Microsoft,CN=Machine,$policy_base_ucs" -s base dn -LLL && fail_test 110

ldbdel -H /var/lib/samba/private/sam.ldb -r "$policy_base" || fail_test 110
ad_wait_for_synchronization; fail_bool 0 110
univention-ldapsearch -b "$policy_base_ucs" -s base dn -LLL  && fail_test 110

ucr set connector/s4/mapping/msgpwl="$connector_s4_mapping_msgpwl"
ad_set_sync_mode "$SYNCMODE"

exit "$RETVAL"
