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 "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 int conn_free_wrapper(connection_struct *conn)
45 static connection_struct *get_conn(TALLOC_CTX *mem_ctx, const char *service)
47 connection_struct *conn;
48 TALLOC_CTX *frame = talloc_stackframe();
52 if (!posix_locking_init(false)) {
59 snum = lp_servicenumber(service);
62 PyErr_SetString(PyExc_RuntimeError, "unknown service");
67 status = create_conn_struct(mem_ctx, NULL, NULL, &conn, snum, "/",
69 PyErr_NTSTATUS_IS_ERR_RAISE(status);
72 /* Ignore read-only and share restrictions */
73 conn->read_only = false;
74 conn->share_access = SEC_RIGHTS_FILE_ALL;
75 talloc_set_destructor(conn, conn_free_wrapper);
79 static int set_sys_acl_conn(const char *fname,
80 SMB_ACL_TYPE_T acltype,
81 SMB_ACL_T theacl, connection_struct *conn)
86 TALLOC_CTX *frame = talloc_stackframe();
88 /* we want total control over the permissions on created files,
89 so set our umask to 0 */
90 saved_umask = umask(0);
92 ret = SMB_VFS_SYS_ACL_SET_FILE( conn, fname, acltype, theacl);
100 static NTSTATUS set_nt_acl_conn(const char *fname,
101 uint32_t security_info_sent, const struct security_descriptor *sd,
102 connection_struct *conn)
104 TALLOC_CTX *frame = talloc_stackframe();
105 NTSTATUS status = NT_STATUS_OK;
107 struct smb_filename *smb_fname = NULL;
111 fsp = talloc_zero(frame, struct files_struct);
114 return NT_STATUS_NO_MEMORY;
116 fsp->fh = talloc(fsp, struct fd_handle);
117 if (fsp->fh == NULL) {
119 return NT_STATUS_NO_MEMORY;
123 /* we want total control over the permissions on created files,
124 so set our umask to 0 */
125 saved_umask = umask(0);
127 smb_fname = synthetic_smb_fname_split(fsp, fname, NULL);
128 if (smb_fname == NULL) {
131 return NT_STATUS_NO_MEMORY;
134 fsp->fsp_name = smb_fname;
137 flags = O_RDONLY|O_DIRECTORY;
139 /* POSIX allows us to open a directory with O_RDONLY. */
143 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, O_RDWR, 00400);
144 if (fsp->fh->fd == -1 && errno == EISDIR) {
145 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, 00400);
147 if (fsp->fh->fd == -1) {
148 printf("open: error=%d (%s)\n", errno, strerror(errno));
151 return NT_STATUS_UNSUCCESSFUL;
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 "
159 smb_fname_str_dbg(smb_fname),
163 return map_nt_error_from_unix(errno);
166 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
167 fsp->vuid = UID_FIELD_INVALID;
169 fsp->can_lock = True;
170 fsp->can_read = True;
171 fsp->can_write = True;
172 fsp->print_file = NULL;
173 fsp->modified = False;
174 fsp->sent_oplock_break = NO_BREAK_SENT;
175 fsp->is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
177 status = SMB_VFS_FSET_NT_ACL( fsp, security_info_sent, sd);
178 if (!NT_STATUS_IS_OK(status)) {
179 DEBUG(0,("set_nt_acl_no_snum: fset_nt_acl returned %s.\n", nt_errstr(status)));
191 static NTSTATUS get_nt_acl_conn(TALLOC_CTX *mem_ctx,
193 connection_struct *conn,
194 uint32_t security_info_wanted,
195 struct security_descriptor **sd)
197 TALLOC_CTX *frame = talloc_stackframe();
199 struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
204 if (smb_fname == NULL) {
206 return NT_STATUS_NO_MEMORY;
209 status = SMB_VFS_GET_NT_ACL(conn,
211 security_info_wanted,
214 if (!NT_STATUS_IS_OK(status)) {
215 DEBUG(0,("get_nt_acl_conn: get_nt_acl returned %s.\n", nt_errstr(status)));
224 static SMB_ACL_T make_simple_acl(gid_t gid, mode_t chmod_mode)
226 TALLOC_CTX *frame = talloc_stackframe();
228 mode_t mode = SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE;
230 mode_t mode_user = (chmod_mode & 0700) >> 6;
231 mode_t mode_group = (chmod_mode & 070) >> 3;
232 mode_t mode_other = chmod_mode & 07;
233 SMB_ACL_ENTRY_T entry;
234 SMB_ACL_T acl = sys_acl_init(frame);
240 if (sys_acl_create_entry(&acl, &entry) != 0) {
245 if (sys_acl_set_tag_type(entry, SMB_ACL_USER_OBJ) != 0) {
250 if (sys_acl_set_permset(entry, &mode_user) != 0) {
255 if (sys_acl_create_entry(&acl, &entry) != 0) {
260 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP_OBJ) != 0) {
265 if (sys_acl_set_permset(entry, &mode_group) != 0) {
270 if (sys_acl_create_entry(&acl, &entry) != 0) {
275 if (sys_acl_set_tag_type(entry, SMB_ACL_OTHER) != 0) {
280 if (sys_acl_set_permset(entry, &mode_other) != 0) {
286 if (sys_acl_create_entry(&acl, &entry) != 0) {
291 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP) != 0) {
296 if (sys_acl_set_qualifier(entry, &gid) != 0) {
301 if (sys_acl_set_permset(entry, &mode_group) != 0) {
307 if (sys_acl_create_entry(&acl, &entry) != 0) {
312 if (sys_acl_set_tag_type(entry, SMB_ACL_MASK) != 0) {
317 if (sys_acl_set_permset(entry, &mode) != 0) {
325 set a simple ACL on a file, as a test
327 static PyObject *py_smbd_set_simple_acl(PyObject *self, PyObject *args, PyObject *kwargs)
329 const char * const kwnames[] = { "fname", "mode", "gid", "service", NULL };
330 char *fname, *service = NULL;
335 connection_struct *conn;
337 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|iz",
338 discard_const_p(char *, kwnames),
339 &fname, &mode, &gid, &service))
342 acl = make_simple_acl(gid, mode);
344 frame = talloc_stackframe();
346 conn = get_conn(frame, service);
351 ret = set_sys_acl_conn(fname, SMB_ACL_TYPE_ACCESS, acl, conn);
357 return PyErr_SetFromErrno(PyExc_OSError);
368 static PyObject *py_smbd_chown(PyObject *self, PyObject *args, PyObject *kwargs)
370 const char * const kwnames[] = { "fname", "uid", "gid", "service", NULL };
371 connection_struct *conn;
374 char *fname, *service = NULL;
379 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sii|z",
380 discard_const_p(char *, kwnames),
381 &fname, &uid, &gid, &service))
384 frame = talloc_stackframe();
386 conn = get_conn(frame, service);
391 /* we want total control over the permissions on created files,
392 so set our umask to 0 */
393 saved_umask = umask(0);
395 ret = SMB_VFS_CHOWN( conn, fname, uid, gid);
400 return PyErr_SetFromErrno(PyExc_OSError);
413 static PyObject *py_smbd_unlink(PyObject *self, PyObject *args, PyObject *kwargs)
415 const char * const kwnames[] = { "fname", "service", NULL };
416 connection_struct *conn;
418 struct smb_filename *smb_fname = NULL;
419 char *fname, *service = NULL;
422 frame = talloc_stackframe();
424 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|z",
425 discard_const_p(char *, kwnames),
431 conn = get_conn(frame, service);
437 smb_fname = synthetic_smb_fname_split(frame, fname, NULL);
438 if (smb_fname == NULL) {
440 return PyErr_NoMemory();
443 ret = SMB_VFS_UNLINK(conn, smb_fname);
447 return PyErr_SetFromErrno(PyExc_OSError);
456 check if we have ACL support
458 static PyObject *py_smbd_have_posix_acls(PyObject *self)
460 #ifdef HAVE_POSIX_ACLS
461 return PyBool_FromLong(true);
463 return PyBool_FromLong(false);
468 set the NT ACL on a file
470 static PyObject *py_smbd_set_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
472 const char * const kwnames[] = { "fname", "security_info_sent", "sd", "service", NULL };
474 char *fname, *service = NULL;
475 int security_info_sent;
477 struct security_descriptor *sd;
478 connection_struct *conn;
481 frame = talloc_stackframe();
483 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
484 "siO|z", discard_const_p(char *, kwnames),
485 &fname, &security_info_sent, &py_sd, &service)) {
490 if (!py_check_dcerpc_type(py_sd, "samba.dcerpc.security", "descriptor")) {
495 conn = get_conn(frame, service);
501 sd = pytalloc_get_type(py_sd, struct security_descriptor);
503 status = set_nt_acl_conn(fname, security_info_sent, sd, conn);
505 PyErr_NTSTATUS_IS_ERR_RAISE(status);
511 Return the NT ACL on a file
513 static PyObject *py_smbd_get_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
515 const char * const kwnames[] = { "fname", "security_info_wanted", "service", NULL };
516 char *fname, *service = NULL;
517 int security_info_wanted;
519 struct security_descriptor *sd;
520 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
521 connection_struct *conn;
524 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|z", discard_const_p(char *, kwnames),
525 &fname, &security_info_wanted, &service)) {
526 TALLOC_FREE(tmp_ctx);
530 conn = get_conn(tmp_ctx, service);
532 TALLOC_FREE(tmp_ctx);
536 status = get_nt_acl_conn(tmp_ctx, fname, conn, security_info_wanted, &sd);
537 PyErr_NTSTATUS_IS_ERR_RAISE(status);
539 py_sd = py_return_ndr_struct("samba.dcerpc.security", "descriptor", sd, sd);
541 TALLOC_FREE(tmp_ctx);
547 set the posix (or similar) ACL on a file
549 static PyObject *py_smbd_set_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
551 const char * const kwnames[] = { "fname", "acl_type", "acl", "service", NULL };
552 TALLOC_CTX *frame = talloc_stackframe();
554 char *fname, *service = NULL;
556 struct smb_acl_t *acl;
558 connection_struct *conn;
560 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|z",
561 discard_const_p(char *, kwnames),
562 &fname, &acl_type, &py_acl, &service)) {
567 if (!py_check_dcerpc_type(py_acl, "samba.dcerpc.smb_acl", "t")) {
572 conn = get_conn(frame, service);
578 acl = pytalloc_get_type(py_acl, struct smb_acl_t);
580 ret = set_sys_acl_conn(fname, acl_type, acl, conn);
584 return PyErr_SetFromErrno(PyExc_OSError);
592 Return the posix (or similar) ACL on a file
594 static PyObject *py_smbd_get_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
596 const char * const kwnames[] = { "fname", "acl_type", "service", NULL };
599 struct smb_acl_t *acl;
601 TALLOC_CTX *frame = talloc_stackframe();
602 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
603 connection_struct *conn;
604 char *service = NULL;
610 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|z",
611 discard_const_p(char *, kwnames),
612 &fname, &acl_type, &service)) {
614 TALLOC_FREE(tmp_ctx);
618 conn = get_conn(frame, service);
621 TALLOC_FREE(tmp_ctx);
625 acl = SMB_VFS_SYS_ACL_GET_FILE( conn, fname, acl_type, tmp_ctx);
628 TALLOC_FREE(tmp_ctx);
629 return PyErr_SetFromErrno(PyExc_OSError);
632 py_acl = py_return_ndr_struct("samba.dcerpc.smb_acl", "t", acl, acl);
635 TALLOC_FREE(tmp_ctx);
640 static PyMethodDef py_smbd_methods[] = {
642 (PyCFunction)py_smbd_have_posix_acls, METH_NOARGS,
645 (PyCFunction)py_smbd_set_simple_acl, METH_VARARGS|METH_KEYWORDS,
648 (PyCFunction)py_smbd_set_nt_acl, METH_VARARGS|METH_KEYWORDS,
651 (PyCFunction)py_smbd_get_nt_acl, METH_VARARGS|METH_KEYWORDS,
654 (PyCFunction)py_smbd_get_sys_acl, METH_VARARGS|METH_KEYWORDS,
657 (PyCFunction)py_smbd_set_sys_acl, METH_VARARGS|METH_KEYWORDS,
660 (PyCFunction)py_smbd_chown, METH_VARARGS|METH_KEYWORDS,
663 (PyCFunction)py_smbd_unlink, METH_VARARGS|METH_KEYWORDS,
673 m = Py_InitModule3("smbd", py_smbd_methods,
674 "Python bindings for the smbd file server.");