auth log: Add tests for anonymous bind and SamLogon
[Samba.git] / source3 / libsmb / pylibsmb.c
blob59c09983688d77f127d0dbcda924ebf1b4fc017d
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;
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,
60 ...)
62 char **_keywords = discard_const_p(char *, keywords);
63 va_list a;
64 int ret;
65 va_start(a, keywords);
66 ret = PyArg_VaParseTupleAndKeywords(args, kw, format,
67 _keywords, a);
68 va_end(a);
69 return ret;
72 struct py_cli_thread;
74 struct py_cli_oplock_break {
75 uint16_t fnum;
76 uint8_t level;
79 struct py_cli_state {
80 PyObject_HEAD
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;
90 #if HAVE_PTHREAD
92 #include <pthread.h>
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;
102 bool do_shutdown;
103 pthread_t id;
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) {
120 int ret;
121 ret = tevent_loop_once(self->ev);
122 assert(ret == 0);
124 PyGILState_Release(gstate);
125 return NULL;
128 static void py_cli_state_trace_callback(enum tevent_trace_point point,
129 void *private_data)
131 struct py_cli_state *self = (struct py_cli_state *)private_data;
132 struct py_cli_thread *t = self->thread_state;
134 switch(point) {
135 case TEVENT_TRACE_BEFORE_WAIT:
136 assert(t->py_threadstate == NULL);
137 t->py_threadstate = PyEval_SaveThread();
138 break;
139 case TEVENT_TRACE_AFTER_WAIT:
140 assert(t->py_threadstate != NULL);
141 PyEval_RestoreThread(t->py_threadstate);
142 t->py_threadstate = NULL;
143 break;
144 default:
145 break;
149 static void py_cli_state_shutdown_handler(struct tevent_context *ev,
150 struct tevent_fd *fde,
151 uint16_t flags,
152 void *private_data)
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) {
158 return;
160 TALLOC_FREE(t->shutdown_fde);
161 t->do_shutdown = true;
164 static int py_cli_thread_destructor(struct py_cli_thread *t)
166 char c = 0;
167 ssize_t written;
168 int ret;
170 do {
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);
182 Py_END_ALLOW_THREADS
183 assert(ret == 0);
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;
193 return 0;
196 static bool py_cli_state_setup_ev(struct py_cli_state *self)
198 struct py_cli_thread *t = NULL;
199 int ret;
201 self->ev = tevent_context_init_byname(NULL, "poll_mt");
202 if (self->ev == NULL) {
203 goto fail;
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) {
210 goto fail;
212 t = self->thread_state;
214 ret = pipe(t->shutdown_pipe);
215 if (ret == -1) {
216 goto fail;
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) {
222 goto fail;
225 PyEval_InitThreads();
227 ret = pthread_create(&t->id, NULL, py_cli_state_poll_thread, self);
228 if (ret != 0) {
229 goto fail;
231 talloc_set_destructor(self->thread_state, py_cli_thread_destructor);
232 return true;
234 fail:
235 if (t != NULL) {
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);
250 return false;
253 struct py_tevent_cond {
254 pthread_mutex_t mutex;
255 pthread_cond_t cond;
256 bool is_done;
259 static void py_tevent_signalme(struct tevent_req *req);
261 static int py_tevent_cond_wait(struct py_tevent_cond *cond)
263 int ret, result;
265 result = pthread_mutex_init(&cond->mutex, NULL);
266 if (result != 0) {
267 goto fail;
269 result = pthread_cond_init(&cond->cond, NULL);
270 if (result != 0) {
271 goto fail_mutex;
274 result = pthread_mutex_lock(&cond->mutex);
275 if (result != 0) {
276 goto fail_cond;
279 cond->is_done = false;
281 while (!cond->is_done) {
283 Py_BEGIN_ALLOW_THREADS
284 result = pthread_cond_wait(&cond->cond, &cond->mutex);
285 Py_END_ALLOW_THREADS
287 if (result != 0) {
288 goto fail_unlock;
292 fail_unlock:
293 ret = pthread_mutex_unlock(&cond->mutex);
294 assert(ret == 0);
295 fail_cond:
296 ret = pthread_cond_destroy(&cond->cond);
297 assert(ret == 0);
298 fail_mutex:
299 ret = pthread_mutex_destroy(&cond->mutex);
300 assert(ret == 0);
301 fail:
302 return result;
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)
315 int ret;
317 ret = pthread_mutex_lock(&cond->mutex);
318 assert(ret == 0);
320 cond->is_done = true;
322 ret = pthread_cond_signal(&cond->cond);
323 assert(ret == 0);
324 ret = pthread_mutex_unlock(&cond->mutex);
325 assert(ret == 0);
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);
336 #else
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) {
342 return false;
345 samba_tevent_set_debug(self->ev, "pylibsmb_tevent");
347 return true;
350 static int py_tevent_req_wait(struct tevent_context *ev,
351 struct tevent_req *req)
353 while (tevent_req_is_in_progress(req)) {
354 int ret;
356 ret = tevent_loop_once(ev);
357 if (ret != 0) {
358 return ret;
361 return 0;
364 #endif
366 static bool py_tevent_req_wait_exc(struct tevent_context *ev,
367 struct tevent_req *req)
369 int ret;
371 if (req == NULL) {
372 PyErr_NoMemory();
373 return false;
375 ret = py_tevent_req_wait(ev, req);
376 if (ret != 0) {
377 TALLOC_FREE(req);
378 errno = ret;
379 PyErr_SetFromErrno(PyExc_RuntimeError);
380 return false;
382 return true;
385 static PyObject *py_cli_state_new(PyTypeObject *type, PyObject *args,
386 PyObject *kwds)
388 struct py_cli_state *self;
390 self = (struct py_cli_state *)type->tp_alloc(type, 0);
391 if (self == NULL) {
392 return NULL;
394 self->cli = NULL;
395 self->ev = NULL;
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,
406 PyObject *kwds)
408 NTSTATUS status;
409 char *host, *share;
410 PyObject *creds = NULL;
411 struct cli_credentials *cli_creds;
412 struct tevent_req *req;
413 bool ret;
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) {
422 return -1;
425 ret = ParseTupleAndKeywords(
426 args, kwds, "ss|O!", kwlist,
427 &host, &share, py_type_Credentials, &creds);
429 Py_DECREF(py_type_Credentials);
431 if (!ret) {
432 return -1;
435 if (!py_cli_state_setup_ev(self)) {
436 return -1;
439 if (creds == NULL) {
440 cli_creds = cli_credentials_init_anon(NULL);
441 } else {
442 cli_creds = PyCredentials_AsCliCredentials(creds);
445 req = cli_full_connection_creds_send(
446 NULL, self->ev, "myname", host, NULL, 0, share, "?????",
447 cli_creds, 0, 0);
448 if (!py_tevent_req_wait_exc(self->ev, req)) {
449 return -1;
451 status = cli_full_connection_creds_recv(req, &self->cli);
452 TALLOC_FREE(req);
454 if (!NT_STATUS_IS_OK(status)) {
455 PyErr_SetNTSTATUS(status);
456 return -1;
459 self->oplock_waiter = cli_smb_oplock_break_waiter_send(
460 self->ev, self->ev, self->cli);
461 if (self->oplock_waiter == NULL) {
462 PyErr_NoMemory();
463 return -1;
465 tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
466 self);
467 return 0;
470 static void py_cli_got_oplock_break(struct tevent_req *req)
472 struct py_cli_state *self = (struct py_cli_state *)
473 tevent_req_callback_data_void(req);
474 struct py_cli_oplock_break b;
475 struct py_cli_oplock_break *tmp;
476 size_t num_breaks;
477 NTSTATUS status;
479 status = cli_smb_oplock_break_waiter_recv(req, &b.fnum, &b.level);
480 TALLOC_FREE(req);
481 self->oplock_waiter = NULL;
483 if (!NT_STATUS_IS_OK(status)) {
484 return;
487 num_breaks = talloc_array_length(self->oplock_breaks);
488 tmp = talloc_realloc(self->ev, self->oplock_breaks,
489 struct py_cli_oplock_break, num_breaks+1);
490 if (tmp == NULL) {
491 return;
493 self->oplock_breaks = tmp;
494 self->oplock_breaks[num_breaks] = b;
496 if (self->oplock_cond != NULL) {
497 py_tevent_cond_signal(self->oplock_cond);
500 self->oplock_waiter = cli_smb_oplock_break_waiter_send(
501 self->ev, self->ev, self->cli);
502 if (self->oplock_waiter == NULL) {
503 return;
505 tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
506 self);
509 static PyObject *py_cli_get_oplock_break(struct py_cli_state *self,
510 PyObject *args)
512 size_t num_oplock_breaks;
514 if (!PyArg_ParseTuple(args, "")) {
515 return NULL;
518 if (self->oplock_cond != NULL) {
519 errno = EBUSY;
520 PyErr_SetFromErrno(PyExc_RuntimeError);
521 return NULL;
524 num_oplock_breaks = talloc_array_length(self->oplock_breaks);
526 if (num_oplock_breaks == 0) {
527 struct py_tevent_cond cond;
528 int ret;
530 self->oplock_cond = &cond;
531 ret = py_tevent_cond_wait(&cond);
532 self->oplock_cond = NULL;
534 if (ret != 0) {
535 errno = ret;
536 PyErr_SetFromErrno(PyExc_RuntimeError);
537 return NULL;
541 num_oplock_breaks = talloc_array_length(self->oplock_breaks);
542 if (num_oplock_breaks > 0) {
543 PyObject *result;
545 result = Py_BuildValue(
546 "{s:i,s:i}",
547 "fnum", self->oplock_breaks[0].fnum,
548 "level", self->oplock_breaks[0].level);
550 memmove(&self->oplock_breaks[0], &self->oplock_breaks[1],
551 sizeof(self->oplock_breaks[0]) *
552 (num_oplock_breaks - 1));
553 self->oplock_breaks = talloc_realloc(
554 NULL, self->oplock_breaks, struct py_cli_oplock_break,
555 num_oplock_breaks - 1);
557 return result;
559 Py_RETURN_NONE;
562 static void py_cli_state_dealloc(struct py_cli_state *self)
564 TALLOC_FREE(self->thread_state);
565 TALLOC_FREE(self->oplock_waiter);
566 TALLOC_FREE(self->ev);
568 if (self->cli != NULL) {
569 cli_shutdown(self->cli);
570 self->cli = NULL;
572 self->ob_type->tp_free((PyObject *)self);
575 static PyObject *py_cli_create(struct py_cli_state *self, PyObject *args,
576 PyObject *kwds)
578 char *fname;
579 unsigned CreateFlags = 0;
580 unsigned DesiredAccess = FILE_GENERIC_READ;
581 unsigned FileAttributes = 0;
582 unsigned ShareAccess = 0;
583 unsigned CreateDisposition = FILE_OPEN;
584 unsigned CreateOptions = 0;
585 unsigned SecurityFlags = 0;
586 uint16_t fnum;
587 struct tevent_req *req;
588 NTSTATUS status;
590 static const char *kwlist[] = {
591 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
592 "ShareAccess", "CreateDisposition", "CreateOptions",
593 "SecurityFlags", NULL };
595 if (!ParseTupleAndKeywords(
596 args, kwds, "s|IIIIIII", kwlist,
597 &fname, &CreateFlags, &DesiredAccess, &FileAttributes,
598 &ShareAccess, &CreateDisposition, &CreateOptions,
599 &SecurityFlags)) {
600 return NULL;
603 req = cli_ntcreate_send(NULL, self->ev, self->cli, fname, CreateFlags,
604 DesiredAccess, FileAttributes, ShareAccess,
605 CreateDisposition, CreateOptions,
606 SecurityFlags);
607 if (!py_tevent_req_wait_exc(self->ev, req)) {
608 return NULL;
610 status = cli_ntcreate_recv(req, &fnum, NULL);
611 TALLOC_FREE(req);
613 if (!NT_STATUS_IS_OK(status)) {
614 PyErr_SetNTSTATUS(status);
615 return NULL;
617 return Py_BuildValue("I", (unsigned)fnum);
620 static PyObject *py_cli_close(struct py_cli_state *self, PyObject *args)
622 struct tevent_req *req;
623 int fnum;
624 NTSTATUS status;
626 if (!PyArg_ParseTuple(args, "i", &fnum)) {
627 return NULL;
630 req = cli_close_send(NULL, self->ev, self->cli, fnum);
631 if (!py_tevent_req_wait_exc(self->ev, req)) {
632 return NULL;
634 status = cli_close_recv(req);
635 TALLOC_FREE(req);
637 if (!NT_STATUS_IS_OK(status)) {
638 PyErr_SetNTSTATUS(status);
639 return NULL;
641 Py_RETURN_NONE;
644 static PyObject *py_cli_write(struct py_cli_state *self, PyObject *args,
645 PyObject *kwds)
647 int fnum;
648 unsigned mode = 0;
649 char *buf;
650 Py_ssize_t buflen;
651 unsigned long long offset;
652 struct tevent_req *req;
653 NTSTATUS status;
654 size_t written;
656 static const char *kwlist[] = {
657 "fnum", "buffer", "offset", "mode", NULL };
659 if (!ParseTupleAndKeywords(
660 args, kwds, "Is#K|I", kwlist,
661 &fnum, &buf, &buflen, &offset, &mode)) {
662 return NULL;
665 req = cli_write_andx_send(NULL, self->ev, self->cli, fnum, mode,
666 (uint8_t *)buf, offset, buflen);
667 if (!py_tevent_req_wait_exc(self->ev, req)) {
668 return NULL;
670 status = cli_write_andx_recv(req, &written);
671 TALLOC_FREE(req);
673 if (!NT_STATUS_IS_OK(status)) {
674 PyErr_SetNTSTATUS(status);
675 return NULL;
677 return Py_BuildValue("K", (unsigned long long)written);
680 static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
681 PyObject *kwds)
683 int fnum;
684 unsigned long long offset;
685 unsigned size;
686 struct tevent_req *req;
687 NTSTATUS status;
688 uint8_t *buf;
689 ssize_t buflen;
690 PyObject *result;
692 static const char *kwlist[] = {
693 "fnum", "offset", "size", NULL };
695 if (!ParseTupleAndKeywords(
696 args, kwds, "IKI", kwlist, &fnum, &offset,
697 &size)) {
698 return NULL;
701 req = cli_read_andx_send(NULL, self->ev, self->cli, fnum,
702 offset, size);
703 if (!py_tevent_req_wait_exc(self->ev, req)) {
704 return NULL;
706 status = cli_read_andx_recv(req, &buflen, &buf);
708 if (!NT_STATUS_IS_OK(status)) {
709 TALLOC_FREE(req);
710 PyErr_SetNTSTATUS(status);
711 return NULL;
713 result = Py_BuildValue("s#", (char *)buf, (int)buflen);
714 TALLOC_FREE(req);
715 return result;
718 static PyObject *py_cli_ftruncate(struct py_cli_state *self, PyObject *args,
719 PyObject *kwds)
721 int fnum;
722 unsigned long long size;
723 struct tevent_req *req;
724 NTSTATUS status;
726 static const char *kwlist[] = {
727 "fnum", "size", NULL };
729 if (!ParseTupleAndKeywords(
730 args, kwds, "IK", kwlist, &fnum, &size)) {
731 return NULL;
734 req = cli_ftruncate_send(NULL, self->ev, self->cli, fnum, size);
735 if (!py_tevent_req_wait_exc(self->ev, req)) {
736 return NULL;
738 status = cli_ftruncate_recv(req);
739 TALLOC_FREE(req);
741 if (!NT_STATUS_IS_OK(status)) {
742 PyErr_SetNTSTATUS(status);
743 return NULL;
745 Py_RETURN_NONE;
748 static PyObject *py_cli_delete_on_close(struct py_cli_state *self,
749 PyObject *args,
750 PyObject *kwds)
752 unsigned fnum, flag;
753 struct tevent_req *req;
754 NTSTATUS status;
756 static const char *kwlist[] = {
757 "fnum", "flag", NULL };
759 if (!ParseTupleAndKeywords(
760 args, kwds, "II", kwlist, &fnum, &flag)) {
761 return NULL;
764 req = cli_nt_delete_on_close_send(NULL, self->ev, self->cli, fnum,
765 flag);
766 if (!py_tevent_req_wait_exc(self->ev, req)) {
767 return NULL;
769 status = cli_nt_delete_on_close_recv(req);
770 TALLOC_FREE(req);
772 if (!NT_STATUS_IS_OK(status)) {
773 PyErr_SetNTSTATUS(status);
774 return NULL;
776 Py_RETURN_NONE;
779 static PyObject *py_cli_list(struct py_cli_state *self,
780 PyObject *args,
781 PyObject *kwds)
783 char *mask;
784 unsigned attribute =
785 FILE_ATTRIBUTE_DIRECTORY |
786 FILE_ATTRIBUTE_SYSTEM |
787 FILE_ATTRIBUTE_HIDDEN;
788 unsigned info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
789 struct tevent_req *req;
790 NTSTATUS status;
791 struct file_info *finfos;
792 size_t i, num_finfos;
793 PyObject *result;
795 const char *kwlist[] = {
796 "mask", "attribute", "info_level", NULL
799 if (!ParseTupleAndKeywords(
800 args, kwds, "s|II", kwlist,
801 &mask, &attribute, &info_level)) {
802 return NULL;
805 req = cli_list_send(NULL, self->ev, self->cli, mask, attribute,
806 info_level);
807 if (!py_tevent_req_wait_exc(self->ev, req)) {
808 return NULL;
810 status = cli_list_recv(req, NULL, &finfos, &num_finfos);
811 TALLOC_FREE(req);
813 if (!NT_STATUS_IS_OK(status)) {
814 PyErr_SetNTSTATUS(status);
815 return NULL;
818 result = Py_BuildValue("[]");
819 if (result == NULL) {
820 return NULL;
823 for (i=0; i<num_finfos; i++) {
824 struct file_info *finfo = &finfos[i];
825 PyObject *file;
826 int ret;
828 file = Py_BuildValue(
829 "{s:s,s:i}",
830 "name", finfo->name,
831 "mode", (int)finfo->mode);
832 if (file == NULL) {
833 Py_XDECREF(result);
834 return NULL;
837 ret = PyList_Append(result, file);
838 if (ret == -1) {
839 Py_XDECREF(result);
840 return NULL;
844 return result;
847 static PyMethodDef py_cli_state_methods[] = {
848 { "create", (PyCFunction)py_cli_create, METH_VARARGS|METH_KEYWORDS,
849 "Open a file" },
850 { "close", (PyCFunction)py_cli_close, METH_VARARGS,
851 "Close a file handle" },
852 { "write", (PyCFunction)py_cli_write, METH_VARARGS|METH_KEYWORDS,
853 "Write to a file handle" },
854 { "read", (PyCFunction)py_cli_read, METH_VARARGS|METH_KEYWORDS,
855 "Read from a file handle" },
856 { "truncate", (PyCFunction)py_cli_ftruncate,
857 METH_VARARGS|METH_KEYWORDS,
858 "Truncate a file" },
859 { "delete_on_close", (PyCFunction)py_cli_delete_on_close,
860 METH_VARARGS|METH_KEYWORDS,
861 "Set/Reset the delete on close flag" },
862 { "readdir", (PyCFunction)py_cli_list,
863 METH_VARARGS|METH_KEYWORDS,
864 "List a directory" },
865 { "get_oplock_break", (PyCFunction)py_cli_get_oplock_break,
866 METH_VARARGS, "Wait for an oplock break" },
867 { NULL, NULL, 0, NULL }
870 static PyTypeObject py_cli_state_type = {
871 PyObject_HEAD_INIT(NULL)
872 .tp_name = "libsmb_samba_internal.Conn",
873 .tp_basicsize = sizeof(struct py_cli_state),
874 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
875 .tp_doc = "libsmb connection",
876 .tp_new = py_cli_state_new,
877 .tp_init = (initproc)py_cli_state_init,
878 .tp_dealloc = (destructor)py_cli_state_dealloc,
879 .tp_methods = py_cli_state_methods,
882 static PyMethodDef py_libsmb_methods[] = {
883 { NULL },
886 void initlibsmb_samba_internal(void);
887 void initlibsmb_samba_internal(void)
889 PyObject *m;
891 talloc_stackframe();
893 m = Py_InitModule3("libsmb_samba_internal", py_libsmb_methods,
894 "libsmb wrapper");
896 if (PyType_Ready(&py_cli_state_type) < 0) {
897 return;
899 Py_INCREF(&py_cli_state_type);
900 PyModule_AddObject(m, "Conn", (PyObject *)&py_cli_state_type);