Changing user password C API

I need to add a function to my UBUS module to allow change of password for a user. I look at shadow.h and also some linux example. But it doesn't make a sense to me, since I can open the database and read an entry but to update an entry I need to open the file and write to it. Here is how I understand the process:

setspent(); //open shado password database
struct spwd *sp = getspname("john");
sp->sp_pwdp = "mynewpassword";
// need to write shadow entry here
endspent();

It just doesn't make sense to me that I can read the existing encrypted password using the above functions, but to write the new password I need to open the /etc/shadow file directly and write a new entry.

Also since the password needs to be encrypted I should call crypt, but crypt requires a salt, where do I get the salt? If I use an old salt then the login function would clearly fail since my salt and the login salt would differ.

Am I missing something here?

Thank you!

Wingkit