wafsamba: improve wording in a comment
[Samba.git] / source3 / smbd / pysmbd.c
blob78c2bcb17505195845adcd50f0803c393278fa08
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;
46 mode_t saved_umask;
48 conn = talloc_zero(NULL, connection_struct);
49 if (conn == NULL) {
50 DEBUG(0, ("talloc failed\n"));
51 return NT_STATUS_NO_MEMORY;
54 if (!(conn->params = talloc(conn, struct share_params))) {
55 DEBUG(0,("get_nt_acl_no_snum: talloc() failed!\n"));
56 TALLOC_FREE(conn);
57 return NT_STATUS_NO_MEMORY;
60 /* we want total control over the permissions on created files,
61 so set our umask to 0 */
62 saved_umask = umask(0);
64 conn->params->service = -1;
66 set_conn_connectpath(conn, "/");
68 smbd_vfs_init(conn);
70 ret = SMB_VFS_SYS_ACL_SET_FILE( conn, fname, acltype, theacl);
71 if (ret != 0) {
72 status = map_nt_error_from_unix_common(ret);
73 DEBUG(0,("set_nt_acl_no_snum: fset_nt_acl returned zero.\n"));
76 umask(saved_umask);
78 conn_free(conn);
80 return status;
83 static NTSTATUS set_nt_acl_no_snum(const char *fname,
84 uint32 security_info_sent, const struct security_descriptor *sd)
86 TALLOC_CTX *frame = talloc_stackframe();
87 connection_struct *conn;
88 NTSTATUS status = NT_STATUS_OK;
89 files_struct *fsp;
90 struct smb_filename *smb_fname = NULL;
91 int flags, ret;
92 mode_t saved_umask;
94 if (!posix_locking_init(false)) {
95 TALLOC_FREE(frame);
96 return NT_STATUS_NO_MEMORY;
99 conn = talloc_zero(frame, connection_struct);
100 if (conn == NULL) {
101 TALLOC_FREE(frame);
102 DEBUG(0, ("talloc failed\n"));
103 return NT_STATUS_NO_MEMORY;
106 if (!(conn->params = talloc(conn, struct share_params))) {
107 DEBUG(0,("set_nt_acl_no_snum: talloc() failed!\n"));
108 TALLOC_FREE(frame);
109 return NT_STATUS_NO_MEMORY;
112 fsp = talloc_zero(frame, struct files_struct);
113 if (fsp == NULL) {
114 TALLOC_FREE(frame);
115 return NT_STATUS_NO_MEMORY;
117 fsp->fh = talloc(fsp, struct fd_handle);
118 if (fsp->fh == NULL) {
119 TALLOC_FREE(frame);
120 return NT_STATUS_NO_MEMORY;
122 fsp->conn = conn;
124 /* we want total control over the permissions on created files,
125 so set our umask to 0 */
126 saved_umask = umask(0);
128 conn->params->service = -1;
130 set_conn_connectpath(conn, "/");
132 smbd_vfs_init(conn);
134 status = create_synthetic_smb_fname_split(fsp, fname, NULL,
135 &smb_fname);
136 if (!NT_STATUS_IS_OK(status)) {
137 TALLOC_FREE(frame);
138 umask(saved_umask);
139 return status;
142 fsp->fsp_name = smb_fname;
144 #ifdef O_DIRECTORY
145 flags = O_RDONLY|O_DIRECTORY;
146 #else
147 /* POSIX allows us to open a directory with O_RDONLY. */
148 flags = O_RDONLY;
149 #endif
151 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, O_RDWR, 00400);
152 if (fsp->fh->fd == -1 && errno == EISDIR) {
153 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, 00400);
155 if (fsp->fh->fd == -1) {
156 printf("open: error=%d (%s)\n", errno, strerror(errno));
157 TALLOC_FREE(frame);
158 umask(saved_umask);
159 return NT_STATUS_UNSUCCESSFUL;
162 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
163 if (ret == -1) {
164 /* If we have an fd, this stat should succeed. */
165 DEBUG(0,("Error doing fstat on open file %s "
166 "(%s)\n",
167 smb_fname_str_dbg(smb_fname),
168 strerror(errno) ));
169 TALLOC_FREE(frame);
170 umask(saved_umask);
171 return map_nt_error_from_unix(errno);
174 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
175 fsp->vuid = UID_FIELD_INVALID;
176 fsp->file_pid = 0;
177 fsp->can_lock = True;
178 fsp->can_read = True;
179 fsp->can_write = True;
180 fsp->print_file = NULL;
181 fsp->modified = False;
182 fsp->sent_oplock_break = NO_BREAK_SENT;
183 fsp->is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
185 status = SMB_VFS_FSET_NT_ACL( fsp, security_info_sent, sd);
186 if (!NT_STATUS_IS_OK(status)) {
187 DEBUG(0,("set_nt_acl_no_snum: fset_nt_acl returned %s.\n", nt_errstr(status)));
190 SMB_VFS_CLOSE(fsp);
192 conn_free(conn);
193 TALLOC_FREE(frame);
195 umask(saved_umask);
196 return status;
200 static SMB_ACL_T make_simple_acl(gid_t gid, mode_t chmod_mode)
202 TALLOC_CTX *frame = talloc_stackframe();
204 mode_t mode = SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE;
206 mode_t mode_user = (chmod_mode & 0700) >> 6;
207 mode_t mode_group = (chmod_mode & 070) >> 3;
208 mode_t mode_other = chmod_mode & 07;
209 SMB_ACL_ENTRY_T entry;
210 SMB_ACL_T acl = sys_acl_init(frame);
212 if (!acl) {
213 return NULL;
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_USER_OBJ) != 0) {
222 TALLOC_FREE(frame);
223 return NULL;
226 if (sys_acl_set_permset(entry, &mode_user) != 0) {
227 TALLOC_FREE(frame);
228 return NULL;
231 if (sys_acl_create_entry(&acl, &entry) != 0) {
232 TALLOC_FREE(frame);
233 return NULL;
236 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP_OBJ) != 0) {
237 TALLOC_FREE(frame);
238 return NULL;
241 if (sys_acl_set_permset(entry, &mode_group) != 0) {
242 TALLOC_FREE(frame);
243 return NULL;
246 if (sys_acl_create_entry(&acl, &entry) != 0) {
247 TALLOC_FREE(frame);
248 return NULL;
251 if (sys_acl_set_tag_type(entry, SMB_ACL_OTHER) != 0) {
252 TALLOC_FREE(frame);
253 return NULL;
256 if (sys_acl_set_permset(entry, &mode_other) != 0) {
257 TALLOC_FREE(frame);
258 return NULL;
261 if (gid != -1) {
262 if (sys_acl_create_entry(&acl, &entry) != 0) {
263 TALLOC_FREE(frame);
264 return NULL;
267 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP) != 0) {
268 TALLOC_FREE(frame);
269 return NULL;
272 if (sys_acl_set_qualifier(entry, &gid) != 0) {
273 TALLOC_FREE(frame);
274 return NULL;
277 if (sys_acl_set_permset(entry, &mode_group) != 0) {
278 TALLOC_FREE(frame);
279 return NULL;
283 if (sys_acl_create_entry(&acl, &entry) != 0) {
284 TALLOC_FREE(frame);
285 return NULL;
288 if (sys_acl_set_tag_type(entry, SMB_ACL_MASK) != 0) {
289 TALLOC_FREE(frame);
290 return NULL;
293 if (sys_acl_set_permset(entry, &mode) != 0) {
294 TALLOC_FREE(frame);
295 return NULL;
297 return acl;
301 set a simple ACL on a file, as a test
303 static PyObject *py_smbd_set_simple_acl(PyObject *self, PyObject *args)
305 NTSTATUS status;
306 char *fname;
307 int mode, gid = -1;
308 SMB_ACL_T acl;
309 TALLOC_CTX *frame;
311 if (!PyArg_ParseTuple(args, "si|i", &fname, &mode, &gid))
312 return NULL;
314 acl = make_simple_acl(gid, mode);
316 frame = talloc_stackframe();
318 status = set_sys_acl_no_snum(fname, SMB_ACL_TYPE_ACCESS, acl);
319 TALLOC_FREE(acl);
321 TALLOC_FREE(frame);
323 PyErr_NTSTATUS_IS_ERR_RAISE(status);
325 Py_RETURN_NONE;
329 chown a file
331 static PyObject *py_smbd_chown(PyObject *self, PyObject *args)
333 connection_struct *conn;
334 NTSTATUS status = NT_STATUS_OK;
335 int ret;
337 char *fname;
338 int uid, gid;
339 TALLOC_CTX *frame;
340 mode_t saved_umask;
342 if (!PyArg_ParseTuple(args, "sii", &fname, &uid, &gid))
343 return NULL;
345 frame = talloc_stackframe();
347 conn = talloc_zero(frame, connection_struct);
348 if (conn == NULL) {
349 PyErr_NoMemory();
350 return NULL;
353 if (!(conn->params = talloc(conn, struct share_params))) {
354 PyErr_NoMemory();
355 return NULL;
358 /* we want total control over the permissions on created files,
359 so set our umask to 0 */
360 saved_umask = umask(0);
362 conn->params->service = -1;
364 set_conn_connectpath(conn, "/");
366 smbd_vfs_init(conn);
368 ret = SMB_VFS_CHOWN( conn, fname, uid, gid);
369 if (ret != 0) {
370 status = map_nt_error_from_unix_common(errno);
371 DEBUG(0,("chown returned failure: %s\n", strerror(errno)));
374 umask(saved_umask);
376 conn_free(conn);
378 TALLOC_FREE(frame);
380 PyErr_NTSTATUS_IS_ERR_RAISE(status);
382 Py_RETURN_NONE;
386 chown a file
388 static PyObject *py_smbd_unlink(PyObject *self, PyObject *args)
390 connection_struct *conn;
391 NTSTATUS status = NT_STATUS_OK;
392 int ret;
393 struct smb_filename *smb_fname = NULL;
394 char *fname;
395 int uid, gid;
396 TALLOC_CTX *frame;
397 mode_t saved_umask;
399 if (!PyArg_ParseTuple(args, "s", &fname))
400 return NULL;
402 frame = talloc_stackframe();
404 conn = talloc_zero(frame, connection_struct);
405 if (conn == NULL) {
406 PyErr_NoMemory();
407 return NULL;
410 if (!(conn->params = talloc(conn, struct share_params))) {
411 PyErr_NoMemory();
412 return NULL;
415 /* we want total control over the permissions on created files,
416 so set our umask to 0 */
417 saved_umask = umask(0);
419 conn->params->service = -1;
421 set_conn_connectpath(conn, "/");
423 smbd_vfs_init(conn);
425 status = create_synthetic_smb_fname_split(frame, fname, NULL,
426 &smb_fname);
427 if (!NT_STATUS_IS_OK(status)) {
428 TALLOC_FREE(frame);
429 umask(saved_umask);
430 PyErr_NTSTATUS_IS_ERR_RAISE(status);
433 ret = SMB_VFS_UNLINK(conn, smb_fname);
434 if (ret != 0) {
435 status = map_nt_error_from_unix_common(errno);
436 DEBUG(0,("unlink returned failure: %s\n", strerror(errno)));
439 umask(saved_umask);
441 conn_free(conn);
443 TALLOC_FREE(frame);
445 PyErr_NTSTATUS_IS_ERR_RAISE(status);
447 Py_RETURN_NONE;
451 check if we have ACL support
453 static PyObject *py_smbd_have_posix_acls(PyObject *self, PyObject *args)
455 #ifdef HAVE_POSIX_ACLS
456 return PyBool_FromLong(true);
457 #else
458 return PyBool_FromLong(false);
459 #endif
463 set the NT ACL on a file
465 static PyObject *py_smbd_set_nt_acl(PyObject *self, PyObject *args)
467 NTSTATUS status;
468 char *fname;
469 int security_info_sent;
470 PyObject *py_sd;
471 struct security_descriptor *sd;
473 if (!PyArg_ParseTuple(args, "siO", &fname, &security_info_sent, &py_sd))
474 return NULL;
476 if (!py_check_dcerpc_type(py_sd, "samba.dcerpc.security", "descriptor")) {
477 return NULL;
480 sd = pytalloc_get_type(py_sd, struct security_descriptor);
482 status = set_nt_acl_no_snum(fname, security_info_sent, sd);
483 PyErr_NTSTATUS_IS_ERR_RAISE(status);
485 Py_RETURN_NONE;
489 Return the NT ACL on a file
491 static PyObject *py_smbd_get_nt_acl(PyObject *self, PyObject *args)
493 char *fname;
494 int security_info_wanted;
495 PyObject *py_sd;
496 struct security_descriptor *sd;
497 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
499 if (!PyArg_ParseTuple(args, "si", &fname, &security_info_wanted))
500 return NULL;
502 sd = get_nt_acl_no_snum(tmp_ctx, fname, security_info_wanted);
504 py_sd = py_return_ndr_struct("samba.dcerpc.security", "descriptor", sd, sd);
506 talloc_free(tmp_ctx);
508 return py_sd;
512 set the posix (or similar) ACL on a file
514 static PyObject *py_smbd_set_sys_acl(PyObject *self, PyObject *args)
516 NTSTATUS status;
517 char *fname;
518 PyObject *py_acl;
519 struct smb_acl_t *acl;
520 int acl_type;
522 if (!PyArg_ParseTuple(args, "siO", &fname, &acl_type, &py_acl))
523 return NULL;
525 if (!py_check_dcerpc_type(py_acl, "samba.dcerpc.smb_acl", "t")) {
526 return NULL;
529 acl = pytalloc_get_type(py_acl, struct smb_acl_t);
531 status = set_sys_acl_no_snum(fname, acl_type, acl);
532 PyErr_NTSTATUS_IS_ERR_RAISE(status);
534 Py_RETURN_NONE;
538 Return the posix (or similar) ACL on a file
540 static PyObject *py_smbd_get_sys_acl(PyObject *self, PyObject *args)
542 char *fname;
543 PyObject *py_acl;
544 struct smb_acl_t *acl;
545 int acl_type;
546 TALLOC_CTX *frame = talloc_stackframe();
547 connection_struct *conn;
548 NTSTATUS status = NT_STATUS_OK;
550 if (!PyArg_ParseTuple(args, "si", &fname, &acl_type)) {
551 TALLOC_FREE(frame);
552 return NULL;
555 conn = talloc_zero(frame, connection_struct);
556 if (conn == NULL) {
557 DEBUG(0, ("talloc failed\n"));
558 PyErr_NoMemory();
559 TALLOC_FREE(frame);
560 return NULL;
563 if (!(conn->params = talloc(conn, struct share_params))) {
564 DEBUG(0,("get_nt_acl_no_snum: talloc() failed!\n"));
565 PyErr_NoMemory();
566 TALLOC_FREE(frame);
567 return NULL;
570 conn->params->service = -1;
572 set_conn_connectpath(conn, "/");
574 smbd_vfs_init(conn);
576 acl = SMB_VFS_SYS_ACL_GET_FILE( conn, fname, acl_type, frame);
577 if (!acl) {
578 TALLOC_FREE(frame);
579 status = map_nt_error_from_unix_common(errno);
580 DEBUG(0,("sys_acl_get_file returned NULL: %s\n", strerror(errno)));
581 PyErr_NTSTATUS_IS_ERR_RAISE(status);
584 conn_free(conn);
586 py_acl = py_return_ndr_struct("samba.dcerpc.smb_acl", "t", acl, acl);
588 TALLOC_FREE(frame);
590 return py_acl;
593 static PyMethodDef py_smbd_methods[] = {
594 { "have_posix_acls",
595 (PyCFunction)py_smbd_have_posix_acls, METH_VARARGS,
596 NULL },
597 { "set_simple_acl",
598 (PyCFunction)py_smbd_set_simple_acl, METH_VARARGS,
599 NULL },
600 { "set_nt_acl",
601 (PyCFunction)py_smbd_set_nt_acl, METH_VARARGS,
602 NULL },
603 { "get_nt_acl",
604 (PyCFunction)py_smbd_get_nt_acl, METH_VARARGS,
605 NULL },
606 { "get_sys_acl",
607 (PyCFunction)py_smbd_get_sys_acl, METH_VARARGS,
608 NULL },
609 { "set_sys_acl",
610 (PyCFunction)py_smbd_set_sys_acl, METH_VARARGS,
611 NULL },
612 { "chown",
613 (PyCFunction)py_smbd_chown, METH_VARARGS,
614 NULL },
615 { "unlink",
616 (PyCFunction)py_smbd_unlink, METH_VARARGS,
617 NULL },
618 { NULL }
621 void initsmbd(void);
622 void initsmbd(void)
624 PyObject *m;
626 m = Py_InitModule3("smbd", py_smbd_methods,
627 "Python bindings for the smbd file server.");
628 if (m == NULL)
629 return;