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;
413 static const char *kwlist[] = {
414 "host", "share", "credentials", NULL
417 PyTypeObject *py_type_Credentials = get_pytype(
418 "samba.credentials", "Credentials");
419 if (py_type_Credentials == NULL) {
423 ret = ParseTupleAndKeywords(
424 args, kwds, "ss|O!", kwlist,
425 &host, &share, py_type_Credentials, &creds);
427 Py_DECREF(py_type_Credentials);
433 if (!py_cli_state_setup_ev(self)) {
438 cli_creds = cli_credentials_init_anon(NULL);
440 cli_creds = PyCredentials_AsCliCredentials(creds);
443 status = cli_full_connection(
444 &self->cli, "myname", host, NULL, 0, share, "?????",
445 cli_credentials_get_username(cli_creds),
446 cli_credentials_get_domain(cli_creds),
447 cli_credentials_get_password(cli_creds),
449 if (!NT_STATUS_IS_OK(status)) {
450 PyErr_SetNTSTATUS(status);
454 self->oplock_waiter = cli_smb_oplock_break_waiter_send(
455 self->ev, self->ev, self->cli);
456 if (self->oplock_waiter == NULL) {
460 tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
465 static void py_cli_got_oplock_break(struct tevent_req *req)
467 struct py_cli_state *self = (struct py_cli_state *)
468 tevent_req_callback_data_void(req);
469 struct py_cli_oplock_break b;
470 struct py_cli_oplock_break *tmp;
474 status = cli_smb_oplock_break_waiter_recv(req, &b.fnum, &b.level);
476 self->oplock_waiter = NULL;
478 if (!NT_STATUS_IS_OK(status)) {
482 num_breaks = talloc_array_length(self->oplock_breaks);
483 tmp = talloc_realloc(self->ev, self->oplock_breaks,
484 struct py_cli_oplock_break, num_breaks+1);
488 self->oplock_breaks = tmp;
489 self->oplock_breaks[num_breaks] = b;
491 if (self->oplock_cond != NULL) {
492 py_tevent_cond_signal(self->oplock_cond);
495 self->oplock_waiter = cli_smb_oplock_break_waiter_send(
496 self->ev, self->ev, self->cli);
497 if (self->oplock_waiter == NULL) {
500 tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
504 static PyObject *py_cli_get_oplock_break(struct py_cli_state *self,
507 size_t num_oplock_breaks;
509 if (!PyArg_ParseTuple(args, "")) {
513 if (self->oplock_cond != NULL) {
515 PyErr_SetFromErrno(PyExc_RuntimeError);
519 num_oplock_breaks = talloc_array_length(self->oplock_breaks);
521 if (num_oplock_breaks == 0) {
522 struct py_tevent_cond cond;
525 self->oplock_cond = &cond;
526 ret = py_tevent_cond_wait(&cond);
527 self->oplock_cond = NULL;
531 PyErr_SetFromErrno(PyExc_RuntimeError);
536 num_oplock_breaks = talloc_array_length(self->oplock_breaks);
537 if (num_oplock_breaks > 0) {
540 result = Py_BuildValue(
542 "fnum", self->oplock_breaks[0].fnum,
543 "level", self->oplock_breaks[0].level);
545 memmove(&self->oplock_breaks[0], &self->oplock_breaks[1],
546 sizeof(self->oplock_breaks[0]) *
547 (num_oplock_breaks - 1));
548 self->oplock_breaks = talloc_realloc(
549 NULL, self->oplock_breaks, struct py_cli_oplock_break,
550 num_oplock_breaks - 1);
557 static void py_cli_state_dealloc(struct py_cli_state *self)
559 TALLOC_FREE(self->thread_state);
560 TALLOC_FREE(self->oplock_waiter);
561 TALLOC_FREE(self->ev);
563 if (self->cli != NULL) {
564 cli_shutdown(self->cli);
567 self->ob_type->tp_free((PyObject *)self);
570 static PyObject *py_cli_create(struct py_cli_state *self, PyObject *args,
574 unsigned CreateFlags = 0;
575 unsigned DesiredAccess = FILE_GENERIC_READ;
576 unsigned FileAttributes = 0;
577 unsigned ShareAccess = 0;
578 unsigned CreateDisposition = FILE_OPEN;
579 unsigned CreateOptions = 0;
580 unsigned SecurityFlags = 0;
582 struct tevent_req *req;
585 static const char *kwlist[] = {
586 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
587 "ShareAccess", "CreateDisposition", "CreateOptions",
588 "SecurityFlags", NULL };
590 if (!ParseTupleAndKeywords(
591 args, kwds, "s|IIIIIII", kwlist,
592 &fname, &CreateFlags, &DesiredAccess, &FileAttributes,
593 &ShareAccess, &CreateDisposition, &CreateOptions,
598 req = cli_ntcreate_send(NULL, self->ev, self->cli, fname, CreateFlags,
599 DesiredAccess, FileAttributes, ShareAccess,
600 CreateDisposition, CreateOptions,
602 if (!py_tevent_req_wait_exc(self->ev, req)) {
605 status = cli_ntcreate_recv(req, &fnum);
608 if (!NT_STATUS_IS_OK(status)) {
609 PyErr_SetNTSTATUS(status);
612 return Py_BuildValue("I", (unsigned)fnum);
615 static PyObject *py_cli_close(struct py_cli_state *self, PyObject *args)
617 struct tevent_req *req;
621 if (!PyArg_ParseTuple(args, "i", &fnum)) {
625 req = cli_close_send(NULL, self->ev, self->cli, fnum);
626 if (!py_tevent_req_wait_exc(self->ev, req)) {
629 status = cli_close_recv(req);
632 if (!NT_STATUS_IS_OK(status)) {
633 PyErr_SetNTSTATUS(status);
639 static PyObject *py_cli_write(struct py_cli_state *self, PyObject *args,
646 unsigned long long offset;
647 struct tevent_req *req;
651 static const char *kwlist[] = {
652 "fnum", "buffer", "offset", "mode", NULL };
654 if (!ParseTupleAndKeywords(
655 args, kwds, "Is#K|I", kwlist,
656 &fnum, &buf, &buflen, &offset, &mode)) {
660 req = cli_write_andx_send(NULL, self->ev, self->cli, fnum, mode,
661 (uint8_t *)buf, offset, buflen);
662 if (!py_tevent_req_wait_exc(self->ev, req)) {
665 status = cli_write_andx_recv(req, &written);
668 if (!NT_STATUS_IS_OK(status)) {
669 PyErr_SetNTSTATUS(status);
672 return Py_BuildValue("K", (unsigned long long)written);
675 static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
679 unsigned long long offset;
681 struct tevent_req *req;
687 static const char *kwlist[] = {
688 "fnum", "offset", "size", NULL };
690 if (!ParseTupleAndKeywords(
691 args, kwds, "IKI", kwlist, &fnum, &offset,
696 req = cli_read_andx_send(NULL, self->ev, self->cli, fnum,
698 if (!py_tevent_req_wait_exc(self->ev, req)) {
701 status = cli_read_andx_recv(req, &buflen, &buf);
703 if (!NT_STATUS_IS_OK(status)) {
705 PyErr_SetNTSTATUS(status);
708 result = Py_BuildValue("s#", (char *)buf, (int)buflen);
713 static PyObject *py_cli_ftruncate(struct py_cli_state *self, PyObject *args,
717 unsigned long long size;
718 struct tevent_req *req;
721 static const char *kwlist[] = {
722 "fnum", "size", NULL };
724 if (!ParseTupleAndKeywords(
725 args, kwds, "IK", kwlist, &fnum, &size)) {
729 req = cli_ftruncate_send(NULL, self->ev, self->cli, fnum, size);
730 if (!py_tevent_req_wait_exc(self->ev, req)) {
733 status = cli_ftruncate_recv(req);
736 if (!NT_STATUS_IS_OK(status)) {
737 PyErr_SetNTSTATUS(status);
743 static PyObject *py_cli_delete_on_close(struct py_cli_state *self,
748 struct tevent_req *req;
751 static const char *kwlist[] = {
752 "fnum", "flag", NULL };
754 if (!ParseTupleAndKeywords(
755 args, kwds, "II", kwlist, &fnum, &flag)) {
759 req = cli_nt_delete_on_close_send(NULL, self->ev, self->cli, fnum,
761 if (!py_tevent_req_wait_exc(self->ev, req)) {
764 status = cli_nt_delete_on_close_recv(req);
767 if (!NT_STATUS_IS_OK(status)) {
768 PyErr_SetNTSTATUS(status);
774 static PyObject *py_cli_list(struct py_cli_state *self,
780 FILE_ATTRIBUTE_DIRECTORY |
781 FILE_ATTRIBUTE_SYSTEM |
782 FILE_ATTRIBUTE_HIDDEN;
783 unsigned info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
784 struct tevent_req *req;
786 struct file_info *finfos;
787 size_t i, num_finfos;
790 const char *kwlist[] = {
791 "mask", "attribute", "info_level", NULL
794 if (!ParseTupleAndKeywords(
795 args, kwds, "s|II", kwlist,
796 &mask, &attribute, &info_level)) {
800 req = cli_list_send(NULL, self->ev, self->cli, mask, attribute,
802 if (!py_tevent_req_wait_exc(self->ev, req)) {
805 status = cli_list_recv(req, NULL, &finfos, &num_finfos);
808 if (!NT_STATUS_IS_OK(status)) {
809 PyErr_SetNTSTATUS(status);
813 result = Py_BuildValue("[]");
814 if (result == NULL) {
818 for (i=0; i<num_finfos; i++) {
819 struct file_info *finfo = &finfos[i];
823 file = Py_BuildValue(
826 "mode", (int)finfo->mode);
832 ret = PyList_Append(result, file);
842 static PyMethodDef py_cli_state_methods[] = {
843 { "create", (PyCFunction)py_cli_create, METH_VARARGS|METH_KEYWORDS,
845 { "close", (PyCFunction)py_cli_close, METH_VARARGS,
846 "Close a file handle" },
847 { "write", (PyCFunction)py_cli_write, METH_VARARGS|METH_KEYWORDS,
848 "Write to a file handle" },
849 { "read", (PyCFunction)py_cli_read, METH_VARARGS|METH_KEYWORDS,
850 "Read from a file handle" },
851 { "truncate", (PyCFunction)py_cli_ftruncate,
852 METH_VARARGS|METH_KEYWORDS,
854 { "delete_on_close", (PyCFunction)py_cli_delete_on_close,
855 METH_VARARGS|METH_KEYWORDS,
856 "Set/Reset the delete on close flag" },
857 { "readdir", (PyCFunction)py_cli_list,
858 METH_VARARGS|METH_KEYWORDS,
859 "List a directory" },
860 { "get_oplock_break", (PyCFunction)py_cli_get_oplock_break,
861 METH_VARARGS, "Wait for an oplock break" },
862 { NULL, NULL, 0, NULL }
865 static PyTypeObject py_cli_state_type = {
866 PyObject_HEAD_INIT(NULL)
867 .tp_name = "libsmb_samba_internal.Conn",
868 .tp_basicsize = sizeof(struct py_cli_state),
869 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
870 .tp_doc = "libsmb connection",
871 .tp_new = py_cli_state_new,
872 .tp_init = (initproc)py_cli_state_init,
873 .tp_dealloc = (destructor)py_cli_state_dealloc,
874 .tp_methods = py_cli_state_methods,
877 static PyMethodDef py_libsmb_methods[] = {
881 void initlibsmb_samba_internal(void);
882 void initlibsmb_samba_internal(void)
888 m = Py_InitModule3("libsmb_samba_internal", py_libsmb_methods,
891 if (PyType_Ready(&py_cli_state_type) < 0) {
894 Py_INCREF(&py_cli_state_type);
895 PyModule_AddObject(m, "Conn", (PyObject *)&py_cli_state_type);