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