Question

I'm trying the get owner's name of a Unix file using C. The only method I have found is to use stat() and then getpwuid(stat.st_uid). But, it only returns the first user name with that uid, where users in the password file can have the same uid. Obviously, this is unacceptable and cannot be trusted.

References:

Owner is recievd from password file: http://pubs.opengroup.org/onlinepubs/007904875/functions/getpwuid.html

Uid is found in password file: http://www.cyberciti.biz/faq/understanding-etcpasswd-file-format/

Unix allow multiple users to have same uid: http://www.e-reading.org.ua/htmbook.php/orelly/networking/puis/ch04_01.htm

Is there an accurate way or a lower level way, some kind of look up table that would guarantee me accurate results?

Was it helpful?

Solution

Unix file permissions work by using the UID. Usernames can't own files. Only UIDs can. So if a file belongs to a specific UID, than all users with that UID own the file.

So it doesn't matter which username you get, since all users with that UID own the file.

OTHER TIPS

If you have multiple names associated with a single UID, you have multiple names associated with that UID. But as far as the kernel is concerned, all thoser names are aliases for the SAME user.

That is, the name is a purely human fiction and the only thing ever stored is the UID.

For example's sake, say we have two lines in /etc/passwd:

user:50:50:...
resu:50:50:...

This defines two mappings from username to UID. Internally, only the numeric UID is used to determine who "someone is", so if resu creates a file and sets the permissions to user:read/write, group:none, other:none, user can still read it, because as far as the kernel are concerned, they are the same user.

/the/example/file
  owner: 50
  group: 50
  permissions: rw-------

The only things stored in the file system, as far as ownership is concerned, is the numerical user ID and the numerical group ID. For mapping from numerical ID to human-friendly string, whatever is the first mapping found is the one used, because they're all identical.

In hort, don't give two users the same numerical ID, because if you do, you'e making life too hard on yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top