I'm trying to write a C program to change a user password using Linux system calls. I have tryed to use the /etc/passwd
and /etc/shadow
files but I'm having problems because the password is encrypted, can you help me to solve this?
void main (int argc, char **argv) {
uid_t uid;
struct passwd *pw;
uid = getuid();
if (argc > 1)
pw = getpwnam(argv[1]);
else
pw = getpwuid(uid);
//system("passwd");
//printf("%i",execl("/usr/bin/passwd","passwd",pw->pw_name)); //here I tried to use execl but it returns error
}
I used system("passwd") but I don't think my teacher will accept that. On the rest of it I was just trying to understand the getpw... stuff, it's not important.
The first thing I learned when I started writing software on Linux after coming from a background in Windows and OS X development is that command line utilities are to be embraced, not shunned. Even for things that have the option of using a C api or a command line utility, it's often smarter and more reliable to fork
and exec
(do NOT popen
unless you don't care to check whether or not the return code is 0!) than to use the C api.
In fact, calling a command line utility is no different than calling a C api unless you need to eke out every last bit of performance in your application. It's practically an API where the function name is the utility name and the parameters are the command line arguments.
So in answer to your question: try exec
passwd
and it'll take care of all your issues.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.