#!/bin/bash

testuser="testuser2"
testgroup="testgroup2"

privatedir=$(samba -b | sed -n 's/^   PRIVATE_DIR: //p')
samldb="$privatedir/sam.ldb"

samba-tool user create "$testuser" Password.1
samba-tool group add "$testgroup"
samba-tool group addmembers "$testgroup" "$testuser"
ldif=$(ldbsearch -H "$samldb" -d0 sAMAccountName="$testgroup" objectSid)
rid=$(sed -n 's/^objectSid: S-1-5-21-.*-.*-.*-//p' <<<"$ldif")

ldif=$(ldbsearch -H "$samldb" -d0 sAMAccountName="$testuser" dn)
user_dn=$(sed -n 's/^dn: //p' <<<"$ldif")

echo -e "\n### Change primaryGroupID to $rid"
ldbmodify -H "$samldb" -d0 <<%EOF
dn: $user_dn
changetype: modify
replace: primaryGroupID
primaryGroupID: $rid
%EOF

samba-tool dbcheck --attrs=member

echo -e "\n### And now change primaryGroupID back to 513"
ldbmodify -H "$samldb" -d0 <<%EOF
dn: $user_dn
changetype: modify
replace: primaryGroupID
primaryGroupID: 513
%EOF

samba-tool dbcheck --attrs=member

echo -e "\n### Try dbcheck --fix --yes"
samba-tool dbcheck --attrs=member --fix --yes

