python3 port for libsmb_samba_internal module
[metze/samba/wip.git] / source3 / libsmb / pylibsmb.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Samba-internal work in progress Python binding for libsmbclient
4  *
5  * Copyright (C) Volker Lendecke 2012
6  *
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.
11  *
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.
16  *
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/>.
19 */
20
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"
30
31 static PyTypeObject *get_pytype(const char *module, const char *type)
32 {
33         PyObject *mod;
34         PyTypeObject *result;
35
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;
42         }
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;
50         }
51         return result;
52 }
53
54 /*
55  * We're using "const char * const *" for keywords,
56  * PyArg_ParseTupleAndKeywords expects a "char **". Confine the
57  * inevitable warnings to just one place.
58  */
59 static int ParseTupleAndKeywords(PyObject *args, PyObject *kw,
60                                  const char *format, const char * const *keywords,
61                                  ...)
62 {
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;
71 }
72
73 struct py_cli_thread;
74
75 struct py_cli_oplock_break {
76         uint16_t fnum;
77         uint8_t level;
78 };
79
80 struct py_cli_state {
81         PyObject_HEAD
82         struct cli_state *cli;
83         struct tevent_context *ev;
84         struct py_cli_thread *thread_state;
85
86         struct tevent_req *oplock_waiter;
87         struct py_cli_oplock_break *oplock_breaks;
88         struct py_tevent_cond *oplock_cond;
89 };
90
91 #if HAVE_PTHREAD
92
93 #include <pthread.h>
94
95 struct py_cli_thread {
96
97         /*
98          * Pipe to make the poll thread wake up in our destructor, so
99          * that we can exit and join the thread.
100          */
101         int shutdown_pipe[2];
102         struct tevent_fd *shutdown_fde;
103         bool do_shutdown;
104         pthread_t id;
105
106         /*
107          * Thread state to release the GIL during the poll(2) syscall
108          */
109         PyThreadState *py_threadstate;
110 };
111
112 static void *py_cli_state_poll_thread(void *private_data)
113 {
114         struct py_cli_state *self = (struct py_cli_state *)private_data;
115         struct py_cli_thread *t = self->thread_state;
116         PyGILState_STATE gstate;
117
118         gstate = PyGILState_Ensure();
119
120         while (!t->do_shutdown) {
121                 int ret;
122                 ret = tevent_loop_once(self->ev);
123                 assert(ret == 0);
124         }
125         PyGILState_Release(gstate);
126         return NULL;
127 }
128
129 static void py_cli_state_trace_callback(enum tevent_trace_point point,
130                                         void *private_data)
131 {
132         struct py_cli_state *self = (struct py_cli_state *)private_data;
133         struct py_cli_thread *t = self->thread_state;
134
135         switch(point) {
136         case TEVENT_TRACE_BEFORE_WAIT:
137                 assert(t->py_threadstate == NULL);
138                 t->py_threadstate = PyEval_SaveThread();
139                 break;
140         case TEVENT_TRACE_AFTER_WAIT:
141                 assert(t->py_threadstate != NULL);
142                 PyEval_RestoreThread(t->py_threadstate);
143                 t->py_threadstate = NULL;
144                 break;
145         default:
146                 break;
147         }
148 }
149
150 static void py_cli_state_shutdown_handler(struct tevent_context *ev,
151                                           struct tevent_fd *fde,
152                                           uint16_t flags,
153                                           void *private_data)
154 {
155         struct py_cli_state *self = (struct py_cli_state *)private_data;
156         struct py_cli_thread *t = self->thread_state;
157
158         if ((flags & TEVENT_FD_READ) == 0) {
159                 return;
160         }
161         TALLOC_FREE(t->shutdown_fde);
162         t->do_shutdown = true;
163 }
164
165 static int py_cli_thread_destructor(struct py_cli_thread *t)
166 {
167         char c = 0;
168         ssize_t written;
169         int ret;
170
171         do {
172                 /*
173                  * This will wake the poll thread from the poll(2)
174                  */
175                 written = write(t->shutdown_pipe[1], &c, 1);
176         } while ((written == -1) && (errno == EINTR));
177
178         /*
179          * Allow the poll thread to do its own cleanup under the GIL
180          */
181         Py_BEGIN_ALLOW_THREADS
182         ret = pthread_join(t->id, NULL);
183         Py_END_ALLOW_THREADS
184         assert(ret == 0);
185
186         if (t->shutdown_pipe[0] != -1) {
187                 close(t->shutdown_pipe[0]);
188                 t->shutdown_pipe[0] = -1;
189         }
190         if (t->shutdown_pipe[1] != -1) {
191                 close(t->shutdown_pipe[1]);
192                 t->shutdown_pipe[1] = -1;
193         }
194         return 0;
195 }
196
197 static bool py_cli_state_setup_ev(struct py_cli_state *self)
198 {
199         struct py_cli_thread *t = NULL;
200         int ret;
201
202         self->ev = tevent_context_init_byname(NULL, "poll_mt");
203         if (self->ev == NULL) {
204                 goto fail;
205         }
206         samba_tevent_set_debug(self->ev, "pylibsmb_tevent_mt");
207         tevent_set_trace_callback(self->ev, py_cli_state_trace_callback, self);
208
209         self->thread_state = talloc_zero(NULL, struct py_cli_thread);
210         if (self->thread_state == NULL) {
211                 goto fail;
212         }
213         t = self->thread_state;
214
215         ret = pipe(t->shutdown_pipe);
216         if (ret == -1) {
217                 goto fail;
218         }
219         t->shutdown_fde = tevent_add_fd(
220                 self->ev, self->ev, t->shutdown_pipe[0], TEVENT_FD_READ,
221                 py_cli_state_shutdown_handler, self);
222         if (t->shutdown_fde == NULL) {
223                 goto fail;
224         }
225
226         PyEval_InitThreads();
227
228         ret = pthread_create(&t->id, NULL, py_cli_state_poll_thread, self);
229         if (ret != 0) {
230                 goto fail;
231         }
232         talloc_set_destructor(self->thread_state, py_cli_thread_destructor);
233         return true;
234
235 fail:
236         if (t != NULL) {
237                 TALLOC_FREE(t->shutdown_fde);
238
239                 if (t->shutdown_pipe[0] != -1) {
240                         close(t->shutdown_pipe[0]);
241                         t->shutdown_pipe[0] = -1;
242                 }
243                 if (t->shutdown_pipe[1] != -1) {
244                         close(t->shutdown_pipe[1]);
245                         t->shutdown_pipe[1] = -1;
246                 }
247         }
248
249         TALLOC_FREE(self->thread_state);
250         TALLOC_FREE(self->ev);
251         return false;
252 }
253
254 struct py_tevent_cond {
255         pthread_mutex_t mutex;
256         pthread_cond_t cond;
257         bool is_done;
258 };
259
260 static void py_tevent_signalme(struct tevent_req *req);
261
262 static int py_tevent_cond_wait(struct py_tevent_cond *cond)
263 {
264         int ret, result;
265
266         result = pthread_mutex_init(&cond->mutex, NULL);
267         if (result != 0) {
268                 goto fail;
269         }
270         result = pthread_cond_init(&cond->cond, NULL);
271         if (result != 0) {
272                 goto fail_mutex;
273         }
274
275         result = pthread_mutex_lock(&cond->mutex);
276         if (result != 0) {
277                 goto fail_cond;
278         }
279
280         cond->is_done = false;
281
282         while (!cond->is_done) {
283
284                 Py_BEGIN_ALLOW_THREADS
285                 result = pthread_cond_wait(&cond->cond, &cond->mutex);
286                 Py_END_ALLOW_THREADS
287
288                 if (result != 0) {
289                         goto fail_unlock;
290                 }
291         }
292
293 fail_unlock:
294         ret = pthread_mutex_unlock(&cond->mutex);
295         assert(ret == 0);
296 fail_cond:
297         ret = pthread_cond_destroy(&cond->cond);
298         assert(ret == 0);
299 fail_mutex:
300         ret = pthread_mutex_destroy(&cond->mutex);
301         assert(ret == 0);
302 fail:
303         return result;
304 }
305
306 static int py_tevent_req_wait(struct tevent_context *ev,
307                               struct tevent_req *req)
308 {
309         struct py_tevent_cond cond;
310         tevent_req_set_callback(req, py_tevent_signalme, &cond);
311         return py_tevent_cond_wait(&cond);
312 }
313
314 static void py_tevent_cond_signal(struct py_tevent_cond *cond)
315 {
316         int ret;
317
318         ret = pthread_mutex_lock(&cond->mutex);
319         assert(ret == 0);
320
321         cond->is_done = true;
322
323         ret = pthread_cond_signal(&cond->cond);
324         assert(ret == 0);
325         ret = pthread_mutex_unlock(&cond->mutex);
326         assert(ret == 0);
327 }
328
329 static void py_tevent_signalme(struct tevent_req *req)
330 {
331         struct py_tevent_cond *cond = (struct py_tevent_cond *)
332                 tevent_req_callback_data_void(req);
333
334         py_tevent_cond_signal(cond);
335 }
336
337 #else
338
339 static bool py_cli_state_setup_ev(struct py_cli_state *self)
340 {
341         self->ev = tevent_context_init(NULL);
342         if (self->ev == NULL) {
343                 return false;
344         }
345
346         samba_tevent_set_debug(self->ev, "pylibsmb_tevent");
347
348         return true;
349 }
350
351 static int py_tevent_req_wait(struct tevent_context *ev,
352                               struct tevent_req *req)
353 {
354         while (tevent_req_is_in_progress(req)) {
355                 int ret;
356
357                 ret = tevent_loop_once(ev);
358                 if (ret != 0) {
359                         return ret;
360                 }
361         }
362         return 0;
363 }
364
365 #endif
366
367 static bool py_tevent_req_wait_exc(struct tevent_context *ev,
368                                    struct tevent_req *req)
369 {
370         int ret;
371
372         if (req == NULL) {
373                 PyErr_NoMemory();
374                 return false;
375         }
376         ret = py_tevent_req_wait(ev, req);
377         if (ret != 0) {
378                 TALLOC_FREE(req);
379                 errno = ret;
380                 PyErr_SetFromErrno(PyExc_RuntimeError);
381                 return false;
382         }
383         return true;
384 }
385
386 static PyObject *py_cli_state_new(PyTypeObject *type, PyObject *args,
387                                   PyObject *kwds)
388 {
389         struct py_cli_state *self;
390
391         self = (struct py_cli_state *)type->tp_alloc(type, 0);
392         if (self == NULL) {
393                 return NULL;
394         }
395         self->cli = NULL;
396         self->ev = NULL;
397         self->thread_state = NULL;
398         self->oplock_waiter = NULL;
399         self->oplock_cond = NULL;
400         self->oplock_breaks = NULL;
401         return (PyObject *)self;
402 }
403
404 static void py_cli_got_oplock_break(struct tevent_req *req);
405
406 static int py_cli_state_init(struct py_cli_state *self, PyObject *args,
407                              PyObject *kwds)
408 {
409         NTSTATUS status;
410         char *host, *share;
411         PyObject *creds = NULL;
412         struct cli_credentials *cli_creds;
413         struct tevent_req *req;
414         bool ret;
415         /*
416          * For now we only support SMB1,
417          * as most of the cli_*_send() function
418          * don't support SMB2, it's only plugged
419          * into the sync wrapper functions currently.
420          */
421         int flags = CLI_FULL_CONNECTION_FORCE_SMB1;
422
423         static const char *kwlist[] = {
424                 "host", "share", "credentials", NULL
425         };
426
427         PyTypeObject *py_type_Credentials = get_pytype(
428                 "samba.credentials", "Credentials");
429         if (py_type_Credentials == NULL) {
430                 return -1;
431         }
432
433         ret = ParseTupleAndKeywords(
434                 args, kwds, "ss|O!", kwlist,
435                 &host, &share, py_type_Credentials, &creds);
436
437         Py_DECREF(py_type_Credentials);
438
439         if (!ret) {
440                 return -1;
441         }
442
443         if (!py_cli_state_setup_ev(self)) {
444                 return -1;
445         }
446
447         if (creds == NULL) {
448                 cli_creds = cli_credentials_init_anon(NULL);
449         } else {
450                 cli_creds = PyCredentials_AsCliCredentials(creds);
451         }
452
453         req = cli_full_connection_creds_send(
454                 NULL, self->ev, "myname", host, NULL, 0, share, "?????",
455                 cli_creds, flags, SMB_SIGNING_DEFAULT);
456         if (!py_tevent_req_wait_exc(self->ev, req)) {
457                 return -1;
458         }
459         status = cli_full_connection_creds_recv(req, &self->cli);
460         TALLOC_FREE(req);
461
462         if (!NT_STATUS_IS_OK(status)) {
463                 PyErr_SetNTSTATUS(status);
464                 return -1;
465         }
466
467         self->oplock_waiter = cli_smb_oplock_break_waiter_send(
468                 self->ev, self->ev, self->cli);
469         if (self->oplock_waiter == NULL) {
470                 PyErr_NoMemory();
471                 return -1;
472         }
473         tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
474                                 self);
475         return 0;
476 }
477
478 static void py_cli_got_oplock_break(struct tevent_req *req)
479 {
480         struct py_cli_state *self = (struct py_cli_state *)
481                 tevent_req_callback_data_void(req);
482         struct py_cli_oplock_break b;
483         struct py_cli_oplock_break *tmp;
484         size_t num_breaks;
485         NTSTATUS status;
486
487         status = cli_smb_oplock_break_waiter_recv(req, &b.fnum, &b.level);
488         TALLOC_FREE(req);
489         self->oplock_waiter = NULL;
490
491         if (!NT_STATUS_IS_OK(status)) {
492                 return;
493         }
494
495         num_breaks = talloc_array_length(self->oplock_breaks);
496         tmp = talloc_realloc(self->ev, self->oplock_breaks,
497                              struct py_cli_oplock_break, num_breaks+1);
498         if (tmp == NULL) {
499                 return;
500         }
501         self->oplock_breaks = tmp;
502         self->oplock_breaks[num_breaks] = b;
503
504         if (self->oplock_cond != NULL) {
505                 py_tevent_cond_signal(self->oplock_cond);
506         }
507
508         self->oplock_waiter = cli_smb_oplock_break_waiter_send(
509                 self->ev, self->ev, self->cli);
510         if (self->oplock_waiter == NULL) {
511                 return;
512         }
513         tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
514                                 self);
515 }
516
517 static PyObject *py_cli_get_oplock_break(struct py_cli_state *self,
518                                          PyObject *args)
519 {
520         size_t num_oplock_breaks;
521
522         if (!PyArg_ParseTuple(args, "")) {
523                 return NULL;
524         }
525
526         if (self->oplock_cond != NULL) {
527                 errno = EBUSY;
528                 PyErr_SetFromErrno(PyExc_RuntimeError);
529                 return NULL;
530         }
531
532         num_oplock_breaks = talloc_array_length(self->oplock_breaks);
533
534         if (num_oplock_breaks == 0) {
535                 struct py_tevent_cond cond;
536                 int ret;
537
538                 self->oplock_cond = &cond;
539                 ret = py_tevent_cond_wait(&cond);
540                 self->oplock_cond = NULL;
541
542                 if (ret != 0) {
543                         errno = ret;
544                         PyErr_SetFromErrno(PyExc_RuntimeError);
545                         return NULL;
546                 }
547         }
548
549         num_oplock_breaks = talloc_array_length(self->oplock_breaks);
550         if (num_oplock_breaks > 0) {
551                 PyObject *result;
552
553                 result = Py_BuildValue(
554                         "{s:i,s:i}",
555                         "fnum", self->oplock_breaks[0].fnum,
556                         "level", self->oplock_breaks[0].level);
557
558                 memmove(&self->oplock_breaks[0], &self->oplock_breaks[1],
559                         sizeof(self->oplock_breaks[0]) *
560                         (num_oplock_breaks - 1));
561                 self->oplock_breaks = talloc_realloc(
562                         NULL, self->oplock_breaks, struct py_cli_oplock_break,
563                         num_oplock_breaks - 1);
564
565                 return result;
566         }
567         Py_RETURN_NONE;
568 }
569
570 static void py_cli_state_dealloc(struct py_cli_state *self)
571 {
572         TALLOC_FREE(self->thread_state);
573         TALLOC_FREE(self->oplock_waiter);
574         TALLOC_FREE(self->ev);
575
576         if (self->cli != NULL) {
577                 cli_shutdown(self->cli);
578                 self->cli = NULL;
579         }
580         Py_TYPE(self)->tp_free((PyObject *)self);
581 }
582
583 static PyObject *py_cli_create(struct py_cli_state *self, PyObject *args,
584                                PyObject *kwds)
585 {
586         char *fname;
587         unsigned CreateFlags = 0;
588         unsigned DesiredAccess = FILE_GENERIC_READ;
589         unsigned FileAttributes = 0;
590         unsigned ShareAccess = 0;
591         unsigned CreateDisposition = FILE_OPEN;
592         unsigned CreateOptions = 0;
593         unsigned SecurityFlags = 0;
594         uint16_t fnum;
595         struct tevent_req *req;
596         NTSTATUS status;
597
598         static const char *kwlist[] = {
599                 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
600                 "ShareAccess", "CreateDisposition", "CreateOptions",
601                 "SecurityFlags", NULL };
602
603         if (!ParseTupleAndKeywords(
604                     args, kwds, "s|IIIIIII", kwlist,
605                     &fname, &CreateFlags, &DesiredAccess, &FileAttributes,
606                     &ShareAccess, &CreateDisposition, &CreateOptions,
607                     &SecurityFlags)) {
608                 return NULL;
609         }
610
611         req = cli_ntcreate_send(NULL, self->ev, self->cli, fname, CreateFlags,
612                                 DesiredAccess, FileAttributes, ShareAccess,
613                                 CreateDisposition, CreateOptions,
614                                 SecurityFlags);
615         if (!py_tevent_req_wait_exc(self->ev, req)) {
616                 return NULL;
617         }
618         status = cli_ntcreate_recv(req, &fnum, NULL);
619         TALLOC_FREE(req);
620
621         if (!NT_STATUS_IS_OK(status)) {
622                 PyErr_SetNTSTATUS(status);
623                 return NULL;
624         }
625         return Py_BuildValue("I", (unsigned)fnum);
626 }
627
628 static PyObject *py_cli_close(struct py_cli_state *self, PyObject *args)
629 {
630         struct tevent_req *req;
631         int fnum;
632         NTSTATUS status;
633
634         if (!PyArg_ParseTuple(args, "i", &fnum)) {
635                 return NULL;
636         }
637
638         req = cli_close_send(NULL, self->ev, self->cli, fnum);
639         if (!py_tevent_req_wait_exc(self->ev, req)) {
640                 return NULL;
641         }
642         status = cli_close_recv(req);
643         TALLOC_FREE(req);
644
645         if (!NT_STATUS_IS_OK(status)) {
646                 PyErr_SetNTSTATUS(status);
647                 return NULL;
648         }
649         Py_RETURN_NONE;
650 }
651
652 static PyObject *py_cli_write(struct py_cli_state *self, PyObject *args,
653                               PyObject *kwds)
654 {
655         int fnum;
656         unsigned mode = 0;
657         char *buf;
658         Py_ssize_t buflen;
659         unsigned long long offset;
660         struct tevent_req *req;
661         NTSTATUS status;
662         size_t written;
663
664         static const char *kwlist[] = {
665                 "fnum", "buffer", "offset", "mode", NULL };
666
667         if (!ParseTupleAndKeywords(
668                     args, kwds, "Is#K|I", kwlist,
669                     &fnum, &buf, &buflen, &offset, &mode)) {
670                 return NULL;
671         }
672
673         req = cli_write_andx_send(NULL, self->ev, self->cli, fnum, mode,
674                                   (uint8_t *)buf, offset, buflen);
675         if (!py_tevent_req_wait_exc(self->ev, req)) {
676                 return NULL;
677         }
678         status = cli_write_andx_recv(req, &written);
679         TALLOC_FREE(req);
680
681         if (!NT_STATUS_IS_OK(status)) {
682                 PyErr_SetNTSTATUS(status);
683                 return NULL;
684         }
685         return Py_BuildValue("K", (unsigned long long)written);
686 }
687
688 static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
689                              PyObject *kwds)
690 {
691         int fnum;
692         unsigned long long offset;
693         unsigned size;
694         struct tevent_req *req;
695         NTSTATUS status;
696         uint8_t *buf;
697         ssize_t buflen;
698         PyObject *result;
699
700         static const char *kwlist[] = {
701                 "fnum", "offset", "size", NULL };
702
703         if (!ParseTupleAndKeywords(
704                     args, kwds, "IKI", kwlist, &fnum, &offset,
705                     &size)) {
706                 return NULL;
707         }
708
709         req = cli_read_andx_send(NULL, self->ev, self->cli, fnum,
710                                  offset, size);
711         if (!py_tevent_req_wait_exc(self->ev, req)) {
712                 return NULL;
713         }
714         status = cli_read_andx_recv(req, &buflen, &buf);
715
716         if (!NT_STATUS_IS_OK(status)) {
717                 TALLOC_FREE(req);
718                 PyErr_SetNTSTATUS(status);
719                 return NULL;
720         }
721         result = Py_BuildValue("s#", (char *)buf, (int)buflen);
722         TALLOC_FREE(req);
723         return result;
724 }
725
726 static PyObject *py_cli_ftruncate(struct py_cli_state *self, PyObject *args,
727                                   PyObject *kwds)
728 {
729         int fnum;
730         unsigned long long size;
731         struct tevent_req *req;
732         NTSTATUS status;
733
734         static const char *kwlist[] = {
735                 "fnum", "size", NULL };
736
737         if (!ParseTupleAndKeywords(
738                     args, kwds, "IK", kwlist, &fnum, &size)) {
739                 return NULL;
740         }
741
742         req = cli_ftruncate_send(NULL, self->ev, self->cli, fnum, size);
743         if (!py_tevent_req_wait_exc(self->ev, req)) {
744                 return NULL;
745         }
746         status = cli_ftruncate_recv(req);
747         TALLOC_FREE(req);
748
749         if (!NT_STATUS_IS_OK(status)) {
750                 PyErr_SetNTSTATUS(status);
751                 return NULL;
752         }
753         Py_RETURN_NONE;
754 }
755
756 static PyObject *py_cli_delete_on_close(struct py_cli_state *self,
757                                         PyObject *args,
758                                         PyObject *kwds)
759 {
760         unsigned fnum, flag;
761         struct tevent_req *req;
762         NTSTATUS status;
763
764         static const char *kwlist[] = {
765                 "fnum", "flag", NULL };
766
767         if (!ParseTupleAndKeywords(
768                     args, kwds, "II", kwlist, &fnum, &flag)) {
769                 return NULL;
770         }
771
772         req = cli_nt_delete_on_close_send(NULL, self->ev, self->cli, fnum,
773                                           flag);
774         if (!py_tevent_req_wait_exc(self->ev, req)) {
775                 return NULL;
776         }
777         status = cli_nt_delete_on_close_recv(req);
778         TALLOC_FREE(req);
779
780         if (!NT_STATUS_IS_OK(status)) {
781                 PyErr_SetNTSTATUS(status);
782                 return NULL;
783         }
784         Py_RETURN_NONE;
785 }
786
787 static PyObject *py_cli_list(struct py_cli_state *self,
788                              PyObject *args,
789                              PyObject *kwds)
790 {
791         char *mask;
792         unsigned attribute =
793                 FILE_ATTRIBUTE_DIRECTORY |
794                 FILE_ATTRIBUTE_SYSTEM |
795                 FILE_ATTRIBUTE_HIDDEN;
796         unsigned info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
797         struct tevent_req *req;
798         NTSTATUS status;
799         struct file_info *finfos;
800         size_t i, num_finfos;
801         PyObject *result;
802
803         const char *kwlist[] = {
804                 "mask", "attribute", "info_level", NULL
805         };
806
807         if (!ParseTupleAndKeywords(
808                     args, kwds, "s|II", kwlist,
809                     &mask, &attribute, &info_level)) {
810                 return NULL;
811         }
812
813         req = cli_list_send(NULL, self->ev, self->cli, mask, attribute,
814                             info_level);
815         if (!py_tevent_req_wait_exc(self->ev, req)) {
816                 return NULL;
817         }
818         status = cli_list_recv(req, NULL, &finfos, &num_finfos);
819         TALLOC_FREE(req);
820
821         if (!NT_STATUS_IS_OK(status)) {
822                 PyErr_SetNTSTATUS(status);
823                 return NULL;
824         }
825
826         result = Py_BuildValue("[]");
827         if (result == NULL) {
828                 return NULL;
829         }
830
831         for (i=0; i<num_finfos; i++) {
832                 struct file_info *finfo = &finfos[i];
833                 PyObject *file;
834                 int ret;
835
836                 file = Py_BuildValue(
837                         "{s:s,s:i}",
838                         "name", finfo->name,
839                         "mode", (int)finfo->mode);
840                 if (file == NULL) {
841                         Py_XDECREF(result);
842                         return NULL;
843                 }
844
845                 ret = PyList_Append(result, file);
846                 if (ret == -1) {
847                         Py_XDECREF(result);
848                         return NULL;
849                 }
850         }
851
852         return result;
853 }
854
855 static PyMethodDef py_cli_state_methods[] = {
856         { "create", (PyCFunction)py_cli_create, METH_VARARGS|METH_KEYWORDS,
857           "Open a file" },
858         { "close", (PyCFunction)py_cli_close, METH_VARARGS,
859           "Close a file handle" },
860         { "write", (PyCFunction)py_cli_write, METH_VARARGS|METH_KEYWORDS,
861           "Write to a file handle" },
862         { "read", (PyCFunction)py_cli_read, METH_VARARGS|METH_KEYWORDS,
863           "Read from a file handle" },
864         { "truncate", (PyCFunction)py_cli_ftruncate,
865           METH_VARARGS|METH_KEYWORDS,
866           "Truncate a file" },
867         { "delete_on_close", (PyCFunction)py_cli_delete_on_close,
868           METH_VARARGS|METH_KEYWORDS,
869           "Set/Reset the delete on close flag" },
870         { "readdir", (PyCFunction)py_cli_list,
871           METH_VARARGS|METH_KEYWORDS,
872           "List a directory" },
873         { "get_oplock_break", (PyCFunction)py_cli_get_oplock_break,
874           METH_VARARGS, "Wait for an oplock break" },
875         { NULL, NULL, 0, NULL }
876 };
877
878 static PyTypeObject py_cli_state_type = {
879         PyVarObject_HEAD_INIT(NULL, 0)
880         .tp_name = "libsmb_samba_internal.Conn",
881         .tp_basicsize = sizeof(struct py_cli_state),
882         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
883         .tp_doc = "libsmb connection",
884         .tp_new = py_cli_state_new,
885         .tp_init = (initproc)py_cli_state_init,
886         .tp_dealloc = (destructor)py_cli_state_dealloc,
887         .tp_methods = py_cli_state_methods,
888 };
889
890 static PyMethodDef py_libsmb_methods[] = {
891         { NULL },
892 };
893
894 void initlibsmb_samba_internal(void);
895
896 static struct PyModuleDef moduledef = {
897     PyModuleDef_HEAD_INIT,
898     .m_name = "libsmb_samba_internal",
899     .m_doc = "libsmb wrapper",
900     .m_size = -1,
901     .m_methods = py_libsmb_methods,
902 };
903
904 MODULE_INIT_FUNC(libsmb_samba_internal)
905 {
906         PyObject *m = NULL;
907
908         talloc_stackframe();
909
910         m = PyModule_Create(&moduledef);
911         if (m == NULL) {
912                 return m;
913         }
914         if (PyType_Ready(&py_cli_state_type) < 0) {
915                 return NULL;
916         }
917         Py_INCREF(&py_cli_state_type);
918         PyModule_AddObject(m, "Conn", (PyObject *)&py_cli_state_type);
919         return m;
920 }