#include <stdio.h>
int main() {
FILE *f ;
f = popen("passwd mukesh","w");
fprintf(f,"c\n");
fprintf(f,"c\n");
pclose(f);
}
有没有办法摆脱提示,用程序本身传递密码
更具体地说,作为
a.out
可执行文件的参数。编辑:或者类似的事情也不起作用:
#include <unistd.h> /* crypt(), etc. */
#include <pwd.h> /* getpass(), getpwnam(). */
#include <string.h> /* strcmp(), etc. */
void
main()
{
/* buffers for reading in the user name and the password. */
char user[21];
char* password;
/* storing the encrypted password, and the salt. */
char* encrypted_password;
char salt[2];
/* user's "/etc/passwd" entry. */
struct passwd* user_info;
/* prompt the user for a user name. */
printf("User name: ");
fflush(stdout); /* flush the prompt to make sure the user sees it. */
fgets(user, 20, stdin);
/* fgets() stores also the new-line that the user typed in. so we */
/* need to locate the new-line character, and truncate it. */
if (strchr(user, '\n'))
(*(strchr(user, '\n'))) = '\0';
/* prompt the user for their password. the getpass() function */
/* prints the given prompt, turns off echo (so the password */
/* typed won't be seen on screen), and returns the string that */
/* the user types. */
password = getpass("Password: ");
/* find the user's encrypted password, as stored in "/etc/passwd". */
user_info = getpwnam(user);
if (!user_info) {
printf("login incorrect.\n");
exit(1);
}
/* take the salt as stored in the password field of the user. */
strncpy(salt, user_info->pw_passwd, 2);
/* encrypt the given password using the found "salt". */
encrypted_password = crypt(password, salt);
/* compare the results of crypt, with the user's stored password field. */
if (strcmp(user_info->pw_passwd, encrypted_password) != 0) {
printf("login incorrect.\n");
exit(1);
}
/* authentication succeeded... */
printf("login successful.\n");
}
最佳答案
不能使用passwd,但可以使用带有-p标志的usermod。从man usermod,
-p, --password PASSWORD
The encrypted password, as returned by crypt(3).
Note: This option is not recommended because the password
(or encrypted password) will be visible by users listing the processes.
关于c - 我如何摆脱这个C程序中的提示?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14588523/