gpt4 book ai didi

c - 我如何摆脱这个c程序中的提示?

转载 作者:太空宇宙 更新时间:2023-11-04 03:58:09 24 4
gpt4 key购买 nike

#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。来自 ma​​n 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com