s3:pylibsmb: add sign=True to require signing
[Samba.git] / source3 / libsmb / pylibsmb.c
blob8e63cbf85686283da1d7a791ddc5c6a188541331
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 "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"
29 #include "trans2.h"
31 static PyTypeObject *get_pytype(const char *module, const char *type)
33 PyObject *mod;
34 PyTypeObject *result;
36 mod = PyImport_ImportModule(module);
37 if (mod == NULL) {
38 PyErr_Format(PyExc_RuntimeError,
39 "Unable to import %s to check type %s",
40 module, type);
41 return NULL;
43 result = (PyTypeObject *)PyObject_GetAttrString(mod, type);
44 Py_DECREF(mod);
45 if (result == NULL) {
46 PyErr_Format(PyExc_RuntimeError,
47 "Unable to find type %s in module %s",
48 module, type);
49 return NULL;
51 return result;
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,
61 ...)
63 char **_keywords = discard_const_p(char *, keywords);
64 va_list a;
65 int ret;
66 va_start(a, keywords);
67 ret = PyArg_VaParseTupleAndKeywords(args, kw, format,
68 _keywords, a);
69 va_end(a);
70 return ret;
73 struct py_cli_thread;
75 struct py_cli_oplock_break {
76 uint16_t fnum;
77 uint8_t level;
80 struct py_cli_state {
81 PyObject_HEAD
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;
93 #ifdef HAVE_PTHREAD
95 #include <pthread.h>
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;
105 bool do_shutdown;
106 pthread_t id;
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) {
123 int ret;
124 ret = tevent_loop_once(self->ev);
125 assert(ret == 0);
127 PyGILState_Release(gstate);
128 return NULL;
131 static void py_cli_state_trace_callback(enum tevent_trace_point point,
132 void *private_data)
134 struct py_cli_state *self = (struct py_cli_state *)private_data;
135 struct py_cli_thread *t = self->thread_state;
137 switch(point) {
138 case TEVENT_TRACE_BEFORE_WAIT:
139 assert(t->py_threadstate == NULL);
140 t->py_threadstate = PyEval_SaveThread();
141 break;
142 case TEVENT_TRACE_AFTER_WAIT:
143 assert(t->py_threadstate != NULL);
144 PyEval_RestoreThread(t->py_threadstate);
145 t->py_threadstate = NULL;
146 break;
147 default:
148 break;
152 static void py_cli_state_shutdown_handler(struct tevent_context *ev,
153 struct tevent_fd *fde,
154 uint16_t flags,
155 void *private_data)
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) {
161 return;
163 TALLOC_FREE(t->shutdown_fde);
164 t->do_shutdown = true;
167 static int py_cli_thread_destructor(struct py_cli_thread *t)
169 char c = 0;
170 ssize_t written;
171 int ret;
173 do {
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);
185 Py_END_ALLOW_THREADS
186 assert(ret == 0);
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;
196 return 0;
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;
205 int ret;
207 self->ev = tevent_context_init_byname(NULL, "poll_mt");
208 if (self->ev == NULL) {
209 goto fail;
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) {
218 goto fail;
220 t = self->thread_state;
222 ret = pipe(t->shutdown_pipe);
223 if (ret == -1) {
224 goto fail;
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) {
230 goto fail;
233 PyEval_InitThreads();
235 ret = pthread_create(&t->id, NULL, py_cli_state_poll_thread, self);
236 if (ret != 0) {
237 goto fail;
239 talloc_set_destructor(self->thread_state, py_cli_thread_destructor);
240 return true;
242 fail:
243 if (t != NULL) {
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);
258 return false;
261 struct py_tevent_cond {
262 pthread_mutex_t mutex;
263 pthread_cond_t cond;
264 bool is_done;
267 static void py_tevent_signalme(struct tevent_req *req);
269 static int py_tevent_cond_wait(struct py_tevent_cond *cond)
271 int ret, result;
273 result = pthread_mutex_init(&cond->mutex, NULL);
274 if (result != 0) {
275 goto fail;
277 result = pthread_cond_init(&cond->cond, NULL);
278 if (result != 0) {
279 goto fail_mutex;
282 result = pthread_mutex_lock(&cond->mutex);
283 if (result != 0) {
284 goto fail_cond;
287 cond->is_done = false;
289 while (!cond->is_done) {
291 Py_BEGIN_ALLOW_THREADS
292 result = pthread_cond_wait(&cond->cond, &cond->mutex);
293 Py_END_ALLOW_THREADS
295 if (result != 0) {
296 goto fail_unlock;
300 fail_unlock:
301 ret = pthread_mutex_unlock(&cond->mutex);
302 assert(ret == 0);
303 fail_cond:
304 ret = pthread_cond_destroy(&cond->cond);
305 assert(ret == 0);
306 fail_mutex:
307 ret = pthread_mutex_destroy(&cond->mutex);
308 assert(ret == 0);
309 fail:
310 return result;
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)
323 int ret;
325 ret = pthread_mutex_lock(&cond->mutex);
326 assert(ret == 0);
328 cond->is_done = true;
330 ret = pthread_cond_signal(&cond->cond);
331 assert(ret == 0);
332 ret = pthread_mutex_unlock(&cond->mutex);
333 assert(ret == 0);
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);
344 #endif
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) {
353 return false;
356 samba_tevent_set_debug(self->ev, "pylibsmb_tevent");
358 self->req_wait_fn = py_tevent_req_wait;
360 return true;
363 static int py_tevent_req_wait(struct tevent_context *ev,
364 struct tevent_req *req)
366 while (tevent_req_is_in_progress(req)) {
367 int ret;
369 ret = tevent_loop_once(ev);
370 if (ret != 0) {
371 return ret;
374 return 0;
377 static bool py_tevent_req_wait_exc(struct py_cli_state *self,
378 struct tevent_req *req)
380 int ret;
382 if (req == NULL) {
383 PyErr_NoMemory();
384 return false;
386 ret = self->req_wait_fn(self->ev, req);
387 if (ret != 0) {
388 TALLOC_FREE(req);
389 errno = ret;
390 PyErr_SetFromErrno(PyExc_RuntimeError);
391 return false;
393 return true;
396 static PyObject *py_cli_state_new(PyTypeObject *type, PyObject *args,
397 PyObject *kwds)
399 struct py_cli_state *self;
401 self = (struct py_cli_state *)type->tp_alloc(type, 0);
402 if (self == NULL) {
403 return NULL;
405 self->cli = NULL;
406 self->ev = NULL;
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,
417 PyObject *kwds)
419 NTSTATUS status;
420 char *host, *share;
421 PyObject *creds = NULL;
422 struct cli_credentials *cli_creds;
423 PyObject *py_multi_threaded = Py_False;
424 bool multi_threaded = false;
425 PyObject *py_sign = Py_False;
426 bool sign = false;
427 int signing_state = SMB_SIGNING_DEFAULT;
428 struct tevent_req *req;
429 bool ret;
431 * For now we only support SMB1,
432 * as most of the cli_*_send() function
433 * don't support SMB2, it's only plugged
434 * into the sync wrapper functions currently.
436 int flags = CLI_FULL_CONNECTION_FORCE_SMB1;
438 static const char *kwlist[] = {
439 "host", "share", "credentials",
440 "multi_threaded", "sign", NULL
443 PyTypeObject *py_type_Credentials = get_pytype(
444 "samba.credentials", "Credentials");
445 if (py_type_Credentials == NULL) {
446 return -1;
449 ret = ParseTupleAndKeywords(
450 args, kwds, "ss|O!OO", kwlist,
451 &host, &share,
452 py_type_Credentials, &creds,
453 &py_multi_threaded,
454 &py_sign);
456 Py_DECREF(py_type_Credentials);
458 if (!ret) {
459 return -1;
462 multi_threaded = PyObject_IsTrue(py_multi_threaded);
463 sign = PyObject_IsTrue(py_sign);
465 if (sign) {
466 signing_state = SMB_SIGNING_REQUIRED;
469 if (multi_threaded) {
470 #ifdef HAVE_PTHREAD
471 ret = py_cli_state_setup_mt_ev(self);
472 if (!ret) {
473 return -1;
475 #else
476 PyErr_SetString(PyExc_RuntimeError,
477 "No PTHREAD support available");
478 return -1;
479 #endif
480 } else {
481 ret = py_cli_state_setup_ev(self);
482 if (!ret) {
483 return -1;
487 if (creds == NULL) {
488 cli_creds = cli_credentials_init_anon(NULL);
489 } else {
490 cli_creds = PyCredentials_AsCliCredentials(creds);
493 req = cli_full_connection_creds_send(
494 NULL, self->ev, "myname", host, NULL, 0, share, "?????",
495 cli_creds, flags, signing_state);
496 if (!py_tevent_req_wait_exc(self, req)) {
497 return -1;
499 status = cli_full_connection_creds_recv(req, &self->cli);
500 TALLOC_FREE(req);
502 if (!NT_STATUS_IS_OK(status)) {
503 PyErr_SetNTSTATUS(status);
504 return -1;
507 self->oplock_waiter = cli_smb_oplock_break_waiter_send(
508 self->ev, self->ev, self->cli);
509 if (self->oplock_waiter == NULL) {
510 PyErr_NoMemory();
511 return -1;
513 tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
514 self);
515 return 0;
518 static void py_cli_got_oplock_break(struct tevent_req *req)
520 struct py_cli_state *self = (struct py_cli_state *)
521 tevent_req_callback_data_void(req);
522 struct py_cli_oplock_break b;
523 struct py_cli_oplock_break *tmp;
524 size_t num_breaks;
525 NTSTATUS status;
527 status = cli_smb_oplock_break_waiter_recv(req, &b.fnum, &b.level);
528 TALLOC_FREE(req);
529 self->oplock_waiter = NULL;
531 if (!NT_STATUS_IS_OK(status)) {
532 return;
535 num_breaks = talloc_array_length(self->oplock_breaks);
536 tmp = talloc_realloc(self->ev, self->oplock_breaks,
537 struct py_cli_oplock_break, num_breaks+1);
538 if (tmp == NULL) {
539 return;
541 self->oplock_breaks = tmp;
542 self->oplock_breaks[num_breaks] = b;
544 if (self->oplock_cond != NULL) {
545 py_tevent_cond_signal(self->oplock_cond);
548 self->oplock_waiter = cli_smb_oplock_break_waiter_send(
549 self->ev, self->ev, self->cli);
550 if (self->oplock_waiter == NULL) {
551 return;
553 tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
554 self);
557 static PyObject *py_cli_get_oplock_break(struct py_cli_state *self,
558 PyObject *args)
560 size_t num_oplock_breaks;
562 if (!PyArg_ParseTuple(args, "")) {
563 return NULL;
566 if (self->oplock_cond != NULL) {
567 errno = EBUSY;
568 PyErr_SetFromErrno(PyExc_RuntimeError);
569 return NULL;
572 num_oplock_breaks = talloc_array_length(self->oplock_breaks);
574 if (num_oplock_breaks == 0) {
575 struct py_tevent_cond cond;
576 int ret;
578 self->oplock_cond = &cond;
579 ret = py_tevent_cond_wait(&cond);
580 self->oplock_cond = NULL;
582 if (ret != 0) {
583 errno = ret;
584 PyErr_SetFromErrno(PyExc_RuntimeError);
585 return NULL;
589 num_oplock_breaks = talloc_array_length(self->oplock_breaks);
590 if (num_oplock_breaks > 0) {
591 PyObject *result;
593 result = Py_BuildValue(
594 "{s:i,s:i}",
595 "fnum", self->oplock_breaks[0].fnum,
596 "level", self->oplock_breaks[0].level);
598 memmove(&self->oplock_breaks[0], &self->oplock_breaks[1],
599 sizeof(self->oplock_breaks[0]) *
600 (num_oplock_breaks - 1));
601 self->oplock_breaks = talloc_realloc(
602 NULL, self->oplock_breaks, struct py_cli_oplock_break,
603 num_oplock_breaks - 1);
605 return result;
607 Py_RETURN_NONE;
610 static void py_cli_state_dealloc(struct py_cli_state *self)
612 TALLOC_FREE(self->thread_state);
613 TALLOC_FREE(self->oplock_waiter);
614 TALLOC_FREE(self->ev);
616 if (self->cli != NULL) {
617 cli_shutdown(self->cli);
618 self->cli = NULL;
620 Py_TYPE(self)->tp_free((PyObject *)self);
623 static PyObject *py_cli_create(struct py_cli_state *self, PyObject *args,
624 PyObject *kwds)
626 char *fname;
627 unsigned CreateFlags = 0;
628 unsigned DesiredAccess = FILE_GENERIC_READ;
629 unsigned FileAttributes = 0;
630 unsigned ShareAccess = 0;
631 unsigned CreateDisposition = FILE_OPEN;
632 unsigned CreateOptions = 0;
633 unsigned SecurityFlags = 0;
634 uint16_t fnum;
635 struct tevent_req *req;
636 NTSTATUS status;
638 static const char *kwlist[] = {
639 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
640 "ShareAccess", "CreateDisposition", "CreateOptions",
641 "SecurityFlags", NULL };
643 if (!ParseTupleAndKeywords(
644 args, kwds, "s|IIIIIII", kwlist,
645 &fname, &CreateFlags, &DesiredAccess, &FileAttributes,
646 &ShareAccess, &CreateDisposition, &CreateOptions,
647 &SecurityFlags)) {
648 return NULL;
651 req = cli_ntcreate_send(NULL, self->ev, self->cli, fname, CreateFlags,
652 DesiredAccess, FileAttributes, ShareAccess,
653 CreateDisposition, CreateOptions,
654 SecurityFlags);
655 if (!py_tevent_req_wait_exc(self, req)) {
656 return NULL;
658 status = cli_ntcreate_recv(req, &fnum, NULL);
659 TALLOC_FREE(req);
661 if (!NT_STATUS_IS_OK(status)) {
662 PyErr_SetNTSTATUS(status);
663 return NULL;
665 return Py_BuildValue("I", (unsigned)fnum);
668 static PyObject *py_cli_close(struct py_cli_state *self, PyObject *args)
670 struct tevent_req *req;
671 int fnum;
672 NTSTATUS status;
674 if (!PyArg_ParseTuple(args, "i", &fnum)) {
675 return NULL;
678 req = cli_close_send(NULL, self->ev, self->cli, fnum);
679 if (!py_tevent_req_wait_exc(self, req)) {
680 return NULL;
682 status = cli_close_recv(req);
683 TALLOC_FREE(req);
685 if (!NT_STATUS_IS_OK(status)) {
686 PyErr_SetNTSTATUS(status);
687 return NULL;
689 Py_RETURN_NONE;
692 static PyObject *py_cli_write(struct py_cli_state *self, PyObject *args,
693 PyObject *kwds)
695 int fnum;
696 unsigned mode = 0;
697 char *buf;
698 Py_ssize_t buflen;
699 unsigned long long offset;
700 struct tevent_req *req;
701 NTSTATUS status;
702 size_t written;
704 static const char *kwlist[] = {
705 "fnum", "buffer", "offset", "mode", NULL };
707 if (!ParseTupleAndKeywords(
708 args, kwds, "Is#K|I", kwlist,
709 &fnum, &buf, &buflen, &offset, &mode)) {
710 return NULL;
713 req = cli_write_andx_send(NULL, self->ev, self->cli, fnum, mode,
714 (uint8_t *)buf, offset, buflen);
715 if (!py_tevent_req_wait_exc(self, req)) {
716 return NULL;
718 status = cli_write_andx_recv(req, &written);
719 TALLOC_FREE(req);
721 if (!NT_STATUS_IS_OK(status)) {
722 PyErr_SetNTSTATUS(status);
723 return NULL;
725 return Py_BuildValue("K", (unsigned long long)written);
728 static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
729 PyObject *kwds)
731 int fnum;
732 unsigned long long offset;
733 unsigned size;
734 struct tevent_req *req;
735 NTSTATUS status;
736 uint8_t *buf;
737 ssize_t buflen;
738 PyObject *result;
740 static const char *kwlist[] = {
741 "fnum", "offset", "size", NULL };
743 if (!ParseTupleAndKeywords(
744 args, kwds, "IKI", kwlist, &fnum, &offset,
745 &size)) {
746 return NULL;
749 req = cli_read_andx_send(NULL, self->ev, self->cli, fnum,
750 offset, size);
751 if (!py_tevent_req_wait_exc(self, req)) {
752 return NULL;
754 status = cli_read_andx_recv(req, &buflen, &buf);
756 if (!NT_STATUS_IS_OK(status)) {
757 TALLOC_FREE(req);
758 PyErr_SetNTSTATUS(status);
759 return NULL;
761 result = Py_BuildValue("s#", (char *)buf, (int)buflen);
762 TALLOC_FREE(req);
763 return result;
766 static PyObject *py_cli_ftruncate(struct py_cli_state *self, PyObject *args,
767 PyObject *kwds)
769 int fnum;
770 unsigned long long size;
771 struct tevent_req *req;
772 NTSTATUS status;
774 static const char *kwlist[] = {
775 "fnum", "size", NULL };
777 if (!ParseTupleAndKeywords(
778 args, kwds, "IK", kwlist, &fnum, &size)) {
779 return NULL;
782 req = cli_ftruncate_send(NULL, self->ev, self->cli, fnum, size);
783 if (!py_tevent_req_wait_exc(self, req)) {
784 return NULL;
786 status = cli_ftruncate_recv(req);
787 TALLOC_FREE(req);
789 if (!NT_STATUS_IS_OK(status)) {
790 PyErr_SetNTSTATUS(status);
791 return NULL;
793 Py_RETURN_NONE;
796 static PyObject *py_cli_delete_on_close(struct py_cli_state *self,
797 PyObject *args,
798 PyObject *kwds)
800 unsigned fnum, flag;
801 struct tevent_req *req;
802 NTSTATUS status;
804 static const char *kwlist[] = {
805 "fnum", "flag", NULL };
807 if (!ParseTupleAndKeywords(
808 args, kwds, "II", kwlist, &fnum, &flag)) {
809 return NULL;
812 req = cli_nt_delete_on_close_send(NULL, self->ev, self->cli, fnum,
813 flag);
814 if (!py_tevent_req_wait_exc(self, req)) {
815 return NULL;
817 status = cli_nt_delete_on_close_recv(req);
818 TALLOC_FREE(req);
820 if (!NT_STATUS_IS_OK(status)) {
821 PyErr_SetNTSTATUS(status);
822 return NULL;
824 Py_RETURN_NONE;
827 static PyObject *py_cli_list(struct py_cli_state *self,
828 PyObject *args,
829 PyObject *kwds)
831 char *mask;
832 unsigned attribute =
833 FILE_ATTRIBUTE_DIRECTORY |
834 FILE_ATTRIBUTE_SYSTEM |
835 FILE_ATTRIBUTE_HIDDEN;
836 unsigned info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
837 struct tevent_req *req;
838 NTSTATUS status;
839 struct file_info *finfos;
840 size_t i, num_finfos;
841 PyObject *result;
843 const char *kwlist[] = {
844 "mask", "attribute", "info_level", NULL
847 if (!ParseTupleAndKeywords(
848 args, kwds, "s|II", kwlist,
849 &mask, &attribute, &info_level)) {
850 return NULL;
853 req = cli_list_send(NULL, self->ev, self->cli, mask, attribute,
854 info_level);
855 if (!py_tevent_req_wait_exc(self, req)) {
856 return NULL;
858 status = cli_list_recv(req, NULL, &finfos, &num_finfos);
859 TALLOC_FREE(req);
861 if (!NT_STATUS_IS_OK(status)) {
862 PyErr_SetNTSTATUS(status);
863 return NULL;
866 result = Py_BuildValue("[]");
867 if (result == NULL) {
868 return NULL;
871 for (i=0; i<num_finfos; i++) {
872 struct file_info *finfo = &finfos[i];
873 PyObject *file;
874 int ret;
876 file = Py_BuildValue(
877 "{s:s,s:i}",
878 "name", finfo->name,
879 "mode", (int)finfo->mode);
880 if (file == NULL) {
881 Py_XDECREF(result);
882 return NULL;
885 ret = PyList_Append(result, file);
886 if (ret == -1) {
887 Py_XDECREF(result);
888 return NULL;
892 return result;
895 static PyMethodDef py_cli_state_methods[] = {
896 { "create", (PyCFunction)py_cli_create, METH_VARARGS|METH_KEYWORDS,
897 "Open a file" },
898 { "close", (PyCFunction)py_cli_close, METH_VARARGS,
899 "Close a file handle" },
900 { "write", (PyCFunction)py_cli_write, METH_VARARGS|METH_KEYWORDS,
901 "Write to a file handle" },
902 { "read", (PyCFunction)py_cli_read, METH_VARARGS|METH_KEYWORDS,
903 "Read from a file handle" },
904 { "truncate", (PyCFunction)py_cli_ftruncate,
905 METH_VARARGS|METH_KEYWORDS,
906 "Truncate a file" },
907 { "delete_on_close", (PyCFunction)py_cli_delete_on_close,
908 METH_VARARGS|METH_KEYWORDS,
909 "Set/Reset the delete on close flag" },
910 { "readdir", (PyCFunction)py_cli_list,
911 METH_VARARGS|METH_KEYWORDS,
912 "List a directory" },
913 { "get_oplock_break", (PyCFunction)py_cli_get_oplock_break,
914 METH_VARARGS, "Wait for an oplock break" },
915 { NULL, NULL, 0, NULL }
918 static PyTypeObject py_cli_state_type = {
919 PyVarObject_HEAD_INIT(NULL, 0)
920 .tp_name = "libsmb_samba_internal.Conn",
921 .tp_basicsize = sizeof(struct py_cli_state),
922 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
923 .tp_doc = "libsmb connection",
924 .tp_new = py_cli_state_new,
925 .tp_init = (initproc)py_cli_state_init,
926 .tp_dealloc = (destructor)py_cli_state_dealloc,
927 .tp_methods = py_cli_state_methods,
930 static PyMethodDef py_libsmb_methods[] = {
931 { NULL },
934 void initlibsmb_samba_internal(void);
936 static struct PyModuleDef moduledef = {
937 PyModuleDef_HEAD_INIT,
938 .m_name = "libsmb_samba_internal",
939 .m_doc = "libsmb wrapper",
940 .m_size = -1,
941 .m_methods = py_libsmb_methods,
944 MODULE_INIT_FUNC(libsmb_samba_internal)
946 PyObject *m = NULL;
948 talloc_stackframe();
950 m = PyModule_Create(&moduledef);
951 if (m == NULL) {
952 return m;
954 if (PyType_Ready(&py_cli_state_type) < 0) {
955 return NULL;
957 Py_INCREF(&py_cli_state_type);
958 PyModule_AddObject(m, "Conn", (PyObject *)&py_cli_state_type);
959 return m;