Where mode & ~mask is mask inverted and AND'd with the file creation mode
Why it's that way I do not know.
mode specifies the permissions to use in case a new file is creâ€
ated. This argument must be supplied when O_CREAT is specified
in flags; if O_CREAT is not specified, then mode is ignored.
The effective permissions are modified by the process’s umask in
the usual way: The permissions of the created file are
(mode & ~umask). Note that this mode only applies to future
accesses of the newly created file; the open() call that creates
a read-only file may well return a read/write file descriptor.
It's for future compatibility. When more mode bits are added (sticky bits, suid bits etc..) the mask is still valid - 007 still works - leaving the new bits as the creating process chooses.
Plain ANDing would mean that anything that set the umask would have to be updated to allow the new bits to be set. It excludes anything other than those bits specified.
Have to admire the forethought of our forefathers.
max dir permissions by default = 777, max file perms = 666.
Your umask is then subtracted[1] from that to determine default perms for dir/file.
If your umask = 002 then:
- "mkdir newdir" will create a directory with perms 775 (rwxrwr-x).
- "touch mywilly" will create a file of perms 664 (rw-rw-r--). And a sticky mess, possibly.
Coupla last points:
1. The umask acts as a "filter" - setting it high means objects get little default perms. Setting it low allows greater permissions, which you may need to tighten up later.
2. your umask is user-centric, so affects *you*, not files or dirs - you don't put a umask against a file and it affects users attempting to change permissions.
3. your umask determines new objects created, so setting it higher for tighter security doesn't magically run "chmod" against your existent files and dirs. Yes, that may sound obvious, but I've known people who believe it to be retrospective.
4. umasks are session-specific - setting it higher then logging out will make it revert to its original setting. Generally, people add their preferred umask to their profile (.bash_profile, etc).
5. An odd umask will always be "rounded down" to the nearest even number, so there is no way a newly-created file will gain execute permission by default. That's part of Unix security from the 70s, and it's still a valid idea. Annoys the fuck out of developers that moan they need to run "chmod u+x" on their scripts first. Annoys people even further when they try "chmod 777" on my server, too.
Hope that helps.
For further headaches, read up about ACLs (chacl) and immutable bits (chattr), also. Yah, they'll fry your noodle. Then read about PolicyKit. Oh, shitting christ on stilts.
[1] no, it's not - read defdoor's correction below!
Last edited by Dungeon-Dave on Tue Jun 16, 2009 11:05 pm 0, edited 2 times in total.
Dungeon-Dave wrote:
Your umask is then subtracted from that to determine default perms for dir/file.
The umask is not subtracted from anything - if a file was being created with a perms of 0444 and my umask was 666 just what would the outcome be ?. Your umask is inverted - i.e. 007 becomes 0777777770 if we are on a system with that wide a permission mode, and then ANDed with the creation mode requested by the call to fopen or creat etc..
Likewise it isn't "rounded down". For file creation it is ANDed with 0666 to prevent files from being executable by default.
Sorry to be pedantic but users have to understand that permissions are bitwise flags - not integers - so likewise the operations upon them are bitwise operations.
Dungeon-Dave wrote:
Your umask is then subtracted from that to determine default perms for dir/file.
The umask is not subtracted from anything....
Sorry to be pedantic but users have to understand that permissions are bitwise flags - not integers - so likewise the operations upon them are bitwise operations.
Sorry, you're right on that front - it's not a true "subtraction" since there isn't "carrying". Bitwise AND is correct; I tend to explain it as a subtraction to classes since many of them aren't familiar with the concept of binary (these are the ones that ask "is the internet down?" and "I've crashed the system again").
I guess I got into the wrong mode before reading properly!
Dungeon-Dave wrote:
Your umask is then subtracted from that to determine default perms for dir/file.
The umask is not subtracted from anything....
Sorry to be pedantic but users have to understand that permissions are bitwise flags - not integers - so likewise the operations upon them are bitwise operations.
Sorry, you're right on that front - it's not a true "subtraction" since there isn't "carrying". Bitwise AND is correct; I tend to explain it as a subtraction to classes since many of them aren't familiar with the concept of binary (these are the ones that ask "is the internet down?" and "I've crashed the system again").
I guess I got into the wrong mode before reading properly!
Okay, bitwise then - but I'll probably just use subtraction if it comes up as the RHCE book that I'm using (the Michael Jang one) says it's subtraction. I'd love to get hold of the RHCT book instead, as this is the certification I'm going for, but I'll be damned if I can find it anywhere....Amazon only have it second-hand for £100
RageMatrix wrote:
Okay, bitwise then - but I'll probably just use subtraction if it comes up as the RHCE book that I'm using (the Michael Jang one) says it's subtraction
RageMatrix wrote:
Okay, bitwise then - but I'll probably just use subtraction if it comes up as the RHCE book that I'm using (the Michael Jang one) says it's subtraction
I'd throw that away then.
<shrug> it's Certification Press...but I'll keep the bitwise thing in mind and mention it if it comes up.
Just to highlight: I use "subtraction" to explain how it works to a mostly non-technical audience.
I say "subtraction" since - as defsdoor mentioned above - it's not a true subtraction: I have to explain that there's no concept of "borrowing", and when it comes to file perms there will never be an odd result (meaning executable perms by default). I have seen it mentioned in some other books, but it's not strictly correct, just easier to understand provided you observe the exceptions.
I've got our shiny new Samba server (well, not really shiny or new - I built it out of bits from our storeroom and threw in a 500GB drive). Anyway....I need to backup the Samba share to an external drive once a month. This bit is easy. The hard bit is that the boss wants incremental daily backups of files that have changed in this directory tree to be copied to a daily sub-directory named with todays date on the external drive.
Can I use rdiff-backup for this? Or something else? Or some sort of scripting? The directory struture is rather complex.
you can drop in a larger sata drive (or plug in a usb external drive) and flash it with linux, so you can run twonky/slimserver/mediatomb/torrent client/etc and free up your pc for more fun things
---
psn: spazhammer75 http://nottsdj.co.uk - Nottinghamshire Weddings and Events DJ