Calling ssh from C-Program with execvp - SSH

This is a discussion on Calling ssh from C-Program with execvp - SSH ; Hello, I try to call ssh from my Linux-Daemon (or for testing with a simple C-program) with fork() and execvp() from the forked client. This works great with all programs I tested, but not with ssh. The program runs with ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: Calling ssh from C-Program with execvp

  1. Calling ssh from C-Program with execvp

    Hello,
    I try to call ssh from my Linux-Daemon (or for testing with a simple
    C-program) with fork() and execvp() from the forked client. This works
    great with all programs I tested, but not with ssh.

    The program runs with root user (necessary). Using public key
    authentification. ssh works on shell:

    ssh -i /etc/mykeys/mykey root@destserver "ls -al"

    If I call the C-program I can see exactly the same call in the process list:

    ps auxwf
    root 23973 0.0 0.2 4856 2104 pts/3 S 11:26
    0:00 /usr/bin/ssh -i /etc/mykeys/mykey root@destserver ls -al

    My parent process gets an timeout. I found no error message in log files,
    but I found "Accepted publickey for root" in the server log (auth.log on
    debian sarge).

    The client ssh_config:

    BatchMode no
    UsePrivilegedPort yes
    PubkeyAuthentication yes
    StrictHostKeyChecking no

    The client ssh version: OpenSSH_4.2p1, OpenSSL 0.9.8a 11 Oct 2005

    The server ssh version: OpenSSH_3.8.1p1 Debian-8.sarge.6, OpenSSL 0.9.7e 25
    Oct 2004


    Do you know, why I can't use ssh over execvp()?

    Thank you in advance
    Manfred


  2. Re: Calling ssh from C-Program with execvp

    On Sun, 21 Jan 2007 11:50:40 +0100
    Manfred Rebentisch wrote:

    > Hello,
    > I try to call ssh from my Linux-Daemon (or for testing with a simple
    > C-program) with fork() and execvp() from the forked client. This works
    > great with all programs I tested, but not with ssh.


    Try setting -o 'BatchMode yes', either on the command line, or in your
    options. Also try doing an ssh -vvv from your program and view the
    output. It sounds like it is waiting for input.

    Doug

    --
    For UNIX, Linux and security articles
    visit http://SecurityBulletins.com/

  3. Re: Calling ssh from C-Program with execvp

    Hello Doug,

    Doug Spencer wrote:

    > On Sun, 21 Jan 2007 11:50:40 +0100
    > Manfred Rebentisch wrote:
    >
    >> Hello,
    >> I try to call ssh from my Linux-Daemon (or for testing with a simple
    >> C-program) with fork() and execvp() from the forked client. This works
    >> great with all programs I tested, but not with ssh.

    >
    > Try setting -o 'BatchMode yes', either on the command line, or in your
    > options. Also try doing an ssh -vvv from your program and view the
    > output. It sounds like it is waiting for input.
    >

    I forgot to tell: testet with BachMode yes too. Same result.

    Thank you for thinking about my problem.

    Manfred


  4. Re: Calling ssh from C-Program with execvp

    On Sun, 21 Jan 2007 17:33:37 +0100
    Manfred Rebentisch wrote:

    > Hello Doug,
    >
    > Doug Spencer wrote:


    > > Try setting -o 'BatchMode yes', either on the command line, or in your
    > > options. Also try doing an ssh -vvv from your program and view the
    > > output. It sounds like it is waiting for input.
    > >

    > I forgot to tell: testet with BachMode yes too. Same result.


    Do you get the same result if you try the command in a shell script
    rather than a C program?

    You should also try doing an strace and check the call and the result.
    Do an strace -o /tmp/output.strace -f ./a.out and check the result
    in /tmp/output.strace.

    Doug

    --
    For UNIX, Linux and security articles
    visit http://SecurityBulletins.com/

  5. Re: Calling ssh from C-Program with execvp


    This program works for me:

    ---------------------------------------------------------------
    #include
    #include
    #include

    main ()
    {
    int status;
    char *argv[] = {"ssh","remote_host","echo","foo",NULL};

    if (fork()) {
    wait(&status);
    } else {
    execvp("/usr/bin/ssh", argv);
    }
    }
    ---------------------------------------------------------------

    Since the log shows successful authentication, something is happening
    during the login. Try -n in case something waiting for input, as well as
    -v as Doug suggested. Also, you might try putting aside any shell init
    files on the remote side.

    --
    Richard Silverman
    res@qoxp.net


  6. Re: Calling ssh from C-Program with execvp

    Hello Richard,

    Richard E. Silverman wrote:

    >
    > This program works for me:
    >

    your simple source does work for me too, including public key.
    So the error is on my side, an executing object with a side effect...

    Thank you!

    Manfred


  7. Re: Calling ssh from C-Program with execvp

    On 2007-01-21 19:28:38 +0100, "Richard E. Silverman" said:

    > ---------------------------------------------------------------
    > #include
    > #include
    > #include
    >
    > main () {
    > int status;
    > char *argv[] = {"ssh","remote_host","echo","foo",NULL};
    >
    > if (fork()) {
    > wait(&status);
    > } else {
    > execvp("/usr/bin/ssh", argv);
    > }
    > }
    > ---------------------------------------------------------------


    Just a slight modification. The main function must return an int
    according to the standard, use:

    int main(int argc, char* argv[])

    or

    int main(void)


    And don't forget the return statement. Just my 2 cents to avoid
    undefined behaviors

    --
    Sensei

    Research (n.): a discovery already published by a chinese guy one month
    before you, copying a russian who did it in the 60s.


  8. Re: Calling ssh from C-Program with execvp

    Hallo,
    I found the reason for my problem. After I put the lines:

    if( open("/dev/null", O_RDONLY) != 0) {
    syslog(LOG_ERR, "failed to open /dev/null");
    exit(EXIT_FAILURE);
    }

    before the call of execvp() and after closing STDIN anything runs!

    Thank you to all!

    Manfred


+ Reply to Thread