for the networkers: a wrapper for libssh2

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

for the networkers: a wrapper for libssh2

Post by infratec »

Hi,

here a late chistmas present for the networkers of us:

Now you can connect to a server via ssh :!:

I wrote a wrapper for libssh2 1.3.0 (at the moment only for the windows dll)
I choose lbssh2, because of the license (BSD) and I don't need the server stuff.

Save this as libssh2.pbi:

Code: Select all

;
;
; libssh.pbi
;
; a wrapper for libssh2
;


CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Windows
    Global LibSSH2_Filename$ = "libssh2.dll"
    Macro OSPrototype
     PrototypeC
    EndMacro
  CompilerCase #PB_OS_Linux
    Global LibSSH2_Filename$ = "libssh2.so.1"
    Macro OSPrototype
     PrototypeC
    EndMacro
  CompilerCase #PB_OS_MacOS
    Global LibSSH2_Filename$ = "libssh2.so.1"
    Macro OSPrototype
     PrototypeC
    EndMacro
CompilerEndSelect 



; Default generate And safe prime sizes For diffie-hellman-group-exchange-sha1
#LIBSSH2_DH_GEX_MINGROUP = 1024
#LIBSSH2_DH_GEX_OPTGROUP = 1536
#LIBSSH2_DH_GEX_MAXGROUP = 2048

; Defaults For pty requests
#LIBSSH2_TERM_WIDTH = 80
#LIBSSH2_TERM_HEIGHT = 24
#LIBSSH2_TERM_WIDTH_PX = 0
#LIBSSH2_TERM_HEIGHT_PX = 0

; 1/4 second
#LIBSSH2_SOCKET_POLL_UDELAY = 250000
; 0.25 * 120 == 30 seconds
#LIBSSH2_SOCKET_POLL_MAXLOOPS = 120

; Maximum size To allow a payload To compress To, plays it safe by falling
; short of spec limits
#LIBSSH2_PACKET_MAXCOMP = 32000

; Maximum size To allow a payload To deccompress To, plays it safe by
; allowing more than spec requires
#LIBSSH2_PACKET_MAXDECOMP = 40000

; Maximum size For an inbound compressed payload, plays it safe by
; overshooting spec limits
#LIBSSH2_PACKET_MAXPAYLOAD = 40000

Structure LIBSSH2_USERAUTH_KBDINT_PROMPT_Str
  *text
  length.i
  echo.a
EndStructure
Define LIBSSH2_USERAUTH_KBDINT_PROMPT.LIBSSH2_USERAUTH_KBDINT_PROMPT_Str

Structure LIBSSH2_USERAUTH_KBDINT_RESPONSE_Str
  *text
  length.i
EndStructure
Define LIBSSH2_USERAUTH_KBDINT_RESPONSE.LIBSSH2_USERAUTH_KBDINT_RESPONSE_Str

; libssh2_session_callback_set() constants
#LIBSSH2_CALLBACK_IGNORE = 0
#LIBSSH2_CALLBACK_DEBUG = 1
#LIBSSH2_CALLBACK_DISCONNECT = 2
#LIBSSH2_CALLBACK_MACERROR = 3
#LIBSSH2_CALLBACK_X11 = 4
#LIBSSH2_CALLBACK_SEND = 5
#LIBSSH2_CALLBACK_RECV = 6


; libssh2_session_method_pref() constants
#LIBSSH2_METHOD_KEX = 0
#LIBSSH2_METHOD_HOSTKEY = 1
#LIBSSH2_METHOD_CRYPT_CS = 2
#LIBSSH2_METHOD_CRYPT_SC = 3
#LIBSSH2_METHOD_MAC_CS = 4
#LIBSSH2_METHOD_MAC_SC = 5
#LIBSSH2_METHOD_COMP_CS = 6
#LIBSSH2_METHOD_COMP_SC = 7
#LIBSSH2_METHOD_LANG_CS = 8
#LIBSSH2_METHOD_LANG_SC = 9 


; flags
#LIBSSH2_FLAG_SIGPIPE = 1
#LIBSSH2_FLAG_COMPRESS = 2





; Poll FD Descriptor Types
#LIBSSH2_POLLFD_SOCKET = 1
#LIBSSH2_POLLFD_CHANNEL = 2
#LIBSSH2_POLLFD_LISTENER = 3



; Poll FD events/revents -- Match sys/poll.h where possible
#LIBSSH2_POLLFD_POLLIN = $0001 ; Data available To be Read Or connection available -- All
#LIBSSH2_POLLFD_POLLPRI = $0002 ; Priority Data available To be Read -- Socket only
#LIBSSH2_POLLFD_POLLEXT = $0002 ; Extended Data available To be Read -- Channel only
#LIBSSH2_POLLFD_POLLOUT = $0004 ; Can may be written -- Socket/Channel
; revents only
#LIBSSH2_POLLFD_POLLERR = $0008 ; Error Condition -- Socket
#LIBSSH2_POLLFD_POLLHUP = $0010 ; HangUp/EOF -- Socket
#LIBSSH2_POLLFD_SESSION_CLOSED = $0010 ; Session Disconnect
#LIBSSH2_POLLFD_POLLNVAL = $0020 ; Invalid request -- Socket Only
#LIBSSH2_POLLFD_POLLEX = $0040 ; Exception Condition -- Socket/Win32
#LIBSSH2_POLLFD_CHANNEL_CLOSED = $0080 ; Channel Disconnect
#LIBSSH2_POLLFD_LISTENER_CLOSED = $0080 ; Listener Disconnect

#HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION = #True
; Block Direction Types
#LIBSSH2_SESSION_BLOCK_INBOUND = $0001
#LIBSSH2_SESSION_BLOCK_OUTBOUND = $0002 


; Hash Types
#LIBSSH2_HOSTKEY_HASH_MD5 = 1
#LIBSSH2_HOSTKEY_HASH_SHA1 = 2

; Hostkey Types
#LIBSSH2_HOSTKEY_TYPE_UNKNOWN	= 0
#LIBSSH2_HOSTKEY_TYPE_RSA	= 1
#LIBSSH2_HOSTKEY_TYPE_DSS	= 2

; Disconnect Codes (defined by SSH protocol)
#SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT = 1
#SSH_DISCONNECT_PROTOCOL_ERROR = 2
#SSH_DISCONNECT_KEY_EXCHANGE_FAILED = 3
#SSH_DISCONNECT_RESERVED = 4
#SSH_DISCONNECT_MAC_ERROR = 5
#SSH_DISCONNECT_COMPRESSION_ERROR = 6
#SSH_DISCONNECT_SERVICE_NOT_AVAILABLE = 7
#SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED = 8
#SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE = 9
#SSH_DISCONNECT_CONNECTION_LOST = 10
#SSH_DISCONNECT_BY_APPLICATION = 11
#SSH_DISCONNECT_TOO_MANY_CONNECTIONS = 12
#SSH_DISCONNECT_AUTH_CANCELLED_BY_USER = 13
#SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE = 14
#SSH_DISCONNECT_ILLEGAL_USER_NAME = 15 



; Error Codes (defined by libssh2)
#LIBSSH2_ERROR_NONE = 0

; The library once used -1 As a generic error Return value on numerous places
; through the code, which subsequently was converted To
; LIBSSH2_ERROR_SOCKET_NONE uses over time. As this is a generic error code,
; the goal is To never ever Return this code but instead make sure that a
; more accurate And descriptive error code is used.
#LIBSSH2_ERROR_SOCKET_NONE = -1
#LIBSSH2_ERROR_BANNER_RECV = -2
#LIBSSH2_ERROR_BANNER_SEND = -3
#LIBSSH2_ERROR_INVALID_MAC = -4
#LIBSSH2_ERROR_KEX_FAILURE = -5
#LIBSSH2_ERROR_ALLOC = -6
#LIBSSH2_ERROR_SOCKET_SEND = -7
#LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE = -8
#LIBSSH2_ERROR_TIMEOUT = -9
#LIBSSH2_ERROR_HOSTKEY_INIT = -10
#LIBSSH2_ERROR_HOSTKEY_SIGN = -11
#LIBSSH2_ERROR_DECRYPT = -12
#LIBSSH2_ERROR_SOCKET_DISCONNECT = -13
#LIBSSH2_ERROR_PROTO = -14
#LIBSSH2_ERROR_PASSWORD_EXPIRED = -15
#LIBSSH2_ERROR_FILE = -16
#LIBSSH2_ERROR_METHOD_NONE = -17
#LIBSSH2_ERROR_AUTHENTICATION_FAILED = -18
#LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED = #LIBSSH2_ERROR_AUTHENTICATION_FAILED
#LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED = -19
#LIBSSH2_ERROR_CHANNEL_OUTOFORDER = -20
#LIBSSH2_ERROR_CHANNEL_FAILURE = -21
#LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED = -22
#LIBSSH2_ERROR_CHANNEL_UNKNOWN = -23
#LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED = -24
#LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED = -25
#LIBSSH2_ERROR_CHANNEL_CLOSED = -26
#LIBSSH2_ERROR_CHANNEL_EOF_SENT = -27
#LIBSSH2_ERROR_SCP_PROTOCOL = -28
#LIBSSH2_ERROR_ZLIB = -29
#LIBSSH2_ERROR_SOCKET_TIMEOUT = -30
#LIBSSH2_ERROR_SFTP_PROTOCOL = -31
#LIBSSH2_ERROR_REQUEST_DENIED = -32
#LIBSSH2_ERROR_METHOD_NOT_SUPPORTED = -33
#LIBSSH2_ERROR_INVAL = -34
#LIBSSH2_ERROR_INVALID_POLL_TYPE = -35
#LIBSSH2_ERROR_PUBLICKEY_PROTOCOL = -36
#LIBSSH2_ERROR_EAGAIN = -37
#LIBSSH2_ERROR_BUFFER_TOO_SMALL = -38
#LIBSSH2_ERROR_BAD_USE = -39
#LIBSSH2_ERROR_COMPRESS = -40
#LIBSSH2_ERROR_OUT_OF_BOUNDARY = -41
#LIBSSH2_ERROR_AGENT_PROTOCOL = -42
#LIBSSH2_ERROR_SOCKET_RECV = -43
#LIBSSH2_ERROR_ENCRYPT = -44
#LIBSSH2_ERROR_BAD_SOCKET = -45
 

; this is a Define To provide the old (<= 1.2.7) name
#LIBSSH2_ERROR_BANNER_NONE = #LIBSSH2_ERROR_BANNER_RECV

; Global API
#LIBSSH2_INIT_NO_CRYPTO = $0001




; Channel API
#LIBSSH2_CHANNEL_WINDOW_DEFAULT = 65536
#LIBSSH2_CHANNEL_PACKET_DEFAULT = 32768
#LIBSSH2_CHANNEL_MINADJUST = 1024

; Extended Data Handling
#LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL = 0
#LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE = 1
#LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE = 2

#SSH_EXTENDED_DATA_STDERR = 1

; Returned by any function that would block during a Read/write opperation
#LIBSSH2CHANNEL_EAGAIN = #LIBSSH2_ERROR_EAGAIN

#LIBSSH2_CHANNEL_FLUSH_EXTENDED_DATA = -1
#LIBSSH2_CHANNEL_FLUSH_ALL = -2 



Structure LibSSH2_Knownhost_Str
  magic.i   ; magic stored by the library
  *node     ; handle to the internal representation of this host
  name$     ; this is NULL if no plain text host name exists
  key$      ; key in base64/printable format
  typemask.i
EndStructure


; host format (2 bits)
#LIBSSH2_KNOWNHOST_TYPE_MASK = $ffff
#LIBSSH2_KNOWNHOST_TYPE_PLAIN = 1
#LIBSSH2_KNOWNHOST_TYPE_SHA1 = 2 ; always base64 encoded
#LIBSSH2_KNOWNHOST_TYPE_CUSTOM = 3

; key format (2 bits)
#LIBSSH2_KNOWNHOST_KEYENC_MASK = (3 << 16)
#LIBSSH2_KNOWNHOST_KEYENC_RAW = (1 << 16)
#LIBSSH2_KNOWNHOST_KEYENC_BASE64 = (2 << 16)

; type of key (2 bits)
#LIBSSH2_KNOWNHOST_KEY_MASK = (3 << 18)
#LIBSSH2_KNOWNHOST_KEY_SHIFT = 18
#LIBSSH2_KNOWNHOST_KEY_RSA1 = (1 << 18)
#LIBSSH2_KNOWNHOST_KEY_SSHRSA = (2 << 18)
#LIBSSH2_KNOWNHOST_KEY_SSHDSS = (3 << 18)
 
#LIBSSH2_KNOWNHOST_CHECK_MATCH = 0
#LIBSSH2_KNOWNHOST_CHECK_MISMATCH = 1
#LIBSSH2_KNOWNHOST_CHECK_NOTFOUND = 2
#LIBSSH2_KNOWNHOST_CHECK_FAILURE = 3


Structure LibSSH2_Agent_Publickey_Str
  magic.i     ; magic stored by the library
  *node       ; handle to the internal representation of key
  *blob       ; public key blob
  blob_len.i  ; length of the public key blob
  comment$    ; comment in printable format
EndStructure

#LIBSSH2_TRACE_TRANS = (1 << 1)
#LIBSSH2_TRACE_KEX = (1 << 2)
#LIBSSH2_TRACE_AUTH = (1 << 3)
#LIBSSH2_TRACE_CONN = (1 << 4)
#LIBSSH2_TRACE_SCP = (1 << 5)
#LIBSSH2_TRACE_SFTP = (1 << 6)
#LIBSSH2_TRACE_ERROR = (1 << 7)
#LIBSSH2_TRACE_PUBLICKEY = (1 << 8)
#LIBSSH2_TRACE_SOCKET = (1 << 9)
 


;libssh2_publickey_add_ex
;libssh2_publickey_init
;libssh2_publickey_list_fetch
;libssh2_publickey_list_free
;libssh2_publickey_remove_ex
;libssh2_publickey_shutdown

;libssh2_sftp_close_handle
;libssh2_sftp_fstat_ex
;libssh2_sftp_fstatvfs
;libssh2_sftp_init
;libssh2_sftp_last_error
;libssh2_sftp_mkdir_ex
;libssh2_sftp_open_ex
;libssh2_sftp_read
;libssh2_sftp_readdir_ex
;libssh2_sftp_rename_ex
;libssh2_sftp_rmdir_ex
;libssh2_sftp_seek
;libssh2_sftp_seek64
;libssh2_sftp_shutdown
;libssh2_sftp_stat_ex
;libssh2_sftp_statvfs
;libssh2_sftp_symlink_ex
;libssh2_sftp_tell
;libssh2_sftp_tell64
;libssh2_sftp_unlink_ex
;libssh2_sftp_write




OSPrototype.i Proto_LibSSH2_Banner_Set(*session, banner.p-utf8)
OSPrototype.i Proto_LibSSH2_Base64_Decode(*session, *dest, *dest_len, *src, src_len.i)
OSPrototype Proto_LibSSH2_Exit()
OSPrototype Proto_LibSSH2_Free(*session, *ptr)
OSPrototype.i Proto_LibSSH2_Hostkey_Hash(*session, hash_type.i)
OSPrototype.i Proto_LibSSH2_Init(flags.i)
OSPrototype.i Proto_LibSSH2_Poll(*fds, nfds.i, timeout.l)
OSPrototype.i Proto_LibSSH2_Version(req_version_num.i)

OSPrototype.i Proto_LibSSH2_Agent_Connect(*agent)
OSPrototype.i Proto_LibSSH2_Agent_Disconnect(*agent)
OSPrototype Proto_LibSSH2_Agent_Free(*agent)
OSPrototype.i Proto_LibSSH2_Agent_Get_Identity(*agent, *identity, *prev_identity)
OSPrototype.i Proto_LibSSH2_Agent_Init(*session)
OSPrototype.i Proto_LibSSH2_Agent_List_Identities(*agent)
OSPrototype.i Proto_LibSSH2_Agent_Userauth(*agent, username.p-utf8, *identity)

OSPrototype.i Proto_LibSSH2_Channel_Close(*channel)
OSPrototype.i Proto_LibSSH2_Channel_Direct_TcpIp_Ex(*session, host.p-utf8, port.i, shost.p-utf8, sport.i)
OSPrototype.i Proto_LibSSH2_Channel_EOF(*channel)
OSPrototype.i Proto_LibSSH2_Channel_Flush_Ex(*channel, streamid.i)
OSPrototype.i Proto_LibSSH2_Channel_Forward_Accept(*listener)
OSPrototype.i Proto_LibSSH2_Channel_Forward_Cancel(*listener)
OSPrototype.i Proto_LibSSH2_Channel_Forward_Listen_Ex(*session, host.p-utf8, port.i, *bound_port, queue_maxsize.i)
OSPrototype.i Proto_LibSSH2_Channel_Free(*channel)
OSPrototype.i Proto_LibSSH2_Channel_Get_Exit_Signal(*channel, *exitsignal, *exitsignal_len, *errmsg, *errmsg_len, *langtag, *langtag_len)
OSPrototype.i Proto_LibSSH2_Channel_Get_Exit_Status(*channel)
OSPrototype.i Proto_LibSSH2_Channel_Handle_Extended_Data2(*channel, ignore_mode.i)
OSPrototype.i Proto_LibSSH2_Channel_Open_Ex(*session, channel_type.p-utf8, channel_type_len.i, window_size.i, packet_size.i, message.p-utf8, message_len.i)
OSPrototype.i Proto_LibSSH2_Poll_Channel_Read(*channel, extended.i)
OSPrototype.i Proto_LibSSH2_Channel_Process_Startup(*channel, request.p-utf8, request_len.i, message.p-utf8, message_len.i)
OSPrototype.i Proto_LibSSH2_Channel_Read_Ex(*channel, stream_id.i, *buf, buflen.i)
OSPrototype.i Proto_LibSSH2_Channel_Receive_Window_Adjust2(*channel, adjustment.l, force.a, *storewindow)
OSPrototype.i Proto_LibSSH2_Channel_Request_Pty_Ex(*channel, term.p-utf8, term_len.i, modes.p-utf8, modes_len.i, width.i, height.i, width_px.i, height_px.i)
OSPrototype.i Proto_LibSSH2_Channel_Request_Pty_Size_Ex(*channel, width.i, height.i, width_px.i, height_px.i)
OSPrototype.i Proto_LibSSH2_Channel_Send_EOF(*channel)
OSPrototype Proto_LibSSH2_Channel_Set_Blocking(*channel, blocking.i)
OSPrototype.i Proto_LibSSH2_Channel_Setenv_Ex(*channel, varname.p-utf8, varname_len.i, value.p-utf8, value_len.i)
OSPrototype.i Proto_LibSSH2_Channel_Wait_EOF(*channel)
OSPrototype.i Proto_LibSSH2_Channel_Wait_Closed(*channel)
OSPrototype.l Proto_LibSSH2_Channel_Window_Read_Ex(*channel, *read_avail, *window_size_initial)
OSPrototype.l Proto_LibSSH2_Channel_Window_Write_Ex(*channel, *window_size_initial)
OSPrototype.i Proto_LibSSH2_Channel_Write_Ex(*channel, stream_id.i, buf.p-utf8, buflen.i)
OSPrototype.i Proto_LibSSH2_Channel_X11_Req_Ex(*channel, single_connection.i, auth_proto$, auth_cookie$, screen_number.i)

OSPrototype Proto_LibSSH2_Keepalive_Config(*session, want_reply.i, interval.i)
OSPrototype.i Proto_LibSSH2_Keepalive_Send(*session, *seconds_to_next)

OSPrototype.i Proto_LibSSH2_Knownhost_Add(*hosts, host.p-utf8, salt.p-utf8, key.p-utf8, keylen.i, typemask.i, *store.LibSSH2_Knownhost_Str)
OSPrototype.i Proto_LibSSH2_Knownhost_Addc(*hosts, host.p-utf8, salt.p-utf8, key.p-utf8, keylen.i, comment.p-utf8, commentlen.i, typemask.i, *store.LibSSH2_Knownhost_Str)
OSPrototype.i Proto_LibSSH2_Knownhost_Check(*hosts, host.p-utf8, key.p-utf8, keylen.i, typemask.i, *knownhost.LibSSH2_Knownhost_Str)
OSPrototype.i Proto_LibSSH2_Knownhost_Checkp(*hosts, host.p-utf8, port.i, key.p-utf8, keylen.i, typemask.i, *knownhost.LibSSH2_Knownhost_Str)
OSPrototype.i Proto_LibSSH2_Knownhost_Del(*hosts, *entry.LibSSH2_Knownhost_Str)
OSPrototype Proto_LibSSH2_Knownhost_Free(*hosts)
OSPrototype.i Proto_LibSSH2_Knownhost_Get(*hosts, *store.LibSSH2_Knownhost_Str, *prev.LibSSH2_Knownhost_Str)
OSPrototype.i Proto_LibSSH2_Knownhost_Init(*session)
OSPrototype.i Proto_LibSSH2_Knownhost_Readfile(*hosts, filename.p-utf8, type.i)
OSPrototype.i Proto_LibSSH2_Knownhost_Readline(*hosts, *line, len.i, type.i)
OSPrototype.i Proto_LibSSH2_Knownhost_Writeline(*hosts, *known.LibSSH2_Knownhost_Str, *buffer, buflen.i, *outlen, type.i)
OSPrototype.i Proto_LibSSH2_Knownhost_Writefile(*hosts, filename.p-utf8, type.i)

OSPrototype.i Proto_LibSSH2_SCP_Recv(*session, path.p-utf8, *sb)
OSPrototype.i Proto_LibSSH2_SCP_Send64(*session, path.p-utf8, mode.i, size.q, mtime.i, atime.i)
OSPrototype.i Proto_LibSSH2_SCP_Send_Ex(*session, path.p-utf8, mode.i, size.i, mtime.l, atime.l)

OSPrototype.i Proto_LibSSH2_Session_Abstract(*session)
OSPrototype.i Proto_LibSSH2_Session_Block_Directions(*session)
OSPrototype.i Proto_LibSSH2_Session_Callback_Set(*session, cbtype.i, *callback)
OSPrototype.i Proto_LibSSH2_Session_Disconnect_Ex(*session, reason.i, description.p-utf8, lang.p-utf8)
OSPrototype.i Proto_LibSSH2_Session_Flag(*session, flag.i, value.i)
OSPrototype.i Proto_LibSSH2_Session_Free(*session)
OSPrototype.i Proto_LibSSH2_Session_Get_Blocking(*session)
OSPrototype.l Proto_LibSSH2_Session_Get_Timeout(*session)
OSPrototype.i Proto_LibSSH2_Session_Handshake(*session, *sock)
OSPrototype.i Proto_LibSSH2_Session_Hostkey(*session, *len, *type)
OSPrototype.i Proto_LibSSH2_Session_Init_Ex(*alloc, *free, *realloc, *abstract)
OSPrototype.i Proto_LibSSH2_Session_Last_Errno(*session)
OSPrototype.i Proto_LibSSH2_Session_Last_Error(*session, *errmsg, *errmsg_len, want_buf.i)
OSPrototype.i Proto_LibSSH2_Session_Method_Pref(*session, method_type.i, prefs.p-utf8)
OSPrototype.i Proto_LibSSH2_Session_Methods(*session, method_type.i)
OSPrototype Proto_LibSSH2_Session_Set_Blocking(*session, blocking.i)
OSPrototype Proto_LibSSH2_Session_Set_Timeout(*session, timeout.l)
OSPrototype.i Proto_LibSSH2_Session_Startup(*session, sock.i)

OSPrototype.i Proto_LibSSH2_Trace(*session, bitmask.i)
OSPrototype.i Proto_LibSSH2_Trace_Sethandler(*session, *context, *callback)

OSPrototype.i Proto_LibSSH2_Userauth_Authenticated(*session)
OSPrototype.i Proto_LibSSH2_Userauth_Hostbased_Fromfile_Ex(*session, username.p-utf8, username_len.i, publickey.p-utf8, privatekey.p-utf8, passphrase.p-utf8, hostname.p-utf8, hostname_len.i, local_username.p-utf8, local_username_len.i)
OSPrototype.i Proto_LibSSH2_Userauth_Keyboard_Interactive_Ex(*session, username$, username_len.i, *response_callback)
OSPrototype.i Proto_LibSSH2_Userauth_List(*session, username.p-utf8, username_len.i)
OSPrototype.i Proto_LibSSH2_Userauth_Password_Ex(*session, username.p-utf8, username_len.i, password.p-utf8, password_len.i, *passwd_change_cb)
OSPrototype.i Proto_LibSSH2_Userauth_Publickey(*session, username.p-utf8, pubkeydata.p-utf8, pubkeydata_len.i, *sign_callback, *abstract)
OSPrototype.i Proto_LibSSH2_Userauth_Publickey_Fromfile_Ex(*session, username.p-utf8, username_len.i, publickey.p-utf8, privatekey.p-utf8, passphrase.p-utf8)




Global LibSSH2_Banner_Set.Proto_LibSSH2_Banner_Set
Global LibSSH2_Base64_Decode.Proto_LibSSH2_Base64_Decode
Global LibSSH2_Exit.Proto_LibSSH2_Exit
Global LibSSH2_Free.Proto_LibSSH2_Free
Global LibSSH2_Hostkey_Hash.Proto_LibSSH2_Hostkey_Hash
Global LibSSH2_Init.Proto_LibSSH2_Init
Global LibSSH2_Poll.Proto_LibSSH2_Poll
Global LibSSH2_Version.Proto_LibSSH2_Version

Global LibSSH2_Agent_Connect.Proto_LibSSH2_Agent_Connect
Global LibSSH2_Agent_Disconnect.Proto_LibSSH2_Agent_Disconnect
Global LibSSH2_Agent_Free.Proto_LibSSH2_Agent_Free
Global LibSSH2_Agent_Get_Identity.Proto_LibSSH2_Agent_Get_Identity
Global LibSSH2_Agent_Init.Proto_LibSSH2_Agent_Init
Global LibSSH2_Agent_List_Identities.Proto_LibSSH2_Agent_List_Identities
Global LibSSH2_Agent_Userauth.Proto_LibSSH2_Agent_Userauth

Global LibSSH2_Channel_Close.Proto_LibSSH2_Channel_Close
Global LibSSH2_Channel_Direct_TcpIp_Ex.Proto_LibSSH2_Channel_Direct_TcpIp_Ex
Global LibSSH2_Channel_EOF.Proto_LibSSH2_Channel_EOF
Global LibSSH2_Channel_Flush_Ex.Proto_LibSSH2_Channel_Flush_Ex
Global LibSSH2_Channel_Forward_Accept.Proto_LibSSH2_Channel_Forward_Accept
Global LibSSH2_Channel_Forward_Cancel.Proto_LibSSH2_Channel_Forward_Cancel
Global LibSSH2_Channel_Forward_Listen_Ex.Proto_LibSSH2_Channel_Forward_Listen_Ex
Global LibSSH2_Channel_Free.Proto_LibSSH2_Channel_Free
Global LibSSH2_Channel_Get_Exit_Signal.Proto_LibSSH2_Channel_Get_Exit_Signal
Global LibSSH2_Channel_Get_Exit_Status.Proto_LibSSH2_Channel_Get_Exit_Status
Global LibSSH2_Channel_Handle_Extended_Data2.Proto_LibSSH2_Channel_Handle_Extended_Data2
Global LibSSH2_Channel_Open_Ex.Proto_LibSSH2_Channel_Open_Ex
Global LibSSH2_Poll_Channel_Read.Proto_LibSSH2_Poll_Channel_Read
Global LibSSH2_Channel_Process_Startup.Proto_LibSSH2_Channel_Process_Startup
Global LibSSH2_Channel_Read_Ex.Proto_LibSSH2_Channel_Read_Ex
Global LibSSH2_Channel_Receive_Window_Adjust2.Proto_LibSSH2_Channel_Receive_Window_Adjust2
Global LibSSH2_Channel_Request_Pty_Ex.Proto_LibSSH2_Channel_Request_Pty_Ex
Global LibSSH2_Channel_Request_Pty_Size_Ex.Proto_LibSSH2_Channel_Request_Pty_Size_Ex
Global LibSSH2_Channel_Send_EOF.Proto_LibSSH2_Channel_Send_EOF
Global LibSSH2_Channel_Set_Blocking.Proto_LibSSH2_Channel_Set_Blocking
Global LibSSH2_Channel_Setenv_Ex.Proto_LibSSH2_Channel_Setenv_Ex
Global LibSSH2_Channel_Wait_EOF.Proto_LibSSH2_Channel_Wait_EOF
Global LibSSH2_Channel_Wait_Closed.Proto_LibSSH2_Channel_Wait_Closed
Global LibSSH2_Channel_Window_Read_Ex.Proto_LibSSH2_Channel_Window_Read_Ex
Global LibSSH2_Channel_Window_Write_Ex.Proto_LibSSH2_Channel_Window_Write_Ex
Global LibSSH2_Channel_Write_Ex.Proto_LibSSH2_Channel_Write_Ex
Global LibSSH2_Channel_X11_Req_Ex.Proto_LibSSH2_Channel_X11_Req_Ex

Global LibSSH2_Keepalive_Config.Proto_LibSSH2_Keepalive_Config
Global LibSSH2_Keepalive_Send.Proto_LibSSH2_Keepalive_Send

Global LibSSH2_Knownhost_Add.Proto_LibSSH2_Knownhost_Add
Global LibSSH2_Knownhost_Addc.Proto_LibSSH2_Knownhost_Addc
Global LibSSH2_Knownhost_Check.Proto_LibSSH2_Knownhost_Check
Global LibSSH2_Knownhost_Checkp.Proto_LibSSH2_Knownhost_Checkp
Global LibSSH2_Knownhost_Del.Proto_LibSSH2_Knownhost_Del
Global LibSSH2_Knownhost_Free.Proto_LibSSH2_Knownhost_Free
Global LibSSH2_Knownhost_Get.Proto_LibSSH2_Knownhost_Get
Global LibSSH2_Knownhost_Init.Proto_LibSSH2_Knownhost_Init
Global LibSSH2_Knownhost_Readline.Proto_LibSSH2_Knownhost_Readline
Global LibSSH2_Knownhost_Readfile.Proto_LibSSH2_Knownhost_Readfile
Global LibSSH2_Knownhost_Writefile.Proto_LibSSH2_Knownhost_Writefile
Global LibSSH2_Knownhost_Writeline.Proto_LibSSH2_Knownhost_Writeline

Global LibSSH2_SCP_Recv.Proto_LibSSH2_SCP_Recv
Global LibSSH2_SCP_Send64.Proto_LibSSH2_SCP_Send64
Global LibSSH2_SCP_Send_Ex.Proto_LibSSH2_SCP_Send_Ex

Global LibSSH2_Session_Abstract.Proto_LibSSH2_Session_Abstract
Global LibSSH2_Session_Block_Directions.Proto_LibSSH2_Session_Block_Directions
Global LibSSH2_Session_Callback_Set.Proto_LibSSH2_Session_Callback_Set
Global LibSSH2_Session_Disconnect_Ex.Proto_LibSSH2_Session_Disconnect_Ex
Global LibSSH2_Session_Flag.Proto_LibSSH2_Session_Flag
Global LibSSH2_Session_Free.Proto_LibSSH2_Session_Free
Global LibSSH2_Session_Get_Blocking.Proto_LibSSH2_Session_Get_Blocking
Global LibSSH2_Session_Get_Timeout.Proto_LibSSH2_Session_Get_Timeout
Global LibSSH2_Session_Handshake.Proto_LibSSH2_Session_Handshake
Global LibSSH2_Session_Hostkey.Proto_LibSSH2_Session_Hostkey
Global LibSSH2_Session_Init_Ex.Proto_LibSSH2_Session_Init_Ex
Global LibSSH2_Session_Last_Errno.Proto_LibSSH2_Session_Last_Errno
Global LibSSH2_Session_Last_Error.Proto_LibSSH2_Session_Last_Error
Global LibSSH2_Session_Method_Pref.Proto_LibSSH2_Session_Method_Pref
Global LibSSH2_Session_Methods.Proto_LibSSH2_Session_Methods
Global LibSSH2_Session_Set_Blocking.Proto_LibSSH2_Session_Set_Blocking
Global LibSSH2_Session_Set_Timeout.Proto_LibSSH2_Session_Set_Timeout
Global LibSSH2_Session_Startup.Proto_LibSSH2_Session_Startup

Global LibSSH2_Trace.Proto_LibSSH2_Trace
Global LibSSH2_Trace_Sethandler.Proto_LibSSH2_Trace_Sethandler

Global LibSSH2_Userauth_Authenticated.Proto_LibSSH2_Userauth_Authenticated
Global LibSSH2_Userauth_Hostbased_Fromfile_Ex.Proto_LibSSH2_Userauth_Hostbased_Fromfile_Ex
Global LibSSH2_Userauth_Keyboard_Interactive_Ex.Proto_LibSSH2_Userauth_Keyboard_Interactive_Ex
Global LibSSH2_Userauth_List.Proto_LibSSH2_Userauth_List
Global LibSSH2_Userauth_Password_Ex.Proto_LibSSH2_Userauth_Password_Ex
Global LibSSH2_Userauth_Publickey_Fromfile_Ex.Proto_LibSSH2_Userauth_Publickey_Fromfile_Ex
Global LibSSH2_Userauth_Publickey.Proto_LibSSH2_Userauth_Publickey




Macro LibSSH2_Channel_Direct_TcpIp(session, host, port)
  LibSSH2_Channel_Direct_TcpIp_Ex(session, host, port, "127.0.0.1", 22)
EndMacro

Macro LibSSH2_Channel_Exec(channel, command)
  LibSSH2_Channel_Process_Startup(channel, "exec", StringByteLength("exec", #PB_UTF8), command, StringByteLength(command, #PB_UTF8))
EndMacro

Macro LibSSH2_Channel_Flush(channel)
  LibSSH2_Channel_Flush_Ex(channel, 0)
EndMacro

Macro LibSSH2_Channel_Flush_StdErr(channel)
  LibSSH2_Channel_Flush_Ex(channel, #SSH_EXTENDED_DATA_STDERR)
EndMacro

Macro LibSSH2_Channel_Forward_Listen(session, port)
  LibSSH2_Channel_Forward_Listen_Ex(session, #Null, port, #Null, 16)
EndMacro

Macro LibSSH2_Channel_Open_Session(session)
  LibSSH2_Channel_Open_Ex(session, "session", StringByteLength("session", #PB_UTF8), #LIBSSH2_CHANNEL_WINDOW_DEFAULT, #LIBSSH2_CHANNEL_PACKET_DEFAULT, #Null$, 0)
EndMacro

Macro LibSSH2_Channel_Read(channel, buf, buflen)
  LibSSH2_Channel_Read_Ex(channel, 0, buf, buflen)
EndMacro

Macro LibSSH2_Channel_Read_StdErr(channel, buf, buflen)
  LibSSH2_Channel_Read_Ex(channel, #SSH_EXTENDED_DATA_STDERR, buf, buflen)
EndMacro

Macro LibSSH2_Channel_Request_Pty(channel, term)
  LibSSH2_Channel_Request_Pty_Ex(channel, term, Len(term), #Null$, 0, #LIBSSH2_TERM_WIDTH, #LIBSSH2_TERM_HEIGHT, #LIBSSH2_TERM_WIDTH_PX, #LIBSSH2_TERM_HEIGHT_PX)
EndMacro

Macro LibSSH2_Channel_Request_Pty_Size(channel, width, height)
  LibSSH2_Channel_Request_Pty_Size_Ex(channel, width, height, 0, 0)
EndMacro

Macro LibSSH2_Channel_Setenv(channel, varname, value)
  LibSSH2_Channel_Setenv_Ex(channel, varname, Len(varname), value, Len(value))
EndMacro

Macro LibSSH2_Channel_Shell(channel)
  LibSSH2_Channel_Process_Startup(channel, "shell", Len("shell"), #Null$, 0)
EndMacro

Macro LibSSH2_Channel_Subsystem(channel, subsystem)
  LibSSH2_Channel_Process_Startup(channel, "subsystem", Len("subsystem"), subsystem, Len(subsystem))
EndMacro

Macro LibSSH2_Channel_Window_Read(channel)
  LibSSH2_Channel_Window_Read_Ex(channel, #Null, #Null)
EndMacro

Macro LibSSH2_Channel_Window_Write(channel)
  LibSSH2_Channel_Window_Write_Ex(channel, #Null)
EndMacro

Macro LibSSH2_Channel_Write(channel, buf, buflen)
  LibSSH2_Channel_Write_Ex(channel, 0, buf, buflen)
EndMacro

Macro LibSSH2_Channel_Write_StdErr(channel, buf, buflen)
  LibSSH2_Channel_Write_Ex(channel, #SSH_EXTENDED_DATA_STDERR, buf, buflen)
EndMacro

Macro LibSSH2_Channel_X11_Req(channel, screen_number)
  LibSSH2_Channel_X11_Req_Ex(channel, 0, #Null, #Null, screen_number)
EndMacro


Macro LibSSH2_SCP_Send(session, path, mode, size)
  LibSSH2_SCP_Send_Ex(session, path, mode, size, 0, 0)
EndMacro


Macro LibSSH2_Session_Disconnect(session, description)
  LibSSH2_Session_Disconnect_Ex(session, #SSH_DISCONNECT_BY_APPLICATION, description, #Null$)
EndMacro

Macro LibSSH2_Session_Init()
  LibSSH2_Session_Init_Ex(#Null, #Null, #Null, #Null)
EndMacro


Macro LibSSH2_Userauth_Hostbased_Fromfile(session, username, publickey, privatekey, passphrase, hostname)
  LibSSH2_Userauth_Hostbased_Fromfile_Ex(session, username, Len(username), publickey, privatekey, passphrase, hostname), Len(hostname), username, Len(username))  
EndMacro

Macro LibSSH2_Userauth_Keyboard_Interactive(session, username, response_callback)
  LibSSH2_Userauth_Keyboard_Interactive_Ex(session, username, Len(username), response_callback)
EndMacro

Macro LibSSH2_Userauth_Password(session, username, password)
 LibSSH2_Userauth_Password_Ex(session, username, StringByteLength(username, #PB_UTF8), password, StringByteLength(password, #PB_UTF8), #Null)
EndMacro

Macro LibSSH2_Userauth_Publickey_Fromfile(session, username, publickey, privatekey, passphrase)
  LibSSH2_Userauth_Publickey_Fromfile_Ex(session, username, Len(username), publickey, privatekey, passphrase)
EndMacro




Procedure.i LibSSH2_LibInit()
  
  Result = #False
  
  LibSSH2 = OpenLibrary(#PB_Any, LibSSH2_Filename$)
  If LibSSH2
    LibSSH2_Banner_Set = GetFunction(LibSSH2, "libssh2_banner_set")
    LibSSH2_Base64_Decode = GetFunction(LibSSH2, "libssh2_base64_decode")
    LibSSH2_Exit = GetFunction(LibSSH2, "libssh2_exit")
    LibSSH2_Free = GetFunction(LibSSH2, "libssh2_free")
    LibSSH2_Hostkey_Hash = GetFunction(LibSSH2, "libssh2_hostkey_hash")
    LibSSH2_Init = GetFunction(LibSSH2, "libssh2_init")
    LibSSH2_Poll = GetFunction(LibSSH2, "libssh2_poll")
    LibSSH2_Version = GetFunction(LibSSH2, "libssh2_version")
    
    LibSSH2_Agent_Connect = GetFunction(LibSSH2, "libssh2_agent_connect")
    LibSSH2_Agent_Disconnect = GetFunction(LibSSH2, "libssh2_agent_disconnect")
    LibSSH2_Agent_Free = GetFunction(LibSSH2, "libssh2_agent_free")
    LibSSH2_Agent_Get_Identity = GetFunction(LibSSH2, "libssh2_agent_get_identity")
    LibSSH2_Agent_Init = GetFunction(LibSSH2, "libssh2_agent_init")
    LibSSH2_Agent_List_Identities = GetFunction(LibSSH2, "libssh2_agent_list_identities")
    LibSSH2_Agent_Userauth = GetFunction(LibSSH2, "libssh2_agent_userauth")
    
    LibSSH2_Channel_Close = GetFunction(LibSSH2, "libssh2_channel_close")
    LibSSH2_Channel_Direct_TcpIp_Ex = GetFunction(LibSSH2, "libssh2_channel_direct_tcpip_ex")
    LibSSH2_Channel_EOF = GetFunction(LibSSH2, "libssh2_channel_eof")
    LibSSH2_Channel_Flush_Ex = GetFunction(LibSSH2, "libssh2_channel_flush_ex")
    LibSSH2_Channel_Forward_Accept = GetFunction(LibSSH2, "libssh2_channel_forward_accept")
    LibSSH2_Channel_Forward_Cancel = GetFunction(LibSSH2, "libssh2_channel_forward_cancel")
    LibSSH2_Channel_Forward_Listen_Ex = GetFunction(LibSSH2, "libssh2_channel_forward_listen_ex")
    LibSSH2_Channel_Free = GetFunction(LibSSH2, "libssh2_channel_free")
    LibSSH2_Channel_Get_Exit_Signal = GetFunction(LibSSH2, "libssh2_channel_get_exit_signal")
    LibSSH2_Channel_Get_Exit_Status = GetFunction(LibSSH2, "libssh2_channel_get_exit_status")
    LibSSH2_Channel_Handle_Extended_Data2 = GetFunction(LibSSH2, "libssh2_channel_handle_extended_data2")
    LibSSH2_Channel_Open_Ex = GetFunction(LibSSH2, "libssh2_channel_open_ex")
    LibSSH2_Poll_Channel_Read = GetFunction(LibSSH2, "libssh2_poll_channel_read")
    LibSSH2_Channel_Process_Startup = GetFunction(LibSSH2, "libssh2_channel_process_startup")
    LibSSH2_Channel_Read_Ex = GetFunction(LibSSH2, "libssh2_channel_read_ex")
    LibSSH2_Channel_Receive_Window_Adjust2 = GetFunction(LibSSH2, "libssh2_channel_receive_window_adjust2")
    LibSSH2_Channel_Request_Pty_Ex = GetFunction(LibSSH2, "libssh2_channel_request_pty_ex")
    LibSSH2_Channel_Request_Pty_Size_Ex = GetFunction(LibSSH2, "libssh2_channel_request_pty_size_ex")
    LibSSH2_Channel_Send_EOF = GetFunction(LibSSH2, "libssh2_channel_send_eof")
    LibSSH2_Channel_Set_Blocking = GetFunction(LibSSH2, "libssh2_channel_set_blocking")
    LibSSH2_Channel_Setenv_Ex = GetFunction(LibSSH2, "libssh2_channel_setenv_ex")
    LibSSH2_Channel_Wait_EOF = GetFunction(LibSSH2, "libssh2_channel_wait_eof")
    LibSSH2_Channel_Wait_Closed = GetFunction(LibSSH2, "libssh2_channel_wait_closed")
    LibSSH2_Channel_Window_Read_Ex = GetFunction(LibSSH2, "libssh2_channel_window_read_ex")
    LibSSH2_Channel_Window_Write_Ex = GetFunction(LibSSH2, "libssh2_channel_window_write_ex")
    LibSSH2_Channel_Write_Ex = GetFunction(LibSSH2, "libssh2_channel_write_ex")
    LibSSH2_Channel_X11_Req_Ex = GetFunction(LibSSH2, "libssh2_channel_x11_req_ex")
    
    LibSSH2_Keepalive_Config = GetFunction(LibSSH2, "libssh2_keepalive_config")
    LibSSH2_Keepalive_Send = GetFunction(LibSSH2, "libssh2_keepalive_send")
    
    LibSSH2_Knownhost_Add = GetFunction(LibSSH2, "libssh2_knownhost_add")
    LibSSH2_Knownhost_Addc = GetFunction(LibSSH2, "libssh2_knownhost_addc")
    LibSSH2_Knownhost_Check = GetFunction(LibSSH2, "libssh2_knownhost_check")
    LibSSH2_Knownhost_Checkp = GetFunction(LibSSH2, "libssh2_knownhost_checkp")
    LibSSH2_Knownhost_Del = GetFunction(LibSSH2, "libssh2_knownhost_del")
    LibSSH2_Knownhost_Free = GetFunction(LibSSH2, "libssh2_knownhost_free")
    LibSSH2_Knownhost_Get = GetFunction(LibSSH2, "libssh2_knownhost_get")
    LibSSH2_Knownhost_Init = GetFunction(LibSSH2, "libssh2_knownhost_init")
    LibSSH2_Knownhost_Readfile = GetFunction(LibSSH2, "libssh2_knownhost_readfile")
    LibSSH2_Knownhost_Readline = GetFunction(LibSSH2, "libssh2_knownhost_readline")
    LibSSH2_Knownhost_Writefile = GetFunction(LibSSH2, "libssh2_knownhost_writefile")
    LibSSH2_Knownhost_Writeline = GetFunction(LibSSH2, "libssh2_knownhost_writeline")
    
    LibSSH2_SCP_Recv = GetFunction(LibSSH2, "libssh2_scp_recv")
    LibSSH2_SCP_Send64 = GetFunction(LibSSH2, "libssh2_scp_send64")
    LibSSH2_SCP_Send_Ex = GetFunction(LibSSH2, "libssh2_scp_send_ex")
    
    LibSSH2_Session_Abstract = GetFunction(LibSSH2, "libssh2_session_abstract")
    LibSSH2_Session_Block_Directions = GetFunction(LibSSH2, "libssh2_session_block_directions")
    LibSSH2_Session_Callback_Set = GetFunction(LibSSH2, "libssh2_session_callback_set")
    LibSSH2_Session_Disconnect_Ex = GetFunction(LibSSH2, "libssh2_session_disconnect_ex")
    LibSSH2_Session_Flag = GetFunction(LibSSH2, "libssh2_session_flag")
    LibSSH2_Session_Free = GetFunction(LibSSH2, "libssh2_session_free")
    LibSSH2_Session_Get_Blocking = GetFunction(LibSSH2, "libssh2_session_get_blocking")
    LibSSH2_Session_Get_Timeout = GetFunction(LibSSH2, "libssh2_session_get_timeout")
    LibSSH2_Session_Handshake = GetFunction(LibSSH2, "libssh2_session_handshake")
    LibSSH2_Session_Hostkey = GetFunction(LibSSH2, "libssh2_session_hostkey")
    LibSSH2_Session_Init_Ex = GetFunction(LibSSH2, "libssh2_session_init_ex")
    LibSSH2_Session_Last_Errno = GetFunction(LibSSH2, "libssh2_session_last_errno")
    LibSSH2_Session_Last_Error = GetFunction(LibSSH2, "libssh2_session_last_error")
    LibSSH2_Session_Method_Pref = GetFunction(LibSSH2, "libssh2_session_method_pref")
    LibSSH2_Session_Methods = GetFunction(LibSSH2, "libssh2_session_methods")
    LibSSH2_Session_Set_Blocking = GetFunction(LibSSH2, "libssh2_session_set_blocking")
    LibSSH2_Session_Set_Timeout = GetFunction(LibSSH2, "libssh2_session_set_timeout")
    LibSSH2_Session_Startup = GetFunction(LibSSH2, "libssh2_session_startup")
    
    LibSSH2_Trace = GetFunction(LibSSH2, "libssh2_trace")
    LibSSH2_Trace_Sethandler = GetFunction(LibSSH2, "libssh2_trace_sethandler")
    
    LibSSH2_Userauth_Authenticated = GetFunction(LibSSH2, "libssh2_userauth_authenticated")
    LibSSH2_Userauth_Hostbased_Fromfile_Ex = GetFunction(LibSSH2, "libssh2_userauth_hostbased_fromfile_ex")
    LibSSH2_Userauth_Keyboard_Interactive_Ex = GetFunction(LibSSH2, "libssh2_userauth_keyboard_interactive_ex")
    LibSSH2_Userauth_List = GetFunction(LibSSH2, "libssh2_userauth_list")
    LibSSH2_Userauth_Password_Ex = GetFunction(LibSSH2, "libssh2_userauth_password_ex")
    LibSSH2_Userauth_Publickey_Fromfile_Ex = GetFunction(LibSSH2, "libssh2_userauth_publickey_fromfile_ex")
    LibSSH2_Userauth_Publickey = GetFunction(LibSSH2, "libssh2_userauth_publickey")
    
    Result = #True
  EndIf
  
  ProcedureReturn Result
  
EndProcedure


Procedure LibSSH2_LibFree()
  If IsLibrary(LibSSH2) : CloseLibrary(LibSSH2) : EndIf
  LibSSH2 = 0
EndProcedure


Procedure.s LibSSH2_ErrorToString(ErrorNo)
  
  Result$ = ""
  
  Select ErrorNo
    Case 0 : Result$ = "NO ERROR"
    Case #LIBSSH2_ERROR_SOCKET_NONE : Result$ = "SOCKET NONE"
    Case #LIBSSH2_ERROR_BANNER_RECV : Result$ = "BANNER RECV"
    Case #LIBSSH2_ERROR_BANNER_SEND : Result$ = "BANNER SEND"
    Case #LIBSSH2_ERROR_INVALID_MAC : Result$ = "INVALID MAC"
    Case #LIBSSH2_ERROR_KEX_FAILURE : Result$ = "KEX FAILURE"
    Case #LIBSSH2_ERROR_ALLOC : Result$ = "ALLOC"
    Case #LIBSSH2_ERROR_SOCKET_SEND : Result$ = "SOCKET SEND"
    Case #LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE : Result$ = "KEY EXCHANGE FAILURE"
    Case #LIBSSH2_ERROR_TIMEOUT : Result$ = "TIMEOUT"
    Case #LIBSSH2_ERROR_HOSTKEY_INIT : Result$ = "HOSTKEY INIT"
    Case #LIBSSH2_ERROR_HOSTKEY_SIGN : Result$ = "HOSTKEY SIGN"
    Case #LIBSSH2_ERROR_DECRYPT : Result$ = "DECRYPT"
    Case #LIBSSH2_ERROR_SOCKET_DISCONNECT : Result$ = "SOCKET DISCONNECT"
    Case #LIBSSH2_ERROR_PROTO : Result$ = "PROTO"
    Case #LIBSSH2_ERROR_PASSWORD_EXPIRED: Result$ = "PASSWORD EXPIRED"
    Case #LIBSSH2_ERROR_FILE : Result$ = "FILE"
    Case #LIBSSH2_ERROR_METHOD_NONE: Result$ = "METHOD NONE"
    Case #LIBSSH2_ERROR_AUTHENTICATION_FAILED : Result$ = "AUTHENTICATION FAILED"
    Case #LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED : Result$ = "PUBLICKEY UNVERIFIED"
    Case #LIBSSH2_ERROR_CHANNEL_OUTOFORDER : Result$ = "CHANNEL OUTOFORDER"
    Case #LIBSSH2_ERROR_CHANNEL_FAILURE : Result$ = "CHANNEL FAILURE"
    Case #LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED : Result$ = "CHANNEL REQUEST DENIED"
    Case #LIBSSH2_ERROR_CHANNEL_UNKNOWN : Result$ = "CHANNEL UNKNOWN"
    Case #LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED : Result$ = "CHANNEL WINDOW EXCEEDED"
    Case #LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED : Result$ = "CHANNEL PACKET EXCEEDED"
    Case #LIBSSH2_ERROR_CHANNEL_CLOSED: Result$ = "CHANNEL CLOSED"
    Case #LIBSSH2_ERROR_CHANNEL_EOF_SENT : Result$ = "CHANNEL EOF SENT"
    Case #LIBSSH2_ERROR_SCP_PROTOCOL : Result$ = "SCP PROTOCOL"
    Case #LIBSSH2_ERROR_ZLIB : Result$ = "ZLIB"
    Case #LIBSSH2_ERROR_SOCKET_TIMEOUT : Result$ = "SOCKET TIMEOUT"
    Case #LIBSSH2_ERROR_SFTP_PROTOCOL : Result$ = "SFTP PROTOCOL"
    Case #LIBSSH2_ERROR_REQUEST_DENIED : Result$ = "REQUEST DENIED"
    Case #LIBSSH2_ERROR_METHOD_NOT_SUPPORTED : Result$ = "METHOD NOT SUPPORTED"
    Case #LIBSSH2_ERROR_INVAL : Result$ = "INVAL"
    Case #LIBSSH2_ERROR_INVALID_POLL_TYPE : Result$ = "INVALID POLL TYPE"
    Case #LIBSSH2_ERROR_PUBLICKEY_PROTOCOL : Result$ = "PUBLICKEY PROTOCOL"
    Case #LIBSSH2_ERROR_EAGAIN : Result$ = "EAGAIN"
    Case #LIBSSH2_ERROR_BUFFER_TOO_SMALL : Result$ = "BUFFER TOO SMALL"
    Case #LIBSSH2_ERROR_BAD_USE : Result$ = "BAD USE"
    Case #LIBSSH2_ERROR_COMPRESS : Result$ = "COMPRESS"
    Case #LIBSSH2_ERROR_OUT_OF_BOUNDARY : Result$ = "OUT OF BOUNDARY"
    Case #LIBSSH2_ERROR_AGENT_PROTOCOL : Result$ = "AGENT PROTOCOL"
    Case #LIBSSH2_ERROR_SOCKET_RECV : Result$ = "SOCKET RECV"
    Case #LIBSSH2_ERROR_ENCRYPT : Result$ = "ENCRYPT"
    Case #LIBSSH2_ERROR_BAD_SOCKET : Result$ = "BAD SOCKET"
    Default : Result$ = "UNKNOWN"
  EndSelect
  
  ProcedureReturn Result$
  
EndProcedure
Oh, it is not fully tested.
And the commented functions (publickey and sftp) are not yet implemented.

Here a working example (ps ax|grep psd on a linux server):

Code: Select all

IncludeFile "libssh2.pbi"

Server$ = "192.168.0.1"
Username$ = "user"
Password$ = "password"

InitNetwork()

Sock = OpenNetworkConnection(Server$, 22, #PB_Network_TCP)
If Sock = 0 : End : EndIf

If LibSSH2_LibInit()
  If LibSSH2_Init(0) = 0
    *Session = LibSSH2_Session_Init()
    If *Session
      rc = LibSSH2_Session_Startup(*Session, ConnectionID(Sock))
      Debug "Session Startup: " + LibSSH2_ErrorToString(rc)
      
      *UserAuthList = LibSSH2_Userauth_List(*Session, Username$, StringByteLength(Username$, #PB_UTF8))
      Debug PeekS(*UserAuthList, -1, #PB_UTF8)
      
      rc = libssh2_userauth_password(*Session, Username$, Password$)
      Debug "UserAuth: " + LibSSH2_ErrorToString(rc)
      
      *Channel = LibSSH2_Channel_Open_Session(*Session)
      
      rc = LibSSH2_Channel_Request_Pty(*Channel, "xterm")
      Debug "Channel Request Pty: " + LibSSH2_ErrorToString(rc)
      
            
      ;rc = LibSSH2_Channel_Shell(*Channel)
      rc = LibSSH2_Channel_Exec(*Channel, "sh")
      Debug "Channel Shell: " + LibSSH2_ErrorToString(rc)
      
      LibSSH2_Channel_Set_Blocking(*Channel, #False)
      
      *Buffer = AllocateMemory(1024)
      
      Text$ = "ps ax | grep pts" + #CR$
      LibSSH2_Channel_Write(*Channel, Text$, StringByteLength(Text$, #PB_UTF8))
      
      Timeout = 100
      Repeat
        Delay(10)
        Count = LibSSH2_Channel_Read(*Channel, *Buffer, 1024)        
        If Count > 0
          Text$ = PeekS(*Buffer, Count, #PB_UTF8|#PB_ByteLength)
          ;Debug Text$
          For i = 1 To CountString(Text$, #LF$) + 1
            Debug Trim(StringField(Text$, i, #LF$), #CR$)
          Next i
          Timeout = 100
        Else
          Timeout - 1
        EndIf
      Until Timeout = 0
      
      If *Channel
        rc = LibSSH2_Channel_Close(*Channel)
        Debug "Channel Close: " + LibSSH2_ErrorToString(rc)
        LibSSH2_Channel_Free(*Channel)
        *Channel = #Null
      EndIf
      
      If *Session
        LibSSH2_Session_Disconnect(*Session, "Normal disconnect.")
        LibSSH2_Session_Free(*Session)
      EndIf
      
    EndIf
    LibSSH2_Exit()
    LibSSH2_LibFree()
  EndIf
EndIf

CloseNetworkConnection(Sock)
You have to change the address, name and password :wink:

Oh, you need also a copy of libssh2.dll
https://curl.se/windows/dl-7.80.0_2/lib ... -mingw.zip
https://curl.se/windows/dl-7.80.0_2/lib ... -mingw.zip

And libcrypto-1_1.dll
https://kb.firedaemon.com/support/solut ... ft-windows

It was not possible to use

Code: Select all

LibSSH2_Channel_Shell(*Channel)
But

Code: Select all

LibSSH2_Channel_Exec(*Channel, "sh")
Did the same :D

Bernd
Last edited by infratec on Tue Nov 29, 2022 11:24 am, edited 6 times in total.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: for the networkers: a wrapper for libssh2

Post by infratec »

:mrgreen: :mrgreen: :mrgreen:

Now also tested with Linux :!:

But I have not the possibility to test it on Mac OS X :cry:
kinglestat
Enthusiast
Enthusiast
Posts: 732
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Re: for the networkers: a wrapper for libssh2

Post by kinglestat »

if only you were a woman!
I'd marry you (and probably divorce you after 2 weeks)
Thanks for this
I may not help with your coding
Just ask about mental issues!

http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
kinglestat
Enthusiast
Enthusiast
Posts: 732
Joined: Fri Jul 14, 2006 8:53 pm
Location: Malta
Contact:

Re: for the networkers: a wrapper for libssh2

Post by kinglestat »

Though I manage to ssh to server with test program I never get pass authentication failed
tried with several ssh servers and with different username/passwords...no luck

I figured out why..its because in my case libssh2_userauth_password will not work, I need to use libssh2_userauth_keyboard_interactive instead. But how to replicate that callback function ? Any ideas?
I may not help with your coding
Just ask about mental issues!

http://www.lulu.com/spotlight/kingwolf
http://www.sen3.net
marsu
New User
New User
Posts: 3
Joined: Wed Aug 06, 2008 10:49 am
Location: FRANCE

Re: for the networkers: a wrapper for libssh2

Post by marsu »

Bonjour,

dans cet exemple, j'utilise le call back pour l'authentification par keyboard interactive.
Cela fonctionne si on utilise *Pwd qui est l'adresse d'une constante, mais ne fonctione pas avec *pwd1 qui est l'adresse d'une variable.
Dans ce cas il y a "invalide memory acces" lors du endprocedure.

Si quelqu'un a une idée...

Code: Select all

IncludeFile "ssh2.pbi"

Global Server.s = "192.168.1.98"
Global Username.s = "vincent"
Global Password.s = "mypassword"

Global *Pwd=@"mypassword"

Global *Pwd1=@Password


ProcedureC kbd_callback(name.s, name_len.l,instruction.s,instruction_len.l,num_prompts.l,*prompts.LIBSSH2_USERAUTH_KBDINT_PROMPT_str, *responses.LIBSSH2_USERAUTH_KBDINT_RESPONSE_str,*abstract)
  If (num_prompts = 1) 
    *responses\text = *pwd
    *responses\length = Len(Password)
  EndIf
EndProcedure

InitNetwork()

Sock = OpenNetworkConnection(Server, 22, #PB_Network_TCP)
If Sock <> 0 
  If LibSSH2_LibInit()
    If LibSSH2_Init(0) = 0
      *Session = LibSSH2_Session_Init()
      If *Session
        rc = libssh2_session_startup(*Session, ConnectionID(Sock))
        If rc <> 0
          Debug "Session Startup: " + LibSSH2_ErrorToString(rc)
        Else        
          *UserAuthList = LibSSH2_Userauth_List(*Session, Username, Len(Username))
          Methode.s= PeekS(*UserAuthList)
          
          If FindString(Methode, "password")
            rc = libssh2_userauth_password(*Session, Username, Password$)
          Else
            rc=LibSSH2_Userauth_Keyboard_Interactive(*session, Username, @kbd_callback())
          EndIf
          
          
          If rc <> 0
            Debug "UserAuth: " + LibSSH2_ErrorToString(rc)
          Else
            Debug "Connexion OK"          
            LibSSH2_Session_Disconnect(*Session, "Normal disconnect.")
            LibSSH2_Session_Free(*Session)
          EndIf
        EndIf
      EndIf
      LibSSH2_Exit()
    EndIf
    LibSSH2_LibFree()
  EndIf
  CloseNetworkConnection(Sock)
EndIf
marsu
New User
New User
Posts: 3
Joined: Wed Aug 06, 2008 10:49 am
Location: FRANCE

Re: for the networkers: a wrapper for libssh2

Post by marsu »

Hello,
i see that is a memory probleme, in the sample of libssh library it use strdup fonction to get memory copy.

if try to use allocatememory but it dont work, globalalloc or localalloc work.

This code work correctly.

Code: Select all


IncludeFile "ssh2.pbi"

Global Server.s = "192.168.1.98"
Global Username.s = "vincent"
Global Password.s = "mypassword"

Procedure.l strdup(str.s)
  Protected siz.l
  Protected *copy
  
  siz = Len(str) + 1;
  *copy = LocalAlloc_(#GMEM_FIXED,siz)
  If *copy =0
    ProcedureReturn #Null
  EndIf
  CopyMemory(@str, *copy, siz);
  ProcedureReturn *copy
EndProcedure

ProcedureC kbd_callback(name.s, name_len.l,instruction.s,instruction_len.l,num_prompts.l,*prompts.LIBSSH2_USERAUTH_KBDINT_PROMPT_str, *responses.LIBSSH2_USERAUTH_KBDINT_RESPONSE_str,*abstract)
  If (num_prompts = 1) 
    *responses\text = strdup(Password)
    *responses\length = Len(Password)
  EndIf
EndProcedure

InitNetwork()

Sock = OpenNetworkConnection(Server, 22, #PB_Network_TCP)
If Sock <> 0 
  If LibSSH2_LibInit()
    If LibSSH2_Init(0) = 0
      *Session = LibSSH2_Session_Init()
      If *Session
        rc = libssh2_session_startup(*Session, ConnectionID(Sock))
        If rc <> 0
          Debug "Session Startup: " + LibSSH2_ErrorToString(rc)
        Else        
          *UserAuthList = LibSSH2_Userauth_List(*Session, Username, Len(Username))
          Methode.s= PeekS(*UserAuthList)
          
          If FindString(Methode, "password")
            rc = libssh2_userauth_password(*Session, Username, Password$)
          Else
            rc=LibSSH2_Userauth_Keyboard_Interactive(*session, Username, @kbd_callback())
          EndIf
          
          
          If rc <> 0
            Debug "UserAuth: " + LibSSH2_ErrorToString(rc)
          Else
            Debug "Connexion OK"          
            LibSSH2_Session_Disconnect(*Session, "Normal disconnect.")
            LibSSH2_Session_Free(*Session)
          EndIf
        EndIf
      EndIf
      LibSSH2_Exit()
    EndIf
    LibSSH2_LibFree()
  EndIf
  CloseNetworkConnection(Sock)
EndIf
verleihnix
User
User
Posts: 13
Joined: Sun Dec 14, 2008 4:55 am
Location: Switzerland

Re: for the networkers: a wrapper for libssh2

Post by verleihnix »

Hey there,

Thank you for your wrapper! I am currently worhking to get libssh2 wo work with PB, when i stumbeled upon this article in the forum. While trying to implement this on PB 5.24 LTS i get an error when compiling the sample:

"Line 366: Native types can't be used with pointers."

Code: Select all

OSPrototype.l Proto_LibSSH2_Channel_Window_Read_Ex(*channel, *read_avail.l, *window_size_initial.l)
Further reading in the forum unveils that something must have changed in PB 5.10+ handling stuff.

[5.10] Native types can't be used with pointers: http://www.purebasic.fr/english/viewtop ... 13&t=52873
Native types can't be used with pointers: http://www.forums.purebasic.com/english ... 13&t=59315

I get the *window_size_initial. and the *read_avail.l seems to be the problem ".l = Long and is a native type" according to the documentation: http://www.purebasic.com/documentation/ ... ables.html. So the .l's have to go ;). Where to go from here, do i have to create structures first?

Is somebody willing to help to get this snipped up and kicking in PC 5.10+ ?

Kind regards and thanks in advance verleihnix
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: for the networkers: a wrapper for libssh2

Post by skywalk »

Code: Select all

; FROM:
OSPrototype.l Proto_LibSSH2_Channel_Window_Read_Ex(*channel, *read_avail.l, *window_size_initial.l)
; TO:
OSPrototype.l Proto_LibSSH2_Channel_Window_Read_Ex(*channel, *read_avail_l, *window_size_initial_l)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
verleihnix
User
User
Posts: 13
Joined: Sun Dec 14, 2008 4:55 am
Location: Switzerland

Re: for the networkers: a wrapper for libssh2

Post by verleihnix »

Thank you skywalk for your fast answer!

it does not throw any error when compiling anymore.

Altho it seems to be a problem to download the libssh2.dll or the mentioned curl package anywhere. The newer version seems to be incompatible with the include. I will keep trying and post a solution if i fond it.

I am really thankful for the good community!

verleihnix
User avatar
ostapas
Enthusiast
Enthusiast
Posts: 192
Joined: Thu Feb 18, 2010 11:10 pm

Re: for the networkers: a wrapper for libssh2

Post by ostapas »

Anyone still has the right libssh2.dll? Can't find it anywhere. Newer versions are spitting out memory errors.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: for the networkers: a wrapper for libssh2

Post by infratec »

I updated the stuff above.

It works now with PB 5.7x

You need:

libssh2.dll
libcrypto-1_1.dll

Follow the links in the first post to get working versions.
hoerbie
Enthusiast
Enthusiast
Posts: 119
Joined: Fri Dec 06, 2013 11:57 am
Location: DE/BY/MUC

Re: for the networkers: a wrapper for libssh2

Post by hoerbie »

Hi,

the above download links for the dlls no longer work, and the curl people seem to remove the extra libssh2 and openssl stuff, in newer versions like 7.86 there is nothing of this anymore.

After searching the web a lot, I found one download directory with working versions:

https://curl.se/windows/dl-7.80.0_2/
libssh2-1.10.0_2-win32-mingw.zip
openssl-3.0.1_2-win32-mingw.zip

64-bit versions are also there, but I haven't tested them.

Greets, hoerbie
Post Reply