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