| 1 | // Copyright (c) 2013, Suryandaru Triandana <[email protected]> |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Use of this source code is governed by a BSD-style license that can be |
| 5 | // found in the LICENSE file. |
| 6 | |
| 7 | // Package capability provides utilities for manipulating POSIX capabilities. |
| 8 | package capability |
| 9 | |
| 10 | type Capabilities interface { |
| 11 | // Get check whether a capability present in the given |
| 12 | // capabilities set. The 'which' value should be one of EFFECTIVE, |
| 13 | // PERMITTED, INHERITABLE, BOUNDING or AMBIENT. |
| 14 | Get(which CapType, what Cap) bool |
| 15 | |
| 16 | // Empty check whether all capability bits of the given capabilities |
| 17 | // set are zero. The 'which' value should be one of EFFECTIVE, |
| 18 | // PERMITTED, INHERITABLE, BOUNDING or AMBIENT. |
| 19 | Empty(which CapType) bool |
| 20 | |
| 21 | // Full check whether all capability bits of the given capabilities |
| 22 | // set are one. The 'which' value should be one of EFFECTIVE, |
| 23 | // PERMITTED, INHERITABLE, BOUNDING or AMBIENT. |
| 24 | Full(which CapType) bool |
| 25 | |
| 26 | // Set sets capabilities of the given capabilities sets. The |
| 27 | // 'which' value should be one or combination (OR'ed) of EFFECTIVE, |
| 28 | // PERMITTED, INHERITABLE, BOUNDING or AMBIENT. |
| 29 | Set(which CapType, caps ...Cap) |
| 30 | |
| 31 | // Unset unsets capabilities of the given capabilities sets. The |
| 32 | // 'which' value should be one or combination (OR'ed) of EFFECTIVE, |
| 33 | // PERMITTED, INHERITABLE, BOUNDING or AMBIENT. |
| 34 | Unset(which CapType, caps ...Cap) |
| 35 | |
| 36 | // Fill sets all bits of the given capabilities kind to one. The |
| 37 | // 'kind' value should be one or combination (OR'ed) of CAPS, |
| 38 | // BOUNDS or AMBS. |
| 39 | Fill(kind CapType) |
| 40 | |
| 41 | // Clear sets all bits of the given capabilities kind to zero. The |
| 42 | // 'kind' value should be one or combination (OR'ed) of CAPS, |
| 43 | // BOUNDS or AMBS. |
| 44 | Clear(kind CapType) |
| 45 | |
| 46 | // String return current capabilities state of the given capabilities |
| 47 | // set as string. The 'which' value should be one of EFFECTIVE, |
| 48 | // PERMITTED, INHERITABLE BOUNDING or AMBIENT |
| 49 | StringCap(which CapType) string |
| 50 | |
| 51 | // String return current capabilities state as string. |
| 52 | String() string |
| 53 | |
| 54 | // Load load actual capabilities value. This will overwrite all |
| 55 | // outstanding changes. |
| 56 | Load() error |
| 57 | |
| 58 | // Apply apply the capabilities settings, so all changes will take |
| 59 | // effect. |
| 60 | Apply(kind CapType) error |
| 61 | } |
| 62 | |
| 63 | // NewPid initializes a new Capabilities object for given pid when |
| 64 | // it is nonzero, or for the current process if pid is 0. |
| 65 | // |
| 66 | // Deprecated: Replace with NewPid2. For example, replace: |
| 67 | // |
| 68 | // c, err := NewPid(0) |
| 69 | // if err != nil { |
| 70 | // return err |
| 71 | // } |
| 72 | // |
| 73 | // with: |
| 74 | // |
| 75 | // c, err := NewPid2(0) |
| 76 | // if err != nil { |
| 77 | // return err |
| 78 | // } |
| 79 | // err = c.Load() |
| 80 | // if err != nil { |
| 81 | // return err |
| 82 | // } |
| 83 | func NewPid(pid int) (Capabilities, error) { |
| 84 | c, err := newPid(pid) |
| 85 | if err != nil { |
| 86 | return c, err |
| 87 | } |
| 88 | err = c.Load() |
| 89 | return c, err |
| 90 | } |
| 91 | |
| 92 | // NewPid2 initializes a new Capabilities object for given pid when |
| 93 | // it is nonzero, or for the current process if pid is 0. This |
| 94 | // does not load the process's current capabilities; to do that you |
| 95 | // must call Load explicitly. |
| 96 | func NewPid2(pid int) (Capabilities, error) { |
| 97 | return newPid(pid) |
| 98 | } |
| 99 | |
| 100 | // NewFile initializes a new Capabilities object for given file path. |
| 101 | // |
| 102 | // Deprecated: Replace with NewFile2. For example, replace: |
| 103 | // |
| 104 | // c, err := NewFile(path) |
| 105 | // if err != nil { |
| 106 | // return err |
| 107 | // } |
| 108 | // |
| 109 | // with: |
| 110 | // |
| 111 | // c, err := NewFile2(path) |
| 112 | // if err != nil { |
| 113 | // return err |
| 114 | // } |
| 115 | // err = c.Load() |
| 116 | // if err != nil { |
| 117 | // return err |
| 118 | // } |
| 119 | func NewFile(path string) (Capabilities, error) { |
| 120 | c, err := newFile(path) |
| 121 | if err != nil { |
| 122 | return c, err |
| 123 | } |
| 124 | err = c.Load() |
| 125 | return c, err |
| 126 | } |
| 127 | |
| 128 | // NewFile2 creates a new initialized Capabilities object for given |
| 129 | // file path. This does not load the process's current capabilities; |
| 130 | // to do that you must call Load explicitly. |
| 131 | func NewFile2(path string) (Capabilities, error) { |
| 132 | return newFile(path) |
| 133 | } |