How to fool getpass()

Discussion in 'C Programming' started by marekdec@gmail.com, Aug 15, 2006.

  1. Guest

    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;
    , Aug 15, 2006
    #1
    1. Advertising

  2. wrote:
    > 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.


    [snip]

    We only discuss Standard C here which doesn't include pseudoterminals,
    getpass(), pipes, etc. Try comp.unix.programmer.

    Robert Gamble
    Robert Gamble, Aug 15, 2006
    #2
    1. Advertising

  3. jmcgill Guest

    wrote:
    > 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.


    This may be a job for expect. (or libexpect, to use in a C program).

    See comp.unix.questions FAQ 3.9
    jmcgill, Aug 15, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lucas Raab

    getpass

    Lucas Raab, Oct 12, 2003, in forum: Python
    Replies:
    1
    Views:
    474
    Jorge Godoy
    Oct 12, 2003
  2. gibber
    Replies:
    1
    Views:
    257
    David Goodger
    Oct 28, 2004
  3. John Salerno

    why does getpass() show the input?

    John Salerno, Aug 9, 2006, in forum: Python
    Replies:
    6
    Views:
    1,779
    John Salerno
    Aug 10, 2006
  4. Adrienne Boswell

    Love the Google April Fool

    Adrienne Boswell, Apr 1, 2009, in forum: HTML
    Replies:
    2
    Views:
    342
    Adrienne Boswell
    Apr 3, 2009
  5. Andrew

    getpass and IDEs

    Andrew, Feb 25, 2011, in forum: Python
    Replies:
    3
    Views:
    504
    Nobody
    Feb 26, 2011
Loading...

Share This Page