s3:pylibsmb: add sign=True to require signing
[amitay/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         PyObject *py_sign = Py_False;
426         bool sign = false;
427         int signing_state = SMB_SIGNING_DEFAULT;
428         struct tevent_req *req;
429         bool ret;
430         /*
431          * For now we only support SMB1,
432          * as most of the cli_*_send() function
433          * don't support SMB2, it's only plugged
434          * into the sync wrapper functions currently.
435          */
436         int flags = CLI_FULL_CONNECTION_FORCE_SMB1;
437
438         static const char *kwlist[] = {
439                 "host", "share", "credentials",
440                 "multi_threaded", "sign", NULL
441         };
442
443         PyTypeObject *py_type_Credentials = get_pytype(
444                 "samba.credentials", "Credentials");
445         if (py_type_Credentials == NULL) {
446                 return -1;
447         }
448
449         ret = ParseTupleAndKeywords(
450                 args, kwds, "ss|O!OO", kwlist,
451                 &host, &share,
452                 py_type_Credentials, &creds,
453                 &py_multi_threaded,
454                 &py_sign);
455
456         Py_DECREF(py_type_Credentials);
457
458         if (!ret) {
459                 return -1;
460         }
461
462         multi_threaded = PyObject_IsTrue(py_multi_threaded);
463         sign = PyObject_IsTrue(py_sign);
464
465         if (sign) {
466                 signing_state = SMB_SIGNING_REQUIRED;
467         }
468
469         if (multi_threaded) {
470 #ifdef HAVE_PTHREAD
471                 ret = py_cli_state_setup_mt_ev(self);
472                 if (!ret) {
473                         return -1;
474                 }
475 #else
476                 PyErr_SetString(PyExc_RuntimeError,
477                                 "No PTHREAD support available");
478                 return -1;
479 #endif
480         } else {
481                 ret = py_cli_state_setup_ev(self);
482                 if (!ret) {
483                         return -1;
484                 }
485         }
486
487         if (creds == NULL) {
488                 cli_creds = cli_credentials_init_anon(NULL);
489         } else {
490                 cli_creds = PyCredentials_AsCliCredentials(creds);
491         }
492
493         req = cli_full_connection_creds_send(
494                 NULL, self->ev, "myname", host, NULL, 0, share, "?????",
495                 cli_creds, flags, signing_state);
496         if (!py_tevent_req_wait_exc(self, req)) {
497                 return -1;
498         }
499         status = cli_full_connection_creds_recv(req, &self->cli);
500         TALLOC_FREE(req);
501
502         if (!NT_STATUS_IS_OK(status)) {
503                 PyErr_SetNTSTATUS(status);
504                 return -1;
505         }
506
507         self->oplock_waiter = cli_smb_oplock_break_waiter_send(
508                 self->ev, self->ev, self->cli);
509         if (self->oplock_waiter == NULL) {
510                 PyErr_NoMemory();
511                 return -1;
512         }
513         tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
514                                 self);
515         return 0;
516 }
517
518 static void py_cli_got_oplock_break(struct tevent_req *req)
519 {
520         struct py_cli_state *self = (struct py_cli_state *)
521                 tevent_req_callback_data_void(req);
522         struct py_cli_oplock_break b;
523         struct py_cli_oplock_break *tmp;
524         size_t num_breaks;
525         NTSTATUS status;
526
527         status = cli_smb_oplock_break_waiter_recv(req, &b.fnum, &b.level);
528         TALLOC_FREE(req);
529         self->oplock_waiter = NULL;
530
531         if (!NT_STATUS_IS_OK(status)) {
532                 return;
533         }
534
535         num_breaks = talloc_array_length(self->oplock_breaks);
536         tmp = talloc_realloc(self->ev, self->oplock_breaks,
537                              struct py_cli_oplock_break, num_breaks+1);
538         if (tmp == NULL) {
539                 return;
540         }
541         self->oplock_breaks = tmp;
542         self->oplock_breaks[num_breaks] = b;
543
544         if (self->oplock_cond != NULL) {
545                 py_tevent_cond_signal(self->oplock_cond);
546         }
547
548         self->oplock_waiter = cli_smb_oplock_break_waiter_send(
549                 self->ev, self->ev, self->cli);
550         if (self->oplock_waiter == NULL) {
551                 return;
552         }
553         tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
554                                 self);
555 }
556
557 static PyObject *py_cli_get_oplock_break(struct py_cli_state *self,
558                                          PyObject *args)
559 {
560         size_t num_oplock_breaks;
561
562         if (!PyArg_ParseTuple(args, "")) {
563                 return NULL;
564         }
565
566         if (self->oplock_cond != NULL) {
567                 errno = EBUSY;
568                 PyErr_SetFromErrno(PyExc_RuntimeError);
569                 return NULL;
570         }
571
572         num_oplock_breaks = talloc_array_length(self->oplock_breaks);
573
574         if (num_oplock_breaks == 0) {
575                 struct py_tevent_cond cond;
576                 int ret;
577
578                 self->oplock_cond = &cond;
579                 ret = py_tevent_cond_wait(&cond);
580                 self->oplock_cond = NULL;
581
582                 if (ret != 0) {
583                         errno = ret;
584                         PyErr_SetFromErrno(PyExc_RuntimeError);
585                         return NULL;
586                 }
587         }
588
589         num_oplock_breaks = talloc_array_length(self->oplock_breaks);
590         if (num_oplock_breaks > 0) {
591                 PyObject *result;
592
593                 result = Py_BuildValue(
594                         "{s:i,s:i}",
595                         "fnum", self->oplock_breaks[0].fnum,
596                         "level", self->oplock_breaks[0].level);
597
598                 memmove(&self->oplock_breaks[0], &self->oplock_breaks[1],
599                         sizeof(self->oplock_breaks[0]) *
600                         (num_oplock_breaks - 1));
601                 self->oplock_breaks = talloc_realloc(
602                         NULL, self->oplock_breaks, struct py_cli_oplock_break,
603                         num_oplock_breaks - 1);
604
605                 return result;
606         }
607         Py_RETURN_NONE;
608 }
609
610 static void py_cli_state_dealloc(struct py_cli_state *self)
611 {
612         TALLOC_FREE(self->thread_state);
613         TALLOC_FREE(self->oplock_waiter);
614         TALLOC_FREE(self->ev);
615
616         if (self->cli != NULL) {
617                 cli_shutdown(self->cli);
618                 self->cli = NULL;
619         }
620         Py_TYPE(self)->tp_free((PyObject *)self);
621 }
622
623 static PyObject *py_cli_create(struct py_cli_state *self, PyObject *args,
624                                PyObject *kwds)
625 {
626         char *fname;
627         unsigned CreateFlags = 0;
628         unsigned DesiredAccess = FILE_GENERIC_READ;
629         unsigned FileAttributes = 0;
630         unsigned ShareAccess = 0;
631         unsigned CreateDisposition = FILE_OPEN;
632         unsigned CreateOptions = 0;
633         unsigned SecurityFlags = 0;
634         uint16_t fnum;
635         struct tevent_req *req;
636         NTSTATUS status;
637
638         static const char *kwlist[] = {
639                 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
640                 "ShareAccess", "CreateDisposition", "CreateOptions",
641                 "SecurityFlags", NULL };
642
643         if (!ParseTupleAndKeywords(
644                     args, kwds, "s|IIIIIII", kwlist,
645                     &fname, &CreateFlags, &DesiredAccess, &FileAttributes,
646                     &ShareAccess, &CreateDisposition, &CreateOptions,
647                     &SecurityFlags)) {
648                 return NULL;
649         }
650
651         req = cli_ntcreate_send(NULL, self->ev, self->cli, fname, CreateFlags,
652                                 DesiredAccess, FileAttributes, ShareAccess,
653                                 CreateDisposition, CreateOptions,
654                                 SecurityFlags);
655         if (!py_tevent_req_wait_exc(self, req)) {
656                 return NULL;
657         }
658         status = cli_ntcreate_recv(req, &fnum, NULL);
659         TALLOC_FREE(req);
660
661         if (!NT_STATUS_IS_OK(status)) {
662                 PyErr_SetNTSTATUS(status);
663                 return NULL;
664         }
665         return Py_BuildValue("I", (unsigned)fnum);
666 }
667
668 static PyObject *py_cli_close(struct py_cli_state *self, PyObject *args)
669 {
670         struct tevent_req *req;
671         int fnum;
672         NTSTATUS status;
673
674         if (!PyArg_ParseTuple(args, "i", &fnum)) {
675                 return NULL;
676         }
677
678         req = cli_close_send(NULL, self->ev, self->cli, fnum);
679         if (!py_tevent_req_wait_exc(self, req)) {
680                 return NULL;
681         }
682         status = cli_close_recv(req);
683         TALLOC_FREE(req);
684
685         if (!NT_STATUS_IS_OK(status)) {
686                 PyErr_SetNTSTATUS(status);
687                 return NULL;
688         }
689         Py_RETURN_NONE;
690 }
691
692 static PyObject *py_cli_write(struct py_cli_state *self, PyObject *args,
693                               PyObject *kwds)
694 {
695         int fnum;
696         unsigned mode = 0;
697         char *buf;
698         Py_ssize_t buflen;
699         unsigned long long offset;
700         struct tevent_req *req;
701         NTSTATUS status;
702         size_t written;
703
704         static const char *kwlist[] = {
705                 "fnum", "buffer", "offset", "mode", NULL };
706
707         if (!ParseTupleAndKeywords(
708                     args, kwds, "Is#K|I", kwlist,
709                     &fnum, &buf, &buflen, &offset, &mode)) {
710                 return NULL;
711         }
712
713         req = cli_write_andx_send(NULL, self->ev, self->cli, fnum, mode,
714                                   (uint8_t *)buf, offset, buflen);
715         if (!py_tevent_req_wait_exc(self, req)) {
716                 return NULL;
717         }
718         status = cli_write_andx_recv(req, &written);
719         TALLOC_FREE(req);
720
721         if (!NT_STATUS_IS_OK(status)) {
722                 PyErr_SetNTSTATUS(status);
723                 return NULL;
724         }
725         return Py_BuildValue("K", (unsigned long long)written);
726 }
727
728 static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
729                              PyObject *kwds)
730 {
731         int fnum;
732         unsigned long long offset;
733         unsigned size;
734         struct tevent_req *req;
735         NTSTATUS status;
736         uint8_t *buf;
737         ssize_t buflen;
738         PyObject *result;
739
740         static const char *kwlist[] = {
741                 "fnum", "offset", "size", NULL };
742
743         if (!ParseTupleAndKeywords(
744                     args, kwds, "IKI", kwlist, &fnum, &offset,
745                     &size)) {
746                 return NULL;
747         }
748
749         req = cli_read_andx_send(NULL, self->ev, self->cli, fnum,
750                                  offset, size);
751         if (!py_tevent_req_wait_exc(self, req)) {
752                 return NULL;
753         }
754         status = cli_read_andx_recv(req, &buflen, &buf);
755
756         if (!NT_STATUS_IS_OK(status)) {
757                 TALLOC_FREE(req);
758                 PyErr_SetNTSTATUS(status);
759                 return NULL;
760         }
761         result = Py_BuildValue("s#", (char *)buf, (int)buflen);
762         TALLOC_FREE(req);
763         return result;
764 }
765
766 static PyObject *py_cli_ftruncate(struct py_cli_state *self, PyObject *args,
767                                   PyObject *kwds)
768 {
769         int fnum;
770         unsigned long long size;
771         struct tevent_req *req;
772         NTSTATUS status;
773
774         static const char *kwlist[] = {
775                 "fnum", "size", NULL };
776
777         if (!ParseTupleAndKeywords(
778                     args, kwds, "IK", kwlist, &fnum, &size)) {
779                 return NULL;
780         }
781
782         req = cli_ftruncate_send(NULL, self->ev, self->cli, fnum, size);
783         if (!py_tevent_req_wait_exc(self, req)) {
784                 return NULL;
785         }
786         status = cli_ftruncate_recv(req);
787         TALLOC_FREE(req);
788
789         if (!NT_STATUS_IS_OK(status)) {
790                 PyErr_SetNTSTATUS(status);
791                 return NULL;
792         }
793         Py_RETURN_NONE;
794 }
795
796 static PyObject *py_cli_delete_on_close(struct py_cli_state *self,
797                                         PyObject *args,
798                                         PyObject *kwds)
799 {
800         unsigned fnum, flag;
801         struct tevent_req *req;
802         NTSTATUS status;
803
804         static const char *kwlist[] = {
805                 "fnum", "flag", NULL };
806
807         if (!ParseTupleAndKeywords(
808                     args, kwds, "II", kwlist, &fnum, &flag)) {
809                 return NULL;
810         }
811
812         req = cli_nt_delete_on_close_send(NULL, self->ev, self->cli, fnum,
813                                           flag);
814         if (!py_tevent_req_wait_exc(self, req)) {
815                 return NULL;
816         }
817         status = cli_nt_delete_on_close_recv(req);
818         TALLOC_FREE(req);
819
820         if (!NT_STATUS_IS_OK(status)) {
821                 PyErr_SetNTSTATUS(status);
822                 return NULL;
823         }
824         Py_RETURN_NONE;
825 }
826
827 static PyObject *py_cli_list(struct py_cli_state *self,
828                              PyObject *args,
829                              PyObject *kwds)
830 {
831         char *mask;
832         unsigned attribute =
833                 FILE_ATTRIBUTE_DIRECTORY |
834                 FILE_ATTRIBUTE_SYSTEM |
835                 FILE_ATTRIBUTE_HIDDEN;
836         unsigned info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
837         struct tevent_req *req;
838         NTSTATUS status;
839         struct file_info *finfos;
840         size_t i, num_finfos;
841         PyObject *result;
842
843         const char *kwlist[] = {
844                 "mask", "attribute", "info_level", NULL
845         };
846
847         if (!ParseTupleAndKeywords(
848                     args, kwds, "s|II", kwlist,
849                     &mask, &attribute, &info_level)) {
850                 return NULL;
851         }
852
853         req = cli_list_send(NULL, self->ev, self->cli, mask, attribute,
854                             info_level);
855         if (!py_tevent_req_wait_exc(self, req)) {
856                 return NULL;
857         }
858         status = cli_list_recv(req, NULL, &finfos, &num_finfos);
859         TALLOC_FREE(req);
860
861         if (!NT_STATUS_IS_OK(status)) {
862                 PyErr_SetNTSTATUS(status);
863                 return NULL;
864         }
865
866         result = Py_BuildValue("[]");
867         if (result == NULL) {
868                 return NULL;
869         }
870
871         for (i=0; i<num_finfos; i++) {
872                 struct file_info *finfo = &finfos[i];
873                 PyObject *file;
874                 int ret;
875
876                 file = Py_BuildValue(
877                         "{s:s,s:i}",
878                         "name", finfo->name,
879                         "mode", (int)finfo->mode);
880                 if (file == NULL) {
881                         Py_XDECREF(result);
882                         return NULL;
883                 }
884
885                 ret = PyList_Append(result, file);
886                 if (ret == -1) {
887                         Py_XDECREF(result);
888                         return NULL;
889                 }
890         }
891
892         return result;
893 }
894
895 static PyMethodDef py_cli_state_methods[] = {
896         { "create", (PyCFunction)py_cli_create, METH_VARARGS|METH_KEYWORDS,
897           "Open a file" },
898         { "close", (PyCFunction)py_cli_close, METH_VARARGS,
899           "Close a file handle" },
900         { "write", (PyCFunction)py_cli_write, METH_VARARGS|METH_KEYWORDS,
901           "Write to a file handle" },
902         { "read", (PyCFunction)py_cli_read, METH_VARARGS|METH_KEYWORDS,
903           "Read from a file handle" },
904         { "truncate", (PyCFunction)py_cli_ftruncate,
905           METH_VARARGS|METH_KEYWORDS,
906           "Truncate a file" },
907         { "delete_on_close", (PyCFunction)py_cli_delete_on_close,
908           METH_VARARGS|METH_KEYWORDS,
909           "Set/Reset the delete on close flag" },
910         { "readdir", (PyCFunction)py_cli_list,
911           METH_VARARGS|METH_KEYWORDS,
912           "List a directory" },
913         { "get_oplock_break", (PyCFunction)py_cli_get_oplock_break,
914           METH_VARARGS, "Wait for an oplock break" },
915         { NULL, NULL, 0, NULL }
916 };
917
918 static PyTypeObject py_cli_state_type = {
919         PyVarObject_HEAD_INIT(NULL, 0)
920         .tp_name = "libsmb_samba_internal.Conn",
921         .tp_basicsize = sizeof(struct py_cli_state),
922         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
923         .tp_doc = "libsmb connection",
924         .tp_new = py_cli_state_new,
925         .tp_init = (initproc)py_cli_state_init,
926         .tp_dealloc = (destructor)py_cli_state_dealloc,
927         .tp_methods = py_cli_state_methods,
928 };
929
930 static PyMethodDef py_libsmb_methods[] = {
931         { NULL },
932 };
933
934 void initlibsmb_samba_internal(void);
935
936 static struct PyModuleDef moduledef = {
937     PyModuleDef_HEAD_INIT,
938     .m_name = "libsmb_samba_internal",
939     .m_doc = "libsmb wrapper",
940     .m_size = -1,
941     .m_methods = py_libsmb_methods,
942 };
943
944 MODULE_INIT_FUNC(libsmb_samba_internal)
945 {
946         PyObject *m = NULL;
947
948         talloc_stackframe();
949
950         m = PyModule_Create(&moduledef);
951         if (m == NULL) {
952                 return m;
953         }
954         if (PyType_Ready(&py_cli_state_type) < 0) {
955                 return NULL;
956         }
957         Py_INCREF(&py_cli_state_type);
958         PyModule_AddObject(m, "Conn", (PyObject *)&py_cli_state_type);
959         return m;
960 }