#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");
}
答案 0 :(得分:1)
您不能使用passwd,但可以将 usermod 与-p标志一起使用。来自 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.