acls

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2026 License: BSD-3-Clause Imports: 10 Imported by: 2

README

codecov

Overview

This library provides the means to, without any cgo dependencies, adjust the regular linux filesystem ACLs (system.posix_acl_access) as well as the default ACLs (system.posix_acl_default). It therefore is a golang native implementation of the getfacl / setfacl commands.

Sample

The following code will try to load the actual ACL entries from the filePath referenced file.

If the filePath referenced object does not have an ACL attached, the regular file permissions are loadded as ACL Entries.

Subsequentyl a new entry for the linux group with GID 5558 and a permission of 7 (rwx) is added.

Then the ACL is applied as an Access ACL to the filePath provided filesystem object.

package main

import (
  log "github.com/sirupsen/logrus"
  "github.com/steiler/acls"
)

func main() {
    // Define the path to the file for which you want to get ACLs.
    filePath := "/tmp/foo"

    // init the ACL struct
    a := &acls.ACL{}
    // load (access) ACL entries from a given path object
    err := a.Load(filePath, acls.PosixACLAccess)
    if err != nil {
        log.Fatal(err)
    }
    // add a new entry referencing a group with GID 5558 granting permission rwx (7)
    err = a.AddEntry(acls.NewEntry(acls.TAG_ACL_GROUP, 5558, 7))
    if err != nil {
        log.Fatal(err)
    }
    // print a visual representation of the ACL
    fmt.Println(a.String())

    // Apply the ACL as an access ACL to the given filesystem path object.
    err = a.Apply(filePath, acls.PosixACLAccess)
    if err != nil {
        log.Fatal(err)
    }
}

The output of the fmt.Println(a.String()) looks like the following:

Version: 2
Entries:
Tag:   USER_OBJ ( 1), ID:       1000, Perm: rwx (7)
Tag:  GROUP_OBJ ( 4), ID:       1000, Perm: rwx (7)
Tag:      GROUP ( 8), ID:       5558, Perm: rwx (7)
Tag:       MASK (16), ID: 4294967295, Perm: rwx (7)
Tag:      OTHER (32), ID: 4294967295, Perm: r-x (5)

Features

- Add ACL Entry
- Delete ACL Entry
- Modify ACL Entry
- Print ACL Entry
- Read ACL entries from one file object, apply to another
- Adjust default and access ACL

Documentation

Index

Constants

View Source
const (
	// Undefined ACL type.
	TAG_ACL_UNDEFINED_FIELD = 0x0
	// Discretionary access rights for
	//processes whose effective user ID
	//matches the user ID of the file's owner.
	TAG_ACL_USER_OBJ = 0x1
	// Discretionary access rights for
	// processes whose effective user ID
	// matches the ACL entry qualifier.
	TAG_ACL_USER = 0x2
	// Discretionary access rights for
	// processes whose effective groupID or
	// any supplemental groups match the group
	// ID of the file's owner.
	TAG_ACL_GROUP_OBJ = 0x4
	// Discretionary access rights for
	// processes whose effective group ID or
	// any supplemental groups match the ACL
	// entry qualifier.
	TAG_ACL_GROUP = 0x8
	// The maximum discretionary access rights
	// that can be granted to a process in the
	// file group class. This is only valid
	// for POSIX.1e ACLs.
	TAG_ACL_MASK = 0x10
	// Discretionary access rights for
	// processes not covered by any other ACL
	// entry. This is only valid for POSIX.1e
	// ACLs.
	TAG_ACL_OTHER = 0x20
	// Same as ACL_OTHER.
	TAG_ACL_OTHER_OBJ = TAG_ACL_OTHER
	// Discretionary access rights for all
	// users. This is only valid for NFSv4
	// ACLs.
	TAG_ACL_EVERYONE = 0x40
)

Variables

This section is empty.

Functions

func PermUintToString

func PermUintToString(p uint16) string

PermUintToString takes an int representation of a permission and returns the string representation "rwx". not granted permissions appear as "-".

func Tag2String

func Tag2String(i Tag) string

Types

type ACL

type ACL struct {
	// contains filtered or unexported fields
}

ACL handles Posix ACL data

func (*ACL) AddEntry

func (a *ACL) AddEntry(e *ACLEntry) error

AddEntry adds the given entry to the ACL It will make sure that no entry with the same Tag and ID combination exists. If so, it will replace (not merge) the existing entry with the given.

func (*ACL) Apply

func (a *ACL) Apply(fsPath string, attr ACLAttr) error

Apply applies the ACL with its ACLEntries to as either access or default ACLs to the given filesstem path

func (*ACL) DeleteEntry

func (a *ACL) DeleteEntry(e *ACLEntry) *ACLEntry

DeleteEntry deletes the entry that has the same tag and id if it exists and returns the deleted entry

func (*ACL) EntryExists

func (a *ACL) EntryExists(e *ACLEntry) int

EntryExists checks if an entry with the given Tag and ID already exist if so it return -1 of not the position of the duplicate is returned as a positive int

func (*ACL) Equal

func (a *ACL) Equal(e *ACL) bool

Equal returns true if the given ACL equals the actual ACL

func (*ACL) GetEntries added in v0.1.4

func (a *ACL) GetEntries() []*ACLEntry

GetEntries returns all ACLEntries of the ACL

func (*ACL) GetEntry added in v0.1.3

func (a *ACL) GetEntry(e *ACLEntry) *ACLEntry

GetEntry returns the entry with the given Tag and ID if it exists, nil otherwise

func (*ACL) Load

func (a *ACL) Load(fsPath string, attr ACLAttr) error

Load loads the attr defined POSIX.ACL type (access or default) from the given filepath

func (*ACL) String

func (a *ACL) String() string

String returns a human readable for of the ACL

func (*ACL) ToByteSlice

func (a *ACL) ToByteSlice(result *bytes.Buffer)

ToByteSlice return the ACL in its byte slice representation read to be used by Setxattr(...)

type ACLAttr

type ACLAttr string
const (
	PosixACLAccess  ACLAttr = "system.posix_acl_access"
	PosixACLDefault ACLAttr = "system.posix_acl_default"
)

type ACLEntry

type ACLEntry struct {
	// contains filtered or unexported fields
}

ACLEntry the ACLEntry represents the single lines of permission.

  • tag references the type (group, user, etc.)
  • perm is the permission in its numerical format
  • id is the id of the group or user or whatever tag points to

func NewEntry

func NewEntry(tag Tag, id uint32, perm uint16) *ACLEntry

NewEntry returns a new ACLEntry

func (*ACLEntry) Equal

func (a *ACLEntry) Equal(e *ACLEntry) bool

Equal returns true if the given ACLEntry equals the actual ACLEntry

func (*ACLEntry) String

func (a *ACLEntry) String() string

String returns a string representation of the ACLEntry

func (*ACLEntry) ToByteSlice

func (a *ACLEntry) ToByteSlice(result *bytes.Buffer)

ToByteSlice returns the ACLEntry as a byte slice in little endian order, which is the representation required for the Setxattr(...) call

type Tag

type Tag uint16

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL