SetACL is a set of rou­tines for man­ag­ing Win­dows per­mis­sions (ACLs) from the com­mand line, from scripts and from pro­grams. These rou­tines can be used from var­i­ous con­tainer or inter­face pro­grams. (http://setacl.sourceforge.net/).

If you start with setacl.exe the syn­tax might be a bit cryp­tic. But when you split up the syn­tax it gets quite eas­ier to read. I use some vari­ables the split it up:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
: –DACL — access con­trol / SACL — audit con­trol
set Users_ReadEx=-actn ace –ace “n:S-1–5-32–545;p:read_ex;s:y;“
set Users_Change=-actn ace –ace “n:S-1–5-32–545;p:change;s:y“
set Users_Full=-actn ace –ace “n:S-1–5-32–545;p:full;s:y“
set Admins_Full=-actn ace –ace “n:S-1–5-32–544;p:full;s:y“
set SYSTEM_Full=-actn ace –ace “n:S-1–5-18;p:full;s:y“
set _DOMAIN-Grp3_Full=-actn ace –ace “n:_DOMAIN_\_DOMAIN-Grp3_;p:full;s:n“
set _DOMAIN-Grp2_ReadEx=-actn ace –ace “n:_DOMAIN_\_DOMAIN-Grp2_;p:read_ex;s:n“
set Owner_DOMAIN-Grp3=-actn setowner –ownr “n:_DOMAIN_\_DOMAIN-Grp3_;s:n“

:this well-known sid is only avi­able on Win­dows Vista!
set TrustedInstaller_List= –actn ace –ace “n:S-1–5-80–956008885-3418522649–1831038044-1853292631–2271478464;p:full;s:y;i:sc“

set ClearInherits=-actn set­prot –op “dacl:p_nc;sacl:p_nc“
set ResetInherits=-actn rstchldrn –rst “dacl,sacl“
set ResetPermAndEnableInherits=-actn set­prot –op “dacl:np;sacl:np” –actn rstchldrn –rst “dacl,sacl“
set RecurseRegistry=-rec yes
set RecurseFolders=-rec cont
set RecurseFiles=-rec obj
set RecurseBoth=-rec cont_obj
set Copy­In­her­its= –actn set­prot –op “dacl:p_c;sacl:p_c“
set Exclude­Filter= –fltr .EXE –fltr .COM –fltr .DLL

:remove the “-silent” to get extra ver­bose mes­sages
set DebugSw=-silent

Relace the string _DOMAIN_ above with your domain (or Work­sta­tion) name, do the same for the _DOMAIN-GrpX.

Now some exam­ples using those pre-defined vari­ables:Add change per­mis­sion for a com­plete direc­tory tree:

1
2
3
4
MYDIR=”%ProgramFiles%FolderToProcess“
setacl.exe –on %MYDIR% –ot file %Copy­In­her­its% %Recurse­Files% %DebugSw%
setacl.exe –on %MYDIR% –ot file %Users_Change% %Recurse­Files% %DebugSw%
setacl.exe –on %MYDIR% –ot file %Admins_Full% %SYSTEM_Full% %TrustedInstaller_List% %Users_Change% %Clear­In­her­its% %DebugSw%

[ad#AdBrite-Text]

Remove change per­mis­sion for a sin­gle file:

1
2
MYFILE=”%ProgramFiles%FolderToProcessfile2.xml“
setacl.exe –on %MYFILE% –ot file %Admins_Full% %SYSTEM_Full% %Users_ReadEx% %Clear­In­her­its% %DebugSw%

Of Course you can add wild­card sup­port for this, an exam­ple how to process all xml files (hint: this should be on ONE line):

1
2
3
4
5
6
7
8
9
10
11
call :FILES_REMOVE-CHANGE-PERM “%ProgramFiles%FolderToProcess” “*.xml“
goto:EOF

:FILE_REMOVE-CHANGE-PERM
setacl.exe –on %1 –ot file %Admins_Full% %SYSTEM_Full% %Users_ReadEx% %Clear­In­her­its% %DebugSw%
goto:EOF

:FILES_REMOVE-CHANGE-PERM
for /f “tokens=*” %%i in (‘dir %1%2 /b /s’)
do call :FILE_REMOVE-CHANGE-PERM “%%i“
goto:EOF

Another exam­ple how a user group can remove its user per­mis­sion on a folder tree. Please note that this user group needs to have the win­dows right “restore files and direc­to­ries”, because they need to change the file owner (Local Secu­rity Set­tings, Local Poli­cies, User Rights Assignment).

1
2
3
4
5
6
7
MYDIR=”%ProgramFiles%FolderToProcess“

:change owner
setacl.exe –on %1 –ot file %Owner_DOMAIN-Grp3% %Recurse­Both%

:set the new per­mis­sion
setacl.exe –on %1 –ot file %Clear­All% %Clear­In­her­its% %Admins_Full% %SYSTEM_Full% %_DOMAIN-Grp3_Full% %_DOMAIN-Grp2_ReadEx%