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/>.
28 #include "python/py3compat.h"
29 #include "smbd/smbd.h"
30 #include "libcli/util/pyerrors.h"
31 #include "librpc/rpc/pyrpc_util.h"
33 #include "system/filesys.h"
35 extern const struct generic_mapping file_generic_mapping;
38 #define DBGC_CLASS DBGC_ACLS
40 static int conn_free_wrapper(connection_struct *conn)
46 static connection_struct *get_conn(TALLOC_CTX *mem_ctx, const char *service)
48 connection_struct *conn;
49 TALLOC_CTX *frame = talloc_stackframe();
53 if (!posix_locking_init(false)) {
60 snum = lp_servicenumber(service);
63 PyErr_SetString(PyExc_RuntimeError, "unknown service");
68 status = create_conn_struct(mem_ctx, NULL, NULL, &conn, snum, "/",
70 PyErr_NTSTATUS_IS_ERR_RAISE(status);
73 /* Ignore read-only and share restrictions */
74 conn->read_only = false;
75 conn->share_access = SEC_RIGHTS_FILE_ALL;
76 talloc_set_destructor(conn, conn_free_wrapper);
80 static int set_sys_acl_conn(const char *fname,
81 SMB_ACL_TYPE_T acltype,
82 SMB_ACL_T theacl, connection_struct *conn)
85 struct smb_filename *smb_fname = NULL;
88 TALLOC_CTX *frame = talloc_stackframe();
90 /* we want total control over the permissions on created files,
91 so set our umask to 0 */
92 saved_umask = umask(0);
94 smb_fname = synthetic_smb_fname_split(frame,
96 lp_posix_pathnames());
97 if (smb_fname == NULL) {
103 ret = SMB_VFS_SYS_ACL_SET_FILE( conn, smb_fname, acltype, theacl);
111 static NTSTATUS set_nt_acl_conn(const char *fname,
112 uint32_t security_info_sent, const struct security_descriptor *sd,
113 connection_struct *conn)
115 TALLOC_CTX *frame = talloc_stackframe();
116 NTSTATUS status = NT_STATUS_OK;
118 struct smb_filename *smb_fname = NULL;
122 fsp = talloc_zero(frame, struct files_struct);
125 return NT_STATUS_NO_MEMORY;
127 fsp->fh = talloc(fsp, struct fd_handle);
128 if (fsp->fh == NULL) {
130 return NT_STATUS_NO_MEMORY;
134 /* we want total control over the permissions on created files,
135 so set our umask to 0 */
136 saved_umask = umask(0);
138 smb_fname = synthetic_smb_fname_split(fsp,
140 lp_posix_pathnames());
141 if (smb_fname == NULL) {
144 return NT_STATUS_NO_MEMORY;
147 fsp->fsp_name = smb_fname;
150 flags = O_RDONLY|O_DIRECTORY;
152 /* POSIX allows us to open a directory with O_RDONLY. */
156 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, O_RDWR, 00400);
157 if (fsp->fh->fd == -1 && errno == EISDIR) {
158 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, 00400);
160 if (fsp->fh->fd == -1) {
161 printf("open: error=%d (%s)\n", errno, strerror(errno));
164 return NT_STATUS_UNSUCCESSFUL;
167 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
169 /* If we have an fd, this stat should succeed. */
170 DEBUG(0,("Error doing fstat on open file %s "
172 smb_fname_str_dbg(smb_fname),
176 return map_nt_error_from_unix(errno);
179 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
180 fsp->vuid = UID_FIELD_INVALID;
182 fsp->can_lock = True;
183 fsp->can_read = True;
184 fsp->can_write = True;
185 fsp->print_file = NULL;
186 fsp->modified = False;
187 fsp->sent_oplock_break = NO_BREAK_SENT;
188 fsp->is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
190 status = SMB_VFS_FSET_NT_ACL( fsp, security_info_sent, sd);
191 if (!NT_STATUS_IS_OK(status)) {
192 DEBUG(0,("set_nt_acl_no_snum: fset_nt_acl returned %s.\n", nt_errstr(status)));
203 static NTSTATUS get_nt_acl_conn(TALLOC_CTX *mem_ctx,
205 connection_struct *conn,
206 uint32_t security_info_wanted,
207 struct security_descriptor **sd)
209 TALLOC_CTX *frame = talloc_stackframe();
211 struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
215 lp_posix_pathnames() ?
216 SMB_FILENAME_POSIX_PATH : 0);
218 if (smb_fname == NULL) {
220 return NT_STATUS_NO_MEMORY;
223 status = SMB_VFS_GET_NT_ACL(conn,
225 security_info_wanted,
228 if (!NT_STATUS_IS_OK(status)) {
229 DEBUG(0,("get_nt_acl_conn: get_nt_acl returned %s.\n", nt_errstr(status)));
237 static int set_acl_entry_perms(SMB_ACL_ENTRY_T entry, mode_t perm_mask)
239 SMB_ACL_PERMSET_T perms = NULL;
241 if (sys_acl_get_permset(entry, &perms) != 0) {
245 if (sys_acl_clear_perms(perms) != 0) {
249 if ((perm_mask & SMB_ACL_READ) != 0 &&
250 sys_acl_add_perm(perms, SMB_ACL_READ) != 0) {
254 if ((perm_mask & SMB_ACL_WRITE) != 0 &&
255 sys_acl_add_perm(perms, SMB_ACL_WRITE) != 0) {
259 if ((perm_mask & SMB_ACL_EXECUTE) != 0 &&
260 sys_acl_add_perm(perms, SMB_ACL_EXECUTE) != 0) {
264 if (sys_acl_set_permset(entry, perms) != 0) {
271 static SMB_ACL_T make_simple_acl(TALLOC_CTX *mem_ctx,
275 mode_t mode = SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE;
277 mode_t mode_user = (chmod_mode & 0700) >> 6;
278 mode_t mode_group = (chmod_mode & 070) >> 3;
279 mode_t mode_other = chmod_mode & 07;
280 SMB_ACL_ENTRY_T entry;
281 SMB_ACL_T acl = sys_acl_init(mem_ctx);
287 if (sys_acl_create_entry(&acl, &entry) != 0) {
292 if (sys_acl_set_tag_type(entry, SMB_ACL_USER_OBJ) != 0) {
297 if (set_acl_entry_perms(entry, mode_user) != 0) {
302 if (sys_acl_create_entry(&acl, &entry) != 0) {
307 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP_OBJ) != 0) {
312 if (set_acl_entry_perms(entry, mode_group) != 0) {
317 if (sys_acl_create_entry(&acl, &entry) != 0) {
322 if (sys_acl_set_tag_type(entry, SMB_ACL_OTHER) != 0) {
327 if (set_acl_entry_perms(entry, mode_other) != 0) {
333 if (sys_acl_create_entry(&acl, &entry) != 0) {
338 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP) != 0) {
343 if (sys_acl_set_qualifier(entry, &gid) != 0) {
348 if (set_acl_entry_perms(entry, mode_group) != 0) {
354 if (sys_acl_create_entry(&acl, &entry) != 0) {
359 if (sys_acl_set_tag_type(entry, SMB_ACL_MASK) != 0) {
364 if (set_acl_entry_perms(entry, mode) != 0) {
373 set a simple ACL on a file, as a test
375 static PyObject *py_smbd_set_simple_acl(PyObject *self, PyObject *args, PyObject *kwargs)
377 const char * const kwnames[] = { "fname", "mode", "gid", "service", NULL };
378 char *fname, *service = NULL;
383 connection_struct *conn;
385 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|iz",
386 discard_const_p(char *, kwnames),
387 &fname, &mode, &gid, &service))
390 frame = talloc_stackframe();
392 acl = make_simple_acl(frame, gid, mode);
398 conn = get_conn(frame, service);
404 ret = set_sys_acl_conn(fname, SMB_ACL_TYPE_ACCESS, acl, conn);
409 return PyErr_SetFromErrno(PyExc_OSError);
420 static PyObject *py_smbd_chown(PyObject *self, PyObject *args, PyObject *kwargs)
422 const char * const kwnames[] = { "fname", "uid", "gid", "service", NULL };
423 connection_struct *conn;
426 char *fname, *service = NULL;
430 struct smb_filename *smb_fname = NULL;
432 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sii|z",
433 discard_const_p(char *, kwnames),
434 &fname, &uid, &gid, &service))
437 frame = talloc_stackframe();
439 conn = get_conn(frame, service);
445 /* we want total control over the permissions on created files,
446 so set our umask to 0 */
447 saved_umask = umask(0);
449 smb_fname = synthetic_smb_fname(talloc_tos(),
453 lp_posix_pathnames() ?
454 SMB_FILENAME_POSIX_PATH : 0);
455 if (smb_fname == NULL) {
459 return PyErr_SetFromErrno(PyExc_OSError);
462 ret = SMB_VFS_CHOWN(conn, smb_fname, uid, gid);
467 return PyErr_SetFromErrno(PyExc_OSError);
480 static PyObject *py_smbd_unlink(PyObject *self, PyObject *args, PyObject *kwargs)
482 const char * const kwnames[] = { "fname", "service", NULL };
483 connection_struct *conn;
485 struct smb_filename *smb_fname = NULL;
486 char *fname, *service = NULL;
489 frame = talloc_stackframe();
491 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|z",
492 discard_const_p(char *, kwnames),
498 conn = get_conn(frame, service);
504 smb_fname = synthetic_smb_fname_split(frame,
506 lp_posix_pathnames());
507 if (smb_fname == NULL) {
509 return PyErr_NoMemory();
512 ret = SMB_VFS_UNLINK(conn, smb_fname);
516 return PyErr_SetFromErrno(PyExc_OSError);
525 check if we have ACL support
527 static PyObject *py_smbd_have_posix_acls(PyObject *self)
529 #ifdef HAVE_POSIX_ACLS
530 return PyBool_FromLong(true);
532 return PyBool_FromLong(false);
537 set the NT ACL on a file
539 static PyObject *py_smbd_set_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
541 const char * const kwnames[] = { "fname", "security_info_sent", "sd", "service", NULL };
543 char *fname, *service = NULL;
544 int security_info_sent;
546 struct security_descriptor *sd;
547 connection_struct *conn;
550 frame = talloc_stackframe();
552 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
553 "siO|z", discard_const_p(char *, kwnames),
554 &fname, &security_info_sent, &py_sd, &service)) {
559 if (!py_check_dcerpc_type(py_sd, "samba.dcerpc.security", "descriptor")) {
564 conn = get_conn(frame, service);
570 sd = pytalloc_get_type(py_sd, struct security_descriptor);
572 status = set_nt_acl_conn(fname, security_info_sent, sd, conn);
574 PyErr_NTSTATUS_IS_ERR_RAISE(status);
580 Return the NT ACL on a file
582 static PyObject *py_smbd_get_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
584 const char * const kwnames[] = { "fname", "security_info_wanted", "service", NULL };
585 char *fname, *service = NULL;
586 int security_info_wanted;
588 struct security_descriptor *sd;
589 TALLOC_CTX *frame = talloc_stackframe();
590 connection_struct *conn;
593 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|z", discard_const_p(char *, kwnames),
594 &fname, &security_info_wanted, &service)) {
599 conn = get_conn(frame, service);
605 status = get_nt_acl_conn(frame, fname, conn, security_info_wanted, &sd);
606 PyErr_NTSTATUS_IS_ERR_RAISE(status);
608 py_sd = py_return_ndr_struct("samba.dcerpc.security", "descriptor", sd, sd);
616 set the posix (or similar) ACL on a file
618 static PyObject *py_smbd_set_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
620 const char * const kwnames[] = { "fname", "acl_type", "acl", "service", NULL };
621 TALLOC_CTX *frame = talloc_stackframe();
623 char *fname, *service = NULL;
625 struct smb_acl_t *acl;
627 connection_struct *conn;
629 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|z",
630 discard_const_p(char *, kwnames),
631 &fname, &acl_type, &py_acl, &service)) {
636 if (!py_check_dcerpc_type(py_acl, "samba.dcerpc.smb_acl", "t")) {
641 conn = get_conn(frame, service);
647 acl = pytalloc_get_type(py_acl, struct smb_acl_t);
649 ret = set_sys_acl_conn(fname, acl_type, acl, conn);
653 return PyErr_SetFromErrno(PyExc_OSError);
661 Return the posix (or similar) ACL on a file
663 static PyObject *py_smbd_get_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
665 const char * const kwnames[] = { "fname", "acl_type", "service", NULL };
668 struct smb_acl_t *acl;
670 TALLOC_CTX *frame = talloc_stackframe();
671 connection_struct *conn;
672 char *service = NULL;
673 struct smb_filename *smb_fname = NULL;
675 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|z",
676 discard_const_p(char *, kwnames),
677 &fname, &acl_type, &service)) {
682 conn = get_conn(frame, service);
688 smb_fname = synthetic_smb_fname_split(frame,
690 lp_posix_pathnames());
691 if (smb_fname == NULL) {
695 acl = SMB_VFS_SYS_ACL_GET_FILE( conn, smb_fname, acl_type, frame);
698 return PyErr_SetFromErrno(PyExc_OSError);
701 py_acl = py_return_ndr_struct("samba.dcerpc.smb_acl", "t", acl, acl);
708 static PyMethodDef py_smbd_methods[] = {
710 (PyCFunction)py_smbd_have_posix_acls, METH_NOARGS,
713 (PyCFunction)py_smbd_set_simple_acl, METH_VARARGS|METH_KEYWORDS,
716 (PyCFunction)py_smbd_set_nt_acl, METH_VARARGS|METH_KEYWORDS,
719 (PyCFunction)py_smbd_get_nt_acl, METH_VARARGS|METH_KEYWORDS,
722 (PyCFunction)py_smbd_get_sys_acl, METH_VARARGS|METH_KEYWORDS,
725 (PyCFunction)py_smbd_set_sys_acl, METH_VARARGS|METH_KEYWORDS,
728 (PyCFunction)py_smbd_chown, METH_VARARGS|METH_KEYWORDS,
731 (PyCFunction)py_smbd_unlink, METH_VARARGS|METH_KEYWORDS,
738 static struct PyModuleDef moduledef = {
739 PyModuleDef_HEAD_INIT,
741 .m_doc = "Python bindings for the smbd file server.",
743 .m_methods = py_smbd_methods,
746 MODULE_INIT_FUNC(smbd)
750 m = PyModule_Create(&moduledef);