Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation. Xattr manipulation bindings.
3 : Copyright (C) Matthieu Patou <mat@matws.net> 2009-2010
4 : Base on work of pyglue.c by Jelmer Vernooij <jelmer@samba.org> 2007 and
5 : Matthias Dieter Wallnöfer 2009
6 :
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.
11 :
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.
16 :
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/>.
19 : */
20 :
21 : #include "lib/replace/system/python.h"
22 : #include "python/py3compat.h"
23 : #include "includes.h"
24 : #include "system/filesys.h"
25 : #include <tdb.h>
26 : #include "lib/tdb_wrap/tdb_wrap.h"
27 : #include "librpc/ndr/libndr.h"
28 : #include "ntvfs/posix/posix_eadb.h"
29 : #include "libcli/util/pyerrors.h"
30 : #include "param/pyparam.h"
31 :
32 0 : static PyObject *py_is_xattr_supported(PyObject *self,
33 : PyObject *Py_UNUSED(ignored))
34 : {
35 0 : Py_RETURN_TRUE;
36 : }
37 :
38 970 : static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
39 : {
40 319 : char *filename, *attribute, *tdbname;
41 319 : DATA_BLOB blob;
42 319 : Py_ssize_t blobsize;
43 319 : NTSTATUS status;
44 319 : TALLOC_CTX *mem_ctx;
45 319 : struct tdb_wrap *eadb;
46 :
47 970 : if (!PyArg_ParseTuple(args, "sss"PYARG_BYTES_LEN, &tdbname, &filename, &attribute,
48 : &blob.data, &blobsize))
49 0 : return NULL;
50 :
51 970 : blob.length = blobsize;
52 970 : mem_ctx = talloc_new(NULL);
53 970 : eadb = tdb_wrap_open(
54 : mem_ctx, tdbname, 50000,
55 : lpcfg_tdb_flags(py_default_loadparm_context(mem_ctx),
56 : TDB_DEFAULT),
57 : O_RDWR|O_CREAT, 0600);
58 :
59 970 : if (eadb == NULL) {
60 0 : PyErr_SetFromErrno(PyExc_IOError);
61 0 : talloc_free(mem_ctx);
62 0 : return NULL;
63 : }
64 970 : status = push_xattr_blob_tdb_raw(eadb, attribute, filename, -1,
65 : &blob);
66 970 : if (!NT_STATUS_IS_OK(status)) {
67 0 : PyErr_SetNTSTATUS(status);
68 0 : talloc_free(mem_ctx);
69 0 : return NULL;
70 : }
71 970 : talloc_free(mem_ctx);
72 970 : Py_RETURN_NONE;
73 : }
74 :
75 2 : static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
76 : {
77 2 : char *filename, *attribute, *tdbname;
78 2 : TALLOC_CTX *mem_ctx;
79 2 : DATA_BLOB blob;
80 2 : PyObject *ret;
81 2 : NTSTATUS status;
82 2 : struct tdb_wrap *eadb = NULL;
83 :
84 2 : if (!PyArg_ParseTuple(args, "sss", &tdbname, &filename, &attribute))
85 0 : return NULL;
86 :
87 2 : mem_ctx = talloc_new(NULL);
88 2 : eadb = tdb_wrap_open(
89 : mem_ctx, tdbname, 50000,
90 : lpcfg_tdb_flags(py_default_loadparm_context(mem_ctx),
91 : TDB_DEFAULT),
92 : O_RDWR|O_CREAT, 0600);
93 2 : if (eadb == NULL) {
94 0 : PyErr_SetFromErrno(PyExc_IOError);
95 0 : talloc_free(mem_ctx);
96 0 : return NULL;
97 : }
98 2 : status = pull_xattr_blob_tdb_raw(eadb, mem_ctx, attribute, filename,
99 : -1, 100, &blob);
100 2 : if (!NT_STATUS_IS_OK(status)) {
101 0 : PyErr_SetNTSTATUS(status);
102 0 : talloc_free(mem_ctx);
103 0 : return NULL;
104 : }
105 2 : ret = Py_BuildValue(PYARG_BYTES_LEN, blob.data, blob.length);
106 2 : talloc_free(mem_ctx);
107 2 : return ret;
108 : }
109 :
110 : static PyMethodDef py_posix_eadb_methods[] = {
111 : { "wrap_getxattr", (PyCFunction)py_wrap_getxattr, METH_VARARGS,
112 : "wrap_getxattr(filename,attribute) -> blob\n"
113 : "Retrieve given attribute on the given file." },
114 : { "wrap_setxattr", (PyCFunction)py_wrap_setxattr, METH_VARARGS,
115 : "wrap_setxattr(filename,attribute,value)\n"
116 : "Set the given attribute to the given value on the given file." },
117 : { "is_xattr_supported", (PyCFunction)py_is_xattr_supported, METH_NOARGS,
118 : "Return true if xattr are supported on this system\n"},
119 : {0}
120 : };
121 :
122 : static struct PyModuleDef moduledef = {
123 : PyModuleDef_HEAD_INIT,
124 : .m_name = "posix_eadb",
125 : .m_doc = "Python bindings for xattr manipulation.",
126 : .m_size = -1,
127 : .m_methods = py_posix_eadb_methods,
128 : };
129 :
130 1732 : MODULE_INIT_FUNC(posix_eadb)
131 : {
132 45 : PyObject *m;
133 :
134 1732 : m = PyModule_Create(&moduledef);
135 :
136 1732 : if (m == NULL)
137 0 : return NULL;
138 :
139 1687 : return m;
140 : }
|