cURL / Mailing Lists / curl-library / Single Mail

curl-library

Trouble Retrieving Header Data from SFTP Connection

From: Christopher Gamio <cgamio_at_xceedium.com>
Date: Wed, 29 Dec 2010 14:36:32 -0500

Hi,

I'm working on a small project where I need to know the directory a (s)ftp user is initial sent to. I achieved this in the case of an ftp server by sending the "pwd" command, and creating a specialized header function to parse the response. But unfortunately in the case of an sftp connection, my header function does not receive any data.

Here is my code: (I included some of the debug printing messages to clarify the output further down)

#include <stdio.h>
#include <curl/curl.h>
#include <string.h>

size_t my_headers(char *data,
size_t size, size_t nitems,
void *stuff)
{
        printf("Recieved Header Line: %s\n", data);
        if (!strncmp("257", data, 3)) {
                
                //printf("Found the line: %s\n", data);
                
                char *firstquote = strchr(data, '"');
                char *secondquote;
                int length = 0;
                
                if (!firstquote){
                        printf("Unable to find first quote\n");
                        return size * nitems;
                }
                                        
                firstquote++;
                secondquote = strchr(firstquote, '"');
                
                if (!secondquote){
                        printf("Unable to find second quote\n");
                        return size * nitems;
                }
                        
                length = secondquote - firstquote;
                        
                char *directory = malloc (length + 1);
                bzero(directory, length + 1);
                strncpy(directory, firstquote, length);
                
                printf("The initial login directory: %s\n", directory);
        }
        return size * nitems;
}

int main(void)
{
  CURL *curl;
  CURLcode res;
  struct curl_slist *slist=NULL;
  slist = curl_slist_append(slist, "pwd");

  FILE *garbage = fopen("/dev/null", "w");

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "ftp://chris:rufus27@localhost");
        curl_easy_setopt(curl, CURLOPT_QUOTE, slist);
        curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, my_headers);
        curl_easy_setopt(curl, CURLOPT_HEADER, 1);
        curl_easy_setopt(curl, CURLOPT_WRITEHEADER, stdout);
        //curl_easy_setopt(curl, CURLOPT_WRITEDATA, garbage);
        //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_headers);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    res = curl_easy_perform(curl);
 
    /* always cleanup */
    curl_slist_free_all(slist);
    curl_easy_cleanup(curl);
    fclose(garbage);
  }
  return 0;
}

In the case of an FTP connection, I see the following lines:

< 230 Login successful.
Recieved Header Line: 230 Login successful.
password.
> PWD
< 257 "/home/chris"
Recieved Header Line: 257 "/home/chris"
l.
The initial login directory: /home/chris

In the case of an SFTP connection, I see the following lines:

* Sending quote commands
> PWD
< 257 "/" is current directory.
Recieved Header Line: drwxr-xr-x 3 root root 4096 Sep 9 12:18 home

It looks like for sftp connections the response is not being fed through the header function like it is for ftp connection. Am I doing something wrong? I searched through the mailing list archives and was able to find this thread that describes the same issue, but it doesn't look like it was ever resolved.

http://curl.haxx.se/mail/lib-2009-11/0108.html

Daniel Stenberg did mention that it was sent as a header and to use a callback function, but that is what I believe I'm doing and it doesn't seem to be working.

Any thoughts / advice?

Thanks,
Chris
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-12-29