Index: config.h.win32-openssl =================================================================== --- config.h.win32-openssl (revision 0) +++ config.h.win32-openssl (revision 0) @@ -0,0 +1,193 @@ +/* config.h. Manually tweaked for Windows. */ + +/* Define to 1 if you have the `cfmakeraw' function. */ +#define HAVE_CFMAKERAW 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_DLFCN_H 1 + +/* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */ +/* #undef HAVE_DOPRNT */ + +/* Define to 1 if you have the `endpwent' function. */ +/* #undef HAVE_ENDPWENT */ + +/* Define to 1 if you have the header file. */ +#define HAVE_FCNTL_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_GCRYPT_H */ + +/* Define to 1 if you have the `getaddrinfo' function. */ +#define HAVE_GETADDRINFO 1 + +/* Define to 1 if you have the `gethostbyname' function. */ +#define HAVE_GETHOSTBYNAME 1 + +/* Define to 1 if you have the `getpass' function. */ +/* #undef HAVE_GETPASS */ + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define to 1 if you have the `crypto' library (-lcrypto). */ +#define HAVE_LIBCRYPTO 1 + +/* Define to 1 if you have the `gcrypt' library (-lgcrypt). */ +/* #undef HAVE_LIBGCRYPT */ + +/* Define to 1 if you have the `z' library (-lz). */ +#define HAVE_LIBZ 1 + +/* Define to 1 if your system has a GNU libc compatible `malloc' function, and + to 0 otherwise. */ +#define HAVE_MALLOC 1 + +/* Define to 1 if you have the `memmove' function. */ +#define HAVE_MEMMOVE 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_MEMORY_H 1 + +/* Define to 1 if you have the `memset' function. */ +#define HAVE_MEMSET 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_NETDB_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_NETINET_IN_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_OPENSSL_AES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_OPENSSL_BLOWFISH_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_OPENSSL_DES_H 1 + +/* Define to 1 if you have the `poll' function. */ +/* #undef HAVE_POLL */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_PTY_H */ + +/* Define to 1 if your system has a GNU libc compatible `realloc' function, + and to 0 otherwise. */ +#define HAVE_REALLOC 1 + +/* Define to 1 if you have the `select' function. */ +#define HAVE_SELECT 1 + +/* Define to 1 if you have the `socket' function. */ +#define HAVE_SOCKET 1 + +/* Define to 1 if you want to enable SSH1 */ +/* #undef HAVE_SSH1 */ + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the `strchr' function. */ +#define HAVE_STRCHR 1 + +/* Define to 1 if you have the `strdup' function. */ +#define HAVE_STRDUP 1 + +/* Define to 1 if you have the `strerror' function. */ +#define HAVE_STRERROR 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if you have the `strstr' function. */ +#define HAVE_STRSTR 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_POLL_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SELECT_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_SOCKET_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TIME_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_TERMIOS_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* Define to 1 if you have the `vprintf' function. */ +#define HAVE_VPRINTF 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_ZLIB_H 1 + +/* Name of package */ +#define PACKAGE "libssh" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "aris@0xbadc0de.be" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "libssh" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "libssh 0.2" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "libssh" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "0.2" + +/* Define as the return type of signal handlers (`int' or `void'). */ +#define RETSIGTYPE void + +/* Define to the type of arg 1 for `select'. */ +#define SELECT_TYPE_ARG1 int + +/* Define to the type of args 2, 3 and 4 for `select'. */ +#define SELECT_TYPE_ARG234 (fd_set *) + +/* Define to the type of arg 5 for `select'. */ +#define SELECT_TYPE_ARG5 (struct timeval *) + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Define to 1 if you can safely include both and . */ +#define TIME_WITH_SYS_TIME 1 + +/* Version number of package */ +#define VERSION "0.2" + +/* Define to 1 if your processor stores words with the most significant byte + first (like Motorola and SPARC, unlike Intel and VAX). */ +/* #undef WORDS_BIGENDIAN */ + +/* Define to empty if `const' does not conform to ANSI C. */ +/* #undef const */ + +/* Define to rpl_malloc if the replacement function should be used. */ +/* #undef malloc */ + +/* Define to rpl_realloc if the replacement function should be used. */ +/* #undef realloc */ Index: include/libssh/libssh.h =================================================================== --- include/libssh/libssh.h (revision 133) +++ include/libssh/libssh.h (working copy) @@ -21,7 +21,11 @@ #ifndef _LIBSSH_H #define _LIBSSH_H #include +#ifdef _WIN32 +#include +#else #include /* for fd_set * */ +#endif #include #define LIBSSH_VERSION "libssh-0.2" Index: libssh/auth.c =================================================================== --- libssh/auth.c (revision 133) +++ libssh/auth.c (working copy) @@ -24,7 +24,11 @@ #include #include #include +#ifdef _WIN32 +#include +#else #include +#endif /** defgroup ssh_auth * \brief functions to authenticate Index: libssh/auth1.c =================================================================== --- libssh/auth1.c (revision 133) +++ libssh/auth1.c (working copy) @@ -22,7 +22,11 @@ #include "libssh/priv.h" #include "libssh/ssh1.h" #include +#ifdef _WIN32 +#include +#else #include +#endif #include /* Index: libssh/buffer.c =================================================================== --- libssh/buffer.c (revision 133) +++ libssh/buffer.c (working copy) @@ -22,7 +22,11 @@ #include #include +#ifdef _WIN32 +#include +#else #include +#endif #include "libssh/priv.h" /** defgroup ssh_buffer Index: libssh/channels.c =================================================================== --- libssh/channels.c (revision 133) +++ libssh/channels.c (working copy) @@ -22,7 +22,11 @@ #include #include +#ifdef _WIN32 +#include +#else #include +#endif #include #include #include Index: libssh/channels1.c =================================================================== --- libssh/channels1.c (revision 133) +++ libssh/channels1.c (working copy) @@ -22,7 +22,11 @@ #include #include +#ifdef _WIN32 +#include +#else #include +#endif #include #include Index: libssh/client.c =================================================================== --- libssh/client.c (revision 133) +++ libssh/client.c (working copy) @@ -22,7 +22,12 @@ #include #include #include +#ifdef _WIN32 +#include +#else #include +#define closesocket close +#endif #include "libssh/priv.h" #include "libssh/ssh2.h" @@ -36,7 +41,7 @@ char buffer[128]; int i = 0; while (i < 127) { - if(session->fd <0 || read(session->fd, &buffer[i], 1)<=0){ + if(session->fd <0 || recv(session->fd, &buffer[i], 1, 0)<=0){ ssh_set_error(session,SSH_FATAL,"Remote host closed connection"); return NULL; } @@ -98,7 +103,7 @@ else session->clientbanner=strdup(banner); snprintf(buffer,128,"%s\r\n",banner); - write(session->fd,buffer,strlen(buffer)); + send(session->fd,buffer,strlen(buffer), 0); return 0; } @@ -251,7 +256,7 @@ session->alive=1; if(!(session->serverbanner=ssh_get_banner(session))){ if(session->fd>=0) - close(session->fd); + closesocket(session->fd); session->fd=-1; session->alive=0; return -1; @@ -261,7 +266,7 @@ /* here we analyse the different protocols the server allows */ if(ssh_analyze_banner(session,&ssh1,&ssh2)){ if(session->fd>=0) - close(session->fd); + closesocket(session->fd); session->fd=-1; session->alive=0; return -1; @@ -275,7 +280,7 @@ ssh_set_error(session,SSH_FATAL, "no version of SSH protocol usable (banner: %s)", session->serverbanner); - close(session->fd); + closesocket(session->fd); session->fd=-1; session->alive=0; return -1; @@ -286,7 +291,7 @@ case 2: if(ssh_get_kex(session,0)){ if(session->fd>=0) - close(session->fd); + closesocket(session->fd); session->fd=-1; session->alive=0; return -1; @@ -295,7 +300,7 @@ ssh_list_kex(&session->server_kex); if(set_kex(session)){ if(session->fd>=0) - close(session->fd); + closesocket(session->fd); session->fd=-1; session->alive=0; return -1; @@ -304,7 +309,7 @@ set_status(options,0.8); if(dh_handshake(session)){ if(session->fd>=0) - close(session->fd); + closesocket(session->fd); session->fd=-1; session->alive=0; return -1; @@ -315,7 +320,7 @@ case 1: if(ssh_get_kex1(session)){ if(session->fd>=0) - close(session->fd); + closesocket(session->fd); session->fd=-1; session->alive=0; return -1; @@ -352,7 +357,7 @@ buffer_add_ssh_string(session->out_buffer,str); free(str); packet_send(session); - close(session->fd); + closesocket(session->fd); session->fd=-1; } session->alive=0; Index: libssh/connect.c =================================================================== --- libssh/connect.c (revision 133) +++ libssh/connect.c (working copy) @@ -20,18 +20,28 @@ the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include #include #include #include #include #include #include +#include +#include +#ifdef _WIN32 +#define _WIN32_WINNT 0x0501 //getaddrinfo, freeaddrinfo +#include +#include +#define SOCKOPT_TYPE_ARG4 char +#else +#include #include #include -#include #include -#include +#define SOCKOPT_TYPE_ARG4 int +#define closesocket close +#endif + #include "libssh/priv.h" #ifndef HAVE_SELECT @@ -43,10 +53,20 @@ #endif static void sock_set_nonblocking(int sock) { - fcntl(sock,F_SETFL,O_NONBLOCK); + #ifndef _WIN32 + fcntl(sock,F_SETFL,O_NONBLOCK); + #else + u_long nonblocking = 1; + ioctlsocket(sock, FIONBIO, &nonblocking); + #endif } static void sock_set_blocking(int sock){ - fcntl(sock,F_SETFL,0); + #ifndef _WIN32 + fcntl(sock,F_SETFL,0); + #else + u_long nonblocking = 0; + ioctlsocket(sock, FIONBIO, &nonblocking); + #endif } static int getai(const char *host, int port, struct addrinfo **ai) @@ -86,19 +106,20 @@ if(ret==0){ /* timeout */ ssh_set_error(session,SSH_FATAL,"Timeout while connecting to %s:%d",host,port); - close(s); + closesocket(s); return -1; } if(ret<0){ ssh_set_error(session,SSH_FATAL,"Select error : %s",strerror(errno)); - close(s); + closesocket(s); return -1; } /* get connect(2) return code. zero means no error */ - getsockopt(s,SOL_SOCKET,SO_ERROR,&ret,&len); + SOCKOPT_TYPE_ARG4 sret; + getsockopt(s,SOL_SOCKET,SO_ERROR,&sret,&len); if (ret!=0){ ssh_set_error(session,SSH_FATAL,"Connecting : %s",strerror(ret)); - close(s); + closesocket(s); return -1; } /* s is connected ? */ @@ -152,7 +173,7 @@ } freeaddrinfo(bind_ai); if(bind_ai2==NULL){ /*cannot bind to any local addresses*/ - close(s); + closesocket(s); s=-1; continue; } @@ -162,7 +183,7 @@ } if(connect(s,ai2->ai_addr,ai2->ai_addrlen)<0){ ssh_set_error(session,SSH_FATAL,"connect: %s",strerror(errno)); - close(s); + closesocket(s); s=-1; continue; } Index: libssh/crypt.c =================================================================== --- libssh/crypt.c (revision 133) +++ libssh/crypt.c (working copy) @@ -31,12 +31,16 @@ #include #endif +#ifdef _WIN32 +#include +#else #include +#endif #include "libssh/priv.h" #include "libssh/crypto.h" u32 packet_decrypt_len(SSH_SESSION *session, char *crypted){ - u32 decrypted; + u32 decrypted; if(session->current_crypto) packet_decrypt(session,crypted,session->current_crypto->in_cipher->blocksize); memcpy(&decrypted,crypted,sizeof(decrypted)); Index: libssh/dh.c =================================================================== --- libssh/dh.c (revision 133) +++ libssh/dh.c (working copy) @@ -37,7 +37,11 @@ #include #include +#ifdef _WIN32 +#include +#else #include +#endif #include #include "libssh/crypto.h" #include "libssh/priv.h" Index: libssh/gai_strerror.c =================================================================== --- libssh/gai_strerror.c (revision 0) +++ libssh/gai_strerror.c (revision 0) @@ -0,0 +1,31 @@ +/* Copyright (C) 1996,97,2001,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +/*#include +#include */ + +const char * +gai_strerror (int code) +{ + static char buffer[128]; + snprintf (buffer, sizeof buffer, "Unknown error (%d)", code); + return buffer; +} + +/* Source: File from GNU C Library(libc) by drepper - http://cvs.savannah.gnu.org/viewvc/libc/posix/gai_strerror.c?revision=1.1&root=libc&view=markup +libc_hidden_def (gai_strerror) */ Index: libssh/kex.c =================================================================== --- libssh/kex.c (revision 133) +++ libssh/kex.c (working copy) @@ -22,27 +22,31 @@ #include #include #include +#ifdef _WIN32 +#include +#else #include +#endif #include "libssh/priv.h" #include "libssh/ssh2.h" #include "libssh/ssh1.h" #ifdef HAVE_LIBGCRYPT -#define BLOWFISH "blowfish-cbc," -#define AES "aes256-cbc,aes192-cbc,aes128-cbc," -#define DES "3des-cbc" +#define BLOWFISH "blowfish-cbc" +#define AES "aes128-cbc," //"aes256-cbc,aes192-cbc,aes128-cbc," +#define DES //"3des-cbc" #elif defined HAVE_LIBCRYPTO #ifdef HAVE_OPENSSL_BLOWFISH_H -#define BLOWFISH "blowfish-cbc," +#define BLOWFISH "blowfish-cbc" #else #define BLOWFISH "" #endif #ifdef HAVE_OPENSSL_AES_H -#define AES "aes256-cbc,aes192-cbc,aes128-cbc," +#define AES "aes128-cbc," //"aes256-cbc,aes192-cbc,aes128-cbc," #else #define AES "" #endif -#define DES "3des-cbc" +#define DES //"3des-cbc" #endif #ifdef HAVE_LIBZ Index: libssh/keyfiles.c =================================================================== --- libssh/keyfiles.c (revision 133) +++ libssh/keyfiles.c (working copy) @@ -37,7 +37,11 @@ #include #include #endif +#ifdef _WIN32 +#include +#else #include +#endif #define MAXLINESIZE 80 #ifdef HAVE_LIBGCRYPT #define MAX_KEY_SIZE 32 @@ -457,15 +461,21 @@ } #endif /* GCRYPT */ -static int default_get_password(char *buf, int size,int rwflag, char *descr){ +static int default_get_password(char *buf, int size,int rwflag, char *descr){ + int len; +#if HAVE_GETPASS char *pass; char buffer[256]; - int len; - snprintf(buffer,256,"Please enter passphrase for %s",descr); + snprintf(buffer,256,"Please enter passphrase for %s",descr); pass=getpass(buffer); snprintf(buf,size,"%s",pass); len=strlen(buf); memset(pass,0,strlen(pass)); +#else + printf("Please enter passphrase for %s: ",descr); + if (!fgets(buf, size, stdin)) return 0; + len=strlen(buf); +#endif return len; } @@ -662,7 +672,7 @@ ssh_set_error(session,SSH_REQUEST_DENIED,"Invalid public key file"); return NULL; } - r=read(fd,buf,sizeof(buf)-1); + r=recv(fd,buf,sizeof(buf)-1, 0); close(fd); if(r<=0){ ssh_set_error(session,SSH_REQUEST_DENIED,"Invalid public key file"); Index: libssh/keys.c =================================================================== --- libssh/keys.c (revision 133) +++ libssh/keys.c (working copy) @@ -22,7 +22,11 @@ MA 02111-1307, USA. */ #include #include +#ifdef _WIN32 +#include +#else #include +#endif #ifdef HAVE_LIBCRYPTO #include #include Index: libssh/messages.c =================================================================== --- libssh/messages.c (revision 133) +++ libssh/messages.c (working copy) @@ -30,7 +30,11 @@ #include "libssh/priv.h" #include "libssh/server.h" #include "libssh/ssh2.h" +#ifdef _WIN32 +#include +#else #include +#endif #include #include Index: libssh/misc.c =================================================================== --- libssh/misc.c (revision 133) +++ libssh/misc.c (working copy) @@ -24,11 +24,18 @@ #include #include #include +#include +#ifdef _WIN32 +#define _WIN32_IE 0x0400 //SHGetSpecialFolderPath +#include +#include +#else #include -#include #include +#endif #include "libssh/libssh.h" +#ifndef _WIN32 /* if the program was executed suid root, don't trust the user ! */ static int is_trusted(){ if(geteuid()!=getuid()) @@ -66,7 +73,7 @@ char *ssh_get_user_home_dir(){ char *home; - char *user; + char *user; int trusted=is_trusted(); if(trusted){ if((home=getenv("HOME"))) @@ -74,8 +81,17 @@ if((user=getenv("USER"))) return get_homedir_from_login(user); } - return get_homedir_from_uid(getuid()); + return get_homedir_from_uid(getuid()); } +#else +char *ssh_get_user_home_dir(){ + static char szPath[MAX_PATH]; + if (SHGetSpecialFolderPathA(NULL, szPath, CSIDL_PROFILE, TRUE)) + return szPath; + else + return NULL; +} +#endif /* we have read access on file */ int ssh_file_readaccess_ok(char *file){ Index: libssh/options.c =================================================================== --- libssh/options.c (revision 133) +++ libssh/options.c (working copy) @@ -23,7 +23,9 @@ #include #include #include +#ifndef _WIN32 #include +#endif #include #include "libssh/priv.h" @@ -289,6 +291,7 @@ return 0; } +#ifndef _WIN32 static char *get_username_from_uid(int uid){ struct passwd *pwd; char *user; @@ -303,6 +306,7 @@ ssh_set_error(NULL,SSH_FATAL,"uid %d doesn't exist !",uid); return NULL; } +#endif /* this function must be called when no specific username has been asked. it has to guess it */ int ssh_options_default_username(SSH_OPTIONS *opt){ @@ -314,11 +318,21 @@ opt->username=strdup(user); return 0; } +#ifndef _WIN32 user=get_username_from_uid(getuid()); if(user){ opt->username=user; return 0; } +#else + DWORD Size = 0; + GetUserName(NULL, &Size); //Get Size + user = malloc(Size); + if (GetUserName(user, &Size)) + return 0; + free(user); +#endif + return -1; } Index: libssh/packet.c =================================================================== --- libssh/packet.c (revision 133) +++ libssh/packet.c (working copy) @@ -27,7 +27,12 @@ #include "libssh/priv.h" #include "libssh/ssh2.h" #include "libssh/ssh1.h" +#ifdef _WIN32 +#include +#else #include +#define closesocket close +#endif #include #include "libssh/crypto.h" @@ -41,7 +46,7 @@ int toread=len; if(fd<0) return SSH_ERROR; - while((r=read(fd,buffer+total,toread))){ + while((r=recv(fd,buffer+total,toread, 0))){ if(r==-1) return SSH_ERROR; total += r; @@ -76,7 +81,7 @@ if(r==SSH_ERROR || r ==0){ ssh_set_error(session,SSH_FATAL, (r==0)?"Connection closed by remote host" : "Error reading socket"); - close(session->fd); + closesocket(session->fd); session->fd=-1; session->alive=0; session->data_except=1; @@ -94,15 +99,15 @@ return SSH_AGAIN; session->data_to_read=0; /* read as much as we can */ - if(session->fd>0) - r=read(session->fd,buffer,sizeof(buffer)); + if(session->fd>=0) + r=recv(session->fd,buffer,sizeof(buffer),0); else r=-1; if(r<=0){ ssh_set_error(session,SSH_FATAL, (r==0)?"Connection closed by remote host" : "Error reading socket"); if(session->fd>=0) - close(session->fd); + closesocket(session->fd); session->fd=-1; session->data_except=1; session->alive=0; @@ -143,7 +148,7 @@ buffer_pass_bytes(session->in_socket_buffer,blocksize); // mark them as read len=packet_decrypt_len(session,buffer); buffer_add_data(session->in_buffer,buffer,blocksize); - if(len> MAX_PACKET_LEN){ + if(len > MAX_PACKET_LEN){ ssh_set_error(session,SSH_FATAL,"read_packet(): Packet len too high(%uld %.8lx)",len,len); return SSH_ERROR; } @@ -346,7 +351,7 @@ if(fd<0) return SSH_ERROR; while(len >0) { - written=write(fd,buffer,len); + written=send(fd,buffer,len, 0); if(written==0 || written==-1) return SSH_ERROR; len-=written; @@ -368,8 +373,8 @@ } 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), - buffer_get_rest_len(session->out_socket_buffer)); + w=send(session->fd,buffer_get_rest(session->out_socket_buffer), + buffer_get_rest_len(session->out_socket_buffer), 0); session->data_to_write=0; } else w=-1; /* write failed */ @@ -377,7 +382,7 @@ session->data_to_write=0; session->data_except=1; session->alive=0; - close(session->fd); + closesocket(session->fd); session->fd=-1; ssh_set_error(session,SSH_FATAL,"Writing packet : error on socket (or connection closed): %s", strerror(errno)); @@ -407,7 +412,7 @@ session->data_to_write=0; session->data_except=1; session->alive=0; - close(session->fd); + closesocket(session->fd); session->fd=-1; ssh_set_error(session,SSH_FATAL,"Writing packet : error on socket (or connection closed): %s", strerror(errno)); @@ -545,7 +550,7 @@ case SSH_MSG_DISCONNECT: ssh_say(2,"Received SSH_MSG_DISCONNECT\n"); ssh_set_error(session,SSH_FATAL,"Received SSH_MSG_DISCONNECT"); - close(session->fd); + closesocket(session->fd); session->fd=-1; session->alive=0; return; @@ -572,7 +577,7 @@ free(error_s); free(error); } - close(session->fd); + closesocket(session->fd); session->fd=-1; session->alive=0; return; Index: libssh/server.c =================================================================== --- libssh/server.c (revision 133) +++ libssh/server.c (working copy) @@ -26,9 +26,23 @@ #include #include #include +#ifdef _WIN32 +#include +#define SOCKOPT_TYPE_ARG4 char + +/* We need to provide hstrerror. Not we can't call the parameter h_errno because it's #defined */ +inline char* hstrerror(int h_errno_val) { + static char text[50]; + sprintf(text, "gethostbyname error %d\n", h_errno); + return text; +} +#else #include #include #include +#define SOCKOPT_TYPE_ARG4 int +#define closesocket close +#endif #include #include #include @@ -38,7 +52,7 @@ #include "libssh/ssh2.h" static int bind_socket(SSH_BIND *ssh_bind,char *hostname, int port) { struct sockaddr_in myaddr; - int opt = 1; + SOCKOPT_TYPE_ARG4 opt = 1; int s = socket(PF_INET, SOCK_STREAM, 0); struct hostent *hp=NULL; #ifdef HAVE_GETHOSTBYNAME @@ -46,7 +60,7 @@ #endif if(!hp){ ssh_set_error(ssh_bind,SSH_FATAL,"resolving %s: %s",hostname,hstrerror(h_errno)); - close(s); + closesocket(s); return -1; } @@ -58,7 +72,7 @@ if (bind(s, (struct sockaddr *) &myaddr, sizeof(myaddr)) < 0) { ssh_set_error(ssh_bind,SSH_FATAL,"Binding to %s:%d : %s",hostname,port, strerror(errno)); - close(s); + closesocket(s); return -1; } return s; @@ -90,7 +104,7 @@ if(listen(fd,10)<0){ ssh_set_error(ssh_bind,SSH_FATAL,"listening to socket %d: %s", fd,strerror(errno)); - close(fd); + closesocket(fd); return -1; } return 0; @@ -157,7 +171,7 @@ void ssh_bind_free(SSH_BIND *ssh_bind){ if(ssh_bind->bindfd>=0) - close(ssh_bind->bindfd); + closesocket(ssh_bind->bindfd); ssh_bind->bindfd=-1; free(ssh_bind); } Index: libssh/session.c =================================================================== --- libssh/session.c (revision 133) +++ libssh/session.c (working copy) @@ -28,6 +28,12 @@ #include "libssh/server.h" #define FIRST_CHANNEL 42 // why not ? it helps to find bugs. +#ifdef _WIN32 +#include +#else +#define closesocket close +#endif + /** defgroup ssh_session * \brief functions that manage a session */ @@ -98,7 +104,7 @@ */ void ssh_silent_disconnect(SSH_SESSION *session){ if(session->fd>=0) - close(session->fd); + closesocket(session->fd); session->alive=0; session->fd=-1; ssh_disconnect(session); Index: libssh/sftp.c =================================================================== --- libssh/sftp.c (revision 133) +++ libssh/sftp.c (working copy) @@ -25,7 +25,11 @@ #include #include #include +#ifdef _WIN32 +#include +#else #include +#endif #include "libssh/priv.h" #include "libssh/ssh2.h" #include "libssh/sftp.h" Index: libssh/sftpserver.c =================================================================== --- libssh/sftpserver.c (revision 133) +++ libssh/sftpserver.c (working copy) @@ -25,7 +25,11 @@ #include #include #include +#ifdef _WIN32 +#include +#else #include +#endif #include SFTP_CLIENT_MESSAGE *sftp_get_client_message(SFTP_SESSION *sftp){ Index: libssh/string.c =================================================================== --- libssh/string.c (revision 133) +++ libssh/string.c (working copy) @@ -21,7 +21,11 @@ MA 02111-1307, USA. */ #include +#ifdef _WIN32 +#include +#else #include +#endif #include #include #include "libssh/priv.h" Index: libssh/wrapper.c =================================================================== --- libssh/wrapper.c (revision 133) +++ libssh/wrapper.c (working copy) @@ -513,6 +513,10 @@ char *server=session->server_kex.methods[SSH_CRYPT_S_C]; char *client=session->client_kex.methods[SSH_CRYPT_S_C]; char *match=ssh_find_matching(client,server); + if(!match){ + ssh_set_error(session,SSH_FATAL,"Crypt_set_algorithms : no matching algorithm function found for %s",server); + return -1; + } while(ssh_ciphertab[i].name && strcmp(match,ssh_ciphertab[i].name)) i++; if(!ssh_ciphertab[i].name){ @@ -525,7 +529,11 @@ /* in */ client=session->client_kex.methods[SSH_CRYPT_C_S]; server=session->server_kex.methods[SSH_CRYPT_S_C]; - match=ssh_find_matching(client,server); + match=ssh_find_matching(client,server); + if(!match){ + ssh_set_error(session,SSH_FATAL,"Crypt_set_algorithms : no matching algorithm function found for %s",server); + return -1; + } while(ssh_ciphertab[i].name && strcmp(match,ssh_ciphertab[i].name)) i++; if(!ssh_ciphertab[i].name){ Index: Makefile.Windows =================================================================== --- Makefile.Windows (revision 0) +++ Makefile.Windows (revision 0) @@ -0,0 +1,33 @@ +CC= gcc +INCS= -I. -Iinclude -Ic:/progra~1/gnuwin32/include +CFLAGS= $(INCS) +LINK= -L. -Lc:/progra~1/gnuwin32/lib -lws2_32 -lcrypto -lgdi32 -lshell32 -lz + +libssh_HEADERS= config.h include/libssh/crypto.h include/libssh/libssh.h include/libssh/priv.h include/libssh/server.h include/libssh/sftp.h include/libssh/ssh1.h include/libssh/ssh2.h +libssh_OBJS = libssh/auth1.o libssh/auth.o libssh/base64.o libssh/buffer.o \ + libssh/channels1.o libssh/channels.o libssh/client.o libssh/connect.o \ + libssh/crc32.o libssh/crypt.o libssh/dh.o libssh/error.o libssh/gcrypt_missing.o \ + libssh/gzip.o libssh/init.o libssh/kex.o libssh/keyfiles.o \ + libssh/keys.o libssh/messages.o libssh/misc.o libssh/options.o \ + libssh/packet.o libssh/server.o libssh/session.o libssh/sftp.o \ + libssh/sftpserver.o libssh/string.o libssh/wrapper.o libssh/gai_strerror.o + + +all: libssh.dll samplesshd.exe + +config.h: config.h.win32-openssl + copy config.h.win32-openssl config.h + +%.o: %.c $(libssh_HEADERS) + $(CC) -c $< -o $@ $(CFLAGS) + +sample.exe: sample.o $(libssh_OBJS) + $(CC) $< -o $@ $(libssh_OBJS) $(LINK) +samplesshd.exe: samplesshd.o $(libssh_OBJS) + $(CC) $< -o $@ $(libssh_OBJS) $(LINK) + +libssh.dll: $(libssh_OBJS) + $(CC) -shared $(libssh_OBJS) -o libssh.dll $(LINK) + +clean: + rm -f $(libssh_OBJS) samplesshd.exe sample.exe libssh.dll config.h Index: samplesshd.c =================================================================== --- samplesshd.c (revision 133) +++ samplesshd.c (working copy) @@ -26,6 +26,15 @@ #include #include +#ifdef _WIN32 +#include +#define KEYS_FOLDER +#endif + +#ifndef KEYS_FOLDER +#define KEYS_FOLDER "/etc/ssh/" +#endif + int auth_password(char *user, char *password){ if(strcmp(user,"aris")) return 0; @@ -43,9 +52,19 @@ int auth=0; int sftp=0; int i; + +#ifdef _WIN32 + struct WSAData wsaData; + if (WSAStartup(MAKEWORD(2, 0), &wsaData)) { + printf("Error initialising Windows sockets.\n"); + return 1; + } +#endif + + ssh_options_getopt(options,&argc,argv); - ssh_options_set_dsa_server_key(options,"/etc/ssh/ssh_host_dsa_key"); - ssh_options_set_rsa_server_key(options,"/etc/ssh/ssh_host_rsa_key"); + ssh_options_set_dsa_server_key(options, KEYS_FOLDER "ssh_host_dsa_key"); + ssh_options_set_rsa_server_key(options, KEYS_FOLDER "ssh_host_rsa_key"); ssh_bind=ssh_bind_new(); ssh_bind_set_options(ssh_bind,options); if(ssh_bind_listen(ssh_bind)<0){ @@ -114,7 +133,7 @@ } while(message && !chan); if(!chan){ printf("error : %s\n",ssh_get_error(session)); - ssh_finalize(); + ssh_finalize(); return 1; } do { @@ -145,6 +164,10 @@ } while (i>0); ssh_disconnect(session); ssh_finalize(); + +#ifdef _WIN32 + WSACleanup(); +#endif return 0; }