1
0
mirror of https://github.com/samba-team/samba.git synced 2025-07-31 20:22:15 +03:00

s4-python: Add python wrapper for xattr_tdb format xattr storage

This uses the format used by the vfs_xattr_tdb module.  The s4
posix:eadb format is kept, just in a new file and module.

Andrew Bartlett
This commit is contained in:
Andrew Bartlett
2012-04-16 18:59:34 +10:00
parent 44b6bf9130
commit 628a24172f
4 changed files with 208 additions and 34 deletions

View File

@ -0,0 +1,126 @@
/*
Unix SMB/CIFS implementation. Xattr manipulation bindings.
Copyright (C) Matthieu Patou <mat@matws.net> 2009-2010
Base on work of pyglue.c by Jelmer Vernooij <jelmer@samba.org> 2007 and
Matthias Dieter Wallnöfer 2009
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Python.h>
#include "includes.h"
#include "system/filesys.h"
#include "tdb_compat.h"
#include "lib/tdb_wrap/tdb_wrap.h"
#include "librpc/ndr/libndr.h"
#include "lib/util/wrap_xattr.h"
#include "ntvfs/posix/posix_eadb.h"
#include "libcli/util/pyerrors.h"
#include "param/pyparam.h"
void initxattr_tdb(void);
static PyObject *py_is_xattr_supported(PyObject *self)
{
return Py_True;
}
static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
{
char *filename, *attribute, *tdbname;
DATA_BLOB blob;
int blobsize;
NTSTATUS status;
TALLOC_CTX *mem_ctx;
struct tdb_wrap *eadb;
if (!PyArg_ParseTuple(args, "ssss#", &tdbname, &filename, &attribute,
&blob.data, &blobsize))
return NULL;
blob.length = blobsize;
mem_ctx = talloc_new(NULL);
eadb = tdb_wrap_open(mem_ctx, tdbname, 50000,
TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
py_default_loadparm_context(mem_ctx));
if (eadb == NULL) {
PyErr_SetFromErrno(PyExc_IOError);
talloc_free(mem_ctx);
return NULL;
}
status = push_xattr_blob_tdb_raw(eadb, attribute, filename, -1,
&blob);
if (!NT_STATUS_IS_OK(status)) {
PyErr_SetNTSTATUS(status);
talloc_free(mem_ctx);
return NULL;
}
talloc_free(mem_ctx);
Py_RETURN_NONE;
}
static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
{
char *filename, *attribute, *tdbname;
TALLOC_CTX *mem_ctx;
DATA_BLOB blob;
PyObject *ret;
NTSTATUS status;
struct tdb_wrap *eadb = NULL;
if (!PyArg_ParseTuple(args, "sss", &tdbname, &filename, &attribute))
return NULL;
mem_ctx = talloc_new(NULL);
eadb = tdb_wrap_open(mem_ctx, tdbname, 50000,
TDB_DEFAULT, O_RDWR|O_CREAT, 0600, py_default_loadparm_context(mem_ctx));
if (eadb == NULL) {
PyErr_SetFromErrno(PyExc_IOError);
talloc_free(mem_ctx);
return NULL;
}
status = pull_xattr_blob_tdb_raw(eadb, mem_ctx, attribute, filename,
-1, 100, &blob);
if (!NT_STATUS_IS_OK(status)) {
PyErr_SetNTSTATUS(status);
talloc_free(mem_ctx);
return NULL;
}
ret = PyString_FromStringAndSize((char *)blob.data, blob.length);
talloc_free(mem_ctx);
return ret;
}
static PyMethodDef py_posix_eadb_methods[] = {
{ "wrap_getxattr", (PyCFunction)py_wrap_getxattr, METH_VARARGS,
"wrap_getxattr(filename,attribute) -> blob\n"
"Retreive given attribute on the given file." },
{ "wrap_setxattr", (PyCFunction)py_wrap_setxattr, METH_VARARGS,
"wrap_setxattr(filename,attribute,value)\n"
"Set the given attribute to the given value on the given file." },
{ "is_xattr_supported", (PyCFunction)py_is_xattr_supported, METH_NOARGS,
"Return true if xattr are supported on this system\n"},
{ NULL }
};
void initposix_eadb(void)
{
PyObject *m;
m = Py_InitModule3("posix_eadb", py_posix_eadb_methods,
"Python bindings for xattr manipulation.");
if (m == NULL)
return;
}

View File

@ -28,6 +28,10 @@
#include "ntvfs/posix/posix_eadb.h"
#include "libcli/util/pyerrors.h"
#include "param/pyparam.h"
#include "source3/lib/dbwrap/dbwrap.h"
#include "source3/lib/dbwrap/dbwrap_open.h"
#include "source3/lib/dbwrap/dbwrap_tdb.h"
#include "source3/lib/xattr_tdb.h"
void initxattr_tdb(void);
@ -41,9 +45,11 @@ static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
char *filename, *attribute, *tdbname;
DATA_BLOB blob;
int blobsize;
NTSTATUS status;
int ret;
TALLOC_CTX *mem_ctx;
struct tdb_wrap *eadb;
struct db_context *eadb = NULL;
struct file_id id;
struct stat sbuf;
if (!PyArg_ParseTuple(args, "ssss#", &tdbname, &filename, &attribute,
&blob.data, &blobsize))
@ -51,19 +57,29 @@ static PyObject *py_wrap_setxattr(PyObject *self, PyObject *args)
blob.length = blobsize;
mem_ctx = talloc_new(NULL);
eadb = tdb_wrap_open(mem_ctx, tdbname, 50000,
TDB_DEFAULT, O_RDWR|O_CREAT, 0600,
py_default_loadparm_context(mem_ctx));
eadb = db_open_tdb(mem_ctx, py_default_loadparm_context(mem_ctx), tdbname, 50000,
TDB_DEFAULT, O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_2);
if (eadb == NULL) {
PyErr_SetFromErrno(PyExc_IOError);
talloc_free(mem_ctx);
return NULL;
}
status = push_xattr_blob_tdb_raw(eadb, attribute, filename, -1,
&blob);
if (!NT_STATUS_IS_OK(status)) {
PyErr_SetNTSTATUS(status);
ret = stat(filename, &sbuf);
if (ret < 0) {
PyErr_SetFromErrno(PyExc_IOError);
talloc_free(mem_ctx);
return NULL;
}
ZERO_STRUCT(id);
id.devid = sbuf.st_dev;
id.inode = sbuf.st_ino;
ret = xattr_tdb_setattr(eadb, &id, attribute, blob.data, blob.length, 0);
if (ret < 0) {
PyErr_SetFromErrno(PyExc_TypeError);
talloc_free(mem_ctx);
return NULL;
}
@ -76,31 +92,47 @@ static PyObject *py_wrap_getxattr(PyObject *self, PyObject *args)
char *filename, *attribute, *tdbname;
TALLOC_CTX *mem_ctx;
DATA_BLOB blob;
PyObject *ret;
NTSTATUS status;
struct tdb_wrap *eadb = NULL;
PyObject *ret_obj;
int ret;
ssize_t xattr_size;
struct db_context *eadb = NULL;
struct file_id id;
struct stat sbuf;
if (!PyArg_ParseTuple(args, "sss", &tdbname, &filename, &attribute))
return NULL;
mem_ctx = talloc_new(NULL);
eadb = tdb_wrap_open(mem_ctx, tdbname, 50000,
TDB_DEFAULT, O_RDWR|O_CREAT, 0600, py_default_loadparm_context(mem_ctx));
eadb = db_open_tdb(mem_ctx, py_default_loadparm_context(mem_ctx), tdbname, 50000,
TDB_DEFAULT, O_RDWR|O_CREAT, 0600, DBWRAP_LOCK_ORDER_2);
if (eadb == NULL) {
PyErr_SetFromErrno(PyExc_IOError);
talloc_free(mem_ctx);
return NULL;
}
status = pull_xattr_blob_tdb_raw(eadb, mem_ctx, attribute, filename,
-1, 100, &blob);
if (!NT_STATUS_IS_OK(status)) {
PyErr_SetNTSTATUS(status);
ret = stat(filename, &sbuf);
if (ret < 0) {
PyErr_SetFromErrno(PyExc_IOError);
talloc_free(mem_ctx);
return NULL;
}
ret = PyString_FromStringAndSize((char *)blob.data, blob.length);
ZERO_STRUCT(id);
id.devid = sbuf.st_dev;
id.inode = sbuf.st_ino;
xattr_size = xattr_tdb_getattr(eadb, mem_ctx, &id, attribute, &blob);
if (xattr_size < 0) {
PyErr_SetFromErrno(PyExc_TypeError);
talloc_free(mem_ctx);
return ret;
return NULL;
}
ret_obj = PyString_FromStringAndSize((char *)blob.data, xattr_size);
talloc_free(mem_ctx);
return ret_obj;
}
static PyMethodDef py_xattr_methods[] = {

View File

@ -52,9 +52,15 @@ bld.SAMBA_LIBRARY('posix_eadb',
autoproto='posix_eadb_proto.h',
private_library=True)
bld.SAMBA_PYTHON('python_posix_eadb',
source='python/pyposix_eadb.c',
deps='pyparam_util posix_eadb',
realname='samba/posix_eadb.so'
)
bld.SAMBA_PYTHON('python_xattr_tdb',
source='python/pyxattr_tdb.c',
deps='pyparam_util posix_eadb',
deps='pyparam_util xattr_tdb',
realname='samba/xattr_tdb.so'
)

View File

@ -20,7 +20,7 @@
import os
import samba.xattr_native, samba.xattr_tdb
import samba.xattr_native, samba.xattr_tdb, samba.posix_eadb
from samba.dcerpc import security, xattr
from samba.ndr import ndr_pack, ndr_unpack
@ -31,28 +31,38 @@ class XattrBackendError(Exception):
def checkset_backend(lp, backend, eadbfile):
'''return the path to the eadb, or None'''
if backend is None:
return lp.get("posix:eadb")
xattr_tdb = lp.get("xattr_tdb:file")
if xattr_tdb is not None:
return (samba.xattr_tdb, lp.get("xattr_tdb:file"))
posix_eadb = lp.get("posix:eadb")
if posix_eadb is not None:
return (samba.posix_eadb, lp.get("posix:eadb"))
elif backend == "native":
return None
return (None, None)
elif backend == "eadb":
if eadbfile is not None:
return (samba.posix_eadb, eadbfile)
else:
return (samba.posix_eadb, os.path.abspath(os.path.join(lp.get("private dir"), "eadb.tdb")))
elif backend == "tdb":
if eadbfile is not None:
return eadbfile
return (samba.xattr_tdb, eadbfile)
else:
return os.path.abspath(os.path.join(lp.get("private dir"), "eadb.tdb"))
return (samba.xattr_tdb, os.path.abspath(os.path.join(lp.get("state dir"), "xattr.tdb")))
else:
raise XattrBackendError("Invalid xattr backend choice %s"%backend)
def getntacl(lp, file, backend=None, eadbfile=None):
eadbname = checkset_backend(lp, backend, eadbfile)
if eadbname is not None:
(backend_obj, dbname) = checkset_backend(lp, backend, eadbfile)
if dbname is not None:
try:
attribute = samba.xattr_tdb.wrap_getxattr(eadbname, file,
attribute = backend_obj.wrap_getxattr(dbname, file,
xattr.XATTR_NTACL_NAME)
except Exception:
# FIXME: Don't catch all exceptions, just those related to opening
# xattrdb
print "Fail to open %s" % eadbname
print "Fail to open %s" % dbname
attribute = samba.xattr_native.wrap_getxattr(file,
xattr.XATTR_NTACL_NAME)
else:
@ -63,20 +73,20 @@ def getntacl(lp, file, backend=None, eadbfile=None):
def setntacl(lp, file, sddl, domsid, backend=None, eadbfile=None):
eadbname = checkset_backend(lp, backend, eadbfile)
(backend_obj, dbname) = checkset_backend(lp, backend, eadbfile)
ntacl = xattr.NTACL()
ntacl.version = 1
sid = security.dom_sid(domsid)
sd = security.descriptor.from_sddl(sddl, sid)
ntacl.info = sd
if eadbname is not None:
if dbname is not None:
try:
samba.xattr_tdb.wrap_setxattr(eadbname,
backend_obj.wrap_setxattr(dbname,
file, xattr.XATTR_NTACL_NAME, ndr_pack(ntacl))
except Exception:
# FIXME: Don't catch all exceptions, just those related to opening
# xattrdb
print "Fail to open %s" % eadbname
print "Fail to open %s" % dbname
samba.xattr_native.wrap_setxattr(file, xattr.XATTR_NTACL_NAME,
ndr_pack(ntacl))
else: