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,
129 lp_posix_pathnames());
130 if (smb_fname == NULL) {
133 return NT_STATUS_NO_MEMORY;
136 fsp->fsp_name = smb_fname;
139 flags = O_RDONLY|O_DIRECTORY;
141 /* POSIX allows us to open a directory with O_RDONLY. */
145 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, O_RDWR, 00400);
146 if (fsp->fh->fd == -1 && errno == EISDIR) {
147 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, 00400);
149 if (fsp->fh->fd == -1) {
150 printf("open: error=%d (%s)\n", errno, strerror(errno));
153 return NT_STATUS_UNSUCCESSFUL;
156 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
158 /* If we have an fd, this stat should succeed. */
159 DEBUG(0,("Error doing fstat on open file %s "
161 smb_fname_str_dbg(smb_fname),
165 return map_nt_error_from_unix(errno);
168 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
169 fsp->vuid = UID_FIELD_INVALID;
171 fsp->can_lock = True;
172 fsp->can_read = True;
173 fsp->can_write = True;
174 fsp->print_file = NULL;
175 fsp->modified = False;
176 fsp->sent_oplock_break = NO_BREAK_SENT;
177 fsp->is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
179 status = SMB_VFS_FSET_NT_ACL( fsp, security_info_sent, sd);
180 if (!NT_STATUS_IS_OK(status)) {
181 DEBUG(0,("set_nt_acl_no_snum: fset_nt_acl returned %s.\n", nt_errstr(status)));
193 static NTSTATUS get_nt_acl_conn(TALLOC_CTX *mem_ctx,
195 connection_struct *conn,
196 uint32_t security_info_wanted,
197 struct security_descriptor **sd)
199 TALLOC_CTX *frame = talloc_stackframe();
201 struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
205 lp_posix_pathnames() ?
206 SMB_FILENAME_POSIX_PATH : 0);
208 if (smb_fname == NULL) {
210 return NT_STATUS_NO_MEMORY;
213 status = SMB_VFS_GET_NT_ACL(conn,
215 security_info_wanted,
218 if (!NT_STATUS_IS_OK(status)) {
219 DEBUG(0,("get_nt_acl_conn: get_nt_acl returned %s.\n", nt_errstr(status)));
228 static SMB_ACL_T make_simple_acl(gid_t gid, mode_t chmod_mode)
230 TALLOC_CTX *frame = talloc_stackframe();
232 mode_t mode = SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE;
234 mode_t mode_user = (chmod_mode & 0700) >> 6;
235 mode_t mode_group = (chmod_mode & 070) >> 3;
236 mode_t mode_other = chmod_mode & 07;
237 SMB_ACL_ENTRY_T entry;
238 SMB_ACL_T acl = sys_acl_init(frame);
244 if (sys_acl_create_entry(&acl, &entry) != 0) {
249 if (sys_acl_set_tag_type(entry, SMB_ACL_USER_OBJ) != 0) {
254 if (sys_acl_set_permset(entry, &mode_user) != 0) {
259 if (sys_acl_create_entry(&acl, &entry) != 0) {
264 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP_OBJ) != 0) {
269 if (sys_acl_set_permset(entry, &mode_group) != 0) {
274 if (sys_acl_create_entry(&acl, &entry) != 0) {
279 if (sys_acl_set_tag_type(entry, SMB_ACL_OTHER) != 0) {
284 if (sys_acl_set_permset(entry, &mode_other) != 0) {
290 if (sys_acl_create_entry(&acl, &entry) != 0) {
295 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP) != 0) {
300 if (sys_acl_set_qualifier(entry, &gid) != 0) {
305 if (sys_acl_set_permset(entry, &mode_group) != 0) {
311 if (sys_acl_create_entry(&acl, &entry) != 0) {
316 if (sys_acl_set_tag_type(entry, SMB_ACL_MASK) != 0) {
321 if (sys_acl_set_permset(entry, &mode) != 0) {
329 set a simple ACL on a file, as a test
331 static PyObject *py_smbd_set_simple_acl(PyObject *self, PyObject *args, PyObject *kwargs)
333 const char * const kwnames[] = { "fname", "mode", "gid", "service", NULL };
334 char *fname, *service = NULL;
339 connection_struct *conn;
341 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|iz",
342 discard_const_p(char *, kwnames),
343 &fname, &mode, &gid, &service))
346 acl = make_simple_acl(gid, mode);
348 frame = talloc_stackframe();
350 conn = get_conn(frame, service);
355 ret = set_sys_acl_conn(fname, SMB_ACL_TYPE_ACCESS, acl, conn);
361 return PyErr_SetFromErrno(PyExc_OSError);
372 static PyObject *py_smbd_chown(PyObject *self, PyObject *args, PyObject *kwargs)
374 const char * const kwnames[] = { "fname", "uid", "gid", "service", NULL };
375 connection_struct *conn;
378 char *fname, *service = NULL;
382 struct smb_filename *smb_fname = NULL;
384 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sii|z",
385 discard_const_p(char *, kwnames),
386 &fname, &uid, &gid, &service))
389 frame = talloc_stackframe();
391 conn = get_conn(frame, service);
396 /* we want total control over the permissions on created files,
397 so set our umask to 0 */
398 saved_umask = umask(0);
400 smb_fname = synthetic_smb_fname(talloc_tos(),
404 lp_posix_pathnames() ?
405 SMB_FILENAME_POSIX_PATH : 0);
406 if (smb_fname == NULL) {
410 return PyErr_SetFromErrno(PyExc_OSError);
413 ret = SMB_VFS_CHOWN(conn, smb_fname, uid, gid);
418 return PyErr_SetFromErrno(PyExc_OSError);
431 static PyObject *py_smbd_unlink(PyObject *self, PyObject *args, PyObject *kwargs)
433 const char * const kwnames[] = { "fname", "service", NULL };
434 connection_struct *conn;
436 struct smb_filename *smb_fname = NULL;
437 char *fname, *service = NULL;
440 frame = talloc_stackframe();
442 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|z",
443 discard_const_p(char *, kwnames),
449 conn = get_conn(frame, service);
455 smb_fname = synthetic_smb_fname_split(frame,
457 lp_posix_pathnames());
458 if (smb_fname == NULL) {
460 return PyErr_NoMemory();
463 ret = SMB_VFS_UNLINK(conn, smb_fname);
467 return PyErr_SetFromErrno(PyExc_OSError);
476 check if we have ACL support
478 static PyObject *py_smbd_have_posix_acls(PyObject *self)
480 #ifdef HAVE_POSIX_ACLS
481 return PyBool_FromLong(true);
483 return PyBool_FromLong(false);
488 set the NT ACL on a file
490 static PyObject *py_smbd_set_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
492 const char * const kwnames[] = { "fname", "security_info_sent", "sd", "service", NULL };
494 char *fname, *service = NULL;
495 int security_info_sent;
497 struct security_descriptor *sd;
498 connection_struct *conn;
501 frame = talloc_stackframe();
503 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
504 "siO|z", discard_const_p(char *, kwnames),
505 &fname, &security_info_sent, &py_sd, &service)) {
510 if (!py_check_dcerpc_type(py_sd, "samba.dcerpc.security", "descriptor")) {
515 conn = get_conn(frame, service);
521 sd = pytalloc_get_type(py_sd, struct security_descriptor);
523 status = set_nt_acl_conn(fname, security_info_sent, sd, conn);
525 PyErr_NTSTATUS_IS_ERR_RAISE(status);
531 Return the NT ACL on a file
533 static PyObject *py_smbd_get_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
535 const char * const kwnames[] = { "fname", "security_info_wanted", "service", NULL };
536 char *fname, *service = NULL;
537 int security_info_wanted;
539 struct security_descriptor *sd;
540 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
541 connection_struct *conn;
544 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|z", discard_const_p(char *, kwnames),
545 &fname, &security_info_wanted, &service)) {
546 TALLOC_FREE(tmp_ctx);
550 conn = get_conn(tmp_ctx, service);
552 TALLOC_FREE(tmp_ctx);
556 status = get_nt_acl_conn(tmp_ctx, fname, conn, security_info_wanted, &sd);
557 PyErr_NTSTATUS_IS_ERR_RAISE(status);
559 py_sd = py_return_ndr_struct("samba.dcerpc.security", "descriptor", sd, sd);
561 TALLOC_FREE(tmp_ctx);
567 set the posix (or similar) ACL on a file
569 static PyObject *py_smbd_set_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
571 const char * const kwnames[] = { "fname", "acl_type", "acl", "service", NULL };
572 TALLOC_CTX *frame = talloc_stackframe();
574 char *fname, *service = NULL;
576 struct smb_acl_t *acl;
578 connection_struct *conn;
580 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|z",
581 discard_const_p(char *, kwnames),
582 &fname, &acl_type, &py_acl, &service)) {
587 if (!py_check_dcerpc_type(py_acl, "samba.dcerpc.smb_acl", "t")) {
592 conn = get_conn(frame, service);
598 acl = pytalloc_get_type(py_acl, struct smb_acl_t);
600 ret = set_sys_acl_conn(fname, acl_type, acl, conn);
604 return PyErr_SetFromErrno(PyExc_OSError);
612 Return the posix (or similar) ACL on a file
614 static PyObject *py_smbd_get_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
616 const char * const kwnames[] = { "fname", "acl_type", "service", NULL };
619 struct smb_acl_t *acl;
621 TALLOC_CTX *frame = talloc_stackframe();
622 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
623 connection_struct *conn;
624 char *service = NULL;
630 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|z",
631 discard_const_p(char *, kwnames),
632 &fname, &acl_type, &service)) {
634 TALLOC_FREE(tmp_ctx);
638 conn = get_conn(frame, service);
641 TALLOC_FREE(tmp_ctx);
645 acl = SMB_VFS_SYS_ACL_GET_FILE( conn, fname, acl_type, tmp_ctx);
648 TALLOC_FREE(tmp_ctx);
649 return PyErr_SetFromErrno(PyExc_OSError);
652 py_acl = py_return_ndr_struct("samba.dcerpc.smb_acl", "t", acl, acl);
655 TALLOC_FREE(tmp_ctx);
660 static PyMethodDef py_smbd_methods[] = {
662 (PyCFunction)py_smbd_have_posix_acls, METH_NOARGS,
665 (PyCFunction)py_smbd_set_simple_acl, METH_VARARGS|METH_KEYWORDS,
668 (PyCFunction)py_smbd_set_nt_acl, METH_VARARGS|METH_KEYWORDS,
671 (PyCFunction)py_smbd_get_nt_acl, METH_VARARGS|METH_KEYWORDS,
674 (PyCFunction)py_smbd_get_sys_acl, METH_VARARGS|METH_KEYWORDS,
677 (PyCFunction)py_smbd_set_sys_acl, METH_VARARGS|METH_KEYWORDS,
680 (PyCFunction)py_smbd_chown, METH_VARARGS|METH_KEYWORDS,
683 (PyCFunction)py_smbd_unlink, METH_VARARGS|METH_KEYWORDS,
693 m = Py_InitModule3("smbd", py_smbd_methods,
694 "Python bindings for the smbd file server.");