2 * Unix SMB/CIFS implementation.
3 * Samba-internal work in progress Python binding for libsmbclient
5 * Copyright (C) Volker Lendecke 2012
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libsmb/libsmb.h"
24 #include "libcli/security/security.h"
25 #include "system/select.h"
26 #include "source4/libcli/util/pyerrors.h"
27 #include "auth/credentials/pycredentials.h"
30 static PyTypeObject
*get_pytype(const char *module
, const char *type
)
35 mod
= PyImport_ImportModule(module
);
37 PyErr_Format(PyExc_RuntimeError
,
38 "Unable to import %s to check type %s",
42 result
= (PyTypeObject
*)PyObject_GetAttrString(mod
, type
);
45 PyErr_Format(PyExc_RuntimeError
,
46 "Unable to find type %s in module %s",
57 struct cli_state
*cli
;
58 struct tevent_context
*ev
;
59 struct py_cli_thread
*thread_state
;
66 struct py_cli_thread
{
69 * Pipe to make the poll thread wake up in our destructor, so
70 * that we can exit and join the thread.
73 struct tevent_fd
*shutdown_fde
;
78 * Thread state to release the GIL during the poll(2) syscall
80 PyThreadState
*py_threadstate
;
83 static void *py_cli_state_poll_thread(void *private_data
)
85 struct py_cli_state
*self
= (struct py_cli_state
*)private_data
;
86 struct py_cli_thread
*t
= self
->thread_state
;
87 PyGILState_STATE gstate
;
89 gstate
= PyGILState_Ensure();
91 while (!t
->do_shutdown
) {
93 ret
= tevent_loop_once(self
->ev
);
96 PyGILState_Release(gstate
);
100 static void py_cli_state_trace_callback(enum tevent_trace_point point
,
103 struct py_cli_state
*self
= (struct py_cli_state
*)private_data
;
104 struct py_cli_thread
*t
= self
->thread_state
;
107 case TEVENT_TRACE_BEFORE_WAIT
:
108 assert(t
->py_threadstate
== NULL
);
109 t
->py_threadstate
= PyEval_SaveThread();
111 case TEVENT_TRACE_AFTER_WAIT
:
112 assert(t
->py_threadstate
!= NULL
);
113 PyEval_RestoreThread(t
->py_threadstate
);
114 t
->py_threadstate
= NULL
;
121 static void py_cli_state_shutdown_handler(struct tevent_context
*ev
,
122 struct tevent_fd
*fde
,
126 struct py_cli_state
*self
= (struct py_cli_state
*)private_data
;
127 struct py_cli_thread
*t
= self
->thread_state
;
129 if ((flags
& TEVENT_FD_READ
) == 0) {
132 TALLOC_FREE(t
->shutdown_fde
);
133 t
->do_shutdown
= true;
136 static int py_cli_thread_destructor(struct py_cli_thread
*t
)
144 * This will wake the poll thread from the poll(2)
146 written
= write(t
->shutdown_pipe
[1], &c
, 1);
147 } while ((written
== -1) && (errno
== EINTR
));
150 * Allow the poll thread to do its own cleanup under the GIL
152 Py_BEGIN_ALLOW_THREADS
153 ret
= pthread_join(t
->id
, NULL
);
157 if (t
->shutdown_pipe
[0] != -1) {
158 close(t
->shutdown_pipe
[0]);
159 t
->shutdown_pipe
[0] = -1;
161 if (t
->shutdown_pipe
[1] != -1) {
162 close(t
->shutdown_pipe
[1]);
163 t
->shutdown_pipe
[1] = -1;
168 static bool py_cli_state_setup_ev(struct py_cli_state
*self
)
170 struct py_cli_thread
*t
= NULL
;
173 self
->ev
= tevent_context_init_byname(NULL
, "poll_mt");
174 if (self
->ev
== NULL
) {
177 tevent_set_trace_callback(self
->ev
, py_cli_state_trace_callback
, self
);
179 self
->thread_state
= talloc_zero(NULL
, struct py_cli_thread
);
180 if (self
->thread_state
== NULL
) {
183 t
= self
->thread_state
;
185 ret
= pipe(t
->shutdown_pipe
);
189 t
->shutdown_fde
= tevent_add_fd(
190 self
->ev
, self
->ev
, t
->shutdown_pipe
[0], TEVENT_FD_READ
,
191 py_cli_state_shutdown_handler
, self
);
192 if (t
->shutdown_fde
== NULL
) {
196 PyEval_InitThreads();
198 ret
= pthread_create(&t
->id
, NULL
, py_cli_state_poll_thread
, self
);
202 talloc_set_destructor(self
->thread_state
, py_cli_thread_destructor
);
207 TALLOC_FREE(t
->shutdown_fde
);
209 if (t
->shutdown_pipe
[0] != -1) {
210 close(t
->shutdown_pipe
[0]);
211 t
->shutdown_pipe
[0] = -1;
213 if (t
->shutdown_pipe
[1] != -1) {
214 close(t
->shutdown_pipe
[1]);
215 t
->shutdown_pipe
[1] = -1;
219 TALLOC_FREE(self
->thread_state
);
220 TALLOC_FREE(self
->ev
);
224 struct py_tevent_cond
{
225 pthread_mutex_t mutex
;
230 static void py_tevent_signalme(struct tevent_req
*req
);
232 static int py_tevent_cond_wait(struct py_tevent_cond
*cond
)
236 result
= pthread_mutex_init(&cond
->mutex
, NULL
);
240 result
= pthread_cond_init(&cond
->cond
, NULL
);
245 result
= pthread_mutex_lock(&cond
->mutex
);
250 cond
->is_done
= false;
252 while (!cond
->is_done
) {
254 Py_BEGIN_ALLOW_THREADS
255 result
= pthread_cond_wait(&cond
->cond
, &cond
->mutex
);
264 ret
= pthread_mutex_unlock(&cond
->mutex
);
267 ret
= pthread_cond_destroy(&cond
->cond
);
270 ret
= pthread_mutex_destroy(&cond
->mutex
);
276 static int py_tevent_req_wait(struct tevent_context
*ev
,
277 struct tevent_req
*req
)
279 struct py_tevent_cond cond
;
280 tevent_req_set_callback(req
, py_tevent_signalme
, &cond
);
281 return py_tevent_cond_wait(&cond
);
284 static void py_tevent_signalme(struct tevent_req
*req
)
286 struct py_tevent_cond
*cond
= (struct py_tevent_cond
*)
287 tevent_req_callback_data_void(req
);
290 ret
= pthread_mutex_lock(&cond
->mutex
);
293 cond
->is_done
= true;
295 ret
= pthread_cond_signal(&cond
->cond
);
297 ret
= pthread_mutex_unlock(&cond
->mutex
);
303 static bool py_cli_state_setup_ev(struct py_cli_state
*self
)
305 self
->ev
= tevent_context_init(NULL
);
306 return (self
->ev
!= NULL
);
309 static int py_tevent_req_wait(struct tevent_context
*ev
,
310 struct tevent_req
*req
)
312 while (tevent_req_is_in_progress(req
)) {
315 ret
= tevent_loop_once(ev
);
325 static bool py_tevent_req_wait_exc(struct tevent_context
*ev
,
326 struct tevent_req
*req
)
334 ret
= py_tevent_req_wait(ev
, req
);
338 PyErr_SetFromErrno(PyExc_RuntimeError
);
344 static PyObject
*py_cli_state_new(PyTypeObject
*type
, PyObject
*args
,
347 struct py_cli_state
*self
;
349 self
= (struct py_cli_state
*)type
->tp_alloc(type
, 0);
355 self
->thread_state
= NULL
;
356 return (PyObject
*)self
;
359 static int py_cli_state_init(struct py_cli_state
*self
, PyObject
*args
,
365 struct cli_credentials
*cli_creds
;
368 static const char *kwlist
[] = {
369 "host", "share", "credentials", NULL
372 PyTypeObject
*py_type_Credentials
= get_pytype(
373 "samba.credentials", "Credentials");
374 if (py_type_Credentials
== NULL
) {
378 ret
= PyArg_ParseTupleAndKeywords(
379 args
, kwds
, "ss|O!", (char **)kwlist
,
380 &host
, &share
, py_type_Credentials
, &creds
);
382 Py_DECREF(py_type_Credentials
);
388 if (!py_cli_state_setup_ev(self
)) {
392 cli_creds
= cli_credentials_from_py_object(creds
);
393 if (cli_creds
== NULL
) {
394 PyErr_SetString(PyExc_TypeError
, "Expected credentials");
398 status
= cli_full_connection(
399 &self
->cli
, "myname", host
, NULL
, 0, share
, "?????",
400 cli_credentials_get_username(cli_creds
),
401 cli_credentials_get_domain(cli_creds
),
402 cli_credentials_get_password(cli_creds
),
404 if (!NT_STATUS_IS_OK(status
)) {
405 PyErr_SetNTSTATUS(status
);
411 static void py_cli_state_dealloc(struct py_cli_state
*self
)
413 TALLOC_FREE(self
->thread_state
);
414 TALLOC_FREE(self
->ev
);
416 if (self
->cli
!= NULL
) {
417 cli_shutdown(self
->cli
);
420 self
->ob_type
->tp_free((PyObject
*)self
);
423 static PyObject
*py_cli_create(struct py_cli_state
*self
, PyObject
*args
,
427 unsigned CreateFlags
= 0;
428 unsigned DesiredAccess
= FILE_GENERIC_READ
;
429 unsigned FileAttributes
= 0;
430 unsigned ShareAccess
= 0;
431 unsigned CreateDisposition
= FILE_OPEN
;
432 unsigned CreateOptions
= 0;
433 unsigned SecurityFlags
= 0;
435 struct tevent_req
*req
;
438 static const char *kwlist
[] = {
439 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
440 "ShareAccess", "CreateDisposition", "CreateOptions",
441 "SecurityFlags", NULL
};
443 if (!PyArg_ParseTupleAndKeywords(
444 args
, kwds
, "s|IIIIIII", (char **)kwlist
,
445 &fname
, &CreateFlags
, &DesiredAccess
, &FileAttributes
,
446 &ShareAccess
, &CreateDisposition
, &CreateOptions
,
451 req
= cli_ntcreate_send(NULL
, self
->ev
, self
->cli
, fname
, CreateFlags
,
452 DesiredAccess
, FileAttributes
, ShareAccess
,
453 CreateDisposition
, CreateOptions
,
455 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
458 status
= cli_ntcreate_recv(req
, &fnum
);
461 if (!NT_STATUS_IS_OK(status
)) {
462 PyErr_SetNTSTATUS(status
);
465 return Py_BuildValue("I", (unsigned)fnum
);
468 static PyObject
*py_cli_close(struct py_cli_state
*self
, PyObject
*args
)
470 struct tevent_req
*req
;
474 if (!PyArg_ParseTuple(args
, "i", &fnum
)) {
478 req
= cli_close_send(NULL
, self
->ev
, self
->cli
, fnum
);
479 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
482 status
= cli_close_recv(req
);
485 if (!NT_STATUS_IS_OK(status
)) {
486 PyErr_SetNTSTATUS(status
);
493 static PyObject
*py_cli_write(struct py_cli_state
*self
, PyObject
*args
,
500 unsigned long long offset
;
501 struct tevent_req
*req
;
505 static const char *kwlist
[] = {
506 "fnum", "buffer", "offset", "mode", NULL
};
508 if (!PyArg_ParseTupleAndKeywords(
509 args
, kwds
, "Is#K|I", (char **)kwlist
,
510 &fnum
, &buf
, &buflen
, &offset
, &mode
)) {
514 req
= cli_write_andx_send(NULL
, self
->ev
, self
->cli
, fnum
, mode
,
515 (uint8_t *)buf
, offset
, buflen
);
516 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
519 status
= cli_write_andx_recv(req
, &written
);
522 if (!NT_STATUS_IS_OK(status
)) {
523 PyErr_SetNTSTATUS(status
);
526 return Py_BuildValue("K", (unsigned long long)written
);
529 static PyObject
*py_cli_read(struct py_cli_state
*self
, PyObject
*args
,
533 unsigned long long offset
;
535 struct tevent_req
*req
;
541 static const char *kwlist
[] = {
542 "fnum", "offset", "size", NULL
};
544 if (!PyArg_ParseTupleAndKeywords(
545 args
, kwds
, "IKI", (char **)kwlist
, &fnum
, &offset
,
550 req
= cli_read_andx_send(NULL
, self
->ev
, self
->cli
, fnum
,
552 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
555 status
= cli_read_andx_recv(req
, &buflen
, &buf
);
557 if (!NT_STATUS_IS_OK(status
)) {
559 PyErr_SetNTSTATUS(status
);
562 result
= Py_BuildValue("s#", (char *)buf
, (int)buflen
);
567 static PyObject
*py_cli_ftruncate(struct py_cli_state
*self
, PyObject
*args
,
571 unsigned long long size
;
572 struct tevent_req
*req
;
575 static const char *kwlist
[] = {
576 "fnum", "size", NULL
};
578 if (!PyArg_ParseTupleAndKeywords(
579 args
, kwds
, "IK", (char **)kwlist
, &fnum
, &size
)) {
583 req
= cli_ftruncate_send(NULL
, self
->ev
, self
->cli
, fnum
, size
);
584 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
587 status
= cli_ftruncate_recv(req
);
590 if (!NT_STATUS_IS_OK(status
)) {
591 PyErr_SetNTSTATUS(status
);
598 static PyObject
*py_cli_delete_on_close(struct py_cli_state
*self
,
603 struct tevent_req
*req
;
606 static const char *kwlist
[] = {
607 "fnum", "flag", NULL
};
609 if (!PyArg_ParseTupleAndKeywords(
610 args
, kwds
, "II", (char **)kwlist
, &fnum
, &flag
)) {
614 req
= cli_nt_delete_on_close_send(NULL
, self
->ev
, self
->cli
, fnum
,
616 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
619 status
= cli_nt_delete_on_close_recv(req
);
622 if (!NT_STATUS_IS_OK(status
)) {
623 PyErr_SetNTSTATUS(status
);
630 static PyObject
*py_cli_list(struct py_cli_state
*self
,
636 FILE_ATTRIBUTE_DIRECTORY
|
637 FILE_ATTRIBUTE_SYSTEM
|
638 FILE_ATTRIBUTE_HIDDEN
;
639 unsigned info_level
= SMB_FIND_FILE_BOTH_DIRECTORY_INFO
;
640 struct tevent_req
*req
;
642 struct file_info
*finfos
;
643 size_t i
, num_finfos
;
646 const char *kwlist
[] = {
647 "mask", "attribute", "info_level", NULL
650 if (!PyArg_ParseTupleAndKeywords(
651 args
, kwds
, "s|II", (char **)kwlist
,
652 &mask
, &attribute
, &info_level
)) {
656 req
= cli_list_send(NULL
, self
->ev
, self
->cli
, mask
, attribute
,
658 if (!py_tevent_req_wait_exc(self
->ev
, req
)) {
661 status
= cli_list_recv(req
, NULL
, &finfos
, &num_finfos
);
664 if (!NT_STATUS_IS_OK(status
)) {
665 PyErr_SetNTSTATUS(status
);
669 result
= Py_BuildValue("[]");
670 if (result
== NULL
) {
674 for (i
=0; i
<num_finfos
; i
++) {
675 struct file_info
*finfo
= &finfos
[i
];
679 file
= Py_BuildValue(
682 "mode", (int)finfo
->mode
);
688 ret
= PyList_Append(result
, file
);
698 static PyMethodDef py_cli_state_methods
[] = {
699 { "create", (PyCFunction
)py_cli_create
, METH_VARARGS
|METH_KEYWORDS
,
701 { "close", (PyCFunction
)py_cli_close
, METH_VARARGS
,
702 "Close a file handle" },
703 { "write", (PyCFunction
)py_cli_write
, METH_VARARGS
|METH_KEYWORDS
,
704 "Write to a file handle" },
705 { "read", (PyCFunction
)py_cli_read
, METH_VARARGS
|METH_KEYWORDS
,
706 "Read from a file handle" },
707 { "truncate", (PyCFunction
)py_cli_ftruncate
,
708 METH_VARARGS
|METH_KEYWORDS
,
710 { "delete_on_close", (PyCFunction
)py_cli_delete_on_close
,
711 METH_VARARGS
|METH_KEYWORDS
,
712 "Set/Reset the delete on close flag" },
713 { "readdir", (PyCFunction
)py_cli_list
,
714 METH_VARARGS
|METH_KEYWORDS
,
715 "List a directory" },
716 { NULL
, NULL
, 0, NULL
}
719 static PyTypeObject py_cli_state_type
= {
720 PyObject_HEAD_INIT(NULL
)
721 .tp_name
= "libsmb_samba_internal.Conn",
722 .tp_basicsize
= sizeof(struct py_cli_state
),
723 .tp_flags
= Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
,
724 .tp_doc
= "libsmb connection",
725 .tp_new
= py_cli_state_new
,
726 .tp_init
= (initproc
)py_cli_state_init
,
727 .tp_dealloc
= (destructor
)py_cli_state_dealloc
,
728 .tp_methods
= py_cli_state_methods
,
731 static PyMethodDef py_libsmb_methods
[] = {
735 void initlibsmb_samba_internal(void);
736 void initlibsmb_samba_internal(void)
742 m
= Py_InitModule3("libsmb_samba_internal", py_libsmb_methods
,
745 if (PyType_Ready(&py_cli_state_type
) < 0) {
748 Py_INCREF(&py_cli_state_type
);
749 PyModule_AddObject(m
, "Conn", (PyObject
*)&py_cli_state_type
);