2 * Unix SMB/CIFS implementation.
3 * Samba-internal work in progress Python binding for libsmbclient
5 * Copyright (C) Volker Lendecke 2012
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libsmb/libsmb.h"
24 #include "libcli/security/security.h"
25 #include "system/select.h"
26 #include "source4/libcli/util/pyerrors.h"
27 #include "auth/credentials/pycredentials.h"
29 static PyTypeObject
*get_pytype(const char *module
, const char *type
)
34 mod
= PyImport_ImportModule(module
);
36 PyErr_Format(PyExc_RuntimeError
,
37 "Unable to import %s to check type %s",
41 result
= (PyTypeObject
*)PyObject_GetAttrString(mod
, type
);
44 PyErr_Format(PyExc_RuntimeError
,
45 "Unable to find type %s in module %s",
56 struct cli_state
*cli
;
57 struct tevent_context
*ev
;
58 struct py_cli_thread
*thread_state
;
65 struct py_cli_thread
{
68 * Pipe to make the poll thread wake up in our destructor, so
69 * that we can exit and join the thread.
72 struct tevent_fd
*shutdown_fde
;
77 * Thread state to release the GIL during the poll(2) syscall
79 PyThreadState
*py_threadstate
;
82 static void *py_cli_state_poll_thread(void *private_data
)
84 struct py_cli_state
*self
= (struct py_cli_state
*)private_data
;
85 struct py_cli_thread
*t
= self
->thread_state
;
86 PyGILState_STATE gstate
;
88 gstate
= PyGILState_Ensure();
90 while (!t
->do_shutdown
) {
92 ret
= tevent_loop_once(self
->ev
);
95 PyGILState_Release(gstate
);
99 static void py_cli_state_trace_callback(enum tevent_trace_point point
,
102 struct py_cli_state
*self
= (struct py_cli_state
*)private_data
;
103 struct py_cli_thread
*t
= self
->thread_state
;
106 case TEVENT_TRACE_BEFORE_WAIT
:
107 assert(t
->py_threadstate
== NULL
);
108 t
->py_threadstate
= PyEval_SaveThread();
110 case TEVENT_TRACE_AFTER_WAIT
:
111 assert(t
->py_threadstate
!= NULL
);
112 PyEval_RestoreThread(t
->py_threadstate
);
113 t
->py_threadstate
= NULL
;
120 static void py_cli_state_shutdown_handler(struct tevent_context
*ev
,
121 struct tevent_fd
*fde
,
125 struct py_cli_state
*self
= (struct py_cli_state
*)private_data
;
126 struct py_cli_thread
*t
= self
->thread_state
;
128 if ((flags
& TEVENT_FD_READ
) == 0) {
131 TALLOC_FREE(t
->shutdown_fde
);
132 t
->do_shutdown
= true;
135 static int py_cli_thread_destructor(struct py_cli_thread
*t
)
143 * This will wake the poll thread from the poll(2)
145 written
= write(t
->shutdown_pipe
[1], &c
, 1);
146 } while ((written
== -1) && (errno
== EINTR
));
149 * Allow the poll thread to do its own cleanup under the GIL
151 Py_BEGIN_ALLOW_THREADS
152 ret
= pthread_join(t
->id
, NULL
);
156 if (t
->shutdown_pipe
[0] != -1) {
157 close(t
->shutdown_pipe
[0]);
158 t
->shutdown_pipe
[0] = -1;
160 if (t
->shutdown_pipe
[1] != -1) {
161 close(t
->shutdown_pipe
[1]);
162 t
->shutdown_pipe
[1] = -1;
167 static bool py_cli_state_setup_ev(struct py_cli_state
*self
)
169 struct py_cli_thread
*t
= NULL
;
172 self
->ev
= tevent_context_init_byname(NULL
, "poll_mt");
173 if (self
->ev
== NULL
) {
176 tevent_set_trace_callback(self
->ev
, py_cli_state_trace_callback
, self
);
178 self
->thread_state
= talloc_zero(NULL
, struct py_cli_thread
);
179 if (self
->thread_state
== NULL
) {
182 t
= self
->thread_state
;
184 ret
= pipe(t
->shutdown_pipe
);
188 t
->shutdown_fde
= tevent_add_fd(
189 self
->ev
, self
->ev
, t
->shutdown_pipe
[0], TEVENT_FD_READ
,
190 py_cli_state_shutdown_handler
, self
);
191 if (t
->shutdown_fde
== NULL
) {
195 PyEval_InitThreads();
197 ret
= pthread_create(&t
->id
, NULL
, py_cli_state_poll_thread
, self
);
201 talloc_set_destructor(self
->thread_state
, py_cli_thread_destructor
);
206 TALLOC_FREE(t
->shutdown_fde
);
208 if (t
->shutdown_pipe
[0] != -1) {
209 close(t
->shutdown_pipe
[0]);
210 t
->shutdown_pipe
[0] = -1;
212 if (t
->shutdown_pipe
[1] != -1) {
213 close(t
->shutdown_pipe
[1]);
214 t
->shutdown_pipe
[1] = -1;
218 TALLOC_FREE(self
->thread_state
);
219 TALLOC_FREE(self
->ev
);
223 struct py_tevent_cond
{
224 pthread_mutex_t mutex
;
229 static void py_tevent_signalme(struct tevent_req
*req
);
231 static int py_tevent_req_wait(struct tevent_context
*ev
,
232 struct tevent_req
*req
)
234 struct py_tevent_cond cond
;
237 result
= pthread_mutex_init(&cond
.mutex
, NULL
);
241 result
= pthread_cond_init(&cond
.cond
, NULL
);
246 cond
.is_done
= false;
247 tevent_req_set_callback(req
, py_tevent_signalme
, &cond
);
249 result
= pthread_mutex_lock(&cond
.mutex
);
254 while (!cond
.is_done
) {
256 Py_BEGIN_ALLOW_THREADS
257 result
= pthread_cond_wait(&cond
.cond
, &cond
.mutex
);
266 ret
= pthread_mutex_unlock(&cond
.mutex
);
269 ret
= pthread_cond_destroy(&cond
.cond
);
272 ret
= pthread_mutex_destroy(&cond
.mutex
);
278 static void py_tevent_signalme(struct tevent_req
*req
)
280 struct py_tevent_cond
*cond
= (struct py_tevent_cond
*)
281 tevent_req_callback_data_void(req
);
284 ret
= pthread_mutex_lock(&cond
->mutex
);
287 cond
->is_done
= true;
289 ret
= pthread_cond_signal(&cond
->cond
);
291 ret
= pthread_mutex_unlock(&cond
->mutex
);
297 static bool py_cli_state_setup_ev(struct py_cli_state
*self
)
299 self
->ev
= tevent_context_init(NULL
);
300 return (self
->ev
!= NULL
);
303 static int py_tevent_req_wait(struct tevent_context
*ev
,
304 struct tevent_req
*req
)
306 while (tevent_req_is_in_progress(req
)) {
309 ret
= tevent_loop_once(ev
);
319 static PyObject
*py_cli_state_new(PyTypeObject
*type
, PyObject
*args
,
322 struct py_cli_state
*self
;
324 self
= (struct py_cli_state
*)type
->tp_alloc(type
, 0);
330 self
->thread_state
= NULL
;
331 return (PyObject
*)self
;
334 static int py_cli_state_init(struct py_cli_state
*self
, PyObject
*args
,
340 struct cli_credentials
*cli_creds
;
343 static const char *kwlist
[] = {
344 "host", "share", "credentials", NULL
347 PyTypeObject
*py_type_Credentials
= get_pytype(
348 "samba.credentials", "Credentials");
349 if (py_type_Credentials
== NULL
) {
353 ret
= PyArg_ParseTupleAndKeywords(
354 args
, kwds
, "ss|O!", (char **)kwlist
,
355 &host
, &share
, py_type_Credentials
, &creds
);
357 Py_DECREF(py_type_Credentials
);
363 if (!py_cli_state_setup_ev(self
)) {
367 cli_creds
= cli_credentials_from_py_object(creds
);
368 if (cli_creds
== NULL
) {
369 PyErr_SetString(PyExc_TypeError
, "Expected credentials");
373 status
= cli_full_connection(
374 &self
->cli
, "myname", host
, NULL
, 0, share
, "?????",
375 cli_credentials_get_username(cli_creds
),
376 cli_credentials_get_domain(cli_creds
),
377 cli_credentials_get_password(cli_creds
),
379 if (!NT_STATUS_IS_OK(status
)) {
380 PyErr_SetNTSTATUS(status
);
386 static void py_cli_state_dealloc(struct py_cli_state
*self
)
388 TALLOC_FREE(self
->thread_state
);
389 TALLOC_FREE(self
->ev
);
391 if (self
->cli
!= NULL
) {
392 cli_shutdown(self
->cli
);
395 self
->ob_type
->tp_free((PyObject
*)self
);
398 static bool py_tevent_req_wait_exc(struct tevent_context
*ev
,
399 struct tevent_req
*req
)
407 ret
= py_tevent_req_wait(ev
, req
);
411 PyErr_SetFromErrno(PyExc_RuntimeError
);
417 static PyObject
*py_cli_create(struct py_cli_state
*self
, PyObject
*args
,
421 unsigned CreateFlags
= 0;
422 unsigned DesiredAccess
= FILE_GENERIC_READ
;
423 unsigned FileAttributes
= 0;
424 unsigned ShareAccess
= 0;
425 unsigned CreateDisposition
= FILE_OPEN
;
426 unsigned CreateOptions
= 0;
427 unsigned SecurityFlags
= 0;
429 struct tevent_req
*req
;
432 static const char *kwlist
[] = {
433 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
434 "ShareAccess", "CreateDisposition", "CreateOptions",
435 "SecurityFlags", NULL
};
437 if (!PyArg_ParseTupleAndKeywords(
438 args
, kwds
, "s|IIIIIII", (char **)kwlist
,
439 &fname
, &CreateFlags
, &DesiredAccess
, &FileAttributes
,
440 &ShareAccess
, &CreateDisposition
, &CreateOptions
,
445 req
= cli_ntcreate_send(NULL
, self
->ev
, self
->cli
, fname
, CreateFlags
,
446 DesiredAccess
, FileAttributes
, ShareAccess
,
447 CreateDisposition
, CreateOptions
,
449 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
452 status
= cli_ntcreate_recv(req
, &fnum
);
455 if (!NT_STATUS_IS_OK(status
)) {
456 PyErr_SetNTSTATUS(status
);
459 return Py_BuildValue("I", (unsigned)fnum
);
462 static PyObject
*py_cli_close(struct py_cli_state
*self
, PyObject
*args
)
464 struct tevent_req
*req
;
468 if (!PyArg_ParseTuple(args
, "i", &fnum
)) {
472 req
= cli_close_send(NULL
, self
->ev
, self
->cli
, fnum
);
473 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
476 status
= cli_close_recv(req
);
479 if (!NT_STATUS_IS_OK(status
)) {
480 PyErr_SetNTSTATUS(status
);
487 static PyObject
*py_cli_write(struct py_cli_state
*self
, PyObject
*args
,
494 unsigned long long offset
;
495 struct tevent_req
*req
;
499 static const char *kwlist
[] = {
500 "fnum", "buffer", "offset", "mode", NULL
};
502 if (!PyArg_ParseTupleAndKeywords(
503 args
, kwds
, "Is#K|I", (char **)kwlist
,
504 &fnum
, &buf
, &buflen
, &offset
, &mode
)) {
508 req
= cli_write_andx_send(NULL
, self
->ev
, self
->cli
, fnum
, mode
,
509 (uint8_t *)buf
, offset
, buflen
);
510 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
513 status
= cli_write_andx_recv(req
, &written
);
516 if (!NT_STATUS_IS_OK(status
)) {
517 PyErr_SetNTSTATUS(status
);
520 return Py_BuildValue("K", (unsigned long long)written
);
523 static PyObject
*py_cli_read(struct py_cli_state
*self
, PyObject
*args
,
527 unsigned long long offset
;
529 struct tevent_req
*req
;
535 static const char *kwlist
[] = {
536 "fnum", "offset", "size", NULL
};
538 if (!PyArg_ParseTupleAndKeywords(
539 args
, kwds
, "IKI", (char **)kwlist
, &fnum
, &offset
,
544 req
= cli_read_andx_send(NULL
, self
->ev
, self
->cli
, fnum
,
546 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
549 status
= cli_read_andx_recv(req
, &buflen
, &buf
);
551 if (!NT_STATUS_IS_OK(status
)) {
553 PyErr_SetNTSTATUS(status
);
556 result
= Py_BuildValue("s#", (char *)buf
, (int)buflen
);
561 static PyObject
*py_cli_ftruncate(struct py_cli_state
*self
, PyObject
*args
,
565 unsigned long long size
;
566 struct tevent_req
*req
;
569 static const char *kwlist
[] = {
570 "fnum", "size", NULL
};
572 if (!PyArg_ParseTupleAndKeywords(
573 args
, kwds
, "IK", (char **)kwlist
, &fnum
, &size
)) {
577 req
= cli_ftruncate_send(NULL
, self
->ev
, self
->cli
, fnum
, size
);
578 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
581 status
= cli_ftruncate_recv(req
);
584 if (!NT_STATUS_IS_OK(status
)) {
585 PyErr_SetNTSTATUS(status
);
592 static PyObject
*py_cli_delete_on_close(struct py_cli_state
*self
,
597 struct tevent_req
*req
;
600 static const char *kwlist
[] = {
601 "fnum", "flag", NULL
};
603 if (!PyArg_ParseTupleAndKeywords(
604 args
, kwds
, "II", (char **)kwlist
, &fnum
, &flag
)) {
608 req
= cli_nt_delete_on_close_send(NULL
, self
->ev
, self
->cli
, fnum
,
610 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
613 status
= cli_nt_delete_on_close_recv(req
);
616 if (!NT_STATUS_IS_OK(status
)) {
617 PyErr_SetNTSTATUS(status
);
624 static PyMethodDef py_cli_state_methods
[] = {
625 { "create", (PyCFunction
)py_cli_create
, METH_VARARGS
|METH_KEYWORDS
,
627 { "close", (PyCFunction
)py_cli_close
, METH_VARARGS
,
628 "Close a file handle" },
629 { "write", (PyCFunction
)py_cli_write
, METH_VARARGS
|METH_KEYWORDS
,
630 "Write to a file handle" },
631 { "read", (PyCFunction
)py_cli_read
, METH_VARARGS
|METH_KEYWORDS
,
632 "Read from a file handle" },
633 { "truncate", (PyCFunction
)py_cli_ftruncate
,
634 METH_VARARGS
|METH_KEYWORDS
,
636 { "delete_on_close", (PyCFunction
)py_cli_delete_on_close
,
637 METH_VARARGS
|METH_KEYWORDS
,
638 "Set/Reset the delete on close flag" },
639 { NULL
, NULL
, 0, NULL
}
642 static PyTypeObject py_cli_state_type
= {
643 PyObject_HEAD_INIT(NULL
)
644 .tp_name
= "libsmb_samba_internal.Conn",
645 .tp_basicsize
= sizeof(struct py_cli_state
),
646 .tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
,
647 .tp_doc
= "libsmb connection",
648 .tp_new
= py_cli_state_new
,
649 .tp_init
= (initproc
)py_cli_state_init
,
650 .tp_dealloc
= (destructor
)py_cli_state_dealloc
,
651 .tp_methods
= py_cli_state_methods
,
654 static PyMethodDef py_libsmb_methods
[] = {
658 void initlibsmb_samba_internal(void);
659 void initlibsmb_samba_internal(void)
665 m
= Py_InitModule3("libsmb_samba_internal", py_libsmb_methods
,
668 if (PyType_Ready(&py_cli_state_type
) < 0) {
671 Py_INCREF(&py_cli_state_type
);
672 PyModule_AddObject(m
, "Conn", (PyObject
*)&py_cli_state_type
);