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 samba_tevent_set_debug(self
->ev
, "pylibsmb_tevent_mt");
205 tevent_set_trace_callback(self
->ev
, py_cli_state_trace_callback
, self
);
207 self
->thread_state
= talloc_zero(NULL
, struct py_cli_thread
);
208 if (self
->thread_state
== NULL
) {
211 t
= self
->thread_state
;
213 ret
= pipe(t
->shutdown_pipe
);
217 t
->shutdown_fde
= tevent_add_fd(
218 self
->ev
, self
->ev
, t
->shutdown_pipe
[0], TEVENT_FD_READ
,
219 py_cli_state_shutdown_handler
, self
);
220 if (t
->shutdown_fde
== NULL
) {
224 PyEval_InitThreads();
226 ret
= pthread_create(&t
->id
, NULL
, py_cli_state_poll_thread
, self
);
230 talloc_set_destructor(self
->thread_state
, py_cli_thread_destructor
);
235 TALLOC_FREE(t
->shutdown_fde
);
237 if (t
->shutdown_pipe
[0] != -1) {
238 close(t
->shutdown_pipe
[0]);
239 t
->shutdown_pipe
[0] = -1;
241 if (t
->shutdown_pipe
[1] != -1) {
242 close(t
->shutdown_pipe
[1]);
243 t
->shutdown_pipe
[1] = -1;
247 TALLOC_FREE(self
->thread_state
);
248 TALLOC_FREE(self
->ev
);
252 struct py_tevent_cond
{
253 pthread_mutex_t mutex
;
258 static void py_tevent_signalme(struct tevent_req
*req
);
260 static int py_tevent_cond_wait(struct py_tevent_cond
*cond
)
264 result
= pthread_mutex_init(&cond
->mutex
, NULL
);
268 result
= pthread_cond_init(&cond
->cond
, NULL
);
273 result
= pthread_mutex_lock(&cond
->mutex
);
278 cond
->is_done
= false;
280 while (!cond
->is_done
) {
282 Py_BEGIN_ALLOW_THREADS
283 result
= pthread_cond_wait(&cond
->cond
, &cond
->mutex
);
292 ret
= pthread_mutex_unlock(&cond
->mutex
);
295 ret
= pthread_cond_destroy(&cond
->cond
);
298 ret
= pthread_mutex_destroy(&cond
->mutex
);
304 static int py_tevent_req_wait(struct tevent_context
*ev
,
305 struct tevent_req
*req
)
307 struct py_tevent_cond cond
;
308 tevent_req_set_callback(req
, py_tevent_signalme
, &cond
);
309 return py_tevent_cond_wait(&cond
);
312 static void py_tevent_cond_signal(struct py_tevent_cond
*cond
)
316 ret
= pthread_mutex_lock(&cond
->mutex
);
319 cond
->is_done
= true;
321 ret
= pthread_cond_signal(&cond
->cond
);
323 ret
= pthread_mutex_unlock(&cond
->mutex
);
327 static void py_tevent_signalme(struct tevent_req
*req
)
329 struct py_tevent_cond
*cond
= (struct py_tevent_cond
*)
330 tevent_req_callback_data_void(req
);
332 py_tevent_cond_signal(cond
);
337 static bool py_cli_state_setup_ev(struct py_cli_state
*self
)
339 self
->ev
= tevent_context_init(NULL
);
340 if (self
->ev
== NULL
) {
344 samba_tevent_set_debug(self
->ev
, "pylibsmb_tevent");
349 static int py_tevent_req_wait(struct tevent_context
*ev
,
350 struct tevent_req
*req
)
352 while (tevent_req_is_in_progress(req
)) {
355 ret
= tevent_loop_once(ev
);
365 static bool py_tevent_req_wait_exc(struct tevent_context
*ev
,
366 struct tevent_req
*req
)
374 ret
= py_tevent_req_wait(ev
, req
);
378 PyErr_SetFromErrno(PyExc_RuntimeError
);
384 static PyObject
*py_cli_state_new(PyTypeObject
*type
, PyObject
*args
,
387 struct py_cli_state
*self
;
389 self
= (struct py_cli_state
*)type
->tp_alloc(type
, 0);
395 self
->thread_state
= NULL
;
396 self
->oplock_waiter
= NULL
;
397 self
->oplock_cond
= NULL
;
398 self
->oplock_breaks
= NULL
;
399 return (PyObject
*)self
;
402 static void py_cli_got_oplock_break(struct tevent_req
*req
);
404 static int py_cli_state_init(struct py_cli_state
*self
, PyObject
*args
,
409 PyObject
*creds
= NULL
;
410 struct cli_credentials
*cli_creds
;
411 struct tevent_req
*req
;
414 static const char *kwlist
[] = {
415 "host", "share", "credentials", NULL
418 PyTypeObject
*py_type_Credentials
= get_pytype(
419 "samba.credentials", "Credentials");
420 if (py_type_Credentials
== NULL
) {
424 ret
= ParseTupleAndKeywords(
425 args
, kwds
, "ss|O!", kwlist
,
426 &host
, &share
, py_type_Credentials
, &creds
);
428 Py_DECREF(py_type_Credentials
);
434 if (!py_cli_state_setup_ev(self
)) {
439 cli_creds
= cli_credentials_init_anon(NULL
);
441 cli_creds
= PyCredentials_AsCliCredentials(creds
);
444 req
= cli_full_connection_send(
445 NULL
, self
->ev
, "myname", host
, NULL
, 0, share
, "?????",
446 cli_credentials_get_username(cli_creds
),
447 cli_credentials_get_domain(cli_creds
),
448 cli_credentials_get_password(cli_creds
),
450 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
453 status
= cli_full_connection_recv(req
, &self
->cli
);
456 if (!NT_STATUS_IS_OK(status
)) {
457 PyErr_SetNTSTATUS(status
);
461 self
->oplock_waiter
= cli_smb_oplock_break_waiter_send(
462 self
->ev
, self
->ev
, self
->cli
);
463 if (self
->oplock_waiter
== NULL
) {
467 tevent_req_set_callback(self
->oplock_waiter
, py_cli_got_oplock_break
,
472 static void py_cli_got_oplock_break(struct tevent_req
*req
)
474 struct py_cli_state
*self
= (struct py_cli_state
*)
475 tevent_req_callback_data_void(req
);
476 struct py_cli_oplock_break b
;
477 struct py_cli_oplock_break
*tmp
;
481 status
= cli_smb_oplock_break_waiter_recv(req
, &b
.fnum
, &b
.level
);
483 self
->oplock_waiter
= NULL
;
485 if (!NT_STATUS_IS_OK(status
)) {
489 num_breaks
= talloc_array_length(self
->oplock_breaks
);
490 tmp
= talloc_realloc(self
->ev
, self
->oplock_breaks
,
491 struct py_cli_oplock_break
, num_breaks
+1);
495 self
->oplock_breaks
= tmp
;
496 self
->oplock_breaks
[num_breaks
] = b
;
498 if (self
->oplock_cond
!= NULL
) {
499 py_tevent_cond_signal(self
->oplock_cond
);
502 self
->oplock_waiter
= cli_smb_oplock_break_waiter_send(
503 self
->ev
, self
->ev
, self
->cli
);
504 if (self
->oplock_waiter
== NULL
) {
507 tevent_req_set_callback(self
->oplock_waiter
, py_cli_got_oplock_break
,
511 static PyObject
*py_cli_get_oplock_break(struct py_cli_state
*self
,
514 size_t num_oplock_breaks
;
516 if (!PyArg_ParseTuple(args
, "")) {
520 if (self
->oplock_cond
!= NULL
) {
522 PyErr_SetFromErrno(PyExc_RuntimeError
);
526 num_oplock_breaks
= talloc_array_length(self
->oplock_breaks
);
528 if (num_oplock_breaks
== 0) {
529 struct py_tevent_cond cond
;
532 self
->oplock_cond
= &cond
;
533 ret
= py_tevent_cond_wait(&cond
);
534 self
->oplock_cond
= NULL
;
538 PyErr_SetFromErrno(PyExc_RuntimeError
);
543 num_oplock_breaks
= talloc_array_length(self
->oplock_breaks
);
544 if (num_oplock_breaks
> 0) {
547 result
= Py_BuildValue(
549 "fnum", self
->oplock_breaks
[0].fnum
,
550 "level", self
->oplock_breaks
[0].level
);
552 memmove(&self
->oplock_breaks
[0], &self
->oplock_breaks
[1],
553 sizeof(self
->oplock_breaks
[0]) *
554 (num_oplock_breaks
- 1));
555 self
->oplock_breaks
= talloc_realloc(
556 NULL
, self
->oplock_breaks
, struct py_cli_oplock_break
,
557 num_oplock_breaks
- 1);
564 static void py_cli_state_dealloc(struct py_cli_state
*self
)
566 TALLOC_FREE(self
->thread_state
);
567 TALLOC_FREE(self
->oplock_waiter
);
568 TALLOC_FREE(self
->ev
);
570 if (self
->cli
!= NULL
) {
571 cli_shutdown(self
->cli
);
574 self
->ob_type
->tp_free((PyObject
*)self
);
577 static PyObject
*py_cli_create(struct py_cli_state
*self
, PyObject
*args
,
581 unsigned CreateFlags
= 0;
582 unsigned DesiredAccess
= FILE_GENERIC_READ
;
583 unsigned FileAttributes
= 0;
584 unsigned ShareAccess
= 0;
585 unsigned CreateDisposition
= FILE_OPEN
;
586 unsigned CreateOptions
= 0;
587 unsigned SecurityFlags
= 0;
589 struct tevent_req
*req
;
592 static const char *kwlist
[] = {
593 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
594 "ShareAccess", "CreateDisposition", "CreateOptions",
595 "SecurityFlags", NULL
};
597 if (!ParseTupleAndKeywords(
598 args
, kwds
, "s|IIIIIII", kwlist
,
599 &fname
, &CreateFlags
, &DesiredAccess
, &FileAttributes
,
600 &ShareAccess
, &CreateDisposition
, &CreateOptions
,
605 req
= cli_ntcreate_send(NULL
, self
->ev
, self
->cli
, fname
, CreateFlags
,
606 DesiredAccess
, FileAttributes
, ShareAccess
,
607 CreateDisposition
, CreateOptions
,
609 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
612 status
= cli_ntcreate_recv(req
, &fnum
);
615 if (!NT_STATUS_IS_OK(status
)) {
616 PyErr_SetNTSTATUS(status
);
619 return Py_BuildValue("I", (unsigned)fnum
);
622 static PyObject
*py_cli_close(struct py_cli_state
*self
, PyObject
*args
)
624 struct tevent_req
*req
;
628 if (!PyArg_ParseTuple(args
, "i", &fnum
)) {
632 req
= cli_close_send(NULL
, self
->ev
, self
->cli
, fnum
);
633 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
636 status
= cli_close_recv(req
);
639 if (!NT_STATUS_IS_OK(status
)) {
640 PyErr_SetNTSTATUS(status
);
646 static PyObject
*py_cli_write(struct py_cli_state
*self
, PyObject
*args
,
653 unsigned long long offset
;
654 struct tevent_req
*req
;
658 static const char *kwlist
[] = {
659 "fnum", "buffer", "offset", "mode", NULL
};
661 if (!ParseTupleAndKeywords(
662 args
, kwds
, "Is#K|I", kwlist
,
663 &fnum
, &buf
, &buflen
, &offset
, &mode
)) {
667 req
= cli_write_andx_send(NULL
, self
->ev
, self
->cli
, fnum
, mode
,
668 (uint8_t *)buf
, offset
, buflen
);
669 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
672 status
= cli_write_andx_recv(req
, &written
);
675 if (!NT_STATUS_IS_OK(status
)) {
676 PyErr_SetNTSTATUS(status
);
679 return Py_BuildValue("K", (unsigned long long)written
);
682 static PyObject
*py_cli_read(struct py_cli_state
*self
, PyObject
*args
,
686 unsigned long long offset
;
688 struct tevent_req
*req
;
694 static const char *kwlist
[] = {
695 "fnum", "offset", "size", NULL
};
697 if (!ParseTupleAndKeywords(
698 args
, kwds
, "IKI", kwlist
, &fnum
, &offset
,
703 req
= cli_read_andx_send(NULL
, self
->ev
, self
->cli
, fnum
,
705 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
708 status
= cli_read_andx_recv(req
, &buflen
, &buf
);
710 if (!NT_STATUS_IS_OK(status
)) {
712 PyErr_SetNTSTATUS(status
);
715 result
= Py_BuildValue("s#", (char *)buf
, (int)buflen
);
720 static PyObject
*py_cli_ftruncate(struct py_cli_state
*self
, PyObject
*args
,
724 unsigned long long size
;
725 struct tevent_req
*req
;
728 static const char *kwlist
[] = {
729 "fnum", "size", NULL
};
731 if (!ParseTupleAndKeywords(
732 args
, kwds
, "IK", kwlist
, &fnum
, &size
)) {
736 req
= cli_ftruncate_send(NULL
, self
->ev
, self
->cli
, fnum
, size
);
737 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
740 status
= cli_ftruncate_recv(req
);
743 if (!NT_STATUS_IS_OK(status
)) {
744 PyErr_SetNTSTATUS(status
);
750 static PyObject
*py_cli_delete_on_close(struct py_cli_state
*self
,
755 struct tevent_req
*req
;
758 static const char *kwlist
[] = {
759 "fnum", "flag", NULL
};
761 if (!ParseTupleAndKeywords(
762 args
, kwds
, "II", kwlist
, &fnum
, &flag
)) {
766 req
= cli_nt_delete_on_close_send(NULL
, self
->ev
, self
->cli
, fnum
,
768 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
771 status
= cli_nt_delete_on_close_recv(req
);
774 if (!NT_STATUS_IS_OK(status
)) {
775 PyErr_SetNTSTATUS(status
);
781 static PyObject
*py_cli_list(struct py_cli_state
*self
,
787 FILE_ATTRIBUTE_DIRECTORY
|
788 FILE_ATTRIBUTE_SYSTEM
|
789 FILE_ATTRIBUTE_HIDDEN
;
790 unsigned info_level
= SMB_FIND_FILE_BOTH_DIRECTORY_INFO
;
791 struct tevent_req
*req
;
793 struct file_info
*finfos
;
794 size_t i
, num_finfos
;
797 const char *kwlist
[] = {
798 "mask", "attribute", "info_level", NULL
801 if (!ParseTupleAndKeywords(
802 args
, kwds
, "s|II", kwlist
,
803 &mask
, &attribute
, &info_level
)) {
807 req
= cli_list_send(NULL
, self
->ev
, self
->cli
, mask
, attribute
,
809 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
812 status
= cli_list_recv(req
, NULL
, &finfos
, &num_finfos
);
815 if (!NT_STATUS_IS_OK(status
)) {
816 PyErr_SetNTSTATUS(status
);
820 result
= Py_BuildValue("[]");
821 if (result
== NULL
) {
825 for (i
=0; i
<num_finfos
; i
++) {
826 struct file_info
*finfo
= &finfos
[i
];
830 file
= Py_BuildValue(
833 "mode", (int)finfo
->mode
);
839 ret
= PyList_Append(result
, file
);
849 static PyMethodDef py_cli_state_methods
[] = {
850 { "create", (PyCFunction
)py_cli_create
, METH_VARARGS
|METH_KEYWORDS
,
852 { "close", (PyCFunction
)py_cli_close
, METH_VARARGS
,
853 "Close a file handle" },
854 { "write", (PyCFunction
)py_cli_write
, METH_VARARGS
|METH_KEYWORDS
,
855 "Write to a file handle" },
856 { "read", (PyCFunction
)py_cli_read
, METH_VARARGS
|METH_KEYWORDS
,
857 "Read from a file handle" },
858 { "truncate", (PyCFunction
)py_cli_ftruncate
,
859 METH_VARARGS
|METH_KEYWORDS
,
861 { "delete_on_close", (PyCFunction
)py_cli_delete_on_close
,
862 METH_VARARGS
|METH_KEYWORDS
,
863 "Set/Reset the delete on close flag" },
864 { "readdir", (PyCFunction
)py_cli_list
,
865 METH_VARARGS
|METH_KEYWORDS
,
866 "List a directory" },
867 { "get_oplock_break", (PyCFunction
)py_cli_get_oplock_break
,
868 METH_VARARGS
, "Wait for an oplock break" },
869 { NULL
, NULL
, 0, NULL
}
872 static PyTypeObject py_cli_state_type
= {
873 PyObject_HEAD_INIT(NULL
)
874 .tp_name
= "libsmb_samba_internal.Conn",
875 .tp_basicsize
= sizeof(struct py_cli_state
),
876 .tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
,
877 .tp_doc
= "libsmb connection",
878 .tp_new
= py_cli_state_new
,
879 .tp_init
= (initproc
)py_cli_state_init
,
880 .tp_dealloc
= (destructor
)py_cli_state_dealloc
,
881 .tp_methods
= py_cli_state_methods
,
884 static PyMethodDef py_libsmb_methods
[] = {
888 void initlibsmb_samba_internal(void);
889 void initlibsmb_samba_internal(void)
895 m
= Py_InitModule3("libsmb_samba_internal", py_libsmb_methods
,
898 if (PyType_Ready(&py_cli_state_type
) < 0) {
901 Py_INCREF(&py_cli_state_type
);
902 PyModule_AddObject(m
, "Conn", (PyObject
*)&py_cli_state_type
);