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
41 #define DIRECTORY_FLAGS O_RDONLY|O_DIRECTORY
43 /* POSIX allows us to open a directory with O_RDONLY. */
44 #define DIRECTORY_FLAGS O_RDONLY
48 static connection_struct *get_conn_tos(
50 const struct auth_session_info *session_info)
52 struct conn_struct_tos *c = NULL;
56 if (!posix_locking_init(false)) {
62 snum = lp_servicenumber(service);
64 PyErr_SetString(PyExc_RuntimeError, "unknown service");
69 status = create_conn_struct_tos(NULL,
74 PyErr_NTSTATUS_IS_ERR_RAISE(status);
76 /* Ignore read-only and share restrictions */
77 c->conn->read_only = false;
78 c->conn->share_access = SEC_RIGHTS_FILE_ALL;
82 static int set_sys_acl_conn(const char *fname,
83 SMB_ACL_TYPE_T acltype,
84 SMB_ACL_T theacl, connection_struct *conn)
87 struct smb_filename *smb_fname = NULL;
90 TALLOC_CTX *frame = talloc_stackframe();
92 /* we want total control over the permissions on created files,
93 so set our umask to 0 */
94 saved_umask = umask(0);
96 smb_fname = synthetic_smb_fname_split(frame,
98 lp_posix_pathnames());
99 if (smb_fname == NULL) {
105 ret = SMB_VFS_SYS_ACL_SET_FILE( conn, smb_fname, acltype, theacl);
114 static NTSTATUS init_files_struct(TALLOC_CTX *mem_ctx,
116 struct connection_struct *conn,
118 struct files_struct **_fsp)
120 struct smb_filename *smb_fname = NULL;
123 struct files_struct *fsp;
125 fsp = talloc_zero(mem_ctx, struct files_struct);
127 return NT_STATUS_NO_MEMORY;
129 fsp->fh = talloc(fsp, struct fd_handle);
130 if (fsp->fh == NULL) {
131 return NT_STATUS_NO_MEMORY;
135 /* we want total control over the permissions on created files,
136 so set our umask to 0 */
137 saved_umask = umask(0);
139 smb_fname = synthetic_smb_fname_split(fsp,
141 lp_posix_pathnames());
142 if (smb_fname == NULL) {
144 return NT_STATUS_NO_MEMORY;
147 fsp->fsp_name = smb_fname;
148 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, 00644);
149 if (fsp->fh->fd == -1) {
151 return NT_STATUS_INVALID_PARAMETER;
154 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
156 /* If we have an fd, this stat should succeed. */
157 DEBUG(0,("Error doing fstat on open file %s (%s)\n",
158 smb_fname_str_dbg(smb_fname),
161 return map_nt_error_from_unix(errno);
164 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
165 fsp->vuid = UID_FIELD_INVALID;
167 fsp->can_lock = True;
168 fsp->can_read = True;
169 fsp->can_write = True;
170 fsp->print_file = NULL;
171 fsp->modified = False;
172 fsp->sent_oplock_break = NO_BREAK_SENT;
173 fsp->is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
180 static NTSTATUS set_nt_acl_conn(const char *fname,
181 uint32_t security_info_sent, const struct security_descriptor *sd,
182 connection_struct *conn)
184 TALLOC_CTX *frame = talloc_stackframe();
185 struct files_struct *fsp = NULL;
186 NTSTATUS status = NT_STATUS_OK;
188 /* first, try to open it as a file with flag O_RDWR */
189 status = init_files_struct(frame,
194 if (!NT_STATUS_IS_OK(status) && errno == EISDIR) {
195 /* if fail, try to open as dir */
196 status = init_files_struct(frame,
203 if (!NT_STATUS_IS_OK(status)) {
204 printf("open: error=%d (%s)\n", errno, strerror(errno));
210 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, sd);
211 if (!NT_STATUS_IS_OK(status)) {
212 DEBUG(0,("set_nt_acl_no_snum: fset_nt_acl returned %s.\n", nt_errstr(status)));
221 static NTSTATUS get_nt_acl_conn(TALLOC_CTX *mem_ctx,
223 connection_struct *conn,
224 uint32_t security_info_wanted,
225 struct security_descriptor **sd)
227 TALLOC_CTX *frame = talloc_stackframe();
229 struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
233 lp_posix_pathnames() ?
234 SMB_FILENAME_POSIX_PATH : 0);
236 if (smb_fname == NULL) {
238 return NT_STATUS_NO_MEMORY;
241 status = SMB_VFS_GET_NT_ACL(conn,
243 security_info_wanted,
246 if (!NT_STATUS_IS_OK(status)) {
247 DEBUG(0,("get_nt_acl_conn: get_nt_acl returned %s.\n", nt_errstr(status)));
255 static int set_acl_entry_perms(SMB_ACL_ENTRY_T entry, mode_t perm_mask)
257 SMB_ACL_PERMSET_T perms = NULL;
259 if (sys_acl_get_permset(entry, &perms) != 0) {
263 if (sys_acl_clear_perms(perms) != 0) {
267 if ((perm_mask & SMB_ACL_READ) != 0 &&
268 sys_acl_add_perm(perms, SMB_ACL_READ) != 0) {
272 if ((perm_mask & SMB_ACL_WRITE) != 0 &&
273 sys_acl_add_perm(perms, SMB_ACL_WRITE) != 0) {
277 if ((perm_mask & SMB_ACL_EXECUTE) != 0 &&
278 sys_acl_add_perm(perms, SMB_ACL_EXECUTE) != 0) {
282 if (sys_acl_set_permset(entry, perms) != 0) {
289 static SMB_ACL_T make_simple_acl(TALLOC_CTX *mem_ctx,
293 mode_t mode = SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE;
295 mode_t mode_user = (chmod_mode & 0700) >> 6;
296 mode_t mode_group = (chmod_mode & 070) >> 3;
297 mode_t mode_other = chmod_mode & 07;
298 SMB_ACL_ENTRY_T entry;
299 SMB_ACL_T acl = sys_acl_init(mem_ctx);
305 if (sys_acl_create_entry(&acl, &entry) != 0) {
310 if (sys_acl_set_tag_type(entry, SMB_ACL_USER_OBJ) != 0) {
315 if (set_acl_entry_perms(entry, mode_user) != 0) {
320 if (sys_acl_create_entry(&acl, &entry) != 0) {
325 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP_OBJ) != 0) {
330 if (set_acl_entry_perms(entry, mode_group) != 0) {
335 if (sys_acl_create_entry(&acl, &entry) != 0) {
340 if (sys_acl_set_tag_type(entry, SMB_ACL_OTHER) != 0) {
345 if (set_acl_entry_perms(entry, mode_other) != 0) {
351 if (sys_acl_create_entry(&acl, &entry) != 0) {
356 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP) != 0) {
361 if (sys_acl_set_qualifier(entry, &gid) != 0) {
366 if (set_acl_entry_perms(entry, mode_group) != 0) {
372 if (sys_acl_create_entry(&acl, &entry) != 0) {
377 if (sys_acl_set_tag_type(entry, SMB_ACL_MASK) != 0) {
382 if (set_acl_entry_perms(entry, mode) != 0) {
391 set a simple ACL on a file, as a test
393 static PyObject *py_smbd_set_simple_acl(PyObject *self, PyObject *args, PyObject *kwargs)
395 const char * const kwnames[] = { "fname", "mode", "gid", "service", NULL };
396 char *fname, *service = NULL;
401 connection_struct *conn;
403 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|iz",
404 discard_const_p(char *, kwnames),
405 &fname, &mode, &gid, &service))
408 frame = talloc_stackframe();
410 acl = make_simple_acl(frame, gid, mode);
416 conn = get_conn_tos(service, NULL);
422 ret = set_sys_acl_conn(fname, SMB_ACL_TYPE_ACCESS, acl, conn);
427 return PyErr_SetFromErrno(PyExc_OSError);
438 static PyObject *py_smbd_chown(PyObject *self, PyObject *args, PyObject *kwargs)
440 const char * const kwnames[] = { "fname", "uid", "gid", "service", NULL };
441 connection_struct *conn;
444 char *fname, *service = NULL;
448 struct smb_filename *smb_fname = NULL;
450 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sii|z",
451 discard_const_p(char *, kwnames),
452 &fname, &uid, &gid, &service))
455 frame = talloc_stackframe();
457 conn = get_conn_tos(service, NULL);
463 /* we want total control over the permissions on created files,
464 so set our umask to 0 */
465 saved_umask = umask(0);
467 smb_fname = synthetic_smb_fname(talloc_tos(),
471 lp_posix_pathnames() ?
472 SMB_FILENAME_POSIX_PATH : 0);
473 if (smb_fname == NULL) {
477 return PyErr_SetFromErrno(PyExc_OSError);
480 ret = SMB_VFS_CHOWN(conn, smb_fname, uid, gid);
485 return PyErr_SetFromErrno(PyExc_OSError);
498 static PyObject *py_smbd_unlink(PyObject *self, PyObject *args, PyObject *kwargs)
500 const char * const kwnames[] = { "fname", "service", NULL };
501 connection_struct *conn;
503 struct smb_filename *smb_fname = NULL;
504 char *fname, *service = NULL;
507 frame = talloc_stackframe();
509 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|z",
510 discard_const_p(char *, kwnames),
516 conn = get_conn_tos(service, NULL);
522 smb_fname = synthetic_smb_fname_split(frame,
524 lp_posix_pathnames());
525 if (smb_fname == NULL) {
527 return PyErr_NoMemory();
530 ret = SMB_VFS_UNLINK(conn, smb_fname);
534 return PyErr_SetFromErrno(PyExc_OSError);
543 check if we have ACL support
545 static PyObject *py_smbd_have_posix_acls(PyObject *self)
547 #ifdef HAVE_POSIX_ACLS
548 return PyBool_FromLong(true);
550 return PyBool_FromLong(false);
555 set the NT ACL on a file
557 static PyObject *py_smbd_set_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
559 const char * const kwnames[] = {
560 "fname", "security_info_sent", "sd",
561 "service", "session_info", NULL };
564 char *fname, *service = NULL;
565 int security_info_sent;
567 struct security_descriptor *sd;
568 PyObject *py_session = Py_None;
569 struct auth_session_info *session_info = NULL;
570 connection_struct *conn;
573 frame = talloc_stackframe();
575 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|zO",
576 discard_const_p(char *, kwnames),
577 &fname, &security_info_sent, &py_sd,
578 &service, &py_session)) {
583 if (!py_check_dcerpc_type(py_sd, "samba.dcerpc.security", "descriptor")) {
588 if (py_session != Py_None) {
589 if (!py_check_dcerpc_type(py_session,
595 session_info = pytalloc_get_type(py_session,
596 struct auth_session_info);
598 PyErr_Format(PyExc_TypeError,
599 "Expected auth_session_info for session_info argument got %s",
600 talloc_get_name(pytalloc_get_ptr(py_session)));
605 conn = get_conn_tos(service, session_info);
611 sd = pytalloc_get_type(py_sd, struct security_descriptor);
613 status = set_nt_acl_conn(fname, security_info_sent, sd, conn);
615 PyErr_NTSTATUS_IS_ERR_RAISE(status);
621 Return the NT ACL on a file
623 static PyObject *py_smbd_get_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
625 const char * const kwnames[] = { "fname", "security_info_wanted", "service", NULL };
626 char *fname, *service = NULL;
627 int security_info_wanted;
629 struct security_descriptor *sd;
630 TALLOC_CTX *frame = talloc_stackframe();
631 connection_struct *conn;
634 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|z", discard_const_p(char *, kwnames),
635 &fname, &security_info_wanted, &service)) {
640 conn = get_conn_tos(service, NULL);
646 status = get_nt_acl_conn(frame, fname, conn, security_info_wanted, &sd);
647 PyErr_NTSTATUS_IS_ERR_RAISE(status);
649 py_sd = py_return_ndr_struct("samba.dcerpc.security", "descriptor", sd, sd);
657 set the posix (or similar) ACL on a file
659 static PyObject *py_smbd_set_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
661 const char * const kwnames[] = { "fname", "acl_type", "acl", "service", NULL };
662 TALLOC_CTX *frame = talloc_stackframe();
664 char *fname, *service = NULL;
666 struct smb_acl_t *acl;
668 connection_struct *conn;
670 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|z",
671 discard_const_p(char *, kwnames),
672 &fname, &acl_type, &py_acl, &service)) {
677 if (!py_check_dcerpc_type(py_acl, "samba.dcerpc.smb_acl", "t")) {
682 conn = get_conn_tos(service, NULL);
688 acl = pytalloc_get_type(py_acl, struct smb_acl_t);
690 ret = set_sys_acl_conn(fname, acl_type, acl, conn);
694 return PyErr_SetFromErrno(PyExc_OSError);
702 Return the posix (or similar) ACL on a file
704 static PyObject *py_smbd_get_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
706 const char * const kwnames[] = { "fname", "acl_type", "service", NULL };
709 struct smb_acl_t *acl;
711 TALLOC_CTX *frame = talloc_stackframe();
712 connection_struct *conn;
713 char *service = NULL;
714 struct smb_filename *smb_fname = NULL;
716 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|z",
717 discard_const_p(char *, kwnames),
718 &fname, &acl_type, &service)) {
723 conn = get_conn_tos(service, NULL);
729 smb_fname = synthetic_smb_fname_split(frame,
731 lp_posix_pathnames());
732 if (smb_fname == NULL) {
736 acl = SMB_VFS_SYS_ACL_GET_FILE( conn, smb_fname, acl_type, frame);
739 return PyErr_SetFromErrno(PyExc_OSError);
742 py_acl = py_return_ndr_struct("samba.dcerpc.smb_acl", "t", acl, acl);
749 static PyObject *py_smbd_mkdir(PyObject *self, PyObject *args, PyObject *kwargs)
751 const char * const kwnames[] = { "fname", "service", NULL };
752 char *fname, *service = NULL;
753 TALLOC_CTX *frame = talloc_stackframe();
754 struct connection_struct *conn = NULL;
755 struct smb_filename *smb_fname = NULL;
757 if (!PyArg_ParseTupleAndKeywords(args,
760 discard_const_p(char *,
768 conn = get_conn_tos(service, NULL);
774 smb_fname = synthetic_smb_fname(talloc_tos(),
778 lp_posix_pathnames() ?
779 SMB_FILENAME_POSIX_PATH : 0);
781 if (smb_fname == NULL) {
787 if (SMB_VFS_MKDIR(conn, smb_fname, 00755) == -1) {
788 DBG_ERR("mkdir error=%d (%s)\n", errno, strerror(errno));
801 static PyObject *py_smbd_create_file(PyObject *self, PyObject *args, PyObject *kwargs)
803 const char * const kwnames[] = { "fname", "service", NULL };
804 char *fname, *service = NULL;
805 TALLOC_CTX *frame = talloc_stackframe();
806 struct connection_struct *conn = NULL;
807 struct files_struct *fsp = NULL;
810 if (!PyArg_ParseTupleAndKeywords(args,
813 discard_const_p(char *,
821 conn = get_conn_tos(service, NULL);
827 status = init_files_struct(frame,
830 O_CREAT|O_EXCL|O_RDWR,
832 if (!NT_STATUS_IS_OK(status)) {
833 DBG_ERR("init_files_struct failed: %s\n",
842 static PyMethodDef py_smbd_methods[] = {
844 (PyCFunction)py_smbd_have_posix_acls, METH_NOARGS,
847 (PyCFunction)py_smbd_set_simple_acl, METH_VARARGS|METH_KEYWORDS,
850 (PyCFunction)py_smbd_set_nt_acl, METH_VARARGS|METH_KEYWORDS,
853 (PyCFunction)py_smbd_get_nt_acl, METH_VARARGS|METH_KEYWORDS,
856 (PyCFunction)py_smbd_get_sys_acl, METH_VARARGS|METH_KEYWORDS,
859 (PyCFunction)py_smbd_set_sys_acl, METH_VARARGS|METH_KEYWORDS,
862 (PyCFunction)py_smbd_chown, METH_VARARGS|METH_KEYWORDS,
865 (PyCFunction)py_smbd_unlink, METH_VARARGS|METH_KEYWORDS,
868 (PyCFunction)py_smbd_mkdir, METH_VARARGS|METH_KEYWORDS,
871 (PyCFunction)py_smbd_create_file, METH_VARARGS|METH_KEYWORDS,
878 static struct PyModuleDef moduledef = {
879 PyModuleDef_HEAD_INIT,
881 .m_doc = "Python bindings for the smbd file server.",
883 .m_methods = py_smbd_methods,
886 MODULE_INIT_FUNC(smbd)
890 m = PyModule_Create(&moduledef);