00001 /*************************************************************************** 00002 * _ _ ____ _ 00003 * Project ___| | | | _ \| | 00004 * / __| | | | |_) | | 00005 * | (__| |_| | _ <| |___ 00006 * \___|\___/|_| \_\_____| 00007 * 00008 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. 00009 * 00010 * This software is licensed as described in the file COPYING, which 00011 * you should have received as part of this distribution. The terms 00012 * are also available at https://curl.haxx.se/docs/copyright.html. 00013 * 00014 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 00015 * copies of the Software, and permit persons to whom the Software is 00016 * furnished to do so, under the terms of the COPYING file. 00017 * 00018 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 00019 * KIND, either express or implied. 00020 * 00021 ***************************************************************************/ 00022 /* <DESC> 00023 * Gets a file using an SFTP URL. 00024 * </DESC> 00025 */ 00026 00027 #include <stdio.h> 00028 00029 #include <curl/curl.h> 00030 00031 /* define this to switch off the use of ssh-agent in this program */ 00032 #undef DISABLE_SSH_AGENT 00033 00034 /* 00035 * This is an example showing how to get a single file from an SFTP server. 00036 * It delays the actual destination file creation until the first write 00037 * callback so that it won't create an empty file in case the remote file 00038 * doesn't exist or something else fails. 00039 */ 00040 00041 struct FtpFile { 00042 const char *filename; 00043 FILE *stream; 00044 }; 00045 00046 static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, 00047 void *stream) 00048 { 00049 struct FtpFile *out=(struct FtpFile *)stream; 00050 if(out && !out->stream) { 00051 /* open file for writing */ 00052 out->stream=fopen(out->filename, "wb"); 00053 if(!out->stream) 00054 return -1; /* failure, can't open file to write */ 00055 } 00056 return fwrite(buffer, size, nmemb, out->stream); 00057 } 00058 00059 00060 int main(void) 00061 { 00062 CURL *curl; 00063 CURLcode res; 00064 struct FtpFile ftpfile={ 00065 "yourfile.bin", /* name to store the file as if successful */ 00066 NULL 00067 }; 00068 00069 curl_global_init(CURL_GLOBAL_DEFAULT); 00070 00071 curl = curl_easy_init(); 00072 if(curl) { 00073 /* 00074 * You better replace the URL with one that works! 00075 */ 00076 curl_easy_setopt(curl, CURLOPT_URL, 00077 "sftp://user@server/home/user/file.txt"); 00078 /* Define our callback to get called when there's data to be written */ 00079 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); 00080 /* Set a pointer to our struct to pass to the callback */ 00081 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); 00082 00083 #ifndef DISABLE_SSH_AGENT 00084 /* We activate ssh agent. For this to work you need 00085 to have ssh-agent running (type set | grep SSH_AGENT to check) or 00086 pageant on Windows (there is an icon in systray if so) */ 00087 curl_easy_setopt(curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_AGENT); 00088 #endif 00089 00090 /* Switch on full protocol/debug output */ 00091 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 00092 00093 res = curl_easy_perform(curl); 00094 00095 /* always cleanup */ 00096 curl_easy_cleanup(curl); 00097 00098 if(CURLE_OK != res) { 00099 /* we failed */ 00100 fprintf(stderr, "curl told us %d\n", res); 00101 } 00102 } 00103 00104 if(ftpfile.stream) 00105 fclose(ftpfile.stream); /* close the local file */ 00106 00107 curl_global_cleanup(); 00108 00109 return 0; 00110 }