1
#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");
}
4

1 回答 1

1

你不能使用 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.
于 2013-01-29T17:27:25.343 回答