Linux File Permissions(In-Depth) - Numeric to String Notation, and Vice Versa; Additional File-Permissions

Linux file permissions(in-depth) - numeric to string notation, and vice versa; additional file-permissions

After in-depth searching on the Web, I found this link about Understanding Linux File Permissions which describes it in detail :

s - This indicated the setuid/setgid permissions. This is not set
displayed in the special permission part of the permissions display,
but is represented as a s in the read portion of the owner or group
permissions.

t - This indicates the sticky bit permissions. This is not set
displayed in the special permission part of the permissions display,
but is represented as a t in the executable portion of the all users
permissions

Setuid/Setgid Special Permissions

---The setuid/setguid permissions are used to tell the system to run an executable as the owner with the owner\'s permissions.

---Be careful using setuid/setgid bits in permissions. If you incorrectly assign permissions to a file owned by root with the setuid/setgid bit set, then you can open your system to intrusion.

---You can only assign the setuid/setgid bit by explicitly defining permissions. The character for the setuid/setguid bit is s.

Sticky Bit Special Permissions

---The sticky bit can be very useful in shared environment because when it has been assigned to the permissions on a directory it sets it so only file owner can rename or delete the said file.

---You can only assign the sticky bit by explicitly defining permissions. The character for the sticky bit is t.

Logic behind conversion from numeric(1/2/4421) to symbolic notation(rwx/s/t) :


EDIT :

The first number represents the Owner permission; the second represents the Group permissions; and the last number represents the permissions for all other users. The numbers are a binary representation of the rwx string.

r = 4
w = 2
x = 1

---> The sticky bit can be set using the chmod command and can be set using its octal mode 1000 or by its symbol t (s is already used by the setuid bit). For example, to add the bit on the directory /usr/local/tmp, one could type chmod 1777 /usr/local/tmp.

---> The setuid and setgid bits are normally set with the command chmod by setting the high-order octal digit to 4 for setuid or 2 for setgid. chmod 6711 file will set both the setuid and setgid bits (4+2=6), making the file read/write/executable for the owner (7), and executable by the group (first 1) and others (second 1).

NOTE :

s  ---  The setuid bit when found in the user triad; the setgid bit when found in the group 
triad; it is not found in the others triad; it also implies that x is set.
S --- Same as s, but x is not set; rare on regular files, and useless on folders.
t --- The sticky bit; it can only be found in the others triad; it also implies that x is
set.
T --- Same as t, but x is not set; rare on regular files, and useless on folders.

s, S, t and T values are always appended before the user-group-others
permission notation. So, first letter of the notation represents s, S, t or T values appended to the string. The next 3 letters are the usual permission.

Your questions/examples related to file-permissions :

1. -r-sr---wt   = 5543, first 5(s set for user = 4 + t set for others = 1),
second 5(r=4,s=1), third 4(r = 4), and last, fourth 3(w=2, t = 1).

2. -r-S-wsrw- = 6436, first 6(S set for user = 4 + s set for group = 2),
second 5(r=4, x=0, since S don't account for x),
third 3(w = 2, s results in x = 1), and last, fourth 6(r=4,w=2).

chmod in java according to mode and not Strings

You can do it with a simple conversion like this:

    public class Chmod {

public Set<PosixFilePermission> fromInt(int perms) {
final char[] ds = Integer.toString(perms).toCharArray();
final char[] ss = {'-','-','-','-','-','-','-','-','-'};
for (int i = ds.length-1; i >= 0; i--) {
int n = ds[i] - '0';
if (i == ds.length-1) {
if ((n & 1) != 0) ss[8] = 'x';
if ((n & 2) != 0) ss[7] = 'w';
if ((n & 4) != 0) ss[6] = 'r';
}
else if (i == ds.length-2) {
if ((n & 1) != 0) ss[5] = 'x';
if ((n & 2) != 0) ss[4] = 'w';
if ((n & 4) != 0) ss[3] = 'r';
}
else if (i == ds.length-3) {
if ((n & 1) != 0) ss[2] = 'x';
if ((n & 2) != 0) ss[1] = 'w';
if ((n & 4) != 0) ss[0] = 'r';
}
}
String sperms = new String(ss);
System.out.printf("%d -> %s\n", perms, sperms);
return PosixFilePermissions.fromString(sperms);
}

public static void main(String[] args) throws Exception {
Chmod test = new Chmod();
test.fromInt(444);
test.fromInt(1);
test.fromInt(777);
test.fromInt(666);
test.fromInt(604);
test.fromInt(0);
}

}

Writing python code to convert linux read, write and execute octal format to the string

  • An easier way.

    def octal_to_string(octal):
    permission = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"]
    result = ""
    # Iterate over each of the digits in octal
    for ___ in [int(n) for n in str(octal)]:
    result += permission[___]
    return result

    print(octal_to_string(755))
    print(octal_to_string(644))
    print(octal_to_string(750))
    print(octal_to_string(600))

Output1

  • According to your logic.

    def octal_to_string(octal):
    result = ""
    value_letters = [(4,"r"),(2,"w"),(1,"x")]
    # Iterate over each of the digits in octal
    for ___ in [int(n) for n in str(octal)]:
    # Check for each of the permissions values
    for value, letter in value_letters:
    if ___ >= value:
    result += letter
    ___ -= value
    else:
    result += "-"
    return result

    print(octal_to_string(755))
    print(octal_to_string(644))
    print(octal_to_string(750))
    print(octal_to_string(600))

Output2



Related Topics



Leave a reply



Submit