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 * const *" 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 * const *keywords
,
62 char **_keywords
= discard_const_p(char *, keywords
);
65 va_start(a
, keywords
);
66 ret
= PyArg_VaParseTupleAndKeywords(args
, kw
, format
,
74 struct py_cli_oplock_break
{
81 struct cli_state
*cli
;
82 struct tevent_context
*ev
;
83 struct py_cli_thread
*thread_state
;
85 struct tevent_req
*oplock_waiter
;
86 struct py_cli_oplock_break
*oplock_breaks
;
87 struct py_tevent_cond
*oplock_cond
;
94 struct py_cli_thread
{
97 * Pipe to make the poll thread wake up in our destructor, so
98 * that we can exit and join the thread.
100 int shutdown_pipe
[2];
101 struct tevent_fd
*shutdown_fde
;
106 * Thread state to release the GIL during the poll(2) syscall
108 PyThreadState
*py_threadstate
;
111 static void *py_cli_state_poll_thread(void *private_data
)
113 struct py_cli_state
*self
= (struct py_cli_state
*)private_data
;
114 struct py_cli_thread
*t
= self
->thread_state
;
115 PyGILState_STATE gstate
;
117 gstate
= PyGILState_Ensure();
119 while (!t
->do_shutdown
) {
121 ret
= tevent_loop_once(self
->ev
);
124 PyGILState_Release(gstate
);
128 static void py_cli_state_trace_callback(enum tevent_trace_point point
,
131 struct py_cli_state
*self
= (struct py_cli_state
*)private_data
;
132 struct py_cli_thread
*t
= self
->thread_state
;
135 case TEVENT_TRACE_BEFORE_WAIT
:
136 assert(t
->py_threadstate
== NULL
);
137 t
->py_threadstate
= PyEval_SaveThread();
139 case TEVENT_TRACE_AFTER_WAIT
:
140 assert(t
->py_threadstate
!= NULL
);
141 PyEval_RestoreThread(t
->py_threadstate
);
142 t
->py_threadstate
= NULL
;
149 static void py_cli_state_shutdown_handler(struct tevent_context
*ev
,
150 struct tevent_fd
*fde
,
154 struct py_cli_state
*self
= (struct py_cli_state
*)private_data
;
155 struct py_cli_thread
*t
= self
->thread_state
;
157 if ((flags
& TEVENT_FD_READ
) == 0) {
160 TALLOC_FREE(t
->shutdown_fde
);
161 t
->do_shutdown
= true;
164 static int py_cli_thread_destructor(struct py_cli_thread
*t
)
172 * This will wake the poll thread from the poll(2)
174 written
= write(t
->shutdown_pipe
[1], &c
, 1);
175 } while ((written
== -1) && (errno
== EINTR
));
178 * Allow the poll thread to do its own cleanup under the GIL
180 Py_BEGIN_ALLOW_THREADS
181 ret
= pthread_join(t
->id
, NULL
);
185 if (t
->shutdown_pipe
[0] != -1) {
186 close(t
->shutdown_pipe
[0]);
187 t
->shutdown_pipe
[0] = -1;
189 if (t
->shutdown_pipe
[1] != -1) {
190 close(t
->shutdown_pipe
[1]);
191 t
->shutdown_pipe
[1] = -1;
196 static bool py_cli_state_setup_ev(struct py_cli_state
*self
)
198 struct py_cli_thread
*t
= NULL
;
201 self
->ev
= tevent_context_init_byname(NULL
, "poll_mt");
202 if (self
->ev
== NULL
) {
205 samba_tevent_set_debug(self
->ev
, "pylibsmb_tevent_mt");
206 tevent_set_trace_callback(self
->ev
, py_cli_state_trace_callback
, self
);
208 self
->thread_state
= talloc_zero(NULL
, struct py_cli_thread
);
209 if (self
->thread_state
== NULL
) {
212 t
= self
->thread_state
;
214 ret
= pipe(t
->shutdown_pipe
);
218 t
->shutdown_fde
= tevent_add_fd(
219 self
->ev
, self
->ev
, t
->shutdown_pipe
[0], TEVENT_FD_READ
,
220 py_cli_state_shutdown_handler
, self
);
221 if (t
->shutdown_fde
== NULL
) {
225 PyEval_InitThreads();
227 ret
= pthread_create(&t
->id
, NULL
, py_cli_state_poll_thread
, self
);
231 talloc_set_destructor(self
->thread_state
, py_cli_thread_destructor
);
236 TALLOC_FREE(t
->shutdown_fde
);
238 if (t
->shutdown_pipe
[0] != -1) {
239 close(t
->shutdown_pipe
[0]);
240 t
->shutdown_pipe
[0] = -1;
242 if (t
->shutdown_pipe
[1] != -1) {
243 close(t
->shutdown_pipe
[1]);
244 t
->shutdown_pipe
[1] = -1;
248 TALLOC_FREE(self
->thread_state
);
249 TALLOC_FREE(self
->ev
);
253 struct py_tevent_cond
{
254 pthread_mutex_t mutex
;
259 static void py_tevent_signalme(struct tevent_req
*req
);
261 static int py_tevent_cond_wait(struct py_tevent_cond
*cond
)
265 result
= pthread_mutex_init(&cond
->mutex
, NULL
);
269 result
= pthread_cond_init(&cond
->cond
, NULL
);
274 result
= pthread_mutex_lock(&cond
->mutex
);
279 cond
->is_done
= false;
281 while (!cond
->is_done
) {
283 Py_BEGIN_ALLOW_THREADS
284 result
= pthread_cond_wait(&cond
->cond
, &cond
->mutex
);
293 ret
= pthread_mutex_unlock(&cond
->mutex
);
296 ret
= pthread_cond_destroy(&cond
->cond
);
299 ret
= pthread_mutex_destroy(&cond
->mutex
);
305 static int py_tevent_req_wait(struct tevent_context
*ev
,
306 struct tevent_req
*req
)
308 struct py_tevent_cond cond
;
309 tevent_req_set_callback(req
, py_tevent_signalme
, &cond
);
310 return py_tevent_cond_wait(&cond
);
313 static void py_tevent_cond_signal(struct py_tevent_cond
*cond
)
317 ret
= pthread_mutex_lock(&cond
->mutex
);
320 cond
->is_done
= true;
322 ret
= pthread_cond_signal(&cond
->cond
);
324 ret
= pthread_mutex_unlock(&cond
->mutex
);
328 static void py_tevent_signalme(struct tevent_req
*req
)
330 struct py_tevent_cond
*cond
= (struct py_tevent_cond
*)
331 tevent_req_callback_data_void(req
);
333 py_tevent_cond_signal(cond
);
338 static bool py_cli_state_setup_ev(struct py_cli_state
*self
)
340 self
->ev
= tevent_context_init(NULL
);
341 if (self
->ev
== NULL
) {
345 samba_tevent_set_debug(self
->ev
, "pylibsmb_tevent");
350 static int py_tevent_req_wait(struct tevent_context
*ev
,
351 struct tevent_req
*req
)
353 while (tevent_req_is_in_progress(req
)) {
356 ret
= tevent_loop_once(ev
);
366 static bool py_tevent_req_wait_exc(struct tevent_context
*ev
,
367 struct tevent_req
*req
)
375 ret
= py_tevent_req_wait(ev
, req
);
379 PyErr_SetFromErrno(PyExc_RuntimeError
);
385 static PyObject
*py_cli_state_new(PyTypeObject
*type
, PyObject
*args
,
388 struct py_cli_state
*self
;
390 self
= (struct py_cli_state
*)type
->tp_alloc(type
, 0);
396 self
->thread_state
= NULL
;
397 self
->oplock_waiter
= NULL
;
398 self
->oplock_cond
= NULL
;
399 self
->oplock_breaks
= NULL
;
400 return (PyObject
*)self
;
403 static void py_cli_got_oplock_break(struct tevent_req
*req
);
405 static int py_cli_state_init(struct py_cli_state
*self
, PyObject
*args
,
410 PyObject
*creds
= NULL
;
411 struct cli_credentials
*cli_creds
;
412 struct tevent_req
*req
;
415 static const char *kwlist
[] = {
416 "host", "share", "credentials", NULL
419 PyTypeObject
*py_type_Credentials
= get_pytype(
420 "samba.credentials", "Credentials");
421 if (py_type_Credentials
== NULL
) {
425 ret
= ParseTupleAndKeywords(
426 args
, kwds
, "ss|O!", kwlist
,
427 &host
, &share
, py_type_Credentials
, &creds
);
429 Py_DECREF(py_type_Credentials
);
435 if (!py_cli_state_setup_ev(self
)) {
440 cli_creds
= cli_credentials_init_anon(NULL
);
442 cli_creds
= PyCredentials_AsCliCredentials(creds
);
445 req
= cli_full_connection_send(
446 NULL
, self
->ev
, "myname", host
, NULL
, 0, share
, "?????",
447 cli_credentials_get_username(cli_creds
),
448 cli_credentials_get_domain(cli_creds
),
449 cli_credentials_get_password(cli_creds
),
451 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
454 status
= cli_full_connection_recv(req
, &self
->cli
);
457 if (!NT_STATUS_IS_OK(status
)) {
458 PyErr_SetNTSTATUS(status
);
462 self
->oplock_waiter
= cli_smb_oplock_break_waiter_send(
463 self
->ev
, self
->ev
, self
->cli
);
464 if (self
->oplock_waiter
== NULL
) {
468 tevent_req_set_callback(self
->oplock_waiter
, py_cli_got_oplock_break
,
473 static void py_cli_got_oplock_break(struct tevent_req
*req
)
475 struct py_cli_state
*self
= (struct py_cli_state
*)
476 tevent_req_callback_data_void(req
);
477 struct py_cli_oplock_break b
;
478 struct py_cli_oplock_break
*tmp
;
482 status
= cli_smb_oplock_break_waiter_recv(req
, &b
.fnum
, &b
.level
);
484 self
->oplock_waiter
= NULL
;
486 if (!NT_STATUS_IS_OK(status
)) {
490 num_breaks
= talloc_array_length(self
->oplock_breaks
);
491 tmp
= talloc_realloc(self
->ev
, self
->oplock_breaks
,
492 struct py_cli_oplock_break
, num_breaks
+1);
496 self
->oplock_breaks
= tmp
;
497 self
->oplock_breaks
[num_breaks
] = b
;
499 if (self
->oplock_cond
!= NULL
) {
500 py_tevent_cond_signal(self
->oplock_cond
);
503 self
->oplock_waiter
= cli_smb_oplock_break_waiter_send(
504 self
->ev
, self
->ev
, self
->cli
);
505 if (self
->oplock_waiter
== NULL
) {
508 tevent_req_set_callback(self
->oplock_waiter
, py_cli_got_oplock_break
,
512 static PyObject
*py_cli_get_oplock_break(struct py_cli_state
*self
,
515 size_t num_oplock_breaks
;
517 if (!PyArg_ParseTuple(args
, "")) {
521 if (self
->oplock_cond
!= NULL
) {
523 PyErr_SetFromErrno(PyExc_RuntimeError
);
527 num_oplock_breaks
= talloc_array_length(self
->oplock_breaks
);
529 if (num_oplock_breaks
== 0) {
530 struct py_tevent_cond cond
;
533 self
->oplock_cond
= &cond
;
534 ret
= py_tevent_cond_wait(&cond
);
535 self
->oplock_cond
= NULL
;
539 PyErr_SetFromErrno(PyExc_RuntimeError
);
544 num_oplock_breaks
= talloc_array_length(self
->oplock_breaks
);
545 if (num_oplock_breaks
> 0) {
548 result
= Py_BuildValue(
550 "fnum", self
->oplock_breaks
[0].fnum
,
551 "level", self
->oplock_breaks
[0].level
);
553 memmove(&self
->oplock_breaks
[0], &self
->oplock_breaks
[1],
554 sizeof(self
->oplock_breaks
[0]) *
555 (num_oplock_breaks
- 1));
556 self
->oplock_breaks
= talloc_realloc(
557 NULL
, self
->oplock_breaks
, struct py_cli_oplock_break
,
558 num_oplock_breaks
- 1);
565 static void py_cli_state_dealloc(struct py_cli_state
*self
)
567 TALLOC_FREE(self
->thread_state
);
568 TALLOC_FREE(self
->oplock_waiter
);
569 TALLOC_FREE(self
->ev
);
571 if (self
->cli
!= NULL
) {
572 cli_shutdown(self
->cli
);
575 self
->ob_type
->tp_free((PyObject
*)self
);
578 static PyObject
*py_cli_create(struct py_cli_state
*self
, PyObject
*args
,
582 unsigned CreateFlags
= 0;
583 unsigned DesiredAccess
= FILE_GENERIC_READ
;
584 unsigned FileAttributes
= 0;
585 unsigned ShareAccess
= 0;
586 unsigned CreateDisposition
= FILE_OPEN
;
587 unsigned CreateOptions
= 0;
588 unsigned SecurityFlags
= 0;
590 struct tevent_req
*req
;
593 static const char *kwlist
[] = {
594 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
595 "ShareAccess", "CreateDisposition", "CreateOptions",
596 "SecurityFlags", NULL
};
598 if (!ParseTupleAndKeywords(
599 args
, kwds
, "s|IIIIIII", kwlist
,
600 &fname
, &CreateFlags
, &DesiredAccess
, &FileAttributes
,
601 &ShareAccess
, &CreateDisposition
, &CreateOptions
,
606 req
= cli_ntcreate_send(NULL
, self
->ev
, self
->cli
, fname
, CreateFlags
,
607 DesiredAccess
, FileAttributes
, ShareAccess
,
608 CreateDisposition
, CreateOptions
,
610 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
613 status
= cli_ntcreate_recv(req
, &fnum
, NULL
);
616 if (!NT_STATUS_IS_OK(status
)) {
617 PyErr_SetNTSTATUS(status
);
620 return Py_BuildValue("I", (unsigned)fnum
);
623 static PyObject
*py_cli_close(struct py_cli_state
*self
, PyObject
*args
)
625 struct tevent_req
*req
;
629 if (!PyArg_ParseTuple(args
, "i", &fnum
)) {
633 req
= cli_close_send(NULL
, self
->ev
, self
->cli
, fnum
);
634 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
637 status
= cli_close_recv(req
);
640 if (!NT_STATUS_IS_OK(status
)) {
641 PyErr_SetNTSTATUS(status
);
647 static PyObject
*py_cli_write(struct py_cli_state
*self
, PyObject
*args
,
654 unsigned long long offset
;
655 struct tevent_req
*req
;
659 static const char *kwlist
[] = {
660 "fnum", "buffer", "offset", "mode", NULL
};
662 if (!ParseTupleAndKeywords(
663 args
, kwds
, "Is#K|I", kwlist
,
664 &fnum
, &buf
, &buflen
, &offset
, &mode
)) {
668 req
= cli_write_andx_send(NULL
, self
->ev
, self
->cli
, fnum
, mode
,
669 (uint8_t *)buf
, offset
, buflen
);
670 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
673 status
= cli_write_andx_recv(req
, &written
);
676 if (!NT_STATUS_IS_OK(status
)) {
677 PyErr_SetNTSTATUS(status
);
680 return Py_BuildValue("K", (unsigned long long)written
);
683 static PyObject
*py_cli_read(struct py_cli_state
*self
, PyObject
*args
,
687 unsigned long long offset
;
689 struct tevent_req
*req
;
695 static const char *kwlist
[] = {
696 "fnum", "offset", "size", NULL
};
698 if (!ParseTupleAndKeywords(
699 args
, kwds
, "IKI", kwlist
, &fnum
, &offset
,
704 req
= cli_read_andx_send(NULL
, self
->ev
, self
->cli
, fnum
,
706 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
709 status
= cli_read_andx_recv(req
, &buflen
, &buf
);
711 if (!NT_STATUS_IS_OK(status
)) {
713 PyErr_SetNTSTATUS(status
);
716 result
= Py_BuildValue("s#", (char *)buf
, (int)buflen
);
721 static PyObject
*py_cli_ftruncate(struct py_cli_state
*self
, PyObject
*args
,
725 unsigned long long size
;
726 struct tevent_req
*req
;
729 static const char *kwlist
[] = {
730 "fnum", "size", NULL
};
732 if (!ParseTupleAndKeywords(
733 args
, kwds
, "IK", kwlist
, &fnum
, &size
)) {
737 req
= cli_ftruncate_send(NULL
, self
->ev
, self
->cli
, fnum
, size
);
738 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
741 status
= cli_ftruncate_recv(req
);
744 if (!NT_STATUS_IS_OK(status
)) {
745 PyErr_SetNTSTATUS(status
);
751 static PyObject
*py_cli_delete_on_close(struct py_cli_state
*self
,
756 struct tevent_req
*req
;
759 static const char *kwlist
[] = {
760 "fnum", "flag", NULL
};
762 if (!ParseTupleAndKeywords(
763 args
, kwds
, "II", kwlist
, &fnum
, &flag
)) {
767 req
= cli_nt_delete_on_close_send(NULL
, self
->ev
, self
->cli
, fnum
,
769 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
772 status
= cli_nt_delete_on_close_recv(req
);
775 if (!NT_STATUS_IS_OK(status
)) {
776 PyErr_SetNTSTATUS(status
);
782 static PyObject
*py_cli_list(struct py_cli_state
*self
,
788 FILE_ATTRIBUTE_DIRECTORY
|
789 FILE_ATTRIBUTE_SYSTEM
|
790 FILE_ATTRIBUTE_HIDDEN
;
791 unsigned info_level
= SMB_FIND_FILE_BOTH_DIRECTORY_INFO
;
792 struct tevent_req
*req
;
794 struct file_info
*finfos
;
795 size_t i
, num_finfos
;
798 const char *kwlist
[] = {
799 "mask", "attribute", "info_level", NULL
802 if (!ParseTupleAndKeywords(
803 args
, kwds
, "s|II", kwlist
,
804 &mask
, &attribute
, &info_level
)) {
808 req
= cli_list_send(NULL
, self
->ev
, self
->cli
, mask
, attribute
,
810 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
813 status
= cli_list_recv(req
, NULL
, &finfos
, &num_finfos
);
816 if (!NT_STATUS_IS_OK(status
)) {
817 PyErr_SetNTSTATUS(status
);
821 result
= Py_BuildValue("[]");
822 if (result
== NULL
) {
826 for (i
=0; i
<num_finfos
; i
++) {
827 struct file_info
*finfo
= &finfos
[i
];
831 file
= Py_BuildValue(
834 "mode", (int)finfo
->mode
);
840 ret
= PyList_Append(result
, file
);
850 static PyMethodDef py_cli_state_methods
[] = {
851 { "create", (PyCFunction
)py_cli_create
, METH_VARARGS
|METH_KEYWORDS
,
853 { "close", (PyCFunction
)py_cli_close
, METH_VARARGS
,
854 "Close a file handle" },
855 { "write", (PyCFunction
)py_cli_write
, METH_VARARGS
|METH_KEYWORDS
,
856 "Write to a file handle" },
857 { "read", (PyCFunction
)py_cli_read
, METH_VARARGS
|METH_KEYWORDS
,
858 "Read from a file handle" },
859 { "truncate", (PyCFunction
)py_cli_ftruncate
,
860 METH_VARARGS
|METH_KEYWORDS
,
862 { "delete_on_close", (PyCFunction
)py_cli_delete_on_close
,
863 METH_VARARGS
|METH_KEYWORDS
,
864 "Set/Reset the delete on close flag" },
865 { "readdir", (PyCFunction
)py_cli_list
,
866 METH_VARARGS
|METH_KEYWORDS
,
867 "List a directory" },
868 { "get_oplock_break", (PyCFunction
)py_cli_get_oplock_break
,
869 METH_VARARGS
, "Wait for an oplock break" },
870 { NULL
, NULL
, 0, NULL
}
873 static PyTypeObject py_cli_state_type
= {
874 PyObject_HEAD_INIT(NULL
)
875 .tp_name
= "libsmb_samba_internal.Conn",
876 .tp_basicsize
= sizeof(struct py_cli_state
),
877 .tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
,
878 .tp_doc
= "libsmb connection",
879 .tp_new
= py_cli_state_new
,
880 .tp_init
= (initproc
)py_cli_state_init
,
881 .tp_dealloc
= (destructor
)py_cli_state_dealloc
,
882 .tp_methods
= py_cli_state_methods
,
885 static PyMethodDef py_libsmb_methods
[] = {
889 void initlibsmb_samba_internal(void);
890 void initlibsmb_samba_internal(void)
896 m
= Py_InitModule3("libsmb_samba_internal", py_libsmb_methods
,
899 if (PyType_Ready(&py_cli_state_type
) < 0) {
902 Py_INCREF(&py_cli_state_type
);
903 PyModule_AddObject(m
, "Conn", (PyObject
*)&py_cli_state_type
);