From ba217dec1c7de84b1e51807660ccd2cc86f8dde4 Mon Sep 17 00:00:00 2001 From: Aris Adamantiadis Date: Sat, 25 Jul 2009 22:29:12 +0200 Subject: First implementation of a scp transfer tool Doesn't work yet. I've got a headache and debugging doesn't help. --- examples/libssh_scp.c | 338 ++++++++++++++++++++++++++++---------------------- 1 file changed, 192 insertions(+), 146 deletions(-) (limited to 'examples/libssh_scp.c') diff --git a/examples/libssh_scp.c b/examples/libssh_scp.c index b31c6c6..8a2c300 100644 --- a/examples/libssh_scp.c +++ b/examples/libssh_scp.c @@ -17,22 +17,37 @@ program. #include #include #include +#include #include #include "examples_common.h" -char *host; -char *user; -int sftp; +char **sources; +int nsources; +char *destination; +int verbosity=0; + +struct location { + int is_ssh; + char *user; + char *host; + char *path; + ssh_session session; + ssh_channel channel; + FILE *file; +}; + +enum { + READ, + WRITE +}; static void usage(const char *argv0){ - fprintf(stderr,"Usage : %s [options] [login@]hostname\n" - "sample scp client - libssh-%s\n" - "Options :\n" - " -l user : log in as user\n" - " -p port : connect to port\n" - " -d : use DSS to verify host public key\n" - " -r : use RSA to verify host public key\n", + fprintf(stderr,"Usage : %s [options] [[user@]host1:]file1 ... \n" + " [[user@]host2:]destination\n" + "sample scp client - libssh-%s\n", +// "Options :\n", +// " -r : use RSA to verify host public key\n", argv0, ssh_version(0)); exit(0); @@ -40,181 +55,212 @@ static void usage(const char *argv0){ static int opts(int argc, char **argv){ int i; - if(strstr(argv[0],"sftp")) - sftp=1; - // for(i=0;ihost=location->user=NULL; + ptr=strchr(loc,':'); + if(ptr != NULL){ + location->is_ssh=1; + location->path=strdup(ptr+1); + *ptr='\0'; + ptr=strchr(loc,'@'); + if(ptr != NULL){ + location->host=strdup(ptr+1); + *ptr='\0'; + location->user=strdup(loc); + } else { + location->host=strdup(loc); + } + } else { + location->is_ssh=0; + location->path=strdup(loc); + } + return location; +} +static int open_location(struct location *loc, int flag){ + char buffer[1024]; + + if(loc->is_ssh && flag==WRITE){ + loc->session=connect_ssh(loc->host,loc->user); + if(!loc->session){ + fprintf(stderr,"Couldn't connect to %s\n",loc->host); + return -1; + } + loc->channel=channel_new(loc->session); + channel_open_session(loc->channel); + channel_request_pty(loc->channel); + snprintf(buffer,sizeof(buffer),"scp -vt %s",loc->path); + fprintf(stderr,"sending \"%s\"\n",buffer); + if(channel_request_exec(loc->channel,buffer) < 0){ + printf("error executing scp: %s\n",ssh_get_error(loc->session)); + return -1; + } + return 0; + } else { + loc->file=fopen(loc->path,flag==READ ? "r":"w"); + if(!loc->file){ + fprintf(stderr,"Error opening %s : %s\n",loc->path,strerror(errno)); + return -1; + } + return 0; + } + return -1; +} + +static int do_copy(struct location *src, struct location *dest){ + int size; + socket_t fd; + struct stat s; + int w,r; + char buffer[4196]; + /*FIXME*/ + if(dest->is_ssh && !src->is_ssh){ + fd=fileno(src->file); + fstat(fd,&s); + size=s.st_size; + } else + size=0; + snprintf(buffer,sizeof(buffer),"C0644 %d %s\n",size,src->path); + printf("writing \"%s\"",buffer); + if(channel_write(dest->channel,buffer,strlen(buffer))<0){ + fprintf(stderr,"channel_write : %s\n",ssh_get_error(dest->session)); + return -1; + } + r=channel_read(dest->channel,buffer,1,0); + write(1,buffer,1); + do { + r=fread(buffer,1,sizeof(buffer),src->file); + if(r==0) + break; + if(r<0){ + fprintf(stderr,"Error reading file: %s\n",strerror(errno)); + return -1; + } + w=channel_write(dest->channel,buffer,r); + if(w<0){ + fprintf(stderr,"error writing in channel: %s\n",ssh_get_error(dest->session)); + return -1; + } + if(w!=r){ + fprintf(stderr,"coulnd write %d bytes : %d\n",r,w); + } + if((r=channel_poll(dest->channel,0))>0){ + r=channel_read(dest->channel,buffer,r,0); + write(1,buffer,r); + } + if((r=channel_poll(dest->channel,1)) > 0){ + r=channel_read(dest->channel,buffer,r,1); + write(1,buffer,r); + } + } while(1); + channel_write(dest->channel,"\0",1); + channel_send_eof(dest->channel); + do{ + if((r=channel_poll(dest->channel,0))>0){ + r=channel_read(dest->channel,buffer,r,0); + if(r<=0) + break; + write(1,buffer,r); + } + if((r=channel_poll(dest->channel,1)) > 0){ + r=channel_read(dest->channel,buffer,r,1); + if(r<=0) + break; + write(1,buffer,r); + } + } while(r>0); + return 0; +} + +int main(int argc, char **argv){ + struct location *dest, *src; + int i; + + if(opts(argc,argv)<0) + return EXIT_FAILURE; + dest=parse_location(destination); + if(open_location(dest,WRITE)<0) + return EXIT_FAILURE; + for(i=0;i