0% found this document useful (0 votes)
16 views6 pages

03-chmod-chown

Test

Uploaded by

JohnKevinStanley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

03-chmod-chown

Test

Uploaded by

JohnKevinStanley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Advanced Programming in

the UNIX Environment


Week 03, Segment 4:
chmod(2) and chown(2)

Department of Computer Science


Stevens Institute of Technology

Jan Schaumann
[email protected]
https://stevens.netmeister.org/631/
CS631 - Advanced Programming in the UNIX Environment

chmod(2), lchmod(2), fchmod(2)


#include <sys/stat.h>
#include <fcntl.h>

int chmod(const char *path, mode_t mode);


int lchmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);
int fchmodat(int fd, const char *path, mode_t mode, int ag);
Returns: 0 if OK, -1 on error

Changes the permission bits on the le. Must be either euid 0 or euid == st_uid.
mode can be any of the bits from our discussion of st_mode as well as:
• S_ISUID – setuid • S_IRWXU – user read, write and execute
• S_ISGID – setgid • S_IRWXG – group read, write and execute
• S_ISVTX – sticky bit (aka “saved text”) • S_IRWXO – other read, write and execute
2
Jan Schaumann 2022-01-18
fi
fl
CS631 - Advanced Programming in the UNIX Environment

chmod(2), lchmod(2), fchmod(2)

3
Jan Schaumann 2022-01-18
CS631 - Advanced Programming in the UNIX Environment

chown(2), lchown(2), fchown(2)


#include <unistd.h>
#include <fcntl.h>

int chown(const char *path, uid_t owner, gid_t group);


int lchown(const char *path, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int fchownat(int fd, const char *path, uid_t owner, gid_t group, int ag);
Returns: 0 if OK, -1 on error

Changes st_uid and st_gid for a le. Generally requires euid 0. (Some SVR4’s let users chown
their les to anybody. POSIX allows either, depending on _POSIX_CHOWN_RESTRICTED.)
owner or group can be -1 to indicate that it should remain the same.
Non-superusers can change the st gid eld if both:
• euid == st_uid; and
• owner == st_uid and group == egid (or one of the supplementary group IDs)
4
Jan Schaumann 2022-01-18
fi
fi
fi
fl
CS631 - Advanced Programming in the UNIX Environment

chown(2), lchown(2), fchown(2)

5
Jan Schaumann 2022-01-18
CS631 - Advanced Programming in the UNIX Environment

chmod(2) and chown(2)

chmod(2) and chown(2) consistently follow the semantics of the other calls we've seen.

Only root and the owner of a le can change its permissions.

Only root can change the owner of a le, but the owner may change the group
ownership of a le.

Changing le permissions and ownerships has signi cant security implications.

Coming up next: default le ownership and permissions for newly created les.

6
Jan Schaumann 2022-01-18
fi
fi
fi
fi
fi
fi
fi

You might also like