2a51a2a158c434dc25ecaaa7d0f2d637546c8ff3
[vlendec/samba-autobuild/.git] / source3 / libsmb / pylibsmb.c
1 /*
2  * Unix SMB/CIFS implementation.
3  *
4  * SMB client Python bindings used internally by Samba (for things like
5  * samba-tool). These Python bindings may change without warning, and so
6  * should not be used outside of the Samba codebase.
7  *
8  * Copyright (C) Volker Lendecke 2012
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25 Template code to use this library:
26
27 -------------------------
28 from samba.samba3 import libsmb_samba_internal as libsmb
29 from samba.samba3 import param as s3param
30 from samba import (credentials,NTSTATUSError)
31
32 lp = s3param.get_context()
33 lp.load("/etc/samba/smb.conf");
34
35 creds = credentials.Credentials()
36 creds.guess(lp)
37 creds.set_username("administrator")
38 creds.set_password("1234")
39
40 c = libsmb.Conn("127.0.0.1",
41                 "tmp",
42                 lp,
43                 creds,
44                 multi_threaded=True)
45 -------------------------
46 */
47
48 #include <Python.h>
49 #include "includes.h"
50 #include "python/py3compat.h"
51 #include "python/modules.h"
52 #include "libcli/smb/smbXcli_base.h"
53 #include "libcli/smb/smb2_negotiate_context.h"
54 #include "libcli/smb/reparse.h"
55 #include "libsmb/libsmb.h"
56 #include "libcli/security/security.h"
57 #include "system/select.h"
58 #include "source4/libcli/util/pyerrors.h"
59 #include "auth/credentials/pycredentials.h"
60 #include "trans2.h"
61 #include "libsmb/clirap.h"
62 #include "librpc/rpc/pyrpc_util.h"
63
64 #define LIST_ATTRIBUTE_MASK \
65         (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN)
66
67 static PyTypeObject *dom_sid_Type = NULL;
68
69 static PyTypeObject *get_pytype(const char *module, const char *type)
70 {
71         PyObject *mod;
72         PyTypeObject *result;
73
74         mod = PyImport_ImportModule(module);
75         if (mod == NULL) {
76                 PyErr_Format(PyExc_RuntimeError,
77                              "Unable to import %s to check type %s",
78                              module, type);
79                 return NULL;
80         }
81         result = (PyTypeObject *)PyObject_GetAttrString(mod, type);
82         Py_DECREF(mod);
83         if (result == NULL) {
84                 PyErr_Format(PyExc_RuntimeError,
85                              "Unable to find type %s in module %s",
86                              module, type);
87                 return NULL;
88         }
89         return result;
90 }
91
92 /*
93  * We're using "const char * const *" for keywords,
94  * PyArg_ParseTupleAndKeywords expects a "char **". Confine the
95  * inevitable warnings to just one place.
96  */
97 static int ParseTupleAndKeywords(PyObject *args, PyObject *kw,
98                                  const char *format, const char * const *keywords,
99                                  ...)
100 {
101         char **_keywords = discard_const_p(char *, keywords);
102         va_list a;
103         int ret;
104         va_start(a, keywords);
105         ret = PyArg_VaParseTupleAndKeywords(args, kw, format,
106                                             _keywords, a);
107         va_end(a);
108         return ret;
109 }
110
111 struct py_cli_thread;
112
113 struct py_cli_oplock_break {
114         uint16_t fnum;
115         uint8_t level;
116 };
117
118 struct py_cli_state {
119         PyObject_HEAD
120         struct cli_state *cli;
121         struct tevent_context *ev;
122         int (*req_wait_fn)(struct tevent_context *ev,
123                            struct tevent_req *req);
124         struct py_cli_thread *thread_state;
125
126         struct tevent_req *oplock_waiter;
127         struct py_cli_oplock_break *oplock_breaks;
128         struct py_tevent_cond *oplock_cond;
129 };
130
131 #ifdef HAVE_PTHREAD
132
133 #include <pthread.h>
134
135 struct py_cli_thread {
136
137         /*
138          * Pipe to make the poll thread wake up in our destructor, so
139          * that we can exit and join the thread.
140          */
141         int shutdown_pipe[2];
142         struct tevent_fd *shutdown_fde;
143         bool do_shutdown;
144         pthread_t id;
145
146         /*
147          * Thread state to release the GIL during the poll(2) syscall
148          */
149         PyThreadState *py_threadstate;
150 };
151
152 static void *py_cli_state_poll_thread(void *private_data)
153 {
154         struct py_cli_state *self = (struct py_cli_state *)private_data;
155         struct py_cli_thread *t = self->thread_state;
156         PyGILState_STATE gstate;
157
158         gstate = PyGILState_Ensure();
159
160         while (!t->do_shutdown) {
161                 int ret;
162                 ret = tevent_loop_once(self->ev);
163                 assert(ret == 0);
164         }
165         PyGILState_Release(gstate);
166         return NULL;
167 }
168
169 static void py_cli_state_trace_callback(enum tevent_trace_point point,
170                                         void *private_data)
171 {
172         struct py_cli_state *self = (struct py_cli_state *)private_data;
173         struct py_cli_thread *t = self->thread_state;
174
175         switch(point) {
176         case TEVENT_TRACE_BEFORE_WAIT:
177                 assert(t->py_threadstate == NULL);
178                 t->py_threadstate = PyEval_SaveThread();
179                 break;
180         case TEVENT_TRACE_AFTER_WAIT:
181                 assert(t->py_threadstate != NULL);
182                 PyEval_RestoreThread(t->py_threadstate);
183                 t->py_threadstate = NULL;
184                 break;
185         default:
186                 break;
187         }
188 }
189
190 static void py_cli_state_shutdown_handler(struct tevent_context *ev,
191                                           struct tevent_fd *fde,
192                                           uint16_t flags,
193                                           void *private_data)
194 {
195         struct py_cli_state *self = (struct py_cli_state *)private_data;
196         struct py_cli_thread *t = self->thread_state;
197
198         if ((flags & TEVENT_FD_READ) == 0) {
199                 return;
200         }
201         TALLOC_FREE(t->shutdown_fde);
202         t->do_shutdown = true;
203 }
204
205 static int py_cli_thread_destructor(struct py_cli_thread *t)
206 {
207         char c = 0;
208         ssize_t written;
209         int ret;
210
211         do {
212                 /*
213                  * This will wake the poll thread from the poll(2)
214                  */
215                 written = write(t->shutdown_pipe[1], &c, 1);
216         } while ((written == -1) && (errno == EINTR));
217
218         /*
219          * Allow the poll thread to do its own cleanup under the GIL
220          */
221         Py_BEGIN_ALLOW_THREADS
222         ret = pthread_join(t->id, NULL);
223         Py_END_ALLOW_THREADS
224         assert(ret == 0);
225
226         if (t->shutdown_pipe[0] != -1) {
227                 close(t->shutdown_pipe[0]);
228                 t->shutdown_pipe[0] = -1;
229         }
230         if (t->shutdown_pipe[1] != -1) {
231                 close(t->shutdown_pipe[1]);
232                 t->shutdown_pipe[1] = -1;
233         }
234         return 0;
235 }
236
237 static int py_tevent_cond_req_wait(struct tevent_context *ev,
238                                    struct tevent_req *req);
239
240 static bool py_cli_state_setup_mt_ev(struct py_cli_state *self)
241 {
242         struct py_cli_thread *t = NULL;
243         int ret;
244
245         self->ev = tevent_context_init_byname(NULL, "poll_mt");
246         if (self->ev == NULL) {
247                 goto fail;
248         }
249         samba_tevent_set_debug(self->ev, "pylibsmb_tevent_mt");
250         tevent_set_trace_callback(self->ev, py_cli_state_trace_callback, self);
251
252         self->req_wait_fn = py_tevent_cond_req_wait;
253
254         self->thread_state = talloc_zero(NULL, struct py_cli_thread);
255         if (self->thread_state == NULL) {
256                 goto fail;
257         }
258         t = self->thread_state;
259
260         ret = pipe(t->shutdown_pipe);
261         if (ret == -1) {
262                 goto fail;
263         }
264         t->shutdown_fde = tevent_add_fd(
265                 self->ev, self->ev, t->shutdown_pipe[0], TEVENT_FD_READ,
266                 py_cli_state_shutdown_handler, self);
267         if (t->shutdown_fde == NULL) {
268                 goto fail;
269         }
270
271         PyEval_InitThreads();
272
273         ret = pthread_create(&t->id, NULL, py_cli_state_poll_thread, self);
274         if (ret != 0) {
275                 goto fail;
276         }
277         talloc_set_destructor(self->thread_state, py_cli_thread_destructor);
278         return true;
279
280 fail:
281         if (t != NULL) {
282                 TALLOC_FREE(t->shutdown_fde);
283
284                 if (t->shutdown_pipe[0] != -1) {
285                         close(t->shutdown_pipe[0]);
286                         t->shutdown_pipe[0] = -1;
287                 }
288                 if (t->shutdown_pipe[1] != -1) {
289                         close(t->shutdown_pipe[1]);
290                         t->shutdown_pipe[1] = -1;
291                 }
292         }
293
294         TALLOC_FREE(self->thread_state);
295         TALLOC_FREE(self->ev);
296         return false;
297 }
298
299 struct py_tevent_cond {
300         pthread_mutex_t mutex;
301         pthread_cond_t cond;
302         bool is_done;
303 };
304
305 static void py_tevent_signalme(struct tevent_req *req);
306
307 static int py_tevent_cond_wait(struct py_tevent_cond *cond)
308 {
309         int ret, result;
310
311         result = pthread_mutex_init(&cond->mutex, NULL);
312         if (result != 0) {
313                 goto fail;
314         }
315         result = pthread_cond_init(&cond->cond, NULL);
316         if (result != 0) {
317                 goto fail_mutex;
318         }
319
320         result = pthread_mutex_lock(&cond->mutex);
321         if (result != 0) {
322                 goto fail_cond;
323         }
324
325         cond->is_done = false;
326
327         while (!cond->is_done) {
328
329                 Py_BEGIN_ALLOW_THREADS
330                 result = pthread_cond_wait(&cond->cond, &cond->mutex);
331                 Py_END_ALLOW_THREADS
332
333                 if (result != 0) {
334                         goto fail_unlock;
335                 }
336         }
337
338 fail_unlock:
339         ret = pthread_mutex_unlock(&cond->mutex);
340         assert(ret == 0);
341 fail_cond:
342         ret = pthread_cond_destroy(&cond->cond);
343         assert(ret == 0);
344 fail_mutex:
345         ret = pthread_mutex_destroy(&cond->mutex);
346         assert(ret == 0);
347 fail:
348         return result;
349 }
350
351 static int py_tevent_cond_req_wait(struct tevent_context *ev,
352                                    struct tevent_req *req)
353 {
354         struct py_tevent_cond cond;
355         tevent_req_set_callback(req, py_tevent_signalme, &cond);
356         return py_tevent_cond_wait(&cond);
357 }
358
359 static void py_tevent_cond_signal(struct py_tevent_cond *cond)
360 {
361         int ret;
362
363         ret = pthread_mutex_lock(&cond->mutex);
364         assert(ret == 0);
365
366         cond->is_done = true;
367
368         ret = pthread_cond_signal(&cond->cond);
369         assert(ret == 0);
370         ret = pthread_mutex_unlock(&cond->mutex);
371         assert(ret == 0);
372 }
373
374 static void py_tevent_signalme(struct tevent_req *req)
375 {
376         struct py_tevent_cond *cond = (struct py_tevent_cond *)
377                 tevent_req_callback_data_void(req);
378
379         py_tevent_cond_signal(cond);
380 }
381
382 #endif
383
384 static int py_tevent_req_wait(struct tevent_context *ev,
385                               struct tevent_req *req);
386
387 static bool py_cli_state_setup_ev(struct py_cli_state *self)
388 {
389         self->ev = tevent_context_init(NULL);
390         if (self->ev == NULL) {
391                 return false;
392         }
393
394         samba_tevent_set_debug(self->ev, "pylibsmb_tevent");
395
396         self->req_wait_fn = py_tevent_req_wait;
397
398         return true;
399 }
400
401 static int py_tevent_req_wait(struct tevent_context *ev,
402                               struct tevent_req *req)
403 {
404         while (tevent_req_is_in_progress(req)) {
405                 int ret;
406
407                 ret = tevent_loop_once(ev);
408                 if (ret != 0) {
409                         return ret;
410                 }
411         }
412         return 0;
413 }
414
415 static bool py_tevent_req_wait_exc(struct py_cli_state *self,
416                                    struct tevent_req *req)
417 {
418         int ret;
419
420         if (req == NULL) {
421                 PyErr_NoMemory();
422                 return false;
423         }
424         ret = self->req_wait_fn(self->ev, req);
425         if (ret != 0) {
426                 TALLOC_FREE(req);
427                 errno = ret;
428                 PyErr_SetFromErrno(PyExc_RuntimeError);
429                 return false;
430         }
431         return true;
432 }
433
434 static PyObject *py_cli_state_new(PyTypeObject *type, PyObject *args,
435                                   PyObject *kwds)
436 {
437         struct py_cli_state *self;
438
439         self = (struct py_cli_state *)type->tp_alloc(type, 0);
440         if (self == NULL) {
441                 return NULL;
442         }
443         self->cli = NULL;
444         self->ev = NULL;
445         self->thread_state = NULL;
446         self->oplock_waiter = NULL;
447         self->oplock_cond = NULL;
448         self->oplock_breaks = NULL;
449         return (PyObject *)self;
450 }
451
452 static struct smb2_negotiate_contexts *py_cli_get_negotiate_contexts(
453         TALLOC_CTX *mem_ctx, PyObject *list)
454 {
455         struct smb2_negotiate_contexts *ctxs = NULL;
456         Py_ssize_t i, len;
457         int ret;
458
459         ret = PyList_Check(list);
460         if (!ret) {
461                 goto fail;
462         }
463
464         len = PyList_Size(list);
465         if (len == 0) {
466                 goto fail;
467         }
468
469         ctxs = talloc_zero(mem_ctx, struct smb2_negotiate_contexts);
470         if (ctxs == NULL) {
471                 goto fail;
472         }
473
474         for (i=0; i<len; i++) {
475                 NTSTATUS status;
476
477                 PyObject *t = PyList_GetItem(list, i);
478                 Py_ssize_t tlen;
479
480                 PyObject *ptype = NULL;
481                 long type;
482
483                 PyObject *pdata = NULL;
484                 DATA_BLOB data = { .data = NULL, };
485
486                 if (t == NULL) {
487                         goto fail;
488                 }
489
490                 ret = PyTuple_Check(t);
491                 if (!ret) {
492                         goto fail;
493                 }
494
495                 tlen = PyTuple_Size(t);
496                 if (tlen != 2) {
497                         goto fail;
498                 }
499
500                 ptype = PyTuple_GetItem(t, 0);
501                 if (ptype == NULL) {
502                         goto fail;
503                 }
504                 type = PyLong_AsLong(ptype);
505                 if ((type < 0) || (type > UINT16_MAX)) {
506                         goto fail;
507                 }
508
509                 pdata = PyTuple_GetItem(t, 1);
510
511                 ret = PyBytes_Check(pdata);
512                 if (!ret) {
513                         goto fail;
514                 }
515
516                 data.data = (uint8_t *)PyBytes_AsString(pdata);
517                 data.length = PyBytes_Size(pdata);
518
519                 status = smb2_negotiate_context_add(
520                         ctxs, ctxs, type, data.data, data.length);
521                 if (!NT_STATUS_IS_OK(status)) {
522                         goto fail;
523                 }
524         }
525         return ctxs;
526
527 fail:
528         TALLOC_FREE(ctxs);
529         return NULL;
530 }
531
532 static void py_cli_got_oplock_break(struct tevent_req *req);
533
534 static int py_cli_state_init(struct py_cli_state *self, PyObject *args,
535                              PyObject *kwds)
536 {
537         NTSTATUS status;
538         char *host, *share;
539         PyObject *creds = NULL;
540         struct cli_credentials *cli_creds;
541         PyObject *py_lp = Py_None;
542         PyObject *py_multi_threaded = Py_False;
543         bool multi_threaded = false;
544         PyObject *py_force_smb1 = Py_False;
545         bool force_smb1 = false;
546         PyObject *py_ipc = Py_False;
547         PyObject *py_posix = Py_False;
548         PyObject *py_negotiate_contexts = NULL;
549         struct smb2_negotiate_contexts *negotiate_contexts = NULL;
550         bool use_ipc = false;
551         bool request_posix = false;
552         struct tevent_req *req;
553         bool ret;
554         int flags = 0;
555
556         static const char *kwlist[] = {
557                 "host", "share", "lp", "creds",
558                 "multi_threaded", "force_smb1",
559                 "ipc",
560                 "posix",
561                 "negotiate_contexts",
562                 NULL
563         };
564
565         PyTypeObject *py_type_Credentials = get_pytype(
566                 "samba.credentials", "Credentials");
567         if (py_type_Credentials == NULL) {
568                 return -1;
569         }
570
571         ret = ParseTupleAndKeywords(
572                 args, kwds, "ssO|O!OOOOO", kwlist,
573                 &host, &share, &py_lp,
574                 py_type_Credentials, &creds,
575                 &py_multi_threaded,
576                 &py_force_smb1,
577                 &py_ipc,
578                 &py_posix,
579                 &py_negotiate_contexts);
580
581         Py_DECREF(py_type_Credentials);
582
583         if (!ret) {
584                 return -1;
585         }
586
587         multi_threaded = PyObject_IsTrue(py_multi_threaded);
588         force_smb1 = PyObject_IsTrue(py_force_smb1);
589
590         if (force_smb1) {
591                 /*
592                  * As most of the cli_*_send() function
593                  * don't support SMB2 (it's only plugged
594                  * into the sync wrapper functions currently)
595                  * we have a way to force SMB1.
596                  */
597                 flags = CLI_FULL_CONNECTION_FORCE_SMB1;
598         }
599
600         use_ipc = PyObject_IsTrue(py_ipc);
601         if (use_ipc) {
602                 flags |= CLI_FULL_CONNECTION_IPC;
603         }
604
605         request_posix = PyObject_IsTrue(py_posix);
606         if (request_posix) {
607                 flags |= CLI_FULL_CONNECTION_REQUEST_POSIX;
608         }
609
610         if (py_negotiate_contexts != NULL) {
611                 negotiate_contexts = py_cli_get_negotiate_contexts(
612                         talloc_tos(), py_negotiate_contexts);
613                 if (negotiate_contexts == NULL) {
614                         return -1;
615                 }
616         }
617
618         if (multi_threaded) {
619 #ifdef HAVE_PTHREAD
620                 ret = py_cli_state_setup_mt_ev(self);
621                 if (!ret) {
622                         return -1;
623                 }
624 #else
625                 PyErr_SetString(PyExc_RuntimeError,
626                                 "No PTHREAD support available");
627                 return -1;
628 #endif
629         } else {
630                 ret = py_cli_state_setup_ev(self);
631                 if (!ret) {
632                         return -1;
633                 }
634         }
635
636         if (creds == NULL) {
637                 cli_creds = cli_credentials_init_anon(NULL);
638         } else {
639                 cli_creds = PyCredentials_AsCliCredentials(creds);
640         }
641
642         req = cli_full_connection_creds_send(
643                 NULL, self->ev, "myname", host, NULL, 0, share, "?????",
644                 cli_creds, flags,
645                 negotiate_contexts);
646         if (!py_tevent_req_wait_exc(self, req)) {
647                 return -1;
648         }
649         status = cli_full_connection_creds_recv(req, &self->cli);
650         TALLOC_FREE(req);
651
652         if (!NT_STATUS_IS_OK(status)) {
653                 PyErr_SetNTSTATUS(status);
654                 return -1;
655         }
656
657         /*
658          * Oplocks require a multi threaded connection
659          */
660         if (self->thread_state == NULL) {
661                 return 0;
662         }
663
664         self->oplock_waiter = cli_smb_oplock_break_waiter_send(
665                 self->ev, self->ev, self->cli);
666         if (self->oplock_waiter == NULL) {
667                 PyErr_NoMemory();
668                 return -1;
669         }
670         tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
671                                 self);
672         return 0;
673 }
674
675 static void py_cli_got_oplock_break(struct tevent_req *req)
676 {
677         struct py_cli_state *self = (struct py_cli_state *)
678                 tevent_req_callback_data_void(req);
679         struct py_cli_oplock_break b;
680         struct py_cli_oplock_break *tmp;
681         size_t num_breaks;
682         NTSTATUS status;
683
684         status = cli_smb_oplock_break_waiter_recv(req, &b.fnum, &b.level);
685         TALLOC_FREE(req);
686         self->oplock_waiter = NULL;
687
688         if (!NT_STATUS_IS_OK(status)) {
689                 return;
690         }
691
692         num_breaks = talloc_array_length(self->oplock_breaks);
693         tmp = talloc_realloc(self->ev, self->oplock_breaks,
694                              struct py_cli_oplock_break, num_breaks+1);
695         if (tmp == NULL) {
696                 return;
697         }
698         self->oplock_breaks = tmp;
699         self->oplock_breaks[num_breaks] = b;
700
701         if (self->oplock_cond != NULL) {
702                 py_tevent_cond_signal(self->oplock_cond);
703         }
704
705         self->oplock_waiter = cli_smb_oplock_break_waiter_send(
706                 self->ev, self->ev, self->cli);
707         if (self->oplock_waiter == NULL) {
708                 return;
709         }
710         tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
711                                 self);
712 }
713
714 static PyObject *py_cli_get_oplock_break(struct py_cli_state *self,
715                                          PyObject *args)
716 {
717         size_t num_oplock_breaks;
718
719         if (!PyArg_ParseTuple(args, "")) {
720                 return NULL;
721         }
722
723         if (self->thread_state == NULL) {
724                 PyErr_SetString(PyExc_RuntimeError,
725                                 "get_oplock_break() only possible on "
726                                 "a multi_threaded connection");
727                 return NULL;
728         }
729
730         if (self->oplock_cond != NULL) {
731                 errno = EBUSY;
732                 PyErr_SetFromErrno(PyExc_RuntimeError);
733                 return NULL;
734         }
735
736         num_oplock_breaks = talloc_array_length(self->oplock_breaks);
737
738         if (num_oplock_breaks == 0) {
739                 struct py_tevent_cond cond;
740                 int ret;
741
742                 self->oplock_cond = &cond;
743                 ret = py_tevent_cond_wait(&cond);
744                 self->oplock_cond = NULL;
745
746                 if (ret != 0) {
747                         errno = ret;
748                         PyErr_SetFromErrno(PyExc_RuntimeError);
749                         return NULL;
750                 }
751         }
752
753         num_oplock_breaks = talloc_array_length(self->oplock_breaks);
754         if (num_oplock_breaks > 0) {
755                 PyObject *result;
756
757                 result = Py_BuildValue(
758                         "{s:i,s:i}",
759                         "fnum", self->oplock_breaks[0].fnum,
760                         "level", self->oplock_breaks[0].level);
761
762                 memmove(&self->oplock_breaks[0], &self->oplock_breaks[1],
763                         sizeof(self->oplock_breaks[0]) *
764                         (num_oplock_breaks - 1));
765                 self->oplock_breaks = talloc_realloc(
766                         NULL, self->oplock_breaks, struct py_cli_oplock_break,
767                         num_oplock_breaks - 1);
768
769                 return result;
770         }
771         Py_RETURN_NONE;
772 }
773
774 static void py_cli_state_dealloc(struct py_cli_state *self)
775 {
776         TALLOC_FREE(self->thread_state);
777         TALLOC_FREE(self->oplock_waiter);
778         TALLOC_FREE(self->ev);
779
780         if (self->cli != NULL) {
781                 cli_shutdown(self->cli);
782                 self->cli = NULL;
783         }
784         Py_TYPE(self)->tp_free((PyObject *)self);
785 }
786
787 static PyObject *py_cli_settimeout(struct py_cli_state *self, PyObject *args)
788 {
789         unsigned int nmsecs = 0;
790         unsigned int omsecs = 0;
791
792         if (!PyArg_ParseTuple(args, "I", &nmsecs)) {
793                 return NULL;
794         }
795
796         omsecs = cli_set_timeout(self->cli, nmsecs);
797
798         return PyLong_FromLong(omsecs);
799 }
800
801 static PyObject *py_cli_echo(struct py_cli_state *self,
802                              PyObject *Py_UNUSED(ignored))
803 {
804         DATA_BLOB data = data_blob_string_const("keepalive");
805         struct tevent_req *req = NULL;
806         NTSTATUS status;
807
808         req = cli_echo_send(NULL, self->ev, self->cli, 1, data);
809         if (!py_tevent_req_wait_exc(self, req)) {
810                 return NULL;
811         }
812         status = cli_echo_recv(req);
813         TALLOC_FREE(req);
814         PyErr_NTSTATUS_NOT_OK_RAISE(status);
815
816         Py_RETURN_NONE;
817 }
818
819 static PyObject *py_cli_create(struct py_cli_state *self, PyObject *args,
820                                PyObject *kwds)
821 {
822         char *fname;
823         unsigned CreateFlags = 0;
824         unsigned DesiredAccess = FILE_GENERIC_READ;
825         unsigned FileAttributes = 0;
826         unsigned ShareAccess = 0;
827         unsigned CreateDisposition = FILE_OPEN;
828         unsigned CreateOptions = 0;
829         unsigned ImpersonationLevel = SMB2_IMPERSONATION_IMPERSONATION;
830         unsigned SecurityFlags = 0;
831         uint16_t fnum;
832         struct tevent_req *req;
833         NTSTATUS status;
834
835         static const char *kwlist[] = {
836                 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
837                 "ShareAccess", "CreateDisposition", "CreateOptions",
838                 "ImpersonationLevel", "SecurityFlags", NULL };
839
840         if (!ParseTupleAndKeywords(
841                     args, kwds, "s|IIIIIIII", kwlist,
842                     &fname, &CreateFlags, &DesiredAccess, &FileAttributes,
843                     &ShareAccess, &CreateDisposition, &CreateOptions,
844                     &ImpersonationLevel, &SecurityFlags)) {
845                 return NULL;
846         }
847
848         req = cli_ntcreate_send(NULL, self->ev, self->cli, fname, CreateFlags,
849                                 DesiredAccess, FileAttributes, ShareAccess,
850                                 CreateDisposition, CreateOptions,
851                                 ImpersonationLevel, SecurityFlags);
852         if (!py_tevent_req_wait_exc(self, req)) {
853                 return NULL;
854         }
855         status = cli_ntcreate_recv(req, &fnum, NULL);
856         TALLOC_FREE(req);
857
858         if (!NT_STATUS_IS_OK(status)) {
859                 PyErr_SetNTSTATUS(status);
860                 return NULL;
861         }
862         return Py_BuildValue("I", (unsigned)fnum);
863 }
864
865 static struct smb2_create_blobs *py_cli_get_create_contexts(
866         TALLOC_CTX *mem_ctx, PyObject *list)
867 {
868         struct smb2_create_blobs *ctxs = NULL;
869         Py_ssize_t i, len;
870         int ret;
871
872         ret = PyList_Check(list);
873         if (!ret) {
874                 goto fail;
875         }
876
877         len = PyList_Size(list);
878         if (len == 0) {
879                 goto fail;
880         }
881
882         ctxs = talloc_zero(mem_ctx, struct smb2_create_blobs);
883         if (ctxs == NULL) {
884                 goto fail;
885         }
886
887         for (i=0; i<len; i++) {
888                 NTSTATUS status;
889
890                 PyObject *t = NULL;
891                 Py_ssize_t tlen;
892
893                 PyObject *pname = NULL;
894                 char *name = NULL;
895
896                 PyObject *pdata = NULL;
897                 DATA_BLOB data = { .data = NULL, };
898
899                 t = PyList_GetItem(list, i);
900                 if (t == NULL) {
901                         goto fail;
902                 }
903
904                 ret = PyTuple_Check(t);
905                 if (!ret) {
906                         goto fail;
907                 }
908
909                 tlen = PyTuple_Size(t);
910                 if (tlen != 2) {
911                         goto fail;
912                 }
913
914                 pname = PyTuple_GetItem(t, 0);
915                 if (pname == NULL) {
916                         goto fail;
917                 }
918                 ret = PyBytes_Check(pname);
919                 if (!ret) {
920                         goto fail;
921                 }
922                 name = PyBytes_AsString(pname);
923
924                 pdata = PyTuple_GetItem(t, 1);
925                 if (pdata == NULL) {
926                         goto fail;
927                 }
928                 ret = PyBytes_Check(pdata);
929                 if (!ret) {
930                         goto fail;
931                 }
932                 data = (DATA_BLOB) {
933                         .data = (uint8_t *)PyBytes_AsString(pdata),
934                         .length = PyBytes_Size(pdata),
935                 };
936                 status = smb2_create_blob_add(ctxs, ctxs, name, data);
937                 if (!NT_STATUS_IS_OK(status)) {
938                         goto fail;
939                 }
940         }
941         return ctxs;
942
943 fail:
944         TALLOC_FREE(ctxs);
945         return NULL;
946 }
947
948 static PyObject *py_cli_create_contexts(const struct smb2_create_blobs *blobs)
949 {
950         PyObject *py_blobs = NULL;
951         uint32_t i;
952
953         if (blobs == NULL) {
954                 Py_RETURN_NONE;
955         }
956
957         py_blobs = PyList_New(blobs->num_blobs);
958         if (py_blobs == NULL) {
959                 return NULL;
960         }
961
962         for (i=0; i<blobs->num_blobs; i++) {
963                 struct smb2_create_blob *blob = &blobs->blobs[i];
964                 PyObject *py_blob = NULL;
965                 int ret;
966
967                 py_blob = Py_BuildValue(
968                         "(yy#)",
969                         blob->tag,
970                         blob->data.data,
971                         (int)blob->data.length);
972                 if (py_blob == NULL) {
973                         goto fail;
974                 }
975
976                 ret = PyList_SetItem(py_blobs, i, py_blob);
977                 if (ret == -1) {
978                         Py_XDECREF(py_blob);
979                         goto fail;
980                 }
981         }
982         return py_blobs;
983
984 fail:
985         Py_XDECREF(py_blobs);
986         return NULL;
987 }
988
989 static PyObject *py_cli_create_returns(const struct smb_create_returns *r)
990 {
991         PyObject *v = NULL;
992
993         v = Py_BuildValue(
994                 "{sLsLsLsLsLsLsLsLsLsL}",
995                 "oplock_level",
996                 (unsigned long long)r->oplock_level,
997                 "flags",
998                 (unsigned long long)r->flags,
999                 "create_action",
1000                 (unsigned long long)r->create_action,
1001                 "creation_time",
1002                 (unsigned long long)r->creation_time,
1003                 "last_access_time",
1004                 (unsigned long long)r->last_access_time,
1005                 "last_write_time",
1006                 (unsigned long long)r->last_write_time,
1007                 "change_time",
1008                 (unsigned long long)r->change_time,
1009                 "allocation_size",
1010                 (unsigned long long)r->allocation_size,
1011                 "end_of_file",
1012                 (unsigned long long)r->end_of_file,
1013                 "file_attributes",
1014                 (unsigned long long)r->file_attributes);
1015         return v;
1016 }
1017
1018 static PyObject *py_cli_symlink_error(const struct symlink_reparse_struct *s)
1019 {
1020         char *subst_utf8 = NULL, *print_utf8 = NULL;
1021         size_t subst_utf8_len, print_utf8_len;
1022         PyObject *v = NULL;
1023         bool ok = true;
1024
1025         /*
1026          * Python wants utf-8, regardless of our unix charset (which
1027          * most likely is utf-8 these days, but you never know).
1028          */
1029
1030         ok = convert_string_talloc(
1031                 talloc_tos(),
1032                 CH_UNIX,
1033                 CH_UTF8,
1034                 s->substitute_name,
1035                 strlen(s->substitute_name),
1036                 &subst_utf8,
1037                 &subst_utf8_len);
1038         if (!ok) {
1039                 goto fail;
1040         }
1041
1042         ok = convert_string_talloc(
1043                 talloc_tos(),
1044                 CH_UNIX,
1045                 CH_UTF8,
1046                 s->print_name,
1047                 strlen(s->print_name),
1048                 &print_utf8,
1049                 &print_utf8_len);
1050         if (!ok) {
1051                 goto fail;
1052         }
1053
1054         v = Py_BuildValue(
1055                 "{sLsssssL}",
1056                 "unparsed_path_length",
1057                 (unsigned long long)s->unparsed_path_length,
1058                 "substitute_name",
1059                 subst_utf8,
1060                 "print_name",
1061                 print_utf8,
1062                 "flags",
1063                 (unsigned long long)s->flags);
1064
1065 fail:
1066         TALLOC_FREE(subst_utf8);
1067         TALLOC_FREE(print_utf8);
1068         return v;
1069 }
1070
1071 static PyObject *py_cli_create_ex(
1072         struct py_cli_state *self, PyObject *args, PyObject *kwds)
1073 {
1074         char *fname = NULL;
1075         unsigned CreateFlags = 0;
1076         unsigned DesiredAccess = FILE_GENERIC_READ;
1077         unsigned FileAttributes = 0;
1078         unsigned ShareAccess = 0;
1079         unsigned CreateDisposition = FILE_OPEN;
1080         unsigned CreateOptions = 0;
1081         unsigned ImpersonationLevel = SMB2_IMPERSONATION_IMPERSONATION;
1082         unsigned SecurityFlags = 0;
1083         PyObject *py_create_contexts_in = NULL;
1084         PyObject *py_create_contexts_out = NULL;
1085         struct smb2_create_blobs *create_contexts_in = NULL;
1086         struct smb2_create_blobs create_contexts_out = { .num_blobs = 0 };
1087         struct smb_create_returns cr = { .create_action = 0, };
1088         struct symlink_reparse_struct *symlink = NULL;
1089         PyObject *py_cr = NULL;
1090         uint16_t fnum;
1091         struct tevent_req *req;
1092         NTSTATUS status;
1093         int ret;
1094         bool ok;
1095         PyObject *v = NULL;
1096
1097         static const char *kwlist[] = {
1098                 "Name",
1099                 "CreateFlags",
1100                 "DesiredAccess",
1101                 "FileAttributes",
1102                 "ShareAccess",
1103                 "CreateDisposition",
1104                 "CreateOptions",
1105                 "ImpersonationLevel",
1106                 "SecurityFlags",
1107                 "CreateContexts",
1108                 NULL };
1109
1110         ret = ParseTupleAndKeywords(
1111                 args,
1112                 kwds,
1113                 "s|IIIIIIIIO",
1114                 kwlist,
1115                 &fname,
1116                 &CreateFlags,
1117                 &DesiredAccess,
1118                 &FileAttributes,
1119                 &ShareAccess,
1120                 &CreateDisposition,
1121                 &CreateOptions,
1122                 &ImpersonationLevel,
1123                 &SecurityFlags,
1124                 &py_create_contexts_in);
1125         if (!ret) {
1126                 return NULL;
1127         }
1128
1129         if (py_create_contexts_in != NULL) {
1130                 create_contexts_in = py_cli_get_create_contexts(
1131                         NULL, py_create_contexts_in);
1132                 if (create_contexts_in == NULL) {
1133                         errno = EINVAL;
1134                         PyErr_SetFromErrno(PyExc_RuntimeError);
1135                         return NULL;
1136                 }
1137         }
1138
1139         if (smbXcli_conn_protocol(self->cli->conn) >= PROTOCOL_SMB2_02) {
1140                 struct cli_smb2_create_flags cflags = {
1141                         .batch_oplock = (CreateFlags & REQUEST_BATCH_OPLOCK),
1142                         .exclusive_oplock = (CreateFlags & REQUEST_OPLOCK),
1143                 };
1144
1145                 req = cli_smb2_create_fnum_send(
1146                         NULL,
1147                         self->ev,
1148                         self->cli,
1149                         fname,
1150                         cflags,
1151                         ImpersonationLevel,
1152                         DesiredAccess,
1153                         FileAttributes,
1154                         ShareAccess,
1155                         CreateDisposition,
1156                         CreateOptions,
1157                         create_contexts_in);
1158         } else {
1159                 req = cli_ntcreate_send(
1160                         NULL,
1161                         self->ev,
1162                         self->cli,
1163                         fname,
1164                         CreateFlags,
1165                         DesiredAccess,
1166                         FileAttributes,
1167                         ShareAccess,
1168                         CreateDisposition,
1169                         CreateOptions,
1170                         ImpersonationLevel,
1171                         SecurityFlags);
1172         }
1173
1174         TALLOC_FREE(create_contexts_in);
1175
1176         ok = py_tevent_req_wait_exc(self, req);
1177         if (!ok) {
1178                 return NULL;
1179         }
1180
1181         if (smbXcli_conn_protocol(self->cli->conn) >= PROTOCOL_SMB2_02) {
1182                 status = cli_smb2_create_fnum_recv(
1183                         req,
1184                         &fnum,
1185                         &cr,
1186                         NULL,
1187                         &create_contexts_out,
1188                         &symlink);
1189         } else {
1190                 status = cli_ntcreate_recv(req, &fnum, &cr);
1191         }
1192
1193         TALLOC_FREE(req);
1194
1195         if (!NT_STATUS_IS_OK(status)) {
1196                 goto fail;
1197         }
1198
1199         SMB_ASSERT(symlink == NULL);
1200
1201         py_create_contexts_out = py_cli_create_contexts(&create_contexts_out);
1202         TALLOC_FREE(create_contexts_out.blobs);
1203         if (py_create_contexts_out == NULL) {
1204                 goto nomem;
1205         }
1206
1207         py_cr = py_cli_create_returns(&cr);
1208         if (py_cr == NULL) {
1209                 goto nomem;
1210         }
1211
1212         v = Py_BuildValue("(IOO)",
1213                           (unsigned)fnum,
1214                           py_cr,
1215                           py_create_contexts_out);
1216         return v;
1217 nomem:
1218         status = NT_STATUS_NO_MEMORY;
1219 fail:
1220         Py_XDECREF(py_create_contexts_out);
1221         Py_XDECREF(py_cr);
1222         Py_XDECREF(v);
1223
1224         if (NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK) &&
1225             (symlink != NULL)) {
1226                 PyErr_SetObject(
1227                         PyObject_GetAttrString(
1228                                 PyImport_ImportModule("samba"),
1229                                 "NTSTATUSError"),
1230                         Py_BuildValue(
1231                                 "I,s,O",
1232                                 NT_STATUS_V(status),
1233                                 get_friendly_nt_error_msg(status),
1234                                 py_cli_symlink_error(symlink)));
1235         } else {
1236                 PyErr_SetNTSTATUS(status);
1237         }
1238         return NULL;
1239 }
1240
1241 static PyObject *py_cli_close(struct py_cli_state *self, PyObject *args)
1242 {
1243         struct tevent_req *req;
1244         int fnum;
1245         int flags = 0;
1246         NTSTATUS status;
1247
1248         if (!PyArg_ParseTuple(args, "i|i", &fnum, &flags)) {
1249                 return NULL;
1250         }
1251
1252         req = cli_close_send(NULL, self->ev, self->cli, fnum, flags);
1253         if (!py_tevent_req_wait_exc(self, req)) {
1254                 return NULL;
1255         }
1256         status = cli_close_recv(req);
1257         TALLOC_FREE(req);
1258
1259         if (!NT_STATUS_IS_OK(status)) {
1260                 PyErr_SetNTSTATUS(status);
1261                 return NULL;
1262         }
1263         Py_RETURN_NONE;
1264 }
1265
1266 static PyObject *py_cli_rename(
1267         struct py_cli_state *self, PyObject *args, PyObject *kwds)
1268 {
1269         char *fname_src = NULL, *fname_dst = NULL;
1270         int replace = false;
1271         struct tevent_req *req = NULL;
1272         NTSTATUS status;
1273         bool ok;
1274
1275         static const char *kwlist[] = { "src", "dst", "replace", NULL };
1276
1277         ok = ParseTupleAndKeywords(
1278                 args, kwds, "ss|p", kwlist, &fname_src, &fname_dst, &replace);
1279         if (!ok) {
1280                 return NULL;
1281         }
1282
1283         req = cli_rename_send(
1284                 NULL, self->ev, self->cli, fname_src, fname_dst, replace);
1285         if (!py_tevent_req_wait_exc(self, req)) {
1286                 return NULL;
1287         }
1288         status = cli_rename_recv(req);
1289         TALLOC_FREE(req);
1290
1291         if (!NT_STATUS_IS_OK(status)) {
1292                 PyErr_SetNTSTATUS(status);
1293                 return NULL;
1294         }
1295         Py_RETURN_NONE;
1296 }
1297
1298
1299 struct push_state {
1300         char *data;
1301         off_t nread;
1302         off_t total_data;
1303 };
1304
1305 /*
1306  * cli_push() helper to write a chunk of data to a remote file
1307  */
1308 static size_t push_data(uint8_t *buf, size_t n, void *priv)
1309 {
1310         struct push_state *state = (struct push_state *)priv;
1311         char *curr_ptr = NULL;
1312         off_t remaining;
1313         size_t copied_bytes;
1314
1315         if (state->nread >= state->total_data) {
1316                 return 0;
1317         }
1318
1319         curr_ptr = state->data + state->nread;
1320         remaining = state->total_data - state->nread;
1321         copied_bytes = MIN(remaining, n);
1322
1323         memcpy(buf, curr_ptr, copied_bytes);
1324         state->nread += copied_bytes;
1325         return copied_bytes;
1326 }
1327
1328 /*
1329  * Writes a file with the contents specified
1330  */
1331 static PyObject *py_smb_savefile(struct py_cli_state *self, PyObject *args)
1332 {
1333         uint16_t fnum;
1334         const char *filename = NULL;
1335         char *data = NULL;
1336         Py_ssize_t size = 0;
1337         NTSTATUS status;
1338         struct tevent_req *req = NULL;
1339         struct push_state state;
1340
1341         if (!PyArg_ParseTuple(args, "s"PYARG_BYTES_LEN":savefile", &filename,
1342                               &data, &size)) {
1343                 return NULL;
1344         }
1345
1346         /* create a new file handle for writing to */
1347         req = cli_ntcreate_send(NULL, self->ev, self->cli, filename, 0,
1348                                 FILE_WRITE_DATA, FILE_ATTRIBUTE_NORMAL,
1349                                 FILE_SHARE_READ|FILE_SHARE_WRITE,
1350                                 FILE_OVERWRITE_IF, FILE_NON_DIRECTORY_FILE,
1351                                 SMB2_IMPERSONATION_IMPERSONATION, 0);
1352         if (!py_tevent_req_wait_exc(self, req)) {
1353                 return NULL;
1354         }
1355         status = cli_ntcreate_recv(req, &fnum, NULL);
1356         TALLOC_FREE(req);
1357         PyErr_NTSTATUS_NOT_OK_RAISE(status);
1358
1359         /* write the new file contents */
1360         state.data = data;
1361         state.nread = 0;
1362         state.total_data = size;
1363
1364         req = cli_push_send(NULL, self->ev, self->cli, fnum, 0, 0, 0,
1365                             push_data, &state);
1366         if (!py_tevent_req_wait_exc(self, req)) {
1367                 return NULL;
1368         }
1369         status = cli_push_recv(req);
1370         TALLOC_FREE(req);
1371         PyErr_NTSTATUS_NOT_OK_RAISE(status);
1372
1373         /* close the file handle */
1374         req = cli_close_send(NULL, self->ev, self->cli, fnum, 0);
1375         if (!py_tevent_req_wait_exc(self, req)) {
1376                 return NULL;
1377         }
1378         status = cli_close_recv(req);
1379         PyErr_NTSTATUS_NOT_OK_RAISE(status);
1380
1381         Py_RETURN_NONE;
1382 }
1383
1384 static PyObject *py_cli_write(struct py_cli_state *self, PyObject *args,
1385                               PyObject *kwds)
1386 {
1387         int fnum;
1388         unsigned mode = 0;
1389         char *buf;
1390         Py_ssize_t buflen;
1391         unsigned long long offset;
1392         struct tevent_req *req;
1393         NTSTATUS status;
1394         size_t written;
1395
1396         static const char *kwlist[] = {
1397                 "fnum", "buffer", "offset", "mode", NULL };
1398
1399         if (!ParseTupleAndKeywords(
1400                     args, kwds, "i" PYARG_BYTES_LEN "K|I", kwlist,
1401                     &fnum, &buf, &buflen, &offset, &mode)) {
1402                 return NULL;
1403         }
1404
1405         req = cli_write_send(NULL, self->ev, self->cli, fnum, mode,
1406                              (uint8_t *)buf, offset, buflen);
1407         if (!py_tevent_req_wait_exc(self, req)) {
1408                 return NULL;
1409         }
1410         status = cli_write_recv(req, &written);
1411         TALLOC_FREE(req);
1412
1413         if (!NT_STATUS_IS_OK(status)) {
1414                 PyErr_SetNTSTATUS(status);
1415                 return NULL;
1416         }
1417         return Py_BuildValue("K", (unsigned long long)written);
1418 }
1419
1420 /*
1421  * Returns the size of the given file
1422  */
1423 static NTSTATUS py_smb_filesize(struct py_cli_state *self, uint16_t fnum,
1424                                 off_t *size)
1425 {
1426         NTSTATUS status;
1427         struct tevent_req *req = NULL;
1428
1429         req = cli_qfileinfo_basic_send(NULL, self->ev, self->cli, fnum);
1430         if (!py_tevent_req_wait_exc(self, req)) {
1431                 return NT_STATUS_INTERNAL_ERROR;
1432         }
1433         status = cli_qfileinfo_basic_recv(
1434                 req, NULL, size, NULL, NULL, NULL, NULL, NULL);
1435         TALLOC_FREE(req);
1436         return status;
1437 }
1438
1439 /*
1440  * Loads the specified file's contents and returns it
1441  */
1442 static PyObject *py_smb_loadfile(struct py_cli_state *self, PyObject *args)
1443 {
1444         NTSTATUS status;
1445         const char *filename = NULL;
1446         struct tevent_req *req = NULL;
1447         uint16_t fnum;
1448         off_t size;
1449         char *buf = NULL;
1450         off_t nread = 0;
1451         PyObject *result = NULL;
1452
1453         if (!PyArg_ParseTuple(args, "s:loadfile", &filename)) {
1454                 return NULL;
1455         }
1456
1457         /* get a read file handle */
1458         req = cli_ntcreate_send(NULL, self->ev, self->cli, filename, 0,
1459                                 FILE_READ_DATA | FILE_READ_ATTRIBUTES,
1460                                 FILE_ATTRIBUTE_NORMAL,
1461                                 FILE_SHARE_READ, FILE_OPEN, 0,
1462                                 SMB2_IMPERSONATION_IMPERSONATION, 0);
1463         if (!py_tevent_req_wait_exc(self, req)) {
1464                 return NULL;
1465         }
1466         status = cli_ntcreate_recv(req, &fnum, NULL);
1467         TALLOC_FREE(req);
1468         PyErr_NTSTATUS_NOT_OK_RAISE(status);
1469
1470         /* get a buffer to hold the file contents */
1471         status = py_smb_filesize(self, fnum, &size);
1472         PyErr_NTSTATUS_NOT_OK_RAISE(status);
1473
1474         result = PyBytes_FromStringAndSize(NULL, size);
1475         if (result == NULL) {
1476                 return NULL;
1477         }
1478
1479         /* read the file contents */
1480         buf = PyBytes_AS_STRING(result);
1481         req = cli_pull_send(NULL, self->ev, self->cli, fnum, 0, size,
1482                             size, cli_read_sink, &buf);
1483         if (!py_tevent_req_wait_exc(self, req)) {
1484                 Py_XDECREF(result);
1485                 return NULL;
1486         }
1487         status = cli_pull_recv(req, &nread);
1488         TALLOC_FREE(req);
1489         if (!NT_STATUS_IS_OK(status)) {
1490                 Py_XDECREF(result);
1491                 PyErr_SetNTSTATUS(status);
1492                 return NULL;
1493         }
1494
1495         /* close the file handle */
1496         req = cli_close_send(NULL, self->ev, self->cli, fnum, 0);
1497         if (!py_tevent_req_wait_exc(self, req)) {
1498                 Py_XDECREF(result);
1499                 return NULL;
1500         }
1501         status = cli_close_recv(req);
1502         TALLOC_FREE(req);
1503         if (!NT_STATUS_IS_OK(status)) {
1504                 Py_XDECREF(result);
1505                 PyErr_SetNTSTATUS(status);
1506                 return NULL;
1507         }
1508
1509         /* sanity-check we read the expected number of bytes */
1510         if (nread > size) {
1511                 Py_XDECREF(result);
1512                 PyErr_Format(PyExc_IOError,
1513                              "read invalid - got %zu requested %zu",
1514                              nread, size);
1515                 return NULL;
1516         }
1517
1518         if (nread < size) {
1519                 if (_PyBytes_Resize(&result, nread) < 0) {
1520                         return NULL;
1521                 }
1522         }
1523
1524         return result;
1525 }
1526
1527 static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
1528                              PyObject *kwds)
1529 {
1530         int fnum;
1531         unsigned long long offset;
1532         unsigned size;
1533         struct tevent_req *req;
1534         NTSTATUS status;
1535         char *buf;
1536         size_t received;
1537         PyObject *result;
1538
1539         static const char *kwlist[] = {
1540                 "fnum", "offset", "size", NULL };
1541
1542         if (!ParseTupleAndKeywords(
1543                     args, kwds, "iKI", kwlist, &fnum, &offset,
1544                     &size)) {
1545                 return NULL;
1546         }
1547
1548         result = PyBytes_FromStringAndSize(NULL, size);
1549         if (result == NULL) {
1550                 return NULL;
1551         }
1552         buf = PyBytes_AS_STRING(result);
1553
1554         req = cli_read_send(NULL, self->ev, self->cli, fnum,
1555                             buf, offset, size);
1556         if (!py_tevent_req_wait_exc(self, req)) {
1557                 Py_XDECREF(result);
1558                 return NULL;
1559         }
1560         status = cli_read_recv(req, &received);
1561         TALLOC_FREE(req);
1562
1563         if (!NT_STATUS_IS_OK(status)) {
1564                 Py_XDECREF(result);
1565                 PyErr_SetNTSTATUS(status);
1566                 return NULL;
1567         }
1568
1569         if (received > size) {
1570                 Py_XDECREF(result);
1571                 PyErr_Format(PyExc_IOError,
1572                              "read invalid - got %zu requested %u",
1573                              received, size);
1574                 return NULL;
1575         }
1576
1577         if (received < size) {
1578                 if (_PyBytes_Resize(&result, received) < 0) {
1579                         return NULL;
1580                 }
1581         }
1582
1583         return result;
1584 }
1585
1586 static PyObject *py_cli_ftruncate(struct py_cli_state *self, PyObject *args,
1587                                   PyObject *kwds)
1588 {
1589         int fnum;
1590         unsigned long long size;
1591         struct tevent_req *req;
1592         NTSTATUS status;
1593
1594         static const char *kwlist[] = {
1595                 "fnum", "size", NULL };
1596
1597         if (!ParseTupleAndKeywords(
1598                     args, kwds, "IK", kwlist, &fnum, &size)) {
1599                 return NULL;
1600         }
1601
1602         req = cli_ftruncate_send(NULL, self->ev, self->cli, fnum, size);
1603         if (!py_tevent_req_wait_exc(self, req)) {
1604                 return NULL;
1605         }
1606         status = cli_ftruncate_recv(req);
1607         TALLOC_FREE(req);
1608
1609         if (!NT_STATUS_IS_OK(status)) {
1610                 PyErr_SetNTSTATUS(status);
1611                 return NULL;
1612         }
1613         Py_RETURN_NONE;
1614 }
1615
1616 static PyObject *py_cli_delete_on_close(struct py_cli_state *self,
1617                                         PyObject *args,
1618                                         PyObject *kwds)
1619 {
1620         unsigned fnum, flag;
1621         struct tevent_req *req;
1622         NTSTATUS status;
1623
1624         static const char *kwlist[] = {
1625                 "fnum", "flag", NULL };
1626
1627         if (!ParseTupleAndKeywords(
1628                     args, kwds, "II", kwlist, &fnum, &flag)) {
1629                 return NULL;
1630         }
1631
1632         req = cli_nt_delete_on_close_send(NULL, self->ev, self->cli, fnum,
1633                                           flag);
1634         if (!py_tevent_req_wait_exc(self, req)) {
1635                 return NULL;
1636         }
1637         status = cli_nt_delete_on_close_recv(req);
1638         TALLOC_FREE(req);
1639
1640         if (!NT_STATUS_IS_OK(status)) {
1641                 PyErr_SetNTSTATUS(status);
1642                 return NULL;
1643         }
1644         Py_RETURN_NONE;
1645 }
1646
1647 struct py_cli_notify_state {
1648         PyObject_HEAD
1649         struct py_cli_state *py_cli_state;
1650         struct tevent_req *req;
1651 };
1652
1653 static void py_cli_notify_state_dealloc(struct py_cli_notify_state *self)
1654 {
1655         TALLOC_FREE(self->req);
1656         Py_CLEAR(self->py_cli_state);
1657         Py_TYPE(self)->tp_free(self);
1658 }
1659
1660 static PyTypeObject py_cli_notify_state_type;
1661
1662 static PyObject *py_cli_notify(struct py_cli_state *self,
1663                                PyObject *args,
1664                                PyObject *kwds)
1665 {
1666         static const char *kwlist[] = {
1667                 "fnum",
1668                 "buffer_size",
1669                 "completion_filter",
1670                 "recursive",
1671                 NULL
1672         };
1673         unsigned fnum = 0;
1674         unsigned buffer_size = 0;
1675         unsigned completion_filter = 0;
1676         PyObject *py_recursive = Py_False;
1677         bool recursive = false;
1678         struct tevent_req *req = NULL;
1679         struct tevent_queue *send_queue = NULL;
1680         struct tevent_req *flush_req = NULL;
1681         bool ok;
1682         struct py_cli_notify_state *py_notify_state = NULL;
1683         struct timeval endtime;
1684
1685         ok = ParseTupleAndKeywords(args,
1686                                    kwds,
1687                                    "IIIO",
1688                                    kwlist,
1689                                    &fnum,
1690                                    &buffer_size,
1691                                    &completion_filter,
1692                                    &py_recursive);
1693         if (!ok) {
1694                 return NULL;
1695         }
1696
1697         recursive = PyObject_IsTrue(py_recursive);
1698
1699         req = cli_notify_send(NULL,
1700                               self->ev,
1701                               self->cli,
1702                               fnum,
1703                               buffer_size,
1704                               completion_filter,
1705                               recursive);
1706         if (req == NULL) {
1707                 PyErr_NoMemory();
1708                 return NULL;
1709         }
1710
1711         /*
1712          * Just wait for the request being submitted to
1713          * the kernel/socket/wire.
1714          */
1715         send_queue = smbXcli_conn_send_queue(self->cli->conn);
1716         flush_req = tevent_queue_wait_send(req,
1717                                            self->ev,
1718                                            send_queue);
1719         endtime = timeval_current_ofs_msec(self->cli->timeout);
1720         ok = tevent_req_set_endtime(flush_req,
1721                                     self->ev,
1722                                     endtime);
1723         if (!ok) {
1724                 TALLOC_FREE(req);
1725                 PyErr_NoMemory();
1726                 return NULL;
1727         }
1728         ok = py_tevent_req_wait_exc(self, flush_req);
1729         if (!ok) {
1730                 TALLOC_FREE(req);
1731                 return NULL;
1732         }
1733         TALLOC_FREE(flush_req);
1734
1735         py_notify_state = (struct py_cli_notify_state *)
1736                 py_cli_notify_state_type.tp_alloc(&py_cli_notify_state_type, 0);
1737         if (py_notify_state == NULL) {
1738                 TALLOC_FREE(req);
1739                 PyErr_NoMemory();
1740                 return NULL;
1741         }
1742         Py_INCREF(self);
1743         py_notify_state->py_cli_state = self;
1744         py_notify_state->req = req;
1745
1746         return (PyObject *)py_notify_state;
1747 }
1748
1749 static PyObject *py_cli_notify_get_changes(struct py_cli_notify_state *self,
1750                                            PyObject *args,
1751                                            PyObject *kwds)
1752 {
1753         struct py_cli_state *py_cli_state = self->py_cli_state;
1754         struct tevent_req *req = self->req;
1755         uint32_t i;
1756         uint32_t num_changes = 0;
1757         struct notify_change *changes = NULL;
1758         PyObject *result = NULL;
1759         NTSTATUS status;
1760         bool ok;
1761         static const char *kwlist[] = {
1762                 "wait",
1763                 NULL
1764         };
1765         PyObject *py_wait = Py_False;
1766         bool wait = false;
1767         bool pending;
1768
1769         ok = ParseTupleAndKeywords(args,
1770                                    kwds,
1771                                    "O",
1772                                    kwlist,
1773                                    &py_wait);
1774         if (!ok) {
1775                 return NULL;
1776         }
1777
1778         wait = PyObject_IsTrue(py_wait);
1779
1780         if (req == NULL) {
1781                 PyErr_SetString(PyExc_RuntimeError,
1782                                 "TODO req == NULL "
1783                                 "- missing change notify request?");
1784                 return NULL;
1785         }
1786
1787         pending = tevent_req_is_in_progress(req);
1788         if (pending && !wait) {
1789                 Py_RETURN_NONE;
1790         }
1791
1792         if (pending) {
1793                 struct timeval endtime;
1794
1795                 endtime = timeval_current_ofs_msec(py_cli_state->cli->timeout);
1796                 ok = tevent_req_set_endtime(req,
1797                                             py_cli_state->ev,
1798                                             endtime);
1799                 if (!ok) {
1800                         TALLOC_FREE(req);
1801                         PyErr_NoMemory();
1802                         return NULL;
1803                 }
1804         }
1805
1806         ok = py_tevent_req_wait_exc(py_cli_state, req);
1807         self->req = NULL;
1808         Py_CLEAR(self->py_cli_state);
1809         if (!ok) {
1810                 return NULL;
1811         }
1812
1813         status = cli_notify_recv(req, req, &num_changes, &changes);
1814         if (!NT_STATUS_IS_OK(status)) {
1815                 TALLOC_FREE(req);
1816                 PyErr_SetNTSTATUS(status);
1817                 return NULL;
1818         }
1819
1820         result = Py_BuildValue("[]");
1821         if (result == NULL) {
1822                 TALLOC_FREE(req);
1823                 return NULL;
1824         }
1825
1826         for (i = 0; i < num_changes; i++) {
1827                 PyObject *change = NULL;
1828                 int ret;
1829
1830                 change = Py_BuildValue("{s:s,s:I}",
1831                                        "name", changes[i].name,
1832                                        "action", changes[i].action);
1833                 if (change == NULL) {
1834                         Py_XDECREF(result);
1835                         TALLOC_FREE(req);
1836                         return NULL;
1837                 }
1838
1839                 ret = PyList_Append(result, change);
1840                 Py_DECREF(change);
1841                 if (ret == -1) {
1842                         Py_XDECREF(result);
1843                         TALLOC_FREE(req);
1844                         return NULL;
1845                 }
1846         }
1847
1848         TALLOC_FREE(req);
1849         return result;
1850 }
1851
1852 static PyMethodDef py_cli_notify_state_methods[] = {
1853         {
1854                 .ml_name = "get_changes",
1855                 .ml_meth = (PyCFunction)py_cli_notify_get_changes,
1856                 .ml_flags = METH_VARARGS|METH_KEYWORDS,
1857                 .ml_doc  = "Wait for change notifications: \n"
1858                            "N.get_changes(wait=BOOLEAN) -> "
1859                            "change notifications as a dictionary\n"
1860                            "\t\tList contents of a directory. The keys are, \n"
1861                            "\t\t\tname: name of changed object\n"
1862                            "\t\t\taction: type of the change\n"
1863                            "None is returned if there's no response yet and "
1864                            "wait=False is passed"
1865         },
1866         {
1867                 .ml_name = NULL
1868         }
1869 };
1870
1871 static PyTypeObject py_cli_notify_state_type = {
1872         PyVarObject_HEAD_INIT(NULL, 0)
1873         .tp_name = "libsmb_samba_cwrapper.Notify",
1874         .tp_basicsize = sizeof(struct py_cli_notify_state),
1875         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1876         .tp_doc = "notify request",
1877         .tp_dealloc = (destructor)py_cli_notify_state_dealloc,
1878         .tp_methods = py_cli_notify_state_methods,
1879 };
1880
1881 /*
1882  * Helper to add posix directory listing entries to an overall Python list
1883  */
1884 static NTSTATUS list_posix_helper(struct file_info *finfo,
1885                                   const char *mask, void *state)
1886 {
1887         PyObject *result = (PyObject *)state;
1888         PyObject *file = NULL;
1889         PyObject *size = NULL;
1890         int ret;
1891
1892         size = PyLong_FromUnsignedLongLong(finfo->size);
1893         /*
1894          * Build a dictionary representing the file info.
1895          * Note: Windows does not always return short_name (so it may be None)
1896          */
1897         file = Py_BuildValue("{s:s,s:i,s:s,s:O,s:l,s:i,s:i,s:i,s:s,s:s}",
1898                              "name", finfo->name,
1899                              "attrib", (int)finfo->attr,
1900                              "short_name", finfo->short_name,
1901                              "size", size,
1902                              "mtime",
1903                              convert_timespec_to_time_t(finfo->mtime_ts),
1904                              "perms", finfo->st_ex_mode,
1905                              "ino", finfo->ino,
1906                              "dev", finfo->st_ex_dev,
1907                              "owner_sid",
1908                              dom_sid_string(finfo, &finfo->owner_sid),
1909                              "group_sid",
1910                              dom_sid_string(finfo, &finfo->group_sid));
1911
1912         Py_CLEAR(size);
1913
1914         if (file == NULL) {
1915                 return NT_STATUS_NO_MEMORY;
1916         }
1917
1918         ret = PyList_Append(result, file);
1919         Py_CLEAR(file);
1920         if (ret == -1) {
1921                 return NT_STATUS_INTERNAL_ERROR;
1922         }
1923
1924         return NT_STATUS_OK;
1925 }
1926
1927 /*
1928  * Helper to add directory listing entries to an overall Python list
1929  */
1930 static NTSTATUS list_helper(struct file_info *finfo,
1931                             const char *mask, void *state)
1932 {
1933         PyObject *result = (PyObject *)state;
1934         PyObject *file = NULL;
1935         PyObject *size = NULL;
1936         int ret;
1937
1938         /* suppress '.' and '..' in the results we return */
1939         if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
1940                 return NT_STATUS_OK;
1941         }
1942         size = PyLong_FromUnsignedLongLong(finfo->size);
1943         /*
1944          * Build a dictionary representing the file info.
1945          * Note: Windows does not always return short_name (so it may be None)
1946          */
1947         file = Py_BuildValue("{s:s,s:i,s:s,s:O,s:l}",
1948                              "name", finfo->name,
1949                              "attrib", (int)finfo->attr,
1950                              "short_name", finfo->short_name,
1951                              "size", size,
1952                              "mtime",
1953                              convert_timespec_to_time_t(finfo->mtime_ts));
1954
1955         Py_CLEAR(size);
1956
1957         if (file == NULL) {
1958                 return NT_STATUS_NO_MEMORY;
1959         }
1960
1961         if (finfo->attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1962                 unsigned long tag = finfo->reparse_tag;
1963
1964                 ret = PyDict_SetItemString(
1965                         file,
1966                         "reparse_tag",
1967                         PyLong_FromUnsignedLong(tag));
1968                 if (ret == -1) {
1969                         return NT_STATUS_INTERNAL_ERROR;
1970                 }
1971         }
1972
1973         ret = PyList_Append(result, file);
1974         Py_CLEAR(file);
1975         if (ret == -1) {
1976                 return NT_STATUS_INTERNAL_ERROR;
1977         }
1978
1979         return NT_STATUS_OK;
1980 }
1981
1982 struct do_listing_state {
1983         const char *mask;
1984         NTSTATUS (*callback_fn)(
1985                 struct file_info *finfo,
1986                 const char *mask,
1987                 void *private_data);
1988         void *private_data;
1989         NTSTATUS status;
1990 };
1991
1992 static void do_listing_cb(struct tevent_req *subreq)
1993 {
1994         struct do_listing_state *state = tevent_req_callback_data_void(subreq);
1995         struct file_info *finfo = NULL;
1996
1997         state->status = cli_list_recv(subreq, NULL, &finfo);
1998         if (!NT_STATUS_IS_OK(state->status)) {
1999                 return;
2000         }
2001         state->callback_fn(finfo, state->mask, state->private_data);
2002         TALLOC_FREE(finfo);
2003 }
2004
2005 static NTSTATUS do_listing(struct py_cli_state *self,
2006                            const char *base_dir, const char *user_mask,
2007                            uint16_t attribute,
2008                            unsigned int info_level,
2009                            bool posix,
2010                            NTSTATUS (*callback_fn)(struct file_info *,
2011                                                    const char *, void *),
2012                            void *priv)
2013 {
2014         char *mask = NULL;
2015         struct do_listing_state state = {
2016                 .mask = mask,
2017                 .callback_fn = callback_fn,
2018                 .private_data = priv,
2019         };
2020         struct tevent_req *req = NULL;
2021         NTSTATUS status;
2022
2023         if (user_mask == NULL) {
2024                 mask = talloc_asprintf(NULL, "%s\\*", base_dir);
2025         } else {
2026                 mask = talloc_asprintf(NULL, "%s\\%s", base_dir, user_mask);
2027         }
2028
2029         if (mask == NULL) {
2030                 return NT_STATUS_NO_MEMORY;
2031         }
2032         dos_format(mask);
2033
2034         req = cli_list_send(NULL, self->ev, self->cli, mask, attribute,
2035                             info_level, posix);
2036         if (req == NULL) {
2037                 status = NT_STATUS_NO_MEMORY;
2038                 goto done;
2039         }
2040         tevent_req_set_callback(req, do_listing_cb, &state);
2041
2042         if (!py_tevent_req_wait_exc(self, req)) {
2043                 return NT_STATUS_INTERNAL_ERROR;
2044         }
2045         TALLOC_FREE(req);
2046
2047         status = state.status;
2048         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_FILES)) {
2049                 status = NT_STATUS_OK;
2050         }
2051
2052 done:
2053         TALLOC_FREE(mask);
2054         return status;
2055 }
2056
2057 static PyObject *py_cli_list(struct py_cli_state *self,
2058                              PyObject *args,
2059                              PyObject *kwds)
2060 {
2061         char *base_dir;
2062         char *user_mask = NULL;
2063         unsigned int attribute = LIST_ATTRIBUTE_MASK;
2064         unsigned int info_level = 0;
2065         bool posix = false;
2066         NTSTATUS status;
2067         enum protocol_types proto = smbXcli_conn_protocol(self->cli->conn);
2068         PyObject *result = NULL;
2069         const char *kwlist[] = { "directory", "mask", "attribs", "posix",
2070                                  "info_level", NULL };
2071         NTSTATUS (*callback_fn)(struct file_info *, const char *, void *) =
2072                 &list_helper;
2073
2074         if (!ParseTupleAndKeywords(args, kwds, "z|sIpI:list", kwlist,
2075                                    &base_dir, &user_mask, &attribute,
2076                                    &posix, &info_level)) {
2077                 return NULL;
2078         }
2079
2080         result = Py_BuildValue("[]");
2081         if (result == NULL) {
2082                 return NULL;
2083         }
2084
2085         if (!info_level) {
2086                 if (proto >= PROTOCOL_SMB2_02) {
2087                         info_level = SMB2_FIND_ID_BOTH_DIRECTORY_INFO;
2088                 } else {
2089                         info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
2090                 }
2091         }
2092
2093         if (posix) {
2094                 callback_fn = &list_posix_helper;
2095         }
2096         status = do_listing(self, base_dir, user_mask, attribute,
2097                             info_level, posix, callback_fn, result);
2098
2099         if (!NT_STATUS_IS_OK(status)) {
2100                 Py_XDECREF(result);
2101                 PyErr_SetNTSTATUS(status);
2102                 return NULL;
2103         }
2104
2105         return result;
2106 }
2107
2108 static PyObject *py_smb_unlink(struct py_cli_state *self, PyObject *args)
2109 {
2110         NTSTATUS status;
2111         const char *filename = NULL;
2112         struct tevent_req *req = NULL;
2113         const uint32_t attrs = (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2114
2115         if (!PyArg_ParseTuple(args, "s:unlink", &filename)) {
2116                 return NULL;
2117         }
2118
2119         req = cli_unlink_send(NULL, self->ev, self->cli, filename, attrs);
2120         if (!py_tevent_req_wait_exc(self, req)) {
2121                 return NULL;
2122         }
2123         status = cli_unlink_recv(req);
2124         TALLOC_FREE(req);
2125         PyErr_NTSTATUS_NOT_OK_RAISE(status);
2126
2127         Py_RETURN_NONE;
2128 }
2129
2130 static PyObject *py_smb_rmdir(struct py_cli_state *self, PyObject *args)
2131 {
2132         NTSTATUS status;
2133         struct tevent_req *req = NULL;
2134         const char *dirname = NULL;
2135
2136         if (!PyArg_ParseTuple(args, "s:rmdir", &dirname)) {
2137                 return NULL;
2138         }
2139
2140         req = cli_rmdir_send(NULL, self->ev, self->cli, dirname);
2141         if (!py_tevent_req_wait_exc(self, req)) {
2142                 return NULL;
2143         }
2144         status = cli_rmdir_recv(req);
2145         TALLOC_FREE(req);
2146         PyErr_NTSTATUS_NOT_OK_RAISE(status);
2147
2148         Py_RETURN_NONE;
2149 }
2150
2151 /*
2152  * Create a directory
2153  */
2154 static PyObject *py_smb_mkdir(struct py_cli_state *self, PyObject *args)
2155 {
2156         NTSTATUS status;
2157         const char *dirname = NULL;
2158         struct tevent_req *req = NULL;
2159
2160         if (!PyArg_ParseTuple(args, "s:mkdir", &dirname)) {
2161                 return NULL;
2162         }
2163
2164         req = cli_mkdir_send(NULL, self->ev, self->cli, dirname);
2165         if (!py_tevent_req_wait_exc(self, req)) {
2166                 return NULL;
2167         }
2168         status = cli_mkdir_recv(req);
2169         TALLOC_FREE(req);
2170         PyErr_NTSTATUS_NOT_OK_RAISE(status);
2171
2172         Py_RETURN_NONE;
2173 }
2174
2175 /*
2176  * Does a whoami call
2177  */
2178 static PyObject *py_smb_posix_whoami(struct py_cli_state *self,
2179                                      PyObject *Py_UNUSED(ignored))
2180 {
2181         TALLOC_CTX *frame = talloc_stackframe();
2182         NTSTATUS status;
2183         struct tevent_req *req = NULL;
2184         uint64_t uid;
2185         uint64_t gid;
2186         uint32_t num_gids;
2187         uint64_t *gids = NULL;
2188         uint32_t num_sids;
2189         struct dom_sid *sids = NULL;
2190         bool guest;
2191         PyObject *py_gids = NULL;
2192         PyObject *py_sids = NULL;
2193         PyObject *py_guest = NULL;
2194         PyObject *py_ret = NULL;
2195         Py_ssize_t i;
2196
2197         req = cli_posix_whoami_send(frame, self->ev, self->cli);
2198         if (!py_tevent_req_wait_exc(self, req)) {
2199                 goto fail;
2200         }
2201         status = cli_posix_whoami_recv(req,
2202                                 frame,
2203                                 &uid,
2204                                 &gid,
2205                                 &num_gids,
2206                                 &gids,
2207                                 &num_sids,
2208                                 &sids,
2209                                 &guest);
2210         if (!NT_STATUS_IS_OK(status)) {
2211                 PyErr_SetNTSTATUS(status);
2212                 goto fail;
2213         }
2214         if (num_gids > PY_SSIZE_T_MAX) {
2215                 PyErr_SetString(PyExc_OverflowError, "posix_whoami: Too many GIDs");
2216                 goto fail;
2217         }
2218         if (num_sids > PY_SSIZE_T_MAX) {
2219                 PyErr_SetString(PyExc_OverflowError, "posix_whoami: Too many SIDs");
2220                 goto fail;
2221         }
2222
2223         py_gids = PyList_New(num_gids);
2224         if (!py_gids) {
2225                 goto fail;
2226         }
2227         for (i = 0; i < num_gids; ++i) {
2228                 int ret;
2229                 PyObject *py_item = PyLong_FromUnsignedLongLong(gids[i]);
2230                 if (!py_item) {
2231                         goto fail2;
2232                 }
2233
2234                 ret = PyList_SetItem(py_gids, i, py_item);
2235                 if (ret) {
2236                         goto fail2;
2237                 }
2238         }
2239         py_sids = PyList_New(num_sids);
2240         if (!py_sids) {
2241                 goto fail2;
2242         }
2243         for (i = 0; i < num_sids; ++i) {
2244                 int ret;
2245                 struct dom_sid *sid;
2246                 PyObject *py_item;
2247
2248                 sid = dom_sid_dup(frame, &sids[i]);
2249                 if (!sid) {
2250                         PyErr_NoMemory();
2251                         goto fail3;
2252                 }
2253
2254                 py_item = pytalloc_steal(dom_sid_Type, sid);
2255                 if (!py_item) {
2256                         PyErr_NoMemory();
2257                         goto fail3;
2258                 }
2259
2260                 ret = PyList_SetItem(py_sids, i, py_item);
2261                 if (ret) {
2262                         goto fail3;
2263                 }
2264         }
2265
2266         py_guest = guest ? Py_True : Py_False;
2267
2268         py_ret = Py_BuildValue("KKNNO",
2269                         uid,
2270                         gid,
2271                         py_gids,
2272                         py_sids,
2273                         py_guest);
2274         if (!py_ret) {
2275                 goto fail3;
2276         }
2277
2278         TALLOC_FREE(frame);
2279         return py_ret;
2280
2281 fail3:
2282         Py_CLEAR(py_sids);
2283
2284 fail2:
2285         Py_CLEAR(py_gids);
2286
2287 fail:
2288         TALLOC_FREE(frame);
2289         return NULL;
2290 }
2291
2292 /*
2293  * Checks existence of a directory
2294  */
2295 static bool check_dir_path(struct py_cli_state *self, const char *path)
2296 {
2297         NTSTATUS status;
2298         struct tevent_req *req = NULL;
2299
2300         req = cli_chkpath_send(NULL, self->ev, self->cli, path);
2301         if (!py_tevent_req_wait_exc(self, req)) {
2302                 return false;
2303         }
2304         status = cli_chkpath_recv(req);
2305         TALLOC_FREE(req);
2306
2307         return NT_STATUS_IS_OK(status);
2308 }
2309
2310 static PyObject *py_smb_chkpath(struct py_cli_state *self, PyObject *args)
2311 {
2312         const char *path = NULL;
2313         bool dir_exists;
2314
2315         if (!PyArg_ParseTuple(args, "s:chkpath", &path)) {
2316                 return NULL;
2317         }
2318
2319         dir_exists = check_dir_path(self, path);
2320         return PyBool_FromLong(dir_exists);
2321 }
2322
2323 static PyObject *py_smb_have_posix(struct py_cli_state *self,
2324                                    PyObject *Py_UNUSED(ignored))
2325 {
2326         bool posix = smbXcli_conn_have_posix(self->cli->conn);
2327
2328         if (posix) {
2329                 Py_RETURN_TRUE;
2330         }
2331         Py_RETURN_FALSE;
2332 }
2333
2334 static PyObject *py_smb_protocol(struct py_cli_state *self,
2335                                  PyObject *Py_UNUSED(ignored))
2336 {
2337         enum protocol_types proto = smbXcli_conn_protocol(self->cli->conn);
2338         PyObject *result = PyLong_FromLong(proto);
2339         return result;
2340 }
2341
2342 static PyObject *py_smb_get_sd(struct py_cli_state *self, PyObject *args)
2343 {
2344         int fnum;
2345         unsigned sinfo;
2346         struct tevent_req *req = NULL;
2347         struct security_descriptor *sd = NULL;
2348         NTSTATUS status;
2349
2350         if (!PyArg_ParseTuple(args, "iI:get_acl", &fnum, &sinfo)) {
2351                 return NULL;
2352         }
2353
2354         req = cli_query_security_descriptor_send(
2355                 NULL, self->ev, self->cli, fnum, sinfo);
2356         if (!py_tevent_req_wait_exc(self, req)) {
2357                 return NULL;
2358         }
2359         status = cli_query_security_descriptor_recv(req, NULL, &sd);
2360         PyErr_NTSTATUS_NOT_OK_RAISE(status);
2361
2362         return py_return_ndr_struct(
2363                 "samba.dcerpc.security", "descriptor", sd, sd);
2364 }
2365
2366 static PyObject *py_smb_set_sd(struct py_cli_state *self, PyObject *args)
2367 {
2368         PyObject *py_sd = NULL;
2369         struct tevent_req *req = NULL;
2370         struct security_descriptor *sd = NULL;
2371         uint16_t fnum;
2372         unsigned int sinfo;
2373         NTSTATUS status;
2374
2375         if (!PyArg_ParseTuple(args, "iOI:set_sd", &fnum, &py_sd, &sinfo)) {
2376                 return NULL;
2377         }
2378
2379         sd = pytalloc_get_type(py_sd, struct security_descriptor);
2380         if (!sd) {
2381                 PyErr_Format(PyExc_TypeError,
2382                         "Expected dcerpc.security.descriptor as argument, got %s",
2383                         pytalloc_get_name(py_sd));
2384                 return NULL;
2385         }
2386
2387         req = cli_set_security_descriptor_send(
2388                 NULL, self->ev, self->cli, fnum, sinfo, sd);
2389         if (!py_tevent_req_wait_exc(self, req)) {
2390                 return NULL;
2391         }
2392
2393         status = cli_set_security_descriptor_recv(req);
2394         PyErr_NTSTATUS_NOT_OK_RAISE(status);
2395
2396         Py_RETURN_NONE;
2397 }
2398
2399 static PyObject *py_smb_smb1_posix(
2400         struct py_cli_state *self, PyObject *Py_UNUSED(ignored))
2401 {
2402         NTSTATUS status;
2403         struct tevent_req *req = NULL;
2404         uint16_t major, minor;
2405         uint32_t caplow, caphigh;
2406         PyObject *result = NULL;
2407
2408         req = cli_unix_extensions_version_send(NULL, self->ev, self->cli);
2409         if (!py_tevent_req_wait_exc(self, req)) {
2410                 return NULL;
2411         }
2412         status = cli_unix_extensions_version_recv(
2413                 req, &major, &minor, &caplow, &caphigh);
2414         TALLOC_FREE(req);
2415         if (!NT_STATUS_IS_OK(status)) {
2416                 PyErr_SetNTSTATUS(status);
2417                 return NULL;
2418         }
2419
2420         req = cli_set_unix_extensions_capabilities_send(
2421                 NULL, self->ev, self->cli, major, minor, caplow, caphigh);
2422         if (!py_tevent_req_wait_exc(self, req)) {
2423                 return NULL;
2424         }
2425         status = cli_set_unix_extensions_capabilities_recv(req);
2426         TALLOC_FREE(req);
2427         if (!NT_STATUS_IS_OK(status)) {
2428                 PyErr_SetNTSTATUS(status);
2429                 return NULL;
2430         }
2431
2432         result = Py_BuildValue(
2433                 "[IIII]",
2434                 (unsigned)minor,
2435                 (unsigned)major,
2436                 (unsigned)caplow,
2437                 (unsigned)caphigh);
2438         return result;
2439 }
2440
2441 static PyObject *py_smb_smb1_readlink(
2442         struct py_cli_state *self, PyObject *args)
2443 {
2444         NTSTATUS status;
2445         const char *filename = NULL;
2446         struct tevent_req *req = NULL;
2447         char *target = NULL;
2448         PyObject *result = NULL;
2449
2450         if (!PyArg_ParseTuple(args, "s:smb1_readlink", &filename)) {
2451                 return NULL;
2452         }
2453
2454         req = cli_posix_readlink_send(NULL, self->ev, self->cli, filename);
2455         if (!py_tevent_req_wait_exc(self, req)) {
2456                 return NULL;
2457         }
2458         status = cli_posix_readlink_recv(req, NULL, &target);
2459         TALLOC_FREE(req);
2460         if (!NT_STATUS_IS_OK(status)) {
2461                 PyErr_SetNTSTATUS(status);
2462                 return NULL;
2463         }
2464
2465         result = PyBytes_FromString(target);
2466         TALLOC_FREE(target);
2467         return result;
2468 }
2469
2470 static PyObject *py_smb_smb1_symlink(
2471         struct py_cli_state *self, PyObject *args)
2472 {
2473         NTSTATUS status;
2474         const char *target = NULL, *newname = NULL;
2475         struct tevent_req *req = NULL;
2476
2477         if (!PyArg_ParseTuple(args, "ss:smb1_symlink", &target, &newname)) {
2478                 return NULL;
2479         }
2480
2481         req = cli_posix_symlink_send(
2482                 NULL, self->ev, self->cli, target, newname);
2483         if (!py_tevent_req_wait_exc(self, req)) {
2484                 return NULL;
2485         }
2486         status = cli_posix_symlink_recv(req);
2487         TALLOC_FREE(req);
2488         if (!NT_STATUS_IS_OK(status)) {
2489                 PyErr_SetNTSTATUS(status);
2490                 return NULL;
2491         }
2492
2493         Py_RETURN_NONE;
2494 }
2495
2496 static PyObject *py_smb_smb1_stat(
2497         struct py_cli_state *self, PyObject *args)
2498 {
2499         NTSTATUS status;
2500         const char *fname = NULL;
2501         struct tevent_req *req = NULL;
2502         struct stat_ex sbuf = { .st_ex_nlink = 0, };
2503
2504         if (!PyArg_ParseTuple(args, "s:smb1_stat", &fname)) {
2505                 return NULL;
2506         }
2507
2508         req = cli_posix_stat_send(NULL, self->ev, self->cli, fname);
2509         if (!py_tevent_req_wait_exc(self, req)) {
2510                 return NULL;
2511         }
2512         status = cli_posix_stat_recv(req, &sbuf);
2513         TALLOC_FREE(req);
2514         if (!NT_STATUS_IS_OK(status)) {
2515                 PyErr_SetNTSTATUS(status);
2516                 return NULL;
2517         }
2518
2519         return Py_BuildValue(
2520                 "{sLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsL}",
2521                 "dev",
2522                 (unsigned long long)sbuf.st_ex_dev,
2523                 "ino",
2524                 (unsigned long long)sbuf.st_ex_ino,
2525                 "mode",
2526                 (unsigned long long)sbuf.st_ex_mode,
2527                 "nlink",
2528                 (unsigned long long)sbuf.st_ex_nlink,
2529                 "uid",
2530                 (unsigned long long)sbuf.st_ex_uid,
2531                 "gid",
2532                 (unsigned long long)sbuf.st_ex_gid,
2533                 "rdev",
2534                 (unsigned long long)sbuf.st_ex_size,
2535                 "atime_sec",
2536                 (unsigned long long)sbuf.st_ex_atime.tv_sec,
2537                 "atime_nsec",
2538                 (unsigned long long)sbuf.st_ex_atime.tv_nsec,
2539                 "mtime_sec",
2540                 (unsigned long long)sbuf.st_ex_mtime.tv_sec,
2541                 "mtime_nsec",
2542                 (unsigned long long)sbuf.st_ex_mtime.tv_nsec,
2543                 "ctime_sec",
2544                 (unsigned long long)sbuf.st_ex_ctime.tv_sec,
2545                 "ctime_nsec",
2546                 (unsigned long long)sbuf.st_ex_ctime.tv_nsec,
2547                 "btime_sec",
2548                 (unsigned long long)sbuf.st_ex_btime.tv_sec,
2549                 "btime_nsec",
2550                 (unsigned long long)sbuf.st_ex_btime.tv_nsec,
2551                 "cached_dos_attributes",
2552                 (unsigned long long)sbuf.cached_dos_attributes,
2553                 "blksize",
2554                 (unsigned long long)sbuf.st_ex_blksize,
2555                 "blocks",
2556                 (unsigned long long)sbuf.st_ex_blocks,
2557                 "flags",
2558                 (unsigned long long)sbuf.st_ex_flags,
2559                 "iflags",
2560                 (unsigned long long)sbuf.st_ex_iflags);
2561 }
2562
2563 static PyObject *py_cli_mknod(
2564         struct py_cli_state *self, PyObject *args, PyObject *kwds)
2565 {
2566         char *fname = NULL;
2567         int mode = 0, major = 0, minor = 0, dev = 0;
2568         struct tevent_req *req = NULL;
2569         static const char *kwlist[] = {
2570                 "fname", "mode", "major", "minor", NULL,
2571         };
2572         NTSTATUS status;
2573         bool ok;
2574
2575         ok = ParseTupleAndKeywords(
2576                 args,
2577                 kwds,
2578                 "sI|II:mknod",
2579                 kwlist,
2580                 &fname,
2581                 &mode,
2582                 &major,
2583                 &minor);
2584         if (!ok) {
2585                 return NULL;
2586         }
2587
2588 #if defined(HAVE_MAKEDEV)
2589         dev = makedev(major, minor);
2590 #endif
2591
2592         req = cli_mknod_send(
2593                 NULL, self->ev, self->cli, fname, mode, dev);
2594         if (!py_tevent_req_wait_exc(self, req)) {
2595                 return NULL;
2596         }
2597         status = cli_mknod_recv(req);
2598         TALLOC_FREE(req);
2599         if (!NT_STATUS_IS_OK(status)) {
2600                 PyErr_SetNTSTATUS(status);
2601                 return NULL;
2602         }
2603         Py_RETURN_NONE;
2604 }
2605
2606 static PyObject *py_cli_fsctl(
2607         struct py_cli_state *self, PyObject *args, PyObject *kwds)
2608 {
2609         int fnum, ctl_code;
2610         int max_out = 0;
2611         char *buf = NULL;
2612         Py_ssize_t buflen;
2613         DATA_BLOB in = { .data = NULL, };
2614         DATA_BLOB out = { .data = NULL, };
2615         struct tevent_req *req = NULL;
2616         PyObject *result = NULL;
2617         static const char *kwlist[] = {
2618                 "fnum", "ctl_code", "in", "max_out", NULL,
2619         };
2620         NTSTATUS status;
2621         bool ok;
2622
2623         ok = ParseTupleAndKeywords(
2624                     args,
2625                     kwds,
2626                     "ii" PYARG_BYTES_LEN "i",
2627                     kwlist,
2628                     &fnum,
2629                     &ctl_code,
2630                     &buf,
2631                     &buflen,
2632                     &max_out);
2633         if (!ok) {
2634                 return NULL;
2635         }
2636
2637         in = (DATA_BLOB) { .data = (uint8_t *)buf, .length = buflen, };
2638
2639         req = cli_fsctl_send(
2640                 NULL, self->ev, self->cli, fnum, ctl_code, &in, max_out);
2641
2642         if (!py_tevent_req_wait_exc(self, req)) {
2643                 return NULL;
2644         }
2645
2646         status = cli_fsctl_recv(req, NULL, &out);
2647         if (!NT_STATUS_IS_OK(status)) {
2648                 PyErr_SetNTSTATUS(status);
2649                 return NULL;
2650         }
2651
2652         result = PyBytes_FromStringAndSize((char *)out.data, out.length);
2653         data_blob_free(&out);
2654         return result;
2655 }
2656
2657 static PyMethodDef py_cli_state_methods[] = {
2658         { "settimeout", (PyCFunction)py_cli_settimeout, METH_VARARGS,
2659           "settimeout(new_timeout_msecs) => return old_timeout_msecs" },
2660         { "echo", (PyCFunction)py_cli_echo, METH_NOARGS,
2661           "Ping the server connection" },
2662         { "create", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_create),
2663                 METH_VARARGS|METH_KEYWORDS,
2664           "Open a file" },
2665         { "create_ex",
2666           PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_create_ex),
2667           METH_VARARGS|METH_KEYWORDS,
2668           "Open a file, SMB2 version returning create contexts" },
2669         { "close", (PyCFunction)py_cli_close, METH_VARARGS,
2670           "Close a file handle" },
2671         { "write", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_write),
2672                 METH_VARARGS|METH_KEYWORDS,
2673           "Write to a file handle" },
2674         { "read", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_read),
2675                 METH_VARARGS|METH_KEYWORDS,
2676           "Read from a file handle" },
2677         { "truncate", PY_DISCARD_FUNC_SIG(PyCFunction,
2678                         py_cli_ftruncate),
2679           METH_VARARGS|METH_KEYWORDS,
2680           "Truncate a file" },
2681         { "delete_on_close", PY_DISCARD_FUNC_SIG(PyCFunction,
2682                                          py_cli_delete_on_close),
2683           METH_VARARGS|METH_KEYWORDS,
2684           "Set/Reset the delete on close flag" },
2685         { "notify", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_notify),
2686           METH_VARARGS|METH_KEYWORDS,
2687           "Wait for change notifications: \n"
2688           "notify(fnum, buffer_size, completion_filter...) -> "
2689           "libsmb_samba_internal.Notify request handle\n" },
2690         { "list", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_list),
2691                 METH_VARARGS|METH_KEYWORDS,
2692           "list(directory, mask='*', attribs=DEFAULT_ATTRS) -> "
2693           "directory contents as a dictionary\n"
2694           "\t\tDEFAULT_ATTRS: FILE_ATTRIBUTE_SYSTEM | "
2695           "FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_ARCHIVE\n\n"
2696           "\t\tList contents of a directory. The keys are, \n"
2697           "\t\t\tname: Long name of the directory item\n"
2698           "\t\t\tshort_name: Short name of the directory item\n"
2699           "\t\t\tsize: File size in bytes\n"
2700           "\t\t\tattrib: Attributes\n"
2701           "\t\t\tmtime: Modification time\n" },
2702         { "get_oplock_break", (PyCFunction)py_cli_get_oplock_break,
2703           METH_VARARGS, "Wait for an oplock break" },
2704         { "unlink", (PyCFunction)py_smb_unlink,
2705           METH_VARARGS,
2706           "unlink(path) -> None\n\n \t\tDelete a file." },
2707         { "mkdir", (PyCFunction)py_smb_mkdir, METH_VARARGS,
2708           "mkdir(path) -> None\n\n \t\tCreate a directory." },
2709         { "posix_whoami", (PyCFunction)py_smb_posix_whoami, METH_NOARGS,
2710         "posix_whoami() -> (uid, gid, gids, sids, guest)" },
2711         { "rmdir", (PyCFunction)py_smb_rmdir, METH_VARARGS,
2712           "rmdir(path) -> None\n\n \t\tDelete a directory." },
2713         { "rename",
2714           PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_rename),
2715           METH_VARARGS|METH_KEYWORDS,
2716           "rename(src,dst) -> None\n\n \t\tRename a file." },
2717         { "chkpath", (PyCFunction)py_smb_chkpath, METH_VARARGS,
2718           "chkpath(dir_path) -> True or False\n\n"
2719           "\t\tReturn true if directory exists, false otherwise." },
2720         { "savefile", (PyCFunction)py_smb_savefile, METH_VARARGS,
2721           "savefile(path, bytes) -> None\n\n"
2722           "\t\tWrite bytes to file." },
2723         { "loadfile", (PyCFunction)py_smb_loadfile, METH_VARARGS,
2724           "loadfile(path) -> file contents as a bytes object"
2725           "\n\n\t\tRead contents of a file." },
2726         { "get_sd", (PyCFunction)py_smb_get_sd, METH_VARARGS,
2727           "get_sd(fnum[, security_info=0]) -> security_descriptor object\n\n"
2728           "\t\tGet security descriptor for opened file." },
2729         { "set_sd", (PyCFunction)py_smb_set_sd, METH_VARARGS,
2730           "set_sd(fnum, security_descriptor[, security_info=0]) -> None\n\n"
2731           "\t\tSet security descriptor for opened file." },
2732         { "protocol",
2733           (PyCFunction)py_smb_protocol,
2734           METH_NOARGS,
2735           "protocol() -> Number"
2736         },
2737         { "have_posix",
2738           (PyCFunction)py_smb_have_posix,
2739           METH_NOARGS,
2740           "have_posix() -> True/False\n\n"
2741           "\t\tReturn if the server has posix extensions"
2742         },
2743         { "smb1_posix",
2744           (PyCFunction)py_smb_smb1_posix,
2745           METH_NOARGS,
2746           "Negotiate SMB1 posix extensions",
2747         },
2748         { "smb1_readlink",
2749           (PyCFunction)py_smb_smb1_readlink,
2750           METH_VARARGS,
2751           "smb1_readlink(path) -> link target",
2752         },
2753         { "smb1_symlink",
2754           (PyCFunction)py_smb_smb1_symlink,
2755           METH_VARARGS,
2756           "smb1_symlink(target, newname) -> None",
2757         },
2758         { "smb1_stat",
2759           (PyCFunction)py_smb_smb1_stat,
2760           METH_VARARGS,
2761           "smb1_stat(path) -> stat info",
2762         },
2763         { "fsctl",
2764           (PyCFunction)py_cli_fsctl,
2765           METH_VARARGS|METH_KEYWORDS,
2766           "fsctl(fnum, ctl_code, in_bytes, max_out) -> out_bytes",
2767         },
2768         { "mknod",
2769           PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_mknod),
2770           METH_VARARGS|METH_KEYWORDS,
2771           "mknod(path, mode | major, minor)",
2772         },
2773         { NULL, NULL, 0, NULL }
2774 };
2775
2776 static PyTypeObject py_cli_state_type = {
2777         PyVarObject_HEAD_INIT(NULL, 0)
2778         .tp_name = "libsmb_samba_cwrapper.LibsmbCConn",
2779         .tp_basicsize = sizeof(struct py_cli_state),
2780         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
2781         .tp_doc = "libsmb cwrapper connection",
2782         .tp_new = py_cli_state_new,
2783         .tp_init = (initproc)py_cli_state_init,
2784         .tp_dealloc = (destructor)py_cli_state_dealloc,
2785         .tp_methods = py_cli_state_methods,
2786 };
2787
2788 static PyMethodDef py_libsmb_methods[] = {
2789         {0},
2790 };
2791
2792 void initlibsmb_samba_cwrapper(void);
2793
2794 static struct PyModuleDef moduledef = {
2795     PyModuleDef_HEAD_INIT,
2796     .m_name = "libsmb_samba_cwrapper",
2797     .m_doc = "libsmb wrapper",
2798     .m_size = -1,
2799     .m_methods = py_libsmb_methods,
2800 };
2801
2802 MODULE_INIT_FUNC(libsmb_samba_cwrapper)
2803 {
2804         PyObject *m = NULL;
2805         PyObject *mod = NULL;
2806
2807         talloc_stackframe();
2808
2809         if (PyType_Ready(&py_cli_state_type) < 0) {
2810                 return NULL;
2811         }
2812         if (PyType_Ready(&py_cli_notify_state_type) < 0) {
2813                 return NULL;
2814         }
2815
2816         m = PyModule_Create(&moduledef);
2817         if (m == NULL) {
2818                 return m;
2819         }
2820
2821         /* Import dom_sid type from dcerpc.security */
2822         mod = PyImport_ImportModule("samba.dcerpc.security");
2823         if (mod == NULL) {
2824                 return NULL;
2825         }
2826
2827         dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
2828         if (dom_sid_Type == NULL) {
2829                 Py_DECREF(mod);
2830                 return NULL;
2831         }
2832
2833         Py_INCREF(&py_cli_state_type);
2834         PyModule_AddObject(m, "LibsmbCConn", (PyObject *)&py_cli_state_type);
2835
2836 #define ADD_FLAGS(val)  PyModule_AddObject(m, #val, PyLong_FromLong(val))
2837
2838         ADD_FLAGS(PROTOCOL_NONE);
2839         ADD_FLAGS(PROTOCOL_CORE);
2840         ADD_FLAGS(PROTOCOL_COREPLUS);
2841         ADD_FLAGS(PROTOCOL_LANMAN1);
2842         ADD_FLAGS(PROTOCOL_LANMAN2);
2843         ADD_FLAGS(PROTOCOL_NT1);
2844         ADD_FLAGS(PROTOCOL_SMB2_02);
2845         ADD_FLAGS(PROTOCOL_SMB2_10);
2846         ADD_FLAGS(PROTOCOL_SMB3_00);
2847         ADD_FLAGS(PROTOCOL_SMB3_02);
2848         ADD_FLAGS(PROTOCOL_SMB3_11);
2849
2850         ADD_FLAGS(FILE_ATTRIBUTE_READONLY);
2851         ADD_FLAGS(FILE_ATTRIBUTE_HIDDEN);
2852         ADD_FLAGS(FILE_ATTRIBUTE_SYSTEM);
2853         ADD_FLAGS(FILE_ATTRIBUTE_VOLUME);
2854         ADD_FLAGS(FILE_ATTRIBUTE_DIRECTORY);
2855         ADD_FLAGS(FILE_ATTRIBUTE_ARCHIVE);
2856         ADD_FLAGS(FILE_ATTRIBUTE_DEVICE);
2857         ADD_FLAGS(FILE_ATTRIBUTE_NORMAL);
2858         ADD_FLAGS(FILE_ATTRIBUTE_TEMPORARY);
2859         ADD_FLAGS(FILE_ATTRIBUTE_SPARSE);
2860         ADD_FLAGS(FILE_ATTRIBUTE_REPARSE_POINT);
2861         ADD_FLAGS(FILE_ATTRIBUTE_COMPRESSED);
2862         ADD_FLAGS(FILE_ATTRIBUTE_OFFLINE);
2863         ADD_FLAGS(FILE_ATTRIBUTE_NONINDEXED);
2864         ADD_FLAGS(FILE_ATTRIBUTE_ENCRYPTED);
2865         ADD_FLAGS(FILE_ATTRIBUTE_ALL_MASK);
2866
2867         ADD_FLAGS(FILE_DIRECTORY_FILE);
2868         ADD_FLAGS(FILE_WRITE_THROUGH);
2869         ADD_FLAGS(FILE_SEQUENTIAL_ONLY);
2870         ADD_FLAGS(FILE_NO_INTERMEDIATE_BUFFERING);
2871         ADD_FLAGS(FILE_SYNCHRONOUS_IO_ALERT);
2872         ADD_FLAGS(FILE_SYNCHRONOUS_IO_NONALERT);
2873         ADD_FLAGS(FILE_NON_DIRECTORY_FILE);
2874         ADD_FLAGS(FILE_CREATE_TREE_CONNECTION);
2875         ADD_FLAGS(FILE_COMPLETE_IF_OPLOCKED);
2876         ADD_FLAGS(FILE_NO_EA_KNOWLEDGE);
2877         ADD_FLAGS(FILE_EIGHT_DOT_THREE_ONLY);
2878         ADD_FLAGS(FILE_RANDOM_ACCESS);
2879         ADD_FLAGS(FILE_DELETE_ON_CLOSE);
2880         ADD_FLAGS(FILE_OPEN_BY_FILE_ID);
2881         ADD_FLAGS(FILE_OPEN_FOR_BACKUP_INTENT);
2882         ADD_FLAGS(FILE_NO_COMPRESSION);
2883         ADD_FLAGS(FILE_RESERVER_OPFILTER);
2884         ADD_FLAGS(FILE_OPEN_REPARSE_POINT);
2885         ADD_FLAGS(FILE_OPEN_NO_RECALL);
2886         ADD_FLAGS(FILE_OPEN_FOR_FREE_SPACE_QUERY);
2887
2888         ADD_FLAGS(FILE_SHARE_READ);
2889         ADD_FLAGS(FILE_SHARE_WRITE);
2890         ADD_FLAGS(FILE_SHARE_DELETE);
2891
2892         /* change notify completion filter flags */
2893         ADD_FLAGS(FILE_NOTIFY_CHANGE_FILE_NAME);
2894         ADD_FLAGS(FILE_NOTIFY_CHANGE_DIR_NAME);
2895         ADD_FLAGS(FILE_NOTIFY_CHANGE_ATTRIBUTES);
2896         ADD_FLAGS(FILE_NOTIFY_CHANGE_SIZE);
2897         ADD_FLAGS(FILE_NOTIFY_CHANGE_LAST_WRITE);
2898         ADD_FLAGS(FILE_NOTIFY_CHANGE_LAST_ACCESS);
2899         ADD_FLAGS(FILE_NOTIFY_CHANGE_CREATION);
2900         ADD_FLAGS(FILE_NOTIFY_CHANGE_EA);
2901         ADD_FLAGS(FILE_NOTIFY_CHANGE_SECURITY);
2902         ADD_FLAGS(FILE_NOTIFY_CHANGE_STREAM_NAME);
2903         ADD_FLAGS(FILE_NOTIFY_CHANGE_STREAM_SIZE);
2904         ADD_FLAGS(FILE_NOTIFY_CHANGE_STREAM_WRITE);
2905         ADD_FLAGS(FILE_NOTIFY_CHANGE_NAME);
2906         ADD_FLAGS(FILE_NOTIFY_CHANGE_ALL);
2907
2908         /* change notify action results */
2909         ADD_FLAGS(NOTIFY_ACTION_ADDED);
2910         ADD_FLAGS(NOTIFY_ACTION_REMOVED);
2911         ADD_FLAGS(NOTIFY_ACTION_MODIFIED);
2912         ADD_FLAGS(NOTIFY_ACTION_OLD_NAME);
2913         ADD_FLAGS(NOTIFY_ACTION_NEW_NAME);
2914         ADD_FLAGS(NOTIFY_ACTION_ADDED_STREAM);
2915         ADD_FLAGS(NOTIFY_ACTION_REMOVED_STREAM);
2916         ADD_FLAGS(NOTIFY_ACTION_MODIFIED_STREAM);
2917
2918         /* CreateDisposition values */
2919         ADD_FLAGS(FILE_SUPERSEDE);
2920         ADD_FLAGS(FILE_OPEN);
2921         ADD_FLAGS(FILE_CREATE);
2922         ADD_FLAGS(FILE_OPEN_IF);
2923         ADD_FLAGS(FILE_OVERWRITE);
2924         ADD_FLAGS(FILE_OVERWRITE_IF);
2925
2926         ADD_FLAGS(FSCTL_DFS_GET_REFERRALS);
2927         ADD_FLAGS(FSCTL_DFS_GET_REFERRALS_EX);
2928         ADD_FLAGS(FSCTL_REQUEST_OPLOCK_LEVEL_1);
2929         ADD_FLAGS(FSCTL_REQUEST_OPLOCK_LEVEL_2);
2930         ADD_FLAGS(FSCTL_REQUEST_BATCH_OPLOCK);
2931         ADD_FLAGS(FSCTL_OPLOCK_BREAK_ACKNOWLEDGE);
2932         ADD_FLAGS(FSCTL_OPBATCH_ACK_CLOSE_PENDING);
2933         ADD_FLAGS(FSCTL_OPLOCK_BREAK_NOTIFY);
2934         ADD_FLAGS(FSCTL_GET_COMPRESSION);
2935         ADD_FLAGS(FSCTL_FILESYS_GET_STATISTICS);
2936         ADD_FLAGS(FSCTL_GET_NTFS_VOLUME_DATA);
2937         ADD_FLAGS(FSCTL_IS_VOLUME_DIRTY);
2938         ADD_FLAGS(FSCTL_FIND_FILES_BY_SID);
2939         ADD_FLAGS(FSCTL_SET_OBJECT_ID);
2940         ADD_FLAGS(FSCTL_GET_OBJECT_ID);
2941         ADD_FLAGS(FSCTL_DELETE_OBJECT_ID);
2942         ADD_FLAGS(FSCTL_SET_REPARSE_POINT);
2943         ADD_FLAGS(FSCTL_GET_REPARSE_POINT);
2944         ADD_FLAGS(FSCTL_DELETE_REPARSE_POINT);
2945         ADD_FLAGS(FSCTL_SET_OBJECT_ID_EXTENDED);
2946         ADD_FLAGS(FSCTL_CREATE_OR_GET_OBJECT_ID);
2947         ADD_FLAGS(FSCTL_SET_SPARSE);
2948         ADD_FLAGS(FSCTL_SET_ZERO_DATA);
2949         ADD_FLAGS(FSCTL_SET_ZERO_ON_DEALLOCATION);
2950         ADD_FLAGS(FSCTL_READ_FILE_USN_DATA);
2951         ADD_FLAGS(FSCTL_WRITE_USN_CLOSE_RECORD);
2952         ADD_FLAGS(FSCTL_QUERY_ALLOCATED_RANGES);
2953         ADD_FLAGS(FSCTL_QUERY_ON_DISK_VOLUME_INFO);
2954         ADD_FLAGS(FSCTL_QUERY_SPARING_INFO);
2955         ADD_FLAGS(FSCTL_FILE_LEVEL_TRIM);
2956         ADD_FLAGS(FSCTL_OFFLOAD_READ);
2957         ADD_FLAGS(FSCTL_OFFLOAD_WRITE);
2958         ADD_FLAGS(FSCTL_SET_INTEGRITY_INFORMATION);
2959         ADD_FLAGS(FSCTL_DUP_EXTENTS_TO_FILE);
2960         ADD_FLAGS(FSCTL_DUPLICATE_EXTENTS_TO_FILE_EX);
2961         ADD_FLAGS(FSCTL_STORAGE_QOS_CONTROL);
2962         ADD_FLAGS(FSCTL_SVHDX_SYNC_TUNNEL_REQUEST);
2963         ADD_FLAGS(FSCTL_QUERY_SHARED_VIRTUAL_DISK_SUPPORT);
2964         ADD_FLAGS(FSCTL_PIPE_PEEK);
2965         ADD_FLAGS(FSCTL_NAMED_PIPE_READ_WRITE);
2966         ADD_FLAGS(FSCTL_PIPE_TRANSCEIVE);
2967         ADD_FLAGS(FSCTL_PIPE_WAIT);
2968         ADD_FLAGS(FSCTL_GET_SHADOW_COPY_DATA);
2969         ADD_FLAGS(FSCTL_SRV_ENUM_SNAPS);
2970         ADD_FLAGS(FSCTL_SRV_REQUEST_RESUME_KEY);
2971         ADD_FLAGS(FSCTL_SRV_COPYCHUNK);
2972         ADD_FLAGS(FSCTL_SRV_COPYCHUNK_WRITE);
2973         ADD_FLAGS(FSCTL_SRV_READ_HASH);
2974         ADD_FLAGS(FSCTL_LMR_REQ_RESILIENCY);
2975         ADD_FLAGS(FSCTL_LMR_SET_LINK_TRACKING_INFORMATION);
2976         ADD_FLAGS(FSCTL_QUERY_NETWORK_INTERFACE_INFO);
2977
2978         ADD_FLAGS(SYMLINK_ERROR_TAG);
2979         ADD_FLAGS(SYMLINK_FLAG_RELATIVE);
2980         ADD_FLAGS(SYMLINK_ADMIN);
2981         ADD_FLAGS(SYMLINK_UNTRUSTED);
2982         ADD_FLAGS(SYMLINK_TRUST_UNKNOWN);
2983         ADD_FLAGS(SYMLINK_TRUST_MASK);
2984
2985         ADD_FLAGS(IO_REPARSE_TAG_RESERVED_ZERO);
2986         ADD_FLAGS(IO_REPARSE_TAG_SYMLINK);
2987         ADD_FLAGS(IO_REPARSE_TAG_MOUNT_POINT);
2988         ADD_FLAGS(IO_REPARSE_TAG_HSM);
2989         ADD_FLAGS(IO_REPARSE_TAG_SIS);
2990         ADD_FLAGS(IO_REPARSE_TAG_DFS);
2991         ADD_FLAGS(IO_REPARSE_TAG_NFS);
2992
2993 #define ADD_STRING(val) PyModule_AddObject(m, #val, PyBytes_FromString(val))
2994
2995         ADD_STRING(SMB2_CREATE_TAG_EXTA);
2996         ADD_STRING(SMB2_CREATE_TAG_MXAC);
2997         ADD_STRING(SMB2_CREATE_TAG_SECD);
2998         ADD_STRING(SMB2_CREATE_TAG_DHNQ);
2999         ADD_STRING(SMB2_CREATE_TAG_DHNC);
3000         ADD_STRING(SMB2_CREATE_TAG_ALSI);
3001         ADD_STRING(SMB2_CREATE_TAG_TWRP);
3002         ADD_STRING(SMB2_CREATE_TAG_QFID);
3003         ADD_STRING(SMB2_CREATE_TAG_RQLS);
3004         ADD_STRING(SMB2_CREATE_TAG_DH2Q);
3005         ADD_STRING(SMB2_CREATE_TAG_DH2C);
3006         ADD_STRING(SMB2_CREATE_TAG_AAPL);
3007         ADD_STRING(SMB2_CREATE_TAG_APP_INSTANCE_ID);
3008         ADD_STRING(SVHDX_OPEN_DEVICE_CONTEXT);
3009         ADD_STRING(SMB2_CREATE_TAG_POSIX);
3010         ADD_FLAGS(SMB2_FIND_POSIX_INFORMATION);
3011         ADD_FLAGS(FILE_SUPERSEDE);
3012         ADD_FLAGS(FILE_OPEN);
3013         ADD_FLAGS(FILE_CREATE);
3014         ADD_FLAGS(FILE_OPEN_IF);
3015         ADD_FLAGS(FILE_OVERWRITE);
3016         ADD_FLAGS(FILE_OVERWRITE_IF);
3017         ADD_FLAGS(FILE_DIRECTORY_FILE);
3018
3019         ADD_FLAGS(SMB2_CLOSE_FLAGS_FULL_INFORMATION);
3020
3021         return m;
3022 }