/* client.c */ /* Copyright 2003 Aris Adamantiadis This file is part of the SSH Library You are free to copy this file, modify it in any way, consider it being public domain. This does not apply to the rest of the library though, but it is allowed to cut-and-paste working code from this file to any license of program. The goal is to show the API in action. It's not a reference on how terminal clients must be made or how a client should react. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #define MAXCMD 10 char *host; char *user; int sftp; char *cmds[MAXCMD]; struct termios terminal; void do_sftp(SSH_SESSION *session); void add_cmd(char *cmd){ int n; for(n=0;cmds[n] && (nname,file->permissions,file->uid,file->gid,file->size); sftp_attributes_free(file); } /* when file=NULL, an error has occured OR the directory listing is end of file */ if(!sftp_dir_eof(dir)){ ssh_say(0,"error : %s\n",ssh_get_error(session)); return; } if(sftp_dir_close(dir)){ ssh_say(0,"Error : %s\n",ssh_get_error(session)); return; } /* this will open a file and copy it into your /home directory */ /* the small buffer size was intended to stress the library. of course, you can use a buffer till 20kbytes without problem */ fichier=sftp_open(sftp,"/usr/bin/ssh",O_RDONLY,NULL); if(!fichier){ ssh_say(0,"Error opening /usr/bin/ssh : %s\n",ssh_get_error(session)); return; } /* open a file for writing... */ to=sftp_open(sftp,"ssh-copy",O_WRONLY | O_CREAT,NULL); if(!to){ ssh_say(0,"Error opening ssh-copy for writing : %s\n",ssh_get_error(session)); return; } while((len=sftp_read(fichier,data,4096)) > 0){ if(sftp_write(to,data,len)!=len){ ssh_say(0,"error writing %d bytes : %s\n",len,ssh_get_error(session)); return; } } printf("finished\n"); if(len<0) ssh_say(0,"Error reading file : %s\n",ssh_get_error(session)); sftp_file_close(fichier); sftp_file_close(to); printf("fichiers fermés\n"); to=sftp_open(sftp,"/tmp/grosfichier",O_WRONLY|O_CREAT,NULL); for(i=0;i<1000;++i){ len=sftp_write(to,data,8000); printf("wrote %d bytes\n",len); if(len != 8000){ printf("chunk %d : %d (%s)\n",i,len,ssh_get_error(session)); } } sftp_file_close(to); /* close the sftp session */ sftp_free(sftp); printf("session sftp terminée\n"); } int auth_kbdint(SSH_SESSION *session){ int err=ssh_userauth_kbdint(session,NULL,NULL); char *name,*instruction,*prompt,*ptr; char buffer[128]; int i,n; char echo; while (err==SSH_AUTH_INFO){ name=ssh_userauth_kbdint_getname(session); instruction=ssh_userauth_kbdint_getinstruction(session); n=ssh_userauth_kbdint_getnprompts(session); if(strlen(name)>0) printf("%s\n",name); if(strlen(instruction)>0) printf("%s\n",instruction); for(i=0;i