Skip to content

Commit e3bc292

Browse files
author
Pan
committed
Added SFTP implementation. Updated SFTP definitions. Updated ssh definitions for cross-platform compatibility.
1 parent 44ea2e4 commit e3bc292

11 files changed

+496
-97
lines changed

ssh/c_sftp.pxd

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-130
1616

1717
from c_ssh cimport uint64_t, uint32_t, uint8_t, ssh_string, ssh_buffer, \
18-
ssh_channel, ssh_session
18+
ssh_channel, ssh_session, timeval
19+
1920

2021
cdef extern from "libssh/sftp.h" nogil:
22+
ctypedef long mode_t
23+
ctypedef long uid_t
24+
ctypedef long gid_t
2125
ctypedef sftp_attributes_struct* sftp_attributes
2226
ctypedef sftp_client_message_struct* sftp_client_message
2327
ctypedef sftp_dir_struct* sftp_dir
@@ -133,3 +137,160 @@ cdef extern from "libssh/sftp.h" nogil:
133137
uint64_t f_fsid
134138
uint64_t f_flag
135139
uint64_t f_namemax
140+
sftp_session sftp_new(ssh_session session)
141+
sftp_session sftp_new_channel(ssh_session session, ssh_channel channel)
142+
void sftp_free(sftp_session sftp)
143+
int sftp_init(sftp_session sftp)
144+
int sftp_get_error(sftp_session sftp)
145+
unsigned int sftp_extensions_get_count(sftp_session sftp)
146+
const char *sftp_extensions_get_name(sftp_session sftp, unsigned int indexn)
147+
const char *sftp_extensions_get_data(sftp_session sftp, unsigned int indexn)
148+
int sftp_extension_supported(sftp_session sftp, const char *name,
149+
const char *data)
150+
sftp_dir sftp_opendir(sftp_session session, const char *path)
151+
sftp_attributes sftp_readdir(sftp_session session, sftp_dir dir)
152+
int sftp_dir_eof(sftp_dir dir)
153+
sftp_attributes sftp_stat(sftp_session session, const char *path)
154+
sftp_attributes sftp_lstat(sftp_session session, const char *path)
155+
sftp_attributes sftp_fstat(sftp_file file)
156+
void sftp_attributes_free(sftp_attributes file)
157+
int sftp_closedir(sftp_dir dir)
158+
int sftp_close(sftp_file file)
159+
sftp_file sftp_open(sftp_session session, const char *file, int accesstype,
160+
mode_t mode)
161+
void sftp_file_set_nonblocking(sftp_file handle)
162+
void sftp_file_set_blocking(sftp_file handle)
163+
ssize_t sftp_read(sftp_file file, void *buf, size_t count)
164+
int sftp_async_read_begin(sftp_file file, uint32_t len)
165+
int sftp_async_read(sftp_file file, void *data, uint32_t len, uint32_t id)
166+
ssize_t sftp_write(sftp_file file, const void *buf, size_t count)
167+
int sftp_seek(sftp_file file, uint32_t new_offset)
168+
int sftp_seek64(sftp_file file, uint64_t new_offset)
169+
unsigned long sftp_tell(sftp_file file)
170+
uint64_t sftp_tell64(sftp_file file)
171+
void sftp_rewind(sftp_file file)
172+
int sftp_unlink(sftp_session sftp, const char *file)
173+
int sftp_rmdir(sftp_session sftp, const char *directory)
174+
int sftp_mkdir(sftp_session sftp, const char *directory, mode_t mode)
175+
int sftp_rename(sftp_session sftp, const char *original, const char *newname)
176+
int sftp_setstat(sftp_session sftp, const char *file, sftp_attributes attr)
177+
int sftp_chown(sftp_session sftp, const char *file, uid_t owner, gid_t group)
178+
int sftp_chmod(sftp_session sftp, const char *file, mode_t mode)
179+
int sftp_utimes(sftp_session sftp, const char *file, const timeval *times)
180+
int sftp_symlink(sftp_session sftp, const char *target, const char *dest)
181+
char *sftp_readlink(sftp_session sftp, const char *path)
182+
sftp_statvfs_t sftp_statvfs(sftp_session sftp, const char *path)
183+
sftp_statvfs_t sftp_fstatvfs(sftp_file file)
184+
void sftp_statvfs_free(sftp_statvfs_t statvfs_o)
185+
int sftp_fsync(sftp_file file)
186+
char *sftp_canonicalize_path(sftp_session sftp, const char *path)
187+
int sftp_server_version(sftp_session sftp)
188+
189+
## Server
190+
# sftp_session sftp_server_new(ssh_session session, ssh_channel chan)
191+
# int sftp_server_init(sftp_session sftp)
192+
enum:
193+
SSH_FXP_INIT
194+
SSH_FXP_VERSION
195+
SSH_FXP_OPEN
196+
SSH_FXP_CLOSE
197+
SSH_FXP_READ
198+
SSH_FXP_WRITE
199+
SSH_FXP_LSTAT
200+
SSH_FXP_FSTAT
201+
SSH_FXP_SETSTAT
202+
SSH_FXP_FSETSTAT
203+
SSH_FXP_OPENDIR
204+
SSH_FXP_READDIR
205+
SSH_FXP_REMOVE
206+
SSH_FXP_MKDIR
207+
SSH_FXP_RMDIR
208+
SSH_FXP_REALPATH
209+
SSH_FXP_STAT
210+
SSH_FXP_RENAME
211+
SSH_FXP_READLINK
212+
SSH_FXP_SYMLINK
213+
SSH_FXP_STATUS
214+
SSH_FXP_HANDLE
215+
SSH_FXP_DATA
216+
SSH_FXP_NAME
217+
SSH_FXP_ATTRS
218+
SSH_FXP_EXTENDED
219+
SSH_FXP_EXTENDED_REPLY
220+
enum:
221+
SSH_FILEXFER_ATTR_SIZE
222+
SSH_FILEXFER_ATTR_PERMISSIONS
223+
SSH_FILEXFER_ATTR_ACCESSTIME
224+
SSH_FILEXFER_ATTR_ACMODTIME
225+
SSH_FILEXFER_ATTR_CREATETIME
226+
SSH_FILEXFER_ATTR_MODIFYTIME
227+
SSH_FILEXFER_ATTR_ACL
228+
SSH_FILEXFER_ATTR_OWNERGROUP
229+
SSH_FILEXFER_ATTR_SUBSECOND_TIMES
230+
SSH_FILEXFER_ATTR_EXTENDED
231+
SSH_FILEXFER_ATTR_UIDGID
232+
# Types
233+
enum:
234+
SSH_FILEXFER_TYPE_REGULAR
235+
SSH_FILEXFER_TYPE_DIRECTORY
236+
SSH_FILEXFER_TYPE_SYMLINK
237+
SSH_FILEXFER_TYPE_SPECIAL
238+
SSH_FILEXFER_TYPE_UNKNOWN
239+
enum:
240+
SSH_FX_OK
241+
SSH_FX_EOF
242+
SSH_FX_NO_SUCH_FILE
243+
SSH_FX_PERMISSION_DENIED
244+
SSH_FX_FAILURE
245+
SSH_FX_BAD_MESSAGE
246+
SSH_FX_NO_CONNECTION
247+
SSH_FX_CONNECTION_LOST
248+
SSH_FX_OP_UNSUPPORTED
249+
SSH_FX_INVALID_HANDLE
250+
SSH_FX_NO_SUCH_PATH
251+
SSH_FX_FILE_ALREADY_EXISTS
252+
SSH_FX_WRITE_PROTECT
253+
SSH_FX_NO_MEDIA
254+
enum:
255+
SSH_FXF_READ
256+
SSH_FXF_WRITE
257+
SSH_FXF_APPEND
258+
SSH_FXF_CREAT
259+
SSH_FXF_TRUNC
260+
SSH_FXF_EXCL
261+
SSH_FXF_TEXT
262+
enum:
263+
SSH_S_IFMT
264+
SSH_S_IFSOCK
265+
SSH_S_IFLNK
266+
SSH_S_IFREG
267+
SSH_S_IFBLK
268+
SSH_S_IFDIR
269+
SSH_S_IFCHR
270+
SSH_S_IFIFO
271+
enum:
272+
SSH_FXF_RENAME_OVERWRITE
273+
SSH_FXF_RENAME_ATOMIC
274+
SSH_FXF_RENAME_NATIVE
275+
enum:
276+
SFTP_OPEN
277+
SFTP_CLOSE
278+
SFTP_READ
279+
SFTP_WRITE
280+
SFTP_LSTAT
281+
SFTP_FSTAT
282+
SFTP_SETSTAT
283+
SFTP_FSETSTAT
284+
SFTP_OPENDIR
285+
SFTP_READDIR
286+
SFTP_REMOVE
287+
SFTP_MKDIR
288+
SFTP_RMDIR
289+
SFTP_REALPATH
290+
SFTP_STAT
291+
SFTP_RENAME
292+
SFTP_READLINK
293+
SFTP_SYMLINK
294+
enum:
295+
SSH_FXE_STATVFS_ST_RDONLY
296+
SSH_FXE_STATVFS_ST_NOSUID

ssh/c_ssh.pxd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
# License along with this library; if not, write to the Free Software
1515
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-130
1616

17-
from posix.time cimport timeval
1817
from posix.select cimport fd_set
1918
from posix.types cimport mode_t
2019

2120

2221
cdef extern from "libssh/libssh.h" nogil:
22+
ctypedef long time_t
23+
ctypedef long subseconds_t
24+
cdef struct timeval:
25+
time_t tv_sec
26+
subseconds_t tv_usec
2327
ctypedef unsigned char uint8_t
2428
ctypedef unsigned short uint16_t
2529
ctypedef unsigned int uint32_t

ssh/c_ssh2.pxd

Lines changed: 0 additions & 86 deletions
This file was deleted.

ssh/callbacks.pyx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ from session cimport Session
2121
from utils cimport handle_ssh_error_codes
2222

2323
cimport c_callbacks
24-
# from c_ssh cimport ssh_auth_callback
24+
from c_ssh cimport ssh_auth_callback
2525

2626

27-
# cdef int auth_callback(const char *prompt, char *buf, size_t len,
28-
# int echo, int verify, void *userdata):
29-
# # (void) verify;
30-
# # (void) userdata;
31-
32-
# return 0
33-
# # ssh_getpass(prompt, buf, len, echo, verify);
27+
cdef int auth_callback(const char *prompt, char *buf, size_t len,
28+
int echo, int verify, void *userdata):
29+
try:
30+
func = <object>userdata
31+
return func()
32+
except Exception:
33+
# TODO - pass back exception
34+
return -1
35+
# ssh_getpass(prompt, buf, len, echo, verify);
3436

3537

3638
cdef class Callbacks:

ssh/channel.pyx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ cdef class Channel:
2727

2828
def __cinit__(self, Session session):
2929
self.session = session
30-
self._channel = NULL
3130

3231
def __dealloc__(self):
3332
if self._channel is not NULL:

ssh/event.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ cdef class Event:
6060
func = <object>userdata
6161
return func()
6262
except Exception:
63+
# TODO - pass back exception
6364
return -1
6465

6566
def add_fd(self, sock, short events, callback=None):

ssh/session.pyx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ from utils cimport to_bytes, to_str, handle_ssh_error_codes, \
2424
handle_auth_error_codes
2525
from options cimport Option
2626
from key cimport SSHKey
27+
from sftp cimport SFTP
2728

2829
from exceptions import OptionError, InvalidAPIUse
2930

3031
cimport c_ssh
32+
from c_sftp cimport sftp_session, sftp_new
3133

3234

3335
cdef bint _check_connected(c_ssh.ssh_session session) nogil except -1:
@@ -82,6 +84,17 @@ cdef class Session:
8284
channel = Channel.from_ptr(_channel, self)
8385
return channel
8486

87+
def sftp_new(self):
88+
cdef sftp_session _sftp
89+
cdef SFTP sftp
90+
with nogil:
91+
_check_connected(self._session)
92+
_sftp = sftp_new(self._session)
93+
if _sftp is NULL:
94+
return handle_ssh_error_codes(
95+
c_ssh.ssh_get_error_code(self._session), self._session)
96+
return SFTP.from_ptr(_sftp, self)
97+
8598
def connect(self):
8699
cdef int rc
87100
with nogil:

ssh/sftp.pxd

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This file is part of ssh-python.
2+
# Copyright (C) 2018 Panos Kittenis
3+
#
4+
# This library is free software; you can redistribute it and/or
5+
# modify it under the terms of the GNU Lesser General Public
6+
# License as published by the Free Software Foundation, version 2.1.
7+
#
8+
# This library is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
# Lesser General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU Lesser General Public
14+
# License along with this library; if not, write to the Free Software
15+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-130
16+
17+
from session cimport Session
18+
19+
cimport c_sftp
20+
21+
22+
cdef class SFTPStatVFS:
23+
cdef c_sftp.sftp_statvfs_t _stats
24+
25+
26+
cdef class SFTPAttributes:
27+
cdef c_sftp.sftp_attributes _attrs
28+
29+
30+
cdef class SFTP:
31+
cdef c_sftp.sftp_session _sftp
32+
cdef Session session
33+
34+
@staticmethod
35+
cdef SFTP from_ptr(c_sftp.sftp_session sftp, Session session)

0 commit comments

Comments
 (0)