-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcallbacks.pyx
More file actions
69 lines (58 loc) · 2.43 KB
/
callbacks.pyx
File metadata and controls
69 lines (58 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# This file is part of ssh-python.
# Copyright (C) 2018-2025 Panos Kittenis.
# Copyright (C) 2018-2025 ssh-python Contributors.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, version 2.1.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-130
from libc.stdlib cimport malloc, free
from libc.string cimport memset
from .session cimport Session
from .utils cimport handle_error_codes
from .c_ssh cimport ssh_auth_callback
from . cimport c_callbacks
cdef int auth_callback(const char *prompt, char *buf, size_t len,
int echo, int verify, void *userdata):
try:
func = <object>userdata
return func()
except Exception:
# TODO - pass back exception
return -1
# ssh_getpass(prompt, buf, len, echo, verify);
cdef class Callbacks:
def __cinit__(self):
self._cb = <c_callbacks.ssh_callbacks>malloc(
sizeof(c_callbacks.ssh_callbacks_struct))
if self._cb is NULL:
raise MemoryError
memset(self._cb, 0, sizeof(c_callbacks.ssh_callbacks_struct))
c_callbacks.ssh_callbacks_init(self._cb)
# self._cb.userdata = NULL
self._cb.auth_function = <c_callbacks.ssh_auth_callback>&auth_callback
# self._cb.log_function = NULL
# self._cb.connect_status_function = NULL
# self._cb.global_request_function = NULL
# self._cb.channel_open_request_x11_function = NULL
# self._cb.channel_open_request_auth_agent_function = NULL
def __dealloc__(self):
if self._cb is not NULL:
free(self._cb)
self._cb = NULL
def set_userdata(self, func not None):
self._cb.userdata = <void *>func
def set_callbacks(self, Session session not None):
cdef int rc
with nogil:
rc = c_callbacks.ssh_set_callbacks(
session._session, self._cb)
return handle_error_codes(rc, session._session)