libsmb: info-level SMB2_FIND_POSIX_INFORMATION doesn't return short name
[Samba.git] / source3 / libsmb / pylibsmb.c
blob00f7e010cf76a0170bd101d079cf72419ea6f993
1 /*
2 * Unix SMB/CIFS implementation.
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.
8 * Copyright (C) Volker Lendecke 2012
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.
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.
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/>.
25 Template code to use this library:
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)
32 lp = s3param.get_context()
33 lp.load("/etc/samba/smb.conf");
35 creds = credentials.Credentials()
36 creds.guess(lp)
37 creds.set_username("administrator")
38 creds.set_password("1234")
40 c = libsmb.Conn("127.0.0.1",
41 "tmp",
42 lp,
43 creds,
44 multi_threaded=True)
45 -------------------------
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"
64 #define LIST_ATTRIBUTE_MASK \
65 (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN)
67 static PyTypeObject *dom_sid_Type = NULL;
69 static PyTypeObject *get_pytype(const char *module, const char *type)
71 PyObject *mod;
72 PyTypeObject *result;
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;
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;
89 return result;
93 * We're using "const char * const *" for keywords,
94 * PyArg_ParseTupleAndKeywords expects a "char **". Confine the
95 * inevitable warnings to just one place.
97 static int ParseTupleAndKeywords(PyObject *args, PyObject *kw,
98 const char *format, const char * const *keywords,
99 ...)
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;
111 struct py_cli_thread;
113 struct py_cli_oplock_break {
114 uint16_t fnum;
115 uint8_t level;
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;
126 struct tevent_req *oplock_waiter;
127 struct py_cli_oplock_break *oplock_breaks;
128 struct py_tevent_cond *oplock_cond;
131 #ifdef HAVE_PTHREAD
133 #include <pthread.h>
135 struct py_cli_thread {
138 * Pipe to make the poll thread wake up in our destructor, so
139 * that we can exit and join the thread.
141 int shutdown_pipe[2];
142 struct tevent_fd *shutdown_fde;
143 bool do_shutdown;
144 pthread_t id;
147 * Thread state to release the GIL during the poll(2) syscall
149 PyThreadState *py_threadstate;
152 static void *py_cli_state_poll_thread(void *private_data)
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;
158 gstate = PyGILState_Ensure();
160 while (!t->do_shutdown) {
161 int ret;
162 ret = tevent_loop_once(self->ev);
163 assert(ret == 0);
165 PyGILState_Release(gstate);
166 return NULL;
169 static void py_cli_state_trace_callback(enum tevent_trace_point point,
170 void *private_data)
172 struct py_cli_state *self = (struct py_cli_state *)private_data;
173 struct py_cli_thread *t = self->thread_state;
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;
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)
195 struct py_cli_state *self = (struct py_cli_state *)private_data;
196 struct py_cli_thread *t = self->thread_state;
198 if ((flags & TEVENT_FD_READ) == 0) {
199 return;
201 TALLOC_FREE(t->shutdown_fde);
202 t->do_shutdown = true;
205 static int py_cli_thread_destructor(struct py_cli_thread *t)
207 char c = 0;
208 ssize_t written;
209 int ret;
211 do {
213 * This will wake the poll thread from the poll(2)
215 written = write(t->shutdown_pipe[1], &c, 1);
216 } while ((written == -1) && (errno == EINTR));
219 * Allow the poll thread to do its own cleanup under the GIL
221 Py_BEGIN_ALLOW_THREADS
222 ret = pthread_join(t->id, NULL);
223 Py_END_ALLOW_THREADS
224 assert(ret == 0);
226 if (t->shutdown_pipe[0] != -1) {
227 close(t->shutdown_pipe[0]);
228 t->shutdown_pipe[0] = -1;
230 if (t->shutdown_pipe[1] != -1) {
231 close(t->shutdown_pipe[1]);
232 t->shutdown_pipe[1] = -1;
234 return 0;
237 static int py_tevent_cond_req_wait(struct tevent_context *ev,
238 struct tevent_req *req);
240 static bool py_cli_state_setup_mt_ev(struct py_cli_state *self)
242 struct py_cli_thread *t = NULL;
243 int ret;
245 self->ev = tevent_context_init_byname(NULL, "poll_mt");
246 if (self->ev == NULL) {
247 goto fail;
249 samba_tevent_set_debug(self->ev, "pylibsmb_tevent_mt");
250 tevent_set_trace_callback(self->ev, py_cli_state_trace_callback, self);
252 self->req_wait_fn = py_tevent_cond_req_wait;
254 self->thread_state = talloc_zero(NULL, struct py_cli_thread);
255 if (self->thread_state == NULL) {
256 goto fail;
258 t = self->thread_state;
260 ret = pipe(t->shutdown_pipe);
261 if (ret == -1) {
262 goto fail;
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;
271 PyEval_InitThreads();
273 ret = pthread_create(&t->id, NULL, py_cli_state_poll_thread, self);
274 if (ret != 0) {
275 goto fail;
277 talloc_set_destructor(self->thread_state, py_cli_thread_destructor);
278 return true;
280 fail:
281 if (t != NULL) {
282 TALLOC_FREE(t->shutdown_fde);
284 if (t->shutdown_pipe[0] != -1) {
285 close(t->shutdown_pipe[0]);
286 t->shutdown_pipe[0] = -1;
288 if (t->shutdown_pipe[1] != -1) {
289 close(t->shutdown_pipe[1]);
290 t->shutdown_pipe[1] = -1;
294 TALLOC_FREE(self->thread_state);
295 TALLOC_FREE(self->ev);
296 return false;
299 struct py_tevent_cond {
300 pthread_mutex_t mutex;
301 pthread_cond_t cond;
302 bool is_done;
305 static void py_tevent_signalme(struct tevent_req *req);
307 static int py_tevent_cond_wait(struct py_tevent_cond *cond)
309 int ret, result;
311 result = pthread_mutex_init(&cond->mutex, NULL);
312 if (result != 0) {
313 goto fail;
315 result = pthread_cond_init(&cond->cond, NULL);
316 if (result != 0) {
317 goto fail_mutex;
320 result = pthread_mutex_lock(&cond->mutex);
321 if (result != 0) {
322 goto fail_cond;
325 cond->is_done = false;
327 while (!cond->is_done) {
329 Py_BEGIN_ALLOW_THREADS
330 result = pthread_cond_wait(&cond->cond, &cond->mutex);
331 Py_END_ALLOW_THREADS
333 if (result != 0) {
334 goto fail_unlock;
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;
351 static int py_tevent_cond_req_wait(struct tevent_context *ev,
352 struct tevent_req *req)
354 struct py_tevent_cond cond;
355 tevent_req_set_callback(req, py_tevent_signalme, &cond);
356 return py_tevent_cond_wait(&cond);
359 static void py_tevent_cond_signal(struct py_tevent_cond *cond)
361 int ret;
363 ret = pthread_mutex_lock(&cond->mutex);
364 assert(ret == 0);
366 cond->is_done = true;
368 ret = pthread_cond_signal(&cond->cond);
369 assert(ret == 0);
370 ret = pthread_mutex_unlock(&cond->mutex);
371 assert(ret == 0);
374 static void py_tevent_signalme(struct tevent_req *req)
376 struct py_tevent_cond *cond = (struct py_tevent_cond *)
377 tevent_req_callback_data_void(req);
379 py_tevent_cond_signal(cond);
382 #endif
384 static int py_tevent_req_wait(struct tevent_context *ev,
385 struct tevent_req *req);
387 static bool py_cli_state_setup_ev(struct py_cli_state *self)
389 self->ev = tevent_context_init(NULL);
390 if (self->ev == NULL) {
391 return false;
394 samba_tevent_set_debug(self->ev, "pylibsmb_tevent");
396 self->req_wait_fn = py_tevent_req_wait;
398 return true;
401 static int py_tevent_req_wait(struct tevent_context *ev,
402 struct tevent_req *req)
404 while (tevent_req_is_in_progress(req)) {
405 int ret;
407 ret = tevent_loop_once(ev);
408 if (ret != 0) {
409 return ret;
412 return 0;
415 static bool py_tevent_req_wait_exc(struct py_cli_state *self,
416 struct tevent_req *req)
418 int ret;
420 if (req == NULL) {
421 PyErr_NoMemory();
422 return false;
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;
431 return true;
434 static PyObject *py_cli_state_new(PyTypeObject *type, PyObject *args,
435 PyObject *kwds)
437 struct py_cli_state *self;
439 self = (struct py_cli_state *)type->tp_alloc(type, 0);
440 if (self == NULL) {
441 return NULL;
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;
452 static struct smb2_negotiate_contexts *py_cli_get_negotiate_contexts(
453 TALLOC_CTX *mem_ctx, PyObject *list)
455 struct smb2_negotiate_contexts *ctxs = NULL;
456 Py_ssize_t i, len;
457 int ret;
459 ret = PyList_Check(list);
460 if (!ret) {
461 goto fail;
464 len = PyList_Size(list);
465 if (len == 0) {
466 goto fail;
469 ctxs = talloc_zero(mem_ctx, struct smb2_negotiate_contexts);
470 if (ctxs == NULL) {
471 goto fail;
474 for (i=0; i<len; i++) {
475 NTSTATUS status;
477 PyObject *t = PyList_GetItem(list, i);
478 Py_ssize_t tlen;
480 PyObject *ptype = NULL;
481 long type;
483 PyObject *pdata = NULL;
484 DATA_BLOB data = { .data = NULL, };
486 if (t == NULL) {
487 goto fail;
490 ret = PyTuple_Check(t);
491 if (!ret) {
492 goto fail;
495 tlen = PyTuple_Size(t);
496 if (tlen != 2) {
497 goto fail;
500 ptype = PyTuple_GetItem(t, 0);
501 if (ptype == NULL) {
502 goto fail;
504 type = PyLong_AsLong(ptype);
505 if ((type < 0) || (type > UINT16_MAX)) {
506 goto fail;
509 pdata = PyTuple_GetItem(t, 1);
511 ret = PyBytes_Check(pdata);
512 if (!ret) {
513 goto fail;
516 data.data = (uint8_t *)PyBytes_AsString(pdata);
517 data.length = PyBytes_Size(pdata);
519 status = smb2_negotiate_context_add(
520 ctxs, ctxs, type, data.data, data.length);
521 if (!NT_STATUS_IS_OK(status)) {
522 goto fail;
525 return ctxs;
527 fail:
528 TALLOC_FREE(ctxs);
529 return NULL;
532 static void py_cli_got_oplock_break(struct tevent_req *req);
534 static int py_cli_state_init(struct py_cli_state *self, PyObject *args,
535 PyObject *kwds)
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;
556 static const char *kwlist[] = {
557 "host", "share", "lp", "creds",
558 "multi_threaded", "force_smb1",
559 "ipc",
560 "posix",
561 "negotiate_contexts",
562 NULL
565 PyTypeObject *py_type_Credentials = get_pytype(
566 "samba.credentials", "Credentials");
567 if (py_type_Credentials == NULL) {
568 return -1;
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);
581 Py_DECREF(py_type_Credentials);
583 if (!ret) {
584 return -1;
587 multi_threaded = PyObject_IsTrue(py_multi_threaded);
588 force_smb1 = PyObject_IsTrue(py_force_smb1);
590 if (force_smb1) {
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.
597 flags = CLI_FULL_CONNECTION_FORCE_SMB1;
600 use_ipc = PyObject_IsTrue(py_ipc);
601 if (use_ipc) {
602 flags |= CLI_FULL_CONNECTION_IPC;
605 request_posix = PyObject_IsTrue(py_posix);
606 if (request_posix) {
607 flags |= CLI_FULL_CONNECTION_REQUEST_POSIX;
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;
618 if (multi_threaded) {
619 #ifdef HAVE_PTHREAD
620 ret = py_cli_state_setup_mt_ev(self);
621 if (!ret) {
622 return -1;
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;
636 if (creds == NULL) {
637 cli_creds = cli_credentials_init_anon(NULL);
638 } else {
639 cli_creds = PyCredentials_AsCliCredentials(creds);
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;
649 status = cli_full_connection_creds_recv(req, &self->cli);
650 TALLOC_FREE(req);
652 if (!NT_STATUS_IS_OK(status)) {
653 PyErr_SetNTSTATUS(status);
654 return -1;
658 * Oplocks require a multi threaded connection
660 if (self->thread_state == NULL) {
661 return 0;
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;
670 tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
671 self);
672 return 0;
675 static void py_cli_got_oplock_break(struct tevent_req *req)
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;
684 status = cli_smb_oplock_break_waiter_recv(req, &b.fnum, &b.level);
685 TALLOC_FREE(req);
686 self->oplock_waiter = NULL;
688 if (!NT_STATUS_IS_OK(status)) {
689 return;
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;
698 self->oplock_breaks = tmp;
699 self->oplock_breaks[num_breaks] = b;
701 if (self->oplock_cond != NULL) {
702 py_tevent_cond_signal(self->oplock_cond);
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;
710 tevent_req_set_callback(self->oplock_waiter, py_cli_got_oplock_break,
711 self);
714 static PyObject *py_cli_get_oplock_break(struct py_cli_state *self,
715 PyObject *args)
717 size_t num_oplock_breaks;
719 if (!PyArg_ParseTuple(args, "")) {
720 return NULL;
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;
730 if (self->oplock_cond != NULL) {
731 errno = EBUSY;
732 PyErr_SetFromErrno(PyExc_RuntimeError);
733 return NULL;
736 num_oplock_breaks = talloc_array_length(self->oplock_breaks);
738 if (num_oplock_breaks == 0) {
739 struct py_tevent_cond cond;
740 int ret;
742 self->oplock_cond = &cond;
743 ret = py_tevent_cond_wait(&cond);
744 self->oplock_cond = NULL;
746 if (ret != 0) {
747 errno = ret;
748 PyErr_SetFromErrno(PyExc_RuntimeError);
749 return NULL;
753 num_oplock_breaks = talloc_array_length(self->oplock_breaks);
754 if (num_oplock_breaks > 0) {
755 PyObject *result;
757 result = Py_BuildValue(
758 "{s:i,s:i}",
759 "fnum", self->oplock_breaks[0].fnum,
760 "level", self->oplock_breaks[0].level);
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);
769 return result;
771 Py_RETURN_NONE;
774 static void py_cli_state_dealloc(struct py_cli_state *self)
776 TALLOC_FREE(self->thread_state);
777 TALLOC_FREE(self->oplock_waiter);
778 TALLOC_FREE(self->ev);
780 if (self->cli != NULL) {
781 cli_shutdown(self->cli);
782 self->cli = NULL;
784 Py_TYPE(self)->tp_free((PyObject *)self);
787 static PyObject *py_cli_settimeout(struct py_cli_state *self, PyObject *args)
789 unsigned int nmsecs = 0;
790 unsigned int omsecs = 0;
792 if (!PyArg_ParseTuple(args, "I", &nmsecs)) {
793 return NULL;
796 omsecs = cli_set_timeout(self->cli, nmsecs);
798 return PyLong_FromLong(omsecs);
801 static PyObject *py_cli_echo(struct py_cli_state *self,
802 PyObject *Py_UNUSED(ignored))
804 DATA_BLOB data = data_blob_string_const("keepalive");
805 struct tevent_req *req = NULL;
806 NTSTATUS status;
808 req = cli_echo_send(NULL, self->ev, self->cli, 1, data);
809 if (!py_tevent_req_wait_exc(self, req)) {
810 return NULL;
812 status = cli_echo_recv(req);
813 TALLOC_FREE(req);
814 PyErr_NTSTATUS_NOT_OK_RAISE(status);
816 Py_RETURN_NONE;
819 static PyObject *py_cli_create(struct py_cli_state *self, PyObject *args,
820 PyObject *kwds)
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;
835 static const char *kwlist[] = {
836 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
837 "ShareAccess", "CreateDisposition", "CreateOptions",
838 "ImpersonationLevel", "SecurityFlags", NULL };
840 if (!ParseTupleAndKeywords(
841 args, kwds, "s|IIIIIIII", kwlist,
842 &fname, &CreateFlags, &DesiredAccess, &FileAttributes,
843 &ShareAccess, &CreateDisposition, &CreateOptions,
844 &ImpersonationLevel, &SecurityFlags)) {
845 return NULL;
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;
855 status = cli_ntcreate_recv(req, &fnum, NULL);
856 TALLOC_FREE(req);
858 if (!NT_STATUS_IS_OK(status)) {
859 PyErr_SetNTSTATUS(status);
860 return NULL;
862 return Py_BuildValue("I", (unsigned)fnum);
865 static struct smb2_create_blobs *py_cli_get_create_contexts(
866 TALLOC_CTX *mem_ctx, PyObject *list)
868 struct smb2_create_blobs *ctxs = NULL;
869 Py_ssize_t i, len;
870 int ret;
872 ret = PyList_Check(list);
873 if (!ret) {
874 goto fail;
877 len = PyList_Size(list);
878 if (len == 0) {
879 goto fail;
882 ctxs = talloc_zero(mem_ctx, struct smb2_create_blobs);
883 if (ctxs == NULL) {
884 goto fail;
887 for (i=0; i<len; i++) {
888 NTSTATUS status;
890 PyObject *t = NULL;
891 Py_ssize_t tlen;
893 PyObject *pname = NULL;
894 char *name = NULL;
896 PyObject *pdata = NULL;
897 DATA_BLOB data = { .data = NULL, };
899 t = PyList_GetItem(list, i);
900 if (t == NULL) {
901 goto fail;
904 ret = PyTuple_Check(t);
905 if (!ret) {
906 goto fail;
909 tlen = PyTuple_Size(t);
910 if (tlen != 2) {
911 goto fail;
914 pname = PyTuple_GetItem(t, 0);
915 if (pname == NULL) {
916 goto fail;
918 ret = PyBytes_Check(pname);
919 if (!ret) {
920 goto fail;
922 name = PyBytes_AsString(pname);
924 pdata = PyTuple_GetItem(t, 1);
925 if (pdata == NULL) {
926 goto fail;
928 ret = PyBytes_Check(pdata);
929 if (!ret) {
930 goto fail;
932 data = (DATA_BLOB) {
933 .data = (uint8_t *)PyBytes_AsString(pdata),
934 .length = PyBytes_Size(pdata),
936 status = smb2_create_blob_add(ctxs, ctxs, name, data);
937 if (!NT_STATUS_IS_OK(status)) {
938 goto fail;
941 return ctxs;
943 fail:
944 TALLOC_FREE(ctxs);
945 return NULL;
948 static PyObject *py_cli_create_contexts(const struct smb2_create_blobs *blobs)
950 PyObject *py_blobs = NULL;
951 uint32_t i;
953 if (blobs == NULL) {
954 Py_RETURN_NONE;
957 py_blobs = PyList_New(blobs->num_blobs);
958 if (py_blobs == NULL) {
959 return NULL;
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;
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;
976 ret = PyList_SetItem(py_blobs, i, py_blob);
977 if (ret == -1) {
978 Py_XDECREF(py_blob);
979 goto fail;
982 return py_blobs;
984 fail:
985 Py_XDECREF(py_blobs);
986 return NULL;
989 static PyObject *py_cli_create_returns(const struct smb_create_returns *r)
991 PyObject *v = NULL;
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;
1018 static PyObject *py_cli_symlink_error(const struct symlink_reparse_struct *s)
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;
1026 * Python wants utf-8, regardless of our unix charset (which
1027 * most likely is utf-8 these days, but you never know).
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;
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;
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);
1065 fail:
1066 TALLOC_FREE(subst_utf8);
1067 TALLOC_FREE(print_utf8);
1068 return v;
1071 static PyObject *py_cli_create_ex(
1072 struct py_cli_state *self, PyObject *args, PyObject *kwds)
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;
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 };
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;
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;
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),
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);
1174 TALLOC_FREE(create_contexts_in);
1176 ok = py_tevent_req_wait_exc(self, req);
1177 if (!ok) {
1178 return NULL;
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);
1193 TALLOC_FREE(req);
1195 if (!NT_STATUS_IS_OK(status)) {
1196 goto fail;
1199 SMB_ASSERT(symlink == NULL);
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;
1207 py_cr = py_cli_create_returns(&cr);
1208 if (py_cr == NULL) {
1209 goto nomem;
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);
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);
1238 return NULL;
1241 static PyObject *py_cli_close(struct py_cli_state *self, PyObject *args)
1243 struct tevent_req *req;
1244 int fnum;
1245 int flags = 0;
1246 NTSTATUS status;
1248 if (!PyArg_ParseTuple(args, "i|i", &fnum, &flags)) {
1249 return NULL;
1252 req = cli_close_send(NULL, self->ev, self->cli, fnum, flags);
1253 if (!py_tevent_req_wait_exc(self, req)) {
1254 return NULL;
1256 status = cli_close_recv(req);
1257 TALLOC_FREE(req);
1259 if (!NT_STATUS_IS_OK(status)) {
1260 PyErr_SetNTSTATUS(status);
1261 return NULL;
1263 Py_RETURN_NONE;
1266 static PyObject *py_cli_rename(
1267 struct py_cli_state *self, PyObject *args, PyObject *kwds)
1269 char *fname_src = NULL, *fname_dst = NULL;
1270 int replace = false;
1271 struct tevent_req *req = NULL;
1272 NTSTATUS status;
1273 bool ok;
1275 static const char *kwlist[] = { "src", "dst", "replace", NULL };
1277 ok = ParseTupleAndKeywords(
1278 args, kwds, "ss|p", kwlist, &fname_src, &fname_dst, &replace);
1279 if (!ok) {
1280 return NULL;
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;
1288 status = cli_rename_recv(req);
1289 TALLOC_FREE(req);
1291 if (!NT_STATUS_IS_OK(status)) {
1292 PyErr_SetNTSTATUS(status);
1293 return NULL;
1295 Py_RETURN_NONE;
1299 struct push_state {
1300 char *data;
1301 off_t nread;
1302 off_t total_data;
1306 * cli_push() helper to write a chunk of data to a remote file
1308 static size_t push_data(uint8_t *buf, size_t n, void *priv)
1310 struct push_state *state = (struct push_state *)priv;
1311 char *curr_ptr = NULL;
1312 off_t remaining;
1313 size_t copied_bytes;
1315 if (state->nread >= state->total_data) {
1316 return 0;
1319 curr_ptr = state->data + state->nread;
1320 remaining = state->total_data - state->nread;
1321 copied_bytes = MIN(remaining, n);
1323 memcpy(buf, curr_ptr, copied_bytes);
1324 state->nread += copied_bytes;
1325 return copied_bytes;
1329 * Writes a file with the contents specified
1331 static PyObject *py_smb_savefile(struct py_cli_state *self, PyObject *args)
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;
1341 if (!PyArg_ParseTuple(args, "s"PYARG_BYTES_LEN":savefile", &filename,
1342 &data, &size)) {
1343 return NULL;
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;
1355 status = cli_ntcreate_recv(req, &fnum, NULL);
1356 TALLOC_FREE(req);
1357 PyErr_NTSTATUS_NOT_OK_RAISE(status);
1359 /* write the new file contents */
1360 state.data = data;
1361 state.nread = 0;
1362 state.total_data = size;
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;
1369 status = cli_push_recv(req);
1370 TALLOC_FREE(req);
1371 PyErr_NTSTATUS_NOT_OK_RAISE(status);
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;
1378 status = cli_close_recv(req);
1379 PyErr_NTSTATUS_NOT_OK_RAISE(status);
1381 Py_RETURN_NONE;
1384 static PyObject *py_cli_write(struct py_cli_state *self, PyObject *args,
1385 PyObject *kwds)
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;
1396 static const char *kwlist[] = {
1397 "fnum", "buffer", "offset", "mode", NULL };
1399 if (!ParseTupleAndKeywords(
1400 args, kwds, "i" PYARG_BYTES_LEN "K|I", kwlist,
1401 &fnum, &buf, &buflen, &offset, &mode)) {
1402 return NULL;
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;
1410 status = cli_write_recv(req, &written);
1411 TALLOC_FREE(req);
1413 if (!NT_STATUS_IS_OK(status)) {
1414 PyErr_SetNTSTATUS(status);
1415 return NULL;
1417 return Py_BuildValue("K", (unsigned long long)written);
1421 * Returns the size of the given file
1423 static NTSTATUS py_smb_filesize(struct py_cli_state *self, uint16_t fnum,
1424 off_t *size)
1426 NTSTATUS status;
1427 struct tevent_req *req = NULL;
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;
1433 status = cli_qfileinfo_basic_recv(
1434 req, NULL, size, NULL, NULL, NULL, NULL, NULL);
1435 TALLOC_FREE(req);
1436 return status;
1440 * Loads the specified file's contents and returns it
1442 static PyObject *py_smb_loadfile(struct py_cli_state *self, PyObject *args)
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;
1453 if (!PyArg_ParseTuple(args, "s:loadfile", &filename)) {
1454 return NULL;
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;
1466 status = cli_ntcreate_recv(req, &fnum, NULL);
1467 TALLOC_FREE(req);
1468 PyErr_NTSTATUS_NOT_OK_RAISE(status);
1470 /* get a buffer to hold the file contents */
1471 status = py_smb_filesize(self, fnum, &size);
1472 PyErr_NTSTATUS_NOT_OK_RAISE(status);
1474 result = PyBytes_FromStringAndSize(NULL, size);
1475 if (result == NULL) {
1476 return NULL;
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;
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;
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;
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;
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;
1518 if (nread < size) {
1519 if (_PyBytes_Resize(&result, nread) < 0) {
1520 return NULL;
1524 return result;
1527 static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
1528 PyObject *kwds)
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;
1539 static const char *kwlist[] = {
1540 "fnum", "offset", "size", NULL };
1542 if (!ParseTupleAndKeywords(
1543 args, kwds, "iKI", kwlist, &fnum, &offset,
1544 &size)) {
1545 return NULL;
1548 result = PyBytes_FromStringAndSize(NULL, size);
1549 if (result == NULL) {
1550 return NULL;
1552 buf = PyBytes_AS_STRING(result);
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;
1560 status = cli_read_recv(req, &received);
1561 TALLOC_FREE(req);
1563 if (!NT_STATUS_IS_OK(status)) {
1564 Py_XDECREF(result);
1565 PyErr_SetNTSTATUS(status);
1566 return NULL;
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;
1577 if (received < size) {
1578 if (_PyBytes_Resize(&result, received) < 0) {
1579 return NULL;
1583 return result;
1586 static PyObject *py_cli_ftruncate(struct py_cli_state *self, PyObject *args,
1587 PyObject *kwds)
1589 int fnum;
1590 unsigned long long size;
1591 struct tevent_req *req;
1592 NTSTATUS status;
1594 static const char *kwlist[] = {
1595 "fnum", "size", NULL };
1597 if (!ParseTupleAndKeywords(
1598 args, kwds, "IK", kwlist, &fnum, &size)) {
1599 return NULL;
1602 req = cli_ftruncate_send(NULL, self->ev, self->cli, fnum, size);
1603 if (!py_tevent_req_wait_exc(self, req)) {
1604 return NULL;
1606 status = cli_ftruncate_recv(req);
1607 TALLOC_FREE(req);
1609 if (!NT_STATUS_IS_OK(status)) {
1610 PyErr_SetNTSTATUS(status);
1611 return NULL;
1613 Py_RETURN_NONE;
1616 static PyObject *py_cli_delete_on_close(struct py_cli_state *self,
1617 PyObject *args,
1618 PyObject *kwds)
1620 unsigned fnum, flag;
1621 struct tevent_req *req;
1622 NTSTATUS status;
1624 static const char *kwlist[] = {
1625 "fnum", "flag", NULL };
1627 if (!ParseTupleAndKeywords(
1628 args, kwds, "II", kwlist, &fnum, &flag)) {
1629 return NULL;
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;
1637 status = cli_nt_delete_on_close_recv(req);
1638 TALLOC_FREE(req);
1640 if (!NT_STATUS_IS_OK(status)) {
1641 PyErr_SetNTSTATUS(status);
1642 return NULL;
1644 Py_RETURN_NONE;
1647 struct py_cli_notify_state {
1648 PyObject_HEAD
1649 struct py_cli_state *py_cli_state;
1650 struct tevent_req *req;
1653 static void py_cli_notify_state_dealloc(struct py_cli_notify_state *self)
1655 TALLOC_FREE(self->req);
1656 Py_CLEAR(self->py_cli_state);
1657 Py_TYPE(self)->tp_free(self);
1660 static PyTypeObject py_cli_notify_state_type;
1662 static PyObject *py_cli_notify(struct py_cli_state *self,
1663 PyObject *args,
1664 PyObject *kwds)
1666 static const char *kwlist[] = {
1667 "fnum",
1668 "buffer_size",
1669 "completion_filter",
1670 "recursive",
1671 NULL
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;
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;
1697 recursive = PyObject_IsTrue(py_recursive);
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;
1712 * Just wait for the request being submitted to
1713 * the kernel/socket/wire.
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;
1728 ok = py_tevent_req_wait_exc(self, flush_req);
1729 if (!ok) {
1730 TALLOC_FREE(req);
1731 return NULL;
1733 TALLOC_FREE(flush_req);
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;
1742 Py_INCREF(self);
1743 py_notify_state->py_cli_state = self;
1744 py_notify_state->req = req;
1746 return (PyObject *)py_notify_state;
1749 static PyObject *py_cli_notify_get_changes(struct py_cli_notify_state *self,
1750 PyObject *args,
1751 PyObject *kwds)
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
1765 PyObject *py_wait = Py_False;
1766 bool wait = false;
1767 bool pending;
1769 ok = ParseTupleAndKeywords(args,
1770 kwds,
1771 "O",
1772 kwlist,
1773 &py_wait);
1774 if (!ok) {
1775 return NULL;
1778 wait = PyObject_IsTrue(py_wait);
1780 if (req == NULL) {
1781 PyErr_SetString(PyExc_RuntimeError,
1782 "TODO req == NULL "
1783 "- missing change notify request?");
1784 return NULL;
1787 pending = tevent_req_is_in_progress(req);
1788 if (pending && !wait) {
1789 Py_RETURN_NONE;
1792 if (pending) {
1793 struct timeval endtime;
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;
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;
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;
1820 result = Py_BuildValue("[]");
1821 if (result == NULL) {
1822 TALLOC_FREE(req);
1823 return NULL;
1826 for (i = 0; i < num_changes; i++) {
1827 PyObject *change = NULL;
1828 int ret;
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;
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;
1848 TALLOC_FREE(req);
1849 return result;
1852 static PyMethodDef py_cli_notify_state_methods[] = {
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"
1867 .ml_name = NULL
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,
1882 * Helper to add posix directory listing entries to an overall Python list
1884 static NTSTATUS list_posix_helper(struct file_info *finfo,
1885 const char *mask, void *state)
1887 PyObject *result = (PyObject *)state;
1888 PyObject *file = NULL;
1889 PyObject *size = NULL;
1890 int ret;
1892 size = PyLong_FromUnsignedLongLong(finfo->size);
1894 * Build a dictionary representing the file info.
1896 file = Py_BuildValue("{s:s,s:i,s:O,s:l,s:i,s:i,s:i,s:s,s:s}",
1897 "name", finfo->name,
1898 "attrib", (int)finfo->attr,
1899 "size", size,
1900 "mtime",
1901 convert_timespec_to_time_t(finfo->mtime_ts),
1902 "perms", finfo->st_ex_mode,
1903 "ino", finfo->ino,
1904 "dev", finfo->st_ex_dev,
1905 "owner_sid",
1906 dom_sid_string(finfo, &finfo->owner_sid),
1907 "group_sid",
1908 dom_sid_string(finfo, &finfo->group_sid));
1910 Py_CLEAR(size);
1912 if (file == NULL) {
1913 return NT_STATUS_NO_MEMORY;
1916 ret = PyList_Append(result, file);
1917 Py_CLEAR(file);
1918 if (ret == -1) {
1919 return NT_STATUS_INTERNAL_ERROR;
1922 return NT_STATUS_OK;
1926 * Helper to add directory listing entries to an overall Python list
1928 static NTSTATUS list_helper(struct file_info *finfo,
1929 const char *mask, void *state)
1931 PyObject *result = (PyObject *)state;
1932 PyObject *file = NULL;
1933 PyObject *size = NULL;
1934 int ret;
1936 /* suppress '.' and '..' in the results we return */
1937 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
1938 return NT_STATUS_OK;
1940 size = PyLong_FromUnsignedLongLong(finfo->size);
1942 * Build a dictionary representing the file info.
1943 * Note: Windows does not always return short_name (so it may be None)
1945 file = Py_BuildValue("{s:s,s:i,s:s,s:O,s:l}",
1946 "name", finfo->name,
1947 "attrib", (int)finfo->attr,
1948 "short_name", finfo->short_name,
1949 "size", size,
1950 "mtime",
1951 convert_timespec_to_time_t(finfo->mtime_ts));
1953 Py_CLEAR(size);
1955 if (file == NULL) {
1956 return NT_STATUS_NO_MEMORY;
1959 if (finfo->attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1960 unsigned long tag = finfo->reparse_tag;
1962 ret = PyDict_SetItemString(
1963 file,
1964 "reparse_tag",
1965 PyLong_FromUnsignedLong(tag));
1966 if (ret == -1) {
1967 return NT_STATUS_INTERNAL_ERROR;
1971 ret = PyList_Append(result, file);
1972 Py_CLEAR(file);
1973 if (ret == -1) {
1974 return NT_STATUS_INTERNAL_ERROR;
1977 return NT_STATUS_OK;
1980 struct do_listing_state {
1981 const char *mask;
1982 NTSTATUS (*callback_fn)(
1983 struct file_info *finfo,
1984 const char *mask,
1985 void *private_data);
1986 void *private_data;
1987 NTSTATUS status;
1990 static void do_listing_cb(struct tevent_req *subreq)
1992 struct do_listing_state *state = tevent_req_callback_data_void(subreq);
1993 struct file_info *finfo = NULL;
1995 state->status = cli_list_recv(subreq, NULL, &finfo);
1996 if (!NT_STATUS_IS_OK(state->status)) {
1997 return;
1999 state->callback_fn(finfo, state->mask, state->private_data);
2000 TALLOC_FREE(finfo);
2003 static NTSTATUS do_listing(struct py_cli_state *self,
2004 const char *base_dir, const char *user_mask,
2005 uint16_t attribute,
2006 unsigned int info_level,
2007 NTSTATUS (*callback_fn)(struct file_info *,
2008 const char *, void *),
2009 void *priv)
2011 char *mask = NULL;
2012 struct do_listing_state state = {
2013 .mask = mask,
2014 .callback_fn = callback_fn,
2015 .private_data = priv,
2017 struct tevent_req *req = NULL;
2018 NTSTATUS status;
2020 if (user_mask == NULL) {
2021 mask = talloc_asprintf(NULL, "%s\\*", base_dir);
2022 } else {
2023 mask = talloc_asprintf(NULL, "%s\\%s", base_dir, user_mask);
2026 if (mask == NULL) {
2027 return NT_STATUS_NO_MEMORY;
2029 dos_format(mask);
2031 req = cli_list_send(NULL, self->ev, self->cli, mask, attribute,
2032 info_level);
2033 if (req == NULL) {
2034 status = NT_STATUS_NO_MEMORY;
2035 goto done;
2037 tevent_req_set_callback(req, do_listing_cb, &state);
2039 if (!py_tevent_req_wait_exc(self, req)) {
2040 return NT_STATUS_INTERNAL_ERROR;
2042 TALLOC_FREE(req);
2044 status = state.status;
2045 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_FILES)) {
2046 status = NT_STATUS_OK;
2049 done:
2050 TALLOC_FREE(mask);
2051 return status;
2054 static PyObject *py_cli_list(struct py_cli_state *self,
2055 PyObject *args,
2056 PyObject *kwds)
2058 char *base_dir;
2059 char *user_mask = NULL;
2060 unsigned int attribute = LIST_ATTRIBUTE_MASK;
2061 unsigned int info_level = 0;
2062 NTSTATUS status;
2063 enum protocol_types proto = smbXcli_conn_protocol(self->cli->conn);
2064 PyObject *result = NULL;
2065 const char *kwlist[] = { "directory", "mask", "attribs",
2066 "info_level", NULL };
2067 NTSTATUS (*callback_fn)(struct file_info *, const char *, void *) =
2068 &list_helper;
2070 if (!ParseTupleAndKeywords(args, kwds, "z|sII:list", kwlist,
2071 &base_dir, &user_mask, &attribute,
2072 &info_level)) {
2073 return NULL;
2076 result = Py_BuildValue("[]");
2077 if (result == NULL) {
2078 return NULL;
2081 if (!info_level) {
2082 if (proto >= PROTOCOL_SMB2_02) {
2083 info_level = SMB2_FIND_ID_BOTH_DIRECTORY_INFO;
2084 } else {
2085 info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
2089 if (info_level == SMB2_FIND_POSIX_INFORMATION) {
2090 callback_fn = &list_posix_helper;
2092 status = do_listing(self, base_dir, user_mask, attribute,
2093 info_level, callback_fn, result);
2095 if (!NT_STATUS_IS_OK(status)) {
2096 Py_XDECREF(result);
2097 PyErr_SetNTSTATUS(status);
2098 return NULL;
2101 return result;
2104 static PyObject *py_smb_unlink(struct py_cli_state *self, PyObject *args)
2106 NTSTATUS status;
2107 const char *filename = NULL;
2108 struct tevent_req *req = NULL;
2109 const uint32_t attrs = (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2111 if (!PyArg_ParseTuple(args, "s:unlink", &filename)) {
2112 return NULL;
2115 req = cli_unlink_send(NULL, self->ev, self->cli, filename, attrs);
2116 if (!py_tevent_req_wait_exc(self, req)) {
2117 return NULL;
2119 status = cli_unlink_recv(req);
2120 TALLOC_FREE(req);
2121 PyErr_NTSTATUS_NOT_OK_RAISE(status);
2123 Py_RETURN_NONE;
2126 static PyObject *py_smb_rmdir(struct py_cli_state *self, PyObject *args)
2128 NTSTATUS status;
2129 struct tevent_req *req = NULL;
2130 const char *dirname = NULL;
2132 if (!PyArg_ParseTuple(args, "s:rmdir", &dirname)) {
2133 return NULL;
2136 req = cli_rmdir_send(NULL, self->ev, self->cli, dirname);
2137 if (!py_tevent_req_wait_exc(self, req)) {
2138 return NULL;
2140 status = cli_rmdir_recv(req);
2141 TALLOC_FREE(req);
2142 PyErr_NTSTATUS_NOT_OK_RAISE(status);
2144 Py_RETURN_NONE;
2148 * Create a directory
2150 static PyObject *py_smb_mkdir(struct py_cli_state *self, PyObject *args)
2152 NTSTATUS status;
2153 const char *dirname = NULL;
2154 struct tevent_req *req = NULL;
2156 if (!PyArg_ParseTuple(args, "s:mkdir", &dirname)) {
2157 return NULL;
2160 req = cli_mkdir_send(NULL, self->ev, self->cli, dirname);
2161 if (!py_tevent_req_wait_exc(self, req)) {
2162 return NULL;
2164 status = cli_mkdir_recv(req);
2165 TALLOC_FREE(req);
2166 PyErr_NTSTATUS_NOT_OK_RAISE(status);
2168 Py_RETURN_NONE;
2172 * Does a whoami call
2174 static PyObject *py_smb_posix_whoami(struct py_cli_state *self,
2175 PyObject *Py_UNUSED(ignored))
2177 TALLOC_CTX *frame = talloc_stackframe();
2178 NTSTATUS status;
2179 struct tevent_req *req = NULL;
2180 uint64_t uid;
2181 uint64_t gid;
2182 uint32_t num_gids;
2183 uint64_t *gids = NULL;
2184 uint32_t num_sids;
2185 struct dom_sid *sids = NULL;
2186 bool guest;
2187 PyObject *py_gids = NULL;
2188 PyObject *py_sids = NULL;
2189 PyObject *py_guest = NULL;
2190 PyObject *py_ret = NULL;
2191 Py_ssize_t i;
2193 req = cli_posix_whoami_send(frame, self->ev, self->cli);
2194 if (!py_tevent_req_wait_exc(self, req)) {
2195 goto fail;
2197 status = cli_posix_whoami_recv(req,
2198 frame,
2199 &uid,
2200 &gid,
2201 &num_gids,
2202 &gids,
2203 &num_sids,
2204 &sids,
2205 &guest);
2206 if (!NT_STATUS_IS_OK(status)) {
2207 PyErr_SetNTSTATUS(status);
2208 goto fail;
2210 if (num_gids > PY_SSIZE_T_MAX) {
2211 PyErr_SetString(PyExc_OverflowError, "posix_whoami: Too many GIDs");
2212 goto fail;
2214 if (num_sids > PY_SSIZE_T_MAX) {
2215 PyErr_SetString(PyExc_OverflowError, "posix_whoami: Too many SIDs");
2216 goto fail;
2219 py_gids = PyList_New(num_gids);
2220 if (!py_gids) {
2221 goto fail;
2223 for (i = 0; i < num_gids; ++i) {
2224 int ret;
2225 PyObject *py_item = PyLong_FromUnsignedLongLong(gids[i]);
2226 if (!py_item) {
2227 goto fail2;
2230 ret = PyList_SetItem(py_gids, i, py_item);
2231 if (ret) {
2232 goto fail2;
2235 py_sids = PyList_New(num_sids);
2236 if (!py_sids) {
2237 goto fail2;
2239 for (i = 0; i < num_sids; ++i) {
2240 int ret;
2241 struct dom_sid *sid;
2242 PyObject *py_item;
2244 sid = dom_sid_dup(frame, &sids[i]);
2245 if (!sid) {
2246 PyErr_NoMemory();
2247 goto fail3;
2250 py_item = pytalloc_steal(dom_sid_Type, sid);
2251 if (!py_item) {
2252 PyErr_NoMemory();
2253 goto fail3;
2256 ret = PyList_SetItem(py_sids, i, py_item);
2257 if (ret) {
2258 goto fail3;
2262 py_guest = guest ? Py_True : Py_False;
2264 py_ret = Py_BuildValue("KKNNO",
2265 uid,
2266 gid,
2267 py_gids,
2268 py_sids,
2269 py_guest);
2270 if (!py_ret) {
2271 goto fail3;
2274 TALLOC_FREE(frame);
2275 return py_ret;
2277 fail3:
2278 Py_CLEAR(py_sids);
2280 fail2:
2281 Py_CLEAR(py_gids);
2283 fail:
2284 TALLOC_FREE(frame);
2285 return NULL;
2289 * Checks existence of a directory
2291 static bool check_dir_path(struct py_cli_state *self, const char *path)
2293 NTSTATUS status;
2294 struct tevent_req *req = NULL;
2296 req = cli_chkpath_send(NULL, self->ev, self->cli, path);
2297 if (!py_tevent_req_wait_exc(self, req)) {
2298 return false;
2300 status = cli_chkpath_recv(req);
2301 TALLOC_FREE(req);
2303 return NT_STATUS_IS_OK(status);
2306 static PyObject *py_smb_chkpath(struct py_cli_state *self, PyObject *args)
2308 const char *path = NULL;
2309 bool dir_exists;
2311 if (!PyArg_ParseTuple(args, "s:chkpath", &path)) {
2312 return NULL;
2315 dir_exists = check_dir_path(self, path);
2316 return PyBool_FromLong(dir_exists);
2319 static PyObject *py_smb_have_posix(struct py_cli_state *self,
2320 PyObject *Py_UNUSED(ignored))
2322 bool posix = smbXcli_conn_have_posix(self->cli->conn);
2324 if (posix) {
2325 Py_RETURN_TRUE;
2327 Py_RETURN_FALSE;
2330 static PyObject *py_smb_protocol(struct py_cli_state *self,
2331 PyObject *Py_UNUSED(ignored))
2333 enum protocol_types proto = smbXcli_conn_protocol(self->cli->conn);
2334 PyObject *result = PyLong_FromLong(proto);
2335 return result;
2338 static PyObject *py_smb_get_sd(struct py_cli_state *self, PyObject *args)
2340 int fnum;
2341 unsigned sinfo;
2342 struct tevent_req *req = NULL;
2343 struct security_descriptor *sd = NULL;
2344 NTSTATUS status;
2346 if (!PyArg_ParseTuple(args, "iI:get_acl", &fnum, &sinfo)) {
2347 return NULL;
2350 req = cli_query_security_descriptor_send(
2351 NULL, self->ev, self->cli, fnum, sinfo);
2352 if (!py_tevent_req_wait_exc(self, req)) {
2353 return NULL;
2355 status = cli_query_security_descriptor_recv(req, NULL, &sd);
2356 PyErr_NTSTATUS_NOT_OK_RAISE(status);
2358 return py_return_ndr_struct(
2359 "samba.dcerpc.security", "descriptor", sd, sd);
2362 static PyObject *py_smb_set_sd(struct py_cli_state *self, PyObject *args)
2364 PyObject *py_sd = NULL;
2365 struct tevent_req *req = NULL;
2366 struct security_descriptor *sd = NULL;
2367 uint16_t fnum;
2368 unsigned int sinfo;
2369 NTSTATUS status;
2371 if (!PyArg_ParseTuple(args, "iOI:set_sd", &fnum, &py_sd, &sinfo)) {
2372 return NULL;
2375 sd = pytalloc_get_type(py_sd, struct security_descriptor);
2376 if (!sd) {
2377 PyErr_Format(PyExc_TypeError,
2378 "Expected dcerpc.security.descriptor as argument, got %s",
2379 pytalloc_get_name(py_sd));
2380 return NULL;
2383 req = cli_set_security_descriptor_send(
2384 NULL, self->ev, self->cli, fnum, sinfo, sd);
2385 if (!py_tevent_req_wait_exc(self, req)) {
2386 return NULL;
2389 status = cli_set_security_descriptor_recv(req);
2390 PyErr_NTSTATUS_NOT_OK_RAISE(status);
2392 Py_RETURN_NONE;
2395 static PyObject *py_smb_smb1_posix(
2396 struct py_cli_state *self, PyObject *Py_UNUSED(ignored))
2398 NTSTATUS status;
2399 struct tevent_req *req = NULL;
2400 uint16_t major, minor;
2401 uint32_t caplow, caphigh;
2402 PyObject *result = NULL;
2404 req = cli_unix_extensions_version_send(NULL, self->ev, self->cli);
2405 if (!py_tevent_req_wait_exc(self, req)) {
2406 return NULL;
2408 status = cli_unix_extensions_version_recv(
2409 req, &major, &minor, &caplow, &caphigh);
2410 TALLOC_FREE(req);
2411 if (!NT_STATUS_IS_OK(status)) {
2412 PyErr_SetNTSTATUS(status);
2413 return NULL;
2416 req = cli_set_unix_extensions_capabilities_send(
2417 NULL, self->ev, self->cli, major, minor, caplow, caphigh);
2418 if (!py_tevent_req_wait_exc(self, req)) {
2419 return NULL;
2421 status = cli_set_unix_extensions_capabilities_recv(req);
2422 TALLOC_FREE(req);
2423 if (!NT_STATUS_IS_OK(status)) {
2424 PyErr_SetNTSTATUS(status);
2425 return NULL;
2428 result = Py_BuildValue(
2429 "[IIII]",
2430 (unsigned)minor,
2431 (unsigned)major,
2432 (unsigned)caplow,
2433 (unsigned)caphigh);
2434 return result;
2437 static PyObject *py_smb_smb1_readlink(
2438 struct py_cli_state *self, PyObject *args)
2440 NTSTATUS status;
2441 const char *filename = NULL;
2442 struct tevent_req *req = NULL;
2443 char *target = NULL;
2444 PyObject *result = NULL;
2446 if (!PyArg_ParseTuple(args, "s:smb1_readlink", &filename)) {
2447 return NULL;
2450 req = cli_posix_readlink_send(NULL, self->ev, self->cli, filename);
2451 if (!py_tevent_req_wait_exc(self, req)) {
2452 return NULL;
2454 status = cli_posix_readlink_recv(req, NULL, &target);
2455 TALLOC_FREE(req);
2456 if (!NT_STATUS_IS_OK(status)) {
2457 PyErr_SetNTSTATUS(status);
2458 return NULL;
2461 result = PyBytes_FromString(target);
2462 TALLOC_FREE(target);
2463 return result;
2466 static PyObject *py_smb_smb1_symlink(
2467 struct py_cli_state *self, PyObject *args)
2469 NTSTATUS status;
2470 const char *target = NULL, *newname = NULL;
2471 struct tevent_req *req = NULL;
2473 if (!PyArg_ParseTuple(args, "ss:smb1_symlink", &target, &newname)) {
2474 return NULL;
2477 req = cli_posix_symlink_send(
2478 NULL, self->ev, self->cli, target, newname);
2479 if (!py_tevent_req_wait_exc(self, req)) {
2480 return NULL;
2482 status = cli_posix_symlink_recv(req);
2483 TALLOC_FREE(req);
2484 if (!NT_STATUS_IS_OK(status)) {
2485 PyErr_SetNTSTATUS(status);
2486 return NULL;
2489 Py_RETURN_NONE;
2492 static PyObject *py_smb_smb1_stat(
2493 struct py_cli_state *self, PyObject *args)
2495 NTSTATUS status;
2496 const char *fname = NULL;
2497 struct tevent_req *req = NULL;
2498 struct stat_ex sbuf = { .st_ex_nlink = 0, };
2500 if (!PyArg_ParseTuple(args, "s:smb1_stat", &fname)) {
2501 return NULL;
2504 req = cli_posix_stat_send(NULL, self->ev, self->cli, fname);
2505 if (!py_tevent_req_wait_exc(self, req)) {
2506 return NULL;
2508 status = cli_posix_stat_recv(req, &sbuf);
2509 TALLOC_FREE(req);
2510 if (!NT_STATUS_IS_OK(status)) {
2511 PyErr_SetNTSTATUS(status);
2512 return NULL;
2515 return Py_BuildValue(
2516 "{sLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsL}",
2517 "dev",
2518 (unsigned long long)sbuf.st_ex_dev,
2519 "ino",
2520 (unsigned long long)sbuf.st_ex_ino,
2521 "mode",
2522 (unsigned long long)sbuf.st_ex_mode,
2523 "nlink",
2524 (unsigned long long)sbuf.st_ex_nlink,
2525 "uid",
2526 (unsigned long long)sbuf.st_ex_uid,
2527 "gid",
2528 (unsigned long long)sbuf.st_ex_gid,
2529 "rdev",
2530 (unsigned long long)sbuf.st_ex_size,
2531 "atime_sec",
2532 (unsigned long long)sbuf.st_ex_atime.tv_sec,
2533 "atime_nsec",
2534 (unsigned long long)sbuf.st_ex_atime.tv_nsec,
2535 "mtime_sec",
2536 (unsigned long long)sbuf.st_ex_mtime.tv_sec,
2537 "mtime_nsec",
2538 (unsigned long long)sbuf.st_ex_mtime.tv_nsec,
2539 "ctime_sec",
2540 (unsigned long long)sbuf.st_ex_ctime.tv_sec,
2541 "ctime_nsec",
2542 (unsigned long long)sbuf.st_ex_ctime.tv_nsec,
2543 "btime_sec",
2544 (unsigned long long)sbuf.st_ex_btime.tv_sec,
2545 "btime_nsec",
2546 (unsigned long long)sbuf.st_ex_btime.tv_nsec,
2547 "cached_dos_attributes",
2548 (unsigned long long)sbuf.cached_dos_attributes,
2549 "blksize",
2550 (unsigned long long)sbuf.st_ex_blksize,
2551 "blocks",
2552 (unsigned long long)sbuf.st_ex_blocks,
2553 "flags",
2554 (unsigned long long)sbuf.st_ex_flags,
2555 "iflags",
2556 (unsigned long long)sbuf.st_ex_iflags);
2559 static PyObject *py_cli_mknod(
2560 struct py_cli_state *self, PyObject *args, PyObject *kwds)
2562 char *fname = NULL;
2563 int mode = 0, major = 0, minor = 0, dev = 0;
2564 struct tevent_req *req = NULL;
2565 static const char *kwlist[] = {
2566 "fname", "mode", "major", "minor", NULL,
2568 NTSTATUS status;
2569 bool ok;
2571 ok = ParseTupleAndKeywords(
2572 args,
2573 kwds,
2574 "sI|II:mknod",
2575 kwlist,
2576 &fname,
2577 &mode,
2578 &major,
2579 &minor);
2580 if (!ok) {
2581 return NULL;
2584 #if defined(HAVE_MAKEDEV)
2585 dev = makedev(major, minor);
2586 #endif
2588 req = cli_mknod_send(
2589 NULL, self->ev, self->cli, fname, mode, dev);
2590 if (!py_tevent_req_wait_exc(self, req)) {
2591 return NULL;
2593 status = cli_mknod_recv(req);
2594 TALLOC_FREE(req);
2595 if (!NT_STATUS_IS_OK(status)) {
2596 PyErr_SetNTSTATUS(status);
2597 return NULL;
2599 Py_RETURN_NONE;
2602 static PyObject *py_cli_fsctl(
2603 struct py_cli_state *self, PyObject *args, PyObject *kwds)
2605 int fnum, ctl_code;
2606 int max_out = 0;
2607 char *buf = NULL;
2608 Py_ssize_t buflen;
2609 DATA_BLOB in = { .data = NULL, };
2610 DATA_BLOB out = { .data = NULL, };
2611 struct tevent_req *req = NULL;
2612 PyObject *result = NULL;
2613 static const char *kwlist[] = {
2614 "fnum", "ctl_code", "in", "max_out", NULL,
2616 NTSTATUS status;
2617 bool ok;
2619 ok = ParseTupleAndKeywords(
2620 args,
2621 kwds,
2622 "ii" PYARG_BYTES_LEN "i",
2623 kwlist,
2624 &fnum,
2625 &ctl_code,
2626 &buf,
2627 &buflen,
2628 &max_out);
2629 if (!ok) {
2630 return NULL;
2633 in = (DATA_BLOB) { .data = (uint8_t *)buf, .length = buflen, };
2635 req = cli_fsctl_send(
2636 NULL, self->ev, self->cli, fnum, ctl_code, &in, max_out);
2638 if (!py_tevent_req_wait_exc(self, req)) {
2639 return NULL;
2642 status = cli_fsctl_recv(req, NULL, &out);
2643 if (!NT_STATUS_IS_OK(status)) {
2644 PyErr_SetNTSTATUS(status);
2645 return NULL;
2648 result = PyBytes_FromStringAndSize((char *)out.data, out.length);
2649 data_blob_free(&out);
2650 return result;
2653 static PyMethodDef py_cli_state_methods[] = {
2654 { "settimeout", (PyCFunction)py_cli_settimeout, METH_VARARGS,
2655 "settimeout(new_timeout_msecs) => return old_timeout_msecs" },
2656 { "echo", (PyCFunction)py_cli_echo, METH_NOARGS,
2657 "Ping the server connection" },
2658 { "create", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_create),
2659 METH_VARARGS|METH_KEYWORDS,
2660 "Open a file" },
2661 { "create_ex",
2662 PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_create_ex),
2663 METH_VARARGS|METH_KEYWORDS,
2664 "Open a file, SMB2 version returning create contexts" },
2665 { "close", (PyCFunction)py_cli_close, METH_VARARGS,
2666 "Close a file handle" },
2667 { "write", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_write),
2668 METH_VARARGS|METH_KEYWORDS,
2669 "Write to a file handle" },
2670 { "read", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_read),
2671 METH_VARARGS|METH_KEYWORDS,
2672 "Read from a file handle" },
2673 { "truncate", PY_DISCARD_FUNC_SIG(PyCFunction,
2674 py_cli_ftruncate),
2675 METH_VARARGS|METH_KEYWORDS,
2676 "Truncate a file" },
2677 { "delete_on_close", PY_DISCARD_FUNC_SIG(PyCFunction,
2678 py_cli_delete_on_close),
2679 METH_VARARGS|METH_KEYWORDS,
2680 "Set/Reset the delete on close flag" },
2681 { "notify", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_notify),
2682 METH_VARARGS|METH_KEYWORDS,
2683 "Wait for change notifications: \n"
2684 "notify(fnum, buffer_size, completion_filter...) -> "
2685 "libsmb_samba_internal.Notify request handle\n" },
2686 { "list", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_list),
2687 METH_VARARGS|METH_KEYWORDS,
2688 "list(directory, mask='*', attribs=DEFAULT_ATTRS) -> "
2689 "directory contents as a dictionary\n"
2690 "\t\tDEFAULT_ATTRS: FILE_ATTRIBUTE_SYSTEM | "
2691 "FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_ARCHIVE\n\n"
2692 "\t\tList contents of a directory. The keys are, \n"
2693 "\t\t\tname: Long name of the directory item\n"
2694 "\t\t\tshort_name: Short name of the directory item\n"
2695 "\t\t\tsize: File size in bytes\n"
2696 "\t\t\tattrib: Attributes\n"
2697 "\t\t\tmtime: Modification time\n" },
2698 { "get_oplock_break", (PyCFunction)py_cli_get_oplock_break,
2699 METH_VARARGS, "Wait for an oplock break" },
2700 { "unlink", (PyCFunction)py_smb_unlink,
2701 METH_VARARGS,
2702 "unlink(path) -> None\n\n \t\tDelete a file." },
2703 { "mkdir", (PyCFunction)py_smb_mkdir, METH_VARARGS,
2704 "mkdir(path) -> None\n\n \t\tCreate a directory." },
2705 { "posix_whoami", (PyCFunction)py_smb_posix_whoami, METH_NOARGS,
2706 "posix_whoami() -> (uid, gid, gids, sids, guest)" },
2707 { "rmdir", (PyCFunction)py_smb_rmdir, METH_VARARGS,
2708 "rmdir(path) -> None\n\n \t\tDelete a directory." },
2709 { "rename",
2710 PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_rename),
2711 METH_VARARGS|METH_KEYWORDS,
2712 "rename(src,dst) -> None\n\n \t\tRename a file." },
2713 { "chkpath", (PyCFunction)py_smb_chkpath, METH_VARARGS,
2714 "chkpath(dir_path) -> True or False\n\n"
2715 "\t\tReturn true if directory exists, false otherwise." },
2716 { "savefile", (PyCFunction)py_smb_savefile, METH_VARARGS,
2717 "savefile(path, bytes) -> None\n\n"
2718 "\t\tWrite bytes to file." },
2719 { "loadfile", (PyCFunction)py_smb_loadfile, METH_VARARGS,
2720 "loadfile(path) -> file contents as a bytes object"
2721 "\n\n\t\tRead contents of a file." },
2722 { "get_sd", (PyCFunction)py_smb_get_sd, METH_VARARGS,
2723 "get_sd(fnum[, security_info=0]) -> security_descriptor object\n\n"
2724 "\t\tGet security descriptor for opened file." },
2725 { "set_sd", (PyCFunction)py_smb_set_sd, METH_VARARGS,
2726 "set_sd(fnum, security_descriptor[, security_info=0]) -> None\n\n"
2727 "\t\tSet security descriptor for opened file." },
2728 { "protocol",
2729 (PyCFunction)py_smb_protocol,
2730 METH_NOARGS,
2731 "protocol() -> Number"
2733 { "have_posix",
2734 (PyCFunction)py_smb_have_posix,
2735 METH_NOARGS,
2736 "have_posix() -> True/False\n\n"
2737 "\t\tReturn if the server has posix extensions"
2739 { "smb1_posix",
2740 (PyCFunction)py_smb_smb1_posix,
2741 METH_NOARGS,
2742 "Negotiate SMB1 posix extensions",
2744 { "smb1_readlink",
2745 (PyCFunction)py_smb_smb1_readlink,
2746 METH_VARARGS,
2747 "smb1_readlink(path) -> link target",
2749 { "smb1_symlink",
2750 (PyCFunction)py_smb_smb1_symlink,
2751 METH_VARARGS,
2752 "smb1_symlink(target, newname) -> None",
2754 { "smb1_stat",
2755 (PyCFunction)py_smb_smb1_stat,
2756 METH_VARARGS,
2757 "smb1_stat(path) -> stat info",
2759 { "fsctl",
2760 (PyCFunction)py_cli_fsctl,
2761 METH_VARARGS|METH_KEYWORDS,
2762 "fsctl(fnum, ctl_code, in_bytes, max_out) -> out_bytes",
2764 { "mknod",
2765 PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_mknod),
2766 METH_VARARGS|METH_KEYWORDS,
2767 "mknod(path, mode | major, minor)",
2769 { NULL, NULL, 0, NULL }
2772 static PyTypeObject py_cli_state_type = {
2773 PyVarObject_HEAD_INIT(NULL, 0)
2774 .tp_name = "libsmb_samba_cwrapper.LibsmbCConn",
2775 .tp_basicsize = sizeof(struct py_cli_state),
2776 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
2777 .tp_doc = "libsmb cwrapper connection",
2778 .tp_new = py_cli_state_new,
2779 .tp_init = (initproc)py_cli_state_init,
2780 .tp_dealloc = (destructor)py_cli_state_dealloc,
2781 .tp_methods = py_cli_state_methods,
2784 static PyMethodDef py_libsmb_methods[] = {
2785 {0},
2788 void initlibsmb_samba_cwrapper(void);
2790 static struct PyModuleDef moduledef = {
2791 PyModuleDef_HEAD_INIT,
2792 .m_name = "libsmb_samba_cwrapper",
2793 .m_doc = "libsmb wrapper",
2794 .m_size = -1,
2795 .m_methods = py_libsmb_methods,
2798 MODULE_INIT_FUNC(libsmb_samba_cwrapper)
2800 PyObject *m = NULL;
2801 PyObject *mod = NULL;
2803 talloc_stackframe();
2805 if (PyType_Ready(&py_cli_state_type) < 0) {
2806 return NULL;
2808 if (PyType_Ready(&py_cli_notify_state_type) < 0) {
2809 return NULL;
2812 m = PyModule_Create(&moduledef);
2813 if (m == NULL) {
2814 return m;
2817 /* Import dom_sid type from dcerpc.security */
2818 mod = PyImport_ImportModule("samba.dcerpc.security");
2819 if (mod == NULL) {
2820 return NULL;
2823 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
2824 if (dom_sid_Type == NULL) {
2825 Py_DECREF(mod);
2826 return NULL;
2829 Py_INCREF(&py_cli_state_type);
2830 PyModule_AddObject(m, "LibsmbCConn", (PyObject *)&py_cli_state_type);
2832 #define ADD_FLAGS(val) PyModule_AddObject(m, #val, PyLong_FromLong(val))
2834 ADD_FLAGS(PROTOCOL_NONE);
2835 ADD_FLAGS(PROTOCOL_CORE);
2836 ADD_FLAGS(PROTOCOL_COREPLUS);
2837 ADD_FLAGS(PROTOCOL_LANMAN1);
2838 ADD_FLAGS(PROTOCOL_LANMAN2);
2839 ADD_FLAGS(PROTOCOL_NT1);
2840 ADD_FLAGS(PROTOCOL_SMB2_02);
2841 ADD_FLAGS(PROTOCOL_SMB2_10);
2842 ADD_FLAGS(PROTOCOL_SMB3_00);
2843 ADD_FLAGS(PROTOCOL_SMB3_02);
2844 ADD_FLAGS(PROTOCOL_SMB3_11);
2846 ADD_FLAGS(FILE_ATTRIBUTE_READONLY);
2847 ADD_FLAGS(FILE_ATTRIBUTE_HIDDEN);
2848 ADD_FLAGS(FILE_ATTRIBUTE_SYSTEM);
2849 ADD_FLAGS(FILE_ATTRIBUTE_VOLUME);
2850 ADD_FLAGS(FILE_ATTRIBUTE_DIRECTORY);
2851 ADD_FLAGS(FILE_ATTRIBUTE_ARCHIVE);
2852 ADD_FLAGS(FILE_ATTRIBUTE_DEVICE);
2853 ADD_FLAGS(FILE_ATTRIBUTE_NORMAL);
2854 ADD_FLAGS(FILE_ATTRIBUTE_TEMPORARY);
2855 ADD_FLAGS(FILE_ATTRIBUTE_SPARSE);
2856 ADD_FLAGS(FILE_ATTRIBUTE_REPARSE_POINT);
2857 ADD_FLAGS(FILE_ATTRIBUTE_COMPRESSED);
2858 ADD_FLAGS(FILE_ATTRIBUTE_OFFLINE);
2859 ADD_FLAGS(FILE_ATTRIBUTE_NONINDEXED);
2860 ADD_FLAGS(FILE_ATTRIBUTE_ENCRYPTED);
2861 ADD_FLAGS(FILE_ATTRIBUTE_ALL_MASK);
2863 ADD_FLAGS(FILE_DIRECTORY_FILE);
2864 ADD_FLAGS(FILE_WRITE_THROUGH);
2865 ADD_FLAGS(FILE_SEQUENTIAL_ONLY);
2866 ADD_FLAGS(FILE_NO_INTERMEDIATE_BUFFERING);
2867 ADD_FLAGS(FILE_SYNCHRONOUS_IO_ALERT);
2868 ADD_FLAGS(FILE_SYNCHRONOUS_IO_NONALERT);
2869 ADD_FLAGS(FILE_NON_DIRECTORY_FILE);
2870 ADD_FLAGS(FILE_CREATE_TREE_CONNECTION);
2871 ADD_FLAGS(FILE_COMPLETE_IF_OPLOCKED);
2872 ADD_FLAGS(FILE_NO_EA_KNOWLEDGE);
2873 ADD_FLAGS(FILE_EIGHT_DOT_THREE_ONLY);
2874 ADD_FLAGS(FILE_RANDOM_ACCESS);
2875 ADD_FLAGS(FILE_DELETE_ON_CLOSE);
2876 ADD_FLAGS(FILE_OPEN_BY_FILE_ID);
2877 ADD_FLAGS(FILE_OPEN_FOR_BACKUP_INTENT);
2878 ADD_FLAGS(FILE_NO_COMPRESSION);
2879 ADD_FLAGS(FILE_RESERVER_OPFILTER);
2880 ADD_FLAGS(FILE_OPEN_REPARSE_POINT);
2881 ADD_FLAGS(FILE_OPEN_NO_RECALL);
2882 ADD_FLAGS(FILE_OPEN_FOR_FREE_SPACE_QUERY);
2884 ADD_FLAGS(FILE_SHARE_READ);
2885 ADD_FLAGS(FILE_SHARE_WRITE);
2886 ADD_FLAGS(FILE_SHARE_DELETE);
2888 /* change notify completion filter flags */
2889 ADD_FLAGS(FILE_NOTIFY_CHANGE_FILE_NAME);
2890 ADD_FLAGS(FILE_NOTIFY_CHANGE_DIR_NAME);
2891 ADD_FLAGS(FILE_NOTIFY_CHANGE_ATTRIBUTES);
2892 ADD_FLAGS(FILE_NOTIFY_CHANGE_SIZE);
2893 ADD_FLAGS(FILE_NOTIFY_CHANGE_LAST_WRITE);
2894 ADD_FLAGS(FILE_NOTIFY_CHANGE_LAST_ACCESS);
2895 ADD_FLAGS(FILE_NOTIFY_CHANGE_CREATION);
2896 ADD_FLAGS(FILE_NOTIFY_CHANGE_EA);
2897 ADD_FLAGS(FILE_NOTIFY_CHANGE_SECURITY);
2898 ADD_FLAGS(FILE_NOTIFY_CHANGE_STREAM_NAME);
2899 ADD_FLAGS(FILE_NOTIFY_CHANGE_STREAM_SIZE);
2900 ADD_FLAGS(FILE_NOTIFY_CHANGE_STREAM_WRITE);
2901 ADD_FLAGS(FILE_NOTIFY_CHANGE_NAME);
2902 ADD_FLAGS(FILE_NOTIFY_CHANGE_ALL);
2904 /* change notify action results */
2905 ADD_FLAGS(NOTIFY_ACTION_ADDED);
2906 ADD_FLAGS(NOTIFY_ACTION_REMOVED);
2907 ADD_FLAGS(NOTIFY_ACTION_MODIFIED);
2908 ADD_FLAGS(NOTIFY_ACTION_OLD_NAME);
2909 ADD_FLAGS(NOTIFY_ACTION_NEW_NAME);
2910 ADD_FLAGS(NOTIFY_ACTION_ADDED_STREAM);
2911 ADD_FLAGS(NOTIFY_ACTION_REMOVED_STREAM);
2912 ADD_FLAGS(NOTIFY_ACTION_MODIFIED_STREAM);
2914 /* CreateDisposition values */
2915 ADD_FLAGS(FILE_SUPERSEDE);
2916 ADD_FLAGS(FILE_OPEN);
2917 ADD_FLAGS(FILE_CREATE);
2918 ADD_FLAGS(FILE_OPEN_IF);
2919 ADD_FLAGS(FILE_OVERWRITE);
2920 ADD_FLAGS(FILE_OVERWRITE_IF);
2922 ADD_FLAGS(FSCTL_DFS_GET_REFERRALS);
2923 ADD_FLAGS(FSCTL_DFS_GET_REFERRALS_EX);
2924 ADD_FLAGS(FSCTL_REQUEST_OPLOCK_LEVEL_1);
2925 ADD_FLAGS(FSCTL_REQUEST_OPLOCK_LEVEL_2);
2926 ADD_FLAGS(FSCTL_REQUEST_BATCH_OPLOCK);
2927 ADD_FLAGS(FSCTL_OPLOCK_BREAK_ACKNOWLEDGE);
2928 ADD_FLAGS(FSCTL_OPBATCH_ACK_CLOSE_PENDING);
2929 ADD_FLAGS(FSCTL_OPLOCK_BREAK_NOTIFY);
2930 ADD_FLAGS(FSCTL_GET_COMPRESSION);
2931 ADD_FLAGS(FSCTL_FILESYS_GET_STATISTICS);
2932 ADD_FLAGS(FSCTL_GET_NTFS_VOLUME_DATA);
2933 ADD_FLAGS(FSCTL_IS_VOLUME_DIRTY);
2934 ADD_FLAGS(FSCTL_FIND_FILES_BY_SID);
2935 ADD_FLAGS(FSCTL_SET_OBJECT_ID);
2936 ADD_FLAGS(FSCTL_GET_OBJECT_ID);
2937 ADD_FLAGS(FSCTL_DELETE_OBJECT_ID);
2938 ADD_FLAGS(FSCTL_SET_REPARSE_POINT);
2939 ADD_FLAGS(FSCTL_GET_REPARSE_POINT);
2940 ADD_FLAGS(FSCTL_DELETE_REPARSE_POINT);
2941 ADD_FLAGS(FSCTL_SET_OBJECT_ID_EXTENDED);
2942 ADD_FLAGS(FSCTL_CREATE_OR_GET_OBJECT_ID);
2943 ADD_FLAGS(FSCTL_SET_SPARSE);
2944 ADD_FLAGS(FSCTL_SET_ZERO_DATA);
2945 ADD_FLAGS(FSCTL_SET_ZERO_ON_DEALLOCATION);
2946 ADD_FLAGS(FSCTL_READ_FILE_USN_DATA);
2947 ADD_FLAGS(FSCTL_WRITE_USN_CLOSE_RECORD);
2948 ADD_FLAGS(FSCTL_QUERY_ALLOCATED_RANGES);
2949 ADD_FLAGS(FSCTL_QUERY_ON_DISK_VOLUME_INFO);
2950 ADD_FLAGS(FSCTL_QUERY_SPARING_INFO);
2951 ADD_FLAGS(FSCTL_FILE_LEVEL_TRIM);
2952 ADD_FLAGS(FSCTL_OFFLOAD_READ);
2953 ADD_FLAGS(FSCTL_OFFLOAD_WRITE);
2954 ADD_FLAGS(FSCTL_SET_INTEGRITY_INFORMATION);
2955 ADD_FLAGS(FSCTL_DUP_EXTENTS_TO_FILE);
2956 ADD_FLAGS(FSCTL_DUPLICATE_EXTENTS_TO_FILE_EX);
2957 ADD_FLAGS(FSCTL_STORAGE_QOS_CONTROL);
2958 ADD_FLAGS(FSCTL_SVHDX_SYNC_TUNNEL_REQUEST);
2959 ADD_FLAGS(FSCTL_QUERY_SHARED_VIRTUAL_DISK_SUPPORT);
2960 ADD_FLAGS(FSCTL_PIPE_PEEK);
2961 ADD_FLAGS(FSCTL_NAMED_PIPE_READ_WRITE);
2962 ADD_FLAGS(FSCTL_PIPE_TRANSCEIVE);
2963 ADD_FLAGS(FSCTL_PIPE_WAIT);
2964 ADD_FLAGS(FSCTL_GET_SHADOW_COPY_DATA);
2965 ADD_FLAGS(FSCTL_SRV_ENUM_SNAPS);
2966 ADD_FLAGS(FSCTL_SRV_REQUEST_RESUME_KEY);
2967 ADD_FLAGS(FSCTL_SRV_COPYCHUNK);
2968 ADD_FLAGS(FSCTL_SRV_COPYCHUNK_WRITE);
2969 ADD_FLAGS(FSCTL_SRV_READ_HASH);
2970 ADD_FLAGS(FSCTL_LMR_REQ_RESILIENCY);
2971 ADD_FLAGS(FSCTL_LMR_SET_LINK_TRACKING_INFORMATION);
2972 ADD_FLAGS(FSCTL_QUERY_NETWORK_INTERFACE_INFO);
2974 ADD_FLAGS(SYMLINK_ERROR_TAG);
2975 ADD_FLAGS(SYMLINK_FLAG_RELATIVE);
2976 ADD_FLAGS(SYMLINK_ADMIN);
2977 ADD_FLAGS(SYMLINK_UNTRUSTED);
2978 ADD_FLAGS(SYMLINK_TRUST_UNKNOWN);
2979 ADD_FLAGS(SYMLINK_TRUST_MASK);
2981 ADD_FLAGS(IO_REPARSE_TAG_RESERVED_ZERO);
2982 ADD_FLAGS(IO_REPARSE_TAG_SYMLINK);
2983 ADD_FLAGS(IO_REPARSE_TAG_MOUNT_POINT);
2984 ADD_FLAGS(IO_REPARSE_TAG_HSM);
2985 ADD_FLAGS(IO_REPARSE_TAG_SIS);
2986 ADD_FLAGS(IO_REPARSE_TAG_DFS);
2987 ADD_FLAGS(IO_REPARSE_TAG_NFS);
2989 #define ADD_STRING(val) PyModule_AddObject(m, #val, PyBytes_FromString(val))
2991 ADD_STRING(SMB2_CREATE_TAG_EXTA);
2992 ADD_STRING(SMB2_CREATE_TAG_MXAC);
2993 ADD_STRING(SMB2_CREATE_TAG_SECD);
2994 ADD_STRING(SMB2_CREATE_TAG_DHNQ);
2995 ADD_STRING(SMB2_CREATE_TAG_DHNC);
2996 ADD_STRING(SMB2_CREATE_TAG_ALSI);
2997 ADD_STRING(SMB2_CREATE_TAG_TWRP);
2998 ADD_STRING(SMB2_CREATE_TAG_QFID);
2999 ADD_STRING(SMB2_CREATE_TAG_RQLS);
3000 ADD_STRING(SMB2_CREATE_TAG_DH2Q);
3001 ADD_STRING(SMB2_CREATE_TAG_DH2C);
3002 ADD_STRING(SMB2_CREATE_TAG_AAPL);
3003 ADD_STRING(SMB2_CREATE_TAG_APP_INSTANCE_ID);
3004 ADD_STRING(SVHDX_OPEN_DEVICE_CONTEXT);
3005 ADD_STRING(SMB2_CREATE_TAG_POSIX);
3006 ADD_FLAGS(SMB2_FIND_POSIX_INFORMATION);
3007 ADD_FLAGS(FILE_SUPERSEDE);
3008 ADD_FLAGS(FILE_OPEN);
3009 ADD_FLAGS(FILE_CREATE);
3010 ADD_FLAGS(FILE_OPEN_IF);
3011 ADD_FLAGS(FILE_OVERWRITE);
3012 ADD_FLAGS(FILE_OVERWRITE_IF);
3013 ADD_FLAGS(FILE_DIRECTORY_FILE);
3015 ADD_FLAGS(SMB2_CLOSE_FLAGS_FULL_INFORMATION);
3017 return m;