.net - How can I remove an access control entry from a printer in C#? -


i've been attempting use wmi remove access control entry (ace) discretionary access control list (dacl) associated local printer in windows 7. code gets security descriptor, , iterates through aces in dacl. unless trustee in ale named "everyone", ace added temporary list.

the temporary list turned array, , replaces dacl property of security descriptor. code calls setsecuritydescriptor() modified descriptor.

the return value 0x8007051b, 0x051b being win32 error code: 1307, security id may not assigned owner of object.

the error message bit confusing, in code (intentionally) changing owner of printer.

if change method include every ace in temporary list, call setsecuritydescriptor() completes successfully.

running application administrator makes no difference.

and, of course, code:

    private void changesecurity()         {             var query = string.format("select * win32_printer name \"%{0}%\"", printername);             var searcher = new managementobjectsearcher(query);             var searchresults = searcher.get();              foreach (managementobject printer in searchresults)             {                 var result = printer.invokemethod("getsecuritydescriptor", null, null);                 var descriptor = (managementbaseobject)result["descriptor"];                 var flags = (uint)descriptor["controlflags"];                 if ((flags & (uint)controlflags.discretionaryaclpresent) == (uint)controlflags.discretionaryaclpresent)                 {                     console.writeline("dacl present");                     var dacl = (managementbaseobject[])descriptor["dacl"];                     var newdacllist = new list<managementbaseobject>();                     foreach (var ace in dacl)                     {                         var trustee = (managementbaseobject)ace["trustee"];                         var acetype = (uint)ace["acetype"];                         if ((acetype & (uint) acetype.accessallowed) == (uint) acetype.accessallowed)                         {                             console.writeline("{0}\\{1}", trustee["domain"], trustee["name"]);                             console.writeline("access mask {0}", ace["accessmask"]);                             if (trustee["name"].tostring() == "everyone" && trustee["domain"] == null)                             {                                 console.writeline("remove access");                             }                             else                             {                                 newdacllist.add(ace);                             }                         }                     }                      descriptor.setpropertyvalue("dacl", newdacllist.toarray());                      var inparams = printer.getmethodparameters("setsecuritydescriptor");                     inparams["descriptor"] = descriptor;                     result = printer.invokemethod("setsecuritydescriptor", inparams, null);                     console.writeline("result code: {0}", result["returnvalue"]);                 }             } 


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -