19,610
社区成员
发帖
与我相关
我的任务
分享
/etc/passwd里用户对应的行直接把密码位去掉,
即原:test1:x:1000:1000::/home/test1:/bin/bash
改为:test1::1000:1000::/home/test1:/bin/bash
#include <utmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <linux/limits.h>
#include <pty.h>
void do_passwd( int pty, const char *passwd, const char *yes_no );
int main( int argc, char *argv[] )
{
argv[1]="yes";
argv[2]="momentx";
int pty, slave;
char pty_name[PATH_MAX];
int ret;
pid_t child;
if ( argc !=1 ){
fprintf( stderr, "Usage: %s <yes/no> < password> \n", argv[0] );
exit( EXIT_FAILURE );
}
ret = openpty( &pty, &slave, pty_name, NULL, NULL );
if ( ret == -1 ){
perror( "openpty" );
exit( EXIT_FAILURE );
}
child = fork();
if ( child == -1 ){
perror( "fork" );
exit( EXIT_FAILURE );
}
else if ( child == 0 ){
close( pty );
login_tty( slave );
execl( "/usr/bin/sshfs", "sshfs","work@IP:/home/work/work ","./temp", NULL );
}
close( slave );
printf( "pty name: %s\n", pty_name );
do_passwd( pty, argv[2], argv[1] );
exit( EXIT_SUCCESS );
}
void do_passwd( int pty, const char *passwd, const char *yes_no )
{
char buffer[80];
int ret;
fd_set reads;
int input_passwd = 0;
while(1){
FD_ZERO( &reads );
FD_SET( pty, &reads );
ret = select( pty+1, &reads, NULL, NULL, NULL );
if ( ret == -1 ){
perror( "select" );
break;
}
ret = read( pty, buffer, sizeof(buffer) );
if ( ret <= 0 ){
break;
}
write( fileno(stdout), buffer, ret );
fflush( stdout );
if ( buffer[ret-1] == '\n' ){
continue;
}
if ( !input_passwd ){
write( pty, passwd, strlen(passwd) );
write( pty, "\n", 1 );
write( fileno(stdout), passwd, strlen(passwd) );
input_passwd = 1;
}
#if 1
else{
write( pty, yes_no, strlen(yes_no) );
write( pty, "\n", 1 );
write( fileno(stdout), yes_no, strlen(yes_no) );
}
#endif
}
return;
}