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()
37 creds.set_username("administrator")
38 creds.set_password("1234")
40 c = libsmb.Conn("127.0.0.1",
45 -------------------------
48 #include "lib/replace/system/python.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"
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
)
74 mod
= PyImport_ImportModule(module
);
76 PyErr_Format(PyExc_RuntimeError
,
77 "Unable to import %s to check type %s",
81 result
= (PyTypeObject
*)PyObject_GetAttrString(mod
, type
);
84 PyErr_Format(PyExc_RuntimeError
,
85 "Unable to find type %s in module %s",
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
,
101 char **_keywords
= discard_const_p(char *, keywords
);
104 va_start(a
, keywords
);
105 ret
= PyArg_VaParseTupleAndKeywords(args
, kw
, format
,
111 struct py_cli_thread
;
113 struct py_cli_oplock_break
{
118 struct py_cli_state
{
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
;
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
;
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
) {
162 ret
= tevent_loop_once(self
->ev
);
165 PyGILState_Release(gstate
);
169 static void py_cli_state_trace_callback(enum tevent_trace_point point
,
172 struct py_cli_state
*self
= (struct py_cli_state
*)private_data
;
173 struct py_cli_thread
*t
= self
->thread_state
;
176 case TEVENT_TRACE_BEFORE_WAIT
:
177 assert(t
->py_threadstate
== NULL
);
178 t
->py_threadstate
= PyEval_SaveThread();
180 case TEVENT_TRACE_AFTER_WAIT
:
181 assert(t
->py_threadstate
!= NULL
);
182 PyEval_RestoreThread(t
->py_threadstate
);
183 t
->py_threadstate
= NULL
;
190 static void py_cli_state_shutdown_handler(struct tevent_context
*ev
,
191 struct tevent_fd
*fde
,
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) {
201 TALLOC_FREE(t
->shutdown_fde
);
202 t
->do_shutdown
= true;
205 static int py_cli_thread_destructor(struct py_cli_thread
*t
)
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
);
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;
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
;
245 self
->ev
= tevent_context_init_byname(NULL
, "poll_mt");
246 if (self
->ev
== NULL
) {
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
) {
258 t
= self
->thread_state
;
260 ret
= pipe(t
->shutdown_pipe
);
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
) {
271 PyEval_InitThreads();
273 ret
= pthread_create(&t
->id
, NULL
, py_cli_state_poll_thread
, self
);
277 talloc_set_destructor(self
->thread_state
, py_cli_thread_destructor
);
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
);
299 struct py_tevent_cond
{
300 pthread_mutex_t mutex
;
305 static void py_tevent_signalme(struct tevent_req
*req
);
307 static int py_tevent_cond_wait(struct py_tevent_cond
*cond
)
311 result
= pthread_mutex_init(&cond
->mutex
, NULL
);
315 result
= pthread_cond_init(&cond
->cond
, NULL
);
320 result
= pthread_mutex_lock(&cond
->mutex
);
325 cond
->is_done
= false;
327 while (!cond
->is_done
) {
329 Py_BEGIN_ALLOW_THREADS
330 result
= pthread_cond_wait(&cond
->cond
, &cond
->mutex
);
339 ret
= pthread_mutex_unlock(&cond
->mutex
);
342 ret
= pthread_cond_destroy(&cond
->cond
);
345 ret
= pthread_mutex_destroy(&cond
->mutex
);
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
)
363 ret
= pthread_mutex_lock(&cond
->mutex
);
366 cond
->is_done
= true;
368 ret
= pthread_cond_signal(&cond
->cond
);
370 ret
= pthread_mutex_unlock(&cond
->mutex
);
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
);
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
) {
394 samba_tevent_set_debug(self
->ev
, "pylibsmb_tevent");
396 self
->req_wait_fn
= py_tevent_req_wait
;
401 static int py_tevent_req_wait(struct tevent_context
*ev
,
402 struct tevent_req
*req
)
404 while (tevent_req_is_in_progress(req
)) {
407 ret
= tevent_loop_once(ev
);
415 static bool py_tevent_req_wait_exc(struct py_cli_state
*self
,
416 struct tevent_req
*req
)
424 ret
= self
->req_wait_fn(self
->ev
, req
);
428 PyErr_SetFromErrno(PyExc_RuntimeError
);
434 static PyObject
*py_cli_state_new(PyTypeObject
*type
, PyObject
*args
,
437 struct py_cli_state
*self
;
439 self
= (struct py_cli_state
*)type
->tp_alloc(type
, 0);
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
;
459 ret
= PyList_Check(list
);
464 len
= PyList_Size(list
);
469 ctxs
= talloc_zero(mem_ctx
, struct smb2_negotiate_contexts
);
474 for (i
=0; i
<len
; i
++) {
477 PyObject
*t
= PyList_GetItem(list
, i
);
480 PyObject
*ptype
= NULL
;
483 PyObject
*pdata
= NULL
;
484 DATA_BLOB data
= { .data
= NULL
, };
490 ret
= PyTuple_Check(t
);
495 tlen
= PyTuple_Size(t
);
500 ptype
= PyTuple_GetItem(t
, 0);
504 type
= PyLong_AsLong(ptype
);
505 if ((type
< 0) || (type
> UINT16_MAX
)) {
509 pdata
= PyTuple_GetItem(t
, 1);
511 ret
= PyBytes_Check(pdata
);
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
)) {
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
,
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
;
556 static const char *kwlist
[] = {
557 "host", "share", "lp", "creds",
558 "multi_threaded", "force_smb1",
561 "negotiate_contexts",
565 PyTypeObject
*py_type_Credentials
= get_pytype(
566 "samba.credentials", "Credentials");
567 if (py_type_Credentials
== NULL
) {
571 ret
= ParseTupleAndKeywords(
572 args
, kwds
, "ssO|O!OOOOO", kwlist
,
573 &host
, &share
, &py_lp
,
574 py_type_Credentials
, &creds
,
579 &py_negotiate_contexts
);
581 Py_DECREF(py_type_Credentials
);
587 multi_threaded
= PyObject_IsTrue(py_multi_threaded
);
588 force_smb1
= PyObject_IsTrue(py_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
);
602 flags
|= CLI_FULL_CONNECTION_IPC
;
605 request_posix
= PyObject_IsTrue(py_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
) {
618 if (multi_threaded
) {
620 ret
= py_cli_state_setup_mt_ev(self
);
625 PyErr_SetString(PyExc_RuntimeError
,
626 "No PTHREAD support available");
630 ret
= py_cli_state_setup_ev(self
);
637 cli_creds
= cli_credentials_init_anon(NULL
);
639 cli_creds
= PyCredentials_AsCliCredentials(creds
);
642 req
= cli_full_connection_creds_send(
643 NULL
, self
->ev
, "myname", host
, NULL
, 0, share
, "?????",
646 if (!py_tevent_req_wait_exc(self
, req
)) {
649 status
= cli_full_connection_creds_recv(req
, &self
->cli
);
652 if (!NT_STATUS_IS_OK(status
)) {
653 PyErr_SetNTSTATUS(status
);
658 * Oplocks require a multi threaded connection
660 if (self
->thread_state
== NULL
) {
664 self
->oplock_waiter
= cli_smb_oplock_break_waiter_send(
665 self
->ev
, self
->ev
, self
->cli
);
666 if (self
->oplock_waiter
== NULL
) {
670 tevent_req_set_callback(self
->oplock_waiter
, py_cli_got_oplock_break
,
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
;
684 status
= cli_smb_oplock_break_waiter_recv(req
, &b
.fnum
, &b
.level
);
686 self
->oplock_waiter
= NULL
;
688 if (!NT_STATUS_IS_OK(status
)) {
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);
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
) {
710 tevent_req_set_callback(self
->oplock_waiter
, py_cli_got_oplock_break
,
714 static PyObject
*py_cli_get_oplock_break(struct py_cli_state
*self
,
717 size_t num_oplock_breaks
;
719 if (!PyArg_ParseTuple(args
, "")) {
723 if (self
->thread_state
== NULL
) {
724 PyErr_SetString(PyExc_RuntimeError
,
725 "get_oplock_break() only possible on "
726 "a multi_threaded connection");
730 if (self
->oplock_cond
!= NULL
) {
732 PyErr_SetFromErrno(PyExc_RuntimeError
);
736 num_oplock_breaks
= talloc_array_length(self
->oplock_breaks
);
738 if (num_oplock_breaks
== 0) {
739 struct py_tevent_cond cond
;
742 self
->oplock_cond
= &cond
;
743 ret
= py_tevent_cond_wait(&cond
);
744 self
->oplock_cond
= NULL
;
748 PyErr_SetFromErrno(PyExc_RuntimeError
);
753 num_oplock_breaks
= talloc_array_length(self
->oplock_breaks
);
754 if (num_oplock_breaks
> 0) {
757 result
= Py_BuildValue(
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);
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
);
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
)) {
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
;
808 req
= cli_echo_send(NULL
, self
->ev
, self
->cli
, 1, data
);
809 if (!py_tevent_req_wait_exc(self
, req
)) {
812 status
= cli_echo_recv(req
);
814 PyErr_NTSTATUS_NOT_OK_RAISE(status
);
819 static PyObject
*py_cli_create(struct py_cli_state
*self
, PyObject
*args
,
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;
832 struct tevent_req
*req
;
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
)) {
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
)) {
855 status
= cli_ntcreate_recv(req
, &fnum
, NULL
);
858 if (!NT_STATUS_IS_OK(status
)) {
859 PyErr_SetNTSTATUS(status
);
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
;
872 ret
= PyList_Check(list
);
877 len
= PyList_Size(list
);
882 ctxs
= talloc_zero(mem_ctx
, struct smb2_create_blobs
);
887 for (i
=0; i
<len
; i
++) {
893 PyObject
*pname
= NULL
;
896 PyObject
*pdata
= NULL
;
897 DATA_BLOB data
= { .data
= NULL
, };
899 t
= PyList_GetItem(list
, i
);
904 ret
= PyTuple_Check(t
);
909 tlen
= PyTuple_Size(t
);
914 pname
= PyTuple_GetItem(t
, 0);
918 ret
= PyBytes_Check(pname
);
922 name
= PyBytes_AsString(pname
);
924 pdata
= PyTuple_GetItem(t
, 1);
928 ret
= PyBytes_Check(pdata
);
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
)) {
948 static PyObject
*py_cli_create_contexts(const struct smb2_create_blobs
*blobs
)
950 PyObject
*py_blobs
= NULL
;
957 py_blobs
= PyList_New(blobs
->num_blobs
);
958 if (py_blobs
== NULL
) {
962 for (i
=0; i
<blobs
->num_blobs
; i
++) {
963 struct smb2_create_blob
*blob
= &blobs
->blobs
[i
];
964 PyObject
*py_blob
= NULL
;
967 py_blob
= Py_BuildValue(
971 (int)blob
->data
.length
);
972 if (py_blob
== NULL
) {
976 ret
= PyList_SetItem(py_blobs
, i
, py_blob
);
985 Py_XDECREF(py_blobs
);
989 static PyObject
*py_cli_create_returns(const struct smb_create_returns
*r
)
994 "{sLsLsLsLsLsLsLsLsLsL}",
996 (unsigned long long)r
->oplock_level
,
998 (unsigned long long)r
->flags
,
1000 (unsigned long long)r
->create_action
,
1002 (unsigned long long)r
->creation_time
,
1004 (unsigned long long)r
->last_access_time
,
1006 (unsigned long long)r
->last_write_time
,
1008 (unsigned long long)r
->change_time
,
1010 (unsigned long long)r
->allocation_size
,
1012 (unsigned long long)r
->end_of_file
,
1014 (unsigned long long)r
->file_attributes
);
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
;
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(
1035 strlen(s
->substitute_name
),
1042 ok
= convert_string_talloc(
1047 strlen(s
->print_name
),
1056 "unparsed_path_length",
1057 (unsigned long long)s
->unparsed_path_length
,
1063 (unsigned long long)s
->flags
);
1066 TALLOC_FREE(subst_utf8
);
1067 TALLOC_FREE(print_utf8
);
1071 static PyObject
*py_cli_create_ex(
1072 struct py_cli_state
*self
, PyObject
*args
, PyObject
*kwds
)
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
;
1091 struct tevent_req
*req
;
1097 static const char *kwlist
[] = {
1103 "CreateDisposition",
1105 "ImpersonationLevel",
1110 ret
= ParseTupleAndKeywords(
1122 &ImpersonationLevel
,
1124 &py_create_contexts_in
);
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
) {
1134 PyErr_SetFromErrno(PyExc_RuntimeError
);
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(
1157 create_contexts_in
);
1159 req
= cli_ntcreate_send(
1174 TALLOC_FREE(create_contexts_in
);
1176 ok
= py_tevent_req_wait_exc(self
, req
);
1181 if (smbXcli_conn_protocol(self
->cli
->conn
) >= PROTOCOL_SMB2_02
) {
1182 status
= cli_smb2_create_fnum_recv(
1187 &create_contexts_out
,
1190 status
= cli_ntcreate_recv(req
, &fnum
, &cr
);
1195 if (!NT_STATUS_IS_OK(status
)) {
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
) {
1207 py_cr
= py_cli_create_returns(&cr
);
1208 if (py_cr
== NULL
) {
1212 v
= Py_BuildValue("(IOO)",
1215 py_create_contexts_out
);
1218 status
= NT_STATUS_NO_MEMORY
;
1220 Py_XDECREF(py_create_contexts_out
);
1224 if (NT_STATUS_EQUAL(status
, NT_STATUS_STOPPED_ON_SYMLINK
) &&
1225 (symlink
!= NULL
)) {
1227 PyObject_GetAttrString(
1228 PyImport_ImportModule("samba"),
1232 NT_STATUS_V(status
),
1233 get_friendly_nt_error_msg(status
),
1234 py_cli_symlink_error(symlink
)));
1236 PyErr_SetNTSTATUS(status
);
1241 static PyObject
*py_cli_close(struct py_cli_state
*self
, PyObject
*args
)
1243 struct tevent_req
*req
;
1248 if (!PyArg_ParseTuple(args
, "i|i", &fnum
, &flags
)) {
1252 req
= cli_close_send(NULL
, self
->ev
, self
->cli
, fnum
, flags
);
1253 if (!py_tevent_req_wait_exc(self
, req
)) {
1256 status
= cli_close_recv(req
);
1259 if (!NT_STATUS_IS_OK(status
)) {
1260 PyErr_SetNTSTATUS(status
);
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
;
1275 static const char *kwlist
[] = { "src", "dst", "replace", NULL
};
1277 ok
= ParseTupleAndKeywords(
1278 args
, kwds
, "ss|p", kwlist
, &fname_src
, &fname_dst
, &replace
);
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
)) {
1288 status
= cli_rename_recv(req
);
1291 if (!NT_STATUS_IS_OK(status
)) {
1292 PyErr_SetNTSTATUS(status
);
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
;
1313 size_t copied_bytes
;
1315 if (state
->nread
>= state
->total_data
) {
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
)
1334 const char *filename
= NULL
;
1336 Py_ssize_t size
= 0;
1338 struct tevent_req
*req
= NULL
;
1339 struct push_state state
;
1341 if (!PyArg_ParseTuple(args
, "s"PYARG_BYTES_LEN
":savefile", &filename
,
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
)) {
1355 status
= cli_ntcreate_recv(req
, &fnum
, NULL
);
1357 PyErr_NTSTATUS_NOT_OK_RAISE(status
);
1359 /* write the new file contents */
1362 state
.total_data
= size
;
1364 req
= cli_push_send(NULL
, self
->ev
, self
->cli
, fnum
, 0, 0, 0,
1366 if (!py_tevent_req_wait_exc(self
, req
)) {
1369 status
= cli_push_recv(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
)) {
1378 status
= cli_close_recv(req
);
1379 PyErr_NTSTATUS_NOT_OK_RAISE(status
);
1384 static PyObject
*py_cli_write(struct py_cli_state
*self
, PyObject
*args
,
1391 unsigned long long offset
;
1392 struct tevent_req
*req
;
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
)) {
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
)) {
1410 status
= cli_write_recv(req
, &written
);
1413 if (!NT_STATUS_IS_OK(status
)) {
1414 PyErr_SetNTSTATUS(status
);
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
,
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
);
1440 * Loads the specified file's contents and returns it
1442 static PyObject
*py_smb_loadfile(struct py_cli_state
*self
, PyObject
*args
)
1445 const char *filename
= NULL
;
1446 struct tevent_req
*req
= NULL
;
1451 PyObject
*result
= NULL
;
1453 if (!PyArg_ParseTuple(args
, "s:loadfile", &filename
)) {
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
)) {
1466 status
= cli_ntcreate_recv(req
, &fnum
, NULL
);
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
) {
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
)) {
1487 status
= cli_pull_recv(req
, &nread
);
1489 if (!NT_STATUS_IS_OK(status
)) {
1491 PyErr_SetNTSTATUS(status
);
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
)) {
1501 status
= cli_close_recv(req
);
1503 if (!NT_STATUS_IS_OK(status
)) {
1505 PyErr_SetNTSTATUS(status
);
1509 /* sanity-check we read the expected number of bytes */
1512 PyErr_Format(PyExc_IOError
,
1513 "read invalid - got %zu requested %zu",
1519 if (_PyBytes_Resize(&result
, nread
) < 0) {
1527 static PyObject
*py_cli_read(struct py_cli_state
*self
, PyObject
*args
,
1531 unsigned long long offset
;
1533 struct tevent_req
*req
;
1539 static const char *kwlist
[] = {
1540 "fnum", "offset", "size", NULL
};
1542 if (!ParseTupleAndKeywords(
1543 args
, kwds
, "iKI", kwlist
, &fnum
, &offset
,
1548 result
= PyBytes_FromStringAndSize(NULL
, size
);
1549 if (result
== NULL
) {
1552 buf
= PyBytes_AS_STRING(result
);
1554 req
= cli_read_send(NULL
, self
->ev
, self
->cli
, fnum
,
1556 if (!py_tevent_req_wait_exc(self
, req
)) {
1560 status
= cli_read_recv(req
, &received
);
1563 if (!NT_STATUS_IS_OK(status
)) {
1565 PyErr_SetNTSTATUS(status
);
1569 if (received
> size
) {
1571 PyErr_Format(PyExc_IOError
,
1572 "read invalid - got %zu requested %u",
1577 if (received
< size
) {
1578 if (_PyBytes_Resize(&result
, received
) < 0) {
1586 static PyObject
*py_cli_ftruncate(struct py_cli_state
*self
, PyObject
*args
,
1590 unsigned long long size
;
1591 struct tevent_req
*req
;
1594 static const char *kwlist
[] = {
1595 "fnum", "size", NULL
};
1597 if (!ParseTupleAndKeywords(
1598 args
, kwds
, "IK", kwlist
, &fnum
, &size
)) {
1602 req
= cli_ftruncate_send(NULL
, self
->ev
, self
->cli
, fnum
, size
);
1603 if (!py_tevent_req_wait_exc(self
, req
)) {
1606 status
= cli_ftruncate_recv(req
);
1609 if (!NT_STATUS_IS_OK(status
)) {
1610 PyErr_SetNTSTATUS(status
);
1616 static PyObject
*py_cli_delete_on_close(struct py_cli_state
*self
,
1620 unsigned fnum
, flag
;
1621 struct tevent_req
*req
;
1624 static const char *kwlist
[] = {
1625 "fnum", "flag", NULL
};
1627 if (!ParseTupleAndKeywords(
1628 args
, kwds
, "II", kwlist
, &fnum
, &flag
)) {
1632 req
= cli_nt_delete_on_close_send(NULL
, self
->ev
, self
->cli
, fnum
,
1634 if (!py_tevent_req_wait_exc(self
, req
)) {
1637 status
= cli_nt_delete_on_close_recv(req
);
1640 if (!NT_STATUS_IS_OK(status
)) {
1641 PyErr_SetNTSTATUS(status
);
1647 struct py_cli_notify_state
{
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
,
1666 static const char *kwlist
[] = {
1669 "completion_filter",
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
;
1682 struct py_cli_notify_state
*py_notify_state
= NULL
;
1683 struct timeval endtime
;
1685 ok
= ParseTupleAndKeywords(args
,
1697 recursive
= PyObject_IsTrue(py_recursive
);
1699 req
= cli_notify_send(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
,
1719 endtime
= timeval_current_ofs_msec(self
->cli
->timeout
);
1720 ok
= tevent_req_set_endtime(flush_req
,
1728 ok
= py_tevent_req_wait_exc(self
, flush_req
);
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
) {
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
,
1753 struct py_cli_state
*py_cli_state
= self
->py_cli_state
;
1754 struct tevent_req
*req
= self
->req
;
1756 uint32_t num_changes
= 0;
1757 struct notify_change
*changes
= NULL
;
1758 PyObject
*result
= NULL
;
1761 static const char *kwlist
[] = {
1765 PyObject
*py_wait
= Py_False
;
1769 ok
= ParseTupleAndKeywords(args
,
1778 wait
= PyObject_IsTrue(py_wait
);
1781 PyErr_SetString(PyExc_RuntimeError
,
1783 "- missing change notify request?");
1787 pending
= tevent_req_is_in_progress(req
);
1788 if (pending
&& !wait
) {
1793 struct timeval endtime
;
1795 endtime
= timeval_current_ofs_msec(py_cli_state
->cli
->timeout
);
1796 ok
= tevent_req_set_endtime(req
,
1806 ok
= py_tevent_req_wait_exc(py_cli_state
, req
);
1808 Py_CLEAR(self
->py_cli_state
);
1813 status
= cli_notify_recv(req
, req
, &num_changes
, &changes
);
1814 if (!NT_STATUS_IS_OK(status
)) {
1816 PyErr_SetNTSTATUS(status
);
1820 result
= Py_BuildValue("[]");
1821 if (result
== NULL
) {
1826 for (i
= 0; i
< num_changes
; i
++) {
1827 PyObject
*change
= NULL
;
1830 change
= Py_BuildValue("{s:s,s:I}",
1831 "name", changes
[i
].name
,
1832 "action", changes
[i
].action
);
1833 if (change
== NULL
) {
1839 ret
= PyList_Append(result
, change
);
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"
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
;
1892 * Build a dictionary representing the file info.
1894 file
= Py_BuildValue("{s:s,s:I,"
1897 "s:i,s:K,s:i,s:i,s:I,"
1899 "name", finfo
->name
,
1900 "attrib", finfo
->attr
,
1902 "size", finfo
->size
,
1903 "allocaction_size", finfo
->allocated_size
,
1906 convert_timespec_to_time_t(finfo
->btime_ts
),
1908 convert_timespec_to_time_t(finfo
->atime_ts
),
1910 convert_timespec_to_time_t(finfo
->mtime_ts
),
1912 convert_timespec_to_time_t(finfo
->ctime_ts
),
1914 "perms", finfo
->st_ex_mode
,
1916 "dev", finfo
->st_ex_dev
,
1917 "nlink", finfo
->st_ex_nlink
,
1918 "reparse_tag", finfo
->reparse_tag
,
1921 dom_sid_string(finfo
, &finfo
->owner_sid
),
1923 dom_sid_string(finfo
, &finfo
->group_sid
));
1925 return NT_STATUS_NO_MEMORY
;
1928 ret
= PyList_Append(result
, file
);
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
;
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
,
1963 convert_timespec_to_time_t(finfo
->mtime_ts
));
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(
1977 PyLong_FromUnsignedLong(tag
));
1979 return NT_STATUS_INTERNAL_ERROR
;
1983 ret
= PyList_Append(result
, file
);
1986 return NT_STATUS_INTERNAL_ERROR
;
1989 return NT_STATUS_OK
;
1992 struct do_listing_state
{
1994 NTSTATUS (*callback_fn
)(
1995 struct file_info
*finfo
,
1997 void *private_data
);
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
)) {
2011 state
->callback_fn(finfo
, state
->mask
, state
->private_data
);
2015 static NTSTATUS
do_listing(struct py_cli_state
*self
,
2016 const char *base_dir
, const char *user_mask
,
2018 unsigned int info_level
,
2019 NTSTATUS (*callback_fn
)(struct file_info
*,
2020 const char *, void *),
2024 struct do_listing_state state
= {
2026 .callback_fn
= callback_fn
,
2027 .private_data
= priv
,
2029 struct tevent_req
*req
= NULL
;
2032 if (user_mask
== NULL
) {
2033 mask
= talloc_asprintf(NULL
, "%s\\*", base_dir
);
2035 mask
= talloc_asprintf(NULL
, "%s\\%s", base_dir
, user_mask
);
2039 return NT_STATUS_NO_MEMORY
;
2043 req
= cli_list_send(NULL
, self
->ev
, self
->cli
, mask
, attribute
,
2046 status
= NT_STATUS_NO_MEMORY
;
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
;
2056 status
= state
.status
;
2057 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MORE_FILES
)) {
2058 status
= NT_STATUS_OK
;
2066 static PyObject
*py_cli_list(struct py_cli_state
*self
,
2071 char *user_mask
= NULL
;
2072 unsigned int attribute
= LIST_ATTRIBUTE_MASK
;
2073 unsigned int info_level
= 0;
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 *) =
2082 if (!ParseTupleAndKeywords(args
, kwds
, "z|sII:list", kwlist
,
2083 &base_dir
, &user_mask
, &attribute
,
2088 result
= Py_BuildValue("[]");
2089 if (result
== NULL
) {
2094 if (proto
>= PROTOCOL_SMB2_02
) {
2095 info_level
= SMB2_FIND_ID_BOTH_DIRECTORY_INFO
;
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
)) {
2109 PyErr_SetNTSTATUS(status
);
2116 static PyObject
*py_smb_unlink(struct py_cli_state
*self
, PyObject
*args
)
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
)) {
2127 req
= cli_unlink_send(NULL
, self
->ev
, self
->cli
, filename
, attrs
);
2128 if (!py_tevent_req_wait_exc(self
, req
)) {
2131 status
= cli_unlink_recv(req
);
2133 PyErr_NTSTATUS_NOT_OK_RAISE(status
);
2138 static PyObject
*py_smb_rmdir(struct py_cli_state
*self
, PyObject
*args
)
2141 struct tevent_req
*req
= NULL
;
2142 const char *dirname
= NULL
;
2144 if (!PyArg_ParseTuple(args
, "s:rmdir", &dirname
)) {
2148 req
= cli_rmdir_send(NULL
, self
->ev
, self
->cli
, dirname
);
2149 if (!py_tevent_req_wait_exc(self
, req
)) {
2152 status
= cli_rmdir_recv(req
);
2154 PyErr_NTSTATUS_NOT_OK_RAISE(status
);
2160 * Create a directory
2162 static PyObject
*py_smb_mkdir(struct py_cli_state
*self
, PyObject
*args
)
2165 const char *dirname
= NULL
;
2166 struct tevent_req
*req
= NULL
;
2168 if (!PyArg_ParseTuple(args
, "s:mkdir", &dirname
)) {
2172 req
= cli_mkdir_send(NULL
, self
->ev
, self
->cli
, dirname
);
2173 if (!py_tevent_req_wait_exc(self
, req
)) {
2176 status
= cli_mkdir_recv(req
);
2178 PyErr_NTSTATUS_NOT_OK_RAISE(status
);
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();
2191 struct tevent_req
*req
= NULL
;
2195 uint64_t *gids
= NULL
;
2197 struct dom_sid
*sids
= NULL
;
2199 PyObject
*py_gids
= NULL
;
2200 PyObject
*py_sids
= NULL
;
2201 PyObject
*py_guest
= NULL
;
2202 PyObject
*py_ret
= NULL
;
2205 req
= cli_posix_whoami_send(frame
, self
->ev
, self
->cli
);
2206 if (!py_tevent_req_wait_exc(self
, req
)) {
2209 status
= cli_posix_whoami_recv(req
,
2218 if (!NT_STATUS_IS_OK(status
)) {
2219 PyErr_SetNTSTATUS(status
);
2222 if (num_gids
> PY_SSIZE_T_MAX
) {
2223 PyErr_SetString(PyExc_OverflowError
, "posix_whoami: Too many GIDs");
2226 if (num_sids
> PY_SSIZE_T_MAX
) {
2227 PyErr_SetString(PyExc_OverflowError
, "posix_whoami: Too many SIDs");
2231 py_gids
= PyList_New(num_gids
);
2235 for (i
= 0; i
< num_gids
; ++i
) {
2237 PyObject
*py_item
= PyLong_FromUnsignedLongLong(gids
[i
]);
2242 ret
= PyList_SetItem(py_gids
, i
, py_item
);
2247 py_sids
= PyList_New(num_sids
);
2251 for (i
= 0; i
< num_sids
; ++i
) {
2253 struct dom_sid
*sid
;
2256 sid
= dom_sid_dup(frame
, &sids
[i
]);
2262 py_item
= pytalloc_steal(dom_sid_Type
, sid
);
2268 ret
= PyList_SetItem(py_sids
, i
, py_item
);
2274 py_guest
= guest
? Py_True
: Py_False
;
2276 py_ret
= Py_BuildValue("KKNNO",
2301 * Checks existence of a directory
2303 static bool check_dir_path(struct py_cli_state
*self
, const char *path
)
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
)) {
2312 status
= cli_chkpath_recv(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
;
2323 if (!PyArg_ParseTuple(args
, "s:chkpath", &path
)) {
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
);
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
);
2350 static PyObject
*py_smb_get_sd(struct py_cli_state
*self
, PyObject
*args
)
2354 struct tevent_req
*req
= NULL
;
2355 struct security_descriptor
*sd
= NULL
;
2358 if (!PyArg_ParseTuple(args
, "iI:get_acl", &fnum
, &sinfo
)) {
2362 req
= cli_query_security_descriptor_send(
2363 NULL
, self
->ev
, self
->cli
, fnum
, sinfo
);
2364 if (!py_tevent_req_wait_exc(self
, req
)) {
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
;
2383 if (!PyArg_ParseTuple(args
, "iOI:set_sd", &fnum
, &py_sd
, &sinfo
)) {
2387 sd
= pytalloc_get_type(py_sd
, struct security_descriptor
);
2389 PyErr_Format(PyExc_TypeError
,
2390 "Expected dcerpc.security.descriptor as argument, got %s",
2391 pytalloc_get_name(py_sd
));
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
)) {
2401 status
= cli_set_security_descriptor_recv(req
);
2402 PyErr_NTSTATUS_NOT_OK_RAISE(status
);
2407 static PyObject
*py_smb_smb1_posix(
2408 struct py_cli_state
*self
, PyObject
*Py_UNUSED(ignored
))
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
)) {
2420 status
= cli_unix_extensions_version_recv(
2421 req
, &major
, &minor
, &caplow
, &caphigh
);
2423 if (!NT_STATUS_IS_OK(status
)) {
2424 PyErr_SetNTSTATUS(status
);
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
)) {
2433 status
= cli_set_unix_extensions_capabilities_recv(req
);
2435 if (!NT_STATUS_IS_OK(status
)) {
2436 PyErr_SetNTSTATUS(status
);
2440 result
= Py_BuildValue(
2449 static PyObject
*py_smb_smb1_readlink(
2450 struct py_cli_state
*self
, PyObject
*args
)
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
)) {
2462 req
= cli_posix_readlink_send(NULL
, self
->ev
, self
->cli
, filename
);
2463 if (!py_tevent_req_wait_exc(self
, req
)) {
2466 status
= cli_posix_readlink_recv(req
, NULL
, &target
);
2468 if (!NT_STATUS_IS_OK(status
)) {
2469 PyErr_SetNTSTATUS(status
);
2473 result
= PyBytes_FromString(target
);
2474 TALLOC_FREE(target
);
2478 static PyObject
*py_smb_smb1_symlink(
2479 struct py_cli_state
*self
, PyObject
*args
)
2482 const char *target
= NULL
, *newname
= NULL
;
2483 struct tevent_req
*req
= NULL
;
2485 if (!PyArg_ParseTuple(args
, "ss:smb1_symlink", &target
, &newname
)) {
2489 req
= cli_posix_symlink_send(
2490 NULL
, self
->ev
, self
->cli
, target
, newname
);
2491 if (!py_tevent_req_wait_exc(self
, req
)) {
2494 status
= cli_posix_symlink_recv(req
);
2496 if (!NT_STATUS_IS_OK(status
)) {
2497 PyErr_SetNTSTATUS(status
);
2504 static PyObject
*py_smb_smb1_stat(
2505 struct py_cli_state
*self
, PyObject
*args
)
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
)) {
2516 req
= cli_posix_stat_send(NULL
, self
->ev
, self
->cli
, fname
);
2517 if (!py_tevent_req_wait_exc(self
, req
)) {
2520 status
= cli_posix_stat_recv(req
, &sbuf
);
2522 if (!NT_STATUS_IS_OK(status
)) {
2523 PyErr_SetNTSTATUS(status
);
2527 return Py_BuildValue(
2528 "{sLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsLsL}",
2530 (unsigned long long)sbuf
.st_ex_dev
,
2532 (unsigned long long)sbuf
.st_ex_ino
,
2534 (unsigned long long)sbuf
.st_ex_mode
,
2536 (unsigned long long)sbuf
.st_ex_nlink
,
2538 (unsigned long long)sbuf
.st_ex_uid
,
2540 (unsigned long long)sbuf
.st_ex_gid
,
2542 (unsigned long long)sbuf
.st_ex_size
,
2544 (unsigned long long)sbuf
.st_ex_atime
.tv_sec
,
2546 (unsigned long long)sbuf
.st_ex_atime
.tv_nsec
,
2548 (unsigned long long)sbuf
.st_ex_mtime
.tv_sec
,
2550 (unsigned long long)sbuf
.st_ex_mtime
.tv_nsec
,
2552 (unsigned long long)sbuf
.st_ex_ctime
.tv_sec
,
2554 (unsigned long long)sbuf
.st_ex_ctime
.tv_nsec
,
2556 (unsigned long long)sbuf
.st_ex_btime
.tv_sec
,
2558 (unsigned long long)sbuf
.st_ex_btime
.tv_nsec
,
2559 "cached_dos_attributes",
2560 (unsigned long long)sbuf
.cached_dos_attributes
,
2562 (unsigned long long)sbuf
.st_ex_blksize
,
2564 (unsigned long long)sbuf
.st_ex_blocks
,
2566 (unsigned long long)sbuf
.st_ex_flags
,
2568 (unsigned long long)sbuf
.st_ex_iflags
);
2571 static PyObject
*py_cli_mknod(
2572 struct py_cli_state
*self
, PyObject
*args
, PyObject
*kwds
)
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
,
2583 ok
= ParseTupleAndKeywords(
2596 #if defined(HAVE_MAKEDEV)
2597 dev
= makedev(major
, minor
);
2600 req
= cli_mknod_send(
2601 NULL
, self
->ev
, self
->cli
, fname
, mode
, dev
);
2602 if (!py_tevent_req_wait_exc(self
, req
)) {
2605 status
= cli_mknod_recv(req
);
2607 if (!NT_STATUS_IS_OK(status
)) {
2608 PyErr_SetNTSTATUS(status
);
2614 static PyObject
*py_cli_fsctl(
2615 struct py_cli_state
*self
, PyObject
*args
, PyObject
*kwds
)
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
,
2631 ok
= ParseTupleAndKeywords(
2634 "ii" PYARG_BYTES_LEN
"i",
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
)) {
2654 status
= cli_fsctl_recv(req
, NULL
, &out
);
2655 if (!NT_STATUS_IS_OK(status
)) {
2656 PyErr_SetNTSTATUS(status
);
2660 result
= PyBytes_FromStringAndSize((char *)out
.data
, out
.length
);
2661 data_blob_free(&out
);
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
,
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
,
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
,
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." },
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." },
2741 (PyCFunction
)py_smb_protocol
,
2743 "protocol() -> Number"
2746 (PyCFunction
)py_smb_have_posix
,
2748 "have_posix() -> True/False\n\n"
2749 "\t\tReturn if the server has posix extensions"
2752 (PyCFunction
)py_smb_smb1_posix
,
2754 "Negotiate SMB1 posix extensions",
2757 (PyCFunction
)py_smb_smb1_readlink
,
2759 "smb1_readlink(path) -> link target",
2762 (PyCFunction
)py_smb_smb1_symlink
,
2764 "smb1_symlink(target, newname) -> None",
2767 (PyCFunction
)py_smb_smb1_stat
,
2769 "smb1_stat(path) -> stat info",
2772 (PyCFunction
)py_cli_fsctl
,
2773 METH_VARARGS
|METH_KEYWORDS
,
2774 "fsctl(fnum, ctl_code, in_bytes, max_out) -> out_bytes",
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
[] = {
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",
2807 .m_methods
= py_libsmb_methods
,
2810 MODULE_INIT_FUNC(libsmb_samba_cwrapper
)
2813 PyObject
*mod
= NULL
;
2815 talloc_stackframe();
2817 if (PyType_Ready(&py_cli_state_type
) < 0) {
2820 if (PyType_Ready(&py_cli_notify_state_type
) < 0) {
2824 m
= PyModule_Create(&moduledef
);
2829 /* Import dom_sid type from dcerpc.security */
2830 mod
= PyImport_ImportModule("samba.dcerpc.security");
2835 dom_sid_Type
= (PyTypeObject
*)PyObject_GetAttrString(mod
, "dom_sid");
2836 if (dom_sid_Type
== 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
);