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 "python/py3compat.h"
24 #include "libsmb/libsmb.h"
25 #include "libcli/security/security.h"
26 #include "system/select.h"
27 #include "source4/libcli/util/pyerrors.h"
28 #include "auth/credentials/pycredentials.h"
31 static PyTypeObject *get_pytype(const char *module, const char *type)
36 mod = PyImport_ImportModule(module);
38 PyErr_Format(PyExc_RuntimeError,
39 "Unable to import %s to check type %s",
43 result = (PyTypeObject *)PyObject_GetAttrString(mod, type);
46 PyErr_Format(PyExc_RuntimeError,
47 "Unable to find type %s in module %s",
55 * We're using "const char * const *" for keywords,
56 * PyArg_ParseTupleAndKeywords expects a "char **". Confine the
57 * inevitable warnings to just one place.
59 static int ParseTupleAndKeywords(PyObject *args, PyObject *kw,
60 const char *format, const char * const *keywords,
63 char **_keywords = discard_const_p(char *, keywords);
66 va_start(a, keywords);
67 ret = PyArg_VaParseTupleAndKeywords(args, kw, format,
75 struct py_cli_oplock_break {
82 struct cli_state *cli;
83 struct tevent_context *ev;
84 int (*req_wait_fn)(struct tevent_context *ev,
85 struct tevent_req *req);
86 struct py_cli_thread *thread_state;
88 struct tevent_req *oplock_waiter;
89 struct py_cli_oplock_break *oplock_breaks;
90 struct py_tevent_cond *oplock_cond;
97 struct py_cli_thread {
100 * Pipe to make the poll thread wake up in our destructor, so
101 * that we can exit and join the thread.
103 int shutdown_pipe[2];
104 struct tevent_fd *shutdown_fde;
109 * Thread state to release the GIL during the poll(2) syscall
111 PyThreadState *py_threadstate;
114 static void *py_cli_state_poll_thread(void *private_data)
116 struct py_cli_state *self = (struct py_cli_state *)private_data;
117 struct py_cli_thread *t = self->thread_state;
118 PyGILState_STATE gstate;
120 gstate = PyGILState_Ensure();
122 while (!t->do_shutdown) {
124 ret = tevent_loop_once(self->ev);
127 PyGILState_Release(gstate);
131 static void py_cli_state_trace_callback(enum tevent_trace_point point,
134 struct py_cli_state *self = (struct py_cli_state *)private_data;
135 struct py_cli_thread *t = self->thread_state;
138 case TEVENT_TRACE_BEFORE_WAIT:
139 assert(t->py_threadstate == NULL);
140 t->py_threadstate = PyEval_SaveThread();
142 case TEVENT_TRACE_AFTER_WAIT:
143 assert(t->py_threadstate != NULL);
144 PyEval_RestoreThread(t->py_threadstate);
145 t->py_threadstate = NULL;
152 static void py_cli_state_shutdown_handler(struct tevent_context *ev,
153 struct tevent_fd *fde,
157 struct py_cli_state *self = (struct py_cli_state *)private_data;
158 struct py_cli_thread *t = self->thread_state;
160 if ((flags & TEVENT_FD_READ) == 0) {
163 TALLOC_FREE(t->shutdown_fde);
164 t->do_shutdown = true;
167 static int py_cli_thread_destructor(struct py_cli_thread *t)
175 * This will wake the poll thread from the poll(2)
177 written = write(t->shutdown_pipe[1], &c, 1);
178 } while ((written == -1) && (errno == EINTR));
181 * Allow the poll thread to do its own cleanup under the GIL
183 Py_BEGIN_ALLOW_THREADS
184 ret = pthread_join(t->id, NULL);
188 if (t->shutdown_pipe[0] != -1) {
189 close(t->shutdown_pipe[0]);
190 t->shutdown_pipe[0] = -1;
192 if (t->shutdown_pipe[1] != -1) {
193 close(t->shutdown_pipe[1]);
194 t->shutdown_pipe[1] = -1;
199 static int py_tevent_cond_req_wait(struct tevent_context *ev,
200 struct tevent_req *req);
202 static bool py_cli_state_setup_mt_ev(struct py_cli_state *self)
204 struct py_cli_thread *t = NULL;
207 self->ev = tevent_context_init_byname(NULL, "poll_mt");
208 if (self->ev == NULL) {
211 samba_tevent_set_debug(self->ev, "pylibsmb_tevent_mt");
212 tevent_set_trace_callback(self->ev, py_cli_state_trace_callback, self);
214 self->req_wait_fn = py_tevent_cond_req_wait;
216 self->thread_state = talloc_zero(NULL, struct py_cli_thread);
217 if (self->thread_state == NULL) {
220 t = self->thread_state;
222 ret = pipe(t->shutdown_pipe);
226 t->shutdown_fde = tevent_add_fd(
227 self->ev, self->ev, t->shutdown_pipe[0], TEVENT_FD_READ,
228 py_cli_state_shutdown_handler, self);
229 if (t->shutdown_fde == NULL) {
233 PyEval_InitThreads();
235 ret = pthread_create(&t->id, NULL, py_cli_state_poll_thread, self);
239 talloc_set_destructor(self->thread_state, py_cli_thread_destructor);
244 TALLOC_FREE(t->shutdown_fde);
246 if (t->shutdown_pipe[0] != -1) {
247 close(t->shutdown_pipe[0]);
248 t->shutdown_pipe[0] = -1;
250 if (t->shutdown_pipe[1] != -1) {
251 close(t->shutdown_pipe[1]);
252 t->shutdown_pipe[1] = -1;
256 TALLOC_FREE(self->thread_state);
257 TALLOC_FREE(self->ev);
261 struct py_tevent_cond {
262 pthread_mutex_t mutex;
267 static void py_tevent_signalme(struct tevent_req *req);
269 static int py_tevent_cond_wait(struct py_tevent_cond *cond)
273 result = pthread_mutex_init(&cond->mutex, NULL);
277 result = pthread_cond_init(&cond->cond, NULL);
282 result = pthread_mutex_lock(&cond->mutex);
287 cond->is_done = false;
289 while (!cond->is_done) {
291 Py_BEGIN_ALLOW_THREADS
292 result = pthread_cond_wait(&cond->cond, &cond->mutex);
301 ret = pthread_mutex_unlock(&cond->mutex);
304 ret = pthread_cond_destroy(&cond->cond);
307 ret = pthread_mutex_destroy(&cond->mutex);
313 static int py_tevent_cond_req_wait(struct tevent_context *ev,
314 struct tevent_req *req)
316 struct py_tevent_cond cond;
317 tevent_req_set_callback(req, py_tevent_signalme, &cond);
318 return py_tevent_cond_wait(&cond);
321 static void py_tevent_cond_signal(struct py_tevent_cond *cond)
325 ret = pthread_mutex_lock(&cond->mutex);
328 cond->is_done = true;
330 ret = pthread_cond_signal(&cond->cond);
332 ret = pthread_mutex_unlock(&cond->mutex);
336 static void py_tevent_signalme(struct tevent_req *req)
338 struct py_tevent_cond *cond = (struct py_tevent_cond *)
339 tevent_req_callback_data_void(req);
341 py_tevent_cond_signal(cond);
346 static int py_tevent_req_wait(struct tevent_context *ev,
347 struct tevent_req *req);
349 static bool py_cli_state_setup_ev(struct py_cli_state *self)
351 self->ev = tevent_context_init(NULL);
352 if (self->ev == NULL) {
356 samba_tevent_set_debug(self->ev, "pylibsmb_tevent");
358 self->req_wait_fn = py_tevent_req_wait;
363 static int py_tevent_req_wait(struct tevent_context *ev,
364 struct tevent_req *req)
366 while (tevent_req_is_in_progress(req)) {
369 ret = tevent_loop_once(ev);
377 static bool py_tevent_req_wait_exc(struct py_cli_state *self,
378 struct tevent_req *req)
386 ret = self->req_wait_fn(self->ev, req);
390 PyErr_SetFromErrno(PyExc_RuntimeError);
396 static PyObject *py_cli_state_new(PyTypeObject *type, PyObject *args,
399 struct py_cli_state *self;
401 self = (struct py_cli_state *)type->tp_alloc(type, 0);
407 self->thread_state = NULL;
408 self->oplock_waiter = NULL;
409 self->oplock_cond = NULL;
410 self->oplock_breaks = NULL;
411 return (PyObject *)self;
414 static void py_cli_got_oplock_break(struct tevent_req *req);
416 static int py_cli_state_init(struct py_cli_state *self, PyObject *args,
421 PyObject *creds = NULL;
422 struct cli_credentials *cli_creds;
423 PyObject *py_multi_threaded = Py_False;
424 bool multi_threaded = false;
425 struct tevent_req *req;
428 * For now we only support SMB1,
429 * as most of the cli_*_send() function
430 * don't support SMB2, it's only plugged
431 * into the sync wrapper functions currently.
433 int flags = CLI_FULL_CONNECTION_FORCE_SMB1;
435 static const char *kwlist[] = {
436 "host", "share", "credentials", "multi_threaded", NULL
439 PyTypeObject *py_type_Credentials = get_pytype(
440 "samba.credentials", "Credentials");
441 if (py_type_Credentials == NULL) {
445 ret = ParseTupleAndKeywords(
446 args, kwds, "ss|O!O", kwlist,
448 py_type_Credentials, &creds,
451 Py_DECREF(py_type_Credentials);
457 multi_threaded = PyObject_IsTrue(py_multi_threaded);
459 if (multi_threaded) {
461 ret = py_cli_state_setup_mt_ev(self);
466 PyErr_SetString(PyExc_RuntimeError,
467 "No PTHREAD support available");
471 ret = py_cli_state_setup_ev(self);
478 cli_creds = cli_credentials_init_anon(NULL);
480 cli_creds = PyCredentials_AsCliCredentials(creds);
483 req = cli_full_connection_creds_send(
484 NULL, self->ev, "myname", host, NULL, 0, share, "?????",
485 cli_creds, flags, SMB_SIGNING_DEFAULT);
486 if (!py_tevent_req_wait_exc(self, req)) {
489 status = cli_full_connection_creds_recv(req, &self->cli);
492 if (!NT_STATUS_IS_OK(status)) {
493 PyErr_SetNTSTATUS(status);
497 self->oplock_waiter = cli_smb_oplock_break_waiter_send(
498 self->ev, self->ev, self->cli);
499 if (self->oplock_waiter == NULL) {
503 tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
508 static void py_cli_got_oplock_break(struct tevent_req *req)
510 struct py_cli_state *self = (struct py_cli_state *)
511 tevent_req_callback_data_void(req);
512 struct py_cli_oplock_break b;
513 struct py_cli_oplock_break *tmp;
517 status = cli_smb_oplock_break_waiter_recv(req, &b.fnum, &b.level);
519 self->oplock_waiter = NULL;
521 if (!NT_STATUS_IS_OK(status)) {
525 num_breaks = talloc_array_length(self->oplock_breaks);
526 tmp = talloc_realloc(self->ev, self->oplock_breaks,
527 struct py_cli_oplock_break, num_breaks+1);
531 self->oplock_breaks = tmp;
532 self->oplock_breaks[num_breaks] = b;
534 if (self->oplock_cond != NULL) {
535 py_tevent_cond_signal(self->oplock_cond);
538 self->oplock_waiter = cli_smb_oplock_break_waiter_send(
539 self->ev, self->ev, self->cli);
540 if (self->oplock_waiter == NULL) {
543 tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
547 static PyObject *py_cli_get_oplock_break(struct py_cli_state *self,
550 size_t num_oplock_breaks;
552 if (!PyArg_ParseTuple(args, "")) {
556 if (self->oplock_cond != NULL) {
558 PyErr_SetFromErrno(PyExc_RuntimeError);
562 num_oplock_breaks = talloc_array_length(self->oplock_breaks);
564 if (num_oplock_breaks == 0) {
565 struct py_tevent_cond cond;
568 self->oplock_cond = &cond;
569 ret = py_tevent_cond_wait(&cond);
570 self->oplock_cond = NULL;
574 PyErr_SetFromErrno(PyExc_RuntimeError);
579 num_oplock_breaks = talloc_array_length(self->oplock_breaks);
580 if (num_oplock_breaks > 0) {
583 result = Py_BuildValue(
585 "fnum", self->oplock_breaks[0].fnum,
586 "level", self->oplock_breaks[0].level);
588 memmove(&self->oplock_breaks[0], &self->oplock_breaks[1],
589 sizeof(self->oplock_breaks[0]) *
590 (num_oplock_breaks - 1));
591 self->oplock_breaks = talloc_realloc(
592 NULL, self->oplock_breaks, struct py_cli_oplock_break,
593 num_oplock_breaks - 1);
600 static void py_cli_state_dealloc(struct py_cli_state *self)
602 TALLOC_FREE(self->thread_state);
603 TALLOC_FREE(self->oplock_waiter);
604 TALLOC_FREE(self->ev);
606 if (self->cli != NULL) {
607 cli_shutdown(self->cli);
610 Py_TYPE(self)->tp_free((PyObject *)self);
613 static PyObject *py_cli_create(struct py_cli_state *self, PyObject *args,
617 unsigned CreateFlags = 0;
618 unsigned DesiredAccess = FILE_GENERIC_READ;
619 unsigned FileAttributes = 0;
620 unsigned ShareAccess = 0;
621 unsigned CreateDisposition = FILE_OPEN;
622 unsigned CreateOptions = 0;
623 unsigned SecurityFlags = 0;
625 struct tevent_req *req;
628 static const char *kwlist[] = {
629 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
630 "ShareAccess", "CreateDisposition", "CreateOptions",
631 "SecurityFlags", NULL };
633 if (!ParseTupleAndKeywords(
634 args, kwds, "s|IIIIIII", kwlist,
635 &fname, &CreateFlags, &DesiredAccess, &FileAttributes,
636 &ShareAccess, &CreateDisposition, &CreateOptions,
641 req = cli_ntcreate_send(NULL, self->ev, self->cli, fname, CreateFlags,
642 DesiredAccess, FileAttributes, ShareAccess,
643 CreateDisposition, CreateOptions,
645 if (!py_tevent_req_wait_exc(self, req)) {
648 status = cli_ntcreate_recv(req, &fnum, NULL);
651 if (!NT_STATUS_IS_OK(status)) {
652 PyErr_SetNTSTATUS(status);
655 return Py_BuildValue("I", (unsigned)fnum);
658 static PyObject *py_cli_close(struct py_cli_state *self, PyObject *args)
660 struct tevent_req *req;
664 if (!PyArg_ParseTuple(args, "i", &fnum)) {
668 req = cli_close_send(NULL, self->ev, self->cli, fnum);
669 if (!py_tevent_req_wait_exc(self, req)) {
672 status = cli_close_recv(req);
675 if (!NT_STATUS_IS_OK(status)) {
676 PyErr_SetNTSTATUS(status);
682 static PyObject *py_cli_write(struct py_cli_state *self, PyObject *args,
689 unsigned long long offset;
690 struct tevent_req *req;
694 static const char *kwlist[] = {
695 "fnum", "buffer", "offset", "mode", NULL };
697 if (!ParseTupleAndKeywords(
698 args, kwds, "Is#K|I", kwlist,
699 &fnum, &buf, &buflen, &offset, &mode)) {
703 req = cli_write_andx_send(NULL, self->ev, self->cli, fnum, mode,
704 (uint8_t *)buf, offset, buflen);
705 if (!py_tevent_req_wait_exc(self, req)) {
708 status = cli_write_andx_recv(req, &written);
711 if (!NT_STATUS_IS_OK(status)) {
712 PyErr_SetNTSTATUS(status);
715 return Py_BuildValue("K", (unsigned long long)written);
718 static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
722 unsigned long long offset;
724 struct tevent_req *req;
730 static const char *kwlist[] = {
731 "fnum", "offset", "size", NULL };
733 if (!ParseTupleAndKeywords(
734 args, kwds, "IKI", kwlist, &fnum, &offset,
739 req = cli_read_andx_send(NULL, self->ev, self->cli, fnum,
741 if (!py_tevent_req_wait_exc(self, req)) {
744 status = cli_read_andx_recv(req, &buflen, &buf);
746 if (!NT_STATUS_IS_OK(status)) {
748 PyErr_SetNTSTATUS(status);
751 result = Py_BuildValue("s#", (char *)buf, (int)buflen);
756 static PyObject *py_cli_ftruncate(struct py_cli_state *self, PyObject *args,
760 unsigned long long size;
761 struct tevent_req *req;
764 static const char *kwlist[] = {
765 "fnum", "size", NULL };
767 if (!ParseTupleAndKeywords(
768 args, kwds, "IK", kwlist, &fnum, &size)) {
772 req = cli_ftruncate_send(NULL, self->ev, self->cli, fnum, size);
773 if (!py_tevent_req_wait_exc(self, req)) {
776 status = cli_ftruncate_recv(req);
779 if (!NT_STATUS_IS_OK(status)) {
780 PyErr_SetNTSTATUS(status);
786 static PyObject *py_cli_delete_on_close(struct py_cli_state *self,
791 struct tevent_req *req;
794 static const char *kwlist[] = {
795 "fnum", "flag", NULL };
797 if (!ParseTupleAndKeywords(
798 args, kwds, "II", kwlist, &fnum, &flag)) {
802 req = cli_nt_delete_on_close_send(NULL, self->ev, self->cli, fnum,
804 if (!py_tevent_req_wait_exc(self, req)) {
807 status = cli_nt_delete_on_close_recv(req);
810 if (!NT_STATUS_IS_OK(status)) {
811 PyErr_SetNTSTATUS(status);
817 static PyObject *py_cli_list(struct py_cli_state *self,
823 FILE_ATTRIBUTE_DIRECTORY |
824 FILE_ATTRIBUTE_SYSTEM |
825 FILE_ATTRIBUTE_HIDDEN;
826 unsigned info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
827 struct tevent_req *req;
829 struct file_info *finfos;
830 size_t i, num_finfos;
833 const char *kwlist[] = {
834 "mask", "attribute", "info_level", NULL
837 if (!ParseTupleAndKeywords(
838 args, kwds, "s|II", kwlist,
839 &mask, &attribute, &info_level)) {
843 req = cli_list_send(NULL, self->ev, self->cli, mask, attribute,
845 if (!py_tevent_req_wait_exc(self, req)) {
848 status = cli_list_recv(req, NULL, &finfos, &num_finfos);
851 if (!NT_STATUS_IS_OK(status)) {
852 PyErr_SetNTSTATUS(status);
856 result = Py_BuildValue("[]");
857 if (result == NULL) {
861 for (i=0; i<num_finfos; i++) {
862 struct file_info *finfo = &finfos[i];
866 file = Py_BuildValue(
869 "mode", (int)finfo->mode);
875 ret = PyList_Append(result, file);
885 static PyMethodDef py_cli_state_methods[] = {
886 { "create", (PyCFunction)py_cli_create, METH_VARARGS|METH_KEYWORDS,
888 { "close", (PyCFunction)py_cli_close, METH_VARARGS,
889 "Close a file handle" },
890 { "write", (PyCFunction)py_cli_write, METH_VARARGS|METH_KEYWORDS,
891 "Write to a file handle" },
892 { "read", (PyCFunction)py_cli_read, METH_VARARGS|METH_KEYWORDS,
893 "Read from a file handle" },
894 { "truncate", (PyCFunction)py_cli_ftruncate,
895 METH_VARARGS|METH_KEYWORDS,
897 { "delete_on_close", (PyCFunction)py_cli_delete_on_close,
898 METH_VARARGS|METH_KEYWORDS,
899 "Set/Reset the delete on close flag" },
900 { "readdir", (PyCFunction)py_cli_list,
901 METH_VARARGS|METH_KEYWORDS,
902 "List a directory" },
903 { "get_oplock_break", (PyCFunction)py_cli_get_oplock_break,
904 METH_VARARGS, "Wait for an oplock break" },
905 { NULL, NULL, 0, NULL }
908 static PyTypeObject py_cli_state_type = {
909 PyVarObject_HEAD_INIT(NULL, 0)
910 .tp_name = "libsmb_samba_internal.Conn",
911 .tp_basicsize = sizeof(struct py_cli_state),
912 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
913 .tp_doc = "libsmb connection",
914 .tp_new = py_cli_state_new,
915 .tp_init = (initproc)py_cli_state_init,
916 .tp_dealloc = (destructor)py_cli_state_dealloc,
917 .tp_methods = py_cli_state_methods,
920 static PyMethodDef py_libsmb_methods[] = {
924 void initlibsmb_samba_internal(void);
926 static struct PyModuleDef moduledef = {
927 PyModuleDef_HEAD_INIT,
928 .m_name = "libsmb_samba_internal",
929 .m_doc = "libsmb wrapper",
931 .m_methods = py_libsmb_methods,
934 MODULE_INIT_FUNC(libsmb_samba_internal)
940 m = PyModule_Create(&moduledef);
944 if (PyType_Ready(&py_cli_state_type) < 0) {
947 Py_INCREF(&py_cli_state_type);
948 PyModule_AddObject(m, "Conn", (PyObject *)&py_cli_state_type);