I want to override getpass() mechanisms of getting password from the
keyboard. I opened a new pseudoterminal: /dev/ptmx and the other side
of it and tried to redirect it to standard ftp client (what i want to
do is to make my program enter the password automatically) I looked
into ftp sources and know that ftp uses getpass() function to acquire
password. Although i'm using pseudoterminal (not pipes) it seems to
work exactly the same as with pipes. Ftp ignores my program and waits
for user to enter password from the keyboard, dunno what to do.
thx for any help.
Marek
here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#include <fcntl.h>
#include <pty.h>
#include <utmp.h>
#define CS_NOT_CONNECTED 0
#define CS_CONNECTED 1
#define RC_ERROR_PROCESS_SPAWN 35
#define RC_GEN_FAIL 1
#define RC_ERROR_OPEN_FILE 2
#define RC_GEN_SUCCESS 0
#define D_BUFSIZE 500
#define D_PATH_MAX 256
int main(int argc, char **argv, char **envp)
{
int forkRes1 = 0;
int forkRes2 = 0;
int telnetInPipe[2],
telnetOutPipe[2];
int ftpMasterFd,
ftpSlaveFd;
int readData = -1;
char buffer[D_BUFSIZE + 1];
char cmdToRun[D_BUFSIZE];
int connectionStatus = CS_NOT_CONNECTED;
int i = 0;
char *tmp;
char name[D_PATH_MAX];
struct termios stermios;
memset(buffer, '\0', sizeof(buffer));
pipe(telnetInPipe);
pipe(telnetOutPipe);
ftpMasterFd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
grantpt(ftpMasterFd);
unlockpt(ftpMasterFd);
tmp = (char *)ptsname(ftpMasterFd);
ftpSlaveFd = open(tmp, O_RDWR | O_NOCTTY);
/* openpty(&ftpMasterFd, &ftpSlaveFd, name, NULL, NULL); */
printf("master: %d, slave: %d, name: %s\n",
ftpMasterFd,
ftpSlaveFd,
tmp);
if (argc<2)
{
printf("Too few parameters.\nUsage:\n\n");
return 0;
}
forkRes1 = fork();
if (0 == forkRes1)
{
/* child1 part */
close(ftpMasterFd);
dup2(ftpSlaveFd, 0);
dup2(ftpSlaveFd, 1);
dup2(ftpSlaveFd, 2);
/*tcgetattr(0, &stermios);
stermios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
stermios.c_lflag |= ICANON;
stermios.c_lflag &= ~(ONLCR);
tcsetattr(0, TCSANOW, &stermios); */
execlp("ftp","ftp","-v", "-n",argv[1], 0);
return RC_ERROR_PROCESS_SPAWN;
}
else
{
/* parent part */
close(ftpSlaveFd);
while (1)
{
do
{
memset(buffer, 0, sizeof(buffer));
readData = read(ftpMasterFd, buffer,D_BUFSIZE);
buffer[readData]='\0';
if (connectionStatus == 3)
{
write(ftpMasterFd, "password\n", 10);
connectionStatus++;
}
printf("%s",
buffer);
processFtpBuffer(buffer,
ftpMasterFd,
&connectionStatus);
fflush(stdout);
}
while (readData==D_BUFSIZE);
fflush(stdout);
}
}
return 0;
}
int processFtpBuffer(char *buffer,
int ftpMasterFd,
int *connectionStatus)
{
int status = RC_GEN_SUCCESS;
char *relativePtr = 0;
int i;
char cmdToRun[300];
FILE *tty;
memset(cmdToRun, 0, sizeof(cmdToRun));
if (0 != strstr(buffer, "220"))
{
write(ftpMasterFd, "user root\n", 10);
}
else if (0 != strstr(buffer, "331"))
{
*connectionStatus = 3;
}
return status;