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