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"
30 static PyTypeObject
*get_pytype(const char *module
, const char *type
)
35 mod
= PyImport_ImportModule(module
);
37 PyErr_Format(PyExc_RuntimeError
,
38 "Unable to import %s to check type %s",
42 result
= (PyTypeObject
*)PyObject_GetAttrString(mod
, type
);
45 PyErr_Format(PyExc_RuntimeError
,
46 "Unable to find type %s in module %s",
54 * We're using "const char **" for keywords,
55 * PyArg_ParseTupleAndKeywords expects a "char **". Confine the
56 * inevitable warnings to just one place.
58 static int ParseTupleAndKeywords(PyObject
*args
, PyObject
*kw
,
59 const char *format
, const char **keywords
,
64 va_start(a
, keywords
);
65 ret
= PyArg_VaParseTupleAndKeywords(args
, kw
, format
,
66 (char **)keywords
, a
);
73 struct py_cli_oplock_break
{
80 struct cli_state
*cli
;
81 struct tevent_context
*ev
;
82 struct py_cli_thread
*thread_state
;
84 struct tevent_req
*oplock_waiter
;
85 struct py_cli_oplock_break
*oplock_breaks
;
86 struct py_tevent_cond
*oplock_cond
;
93 struct py_cli_thread
{
96 * Pipe to make the poll thread wake up in our destructor, so
97 * that we can exit and join the thread.
100 struct tevent_fd
*shutdown_fde
;
105 * Thread state to release the GIL during the poll(2) syscall
107 PyThreadState
*py_threadstate
;
110 static void *py_cli_state_poll_thread(void *private_data
)
112 struct py_cli_state
*self
= (struct py_cli_state
*)private_data
;
113 struct py_cli_thread
*t
= self
->thread_state
;
114 PyGILState_STATE gstate
;
116 gstate
= PyGILState_Ensure();
118 while (!t
->do_shutdown
) {
120 ret
= tevent_loop_once(self
->ev
);
123 PyGILState_Release(gstate
);
127 static void py_cli_state_trace_callback(enum tevent_trace_point point
,
130 struct py_cli_state
*self
= (struct py_cli_state
*)private_data
;
131 struct py_cli_thread
*t
= self
->thread_state
;
134 case TEVENT_TRACE_BEFORE_WAIT
:
135 assert(t
->py_threadstate
== NULL
);
136 t
->py_threadstate
= PyEval_SaveThread();
138 case TEVENT_TRACE_AFTER_WAIT
:
139 assert(t
->py_threadstate
!= NULL
);
140 PyEval_RestoreThread(t
->py_threadstate
);
141 t
->py_threadstate
= NULL
;
148 static void py_cli_state_shutdown_handler(struct tevent_context
*ev
,
149 struct tevent_fd
*fde
,
153 struct py_cli_state
*self
= (struct py_cli_state
*)private_data
;
154 struct py_cli_thread
*t
= self
->thread_state
;
156 if ((flags
& TEVENT_FD_READ
) == 0) {
159 TALLOC_FREE(t
->shutdown_fde
);
160 t
->do_shutdown
= true;
163 static int py_cli_thread_destructor(struct py_cli_thread
*t
)
171 * This will wake the poll thread from the poll(2)
173 written
= write(t
->shutdown_pipe
[1], &c
, 1);
174 } while ((written
== -1) && (errno
== EINTR
));
177 * Allow the poll thread to do its own cleanup under the GIL
179 Py_BEGIN_ALLOW_THREADS
180 ret
= pthread_join(t
->id
, NULL
);
184 if (t
->shutdown_pipe
[0] != -1) {
185 close(t
->shutdown_pipe
[0]);
186 t
->shutdown_pipe
[0] = -1;
188 if (t
->shutdown_pipe
[1] != -1) {
189 close(t
->shutdown_pipe
[1]);
190 t
->shutdown_pipe
[1] = -1;
195 static bool py_cli_state_setup_ev(struct py_cli_state
*self
)
197 struct py_cli_thread
*t
= NULL
;
200 self
->ev
= tevent_context_init_byname(NULL
, "poll_mt");
201 if (self
->ev
== NULL
) {
204 tevent_set_trace_callback(self
->ev
, py_cli_state_trace_callback
, self
);
206 self
->thread_state
= talloc_zero(NULL
, struct py_cli_thread
);
207 if (self
->thread_state
== NULL
) {
210 t
= self
->thread_state
;
212 ret
= pipe(t
->shutdown_pipe
);
216 t
->shutdown_fde
= tevent_add_fd(
217 self
->ev
, self
->ev
, t
->shutdown_pipe
[0], TEVENT_FD_READ
,
218 py_cli_state_shutdown_handler
, self
);
219 if (t
->shutdown_fde
== NULL
) {
223 PyEval_InitThreads();
225 ret
= pthread_create(&t
->id
, NULL
, py_cli_state_poll_thread
, self
);
229 talloc_set_destructor(self
->thread_state
, py_cli_thread_destructor
);
234 TALLOC_FREE(t
->shutdown_fde
);
236 if (t
->shutdown_pipe
[0] != -1) {
237 close(t
->shutdown_pipe
[0]);
238 t
->shutdown_pipe
[0] = -1;
240 if (t
->shutdown_pipe
[1] != -1) {
241 close(t
->shutdown_pipe
[1]);
242 t
->shutdown_pipe
[1] = -1;
246 TALLOC_FREE(self
->thread_state
);
247 TALLOC_FREE(self
->ev
);
251 struct py_tevent_cond
{
252 pthread_mutex_t mutex
;
257 static void py_tevent_signalme(struct tevent_req
*req
);
259 static int py_tevent_cond_wait(struct py_tevent_cond
*cond
)
263 result
= pthread_mutex_init(&cond
->mutex
, NULL
);
267 result
= pthread_cond_init(&cond
->cond
, NULL
);
272 result
= pthread_mutex_lock(&cond
->mutex
);
277 cond
->is_done
= false;
279 while (!cond
->is_done
) {
281 Py_BEGIN_ALLOW_THREADS
282 result
= pthread_cond_wait(&cond
->cond
, &cond
->mutex
);
291 ret
= pthread_mutex_unlock(&cond
->mutex
);
294 ret
= pthread_cond_destroy(&cond
->cond
);
297 ret
= pthread_mutex_destroy(&cond
->mutex
);
303 static int py_tevent_req_wait(struct tevent_context
*ev
,
304 struct tevent_req
*req
)
306 struct py_tevent_cond cond
;
307 tevent_req_set_callback(req
, py_tevent_signalme
, &cond
);
308 return py_tevent_cond_wait(&cond
);
311 static void py_tevent_cond_signal(struct py_tevent_cond
*cond
)
315 ret
= pthread_mutex_lock(&cond
->mutex
);
318 cond
->is_done
= true;
320 ret
= pthread_cond_signal(&cond
->cond
);
322 ret
= pthread_mutex_unlock(&cond
->mutex
);
326 static void py_tevent_signalme(struct tevent_req
*req
)
328 struct py_tevent_cond
*cond
= (struct py_tevent_cond
*)
329 tevent_req_callback_data_void(req
);
331 py_tevent_cond_signal(cond
);
336 static bool py_cli_state_setup_ev(struct py_cli_state
*self
)
338 self
->ev
= tevent_context_init(NULL
);
339 return (self
->ev
!= NULL
);
342 static int py_tevent_req_wait(struct tevent_context
*ev
,
343 struct tevent_req
*req
)
345 while (tevent_req_is_in_progress(req
)) {
348 ret
= tevent_loop_once(ev
);
358 static bool py_tevent_req_wait_exc(struct tevent_context
*ev
,
359 struct tevent_req
*req
)
367 ret
= py_tevent_req_wait(ev
, req
);
371 PyErr_SetFromErrno(PyExc_RuntimeError
);
377 static PyObject
*py_cli_state_new(PyTypeObject
*type
, PyObject
*args
,
380 struct py_cli_state
*self
;
382 self
= (struct py_cli_state
*)type
->tp_alloc(type
, 0);
388 self
->thread_state
= NULL
;
389 self
->oplock_waiter
= NULL
;
390 self
->oplock_cond
= NULL
;
391 self
->oplock_breaks
= NULL
;
392 return (PyObject
*)self
;
395 static void py_cli_got_oplock_break(struct tevent_req
*req
);
397 static int py_cli_state_init(struct py_cli_state
*self
, PyObject
*args
,
403 struct cli_credentials
*cli_creds
;
406 static const char *kwlist
[] = {
407 "host", "share", "credentials", NULL
410 PyTypeObject
*py_type_Credentials
= get_pytype(
411 "samba.credentials", "Credentials");
412 if (py_type_Credentials
== NULL
) {
416 ret
= ParseTupleAndKeywords(
417 args
, kwds
, "ss|O!", kwlist
,
418 &host
, &share
, py_type_Credentials
, &creds
);
420 Py_DECREF(py_type_Credentials
);
426 if (!py_cli_state_setup_ev(self
)) {
430 cli_creds
= cli_credentials_from_py_object(creds
);
431 if (cli_creds
== NULL
) {
432 PyErr_SetString(PyExc_TypeError
, "Expected credentials");
436 status
= cli_full_connection(
437 &self
->cli
, "myname", host
, NULL
, 0, share
, "?????",
438 cli_credentials_get_username(cli_creds
),
439 cli_credentials_get_domain(cli_creds
),
440 cli_credentials_get_password(cli_creds
),
442 if (!NT_STATUS_IS_OK(status
)) {
443 PyErr_SetNTSTATUS(status
);
447 self
->oplock_waiter
= cli_smb_oplock_break_waiter_send(
448 self
->ev
, self
->ev
, self
->cli
);
449 if (self
->oplock_waiter
== NULL
) {
453 tevent_req_set_callback(self
->oplock_waiter
, py_cli_got_oplock_break
,
458 static void py_cli_got_oplock_break(struct tevent_req
*req
)
460 struct py_cli_state
*self
= (struct py_cli_state
*)
461 tevent_req_callback_data_void(req
);
462 struct py_cli_oplock_break b
;
463 struct py_cli_oplock_break
*tmp
;
467 status
= cli_smb_oplock_break_waiter_recv(req
, &b
.fnum
, &b
.level
);
469 self
->oplock_waiter
= NULL
;
471 if (!NT_STATUS_IS_OK(status
)) {
475 num_breaks
= talloc_array_length(self
->oplock_breaks
);
476 tmp
= talloc_realloc(self
->ev
, self
->oplock_breaks
,
477 struct py_cli_oplock_break
, num_breaks
+1);
481 self
->oplock_breaks
= tmp
;
482 self
->oplock_breaks
[num_breaks
] = b
;
484 if (self
->oplock_cond
!= NULL
) {
485 py_tevent_cond_signal(self
->oplock_cond
);
488 self
->oplock_waiter
= cli_smb_oplock_break_waiter_send(
489 self
->ev
, self
->ev
, self
->cli
);
490 if (self
->oplock_waiter
== NULL
) {
493 tevent_req_set_callback(self
->oplock_waiter
, py_cli_got_oplock_break
,
497 static PyObject
*py_cli_get_oplock_break(struct py_cli_state
*self
,
500 size_t num_oplock_breaks
;
502 if (!PyArg_ParseTuple(args
, "")) {
506 if (self
->oplock_cond
!= NULL
) {
508 PyErr_SetFromErrno(PyExc_RuntimeError
);
512 num_oplock_breaks
= talloc_array_length(self
->oplock_breaks
);
514 if (num_oplock_breaks
== 0) {
515 struct py_tevent_cond cond
;
518 self
->oplock_cond
= &cond
;
519 ret
= py_tevent_cond_wait(&cond
);
520 self
->oplock_cond
= NULL
;
524 PyErr_SetFromErrno(PyExc_RuntimeError
);
529 num_oplock_breaks
= talloc_array_length(self
->oplock_breaks
);
530 if (num_oplock_breaks
> 0) {
533 result
= Py_BuildValue(
535 "fnum", self
->oplock_breaks
[0].fnum
,
536 "level", self
->oplock_breaks
[0].level
);
538 memmove(&self
->oplock_breaks
[0], &self
->oplock_breaks
[1],
539 sizeof(self
->oplock_breaks
[0]) *
540 (num_oplock_breaks
- 1));
541 self
->oplock_breaks
= talloc_realloc(
542 NULL
, self
->oplock_breaks
, struct py_cli_oplock_break
,
543 num_oplock_breaks
- 1);
550 static void py_cli_state_dealloc(struct py_cli_state
*self
)
552 TALLOC_FREE(self
->thread_state
);
553 TALLOC_FREE(self
->oplock_waiter
);
554 TALLOC_FREE(self
->ev
);
556 if (self
->cli
!= NULL
) {
557 cli_shutdown(self
->cli
);
560 self
->ob_type
->tp_free((PyObject
*)self
);
563 static PyObject
*py_cli_create(struct py_cli_state
*self
, PyObject
*args
,
567 unsigned CreateFlags
= 0;
568 unsigned DesiredAccess
= FILE_GENERIC_READ
;
569 unsigned FileAttributes
= 0;
570 unsigned ShareAccess
= 0;
571 unsigned CreateDisposition
= FILE_OPEN
;
572 unsigned CreateOptions
= 0;
573 unsigned SecurityFlags
= 0;
575 struct tevent_req
*req
;
578 static const char *kwlist
[] = {
579 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
580 "ShareAccess", "CreateDisposition", "CreateOptions",
581 "SecurityFlags", NULL
};
583 if (!ParseTupleAndKeywords(
584 args
, kwds
, "s|IIIIIII", kwlist
,
585 &fname
, &CreateFlags
, &DesiredAccess
, &FileAttributes
,
586 &ShareAccess
, &CreateDisposition
, &CreateOptions
,
591 req
= cli_ntcreate_send(NULL
, self
->ev
, self
->cli
, fname
, CreateFlags
,
592 DesiredAccess
, FileAttributes
, ShareAccess
,
593 CreateDisposition
, CreateOptions
,
595 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
598 status
= cli_ntcreate_recv(req
, &fnum
);
601 if (!NT_STATUS_IS_OK(status
)) {
602 PyErr_SetNTSTATUS(status
);
605 return Py_BuildValue("I", (unsigned)fnum
);
608 static PyObject
*py_cli_close(struct py_cli_state
*self
, PyObject
*args
)
610 struct tevent_req
*req
;
614 if (!PyArg_ParseTuple(args
, "i", &fnum
)) {
618 req
= cli_close_send(NULL
, self
->ev
, self
->cli
, fnum
);
619 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
622 status
= cli_close_recv(req
);
625 if (!NT_STATUS_IS_OK(status
)) {
626 PyErr_SetNTSTATUS(status
);
632 static PyObject
*py_cli_write(struct py_cli_state
*self
, PyObject
*args
,
639 unsigned long long offset
;
640 struct tevent_req
*req
;
644 static const char *kwlist
[] = {
645 "fnum", "buffer", "offset", "mode", NULL
};
647 if (!ParseTupleAndKeywords(
648 args
, kwds
, "Is#K|I", kwlist
,
649 &fnum
, &buf
, &buflen
, &offset
, &mode
)) {
653 req
= cli_write_andx_send(NULL
, self
->ev
, self
->cli
, fnum
, mode
,
654 (uint8_t *)buf
, offset
, buflen
);
655 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
658 status
= cli_write_andx_recv(req
, &written
);
661 if (!NT_STATUS_IS_OK(status
)) {
662 PyErr_SetNTSTATUS(status
);
665 return Py_BuildValue("K", (unsigned long long)written
);
668 static PyObject
*py_cli_read(struct py_cli_state
*self
, PyObject
*args
,
672 unsigned long long offset
;
674 struct tevent_req
*req
;
680 static const char *kwlist
[] = {
681 "fnum", "offset", "size", NULL
};
683 if (!ParseTupleAndKeywords(
684 args
, kwds
, "IKI", kwlist
, &fnum
, &offset
,
689 req
= cli_read_andx_send(NULL
, self
->ev
, self
->cli
, fnum
,
691 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
694 status
= cli_read_andx_recv(req
, &buflen
, &buf
);
696 if (!NT_STATUS_IS_OK(status
)) {
698 PyErr_SetNTSTATUS(status
);
701 result
= Py_BuildValue("s#", (char *)buf
, (int)buflen
);
706 static PyObject
*py_cli_ftruncate(struct py_cli_state
*self
, PyObject
*args
,
710 unsigned long long size
;
711 struct tevent_req
*req
;
714 static const char *kwlist
[] = {
715 "fnum", "size", NULL
};
717 if (!ParseTupleAndKeywords(
718 args
, kwds
, "IK", kwlist
, &fnum
, &size
)) {
722 req
= cli_ftruncate_send(NULL
, self
->ev
, self
->cli
, fnum
, size
);
723 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
726 status
= cli_ftruncate_recv(req
);
729 if (!NT_STATUS_IS_OK(status
)) {
730 PyErr_SetNTSTATUS(status
);
736 static PyObject
*py_cli_delete_on_close(struct py_cli_state
*self
,
741 struct tevent_req
*req
;
744 static const char *kwlist
[] = {
745 "fnum", "flag", NULL
};
747 if (!ParseTupleAndKeywords(
748 args
, kwds
, "II", kwlist
, &fnum
, &flag
)) {
752 req
= cli_nt_delete_on_close_send(NULL
, self
->ev
, self
->cli
, fnum
,
754 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
757 status
= cli_nt_delete_on_close_recv(req
);
760 if (!NT_STATUS_IS_OK(status
)) {
761 PyErr_SetNTSTATUS(status
);
767 static PyObject
*py_cli_list(struct py_cli_state
*self
,
773 FILE_ATTRIBUTE_DIRECTORY
|
774 FILE_ATTRIBUTE_SYSTEM
|
775 FILE_ATTRIBUTE_HIDDEN
;
776 unsigned info_level
= SMB_FIND_FILE_BOTH_DIRECTORY_INFO
;
777 struct tevent_req
*req
;
779 struct file_info
*finfos
;
780 size_t i
, num_finfos
;
783 const char *kwlist
[] = {
784 "mask", "attribute", "info_level", NULL
787 if (!ParseTupleAndKeywords(
788 args
, kwds
, "s|II", kwlist
,
789 &mask
, &attribute
, &info_level
)) {
793 req
= cli_list_send(NULL
, self
->ev
, self
->cli
, mask
, attribute
,
795 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
798 status
= cli_list_recv(req
, NULL
, &finfos
, &num_finfos
);
801 if (!NT_STATUS_IS_OK(status
)) {
802 PyErr_SetNTSTATUS(status
);
806 result
= Py_BuildValue("[]");
807 if (result
== NULL
) {
811 for (i
=0; i
<num_finfos
; i
++) {
812 struct file_info
*finfo
= &finfos
[i
];
816 file
= Py_BuildValue(
819 "mode", (int)finfo
->mode
);
825 ret
= PyList_Append(result
, file
);
835 static PyMethodDef py_cli_state_methods
[] = {
836 { "create", (PyCFunction
)py_cli_create
, METH_VARARGS
|METH_KEYWORDS
,
838 { "close", (PyCFunction
)py_cli_close
, METH_VARARGS
,
839 "Close a file handle" },
840 { "write", (PyCFunction
)py_cli_write
, METH_VARARGS
|METH_KEYWORDS
,
841 "Write to a file handle" },
842 { "read", (PyCFunction
)py_cli_read
, METH_VARARGS
|METH_KEYWORDS
,
843 "Read from a file handle" },
844 { "truncate", (PyCFunction
)py_cli_ftruncate
,
845 METH_VARARGS
|METH_KEYWORDS
,
847 { "delete_on_close", (PyCFunction
)py_cli_delete_on_close
,
848 METH_VARARGS
|METH_KEYWORDS
,
849 "Set/Reset the delete on close flag" },
850 { "readdir", (PyCFunction
)py_cli_list
,
851 METH_VARARGS
|METH_KEYWORDS
,
852 "List a directory" },
853 { "get_oplock_break", (PyCFunction
)py_cli_get_oplock_break
,
854 METH_VARARGS
, "Wait for an oplock break" },
855 { NULL
, NULL
, 0, NULL
}
858 static PyTypeObject py_cli_state_type
= {
859 PyObject_HEAD_INIT(NULL
)
860 .tp_name
= "libsmb_samba_internal.Conn",
861 .tp_basicsize
= sizeof(struct py_cli_state
),
862 .tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
,
863 .tp_doc
= "libsmb connection",
864 .tp_new
= py_cli_state_new
,
865 .tp_init
= (initproc
)py_cli_state_init
,
866 .tp_dealloc
= (destructor
)py_cli_state_dealloc
,
867 .tp_methods
= py_cli_state_methods
,
870 static PyMethodDef py_libsmb_methods
[] = {
874 void initlibsmb_samba_internal(void);
875 void initlibsmb_samba_internal(void)
881 m
= Py_InitModule3("libsmb_samba_internal", py_libsmb_methods
,
884 if (PyType_Ready(&py_cli_state_type
) < 0) {
887 Py_INCREF(&py_cli_state_type
);
888 PyModule_AddObject(m
, "Conn", (PyObject
*)&py_cli_state_type
);