pysmbd: Add hook for unlink() so python scripts can remove xattr.tdb entries
[Samba.git] / source3 / smbd / pysmbd.c
blob832eaeb9d6ae5138ecac3d9845fef584623aa578
1 /*
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/>.
26 #include "includes.h"
27 #include "smbd/smbd.h"
28 #include <Python.h>
29 #include "libcli/util/pyerrors.h"
30 #include "librpc/rpc/pyrpc_util.h"
31 #include <pytalloc.h>
32 #include "system/filesys.h"
34 extern const struct generic_mapping file_generic_mapping;
36 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_ACLS
39 static NTSTATUS set_sys_acl_no_snum(const char *fname,
40 SMB_ACL_TYPE_T acltype,
41 SMB_ACL_T theacl)
43 connection_struct *conn;
44 NTSTATUS status = NT_STATUS_OK;
45 int ret;
47 conn = talloc_zero(NULL, connection_struct);
48 if (conn == NULL) {
49 DEBUG(0, ("talloc failed\n"));
50 return NT_STATUS_NO_MEMORY;
53 if (!(conn->params = talloc(conn, struct share_params))) {
54 DEBUG(0,("get_nt_acl_no_snum: talloc() failed!\n"));
55 TALLOC_FREE(conn);
56 return NT_STATUS_NO_MEMORY;
59 conn->params->service = -1;
61 set_conn_connectpath(conn, "/");
63 smbd_vfs_init(conn);
65 ret = SMB_VFS_SYS_ACL_SET_FILE( conn, fname, acltype, theacl);
66 if (ret != 0) {
67 status = map_nt_error_from_unix_common(ret);
68 DEBUG(0,("set_nt_acl_no_snum: fset_nt_acl returned zero.\n"));
71 conn_free(conn);
73 return status;
76 static NTSTATUS set_nt_acl_no_snum(const char *fname,
77 uint32 security_info_sent, const struct security_descriptor *sd)
79 TALLOC_CTX *frame = talloc_stackframe();
80 connection_struct *conn;
81 NTSTATUS status = NT_STATUS_OK;
82 files_struct *fsp;
83 struct smb_filename *smb_fname = NULL;
84 int flags;
86 conn = talloc_zero(frame, connection_struct);
87 if (conn == NULL) {
88 DEBUG(0, ("talloc failed\n"));
89 return NT_STATUS_NO_MEMORY;
92 if (!(conn->params = talloc(conn, struct share_params))) {
93 DEBUG(0,("set_nt_acl_no_snum: talloc() failed!\n"));
94 TALLOC_FREE(frame);
95 return NT_STATUS_NO_MEMORY;
98 conn->params->service = -1;
100 set_conn_connectpath(conn, "/");
102 smbd_vfs_init(conn);
104 fsp = talloc_zero(frame, struct files_struct);
105 if (fsp == NULL) {
106 TALLOC_FREE(frame);
107 return NT_STATUS_NO_MEMORY;
109 fsp->fh = talloc(fsp, struct fd_handle);
110 if (fsp->fh == NULL) {
111 TALLOC_FREE(frame);
112 return NT_STATUS_NO_MEMORY;
114 fsp->conn = conn;
116 status = create_synthetic_smb_fname_split(fsp, fname, NULL,
117 &smb_fname);
118 if (!NT_STATUS_IS_OK(status)) {
119 TALLOC_FREE(frame);
120 return status;
123 fsp->fsp_name = smb_fname;
125 #ifdef O_DIRECTORY
126 flags = O_RDONLY|O_DIRECTORY;
127 #else
128 /* POSIX allows us to open a directory with O_RDONLY. */
129 flags = O_RDONLY;
130 #endif
132 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, O_RDWR, 00400);
133 if (fsp->fh->fd == -1 && errno == EISDIR) {
134 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, 00400);
136 if (fsp->fh->fd == -1) {
137 printf("open: error=%d (%s)\n", errno, strerror(errno));
138 TALLOC_FREE(frame);
139 return NT_STATUS_UNSUCCESSFUL;
142 status = SMB_VFS_FSET_NT_ACL( fsp, security_info_sent, sd);
143 if (!NT_STATUS_IS_OK(status)) {
144 DEBUG(0,("set_nt_acl_no_snum: fset_nt_acl returned %s.\n", nt_errstr(status)));
147 conn_free(conn);
148 TALLOC_FREE(frame);
150 return status;
154 static SMB_ACL_T make_simple_acl(gid_t gid, mode_t chmod_mode)
156 TALLOC_CTX *frame = talloc_stackframe();
158 mode_t mode = SMB_ACL_READ|SMB_ACL_WRITE;
160 mode_t mode_user = (chmod_mode & 0700) >> 6;
161 mode_t mode_group = (chmod_mode & 070) >> 3;
162 mode_t mode_other = chmod_mode & 07;
163 SMB_ACL_ENTRY_T entry;
164 SMB_ACL_T acl = sys_acl_init(frame);
166 if (!acl) {
167 return NULL;
170 if (sys_acl_create_entry(&acl, &entry) != 0) {
171 TALLOC_FREE(frame);
172 return NULL;
175 if (sys_acl_set_tag_type(entry, SMB_ACL_USER_OBJ) != 0) {
176 TALLOC_FREE(frame);
177 return NULL;
180 if (sys_acl_set_permset(entry, &mode_user) != 0) {
181 TALLOC_FREE(frame);
182 return NULL;
185 if (sys_acl_create_entry(&acl, &entry) != 0) {
186 TALLOC_FREE(frame);
187 return NULL;
190 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP_OBJ) != 0) {
191 TALLOC_FREE(frame);
192 return NULL;
195 if (sys_acl_set_permset(entry, &mode_group) != 0) {
196 TALLOC_FREE(frame);
197 return NULL;
200 if (sys_acl_create_entry(&acl, &entry) != 0) {
201 TALLOC_FREE(frame);
202 return NULL;
205 if (sys_acl_set_tag_type(entry, SMB_ACL_OTHER) != 0) {
206 TALLOC_FREE(frame);
207 return NULL;
210 if (sys_acl_set_permset(entry, &mode_other) != 0) {
211 TALLOC_FREE(frame);
212 return NULL;
215 if (gid != -1) {
216 if (sys_acl_create_entry(&acl, &entry) != 0) {
217 TALLOC_FREE(frame);
218 return NULL;
221 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP) != 0) {
222 TALLOC_FREE(frame);
223 return NULL;
226 if (sys_acl_set_qualifier(entry, &gid) != 0) {
227 TALLOC_FREE(frame);
228 return NULL;
231 if (sys_acl_set_permset(entry, &mode_group) != 0) {
232 TALLOC_FREE(frame);
233 return NULL;
237 if (sys_acl_create_entry(&acl, &entry) != 0) {
238 TALLOC_FREE(frame);
239 return NULL;
242 if (sys_acl_set_tag_type(entry, SMB_ACL_MASK) != 0) {
243 TALLOC_FREE(frame);
244 return NULL;
247 if (sys_acl_set_permset(entry, &mode) != 0) {
248 TALLOC_FREE(frame);
249 return NULL;
251 return acl;
255 set a simple ACL on a file, as a test
257 static PyObject *py_smbd_set_simple_acl(PyObject *self, PyObject *args)
259 NTSTATUS status;
260 char *fname;
261 int mode, gid = -1;
262 SMB_ACL_T acl;
263 TALLOC_CTX *frame;
265 if (!PyArg_ParseTuple(args, "si|i", &fname, &mode, &gid))
266 return NULL;
268 acl = make_simple_acl(gid, mode);
270 frame = talloc_stackframe();
272 status = set_sys_acl_no_snum(fname, SMB_ACL_TYPE_ACCESS, acl);
273 TALLOC_FREE(acl);
275 TALLOC_FREE(frame);
277 PyErr_NTSTATUS_IS_ERR_RAISE(status);
279 Py_RETURN_NONE;
283 chown a file
285 static PyObject *py_smbd_chown(PyObject *self, PyObject *args)
287 connection_struct *conn;
288 NTSTATUS status = NT_STATUS_OK;
289 int ret;
291 char *fname;
292 int uid, gid;
293 TALLOC_CTX *frame;
295 if (!PyArg_ParseTuple(args, "sii", &fname, &uid, &gid))
296 return NULL;
298 frame = talloc_stackframe();
300 conn = talloc_zero(frame, connection_struct);
301 if (conn == NULL) {
302 PyErr_NoMemory();
303 return NULL;
306 if (!(conn->params = talloc(conn, struct share_params))) {
307 PyErr_NoMemory();
308 return NULL;
311 conn->params->service = -1;
313 set_conn_connectpath(conn, "/");
315 smbd_vfs_init(conn);
317 ret = SMB_VFS_CHOWN( conn, fname, uid, gid);
318 if (ret != 0) {
319 status = map_nt_error_from_unix_common(errno);
320 DEBUG(0,("chown returned failure: %s\n", strerror(errno)));
323 conn_free(conn);
325 TALLOC_FREE(frame);
327 PyErr_NTSTATUS_IS_ERR_RAISE(status);
329 Py_RETURN_NONE;
333 chown a file
335 static PyObject *py_smbd_unlink(PyObject *self, PyObject *args)
337 connection_struct *conn;
338 NTSTATUS status = NT_STATUS_OK;
339 int ret;
340 struct smb_filename *smb_fname = NULL;
341 char *fname;
342 int uid, gid;
343 TALLOC_CTX *frame;
344 mode_t saved_umask;
346 if (!PyArg_ParseTuple(args, "s", &fname))
347 return NULL;
349 frame = talloc_stackframe();
351 conn = talloc_zero(frame, connection_struct);
352 if (conn == NULL) {
353 PyErr_NoMemory();
354 return NULL;
357 if (!(conn->params = talloc(conn, struct share_params))) {
358 PyErr_NoMemory();
359 return NULL;
362 /* we want total control over the permissions on created files,
363 so set our umask to 0 */
364 saved_umask = umask(0);
366 conn->params->service = -1;
368 set_conn_connectpath(conn, "/");
370 smbd_vfs_init(conn);
372 status = create_synthetic_smb_fname_split(frame, fname, NULL,
373 &smb_fname);
374 if (!NT_STATUS_IS_OK(status)) {
375 TALLOC_FREE(frame);
376 umask(saved_umask);
377 PyErr_NTSTATUS_IS_ERR_RAISE(status);
380 ret = SMB_VFS_UNLINK(conn, smb_fname);
381 if (ret != 0) {
382 status = map_nt_error_from_unix_common(errno);
383 DEBUG(0,("unlink returned failure: %s\n", strerror(errno)));
386 umask(saved_umask);
388 conn_free(conn);
390 TALLOC_FREE(frame);
392 PyErr_NTSTATUS_IS_ERR_RAISE(status);
394 Py_RETURN_NONE;
398 check if we have ACL support
400 static PyObject *py_smbd_have_posix_acls(PyObject *self, PyObject *args)
402 #ifdef HAVE_POSIX_ACLS
403 return PyBool_FromLong(true);
404 #else
405 return PyBool_FromLong(false);
406 #endif
410 set the NT ACL on a file
412 static PyObject *py_smbd_set_nt_acl(PyObject *self, PyObject *args)
414 NTSTATUS status;
415 char *fname;
416 int security_info_sent;
417 PyObject *py_sd;
418 struct security_descriptor *sd;
420 if (!PyArg_ParseTuple(args, "siO", &fname, &security_info_sent, &py_sd))
421 return NULL;
423 if (!py_check_dcerpc_type(py_sd, "samba.dcerpc.security", "descriptor")) {
424 return NULL;
427 sd = pytalloc_get_type(py_sd, struct security_descriptor);
429 status = set_nt_acl_no_snum(fname, security_info_sent, sd);
430 PyErr_NTSTATUS_IS_ERR_RAISE(status);
432 Py_RETURN_NONE;
436 Return the NT ACL on a file
438 static PyObject *py_smbd_get_nt_acl(PyObject *self, PyObject *args)
440 char *fname;
441 int security_info_wanted;
442 PyObject *py_sd;
443 struct security_descriptor *sd;
444 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
446 if (!PyArg_ParseTuple(args, "si", &fname, &security_info_wanted))
447 return NULL;
449 sd = get_nt_acl_no_snum(tmp_ctx, fname, security_info_wanted);
451 py_sd = py_return_ndr_struct("samba.dcerpc.security", "descriptor", sd, sd);
453 talloc_free(tmp_ctx);
455 return py_sd;
459 set the posix (or similar) ACL on a file
461 static PyObject *py_smbd_set_sys_acl(PyObject *self, PyObject *args)
463 NTSTATUS status;
464 char *fname;
465 PyObject *py_acl;
466 struct smb_acl_t *acl;
467 int acl_type;
469 if (!PyArg_ParseTuple(args, "siO", &fname, &acl_type, &py_acl))
470 return NULL;
472 if (!py_check_dcerpc_type(py_acl, "samba.dcerpc.smb_acl", "t")) {
473 return NULL;
476 acl = pytalloc_get_type(py_acl, struct smb_acl_t);
478 status = set_sys_acl_no_snum(fname, acl_type, acl);
479 PyErr_NTSTATUS_IS_ERR_RAISE(status);
481 Py_RETURN_NONE;
485 Return the posix (or similar) ACL on a file
487 static PyObject *py_smbd_get_sys_acl(PyObject *self, PyObject *args)
489 char *fname;
490 PyObject *py_acl;
491 struct smb_acl_t *acl;
492 int acl_type;
493 TALLOC_CTX *frame = talloc_stackframe();
494 connection_struct *conn;
495 NTSTATUS status = NT_STATUS_OK;
497 if (!PyArg_ParseTuple(args, "si", &fname, &acl_type)) {
498 TALLOC_FREE(frame);
499 return NULL;
502 conn = talloc_zero(frame, connection_struct);
503 if (conn == NULL) {
504 DEBUG(0, ("talloc failed\n"));
505 PyErr_NoMemory();
506 TALLOC_FREE(frame);
507 return NULL;
510 if (!(conn->params = talloc(conn, struct share_params))) {
511 DEBUG(0,("get_nt_acl_no_snum: talloc() failed!\n"));
512 PyErr_NoMemory();
513 TALLOC_FREE(frame);
514 return NULL;
517 conn->params->service = -1;
519 set_conn_connectpath(conn, "/");
521 smbd_vfs_init(conn);
523 acl = SMB_VFS_SYS_ACL_GET_FILE( conn, fname, acl_type, frame);
524 if (!acl) {
525 TALLOC_FREE(frame);
526 status = map_nt_error_from_unix_common(errno);
527 DEBUG(0,("sys_acl_get_file returned NULL: %s\n", strerror(errno)));
528 PyErr_NTSTATUS_IS_ERR_RAISE(status);
531 conn_free(conn);
533 py_acl = py_return_ndr_struct("samba.dcerpc.smb_acl", "t", acl, acl);
535 TALLOC_FREE(frame);
537 return py_acl;
540 static PyMethodDef py_smbd_methods[] = {
541 { "have_posix_acls",
542 (PyCFunction)py_smbd_have_posix_acls, METH_VARARGS,
543 NULL },
544 { "set_simple_acl",
545 (PyCFunction)py_smbd_set_simple_acl, METH_VARARGS,
546 NULL },
547 { "set_nt_acl",
548 (PyCFunction)py_smbd_set_nt_acl, METH_VARARGS,
549 NULL },
550 { "get_nt_acl",
551 (PyCFunction)py_smbd_get_nt_acl, METH_VARARGS,
552 NULL },
553 { "get_sys_acl",
554 (PyCFunction)py_smbd_get_sys_acl, METH_VARARGS,
555 NULL },
556 { "set_sys_acl",
557 (PyCFunction)py_smbd_set_sys_acl, METH_VARARGS,
558 NULL },
559 { "chown",
560 (PyCFunction)py_smbd_chown, METH_VARARGS,
561 NULL },
562 { "unlink",
563 (PyCFunction)py_smbd_unlink, METH_VARARGS,
564 NULL },
565 { NULL }
568 void initsmbd(void);
569 void initsmbd(void)
571 PyObject *m;
573 m = Py_InitModule3("smbd", py_smbd_methods,
574 "Python bindings for the smbd file server.");
575 if (m == NULL)
576 return;