1 #define VERBOSE 1
   2 
   3 #ifdef WIN32
   4 # include <winsock2.h>
   5 #define LIBSSH2_WIN32
   6 #define LIBSSH2_API
   7 #else
   8 #include <unistd.h>
   9 # include <sys/socket.h>
  10 # include <netinet/in.h>
  11 #include <arpa/inet.h>
  12 #endif
  13 #include <libssh2.h>
  14 #include <libssh2_sftp.h>
  15 
  16 #include <sys/types.h>
  17 #include <fcntl.h>
  18 #include <errno.h>
  19 #include <stdio.h>
  20 #include <ctype.h>
  21 
  22 static LIBSSH2_SESSION *session=NULL;
  23 static LIBSSH2_SFTP *sftp_session=NULL;
  24 static int sock=0;
  25 
  26 static void extract_location(const char *location, char *server, char *user, char*passwd, char *file)
  27 {
  28   int i,j;
  29   int lg_location;
  30   lg_location=strlen(location);
  31   for (i=0;i<lg_location;i++)
  32   {
  33     if (location[i]=='\n')
  34       break;
  35     server[i]=location[i];
  36   }
  37   server[i]='\0';
  38   i++;
  39 
  40   for (j=0;i<lg_location;i++,j++)
  41   {
  42     if (location[i]=='\n')
  43       break;
  44     user[j]=location[i];
  45   }
  46   user[j]='\0';
  47   i++;
  48 
  49   for (j=0;i<lg_location;i++,j++)
  50   {
  51     if (location[i]=='\n')
  52       break;
  53     passwd[j]=location[i];
  54   }
  55   passwd[j]='\0';
  56   i++;
  57 
  58   for (j=0;i<lg_location;i++,j++)
  59   {
  60     if (location[i]=='\0')
  61       break;
  62     file[j]=location[i];
  63   }
  64   file[j]='\0';
  65 }
  66 
  67 static void close_sftp_session()
  68 {
  69   if (sftp_session)
  70     libssh2_sftp_shutdown(sftp_session);
  71   sftp_session=NULL;
  72 
  73   if (session)
  74   {
  75     libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
  76     libssh2_session_free(session);
  77   }
  78   session=NULL;
  79 
  80   if (sock)
  81   {
  82 #ifdef WIN32
  83     Sleep(1000);
  84     closesocket(sock);
  85 #else
  86     sleep(1);
  87     close(sock);
  88 #endif
  89   }
  90   sock=0;
  91 }
  92 
  93 static  int open_sftp_session(const char *server,const char *user,const char *passwd)
  94 {
  95   struct sockaddr_in sin;
  96   int rc;
  97 
  98   sock = socket(AF_INET, SOCK_STREAM, 0);
  99 
 100   sin.sin_family = AF_INET;
 101   sin.sin_port = htons(22);
 102   sin.sin_addr.s_addr = inet_addr(server);
 103   if (connect(sock, (struct sockaddr*)(&sin),
 104         sizeof(struct sockaddr_in)) != 0)
 105   {
 106     close_sftp_session();
 107     return 0;
 108   }
 109 
 110   /* Create a session instance
 111   */

 112   session = libssh2_session_init();
 113   if(!session)
 114   {
 115     close_sftp_session();
 116     return 0;
 117   }
 118 
 119   /* ... start it up. This will trade welcome banners, exchange keys,
 120    * and setup crypto, compression, and MAC layers
 121    */

 122   rc = libssh2_session_startup(session, sock);
 123   if(rc)
 124   {
 125     close_sftp_session();
 126     return 0;
 127   }
 128 
 129   libssh2_session_set_blocking(session, 1);
 130 
 131   if (libssh2_userauth_password(session, user, passwd))
 132   {
 133     close_sftp_session();
 134     return 0;
 135   }
 136 
 137   sftp_session = libssh2_sftp_init(session);
 138 
 139   if (!sftp_session)
 140   {
 141     close_sftp_session();
 142     return 0;
 143   }
 144   return 1;
 145 }
 146 
 147 static int csfputs(const char *s, void *stream)
 148 {
 149 #if VERBOSE
 150   fprintf(stderr,"csfputs:%s\n",s);
 151 #endif
 152   return libssh2_sftp_write((LIBSSH2_SFTP_HANDLE*)stream,s,strlen(s));
 153 }
 154 
 155 static void *csfopenappend(const char *location)
 156 {
 157   LIBSSH2_SFTP_HANDLE *handle;
 158   char server[1024];
 159   char user[1024];
 160   char passwd[1024];
 161   char file[1024];
 162   LIBSSH2_SFTP_ATTRIBUTES attrs;
 163 
 164   extract_location(location,server,user,passwd,file);
 165 #if VERBOSE
 166   fprintf(stderr,"csfopenappend %s:%s:%s\n",server,user,file);
 167 #endif
 168   if (open_sftp_session(server,user,passwd))
 169   {
 170     handle = libssh2_sftp_open(sftp_session, file, LIBSSH2_FXF_CREAT|LIBSSH2_FXF_WRITE,
 171         LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
 172         LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH );
 173   }
 174   else
 175     return NULL;
 176 
 177   if (handle)
 178   {
 179     if (libssh2_sftp_fstat(handle,&attrs)==0)
 180     { /* Go to the end of the file */
 181       libssh2_sftp_seek(handle,attrs.filesize);
 182     }
 183   }
 184   return handle;
 185 }
 186 
 187 static void *csfopenread(const char *location)
 188 {
 189   char server[1024];
 190   char user[1024];
 191   char passwd[1024];
 192   char file[1024];
 193 
 194   extract_location(location,server,user,passwd,file);
 195 #if VERBOSE
 196   fprintf(stderr,"csfopenread %s:%s:%s\n",server,user,file);
 197 #endif
 198   if (open_sftp_session(server,user,passwd))
 199     return (void*)  libssh2_sftp_open(sftp_session, file, LIBSSH2_FXF_READ, 0);
 200   else
 201     return NULL;
 202 }
 203 
 204 static void *csfopenwrite(const char *location)
 205 {
 206   char server[1024];
 207   char user[1024];
 208   char passwd[1024];
 209   char file[1024];
 210 
 211   extract_location(location,server,user,passwd,file);
 212 #if VERBOSE
 213   fprintf(stderr,"csfopenwrite %s:%s:%s\n",server,user,file);
 214 #endif
 215   if (open_sftp_session(server,user,passwd))
 216     return (void*)  libssh2_sftp_open(sftp_session, file, LIBSSH2_FXF_CREAT|LIBSSH2_FXF_WRITE,
 217         LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
 218         LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH );
 219   else
 220     return NULL;
 221 }
 222 
 223 static char *csfgets(char *s, int size, void *stream)
 224 {
 225   size_t ss;
 226   ss = libssh2_sftp_read((LIBSSH2_SFTP_HANDLE*)stream,s,size-1);
 227   if (ss)
 228   {
 229     s[ss]='\0';
 230 #if VERBOSE
 231     fprintf(stderr,"csfgets:%s\n",s);
 232 #endif
 233     return s;
 234   }
 235   else
 236     return NULL;
 237 }
 238 
 239 static int csremove(const char *location)
 240 {
 241   int ret;
 242   char server[1024];
 243   char user[1024];
 244   char passwd[1024];
 245   char file[1024];
 246 
 247   extract_location(location,server,user,passwd,file);
 248 #if VERBOSE
 249   fprintf(stderr,"csremove %s:%s:%s\n",server,user,file);
 250 #endif
 251   if (open_sftp_session(server,user,passwd))
 252   {
 253     ret = libssh2_sftp_unlink(sftp_session,file);
 254     close_sftp_session();
 255     return ret;
 256   }
 257   else
 258     return -1;
 259 }
 260 
 261 static int csfclose(void *fp)
 262 {
 263 #if VERBOSE
 264   fprintf(stderr,"csfclose\n");
 265 #endif
 266   return libssh2_sftp_close((LIBSSH2_SFTP_HANDLE*)fp);
 267 }
 268 
 269 int main()
 270 {
 271   char location[1024];
 272   char tmp[1024];
 273 #ifdef WIN32
 274   WSADATA wsadata;
 275 
 276   WSAStartup(WINSOCK_VERSION, &wsadata);
 277 #endif
 278 
 279   location[0]='\0';
 280 
 281   printf("server IP:"); fflush(stdout);
 282   fgets(tmp,sizeof(tmp),stdin);
 283   strcat(location,tmp);
 284 
 285   printf("user:"); fflush(stdout);
 286   fgets(tmp,sizeof(tmp),stdin);
 287   strcat(location,tmp);
 288 
 289   printf("passwd:"); fflush(stdout);
 290   fgets(tmp,sizeof(tmp),stdin);
 291   strcat(location,tmp);
 292 
 293   printf(".csexe file name (without extension}:"); fflush(stdout);
 294   fgets(tmp,sizeof(tmp),stdin);
 295   strcat(location,tmp);
 296 
 297   location[strlen(location)-1]='\0';
 298 
 299 #ifdef __COVERAGESCANNER__
 300   __coveragescanner_set_custom_io( csfgets,
 301       csfputs,
 302       csfopenappend,
 303       csfopenread,
 304       csfopenwrite,
 305       csfclose,
 306       csremove);
 307   __coveragescanner_install(location);
 308 #endif
 309 }