Search code examples
linuxpasswordspasswd

What API do I call to set a user's password on linux?


I know about passwd(1) and crypt(3). What I'm looking for is a C API to call which will set the user's password in the passwd/shadow files, without having to programatically walk the files and overwrite the entry for the user in question. Application runs as root.

Does such an API exist?

EDIT: Guess I should specify, the password is being synced between different systems, so we cannot simply call system("passwd") and allow the user to enter whatever password they want when passwd prompts them. We need to know the password so we can programatically update the other systems with the same password.


Solution

  • As 8jean commented above, looks like /usr/sbin/chpasswd might be the easiest way. Otherwise I'd have gone with noha's comment of using functions like fgetpwent() -- or fgetspent() for dealing with the shadow file -- to walk the list of users and modify the record(s) I need to change. Thanks, everyone!