smbXsrv_session: Remove a "can't happen" NULL check
[Samba.git] / source3 / libsmb / pylibsmb.c
blob52ba449bfa9384046cb71b952cb217d4e191c890
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 "lib/replace/system/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 int ret;
1892 * Build a dictionary representing the file info.
1894 file = Py_BuildValue("{s:s,s:I,"
1895 "s:K,s:K,"
1896 "s:l,s:l,s:l,s:l,"
1897 "s:i,s:K,s:i,s:i,s:I,"
1898 "s:s,s:s}",
1899 "name", finfo->name,
1900 "attrib", finfo->attr,
1902 "size", finfo->size,
1903 "allocaction_size", finfo->allocated_size,
1905 "btime",
1906 convert_timespec_to_time_t(finfo->btime_ts),
1907 "atime",
1908 convert_timespec_to_time_t(finfo->atime_ts),
1909 "mtime",
1910 convert_timespec_to_time_t(finfo->mtime_ts),
1911 "ctime",
1912 convert_timespec_to_time_t(finfo->ctime_ts),
1914 "perms", finfo->st_ex_mode,
1915 "ino", finfo->ino,
1916 "dev", finfo->st_ex_dev,
1917 "nlink", finfo->st_ex_nlink,
1918 "reparse_tag", finfo->reparse_tag,
1920 "owner_sid",
1921 dom_sid_string(finfo, &finfo->owner_sid),
1922 "group_sid",
1923 dom_sid_string(finfo, &finfo->group_sid));
1924 if (file == NULL) {
1925 return NT_STATUS_NO_MEMORY;
1928 ret = PyList_Append(result, file);
1929 Py_CLEAR(file);
1930 if (ret == -1) {
1931 return NT_STATUS_INTERNAL_ERROR;
1934 return NT_STATUS_OK;
1938 * Helper to add directory listing entries to an overall Python list
1940 static NTSTATUS list_helper(struct file_info *finfo,
1941 const char *mask, void *state)
1943 PyObject *result = (PyObject *)state;
1944 PyObject *file = NULL;
1945 PyObject *size = NULL;
1946 int ret;
1948 /* suppress '.' and '..' in the results we return */
1949 if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
1950 return NT_STATUS_OK;
1952 size = PyLong_FromUnsignedLongLong(finfo->size);
1954 * Build a dictionary representing the file info.
1955 * Note: Windows does not always return short_name (so it may be None)
1957 file = Py_BuildValue("{s:s,s:i,s:s,s:O,s:l}",
1958 "name", finfo->name,
1959 "attrib", (int)finfo->attr,
1960 "short_name", finfo->short_name,
1961 "size", size,
1962 "mtime",
1963 convert_timespec_to_time_t(finfo->mtime_ts));
1965 Py_CLEAR(size);
1967 if (file == NULL) {
1968 return NT_STATUS_NO_MEMORY;
1971 if (finfo->attr & FILE_ATTRIBUTE_REPARSE_POINT) {
1972 unsigned long tag = finfo->reparse_tag;
1974 ret = PyDict_SetItemString(
1975 file,
1976 "reparse_tag",
1977 PyLong_FromUnsignedLong(tag));
1978 if (ret == -1) {
1979 return NT_STATUS_INTERNAL_ERROR;
1983 ret = PyList_Append(result, file);
1984 Py_CLEAR(file);
1985 if (ret == -1) {
1986 return NT_STATUS_INTERNAL_ERROR;
1989 return NT_STATUS_OK;
1992 struct do_listing_state {
1993 const char *mask;
1994 NTSTATUS (*callback_fn)(
1995 struct file_info *finfo,
1996 const char *mask,
1997 void *private_data);
1998 void *private_data;
1999 NTSTATUS status;
2002 static void do_listing_cb(struct tevent_req *subreq)
2004 struct do_listing_state *state = tevent_req_callback_data_void(subreq);
2005 struct file_info *finfo = NULL;
2007 state->status = cli_list_recv(subreq, NULL, &finfo);
2008 if (!NT_STATUS_IS_OK(state->status)) {
2009 return;
2011 state->callback_fn(finfo, state->mask, state->private_data);
2012 TALLOC_FREE(finfo);
2015 static NTSTATUS do_listing(struct py_cli_state *self,
2016 const char *base_dir, const char *user_mask,
2017 uint16_t attribute,
2018 unsigned int info_level,
2019 NTSTATUS (*callback_fn)(struct file_info *,
2020 const char *, void *),
2021 void *priv)
2023 char *mask = NULL;
2024 struct do_listing_state state = {
2025 .mask = mask,
2026 .callback_fn = callback_fn,
2027 .private_data = priv,
2029 struct tevent_req *req = NULL;
2030 NTSTATUS status;
2032 if (user_mask == NULL) {
2033 mask = talloc_asprintf(NULL, "%s\\*", base_dir);
2034 } else {
2035 mask = talloc_asprintf(NULL, "%s\\%s", base_dir, user_mask);
2038 if (mask == NULL) {
2039 return NT_STATUS_NO_MEMORY;
2041 dos_format(mask);
2043 req = cli_list_send(NULL, self->ev, self->cli, mask, attribute,
2044 info_level);
2045 if (req == NULL) {
2046 status = NT_STATUS_NO_MEMORY;
2047 goto done;
2049 tevent_req_set_callback(req, do_listing_cb, &state);
2051 if (!py_tevent_req_wait_exc(self, req)) {
2052 return NT_STATUS_INTERNAL_ERROR;
2054 TALLOC_FREE(req);
2056 status = state.status;
2057 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_FILES)) {
2058 status = NT_STATUS_OK;
2061 done:
2062 TALLOC_FREE(mask);
2063 return status;
2066 static PyObject *py_cli_list(struct py_cli_state *self,
2067 PyObject *args,
2068 PyObject *kwds)
2070 char *base_dir;
2071 char *user_mask = NULL;
2072 unsigned int attribute = LIST_ATTRIBUTE_MASK;
2073 unsigned int info_level = 0;
2074 NTSTATUS status;
2075 enum protocol_types proto = smbXcli_conn_protocol(self->cli->conn);
2076 PyObject *result = NULL;
2077 const char *kwlist[] = { "directory", "mask", "attribs",
2078 "info_level", NULL };
2079 NTSTATUS (*callback_fn)(struct file_info *, const char *, void *) =
2080 &list_helper;
2082 if (!ParseTupleAndKeywords(args, kwds, "z|sII:list", kwlist,
2083 &base_dir, &user_mask, &attribute,
2084 &info_level)) {
2085 return NULL;
2088 result = Py_BuildValue("[]");
2089 if (result == NULL) {
2090 return NULL;
2093 if (!info_level) {
2094 if (proto >= PROTOCOL_SMB2_02) {
2095 info_level = SMB2_FIND_ID_BOTH_DIRECTORY_INFO;
2096 } else {
2097 info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
2101 if (info_level == SMB2_FIND_POSIX_INFORMATION) {
2102 callback_fn = &list_posix_helper;
2104 status = do_listing(self, base_dir, user_mask, attribute,
2105 info_level, callback_fn, result);
2107 if (!NT_STATUS_IS_OK(status)) {
2108 Py_XDECREF(result);
2109 PyErr_SetNTSTATUS(status);
2110 return NULL;
2113 return result;
2116 static PyObject *py_smb_unlink(struct py_cli_state *self, PyObject *args)
2118 NTSTATUS status;
2119 const char *filename = NULL;
2120 struct tevent_req *req = NULL;
2121 const uint32_t attrs = (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2123 if (!PyArg_ParseTuple(args, "s:unlink", &filename)) {
2124 return NULL;
2127 req = cli_unlink_send(NULL, self->ev, self->cli, filename, attrs);
2128 if (!py_tevent_req_wait_exc(self, req)) {
2129 return NULL;
2131 status = cli_unlink_recv(req);
2132 TALLOC_FREE(req);
2133 PyErr_NTSTATUS_NOT_OK_RAISE(status);
2135 Py_RETURN_NONE;
2138 static PyObject *py_smb_rmdir(struct py_cli_state *self, PyObject *args)
2140 NTSTATUS status;
2141 struct tevent_req *req = NULL;
2142 const char *dirname = NULL;
2144 if (!PyArg_ParseTuple(args, "s:rmdir", &dirname)) {
2145 return NULL;
2148 req = cli_rmdir_send(NULL, self->ev, self->cli, dirname);
2149 if (!py_tevent_req_wait_exc(self, req)) {
2150 return NULL;
2152 status = cli_rmdir_recv(req);
2153 TALLOC_FREE(req);
2154 PyErr_NTSTATUS_NOT_OK_RAISE(status);
2156 Py_RETURN_NONE;
2160 * Create a directory
2162 static PyObject *py_smb_mkdir(struct py_cli_state *self, PyObject *args)
2164 NTSTATUS status;
2165 const char *dirname = NULL;
2166 struct tevent_req *req = NULL;
2168 if (!PyArg_ParseTuple(args, "s:mkdir", &dirname)) {
2169 return NULL;
2172 req = cli_mkdir_send(NULL, self->ev, self->cli, dirname);
2173 if (!py_tevent_req_wait_exc(self, req)) {
2174 return NULL;
2176 status = cli_mkdir_recv(req);
2177 TALLOC_FREE(req);
2178 PyErr_NTSTATUS_NOT_OK_RAISE(status);
2180 Py_RETURN_NONE;
2184 * Does a whoami call
2186 static PyObject *py_smb_posix_whoami(struct py_cli_state *self,
2187 PyObject *Py_UNUSED(ignored))
2189 TALLOC_CTX *frame = talloc_stackframe();
2190 NTSTATUS status;
2191 struct tevent_req *req = NULL;
2192 uint64_t uid;
2193 uint64_t gid;
2194 uint32_t num_gids;
2195 uint64_t *gids = NULL;
2196 uint32_t num_sids;
2197 struct dom_sid *sids = NULL;
2198 bool guest;
2199 PyObject *py_gids = NULL;
2200 PyObject *py_sids = NULL;
2201 PyObject *py_guest = NULL;
2202 PyObject *py_ret = NULL;
2203 Py_ssize_t i;
2205 req = cli_posix_whoami_send(frame, self->ev, self->cli);
2206 if (!py_tevent_req_wait_exc(self, req)) {
2207 goto fail;
2209 status = cli_posix_whoami_recv(req,
2210 frame,
2211 &uid,
2212 &gid,
2213 &num_gids,
2214 &gids,
2215 &num_sids,
2216 &sids,
2217 &guest);
2218 if (!NT_STATUS_IS_OK(status)) {
2219 PyErr_SetNTSTATUS(status);
2220 goto fail;
2222 if (num_gids > PY_SSIZE_T_MAX) {
2223 PyErr_SetString(PyExc_OverflowError, "posix_whoami: Too many GIDs");
2224 goto fail;
2226 if (num_sids > PY_SSIZE_T_MAX) {
2227 PyErr_SetString(PyExc_OverflowError, "posix_whoami: Too many SIDs");
2228 goto fail;
2231 py_gids = PyList_New(num_gids);
2232 if (!py_gids) {
2233 goto fail;
2235 for (i = 0; i < num_gids; ++i) {
2236 int ret;
2237 PyObject *py_item = PyLong_FromUnsignedLongLong(gids[i]);
2238 if (!py_item) {
2239 goto fail2;
2242 ret = PyList_SetItem(py_gids, i, py_item);
2243 if (ret) {
2244 goto fail2;
2247 py_sids = PyList_New(num_sids);
2248 if (!py_sids) {
2249 goto fail2;
2251 for (i = 0; i < num_sids; ++i) {
2252 int ret;
2253 struct dom_sid *sid;
2254 PyObject *py_item;
2256 sid = dom_sid_dup(frame, &sids[i]);
2257 if (!sid) {
2258 PyErr_NoMemory();
2259 goto fail3;
2262 py_item = pytalloc_steal(dom_sid_Type, sid);
2263 if (!py_item) {
2264 PyErr_NoMemory();
2265 goto fail3;
2268 ret = PyList_SetItem(py_sids, i, py_item);
2269 if (ret) {
2270 goto fail3;
2274 py_guest = guest ? Py_True : Py_False;
2276 py_ret = Py_BuildValue("KKNNO",
2277 uid,
2278 gid,
2279 py_gids,
2280 py_sids,
2281 py_guest);
2282 if (!py_ret) {
2283 goto fail3;
2286 TALLOC_FREE(frame);
2287 return py_ret;
2289 fail3:
2290 Py_CLEAR(py_sids);
2292 fail2:
2293 Py_CLEAR(py_gids);
2295 fail:
2296 TALLOC_FREE(frame);
2297 return NULL;
2301 * Checks existence of a directory
2303 static bool check_dir_path(struct py_cli_state *self, const char *path)
2305 NTSTATUS status;
2306 struct tevent_req *req = NULL;
2308 req = cli_chkpath_send(NULL, self->ev, self->cli, path);
2309 if (!py_tevent_req_wait_exc(self, req)) {
2310 return false;
2312 status = cli_chkpath_recv(req);
2313 TALLOC_FREE(req);
2315 return NT_STATUS_IS_OK(status);
2318 static PyObject *py_smb_chkpath(struct py_cli_state *self, PyObject *args)
2320 const char *path = NULL;
2321 bool dir_exists;
2323 if (!PyArg_ParseTuple(args, "s:chkpath", &path)) {
2324 return NULL;
2327 dir_exists = check_dir_path(self, path);
2328 return PyBool_FromLong(dir_exists);
2331 static PyObject *py_smb_have_posix(struct py_cli_state *self,
2332 PyObject *Py_UNUSED(ignored))
2334 bool posix = smbXcli_conn_have_posix(self->cli->conn);
2336 if (posix) {
2337 Py_RETURN_TRUE;
2339 Py_RETURN_FALSE;
2342 static PyObject *py_smb_protocol(struct py_cli_state *self,
2343 PyObject *Py_UNUSED(ignored))
2345 enum protocol_types proto = smbXcli_conn_protocol(self->cli->conn);
2346 PyObject *result = PyLong_FromLong(proto);
2347 return result;
2350 static PyObject *py_smb_get_sd(struct py_cli_state *self, PyObject *args)
2352 int fnum;
2353 unsigned sinfo;
2354 struct tevent_req *req = NULL;
2355 struct security_descriptor *sd = NULL;
2356 NTSTATUS status;
2358 if (!PyArg_ParseTuple(args, "iI:get_acl", &fnum, &sinfo)) {
2359 return NULL;
2362 req = cli_query_security_descriptor_send(
2363 NULL, self->ev, self->cli, fnum, sinfo);
2364 if (!py_tevent_req_wait_exc(self, req)) {
2365 return NULL;
2367 status = cli_query_security_descriptor_recv(req, NULL, &sd);
2368 PyErr_NTSTATUS_NOT_OK_RAISE(status);
2370 return py_return_ndr_struct(
2371 "samba.dcerpc.security", "descriptor", sd, sd);
2374 static PyObject *py_smb_set_sd(struct py_cli_state *self, PyObject *args)
2376 PyObject *py_sd = NULL;
2377 struct tevent_req *req = NULL;
2378 struct security_descriptor *sd = NULL;
2379 uint16_t fnum;
2380 unsigned int sinfo;
2381 NTSTATUS status;
2383 if (!PyArg_ParseTuple(args, "iOI:set_sd", &fnum, &py_sd, &sinfo)) {
2384 return NULL;
2387 sd = pytalloc_get_type(py_sd, struct security_descriptor);
2388 if (!sd) {
2389 PyErr_Format(PyExc_TypeError,
2390 "Expected dcerpc.security.descriptor as argument, got %s",
2391 pytalloc_get_name(py_sd));
2392 return NULL;
2395 req = cli_set_security_descriptor_send(
2396 NULL, self->ev, self->cli, fnum, sinfo, sd);
2397 if (!py_tevent_req_wait_exc(self, req)) {
2398 return NULL;
2401 status = cli_set_security_descriptor_recv(req);
2402 PyErr_NTSTATUS_NOT_OK_RAISE(status);
2404 Py_RETURN_NONE;
2407 static PyObject *py_smb_smb1_posix(
2408 struct py_cli_state *self, PyObject *Py_UNUSED(ignored))
2410 NTSTATUS status;
2411 struct tevent_req *req = NULL;
2412 uint16_t major, minor;
2413 uint32_t caplow, caphigh;
2414 PyObject *result = NULL;
2416 req = cli_unix_extensions_version_send(NULL, self->ev, self->cli);
2417 if (!py_tevent_req_wait_exc(self, req)) {
2418 return NULL;
2420 status = cli_unix_extensions_version_recv(
2421 req, &major, &minor, &caplow, &caphigh);
2422 TALLOC_FREE(req);
2423 if (!NT_STATUS_IS_OK(status)) {
2424 PyErr_SetNTSTATUS(status);
2425 return NULL;
2428 req = cli_set_unix_extensions_capabilities_send(
2429 NULL, self->ev, self->cli, major, minor, caplow, caphigh);
2430 if (!py_tevent_req_wait_exc(self, req)) {
2431 return NULL;
2433 status = cli_set_unix_extensions_capabilities_recv(req);
2434 TALLOC_FREE(req);
2435 if (!NT_STATUS_IS_OK(status)) {
2436 PyErr_SetNTSTATUS(status);
2437 return NULL;
2440 result = Py_BuildValue(
2441 "[IIII]",
2442 (unsigned)minor,
2443 (unsigned)major,
2444 (unsigned)caplow,
2445 (unsigned)caphigh);
2446 return result;
2449 static PyObject *py_smb_smb1_readlink(
2450 struct py_cli_state *self, PyObject *args)
2452 NTSTATUS status;
2453 const char *filename = NULL;
2454 struct tevent_req *req = NULL;
2455 char *target = NULL;
2456 PyObject *result = NULL;
2458 if (!PyArg_ParseTuple(args, "s:smb1_readlink", &filename)) {
2459 return NULL;
2462 req = cli_posix_readlink_send(NULL, self->ev, self->cli, filename);
2463 if (!py_tevent_req_wait_exc(self, req)) {
2464 return NULL;
2466 status = cli_posix_readlink_recv(req, NULL, &target);
2467 TALLOC_FREE(req);
2468 if (!NT_STATUS_IS_OK(status)) {
2469 PyErr_SetNTSTATUS(status);
2470 return NULL;
2473 result = PyBytes_FromString(target);
2474 TALLOC_FREE(target);
2475 return result;
2478 static PyObject *py_smb_smb1_symlink(
2479 struct py_cli_state *self, PyObject *args)
2481 NTSTATUS status;
2482 const char *target = NULL, *newname = NULL;
2483 struct tevent_req *req = NULL;
2485 if (!PyArg_ParseTuple(args, "ss:smb1_symlink", &target, &newname)) {
2486 return NULL;
2489 req = cli_posix_symlink_send(
2490 NULL, self->ev, self->cli, target, newname);
2491 if (!py_tevent_req_wait_exc(self, req)) {
2492 return NULL;
2494 status = cli_posix_symlink_recv(req);
2495 TALLOC_FREE(req);
2496 if (!NT_STATUS_IS_OK(status)) {
2497 PyErr_SetNTSTATUS(status);
2498 return NULL;
2501 Py_RETURN_NONE;
2504 static PyObject *py_smb_smb1_stat(
2505 struct py_cli_state *self, PyObject *args)
2507 NTSTATUS status;
2508 const char *fname = NULL;
2509 struct tevent_req *req = NULL;
2510 struct stat_ex sbuf = { .st_ex_nlink = 0, };
2512 if (!PyArg_ParseTuple(args, "s:smb1_stat", &fname)) {
2513 return NULL;
2516 req = cli_posix_stat_send(NULL, self->ev, self->cli, fname);
2517 if (!py_tevent_req_wait_exc(self, req)) {
2518 return NULL;
2520 status = cli_posix_stat_recv(req, &sbuf);
2521 TALLOC_FREE(req);
2522 if (!NT_STATUS_IS_OK(status)) {
2523 PyErr_SetNTSTATUS(status);
2524 return NULL;
2527 return Py_BuildValue(
2528 "{sLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsL}",
2529 "dev",
2530 (unsigned long long)sbuf.st_ex_dev,
2531 "ino",
2532 (unsigned long long)sbuf.st_ex_ino,
2533 "mode",
2534 (unsigned long long)sbuf.st_ex_mode,
2535 "nlink",
2536 (unsigned long long)sbuf.st_ex_nlink,
2537 "uid",
2538 (unsigned long long)sbuf.st_ex_uid,
2539 "gid",
2540 (unsigned long long)sbuf.st_ex_gid,
2541 "rdev",
2542 (unsigned long long)sbuf.st_ex_size,
2543 "atime_sec",
2544 (unsigned long long)sbuf.st_ex_atime.tv_sec,
2545 "atime_nsec",
2546 (unsigned long long)sbuf.st_ex_atime.tv_nsec,
2547 "mtime_sec",
2548 (unsigned long long)sbuf.st_ex_mtime.tv_sec,
2549 "mtime_nsec",
2550 (unsigned long long)sbuf.st_ex_mtime.tv_nsec,
2551 "ctime_sec",
2552 (unsigned long long)sbuf.st_ex_ctime.tv_sec,
2553 "ctime_nsec",
2554 (unsigned long long)sbuf.st_ex_ctime.tv_nsec,
2555 "btime_sec",
2556 (unsigned long long)sbuf.st_ex_btime.tv_sec,
2557 "btime_nsec",
2558 (unsigned long long)sbuf.st_ex_btime.tv_nsec,
2559 "cached_dos_attributes",
2560 (unsigned long long)sbuf.cached_dos_attributes,
2561 "blksize",
2562 (unsigned long long)sbuf.st_ex_blksize,
2563 "blocks",
2564 (unsigned long long)sbuf.st_ex_blocks,
2565 "flags",
2566 (unsigned long long)sbuf.st_ex_flags,
2567 "iflags",
2568 (unsigned long long)sbuf.st_ex_iflags);
2571 static PyObject *py_cli_mknod(
2572 struct py_cli_state *self, PyObject *args, PyObject *kwds)
2574 char *fname = NULL;
2575 int mode = 0, major = 0, minor = 0, dev = 0;
2576 struct tevent_req *req = NULL;
2577 static const char *kwlist[] = {
2578 "fname", "mode", "major", "minor", NULL,
2580 NTSTATUS status;
2581 bool ok;
2583 ok = ParseTupleAndKeywords(
2584 args,
2585 kwds,
2586 "sI|II:mknod",
2587 kwlist,
2588 &fname,
2589 &mode,
2590 &major,
2591 &minor);
2592 if (!ok) {
2593 return NULL;
2596 #if defined(HAVE_MAKEDEV)
2597 dev = makedev(major, minor);
2598 #endif
2600 req = cli_mknod_send(
2601 NULL, self->ev, self->cli, fname, mode, dev);
2602 if (!py_tevent_req_wait_exc(self, req)) {
2603 return NULL;
2605 status = cli_mknod_recv(req);
2606 TALLOC_FREE(req);
2607 if (!NT_STATUS_IS_OK(status)) {
2608 PyErr_SetNTSTATUS(status);
2609 return NULL;
2611 Py_RETURN_NONE;
2614 static PyObject *py_cli_fsctl(
2615 struct py_cli_state *self, PyObject *args, PyObject *kwds)
2617 int fnum, ctl_code;
2618 int max_out = 0;
2619 char *buf = NULL;
2620 Py_ssize_t buflen;
2621 DATA_BLOB in = { .data = NULL, };
2622 DATA_BLOB out = { .data = NULL, };
2623 struct tevent_req *req = NULL;
2624 PyObject *result = NULL;
2625 static const char *kwlist[] = {
2626 "fnum", "ctl_code", "in", "max_out", NULL,
2628 NTSTATUS status;
2629 bool ok;
2631 ok = ParseTupleAndKeywords(
2632 args,
2633 kwds,
2634 "ii" PYARG_BYTES_LEN "i",
2635 kwlist,
2636 &fnum,
2637 &ctl_code,
2638 &buf,
2639 &buflen,
2640 &max_out);
2641 if (!ok) {
2642 return NULL;
2645 in = (DATA_BLOB) { .data = (uint8_t *)buf, .length = buflen, };
2647 req = cli_fsctl_send(
2648 NULL, self->ev, self->cli, fnum, ctl_code, &in, max_out);
2650 if (!py_tevent_req_wait_exc(self, req)) {
2651 return NULL;
2654 status = cli_fsctl_recv(req, NULL, &out);
2655 if (!NT_STATUS_IS_OK(status)) {
2656 PyErr_SetNTSTATUS(status);
2657 return NULL;
2660 result = PyBytes_FromStringAndSize((char *)out.data, out.length);
2661 data_blob_free(&out);
2662 return result;
2665 static PyMethodDef py_cli_state_methods[] = {
2666 { "settimeout", (PyCFunction)py_cli_settimeout, METH_VARARGS,
2667 "settimeout(new_timeout_msecs) => return old_timeout_msecs" },
2668 { "echo", (PyCFunction)py_cli_echo, METH_NOARGS,
2669 "Ping the server connection" },
2670 { "create", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_create),
2671 METH_VARARGS|METH_KEYWORDS,
2672 "Open a file" },
2673 { "create_ex",
2674 PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_create_ex),
2675 METH_VARARGS|METH_KEYWORDS,
2676 "Open a file, SMB2 version returning create contexts" },
2677 { "close", (PyCFunction)py_cli_close, METH_VARARGS,
2678 "Close a file handle" },
2679 { "write", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_write),
2680 METH_VARARGS|METH_KEYWORDS,
2681 "Write to a file handle" },
2682 { "read", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_read),
2683 METH_VARARGS|METH_KEYWORDS,
2684 "Read from a file handle" },
2685 { "truncate", PY_DISCARD_FUNC_SIG(PyCFunction,
2686 py_cli_ftruncate),
2687 METH_VARARGS|METH_KEYWORDS,
2688 "Truncate a file" },
2689 { "delete_on_close", PY_DISCARD_FUNC_SIG(PyCFunction,
2690 py_cli_delete_on_close),
2691 METH_VARARGS|METH_KEYWORDS,
2692 "Set/Reset the delete on close flag" },
2693 { "notify", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_notify),
2694 METH_VARARGS|METH_KEYWORDS,
2695 "Wait for change notifications: \n"
2696 "notify(fnum, buffer_size, completion_filter...) -> "
2697 "libsmb_samba_internal.Notify request handle\n" },
2698 { "list", PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_list),
2699 METH_VARARGS|METH_KEYWORDS,
2700 "list(directory, mask='*', attribs=DEFAULT_ATTRS) -> "
2701 "directory contents as a dictionary\n"
2702 "\t\tDEFAULT_ATTRS: FILE_ATTRIBUTE_SYSTEM | "
2703 "FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_ARCHIVE\n\n"
2704 "\t\tList contents of a directory. The keys are, \n"
2705 "\t\t\tname: Long name of the directory item\n"
2706 "\t\t\tshort_name: Short name of the directory item\n"
2707 "\t\t\tsize: File size in bytes\n"
2708 "\t\t\tattrib: Attributes\n"
2709 "\t\t\tmtime: Modification time\n" },
2710 { "get_oplock_break", (PyCFunction)py_cli_get_oplock_break,
2711 METH_VARARGS, "Wait for an oplock break" },
2712 { "unlink", (PyCFunction)py_smb_unlink,
2713 METH_VARARGS,
2714 "unlink(path) -> None\n\n \t\tDelete a file." },
2715 { "mkdir", (PyCFunction)py_smb_mkdir, METH_VARARGS,
2716 "mkdir(path) -> None\n\n \t\tCreate a directory." },
2717 { "posix_whoami", (PyCFunction)py_smb_posix_whoami, METH_NOARGS,
2718 "posix_whoami() -> (uid, gid, gids, sids, guest)" },
2719 { "rmdir", (PyCFunction)py_smb_rmdir, METH_VARARGS,
2720 "rmdir(path) -> None\n\n \t\tDelete a directory." },
2721 { "rename",
2722 PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_rename),
2723 METH_VARARGS|METH_KEYWORDS,
2724 "rename(src,dst) -> None\n\n \t\tRename a file." },
2725 { "chkpath", (PyCFunction)py_smb_chkpath, METH_VARARGS,
2726 "chkpath(dir_path) -> True or False\n\n"
2727 "\t\tReturn true if directory exists, false otherwise." },
2728 { "savefile", (PyCFunction)py_smb_savefile, METH_VARARGS,
2729 "savefile(path, bytes) -> None\n\n"
2730 "\t\tWrite bytes to file." },
2731 { "loadfile", (PyCFunction)py_smb_loadfile, METH_VARARGS,
2732 "loadfile(path) -> file contents as a bytes object"
2733 "\n\n\t\tRead contents of a file." },
2734 { "get_sd", (PyCFunction)py_smb_get_sd, METH_VARARGS,
2735 "get_sd(fnum[, security_info=0]) -> security_descriptor object\n\n"
2736 "\t\tGet security descriptor for opened file." },
2737 { "set_sd", (PyCFunction)py_smb_set_sd, METH_VARARGS,
2738 "set_sd(fnum, security_descriptor[, security_info=0]) -> None\n\n"
2739 "\t\tSet security descriptor for opened file." },
2740 { "protocol",
2741 (PyCFunction)py_smb_protocol,
2742 METH_NOARGS,
2743 "protocol() -> Number"
2745 { "have_posix",
2746 (PyCFunction)py_smb_have_posix,
2747 METH_NOARGS,
2748 "have_posix() -> True/False\n\n"
2749 "\t\tReturn if the server has posix extensions"
2751 { "smb1_posix",
2752 (PyCFunction)py_smb_smb1_posix,
2753 METH_NOARGS,
2754 "Negotiate SMB1 posix extensions",
2756 { "smb1_readlink",
2757 (PyCFunction)py_smb_smb1_readlink,
2758 METH_VARARGS,
2759 "smb1_readlink(path) -> link target",
2761 { "smb1_symlink",
2762 (PyCFunction)py_smb_smb1_symlink,
2763 METH_VARARGS,
2764 "smb1_symlink(target, newname) -> None",
2766 { "smb1_stat",
2767 (PyCFunction)py_smb_smb1_stat,
2768 METH_VARARGS,
2769 "smb1_stat(path) -> stat info",
2771 { "fsctl",
2772 (PyCFunction)py_cli_fsctl,
2773 METH_VARARGS|METH_KEYWORDS,
2774 "fsctl(fnum, ctl_code, in_bytes, max_out) -> out_bytes",
2776 { "mknod",
2777 PY_DISCARD_FUNC_SIG(PyCFunction, py_cli_mknod),
2778 METH_VARARGS|METH_KEYWORDS,
2779 "mknod(path, mode | major, minor)",
2781 { NULL, NULL, 0, NULL }
2784 static PyTypeObject py_cli_state_type = {
2785 PyVarObject_HEAD_INIT(NULL, 0)
2786 .tp_name = "libsmb_samba_cwrapper.LibsmbCConn",
2787 .tp_basicsize = sizeof(struct py_cli_state),
2788 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
2789 .tp_doc = "libsmb cwrapper connection",
2790 .tp_new = py_cli_state_new,
2791 .tp_init = (initproc)py_cli_state_init,
2792 .tp_dealloc = (destructor)py_cli_state_dealloc,
2793 .tp_methods = py_cli_state_methods,
2796 static PyMethodDef py_libsmb_methods[] = {
2797 {0},
2800 void initlibsmb_samba_cwrapper(void);
2802 static struct PyModuleDef moduledef = {
2803 PyModuleDef_HEAD_INIT,
2804 .m_name = "libsmb_samba_cwrapper",
2805 .m_doc = "libsmb wrapper",
2806 .m_size = -1,
2807 .m_methods = py_libsmb_methods,
2810 MODULE_INIT_FUNC(libsmb_samba_cwrapper)
2812 PyObject *m = NULL;
2813 PyObject *mod = NULL;
2815 talloc_stackframe();
2817 if (PyType_Ready(&py_cli_state_type) < 0) {
2818 return NULL;
2820 if (PyType_Ready(&py_cli_notify_state_type) < 0) {
2821 return NULL;
2824 m = PyModule_Create(&moduledef);
2825 if (m == NULL) {
2826 return m;
2829 /* Import dom_sid type from dcerpc.security */
2830 mod = PyImport_ImportModule("samba.dcerpc.security");
2831 if (mod == NULL) {
2832 return NULL;
2835 dom_sid_Type = (PyTypeObject *)PyObject_GetAttrString(mod, "dom_sid");
2836 if (dom_sid_Type == NULL) {
2837 Py_DECREF(mod);
2838 return NULL;
2841 Py_INCREF(&py_cli_state_type);
2842 PyModule_AddObject(m, "LibsmbCConn", (PyObject *)&py_cli_state_type);
2844 #define ADD_FLAGS(val) PyModule_AddObject(m, #val, PyLong_FromLong(val))
2846 ADD_FLAGS(PROTOCOL_NONE);
2847 ADD_FLAGS(PROTOCOL_CORE);
2848 ADD_FLAGS(PROTOCOL_COREPLUS);
2849 ADD_FLAGS(PROTOCOL_LANMAN1);
2850 ADD_FLAGS(PROTOCOL_LANMAN2);
2851 ADD_FLAGS(PROTOCOL_NT1);
2852 ADD_FLAGS(PROTOCOL_SMB2_02);
2853 ADD_FLAGS(PROTOCOL_SMB2_10);
2854 ADD_FLAGS(PROTOCOL_SMB3_00);
2855 ADD_FLAGS(PROTOCOL_SMB3_02);
2856 ADD_FLAGS(PROTOCOL_SMB3_11);
2858 ADD_FLAGS(FILE_ATTRIBUTE_READONLY);
2859 ADD_FLAGS(FILE_ATTRIBUTE_HIDDEN);
2860 ADD_FLAGS(FILE_ATTRIBUTE_SYSTEM);
2861 ADD_FLAGS(FILE_ATTRIBUTE_VOLUME);
2862 ADD_FLAGS(FILE_ATTRIBUTE_DIRECTORY);
2863 ADD_FLAGS(FILE_ATTRIBUTE_ARCHIVE);
2864 ADD_FLAGS(FILE_ATTRIBUTE_DEVICE);
2865 ADD_FLAGS(FILE_ATTRIBUTE_NORMAL);
2866 ADD_FLAGS(FILE_ATTRIBUTE_TEMPORARY);
2867 ADD_FLAGS(FILE_ATTRIBUTE_SPARSE);
2868 ADD_FLAGS(FILE_ATTRIBUTE_REPARSE_POINT);
2869 ADD_FLAGS(FILE_ATTRIBUTE_COMPRESSED);
2870 ADD_FLAGS(FILE_ATTRIBUTE_OFFLINE);
2871 ADD_FLAGS(FILE_ATTRIBUTE_NONINDEXED);
2872 ADD_FLAGS(FILE_ATTRIBUTE_ENCRYPTED);
2873 ADD_FLAGS(FILE_ATTRIBUTE_ALL_MASK);
2875 ADD_FLAGS(FILE_DIRECTORY_FILE);
2876 ADD_FLAGS(FILE_WRITE_THROUGH);
2877 ADD_FLAGS(FILE_SEQUENTIAL_ONLY);
2878 ADD_FLAGS(FILE_NO_INTERMEDIATE_BUFFERING);
2879 ADD_FLAGS(FILE_SYNCHRONOUS_IO_ALERT);
2880 ADD_FLAGS(FILE_SYNCHRONOUS_IO_NONALERT);
2881 ADD_FLAGS(FILE_NON_DIRECTORY_FILE);
2882 ADD_FLAGS(FILE_CREATE_TREE_CONNECTION);
2883 ADD_FLAGS(FILE_COMPLETE_IF_OPLOCKED);
2884 ADD_FLAGS(FILE_NO_EA_KNOWLEDGE);
2885 ADD_FLAGS(FILE_EIGHT_DOT_THREE_ONLY);
2886 ADD_FLAGS(FILE_RANDOM_ACCESS);
2887 ADD_FLAGS(FILE_DELETE_ON_CLOSE);
2888 ADD_FLAGS(FILE_OPEN_BY_FILE_ID);
2889 ADD_FLAGS(FILE_OPEN_FOR_BACKUP_INTENT);
2890 ADD_FLAGS(FILE_NO_COMPRESSION);
2891 ADD_FLAGS(FILE_RESERVER_OPFILTER);
2892 ADD_FLAGS(FILE_OPEN_REPARSE_POINT);
2893 ADD_FLAGS(FILE_OPEN_NO_RECALL);
2894 ADD_FLAGS(FILE_OPEN_FOR_FREE_SPACE_QUERY);
2896 ADD_FLAGS(FILE_SHARE_READ);
2897 ADD_FLAGS(FILE_SHARE_WRITE);
2898 ADD_FLAGS(FILE_SHARE_DELETE);
2900 /* change notify completion filter flags */
2901 ADD_FLAGS(FILE_NOTIFY_CHANGE_FILE_NAME);
2902 ADD_FLAGS(FILE_NOTIFY_CHANGE_DIR_NAME);
2903 ADD_FLAGS(FILE_NOTIFY_CHANGE_ATTRIBUTES);
2904 ADD_FLAGS(FILE_NOTIFY_CHANGE_SIZE);
2905 ADD_FLAGS(FILE_NOTIFY_CHANGE_LAST_WRITE);
2906 ADD_FLAGS(FILE_NOTIFY_CHANGE_LAST_ACCESS);
2907 ADD_FLAGS(FILE_NOTIFY_CHANGE_CREATION);
2908 ADD_FLAGS(FILE_NOTIFY_CHANGE_EA);
2909 ADD_FLAGS(FILE_NOTIFY_CHANGE_SECURITY);
2910 ADD_FLAGS(FILE_NOTIFY_CHANGE_STREAM_NAME);
2911 ADD_FLAGS(FILE_NOTIFY_CHANGE_STREAM_SIZE);
2912 ADD_FLAGS(FILE_NOTIFY_CHANGE_STREAM_WRITE);
2913 ADD_FLAGS(FILE_NOTIFY_CHANGE_NAME);
2914 ADD_FLAGS(FILE_NOTIFY_CHANGE_ALL);
2916 /* change notify action results */
2917 ADD_FLAGS(NOTIFY_ACTION_ADDED);
2918 ADD_FLAGS(NOTIFY_ACTION_REMOVED);
2919 ADD_FLAGS(NOTIFY_ACTION_MODIFIED);
2920 ADD_FLAGS(NOTIFY_ACTION_OLD_NAME);
2921 ADD_FLAGS(NOTIFY_ACTION_NEW_NAME);
2922 ADD_FLAGS(NOTIFY_ACTION_ADDED_STREAM);
2923 ADD_FLAGS(NOTIFY_ACTION_REMOVED_STREAM);
2924 ADD_FLAGS(NOTIFY_ACTION_MODIFIED_STREAM);
2926 /* CreateDisposition values */
2927 ADD_FLAGS(FILE_SUPERSEDE);
2928 ADD_FLAGS(FILE_OPEN);
2929 ADD_FLAGS(FILE_CREATE);
2930 ADD_FLAGS(FILE_OPEN_IF);
2931 ADD_FLAGS(FILE_OVERWRITE);
2932 ADD_FLAGS(FILE_OVERWRITE_IF);
2934 ADD_FLAGS(FSCTL_DFS_GET_REFERRALS);
2935 ADD_FLAGS(FSCTL_DFS_GET_REFERRALS_EX);
2936 ADD_FLAGS(FSCTL_REQUEST_OPLOCK_LEVEL_1);
2937 ADD_FLAGS(FSCTL_REQUEST_OPLOCK_LEVEL_2);
2938 ADD_FLAGS(FSCTL_REQUEST_BATCH_OPLOCK);
2939 ADD_FLAGS(FSCTL_OPLOCK_BREAK_ACKNOWLEDGE);
2940 ADD_FLAGS(FSCTL_OPBATCH_ACK_CLOSE_PENDING);
2941 ADD_FLAGS(FSCTL_OPLOCK_BREAK_NOTIFY);
2942 ADD_FLAGS(FSCTL_GET_COMPRESSION);
2943 ADD_FLAGS(FSCTL_FILESYS_GET_STATISTICS);
2944 ADD_FLAGS(FSCTL_GET_NTFS_VOLUME_DATA);
2945 ADD_FLAGS(FSCTL_IS_VOLUME_DIRTY);
2946 ADD_FLAGS(FSCTL_FIND_FILES_BY_SID);
2947 ADD_FLAGS(FSCTL_SET_OBJECT_ID);
2948 ADD_FLAGS(FSCTL_GET_OBJECT_ID);
2949 ADD_FLAGS(FSCTL_DELETE_OBJECT_ID);
2950 ADD_FLAGS(FSCTL_SET_REPARSE_POINT);
2951 ADD_FLAGS(FSCTL_GET_REPARSE_POINT);
2952 ADD_FLAGS(FSCTL_DELETE_REPARSE_POINT);
2953 ADD_FLAGS(FSCTL_SET_OBJECT_ID_EXTENDED);
2954 ADD_FLAGS(FSCTL_CREATE_OR_GET_OBJECT_ID);
2955 ADD_FLAGS(FSCTL_SET_SPARSE);
2956 ADD_FLAGS(FSCTL_SET_ZERO_DATA);
2957 ADD_FLAGS(FSCTL_SET_ZERO_ON_DEALLOCATION);
2958 ADD_FLAGS(FSCTL_READ_FILE_USN_DATA);
2959 ADD_FLAGS(FSCTL_WRITE_USN_CLOSE_RECORD);
2960 ADD_FLAGS(FSCTL_QUERY_ALLOCATED_RANGES);
2961 ADD_FLAGS(FSCTL_QUERY_ON_DISK_VOLUME_INFO);
2962 ADD_FLAGS(FSCTL_QUERY_SPARING_INFO);
2963 ADD_FLAGS(FSCTL_FILE_LEVEL_TRIM);
2964 ADD_FLAGS(FSCTL_OFFLOAD_READ);
2965 ADD_FLAGS(FSCTL_OFFLOAD_WRITE);
2966 ADD_FLAGS(FSCTL_SET_INTEGRITY_INFORMATION);
2967 ADD_FLAGS(FSCTL_DUP_EXTENTS_TO_FILE);
2968 ADD_FLAGS(FSCTL_DUPLICATE_EXTENTS_TO_FILE_EX);
2969 ADD_FLAGS(FSCTL_STORAGE_QOS_CONTROL);
2970 ADD_FLAGS(FSCTL_SVHDX_SYNC_TUNNEL_REQUEST);
2971 ADD_FLAGS(FSCTL_QUERY_SHARED_VIRTUAL_DISK_SUPPORT);
2972 ADD_FLAGS(FSCTL_PIPE_PEEK);
2973 ADD_FLAGS(FSCTL_NAMED_PIPE_READ_WRITE);
2974 ADD_FLAGS(FSCTL_PIPE_TRANSCEIVE);
2975 ADD_FLAGS(FSCTL_PIPE_WAIT);
2976 ADD_FLAGS(FSCTL_GET_SHADOW_COPY_DATA);
2977 ADD_FLAGS(FSCTL_SRV_ENUM_SNAPS);
2978 ADD_FLAGS(FSCTL_SRV_REQUEST_RESUME_KEY);
2979 ADD_FLAGS(FSCTL_SRV_COPYCHUNK);
2980 ADD_FLAGS(FSCTL_SRV_COPYCHUNK_WRITE);
2981 ADD_FLAGS(FSCTL_SRV_READ_HASH);
2982 ADD_FLAGS(FSCTL_LMR_REQ_RESILIENCY);
2983 ADD_FLAGS(FSCTL_LMR_SET_LINK_TRACKING_INFORMATION);
2984 ADD_FLAGS(FSCTL_QUERY_NETWORK_INTERFACE_INFO);
2986 ADD_FLAGS(SYMLINK_ERROR_TAG);
2987 ADD_FLAGS(SYMLINK_FLAG_RELATIVE);
2988 ADD_FLAGS(SYMLINK_ADMIN);
2989 ADD_FLAGS(SYMLINK_UNTRUSTED);
2990 ADD_FLAGS(SYMLINK_TRUST_UNKNOWN);
2991 ADD_FLAGS(SYMLINK_TRUST_MASK);
2993 ADD_FLAGS(IO_REPARSE_TAG_RESERVED_ZERO);
2994 ADD_FLAGS(IO_REPARSE_TAG_SYMLINK);
2995 ADD_FLAGS(IO_REPARSE_TAG_MOUNT_POINT);
2996 ADD_FLAGS(IO_REPARSE_TAG_HSM);
2997 ADD_FLAGS(IO_REPARSE_TAG_SIS);
2998 ADD_FLAGS(IO_REPARSE_TAG_DFS);
2999 ADD_FLAGS(IO_REPARSE_TAG_NFS);
3001 #define ADD_STRING(val) PyModule_AddObject(m, #val, PyBytes_FromString(val))
3003 ADD_STRING(SMB2_CREATE_TAG_EXTA);
3004 ADD_STRING(SMB2_CREATE_TAG_MXAC);
3005 ADD_STRING(SMB2_CREATE_TAG_SECD);
3006 ADD_STRING(SMB2_CREATE_TAG_DHNQ);
3007 ADD_STRING(SMB2_CREATE_TAG_DHNC);
3008 ADD_STRING(SMB2_CREATE_TAG_ALSI);
3009 ADD_STRING(SMB2_CREATE_TAG_TWRP);
3010 ADD_STRING(SMB2_CREATE_TAG_QFID);
3011 ADD_STRING(SMB2_CREATE_TAG_RQLS);
3012 ADD_STRING(SMB2_CREATE_TAG_DH2Q);
3013 ADD_STRING(SMB2_CREATE_TAG_DH2C);
3014 ADD_STRING(SMB2_CREATE_TAG_AAPL);
3015 ADD_STRING(SMB2_CREATE_TAG_APP_INSTANCE_ID);
3016 ADD_STRING(SVHDX_OPEN_DEVICE_CONTEXT);
3017 ADD_STRING(SMB2_CREATE_TAG_POSIX);
3018 ADD_FLAGS(SMB2_FIND_POSIX_INFORMATION);
3019 ADD_FLAGS(FILE_SUPERSEDE);
3020 ADD_FLAGS(FILE_OPEN);
3021 ADD_FLAGS(FILE_CREATE);
3022 ADD_FLAGS(FILE_OPEN_IF);
3023 ADD_FLAGS(FILE_OVERWRITE);
3024 ADD_FLAGS(FILE_OVERWRITE_IF);
3025 ADD_FLAGS(FILE_DIRECTORY_FILE);
3027 ADD_FLAGS(SMB2_CLOSE_FLAGS_FULL_INFORMATION);
3029 return m;