1
1

oops, forgot to verify that it compiled fine

also, published changes from Norbert including lots of session->fd checks


git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@81 7dcaeef0-15fb-0310-b436-a5af3365683c
Этот коммит содержится в:
Aris Adamantiadis 2006-11-12 00:57:48 +00:00
родитель 4442e0e262
Коммит ff273b26b3
6 изменённых файлов: 66 добавлений и 25 удалений

Просмотреть файл

@ -3,6 +3,9 @@
/* Define to 1 if you have the `cfmakeraw' function. */ /* Define to 1 if you have the `cfmakeraw' function. */
#undef HAVE_CFMAKERAW #undef HAVE_CFMAKERAW
/* Define to 1 if you have the <dlfcn.h> header file. */
#undef HAVE_DLFCN_H
/* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */ /* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */
#undef HAVE_DOPRNT #undef HAVE_DOPRNT
@ -15,8 +18,8 @@
/* Define to 1 if you have the <gcrypt.h> header file. */ /* Define to 1 if you have the <gcrypt.h> header file. */
#undef HAVE_GCRYPT_H #undef HAVE_GCRYPT_H
/* Define to 1 if you have the `gethostbyaddr' function. */ /* Define to 1 if you have the `getaddrinfo' function. */
#undef HAVE_GETHOSTBYADDR #undef HAVE_GETADDRINFO
/* Define to 1 if you have the `gethostbyname' function. */ /* Define to 1 if you have the `gethostbyname' function. */
#undef HAVE_GETHOSTBYNAME #undef HAVE_GETHOSTBYNAME
@ -64,9 +67,6 @@
/* Define to 1 if you have the <openssl/des.h> header file. */ /* Define to 1 if you have the <openssl/des.h> header file. */
#undef HAVE_OPENSSL_DES_H #undef HAVE_OPENSSL_DES_H
/* Define to 1 if you have the <pam/pam_appl.h> header file. */
#undef HAVE_PAM_PAM_APPL_H
/* Define to 1 if you have the `poll' function. */ /* Define to 1 if you have the `poll' function. */
#undef HAVE_POLL #undef HAVE_POLL
@ -77,9 +77,6 @@
and to 0 otherwise. */ and to 0 otherwise. */
#undef HAVE_REALLOC #undef HAVE_REALLOC
/* Define to 1 if you have the <security/pam_appl.h> header file. */
#undef HAVE_SECURITY_PAM_APPL_H
/* Define to 1 if you have the `select' function. */ /* Define to 1 if you have the `select' function. */
#undef HAVE_SELECT #undef HAVE_SELECT

Просмотреть файл

@ -79,7 +79,7 @@ fi
AC_CHECK_LIB([z], [deflateInit_]) AC_CHECK_LIB([z], [deflateInit_])
AC_SEARCH_LIBS([hstrerror],[nsl resolv]) AC_SEARCH_LIBS([hstrerror],[nsl resolv])
AC_SEARCH_LIBS([getaddrinfo],[nsl resolv]) AC_SEARCH_LIBS([getaddrinfo],[nsl socket])
AC_SEARCH_LIBS([gethostbyname],[nsl resolv]) AC_SEARCH_LIBS([gethostbyname],[nsl resolv])
# Checks for header files. # Checks for header files.

Просмотреть файл

@ -36,7 +36,7 @@ char *ssh_get_banner(SSH_SESSION *session){
char buffer[128]; char buffer[128];
int i = 0; int i = 0;
while (i < 127) { while (i < 127) {
if(read(session->fd, &buffer[i], 1)<=0){ if(session->fd >=0 && read(session->fd, &buffer[i], 1)<=0){
ssh_set_error(session,SSH_FATAL,"Remote host closed connection"); ssh_set_error(session,SSH_FATAL,"Remote host closed connection");
return NULL; return NULL;
} }
@ -232,6 +232,7 @@ int ssh_connect(SSH_SESSION *session){
ssh_set_error(session,SSH_FATAL,"Must set options before connect"); ssh_set_error(session,SSH_FATAL,"Must set options before connect");
return SSH_ERROR; return SSH_ERROR;
} }
session->alive=0;
session->client=1; session->client=1;
ssh_crypto_init(); ssh_crypto_init();
if(options->fd==-1 && !options->host){ if(options->fd==-1 && !options->host){
@ -249,43 +250,63 @@ int ssh_connect(SSH_SESSION *session){
session->fd=fd; session->fd=fd;
session->alive=1; session->alive=1;
if(!(session->serverbanner=ssh_get_banner(session))){ if(!(session->serverbanner=ssh_get_banner(session))){
close(fd); if(session->fd>=0)
close(session->fd);
session->fd=-1;
session->alive=0;
return -1; return -1;
} }
set_status(options,0.4); set_status(options,0.4);
ssh_say(2,"banner : %s\n",session->serverbanner); ssh_say(2,"banner : %s\n",session->serverbanner);
/* here we analyse the different protocols the server allows */ /* here we analyse the different protocols the server allows */
if(ssh_analyze_banner(session,&ssh1,&ssh2)){ if(ssh_analyze_banner(session,&ssh1,&ssh2)){
if(session->fd>=0)
close(session->fd);
session->fd=-1;
session->alive=0;
return -1; return -1;
} }
/* here we decide which version of the protocol to use */ /* here we decide which version of the protocol to use */
if(ssh2 && options->ssh2allowed) if(ssh2 && options->ssh2allowed)
session->version=2; session->version=2;
else { else if(ssh1 && options->ssh1allowed)
if(ssh1 && options->ssh1allowed)
session->version=1; session->version=1;
else { else {
ssh_set_error(session,SSH_FATAL, ssh_set_error(session,SSH_FATAL,
"no version of SSH protocol usable (banner: %s)", "no version of SSH protocol usable (banner: %s)",
session->serverbanner); session->serverbanner);
close(session->fd);
session->fd=-1;
session->alive=0;
return -1; return -1;
} }
}
ssh_send_banner(session,0); ssh_send_banner(session,0);
set_status(options,0.5); set_status(options,0.5);
switch(session->version){ switch(session->version){
case 2: case 2:
if(ssh_get_kex(session,0)){ if(ssh_get_kex(session,0)){
if(session->fd>=0)
close(session->fd);
session->fd=-1;
session->alive=0;
return -1; return -1;
} }
set_status(options,0.6); set_status(options,0.6);
ssh_list_kex(&session->server_kex); ssh_list_kex(&session->server_kex);
if(set_kex(session)){ if(set_kex(session)){
if(session->fd>=0)
close(session->fd);
session->fd=-1;
session->alive=0;
return -1; return -1;
} }
ssh_send_kex(session,0); ssh_send_kex(session,0);
set_status(options,0.8); set_status(options,0.8);
if(dh_handshake(session)){ if(dh_handshake(session)){
if(session->fd>=0)
close(session->fd);
session->fd=-1;
session->alive=0;
return -1; return -1;
} }
set_status(options,1.0); set_status(options,1.0);
@ -293,6 +314,10 @@ int ssh_connect(SSH_SESSION *session){
break; break;
case 1: case 1:
if(ssh_get_kex1(session)){ if(ssh_get_kex1(session)){
if(session->fd>=0)
close(session->fd);
session->fd=-1;
session->alive=0;
return -1; return -1;
} }
set_status(options,0.6); set_status(options,0.6);

Просмотреть файл

@ -146,7 +146,7 @@ int ssh_connect_host(SSH_SESSION *session, const char *host, const char
} }
freeaddrinfo(bind_ai); freeaddrinfo(bind_ai);
} }
if(timeout){ if(timeout||usec){
return ssh_connect_ai_timeout(session,host,port,ai,timeout,usec,s); return ssh_connect_ai_timeout(session,host,port,ai,timeout,usec,s);
} }
if(connect(s,ai->ai_addr,ai->ai_addrlen)<0){ if(connect(s,ai->ai_addr,ai->ai_addrlen)<0){
@ -178,9 +178,10 @@ int ssh_fd_poll(SSH_SESSION *session, int *write, int *except){
FD_ZERO(&wdes); FD_ZERO(&wdes);
FD_ZERO(&edes); FD_ZERO(&edes);
if(!session->alive){ if(!session->alive || session->fd<0){
*except=1; *except=1;
*write=0; *write=0;
session->alive=0;
return 0; return 0;
} }
if(!session->data_to_read) if(!session->data_to_read)

Просмотреть файл

@ -39,6 +39,8 @@ static int completeread(int fd, void *buffer, int len){
int r; int r;
int total=0; int total=0;
int toread=len; int toread=len;
if(fd<0)
return SSH_ERROR;
while((r=read(fd,buffer+total,toread))){ while((r=read(fd,buffer+total,toread))){
if(r==-1) if(r==-1)
return SSH_ERROR; return SSH_ERROR;
@ -76,6 +78,7 @@ static int socket_read(SSH_SESSION *session,int len){
(r==0)?"Connection closed by remote host" : "Error reading socket"); (r==0)?"Connection closed by remote host" : "Error reading socket");
close(session->fd); close(session->fd);
session->fd=-1; session->fd=-1;
session->alive=0;
session->data_except=1; session->data_except=1;
return SSH_ERROR; return SSH_ERROR;
} }
@ -91,7 +94,10 @@ static int socket_read(SSH_SESSION *session,int len){
return SSH_AGAIN; return SSH_AGAIN;
session->data_to_read=0; session->data_to_read=0;
/* read as much as we can */ /* read as much as we can */
if(session->fd>0)
r=read(session->fd,buffer,sizeof(buffer)); r=read(session->fd,buffer,sizeof(buffer));
else
r=-1;
if(r<=0){ if(r<=0){
ssh_set_error(session,SSH_FATAL, ssh_set_error(session,SSH_FATAL,
(r==0)?"Connection closed by remote host" : "Error reading socket"); (r==0)?"Connection closed by remote host" : "Error reading socket");
@ -335,6 +341,8 @@ int packet_translate(SSH_SESSION *session){
static int atomic_write(int fd, void *buffer, int len){ static int atomic_write(int fd, void *buffer, int len){
int written; int written;
if(fd<0)
return SSH_ERROR;
while(len >0) { while(len >0) {
written=write(fd,buffer,len); written=write(fd,buffer,len);
if(written==0 || written==-1) if(written==0 || written==-1)
@ -352,9 +360,12 @@ static int packet_nonblocking_flush(SSH_SESSION *session){
int w; int w;
ssh_fd_poll(session,&can_write,&except); /* internally sets data_to_write */ ssh_fd_poll(session,&can_write,&except); /* internally sets data_to_write */
while(session->data_to_write && buffer_get_rest_len(session->out_socket_buffer)>0){ while(session->data_to_write && buffer_get_rest_len(session->out_socket_buffer)>0){
if(session->fd<0){
w=write(session->fd,buffer_get_rest(session->out_socket_buffer), w=write(session->fd,buffer_get_rest(session->out_socket_buffer),
buffer_get_rest_len(session->out_socket_buffer)); buffer_get_rest_len(session->out_socket_buffer));
session->data_to_write=0; session->data_to_write=0;
} else
w=-1; /* write failed */
if(w<0){ if(w<0){
session->data_to_write=0; session->data_to_write=0;
session->data_except=1; session->data_except=1;
@ -376,6 +387,10 @@ static int packet_nonblocking_flush(SSH_SESSION *session){
/* blocking packet flush */ /* blocking packet flush */
static int packet_blocking_flush(SSH_SESSION *session){ static int packet_blocking_flush(SSH_SESSION *session){
if(session->fd<0) {
session->alive=0;
return SSH_ERROR;
}
if(session->data_except) if(session->data_except)
return SSH_ERROR; return SSH_ERROR;
if(buffer_get_rest(session->out_socket_buffer)==0) if(buffer_get_rest(session->out_socket_buffer)==0)

Просмотреть файл

@ -43,6 +43,7 @@ SSH_SESSION *ssh_new() {
session->next_crypto=crypto_new(); session->next_crypto=crypto_new();
session->maxchannel=FIRST_CHANNEL; session->maxchannel=FIRST_CHANNEL;
session->fd=-1; session->fd=-1;
session->alive=0;
session->blocking=1; session->blocking=1;
return session; return session;
} }
@ -96,8 +97,10 @@ void ssh_cleanup(SSH_SESSION *session){
* \param session current ssh session * \param session current ssh session
*/ */
void ssh_silent_disconnect(SSH_SESSION *session){ void ssh_silent_disconnect(SSH_SESSION *session){
if(session->fd>=0)
close(session->fd); close(session->fd);
session->alive=0; session->alive=0;
session->fd=-1;
ssh_disconnect(session); ssh_disconnect(session);
} }