@@ -359,6 +359,156 @@ func TestBuildSecurityDescriptor(t *testing.T) {
359
359
}
360
360
}
361
361
362
+ func TestGetEntriesFromACL (t * testing.T ) {
363
+ // Create a temporary file to set ACLs on and test getting the ACEs from the ACL.
364
+ f , err := os .CreateTemp ("" , "foo.lish" )
365
+ defer os .Remove (f .Name ())
366
+
367
+ f .Close ()
368
+
369
+ // Well-known SID Strings:
370
+ // https://support.microsoft.com/en-us/help/243330/well-known-security-identifiers-in-windows-operating-systems
371
+ ownerSid , err := windows .StringToSid ("S-1-3-2" )
372
+ if err != nil {
373
+ t .Fatal (err )
374
+ }
375
+ groupSid , err := windows .StringToSid ("S-1-3-3" )
376
+ if err != nil {
377
+ t .Fatal (err )
378
+ }
379
+ worldSid , err := windows .StringToSid ("S-1-1-0" )
380
+ if err != nil {
381
+ t .Fatal (err )
382
+ }
383
+
384
+ ownerPermissions := windows .ACCESS_MASK (windows .GENERIC_ALL )
385
+ groupPermissions := windows .ACCESS_MASK (windows .GENERIC_READ | windows .GENERIC_EXECUTE )
386
+ worldPermissions := windows .ACCESS_MASK (windows .GENERIC_READ )
387
+
388
+ access := []windows.EXPLICIT_ACCESS {
389
+ {
390
+ AccessPermissions : ownerPermissions ,
391
+ AccessMode : windows .GRANT_ACCESS ,
392
+ Trustee : windows.TRUSTEE {
393
+ TrusteeForm : windows .TRUSTEE_IS_SID ,
394
+ TrusteeValue : windows .TrusteeValueFromSID (ownerSid ),
395
+ },
396
+ },
397
+ {
398
+ AccessPermissions : groupPermissions ,
399
+ AccessMode : windows .GRANT_ACCESS ,
400
+ Trustee : windows.TRUSTEE {
401
+ TrusteeForm : windows .TRUSTEE_IS_SID ,
402
+ TrusteeType : windows .TRUSTEE_IS_GROUP ,
403
+ TrusteeValue : windows .TrusteeValueFromSID (groupSid ),
404
+ },
405
+ },
406
+ {
407
+ AccessPermissions : worldPermissions ,
408
+ AccessMode : windows .GRANT_ACCESS ,
409
+ Trustee : windows.TRUSTEE {
410
+ TrusteeForm : windows .TRUSTEE_IS_SID ,
411
+ TrusteeType : windows .TRUSTEE_IS_GROUP ,
412
+ TrusteeValue : windows .TrusteeValueFromSID (worldSid ),
413
+ },
414
+ },
415
+ }
416
+
417
+ acl , err := windows .ACLFromEntries (access , nil )
418
+ if err != nil {
419
+ t .Fatal (err )
420
+ }
421
+
422
+ // Set new ACL.
423
+ err = windows .SetNamedSecurityInfo (
424
+ f .Name (),
425
+ windows .SE_FILE_OBJECT ,
426
+ windows .DACL_SECURITY_INFORMATION | windows .PROTECTED_DACL_SECURITY_INFORMATION ,
427
+ nil ,
428
+ nil ,
429
+ acl ,
430
+ nil ,
431
+ )
432
+ if err != nil {
433
+ t .Fatal (err )
434
+ }
435
+
436
+ descriptor , err := windows .GetNamedSecurityInfo (
437
+ f .Name (),
438
+ windows .SE_FILE_OBJECT ,
439
+ windows .DACL_SECURITY_INFORMATION | windows .PROTECTED_DACL_SECURITY_INFORMATION | windows .OWNER_SECURITY_INFORMATION | windows .GROUP_SECURITY_INFORMATION ,
440
+ )
441
+ if err != nil {
442
+ t .Fatal (err )
443
+ }
444
+
445
+ dacl , _ , err := descriptor .DACL ()
446
+ if err != nil {
447
+ t .Fatal (err )
448
+ }
449
+
450
+ owner , _ , err := descriptor .Owner ()
451
+ if err != nil {
452
+ t .Fatal (err )
453
+ }
454
+
455
+ group , _ , err := descriptor .Group ()
456
+ if err != nil {
457
+ t .Fatal (err )
458
+ }
459
+
460
+ entries , err := windows .GetEntriesFromACL (dacl )
461
+ if err != nil {
462
+ t .Fatal (err )
463
+ }
464
+
465
+ if len (entries ) != 3 {
466
+ t .Fatalf ("Expected newly set ACL to only have 3 entries." )
467
+ }
468
+
469
+ // https://docs.microsoft.com/en-us/windows/win32/fileio/file-access-rights-constants
470
+ // read = read data | read attributes
471
+ read := 0x0001 | 0x0080
472
+
473
+ // write = write data | append data | write attributes | write EA
474
+ write := 0x0002 | 0x0004 | 0x0100 | 0x0010
475
+
476
+ // execute = read data | file execute
477
+ execute := 0x0001 | 0x0020
478
+
479
+ // Check the set ACEs. We should have the equivalent of 754.
480
+ for _ , entry := range entries {
481
+ mask := int (entry .Mask )
482
+ actual := 0
483
+
484
+ if mask & read == read {
485
+ actual |= 4
486
+ }
487
+ if mask & write == write {
488
+ actual |= 2
489
+ }
490
+ if mask & execute == execute {
491
+ actual |= 1
492
+ }
493
+
494
+ if owner .Equals (& entry .Sid ) {
495
+ if actual != 7 {
496
+ t .Fatalf ("Expected owner to have FullAccess permissions." )
497
+ }
498
+ } else if group .Equals (& entry .Sid ) {
499
+ if actual != 5 {
500
+ t .Fatalf ("Expected group to have only Read and Execute permissions." )
501
+ }
502
+ } else if worldSid .Equals (& entry .Sid ) {
503
+ if actual != 4 {
504
+ t .Fatalf ("Expected the World to have only Read permissions." )
505
+ }
506
+ } else {
507
+ t .Fatalf ("Unexpected SID in ACEs: %s" , (& entry .Sid ).String ())
508
+ }
509
+ }
510
+ }
511
+
362
512
func TestGetDiskFreeSpaceEx (t * testing.T ) {
363
513
cwd , err := windows .UTF16PtrFromString ("." )
364
514
if err != nil {
0 commit comments