s3: Add "readdir" to pylibsmb
[Samba/id10ts.git] / source3 / libsmb / pylibsmb.c
blob6641918e97e32f07d569e93fafc59297d41323f7
1 /*
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/>.
21 #include <Python.h>
22 #include "includes.h"
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"
28 #include "trans2.h"
30 static PyTypeObject *get_pytype(const char *module, const char *type)
32 PyObject *mod;
33 PyTypeObject *result;
35 mod = PyImport_ImportModule(module);
36 if (mod == NULL) {
37 PyErr_Format(PyExc_RuntimeError,
38 "Unable to import %s to check type %s",
39 module, type);
40 return NULL;
42 result = (PyTypeObject *)PyObject_GetAttrString(mod, type);
43 Py_DECREF(mod);
44 if (result == NULL) {
45 PyErr_Format(PyExc_RuntimeError,
46 "Unable to find type %s in module %s",
47 module, type);
48 return NULL;
50 return result;
53 struct py_cli_thread;
55 struct py_cli_state {
56 PyObject_HEAD
57 struct cli_state *cli;
58 struct tevent_context *ev;
59 struct py_cli_thread *thread_state;
62 #if HAVE_PTHREAD
64 #include <pthread.h>
66 struct py_cli_thread {
69 * Pipe to make the poll thread wake up in our destructor, so
70 * that we can exit and join the thread.
72 int shutdown_pipe[2];
73 struct tevent_fd *shutdown_fde;
74 bool do_shutdown;
75 pthread_t id;
78 * Thread state to release the GIL during the poll(2) syscall
80 PyThreadState *py_threadstate;
83 static void *py_cli_state_poll_thread(void *private_data)
85 struct py_cli_state *self = (struct py_cli_state *)private_data;
86 struct py_cli_thread *t = self->thread_state;
87 PyGILState_STATE gstate;
89 gstate = PyGILState_Ensure();
91 while (!t->do_shutdown) {
92 int ret;
93 ret = tevent_loop_once(self->ev);
94 assert(ret == 0);
96 PyGILState_Release(gstate);
97 return NULL;
100 static void py_cli_state_trace_callback(enum tevent_trace_point point,
101 void *private_data)
103 struct py_cli_state *self = (struct py_cli_state *)private_data;
104 struct py_cli_thread *t = self->thread_state;
106 switch(point) {
107 case TEVENT_TRACE_BEFORE_WAIT:
108 assert(t->py_threadstate == NULL);
109 t->py_threadstate = PyEval_SaveThread();
110 break;
111 case TEVENT_TRACE_AFTER_WAIT:
112 assert(t->py_threadstate != NULL);
113 PyEval_RestoreThread(t->py_threadstate);
114 t->py_threadstate = NULL;
115 break;
116 default:
117 break;
121 static void py_cli_state_shutdown_handler(struct tevent_context *ev,
122 struct tevent_fd *fde,
123 uint16_t flags,
124 void *private_data)
126 struct py_cli_state *self = (struct py_cli_state *)private_data;
127 struct py_cli_thread *t = self->thread_state;
129 if ((flags & TEVENT_FD_READ) == 0) {
130 return;
132 TALLOC_FREE(t->shutdown_fde);
133 t->do_shutdown = true;
136 static int py_cli_thread_destructor(struct py_cli_thread *t)
138 char c = 0;
139 ssize_t written;
140 int ret;
142 do {
144 * This will wake the poll thread from the poll(2)
146 written = write(t->shutdown_pipe[1], &c, 1);
147 } while ((written == -1) && (errno == EINTR));
150 * Allow the poll thread to do its own cleanup under the GIL
152 Py_BEGIN_ALLOW_THREADS
153 ret = pthread_join(t->id, NULL);
154 Py_END_ALLOW_THREADS
155 assert(ret == 0);
157 if (t->shutdown_pipe[0] != -1) {
158 close(t->shutdown_pipe[0]);
159 t->shutdown_pipe[0] = -1;
161 if (t->shutdown_pipe[1] != -1) {
162 close(t->shutdown_pipe[1]);
163 t->shutdown_pipe[1] = -1;
165 return 0;
168 static bool py_cli_state_setup_ev(struct py_cli_state *self)
170 struct py_cli_thread *t = NULL;
171 int ret;
173 self->ev = tevent_context_init_byname(NULL, "poll_mt");
174 if (self->ev == NULL) {
175 goto fail;
177 tevent_set_trace_callback(self->ev, py_cli_state_trace_callback, self);
179 self->thread_state = talloc_zero(NULL, struct py_cli_thread);
180 if (self->thread_state == NULL) {
181 goto fail;
183 t = self->thread_state;
185 ret = pipe(t->shutdown_pipe);
186 if (ret == -1) {
187 goto fail;
189 t->shutdown_fde = tevent_add_fd(
190 self->ev, self->ev, t->shutdown_pipe[0], TEVENT_FD_READ,
191 py_cli_state_shutdown_handler, self);
192 if (t->shutdown_fde == NULL) {
193 goto fail;
196 PyEval_InitThreads();
198 ret = pthread_create(&t->id, NULL, py_cli_state_poll_thread, self);
199 if (ret != 0) {
200 goto fail;
202 talloc_set_destructor(self->thread_state, py_cli_thread_destructor);
203 return true;
205 fail:
206 if (t != NULL) {
207 TALLOC_FREE(t->shutdown_fde);
209 if (t->shutdown_pipe[0] != -1) {
210 close(t->shutdown_pipe[0]);
211 t->shutdown_pipe[0] = -1;
213 if (t->shutdown_pipe[1] != -1) {
214 close(t->shutdown_pipe[1]);
215 t->shutdown_pipe[1] = -1;
219 TALLOC_FREE(self->thread_state);
220 TALLOC_FREE(self->ev);
221 return false;
224 struct py_tevent_cond {
225 pthread_mutex_t mutex;
226 pthread_cond_t cond;
227 bool is_done;
230 static void py_tevent_signalme(struct tevent_req *req);
232 static int py_tevent_cond_wait(struct py_tevent_cond *cond)
234 int ret, result;
236 result = pthread_mutex_init(&cond->mutex, NULL);
237 if (result != 0) {
238 goto fail;
240 result = pthread_cond_init(&cond->cond, NULL);
241 if (result != 0) {
242 goto fail_mutex;
245 result = pthread_mutex_lock(&cond->mutex);
246 if (result != 0) {
247 goto fail_cond;
250 cond->is_done = false;
252 while (!cond->is_done) {
254 Py_BEGIN_ALLOW_THREADS
255 result = pthread_cond_wait(&cond->cond, &cond->mutex);
256 Py_END_ALLOW_THREADS
258 if (result != 0) {
259 goto fail_unlock;
263 fail_unlock:
264 ret = pthread_mutex_unlock(&cond->mutex);
265 assert(ret == 0);
266 fail_cond:
267 ret = pthread_cond_destroy(&cond->cond);
268 assert(ret == 0);
269 fail_mutex:
270 ret = pthread_mutex_destroy(&cond->mutex);
271 assert(ret == 0);
272 fail:
273 return result;
276 static int py_tevent_req_wait(struct tevent_context *ev,
277 struct tevent_req *req)
279 struct py_tevent_cond cond;
280 tevent_req_set_callback(req, py_tevent_signalme, &cond);
281 return py_tevent_cond_wait(&cond);
284 static void py_tevent_signalme(struct tevent_req *req)
286 struct py_tevent_cond *cond = (struct py_tevent_cond *)
287 tevent_req_callback_data_void(req);
288 int ret;
290 ret = pthread_mutex_lock(&cond->mutex);
291 assert(ret == 0);
293 cond->is_done = true;
295 ret = pthread_cond_signal(&cond->cond);
296 assert(ret == 0);
297 ret = pthread_mutex_unlock(&cond->mutex);
298 assert(ret == 0);
301 #else
303 static bool py_cli_state_setup_ev(struct py_cli_state *self)
305 self->ev = tevent_context_init(NULL);
306 return (self->ev != NULL);
309 static int py_tevent_req_wait(struct tevent_context *ev,
310 struct tevent_req *req)
312 while (tevent_req_is_in_progress(req)) {
313 int ret;
315 ret = tevent_loop_once(ev);
316 if (ret != 0) {
317 return ret;
320 return 0;
323 #endif
325 static bool py_tevent_req_wait_exc(struct tevent_context *ev,
326 struct tevent_req *req)
328 int ret;
330 if (req == NULL) {
331 PyErr_NoMemory();
332 return false;
334 ret = py_tevent_req_wait(ev, req);
335 if (ret != 0) {
336 TALLOC_FREE(req);
337 errno = ret;
338 PyErr_SetFromErrno(PyExc_RuntimeError);
339 return false;
341 return true;
344 static PyObject *py_cli_state_new(PyTypeObject *type, PyObject *args,
345 PyObject *kwds)
347 struct py_cli_state *self;
349 self = (struct py_cli_state *)type->tp_alloc(type, 0);
350 if (self == NULL) {
351 return NULL;
353 self->cli = NULL;
354 self->ev = NULL;
355 self->thread_state = NULL;
356 return (PyObject *)self;
359 static int py_cli_state_init(struct py_cli_state *self, PyObject *args,
360 PyObject *kwds)
362 NTSTATUS status;
363 char *host, *share;
364 PyObject *creds;
365 struct cli_credentials *cli_creds;
366 bool ret;
368 static const char *kwlist[] = {
369 "host", "share", "credentials", NULL
372 PyTypeObject *py_type_Credentials = get_pytype(
373 "samba.credentials", "Credentials");
374 if (py_type_Credentials == NULL) {
375 return -1;
378 ret = PyArg_ParseTupleAndKeywords(
379 args, kwds, "ss|O!", (char **)kwlist,
380 &host, &share, py_type_Credentials, &creds);
382 Py_DECREF(py_type_Credentials);
384 if (!ret) {
385 return -1;
388 if (!py_cli_state_setup_ev(self)) {
389 return -1;
392 cli_creds = cli_credentials_from_py_object(creds);
393 if (cli_creds == NULL) {
394 PyErr_SetString(PyExc_TypeError, "Expected credentials");
395 return -1;
398 status = cli_full_connection(
399 &self->cli, "myname", host, NULL, 0, share, "?????",
400 cli_credentials_get_username(cli_creds),
401 cli_credentials_get_domain(cli_creds),
402 cli_credentials_get_password(cli_creds),
403 0, 0);
404 if (!NT_STATUS_IS_OK(status)) {
405 PyErr_SetNTSTATUS(status);
406 return -1;
408 return 0;
411 static void py_cli_state_dealloc(struct py_cli_state *self)
413 TALLOC_FREE(self->thread_state);
414 TALLOC_FREE(self->ev);
416 if (self->cli != NULL) {
417 cli_shutdown(self->cli);
418 self->cli = NULL;
420 self->ob_type->tp_free((PyObject *)self);
423 static PyObject *py_cli_create(struct py_cli_state *self, PyObject *args,
424 PyObject *kwds)
426 char *fname;
427 unsigned CreateFlags = 0;
428 unsigned DesiredAccess = FILE_GENERIC_READ;
429 unsigned FileAttributes = 0;
430 unsigned ShareAccess = 0;
431 unsigned CreateDisposition = FILE_OPEN;
432 unsigned CreateOptions = 0;
433 unsigned SecurityFlags = 0;
434 uint16_t fnum;
435 struct tevent_req *req;
436 NTSTATUS status;
438 static const char *kwlist[] = {
439 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
440 "ShareAccess", "CreateDisposition", "CreateOptions",
441 "SecurityFlags", NULL };
443 if (!PyArg_ParseTupleAndKeywords(
444 args, kwds, "s|IIIIIII", (char **)kwlist,
445 &fname, &CreateFlags, &DesiredAccess, &FileAttributes,
446 &ShareAccess, &CreateDisposition, &CreateOptions,
447 &SecurityFlags)) {
448 return NULL;
451 req = cli_ntcreate_send(NULL, self->ev, self->cli, fname, CreateFlags,
452 DesiredAccess, FileAttributes, ShareAccess,
453 CreateDisposition, CreateOptions,
454 SecurityFlags);
455 if (!py_tevent_req_wait_exc(self->ev, req)) {
456 return NULL;
458 status = cli_ntcreate_recv(req, &fnum);
459 TALLOC_FREE(req);
461 if (!NT_STATUS_IS_OK(status)) {
462 PyErr_SetNTSTATUS(status);
463 return NULL;
465 return Py_BuildValue("I", (unsigned)fnum);
468 static PyObject *py_cli_close(struct py_cli_state *self, PyObject *args)
470 struct tevent_req *req;
471 int fnum;
472 NTSTATUS status;
474 if (!PyArg_ParseTuple(args, "i", &fnum)) {
475 return NULL;
478 req = cli_close_send(NULL, self->ev, self->cli, fnum);
479 if (!py_tevent_req_wait_exc(self->ev, req)) {
480 return NULL;
482 status = cli_close_recv(req);
483 TALLOC_FREE(req);
485 if (!NT_STATUS_IS_OK(status)) {
486 PyErr_SetNTSTATUS(status);
487 return NULL;
489 Py_INCREF(Py_None);
490 return Py_None;
493 static PyObject *py_cli_write(struct py_cli_state *self, PyObject *args,
494 PyObject *kwds)
496 int fnum;
497 unsigned mode = 0;
498 char *buf;
499 int buflen;
500 unsigned long long offset;
501 struct tevent_req *req;
502 NTSTATUS status;
503 size_t written;
505 static const char *kwlist[] = {
506 "fnum", "buffer", "offset", "mode", NULL };
508 if (!PyArg_ParseTupleAndKeywords(
509 args, kwds, "Is#K|I", (char **)kwlist,
510 &fnum, &buf, &buflen, &offset, &mode)) {
511 return NULL;
514 req = cli_write_andx_send(NULL, self->ev, self->cli, fnum, mode,
515 (uint8_t *)buf, offset, buflen);
516 if (!py_tevent_req_wait_exc(self->ev, req)) {
517 return NULL;
519 status = cli_write_andx_recv(req, &written);
520 TALLOC_FREE(req);
522 if (!NT_STATUS_IS_OK(status)) {
523 PyErr_SetNTSTATUS(status);
524 return NULL;
526 return Py_BuildValue("K", (unsigned long long)written);
529 static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
530 PyObject *kwds)
532 int fnum;
533 unsigned long long offset;
534 unsigned size;
535 struct tevent_req *req;
536 NTSTATUS status;
537 uint8_t *buf;
538 ssize_t buflen;
539 PyObject *result;
541 static const char *kwlist[] = {
542 "fnum", "offset", "size", NULL };
544 if (!PyArg_ParseTupleAndKeywords(
545 args, kwds, "IKI", (char **)kwlist, &fnum, &offset,
546 &size)) {
547 return NULL;
550 req = cli_read_andx_send(NULL, self->ev, self->cli, fnum,
551 offset, size);
552 if (!py_tevent_req_wait_exc(self->ev, req)) {
553 return NULL;
555 status = cli_read_andx_recv(req, &buflen, &buf);
557 if (!NT_STATUS_IS_OK(status)) {
558 TALLOC_FREE(req);
559 PyErr_SetNTSTATUS(status);
560 return NULL;
562 result = Py_BuildValue("s#", (char *)buf, (int)buflen);
563 TALLOC_FREE(req);
564 return result;
567 static PyObject *py_cli_ftruncate(struct py_cli_state *self, PyObject *args,
568 PyObject *kwds)
570 int fnum;
571 unsigned long long size;
572 struct tevent_req *req;
573 NTSTATUS status;
575 static const char *kwlist[] = {
576 "fnum", "size", NULL };
578 if (!PyArg_ParseTupleAndKeywords(
579 args, kwds, "IK", (char **)kwlist, &fnum, &size)) {
580 return NULL;
583 req = cli_ftruncate_send(NULL, self->ev, self->cli, fnum, size);
584 if (!py_tevent_req_wait_exc(self->ev, req)) {
585 return NULL;
587 status = cli_ftruncate_recv(req);
588 TALLOC_FREE(req);
590 if (!NT_STATUS_IS_OK(status)) {
591 PyErr_SetNTSTATUS(status);
592 return NULL;
594 Py_INCREF(Py_None);
595 return Py_None;
598 static PyObject *py_cli_delete_on_close(struct py_cli_state *self,
599 PyObject *args,
600 PyObject *kwds)
602 unsigned fnum, flag;
603 struct tevent_req *req;
604 NTSTATUS status;
606 static const char *kwlist[] = {
607 "fnum", "flag", NULL };
609 if (!PyArg_ParseTupleAndKeywords(
610 args, kwds, "II", (char **)kwlist, &fnum, &flag)) {
611 return NULL;
614 req = cli_nt_delete_on_close_send(NULL, self->ev, self->cli, fnum,
615 flag);
616 if (!py_tevent_req_wait_exc(self->ev, req)) {
617 return NULL;
619 status = cli_nt_delete_on_close_recv(req);
620 TALLOC_FREE(req);
622 if (!NT_STATUS_IS_OK(status)) {
623 PyErr_SetNTSTATUS(status);
624 return NULL;
626 Py_INCREF(Py_None);
627 return Py_None;
630 static PyObject *py_cli_list(struct py_cli_state *self,
631 PyObject *args,
632 PyObject *kwds)
634 char *mask;
635 unsigned attribute =
636 FILE_ATTRIBUTE_DIRECTORY |
637 FILE_ATTRIBUTE_SYSTEM |
638 FILE_ATTRIBUTE_HIDDEN;
639 unsigned info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
640 struct tevent_req *req;
641 NTSTATUS status;
642 struct file_info *finfos;
643 size_t i, num_finfos;
644 PyObject *result;
646 const char *kwlist[] = {
647 "mask", "attribute", "info_level", NULL
650 if (!PyArg_ParseTupleAndKeywords(
651 args, kwds, "s|II", (char **)kwlist,
652 &mask, &attribute, &info_level)) {
653 return NULL;
656 req = cli_list_send(NULL, self->ev, self->cli, mask, attribute,
657 info_level);
658 if (!py_tevent_req_wait_exc(self->ev, req)) {
659 return NULL;
661 status = cli_list_recv(req, NULL, &finfos, &num_finfos);
662 TALLOC_FREE(req);
664 if (!NT_STATUS_IS_OK(status)) {
665 PyErr_SetNTSTATUS(status);
666 return NULL;
669 result = Py_BuildValue("[]");
670 if (result == NULL) {
671 return NULL;
674 for (i=0; i<num_finfos; i++) {
675 struct file_info *finfo = &finfos[i];
676 PyObject *file;
677 int ret;
679 file = Py_BuildValue(
680 "{s:s,s:i}",
681 "name", finfo->name,
682 "mode", (int)finfo->mode);
683 if (file == NULL) {
684 Py_XDECREF(result);
685 return NULL;
688 ret = PyList_Append(result, file);
689 if (ret == -1) {
690 Py_XDECREF(result);
691 return NULL;
695 return result;
698 static PyMethodDef py_cli_state_methods[] = {
699 { "create", (PyCFunction)py_cli_create, METH_VARARGS|METH_KEYWORDS,
700 "Open a file" },
701 { "close", (PyCFunction)py_cli_close, METH_VARARGS,
702 "Close a file handle" },
703 { "write", (PyCFunction)py_cli_write, METH_VARARGS|METH_KEYWORDS,
704 "Write to a file handle" },
705 { "read", (PyCFunction)py_cli_read, METH_VARARGS|METH_KEYWORDS,
706 "Read from a file handle" },
707 { "truncate", (PyCFunction)py_cli_ftruncate,
708 METH_VARARGS|METH_KEYWORDS,
709 "Truncate a file" },
710 { "delete_on_close", (PyCFunction)py_cli_delete_on_close,
711 METH_VARARGS|METH_KEYWORDS,
712 "Set/Reset the delete on close flag" },
713 { "readdir", (PyCFunction)py_cli_list,
714 METH_VARARGS|METH_KEYWORDS,
715 "List a directory" },
716 { NULL, NULL, 0, NULL }
719 static PyTypeObject py_cli_state_type = {
720 PyObject_HEAD_INIT(NULL)
721 .tp_name = "libsmb_samba_internal.Conn",
722 .tp_basicsize = sizeof(struct py_cli_state),
723 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
724 .tp_doc = "libsmb connection",
725 .tp_new = py_cli_state_new,
726 .tp_init = (initproc)py_cli_state_init,
727 .tp_dealloc = (destructor)py_cli_state_dealloc,
728 .tp_methods = py_cli_state_methods,
731 static PyMethodDef py_libsmb_methods[] = {
732 { NULL },
735 void initlibsmb_samba_internal(void);
736 void initlibsmb_samba_internal(void)
738 PyObject *m;
740 talloc_stackframe();
742 m = Py_InitModule3("libsmb_samba_internal", py_libsmb_methods,
743 "libsmb wrapper");
745 if (PyType_Ready(&py_cli_state_type) < 0) {
746 return;
748 Py_INCREF(&py_cli_state_type);
749 PyModule_AddObject(m, "Conn", (PyObject *)&py_cli_state_type);