Why doesn't glibc provide standard library to modify /etc/passwd and /etc/shadow, i.e add a user, change passwd,
I came to that conclusion because I checked the source code of shadow
and failed to find such interface
Because it's not glibc's responsibility. glibc delegates to other services installed on the system for auth; if anything it would be those that would provide the ability to modify the userdb.
getpwnam(3)
and friends live in glibc. If setpwnam(3)
and such existed, why wouldn't they live there, too? Granted, these may well just be interfaces to other mechanisms, but from the programmer's perspective, they get these facilities from glibc. It's not a question of responsibility. It's a question of the reverse mapping the OP wants being far from trivial, so if someone did create a library that did this, its API would be a colossal mess.
Commented
Jan 8, 2013 at 18:39
You're starting with an assumption here, which is that /etc/{passwd,shadow,group}
is always the single point of truth (SPOT) for user information on Unix boxes. That hasn't been true since the mid-1980s, when Sun introduced NIS.
The reverse case APIs do exist: getpwnam(3)
and friends. If your site's SPOT for user data is an LDAP server, it's easy to see how you would reduce its rich output to show only those things that exist in /etc/{passwd,shadow}
.
But, you want an API that does the reverse. How would you implement setpwnam(3)
in a world where you don't know if the back-end data store is /etc/*
, or NIS, or NIS+, or LDAP, or ActiveDirectory, or...? You can filter krill from an ocean, but you can't get cubic meters of ocean from a bucketful of krill.
getpwnam
. Some backends would support it (e.g., files
), some would not (e.g., ldap
). I suspect the real answer is that a simplified interface isn't actually useful to much anyone.
/etc/*
, which is not at all what nsswitch is for. Its point is to do the "krill filtering" operation: pull requested info from a configurable series of places and present it in a common form. If there were an inverse nsswitch, its API would have to have all the functionality you find in libldap
, nis_*()
, etc. I've programmed using libldap
...it's far from simple. Now create an API that has to be able to take information suitable for popupating not just LDAP, but also NIS+, AD...<shudder>
Commented
Jan 8, 2013 at 19:24
getpwnam
isn't a perfect abstraction, either. E.g., take a look a PAM. It abstracts "change password". Does that work on every LDAP setup? No. But its still useful on the setups it does work on. But I guess if we want to continue this, we should use chat.
Simple answer if you think about it, there can be no C standard library to interact with them, simply because they are not C standard features. How would this work on windows?
passwd
/shadow
system considered standardized? Posix or otherwise?
Commented
Jan 8, 2013 at 10:12