| 1 |
unit libssh; |
| 2 |
|
| 3 |
{$mode delphi} |
| 4 |
{$packrecords c} |
| 5 |
|
| 6 |
interface |
| 7 |
|
| 8 |
uses |
| 9 |
Classes, SysUtils, CTypes, DynLibs; |
| 10 |
|
| 11 |
const |
| 12 |
//* Hash Types */ |
| 13 |
LIBSSH2_HOSTKEY_HASH_MD5 = 1; |
| 14 |
LIBSSH2_HOSTKEY_HASH_SHA1 = 2; |
| 15 |
LIBSSH2_HOSTKEY_HASH_SHA256 = 3; |
| 16 |
|
| 17 |
//* Method constants */ |
| 18 |
LIBSSH2_METHOD_KEX = 0; |
| 19 |
LIBSSH2_METHOD_HOSTKEY = 1; |
| 20 |
LIBSSH2_METHOD_CRYPT_CS = 2; |
| 21 |
LIBSSH2_METHOD_CRYPT_SC = 3; |
| 22 |
|
| 23 |
//* Disconnect Codes (defined by SSH protocol) */ |
| 24 |
SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT = 1; |
| 25 |
SSH_DISCONNECT_PROTOCOL_ERROR = 2; |
| 26 |
SSH_DISCONNECT_KEY_EXCHANGE_FAILED = 3; |
| 27 |
SSH_DISCONNECT_RESERVED = 4; |
| 28 |
SSH_DISCONNECT_MAC_ERROR = 5; |
| 29 |
SSH_DISCONNECT_COMPRESSION_ERROR = 6; |
| 30 |
SSH_DISCONNECT_SERVICE_NOT_AVAILABLE = 7; |
| 31 |
SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED = 8; |
| 32 |
SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE = 9; |
| 33 |
SSH_DISCONNECT_CONNECTION_LOST = 10; |
| 34 |
SSH_DISCONNECT_BY_APPLICATION = 11; |
| 35 |
SSH_DISCONNECT_TOO_MANY_CONNECTIONS = 12; |
| 36 |
SSH_DISCONNECT_AUTH_CANCELLED_BY_USER = 13; |
| 37 |
SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE = 14; |
| 38 |
SSH_DISCONNECT_ILLEGAL_USER_NAME = 15; |
| 39 |
|
| 40 |
{ Error Codes (defined by libssh2) } |
| 41 |
LIBSSH2_ERROR_NONE = 0; |
| 42 |
LIBSSH2_ERROR_SOCKET_NONE = -(1); |
| 43 |
LIBSSH2_ERROR_BANNER_RECV = -(2); |
| 44 |
LIBSSH2_ERROR_BANNER_SEND = -(3); |
| 45 |
LIBSSH2_ERROR_INVALID_MAC = -(4); |
| 46 |
LIBSSH2_ERROR_KEX_FAILURE = -(5); |
| 47 |
LIBSSH2_ERROR_ALLOC = -(6); |
| 48 |
LIBSSH2_ERROR_SOCKET_SEND = -(7); |
| 49 |
LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE = -(8); |
| 50 |
LIBSSH2_ERROR_TIMEOUT = -(9); |
| 51 |
LIBSSH2_ERROR_HOSTKEY_INIT = -(10); |
| 52 |
LIBSSH2_ERROR_HOSTKEY_SIGN = -(11); |
| 53 |
LIBSSH2_ERROR_DECRYPT = -(12); |
| 54 |
LIBSSH2_ERROR_SOCKET_DISCONNECT = -(13); |
| 55 |
LIBSSH2_ERROR_PROTO = -(14); |
| 56 |
LIBSSH2_ERROR_PASSWORD_EXPIRED = -(15); |
| 57 |
LIBSSH2_ERROR_FILE = -(16); |
| 58 |
LIBSSH2_ERROR_METHOD_NONE = -(17); |
| 59 |
LIBSSH2_ERROR_AUTHENTICATION_FAILED = -(18); |
| 60 |
LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED = LIBSSH2_ERROR_AUTHENTICATION_FAILED; |
| 61 |
LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED = -(19); |
| 62 |
LIBSSH2_ERROR_CHANNEL_OUTOFORDER = -(20); |
| 63 |
LIBSSH2_ERROR_CHANNEL_FAILURE = -(21); |
| 64 |
LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED = -(22); |
| 65 |
LIBSSH2_ERROR_CHANNEL_UNKNOWN = -(23); |
| 66 |
LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED = -(24); |
| 67 |
LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED = -(25); |
| 68 |
LIBSSH2_ERROR_CHANNEL_CLOSED = -(26); |
| 69 |
LIBSSH2_ERROR_CHANNEL_EOF_SENT = -(27); |
| 70 |
LIBSSH2_ERROR_SCP_PROTOCOL = -(28); |
| 71 |
LIBSSH2_ERROR_ZLIB = -(29); |
| 72 |
LIBSSH2_ERROR_SOCKET_TIMEOUT = -(30); |
| 73 |
LIBSSH2_ERROR_SFTP_PROTOCOL = -(31); |
| 74 |
LIBSSH2_ERROR_REQUEST_DENIED = -(32); |
| 75 |
LIBSSH2_ERROR_METHOD_NOT_SUPPORTED = -(33); |
| 76 |
LIBSSH2_ERROR_INVAL = -(34); |
| 77 |
LIBSSH2_ERROR_INVALID_POLL_TYPE = -(35); |
| 78 |
LIBSSH2_ERROR_PUBLICKEY_PROTOCOL = -(36); |
| 79 |
LIBSSH2_ERROR_EAGAIN = -(37); |
| 80 |
LIBSSH2_ERROR_BUFFER_TOO_SMALL = -(38); |
| 81 |
LIBSSH2_ERROR_BAD_USE = -(39); |
| 82 |
LIBSSH2_ERROR_COMPRESS = -(40); |
| 83 |
LIBSSH2_ERROR_OUT_OF_BOUNDARY = -(41); |
| 84 |
LIBSSH2_ERROR_AGENT_PROTOCOL = -(42); |
| 85 |
LIBSSH2_ERROR_SOCKET_RECV = -(43); |
| 86 |
LIBSSH2_ERROR_ENCRYPT = -(44); |
| 87 |
LIBSSH2_ERROR_BAD_SOCKET = -(45); |
| 88 |
LIBSSH2_ERROR_KNOWN_HOSTS = -(46); |
| 89 |
|
| 90 |
//* Channel API */ |
| 91 |
LIBSSH2_CHANNEL_WINDOW_DEFAULT = (2*1024*1024); |
| 92 |
LIBSSH2_CHANNEL_PACKET_DEFAULT = 32768; |
| 93 |
|
| 94 |
//* Flags for open_ex() */ |
| 95 |
_LIBSSH2_SFTP_OPENFILE = 0; |
| 96 |
_LIBSSH2_SFTP_OPENDIR = 1; |
| 97 |
|
| 98 |
//* Flags for rename_ex() */ |
| 99 |
LIBSSH2_SFTP_RENAME_OVERWRITE = $00000001; |
| 100 |
LIBSSH2_SFTP_RENAME_ATOMIC = $00000002; |
| 101 |
LIBSSH2_SFTP_RENAME_NATIVE = $00000004; |
| 102 |
|
| 103 |
//* Flags for stat_ex() */ |
| 104 |
_LIBSSH2_SFTP_STAT = 0; |
| 105 |
_LIBSSH2_SFTP_LSTAT = 1; |
| 106 |
_LIBSSH2_SFTP_SETSTAT = 2; |
| 107 |
|
| 108 |
//* Flags for symlink_ex() */ |
| 109 |
_LIBSSH2_SFTP_SYMLINK = 0; |
| 110 |
_LIBSSH2_SFTP_READLINK = 1; |
| 111 |
_LIBSSH2_SFTP_REALPATH = 2; |
| 112 |
|
| 113 |
//* SFTP attribute flag bits */ |
| 114 |
LIBSSH2_SFTP_ATTR_SIZE = $00000001; |
| 115 |
LIBSSH2_SFTP_ATTR_UIDGID = $00000002; |
| 116 |
LIBSSH2_SFTP_ATTR_PERMISSIONS = $00000004; |
| 117 |
LIBSSH2_SFTP_ATTR_ACMODTIME = $00000008; |
| 118 |
LIBSSH2_SFTP_ATTR_EXTENDED = $80000000; |
| 119 |
|
| 120 |
//* File mode */ |
| 121 |
//* Read, write, execute/search by owner */ |
| 122 |
LIBSSH2_SFTP_S_IRWXU = 448; //* RWX mask for owner */ |
| 123 |
LIBSSH2_SFTP_S_IRUSR = 256; //* R for owner */ |
| 124 |
LIBSSH2_SFTP_S_IWUSR = 128; //* W for owner */ |
| 125 |
LIBSSH2_SFTP_S_IXUSR = 64; //* X for owner */ |
| 126 |
//* Read, write, execute/search by group */ |
| 127 |
LIBSSH2_SFTP_S_IRWXG = 56; //* RWX mask for group */ |
| 128 |
LIBSSH2_SFTP_S_IRGRP = 32; //* R for group */ |
| 129 |
LIBSSH2_SFTP_S_IWGRP = 16; //* W for group */ |
| 130 |
LIBSSH2_SFTP_S_IXGRP = 8; //* X for group */ |
| 131 |
//* Read, write, execute/search by others */ |
| 132 |
LIBSSH2_SFTP_S_IRWXO = 7; //* RWX mask for other */ |
| 133 |
LIBSSH2_SFTP_S_IROTH = 4; //* R for other */ |
| 134 |
LIBSSH2_SFTP_S_IWOTH = 2; //* W for other */ |
| 135 |
LIBSSH2_SFTP_S_IXOTH = 1; //* X for other */ |
| 136 |
|
| 137 |
//* SFTP File Transfer Flags -- (e.g. flags parameter to sftp_open()) */ |
| 138 |
LIBSSH2_FXF_READ = $00000001; |
| 139 |
LIBSSH2_FXF_WRITE = $00000002; |
| 140 |
LIBSSH2_FXF_APPEND = $00000004; |
| 141 |
LIBSSH2_FXF_CREAT = $00000008; |
| 142 |
LIBSSH2_FXF_TRUNC = $00000010; |
| 143 |
LIBSSH2_FXF_EXCL = $00000020; |
| 144 |
|
| 145 |
type |
| 146 |
//* Session API */ |
| 147 |
PLIBSSH2_SESSION = type Pointer; |
| 148 |
//* Agent API */ |
| 149 |
PLIBSSH2_AGENT = type Pointer; |
| 150 |
libssh2_agent_publickey = record |
| 151 |
magic: cuint; |
| 152 |
node: Pointer; |
| 153 |
blob: PByte; |
| 154 |
blob_len: csize_t; |
| 155 |
comment: PAnsiChar; |
| 156 |
end; |
| 157 |
Plibssh2_agent_publickey = ^libssh2_agent_publickey; |
| 158 |
PPlibssh2_agent_publickey = ^Plibssh2_agent_publickey; |
| 159 |
//* Channel API */ |
| 160 |
PLIBSSH2_CHANNEL = type Pointer; |
| 161 |
//* SFTP API */ |
| 162 |
PLIBSSH2_SFTP = type Pointer; |
| 163 |
PLIBSSH2_SFTP_HANDLE = type Pointer; |
| 164 |
PLIBSSH2_SFTP_ATTRIBUTES = ^LIBSSH2_SFTP_ATTRIBUTES; |
| 165 |
LIBSSH2_SFTP_ATTRIBUTES = record |
| 166 |
flags: culong; |
| 167 |
filesize: cuint64; |
| 168 |
uid, gid: culong; |
| 169 |
permissions: culong; |
| 170 |
atime, mtime: culong; |
| 171 |
end; |
| 172 |
PLIBSSH2_SFTP_STATVFS = ^_LIBSSH2_SFTP_STATVFS; |
| 173 |
_LIBSSH2_SFTP_STATVFS = record |
| 174 |
f_bsize: cuint64; //* file system block size */ |
| 175 |
f_frsize: cuint64; //* fragment size */ |
| 176 |
f_blocks: cuint64; //* size of fs in f_frsize units */ |
| 177 |
f_bfree: cuint64; //* # free blocks */ |
| 178 |
f_bavail: cuint64; //* # free blocks for non-root */ |
| 179 |
f_files: cuint64; //* # inodes */ |
| 180 |
f_ffree: cuint64; //* # free inodes */ |
| 181 |
f_favail: cuint64; //* # free inodes for non-root */ |
| 182 |
f_fsid: cuint64; //* file system ID */ |
| 183 |
f_flag: cuint64; //* mount flags */ |
| 184 |
f_namemax: cuint64; //* maximum filename length */ |
| 185 |
end; |
| 186 |
PLIBSSH2_USERAUTH_KBDINT_PROMPT = ^LIBSSH2_USERAUTH_KBDINT_PROMPT; |
| 187 |
LIBSSH2_USERAUTH_KBDINT_PROMPT = record |
| 188 |
text: PAnsiChar; |
| 189 |
length: cuint; |
| 190 |
echo: cuchar; |
| 191 |
end; |
| 192 |
PLIBSSH2_USERAUTH_KBDINT_RESPONSE = ^LIBSSH2_USERAUTH_KBDINT_RESPONSE; |
| 193 |
LIBSSH2_USERAUTH_KBDINT_RESPONSE = record |
| 194 |
text: PAnsiChar; |
| 195 |
length: cuint; |
| 196 |
end; |
| 197 |
Plibssh2_struct_stat = type Pointer; |
| 198 |
//* Malloc callbacks */ |
| 199 |
LIBSSH2_ALLOC_FUNC = function(count: csize_t; abstract: Pointer): Pointer; cdecl; |
| 200 |
LIBSSH2_REALLOC_FUNC = function(ptr: Pointer; count: csize_t; abstract: Pointer): Pointer; cdecl; |
| 201 |
LIBSSH2_FREE_FUNC = procedure(ptr: Pointer; abstract: Pointer); cdecl; |
| 202 |
//* Callbacks for special SSH packets */ |
| 203 |
LIBSSH2_PASSWD_CHANGEREQ_FUNC = procedure(session: PLIBSSH2_SESSION; var newpw: PAnsiChar; |
| 204 |
var newpw_len: cint; abstract: Pointer); cdecl; |
| 205 |
//* 'keyboard-interactive' authentication callback */ |
| 206 |
LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC = procedure(const name: PAnsiChar; name_len: cint; |
| 207 |
const instruction: PAnsiChar; instruction_len: cint; |
| 208 |
num_prompts: cint; const prompts: PLIBSSH2_USERAUTH_KBDINT_PROMPT; |
| 209 |
responses: PLIBSSH2_USERAUTH_KBDINT_RESPONSE; abstract: PPointer); cdecl; |
| 210 |
|
| 211 |
var |
| 212 |
//* Global API */ |
| 213 |
libssh2_init: function(flags: cint): cint; cdecl; |
| 214 |
libssh2_exit: procedure(); cdecl; |
| 215 |
libssh2_version: function(required_version: cint): PAnsiChar; cdecl; |
| 216 |
//* Session API */ |
| 217 |
libssh2_session_init_ex: function(my_alloc: LIBSSH2_ALLOC_FUNC; |
| 218 |
my_free: LIBSSH2_FREE_FUNC; |
| 219 |
my_realloc: LIBSSH2_REALLOC_FUNC; |
| 220 |
abstract: Pointer): PLIBSSH2_SESSION; cdecl; |
| 221 |
libssh2_session_handshake: function(session: PLIBSSH2_SESSION; sock: cint): cint; cdecl; |
| 222 |
libssh2_hostkey_hash: function(session: PLIBSSH2_SESSION; hash_type: cint): PAnsiChar; cdecl; |
| 223 |
libssh2_session_methods: function(session: PLIBSSH2_SESSION; method_type: cint): PAnsiChar; cdecl; |
| 224 |
libssh2_session_disconnect_ex: function(session: PLIBSSH2_SESSION; |
| 225 |
reason: cint; |
| 226 |
const description: PAnsiChar; |
| 227 |
const lang: PAnsiChar): cint; cdecl; |
| 228 |
libssh2_session_free: function(session: PLIBSSH2_SESSION): cint; cdecl; |
| 229 |
libssh2_session_set_blocking: procedure(session: PLIBSSH2_SESSION; blocking: cint); cdecl; |
| 230 |
libssh2_session_last_errno: function(session: PLIBSSH2_SESSION): cint; cdecl; |
| 231 |
libssh2_session_set_timeout: procedure(session: PLIBSSH2_SESSION; timeout: clong); cdecl; |
| 232 |
libssh2_session_last_error: function(session: PLIBSSH2_SESSION; errmsg: PPAnsiChar; |
| 233 |
errmsg_len: pcint; want_buf: cint): cint; cdecl; |
| 234 |
//* Userauth API */ |
| 235 |
libssh2_userauth_authenticated: function(session: PLIBSSH2_SESSION): cint; cdecl; |
| 236 |
libssh2_userauth_list: function(session: PLIBSSH2_SESSION; |
| 237 |
const username: PAnsiChar; username_len: cuint): PAnsiChar; cdecl; |
| 238 |
libssh2_userauth_password_ex: function(session: PLIBSSH2_SESSION; |
| 239 |
const username: PAnsiChar; |
| 240 |
username_len: cuint; |
| 241 |
const password: PAnsiChar; |
| 242 |
password_len: cuint; |
| 243 |
passwd_change_cb: LIBSSH2_PASSWD_CHANGEREQ_FUNC): cint; cdecl; |
| 244 |
libssh2_userauth_keyboard_interactive_ex: function(session: PLIBSSH2_SESSION; |
| 245 |
const username: PAnsiChar; |
| 246 |
username_len: cuint; |
| 247 |
response_callback: LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC): cint; cdecl; |
| 248 |
libssh2_userauth_publickey_fromfile_ex: function(session: PLIBSSH2_SESSION; |
| 249 |
const username: PAnsiChar; username_len: cuint; |
| 250 |
const publickey, privatekey, passphrase: PAnsiChar): cint; cdecl; |
| 251 |
//* Agent API */ |
| 252 |
libssh2_agent_init: function(session: PLIBSSH2_SESSION): PLIBSSH2_AGENT; cdecl; |
| 253 |
libssh2_agent_connect: function(agent: PLIBSSH2_AGENT): cint; cdecl; |
| 254 |
libssh2_agent_list_identities: function(agent: PLIBSSH2_AGENT): cint; cdecl; |
| 255 |
libssh2_agent_get_identity: function(agent: PLIBSSH2_AGENT; store: PPlibssh2_agent_publickey; prev: Plibssh2_agent_publickey): cint; cdecl; |
| 256 |
libssh2_agent_userauth: function(agent: PLIBSSH2_AGENT; const username: PAnsiChar; identity: Plibssh2_agent_publickey): cint; cdecl; |
| 257 |
libssh2_agent_disconnect: function(agent: PLIBSSH2_AGENT): cint; cdecl; |
| 258 |
libssh2_agent_free: procedure(agent: PLIBSSH2_AGENT); cdecl; |
| 259 |
//* Channel API */ |
| 260 |
libssh2_channel_open_ex: function(session: PLIBSSH2_SESSION; const channel_type: PAnsiChar; |
| 261 |
channel_type_len, window_size, packet_size: cuint; |
| 262 |
const message: PAnsiChar; message_len: cuint): PLIBSSH2_CHANNEL; cdecl; |
| 263 |
libssh2_channel_free: function(channel: PLIBSSH2_CHANNEL): cint; cdecl; |
| 264 |
libssh2_channel_set_blocking: procedure (channel: PLIBSSH2_CHANNEL; blocking: cint); cdecl; |
| 265 |
libssh2_channel_process_startup: function(channel: PLIBSSH2_CHANNEL; |
| 266 |
const request: PAnsiChar; request_len: cuint; |
| 267 |
const message: PAnsiChar; message_len: cuint): cint; cdecl; |
| 268 |
libssh2_channel_flush_ex: function(channel: PLIBSSH2_CHANNEL; streamid: cint): cint; cdecl; |
| 269 |
libssh2_channel_send_eof: function(channel: PLIBSSH2_CHANNEL): cint; cdecl; |
| 270 |
libssh2_channel_eof: function(channel: PLIBSSH2_CHANNEL): cint; cdecl; |
| 271 |
libssh2_channel_read_ex: function(channel: PLIBSSH2_CHANNEL; stream_id: cint; |
| 272 |
buf: PAnsiChar; buflen: csize_t): ptrint; cdecl; |
| 273 |
libssh2_channel_write_ex: function(channel: PLIBSSH2_CHANNEL; stream_id: cint; |
| 274 |
const buf: PAnsiChar; buflen: csize_t): ptrint; cdecl; |
| 275 |
libssh2_channel_get_exit_status: function(channel: PLIBSSH2_CHANNEL): cint; cdecl; |
| 276 |
libssh2_scp_send64: function(session: PLIBSSH2_SESSION; const path: PAnsiChar; mode: cint; |
| 277 |
size: cuint64; mtime, atime: ptrint): PLIBSSH2_CHANNEL; cdecl; |
| 278 |
libssh2_scp_recv2: function(session: PLIBSSH2_SESSION; const path: PAnsiChar; |
| 279 |
sb: Plibssh2_struct_stat): PLIBSSH2_CHANNEL; cdecl; |
| 280 |
//* SFTP API */ |
| 281 |
libssh2_sftp_init: function(session: PLIBSSH2_SESSION): PLIBSSH2_SFTP; cdecl; |
| 282 |
libssh2_sftp_shutdown: function(sftp: PLIBSSH2_SFTP): cint; cdecl; |
| 283 |
libssh2_sftp_last_error: function(sftp: PLIBSSH2_SFTP): culong; cdecl; |
| 284 |
//* File / Directory Ops */ |
| 285 |
libssh2_sftp_open_ex: function(sftp: PLIBSSH2_SFTP; |
| 286 |
const filename: PAnsiChar; |
| 287 |
filename_len: cint; flags: culong; |
| 288 |
mode: clong; open_type: cint): PLIBSSH2_SFTP_HANDLE; cdecl; |
| 289 |
libssh2_sftp_read: function(handle: PLIBSSH2_SFTP_HANDLE; |
| 290 |
buffer: PAnsiChar; buffer_maxlen: csize_t): ptrint; cdecl; |
| 291 |
libssh2_sftp_write: function(handle: PLIBSSH2_SFTP_HANDLE; |
| 292 |
buffer: PByte; count: csize_t): ptrint; cdecl; |
| 293 |
libssh2_sftp_readdir_ex: function(handle: PLIBSSH2_SFTP_HANDLE; |
| 294 |
buffer: PAnsiChar; buffer_maxlen: csize_t; |
| 295 |
longentry: PAnsiChar; longentry_maxlen: csize_t; |
| 296 |
attrs: PLIBSSH2_SFTP_ATTRIBUTES): cint; cdecl; |
| 297 |
libssh2_sftp_close_handle: function(handle: PLIBSSH2_SFTP_HANDLE): cint; cdecl; |
| 298 |
libssh2_sftp_seek64: procedure(handle: PLIBSSH2_SFTP_HANDLE; offset: cuint64); cdecl; |
| 299 |
//* Miscellaneous Ops */ |
| 300 |
libssh2_sftp_rename_ex: function(sftp: PLIBSSH2_SFTP; |
| 301 |
const source_filename: PAnsiChar; |
| 302 |
srouce_filename_len: cuint; |
| 303 |
const dest_filename: PAnsiChar; |
| 304 |
dest_filename_len: cuint; |
| 305 |
flags: clong): cint; cdecl; |
| 306 |
libssh2_sftp_unlink_ex: function(sftp: PLIBSSH2_SFTP; |
| 307 |
const filename: PAnsiChar; |
| 308 |
filename_len: cuint): cint; cdecl; |
| 309 |
libssh2_sftp_statvfs: function(sftp: PLIBSSH2_SFTP; |
| 310 |
const path: PAnsiChar; |
| 311 |
path_len: csize_t; |
| 312 |
st: PLIBSSH2_SFTP_STATVFS): cint; cdecl; |
| 313 |
libssh2_sftp_mkdir_ex: function(sftp: PLIBSSH2_SFTP; |
| 314 |
const path: PAnsiChar; |
| 315 |
path_len: cuint; mode: clong): cint; cdecl; |
| 316 |
libssh2_sftp_rmdir_ex: function(sftp: PLIBSSH2_SFTP; |
| 317 |
const path: PAnsiChar; |
| 318 |
path_len: cuint): cint; cdecl; |
| 319 |
libssh2_sftp_stat_ex: function(sftp: PLIBSSH2_SFTP; |
| 320 |
const path: PAnsiChar; |
| 321 |
path_len: cuint; |
| 322 |
stat_type: cint; |
| 323 |
attrs: PLIBSSH2_SFTP_ATTRIBUTES): cint; cdecl; |
| 324 |
libssh2_sftp_symlink_ex: function(sftp: PLIBSSH2_SFTP; |
| 325 |
const path: PAnsiChar; |
| 326 |
path_len: cuint; |
| 327 |
target: PAnsiChar; |
| 328 |
target_len: cuint; link_type: cint): cint; cdecl; |
| 329 |
|
| 330 |
//* Inline functions */ |
| 331 |
function libssh2_session_init(abstract: Pointer): PLIBSSH2_SESSION; inline; |
| 332 |
function libssh2_session_disconnect(session: PLIBSSH2_SESSION; const description: PAnsiChar): cint; inline; |
| 333 |
function libssh2_userauth_password(session: PLIBSSH2_SESSION; const username: PAnsiChar; const password: PAnsiChar): cint; inline; |
| 334 |
function libssh2_userauth_keyboard_interactive(session: PLIBSSH2_SESSION; const username: PAnsiChar; response_callback: LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC): cint; inline; |
| 335 |
function libssh2_userauth_publickey_fromfile(session: PLIBSSH2_SESSION; const username, publickey, privatekey, passphrase: PAnsiChar): cint; inline; |
| 336 |
function libssh2_channel_open_session(session: PLIBSSH2_SESSION): PLIBSSH2_CHANNEL; inline; |
| 337 |
function libssh2_channel_exec(channel: PLIBSSH2_CHANNEL; command: PAnsiChar): cint; inline; |
| 338 |
function libssh2_channel_flush(channel: PLIBSSH2_CHANNEL): cint; inline; |
| 339 |
function libssh2_channel_read(channel: PLIBSSH2_CHANNEL; buf: PAnsiChar; buflen: csize_t): ptrint; inline; |
| 340 |
function libssh2_channel_read_stderr(channel: PLIBSSH2_CHANNEL; buf: PAnsiChar; buflen: csize_t): ptrint; inline; |
| 341 |
function libssh2_channel_write(channel: PLIBSSH2_CHANNEL; const buf: PAnsiChar; buflen: csize_t): ptrint; inline; |
| 342 |
function libssh2_sftp_open(sftp: PLIBSSH2_SFTP; const filename: PAnsiChar; flags: culong; mode: clong): PLIBSSH2_SFTP_HANDLE; inline; |
| 343 |
function libssh2_sftp_opendir(sftp: PLIBSSH2_SFTP; const path: PAnsiChar): PLIBSSH2_SFTP_HANDLE; inline; |
| 344 |
function libssh2_sftp_close(handle: PLIBSSH2_SFTP_HANDLE): cint; inline; |
| 345 |
function libssh2_sftp_closedir(handle: PLIBSSH2_SFTP_HANDLE): cint; inline; |
| 346 |
function libssh2_sftp_rename(sftp: PLIBSSH2_SFTP; const sourcefile: PAnsiChar; const destfile: PAnsiChar): cint; inline; |
| 347 |
function libssh2_sftp_unlink(sftp: PLIBSSH2_SFTP; const filename: PAnsiChar): cint; inline; |
| 348 |
function libssh2_sftp_mkdir(sftp: PLIBSSH2_SFTP; const path: PAnsiChar; mode: clong): cint; inline; |
| 349 |
function libssh2_sftp_rmdir(sftp: PLIBSSH2_SFTP; const path: PAnsiChar): cint; inline; |
| 350 |
function libssh2_sftp_stat(sftp: PLIBSSH2_SFTP; const path: PAnsiChar; attrs: PLIBSSH2_SFTP_ATTRIBUTES): cint; inline; |
| 351 |
function libssh2_sftp_lstat(sftp: PLIBSSH2_SFTP; const path: PAnsiChar; attrs: PLIBSSH2_SFTP_ATTRIBUTES): cint; inline; |
| 352 |
function libssh2_sftp_setstat(sftp: PLIBSSH2_SFTP; const path: PAnsiChar; attrs: PLIBSSH2_SFTP_ATTRIBUTES): cint; inline; |
| 353 |
function libssh2_sftp_symlink(sftp: PLIBSSH2_SFTP; const orig: PAnsiChar; linkpath: PAnsiChar): cint; inline; |
| 354 |
function libssh2_sftp_readlink(sftp: PLIBSSH2_SFTP; const path: PAnsiChar; target: PAnsiChar; maxlen: cuint): cint; inline; |
| 355 |
function libssh2_sftp_realpath(sftp: PLIBSSH2_SFTP; const path: PAnsiChar; target: PAnsiChar; maxlen: cuint): cint; inline; |
| 356 |
|
| 357 |
const |
| 358 |
LibSSHName = |
| 359 |
{$IF DEFINED(MSWINDOWS)} |
| 360 |
'libssh2.dll' |
| 361 |
{$ELSEIF DEFINED(DARWIN)} |
| 362 |
'libssh2.dylib' |
| 363 |
{$ELSEIF DEFINED(UNIX)} |
| 364 |
'libssh2.so.1' |
| 365 |
{$ENDIF} |
| 366 |
; |
| 367 |
|
| 368 |
var |
| 369 |
libssh2: TLibHandle = NilHandle; |
| 370 |
|
| 371 |
implementation |
| 372 |
|
| 373 |
uses |
| 374 |
DCOSUtils; |
| 375 |
|
| 376 |
function libssh2_alloc(count: csize_t; abstract: Pointer): Pointer; cdecl; |
| 377 |
begin |
| 378 |
Result:= GetMem(count); |
| 379 |
end; |
| 380 |
|
| 381 |
function libssh2_realloc(ptr: Pointer; count: csize_t; abstract: Pointer): Pointer; cdecl; |
| 382 |
begin |
| 383 |
Result:= ReAllocMem(ptr, count); |
| 384 |
end; |
| 385 |
|
| 386 |
procedure libssh2_free(ptr: Pointer; abstract: Pointer); cdecl; |
| 387 |
begin |
| 388 |
FreeMem(ptr); |
| 389 |
end; |
| 390 |
|
| 391 |
function libssh2_session_init(abstract: Pointer): PLIBSSH2_SESSION; |
| 392 |
begin |
| 393 |
Result:= libssh2_session_init_ex(libssh2_alloc, libssh2_free, libssh2_realloc, abstract); |
| 394 |
end; |
| 395 |
|
| 396 |
function libssh2_session_disconnect(session: PLIBSSH2_SESSION; const description: PAnsiChar): cint; |
| 397 |
begin |
| 398 |
Result:= libssh2_session_disconnect_ex(session, SSH_DISCONNECT_BY_APPLICATION, |
| 399 |
description, ''); |
| 400 |
end; |
| 401 |
|
| 402 |
function libssh2_userauth_password(session: PLIBSSH2_SESSION; |
| 403 |
const username: PAnsiChar; const password: PAnsiChar): cint; |
| 404 |
begin |
| 405 |
Result:= libssh2_userauth_password_ex(session, username, strlen(username), |
| 406 |
password, strlen(password), nil); |
| 407 |
end; |
| 408 |
|
| 409 |
function libssh2_userauth_keyboard_interactive(session: PLIBSSH2_SESSION; |
| 410 |
const username: PAnsiChar; |
| 411 |
response_callback: LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC): cint; |
| 412 |
begin |
| 413 |
Result:= libssh2_userauth_keyboard_interactive_ex(session, username, strlen(username), response_callback); |
| 414 |
end; |
| 415 |
|
| 416 |
function libssh2_userauth_publickey_fromfile(session: PLIBSSH2_SESSION; |
| 417 |
const username, publickey, privatekey, passphrase: PAnsiChar): cint; |
| 418 |
begin |
| 419 |
Result:= libssh2_userauth_publickey_fromfile_ex(session, username, strlen(username), publickey, privatekey, passphrase); |
| 420 |
end; |
| 421 |
|
| 422 |
function libssh2_channel_open_session(session: PLIBSSH2_SESSION): PLIBSSH2_CHANNEL; |
| 423 |
begin |
| 424 |
Result:= libssh2_channel_open_ex(session, 'session', Length('session'), |
| 425 |
LIBSSH2_CHANNEL_WINDOW_DEFAULT, LIBSSH2_CHANNEL_PACKET_DEFAULT, nil, 0); |
| 426 |
end; |
| 427 |
|
| 428 |
function libssh2_channel_exec(channel: PLIBSSH2_CHANNEL; command: PAnsiChar): cint; |
| 429 |
begin |
| 430 |
REsult:= libssh2_channel_process_startup(channel, 'exec', Length('exec'), command, strlen(command)); |
| 431 |
end; |
| 432 |
|
| 433 |
function libssh2_channel_flush(channel: PLIBSSH2_CHANNEL): cint; |
| 434 |
begin |
| 435 |
Result:= libssh2_channel_flush_ex(channel, 0); |
| 436 |
end; |
| 437 |
|
| 438 |
function libssh2_channel_read(channel: PLIBSSH2_CHANNEL; buf: PAnsiChar; |
| 439 |
buflen: csize_t): ptrint; cdecl; |
| 440 |
begin |
| 441 |
Result:= libssh2_channel_read_ex(channel, 0, buf, buflen); |
| 442 |
end; |
| 443 |
|
| 444 |
function libssh2_channel_read_stderr(channel: PLIBSSH2_CHANNEL; buf: PAnsiChar; |
| 445 |
buflen: csize_t): ptrint; cdecl; |
| 446 |
begin |
| 447 |
Result:= libssh2_channel_read_ex(channel, 1, buf, buflen); |
| 448 |
end; |
| 449 |
|
| 450 |
function libssh2_channel_write(channel: PLIBSSH2_CHANNEL; const buf: PAnsiChar; |
| 451 |
buflen: csize_t): ptrint; |
| 452 |
begin |
| 453 |
Result:= libssh2_channel_write_ex(channel, 0, buf, buflen); |
| 454 |
end; |
| 455 |
|
| 456 |
function libssh2_sftp_open(sftp: PLIBSSH2_SFTP; const filename: PAnsiChar; |
| 457 |
flags: culong; mode: clong): PLIBSSH2_SFTP_HANDLE; |
| 458 |
begin |
| 459 |
Result:= libssh2_sftp_open_ex(sftp, filename, strlen(filename), flags, mode, _LIBSSH2_SFTP_OPENFILE); |
| 460 |
end; |
| 461 |
|
| 462 |
function libssh2_sftp_opendir(sftp: PLIBSSH2_SFTP; const path: PAnsiChar): PLIBSSH2_SFTP_HANDLE; |
| 463 |
begin |
| 464 |
Result:= libssh2_sftp_open_ex(sftp, path, strlen(path), 0, 0, _LIBSSH2_SFTP_OPENDIR); |
| 465 |
end; |
| 466 |
|
| 467 |
function libssh2_sftp_close(handle: PLIBSSH2_SFTP_HANDLE): cint; |
| 468 |
begin |
| 469 |
Result:= libssh2_sftp_close_handle(handle); |
| 470 |
end; |
| 471 |
|
| 472 |
function libssh2_sftp_closedir(handle: PLIBSSH2_SFTP_HANDLE): cint; |
| 473 |
begin |
| 474 |
Result:= libssh2_sftp_close_handle(handle); |
| 475 |
end; |
| 476 |
|
| 477 |
function libssh2_sftp_rename(sftp: PLIBSSH2_SFTP; const sourcefile: PAnsiChar; const destfile: PAnsiChar): cint; |
| 478 |
begin |
| 479 |
Result:= libssh2_sftp_rename_ex(sftp, sourcefile, strlen(sourcefile), |
| 480 |
destfile, strlen(destfile), |
| 481 |
LIBSSH2_SFTP_RENAME_OVERWRITE or |
| 482 |
LIBSSH2_SFTP_RENAME_ATOMIC or |
| 483 |
LIBSSH2_SFTP_RENAME_NATIVE); |
| 484 |
end; |
| 485 |
|
| 486 |
function libssh2_sftp_unlink(sftp: PLIBSSH2_SFTP; const filename: PAnsiChar): cint; |
| 487 |
begin |
| 488 |
Result:= libssh2_sftp_unlink_ex(sftp, filename, strlen(filename)); |
| 489 |
end; |
| 490 |
|
| 491 |
function libssh2_sftp_mkdir(sftp: PLIBSSH2_SFTP; const path: PAnsiChar; mode: clong): cint; |
| 492 |
begin |
| 493 |
Result:= libssh2_sftp_mkdir_ex(sftp, path, strlen(path), mode); |
| 494 |
end; |
| 495 |
|
| 496 |
function libssh2_sftp_rmdir(sftp: PLIBSSH2_SFTP; const path: PAnsiChar): cint; |
| 497 |
begin |
| 498 |
Result:= libssh2_sftp_rmdir_ex(sftp, path, strlen(path)); |
| 499 |
end; |
| 500 |
|
| 501 |
function libssh2_sftp_stat(sftp: PLIBSSH2_SFTP; const path: PAnsiChar; |
| 502 |
attrs: PLIBSSH2_SFTP_ATTRIBUTES): cint; |
| 503 |
begin |
| 504 |
Result:= libssh2_sftp_stat_ex(sftp, path, strlen(path), _LIBSSH2_SFTP_STAT, attrs); |
| 505 |
end; |
| 506 |
|
| 507 |
function libssh2_sftp_lstat(sftp: PLIBSSH2_SFTP; const path: PAnsiChar; |
| 508 |
attrs: PLIBSSH2_SFTP_ATTRIBUTES): cint; |
| 509 |
begin |
| 510 |
Result:= libssh2_sftp_stat_ex(sftp, path, strlen(path), _LIBSSH2_SFTP_LSTAT, attrs); |
| 511 |
end; |
| 512 |
|
| 513 |
function libssh2_sftp_setstat(sftp: PLIBSSH2_SFTP; const path: PAnsiChar; |
| 514 |
attrs: PLIBSSH2_SFTP_ATTRIBUTES): cint; |
| 515 |
begin |
| 516 |
repeat |
| 517 |
Result:= libssh2_sftp_stat_ex(sftp, path, strlen(path), _LIBSSH2_SFTP_SETSTAT, attrs); |
| 518 |
Sleep(1); |
| 519 |
until Result <> LIBSSH2_ERROR_EAGAIN; |
| 520 |
end; |
| 521 |
|
| 522 |
function libssh2_sftp_symlink(sftp: PLIBSSH2_SFTP; const orig: PAnsiChar; |
| 523 |
linkpath: PAnsiChar): cint; |
| 524 |
begin |
| 525 |
Result:= libssh2_sftp_symlink_ex(sftp, orig, strlen(orig), linkpath, strlen(linkpath), _LIBSSH2_SFTP_SYMLINK); |
| 526 |
end; |
| 527 |
|
| 528 |
function libssh2_sftp_readlink(sftp: PLIBSSH2_SFTP; const path: PAnsiChar; |
| 529 |
target: PAnsiChar; maxlen: cuint): cint; |
| 530 |
begin |
| 531 |
Result:= libssh2_sftp_symlink_ex(sftp, path, strlen(path), target, maxlen, _LIBSSH2_SFTP_READLINK) |
| 532 |
end; |
| 533 |
|
| 534 |
function libssh2_sftp_realpath(sftp: PLIBSSH2_SFTP; const path: PAnsiChar; |
| 535 |
target: PAnsiChar; maxlen: cuint): cint; |
| 536 |
begin |
| 537 |
Result:= libssh2_sftp_symlink_ex(sftp, path, strlen(path), target, maxlen, _LIBSSH2_SFTP_REALPATH); |
| 538 |
end; |
| 539 |
|
| 540 |
procedure Initialize; |
| 541 |
begin |
| 542 |
libssh2:= LoadLibrary(LibSSHName); |
| 543 |
if (libssh2 <> NilHandle) then |
| 544 |
try |
| 545 |
//* Global API */ |
| 546 |
libssh2_init:= SafeGetProcAddress(libssh2, 'libssh2_init'); |
| 547 |
libssh2_exit:= SafeGetProcAddress(libssh2, 'libssh2_exit'); |
| 548 |
libssh2_version:= SafeGetProcAddress(libssh2, 'libssh2_version'); |
| 549 |
//* Session API */ |
| 550 |
libssh2_session_init_ex:= SafeGetProcAddress(libssh2, 'libssh2_session_init_ex'); |
| 551 |
libssh2_session_handshake:= SafeGetProcAddress(libssh2, 'libssh2_session_handshake'); |
| 552 |
libssh2_hostkey_hash:= SafeGetProcAddress(libssh2, 'libssh2_hostkey_hash'); |
| 553 |
libssh2_session_methods:= SafeGetProcAddress(libssh2, 'libssh2_session_methods'); |
| 554 |
libssh2_session_disconnect_ex:= SafeGetProcAddress(libssh2, 'libssh2_session_disconnect_ex'); |
| 555 |
libssh2_session_free:= SafeGetProcAddress(libssh2, 'libssh2_session_free'); |
| 556 |
libssh2_session_set_blocking:= SafeGetProcAddress(libssh2, 'libssh2_session_set_blocking'); |
| 557 |
libssh2_session_last_errno:= SafeGetProcAddress(libssh2, 'libssh2_session_last_errno'); |
| 558 |
libssh2_session_last_error:= SafeGetProcAddress(libssh2, 'libssh2_session_last_error'); |
| 559 |
libssh2_session_set_timeout:= SafeGetProcAddress(libssh2, 'libssh2_session_set_timeout'); |
| 560 |
//* Userauth API */ |
| 561 |
libssh2_userauth_list:= SafeGetProcAddress(libssh2, 'libssh2_userauth_list'); |
| 562 |
libssh2_userauth_password_ex:= SafeGetProcAddress(libssh2, 'libssh2_userauth_password_ex'); |
| 563 |
libssh2_userauth_authenticated:= SafeGetProcAddress(libssh2, 'libssh2_userauth_authenticated'); |
| 564 |
libssh2_userauth_keyboard_interactive_ex:= SafeGetProcAddress(libssh2, 'libssh2_userauth_keyboard_interactive_ex'); |
| 565 |
libssh2_userauth_publickey_fromfile_ex:= SafeGetProcAddress(libssh2, 'libssh2_userauth_publickey_fromfile_ex'); |
| 566 |
|
| 567 |
//* Agent API */ |
| 568 |
libssh2_agent_init:= SafeGetProcAddress(libssh2, 'libssh2_agent_init'); |
| 569 |
libssh2_agent_connect:= SafeGetProcAddress(libssh2, 'libssh2_agent_connect'); |
| 570 |
libssh2_agent_list_identities:= SafeGetProcAddress(libssh2, 'libssh2_agent_list_identities'); |
| 571 |
libssh2_agent_get_identity:= SafeGetProcAddress(libssh2, 'libssh2_agent_get_identity'); |
| 572 |
libssh2_agent_userauth:= SafeGetProcAddress(libssh2, 'libssh2_agent_userauth'); |
| 573 |
libssh2_agent_disconnect:= SafeGetProcAddress(libssh2, 'libssh2_agent_disconnect'); |
| 574 |
libssh2_agent_free:= SafeGetProcAddress(libssh2, 'libssh2_agent_free'); |
| 575 |
|
| 576 |
//* Channel API */ |
| 577 |
libssh2_channel_open_ex:= SafeGetProcAddress(libssh2, 'libssh2_channel_open_ex'); |
| 578 |
libssh2_channel_free:= SafeGetProcAddress(libssh2, 'libssh2_channel_free'); |
| 579 |
libssh2_channel_set_blocking:= SafeGetProcAddress(libssh2, 'libssh2_channel_set_blocking'); |
| 580 |
libssh2_channel_process_startup:= SafeGetProcAddress(libssh2, 'libssh2_channel_process_startup'); |
| 581 |
libssh2_channel_flush_ex:= SafeGetProcAddress(libssh2, 'libssh2_channel_flush_ex'); |
| 582 |
libssh2_channel_send_eof:= SafeGetProcAddress(libssh2, 'libssh2_channel_send_eof'); |
| 583 |
libssh2_channel_eof:= SafeGetProcAddress(libssh2, 'libssh2_channel_eof'); |
| 584 |
libssh2_channel_read_ex:= SafeGetProcAddress(libssh2, 'libssh2_channel_read_ex'); |
| 585 |
libssh2_channel_write_ex:= SafeGetProcAddress(libssh2, 'libssh2_channel_write_ex'); |
| 586 |
libssh2_channel_get_exit_status:= SafeGetProcAddress(libssh2, 'libssh2_channel_get_exit_status'); |
| 587 |
libssh2_scp_send64:= SafeGetProcAddress(libssh2, 'libssh2_scp_send64'); |
| 588 |
libssh2_scp_recv2:= SafeGetProcAddress(libssh2, 'libssh2_scp_recv2'); |
| 589 |
|
| 590 |
//* SFTP API */ |
| 591 |
libssh2_sftp_init:= SafeGetProcAddress(libssh2, 'libssh2_sftp_init'); |
| 592 |
libssh2_sftp_shutdown:= SafeGetProcAddress(libssh2, 'libssh2_sftp_shutdown'); |
| 593 |
libssh2_sftp_last_error:= SafeGetProcAddress(libssh2, 'libssh2_sftp_last_error'); |
| 594 |
//* File / Directory Ops */ |
| 595 |
libssh2_sftp_open_ex:= SafeGetProcAddress(libssh2, 'libssh2_sftp_open_ex'); |
| 596 |
libssh2_sftp_read:= SafeGetProcAddress(libssh2, 'libssh2_sftp_read'); |
| 597 |
libssh2_sftp_write:= SafeGetProcAddress(libssh2, 'libssh2_sftp_write'); |
| 598 |
libssh2_sftp_readdir_ex:= SafeGetProcAddress(libssh2, 'libssh2_sftp_readdir_ex'); |
| 599 |
libssh2_sftp_close_handle:= SafeGetProcAddress(libssh2, 'libssh2_sftp_close_handle'); |
| 600 |
libssh2_sftp_seek64:= SafeGetProcAddress(libssh2, 'libssh2_sftp_seek64'); |
| 601 |
//* Miscellaneous Ops */ |
| 602 |
libssh2_sftp_rename_ex:= SafeGetProcAddress(libssh2, 'libssh2_sftp_rename_ex'); |
| 603 |
libssh2_sftp_unlink_ex:= SafeGetProcAddress(libssh2, 'libssh2_sftp_unlink_ex'); |
| 604 |
libssh2_sftp_statvfs:= SafeGetProcAddress(libssh2, 'libssh2_sftp_statvfs'); |
| 605 |
libssh2_sftp_mkdir_ex:= SafeGetProcAddress(libssh2, 'libssh2_sftp_mkdir_ex'); |
| 606 |
libssh2_sftp_rmdir_ex:= SafeGetProcAddress(libssh2, 'libssh2_sftp_rmdir_ex'); |
| 607 |
libssh2_sftp_stat_ex:= SafeGetProcAddress(libssh2, 'libssh2_sftp_stat_ex'); |
| 608 |
libssh2_sftp_symlink_ex:= SafeGetProcAddress(libssh2, 'libssh2_sftp_symlink_ex'); |
| 609 |
// Initialize the libssh2 functions |
| 610 |
if (libssh2_init(0) <> 0) then raise Exception.Create(EmptyStr); |
| 611 |
except |
| 612 |
FreeLibrary(libssh2); |
| 613 |
libssh2:= NilHandle; |
| 614 |
end; |
| 615 |
end; |
| 616 |
|
| 617 |
initialization |
| 618 |
Initialize; |
| 619 |
|
| 620 |
finalization |
| 621 |
if (libssh2 <> NilHandle) then |
| 622 |
begin |
| 623 |
libssh2_exit(); |
| 624 |
FreeLibrary(libssh2); |
| 625 |
end; |
| 626 |
|
| 627 |
end. |
| 628 |
|