/* 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 "config.h" #include #include #include #include #include #include #include #ifdef HAVE_PTY_H #include #endif #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] && (nc_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); termios_p->c_oflag &= ~OPOST; termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); termios_p->c_cflag &= ~(CSIZE|PARENB); termios_p->c_cflag |= CS8; } #endif void do_cleanup(){ tcsetattr(0,TCSANOW,&terminal); } void do_exit(){ do_cleanup(); exit(0); } CHANNEL *chan; int signal_delayed=0; void setsignal(); void sigwindowchanged(){ signal_delayed=1; } void sizechanged(){ struct winsize win = { 0, 0, 0, 0 }; ioctl(1, TIOCGWINSZ, &win); channel_change_pty_size(chan,win.ws_col, win.ws_row); // printf("Changed pty size\n"); setsignal(); } void setsignal(){ signal(SIGWINCH,sigwindowchanged); signal_delayed=0; } void select_loop(SSH_SESSION *session,CHANNEL *channel){ fd_set fds; struct timeval timeout; char buffer[10]; BUFFER *readbuf=buffer_new(); CHANNEL *channels[2]; int lus; int eof=0; int maxfd; int ret; while(channel){ /* when a signal is caught, ssh_select will return * with SSH_EINTR, which means it should be started * again. It lets you handle the signal the faster you * can, like in this window changed example. Of course, if * your signal handler doesn't call libssh at all, you're * free to handle signals directly in sighandler. */ do{ FD_ZERO(&fds); if(!eof) FD_SET(0,&fds); timeout.tv_sec=30; timeout.tv_usec=0; FD_SET(ssh_get_fd(session),&fds); maxfd=ssh_get_fd(session)+1; ret=select(maxfd,&fds,NULL,NULL,&timeout); if(ret==EINTR) continue; if(FD_ISSET(0,&fds)){ lus=read(0,buffer,10); if(lus) channel_write(channel,buffer,lus); else { eof=1; channel_send_eof(channel); } } if(FD_ISSET(ssh_get_fd(session),&fds)){ ssh_set_fd_toread(session); } channels[0]=channel; // set the first channel we want to read from channels[1]=NULL; ret=channel_select(channels,NULL,NULL,NULL); // no specific timeout - just poll if(signal_delayed) sizechanged(); } while (ret==EINTR || ret==SSH_EINTR); // we already looked for input from stdin. Now, we are looking for input from the channel if(channel && channel_is_closed(channel)){ channel_free(channel); channel=NULL; channels[0]=NULL; } if(channels[0]){ while(channel && channel_is_open(channel) && channel_poll(channel,0)){ lus=channel_read(channel,readbuf,0,0); if(lus==-1){ ssh_say(0,"error reading channel : %s\n",ssh_get_error(session)); return; } if(lus==0){ ssh_say(1,"EOF received\n"); channel_free(channel); channel=channels[0]=NULL; } else write(1,buffer_get(readbuf),lus); } while(channel && channel_is_open(channel) && channel_poll(channel,1)){ /* stderr */ lus=channel_read(channel,readbuf,0,1); if(lus==-1){ ssh_say(0,"error reading channel : %s\n",ssh_get_error(session)); return; } if(lus==0){ ssh_say(1,"EOF received\n"); channel_free(channel); channel=channels[0]=NULL; } else write(2,buffer_get(readbuf),lus); } } if(channel && channel_is_closed(channel)){ channel_free(channel); channel=NULL; } } buffer_free(readbuf); } void shell(SSH_SESSION *session){ CHANNEL *channel; struct termios terminal_local; int interactive=isatty(0); channel = channel_new(session); if(interactive){ tcgetattr(0,&terminal_local); memcpy(&terminal,&terminal_local,sizeof(struct termios)); } if(channel_open_session(channel)){ printf("error opening channel : %s\n",ssh_get_error(session)); return; } chan=channel; if(interactive){ channel_request_pty(channel); sizechanged(); } if(channel_request_shell(channel)){ printf("Requesting shell : %s\n",ssh_get_error(session)); return; } if(interactive){ cfmakeraw(&terminal_local); tcsetattr(0,TCSANOW,&terminal_local); setsignal(); } signal(SIGTERM,do_cleanup); select_loop(session,channel); } void batch_shell(SSH_SESSION *session){ CHANNEL *channel; char buffer[1024]; int i,s=0; for(i=0;iname,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�\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�\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