2 Unix SMB/CIFS implementation.
3 Set NT and POSIX ACLs and other VFS operations from Python
5 Copyrigyt (C) Andrew Bartlett 2012
6 Copyright (C) Jeremy Allison 1994-2009.
7 Copyright (C) Andreas Gruenbacher 2002.
8 Copyright (C) Simo Sorce <idra@samba.org> 2009.
9 Copyright (C) Simo Sorce 2002
10 Copyright (C) Eric Lorimer 2002
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "smbd/smbd.h"
29 #include "libcli/util/pyerrors.h"
30 #include "librpc/rpc/pyrpc_util.h"
32 #include "system/filesys.h"
34 extern const struct generic_mapping file_generic_mapping;
37 #define DBGC_CLASS DBGC_ACLS
39 static NTSTATUS set_sys_acl_no_snum(const char *fname,
40 SMB_ACL_TYPE_T acltype,
43 connection_struct *conn;
44 NTSTATUS status = NT_STATUS_OK;
47 conn = talloc_zero(NULL, connection_struct);
49 DEBUG(0, ("talloc failed\n"));
50 return NT_STATUS_NO_MEMORY;
53 if (!(conn->params = talloc(conn, struct share_params))) {
54 DEBUG(0,("set_sys_acl_no_snum: talloc() failed!\n"));
56 return NT_STATUS_NO_MEMORY;
59 conn->params->service = -1;
61 set_conn_connectpath(conn, "/");
65 ret = SMB_VFS_SYS_ACL_SET_FILE( conn, fname, acltype, theacl);
67 status = map_nt_error_from_unix_common(ret);
68 DEBUG(0,("set_sys_acl_no_snum: SMB_VFS_SYS_ACL_SET_FILE "
77 static NTSTATUS set_nt_acl_no_snum(const char *fname,
78 uint32 security_info_sent, const struct security_descriptor *sd)
80 TALLOC_CTX *frame = talloc_stackframe();
81 connection_struct *conn;
82 NTSTATUS status = NT_STATUS_OK;
84 struct smb_filename *smb_fname = NULL;
87 conn = talloc_zero(frame, connection_struct);
89 DEBUG(0, ("talloc failed\n"));
90 return NT_STATUS_NO_MEMORY;
93 if (!(conn->params = talloc(conn, struct share_params))) {
94 DEBUG(0,("set_nt_acl_no_snum: talloc() failed!\n"));
96 return NT_STATUS_NO_MEMORY;
99 conn->params->service = -1;
101 set_conn_connectpath(conn, "/");
104 if (!posix_locking_init(false)) {
105 return NT_STATUS_NO_MEMORY;
108 fsp = talloc_zero(frame, struct files_struct);
111 return NT_STATUS_NO_MEMORY;
113 fsp->fh = talloc(fsp, struct fd_handle);
114 if (fsp->fh == NULL) {
116 return NT_STATUS_NO_MEMORY;
120 status = create_synthetic_smb_fname_split(fsp, fname, NULL,
122 if (!NT_STATUS_IS_OK(status)) {
127 fsp->fsp_name = smb_fname;
130 flags = O_RDONLY|O_DIRECTORY;
132 /* POSIX allows us to open a directory with O_RDONLY. */
136 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, O_RDWR, 00400);
137 if (fsp->fh->fd == -1 && errno == EISDIR) {
138 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, 00400);
140 if (fsp->fh->fd == -1) {
141 printf("open: error=%d (%s)\n", errno, strerror(errno));
143 return NT_STATUS_UNSUCCESSFUL;
146 status = SMB_VFS_FSET_NT_ACL( fsp, security_info_sent, sd);
147 if (!NT_STATUS_IS_OK(status)) {
148 DEBUG(0,("set_nt_acl_no_snum: fset_nt_acl returned %s.\n", nt_errstr(status)));
160 static SMB_ACL_T make_simple_acl(gid_t gid, mode_t chmod_mode)
162 TALLOC_CTX *frame = talloc_stackframe();
164 mode_t mode = SMB_ACL_READ|SMB_ACL_WRITE;
166 mode_t mode_user = (chmod_mode & 0700) >> 6;
167 mode_t mode_group = (chmod_mode & 070) >> 3;
168 mode_t mode_other = chmod_mode & 07;
169 SMB_ACL_ENTRY_T entry;
170 SMB_ACL_T acl = sys_acl_init(frame);
176 if (sys_acl_create_entry(&acl, &entry) != 0) {
181 if (sys_acl_set_tag_type(entry, SMB_ACL_USER_OBJ) != 0) {
186 if (sys_acl_set_permset(entry, &mode_user) != 0) {
191 if (sys_acl_create_entry(&acl, &entry) != 0) {
196 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP_OBJ) != 0) {
201 if (sys_acl_set_permset(entry, &mode_group) != 0) {
206 if (sys_acl_create_entry(&acl, &entry) != 0) {
211 if (sys_acl_set_tag_type(entry, SMB_ACL_OTHER) != 0) {
216 if (sys_acl_set_permset(entry, &mode_other) != 0) {
222 if (sys_acl_create_entry(&acl, &entry) != 0) {
227 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP) != 0) {
232 if (sys_acl_set_qualifier(entry, &gid) != 0) {
237 if (sys_acl_set_permset(entry, &mode_group) != 0) {
243 if (sys_acl_create_entry(&acl, &entry) != 0) {
248 if (sys_acl_set_tag_type(entry, SMB_ACL_MASK) != 0) {
253 if (sys_acl_set_permset(entry, &mode) != 0) {
261 set a simple ACL on a file, as a test
263 static PyObject *py_smbd_set_simple_acl(PyObject *self, PyObject *args)
271 if (!PyArg_ParseTuple(args, "si|i", &fname, &mode, &gid))
274 acl = make_simple_acl(gid, mode);
276 frame = talloc_stackframe();
278 status = set_sys_acl_no_snum(fname, SMB_ACL_TYPE_ACCESS, acl);
283 PyErr_NTSTATUS_IS_ERR_RAISE(status);
291 static PyObject *py_smbd_chown(PyObject *self, PyObject *args)
293 connection_struct *conn;
294 NTSTATUS status = NT_STATUS_OK;
301 if (!PyArg_ParseTuple(args, "sii", &fname, &uid, &gid))
304 frame = talloc_stackframe();
306 conn = talloc_zero(frame, connection_struct);
312 if (!(conn->params = talloc(conn, struct share_params))) {
317 conn->params->service = -1;
319 set_conn_connectpath(conn, "/");
323 ret = SMB_VFS_CHOWN( conn, fname, uid, gid);
325 status = map_nt_error_from_unix_common(errno);
326 DEBUG(0,("chown returned failure: %s\n", strerror(errno)));
333 PyErr_NTSTATUS_IS_ERR_RAISE(status);
341 static PyObject *py_smbd_unlink(PyObject *self, PyObject *args)
343 connection_struct *conn;
344 NTSTATUS status = NT_STATUS_OK;
346 struct smb_filename *smb_fname = NULL;
352 if (!PyArg_ParseTuple(args, "s", &fname))
355 frame = talloc_stackframe();
357 conn = talloc_zero(frame, connection_struct);
363 if (!(conn->params = talloc(conn, struct share_params))) {
368 /* we want total control over the permissions on created files,
369 so set our umask to 0 */
370 saved_umask = umask(0);
372 conn->params->service = -1;
374 set_conn_connectpath(conn, "/");
378 status = create_synthetic_smb_fname_split(frame, fname, NULL,
380 if (!NT_STATUS_IS_OK(status)) {
383 PyErr_NTSTATUS_IS_ERR_RAISE(status);
386 ret = SMB_VFS_UNLINK(conn, smb_fname);
388 status = map_nt_error_from_unix_common(errno);
389 DEBUG(0,("unlink returned failure: %s\n", strerror(errno)));
398 PyErr_NTSTATUS_IS_ERR_RAISE(status);
404 check if we have ACL support
406 static PyObject *py_smbd_have_posix_acls(PyObject *self, PyObject *args)
408 #ifdef HAVE_POSIX_ACLS
409 return PyBool_FromLong(true);
411 return PyBool_FromLong(false);
416 set the NT ACL on a file
418 static PyObject *py_smbd_set_nt_acl(PyObject *self, PyObject *args)
422 int security_info_sent;
424 struct security_descriptor *sd;
426 if (!PyArg_ParseTuple(args, "siO", &fname, &security_info_sent, &py_sd))
429 if (!py_check_dcerpc_type(py_sd, "samba.dcerpc.security", "descriptor")) {
433 sd = pytalloc_get_type(py_sd, struct security_descriptor);
435 status = set_nt_acl_no_snum(fname, security_info_sent, sd);
436 PyErr_NTSTATUS_IS_ERR_RAISE(status);
442 Return the NT ACL on a file
444 static PyObject *py_smbd_get_nt_acl(PyObject *self, PyObject *args)
447 int security_info_wanted;
449 struct security_descriptor *sd;
450 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
452 if (!PyArg_ParseTuple(args, "si", &fname, &security_info_wanted))
455 sd = get_nt_acl_no_snum(tmp_ctx, fname, security_info_wanted);
457 py_sd = py_return_ndr_struct("samba.dcerpc.security", "descriptor", sd, sd);
459 talloc_free(tmp_ctx);
465 set the posix (or similar) ACL on a file
467 static PyObject *py_smbd_set_sys_acl(PyObject *self, PyObject *args)
472 struct smb_acl_t *acl;
475 if (!PyArg_ParseTuple(args, "siO", &fname, &acl_type, &py_acl))
478 if (!py_check_dcerpc_type(py_acl, "samba.dcerpc.smb_acl", "t")) {
482 acl = pytalloc_get_type(py_acl, struct smb_acl_t);
484 status = set_sys_acl_no_snum(fname, acl_type, acl);
485 PyErr_NTSTATUS_IS_ERR_RAISE(status);
491 Return the posix (or similar) ACL on a file
493 static PyObject *py_smbd_get_sys_acl(PyObject *self, PyObject *args)
497 struct smb_acl_t *acl;
499 TALLOC_CTX *frame = talloc_stackframe();
500 connection_struct *conn;
501 NTSTATUS status = NT_STATUS_OK;
503 if (!PyArg_ParseTuple(args, "si", &fname, &acl_type)) {
508 conn = talloc_zero(frame, connection_struct);
510 DEBUG(0, ("talloc failed\n"));
516 if (!(conn->params = talloc(conn, struct share_params))) {
517 DEBUG(0,("get_nt_acl_no_snum: talloc() failed!\n"));
523 conn->params->service = -1;
525 set_conn_connectpath(conn, "/");
529 acl = SMB_VFS_SYS_ACL_GET_FILE( conn, fname, acl_type, frame);
532 status = map_nt_error_from_unix_common(errno);
533 DEBUG(0,("sys_acl_get_file returned NULL: %s\n", strerror(errno)));
534 PyErr_NTSTATUS_IS_ERR_RAISE(status);
539 py_acl = py_return_ndr_struct("samba.dcerpc.smb_acl", "t", acl, acl);
546 static PyMethodDef py_smbd_methods[] = {
548 (PyCFunction)py_smbd_have_posix_acls, METH_VARARGS,
551 (PyCFunction)py_smbd_set_simple_acl, METH_VARARGS,
554 (PyCFunction)py_smbd_set_nt_acl, METH_VARARGS,
557 (PyCFunction)py_smbd_get_nt_acl, METH_VARARGS,
560 (PyCFunction)py_smbd_get_sys_acl, METH_VARARGS,
563 (PyCFunction)py_smbd_set_sys_acl, METH_VARARGS,
566 (PyCFunction)py_smbd_chown, METH_VARARGS,
569 (PyCFunction)py_smbd_unlink, METH_VARARGS,
579 m = Py_InitModule3("smbd", py_smbd_methods,
580 "Python bindings for the smbd file server.");