winbind: Keep "force_reauth" in invalidate_cm_connection
[Samba.git] / source3 / smbd / pysmbd.c
blobbe30b866e2067e5cfa2183ffe97ed5514286fc15
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 <Python.h>
27 #include "includes.h"
28 #include "smbd/smbd.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 int conn_free_wrapper(connection_struct *conn)
41 conn_free(conn);
42 return 0;
45 static connection_struct *get_conn(TALLOC_CTX *mem_ctx, const char *service)
47 connection_struct *conn;
48 TALLOC_CTX *frame = talloc_stackframe();
49 int snum = -1;
50 NTSTATUS status;
52 if (!posix_locking_init(false)) {
53 PyErr_NoMemory();
54 TALLOC_FREE(frame);
55 return NULL;
58 if (service) {
59 snum = lp_servicenumber(service);
60 if (snum == -1) {
61 TALLOC_FREE(frame);
62 PyErr_SetString(PyExc_RuntimeError, "unknown service");
63 return NULL;
67 status = create_conn_struct(mem_ctx, NULL, NULL, &conn, snum, "/",
68 NULL);
69 PyErr_NTSTATUS_IS_ERR_RAISE(status);
71 TALLOC_FREE(frame);
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);
76 return conn;
79 static int set_sys_acl_conn(const char *fname,
80 SMB_ACL_TYPE_T acltype,
81 SMB_ACL_T theacl, connection_struct *conn)
83 int ret;
84 struct smb_filename *smb_fname = NULL;
85 mode_t saved_umask;
87 TALLOC_CTX *frame = talloc_stackframe();
89 /* we want total control over the permissions on created files,
90 so set our umask to 0 */
91 saved_umask = umask(0);
93 smb_fname = synthetic_smb_fname_split(frame,
94 fname,
95 lp_posix_pathnames());
96 if (smb_fname == NULL) {
97 TALLOC_FREE(frame);
98 umask(saved_umask);
99 return -1;
102 ret = SMB_VFS_SYS_ACL_SET_FILE( conn, smb_fname, acltype, theacl);
104 umask(saved_umask);
106 TALLOC_FREE(frame);
107 return ret;
110 static NTSTATUS set_nt_acl_conn(const char *fname,
111 uint32_t security_info_sent, const struct security_descriptor *sd,
112 connection_struct *conn)
114 TALLOC_CTX *frame = talloc_stackframe();
115 NTSTATUS status = NT_STATUS_OK;
116 files_struct *fsp;
117 struct smb_filename *smb_fname = NULL;
118 int flags, ret;
119 mode_t saved_umask;
121 fsp = talloc_zero(frame, struct files_struct);
122 if (fsp == NULL) {
123 TALLOC_FREE(frame);
124 return NT_STATUS_NO_MEMORY;
126 fsp->fh = talloc(fsp, struct fd_handle);
127 if (fsp->fh == NULL) {
128 TALLOC_FREE(frame);
129 return NT_STATUS_NO_MEMORY;
131 fsp->conn = conn;
133 /* we want total control over the permissions on created files,
134 so set our umask to 0 */
135 saved_umask = umask(0);
137 smb_fname = synthetic_smb_fname_split(fsp,
138 fname,
139 lp_posix_pathnames());
140 if (smb_fname == NULL) {
141 TALLOC_FREE(frame);
142 umask(saved_umask);
143 return NT_STATUS_NO_MEMORY;
146 fsp->fsp_name = smb_fname;
148 #ifdef O_DIRECTORY
149 flags = O_RDONLY|O_DIRECTORY;
150 #else
151 /* POSIX allows us to open a directory with O_RDONLY. */
152 flags = O_RDONLY;
153 #endif
155 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, O_RDWR, 00400);
156 if (fsp->fh->fd == -1 && errno == EISDIR) {
157 fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, 00400);
159 if (fsp->fh->fd == -1) {
160 printf("open: error=%d (%s)\n", errno, strerror(errno));
161 TALLOC_FREE(frame);
162 umask(saved_umask);
163 return NT_STATUS_UNSUCCESSFUL;
166 ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
167 if (ret == -1) {
168 /* If we have an fd, this stat should succeed. */
169 DEBUG(0,("Error doing fstat on open file %s "
170 "(%s)\n",
171 smb_fname_str_dbg(smb_fname),
172 strerror(errno) ));
173 TALLOC_FREE(frame);
174 umask(saved_umask);
175 return map_nt_error_from_unix(errno);
178 fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
179 fsp->vuid = UID_FIELD_INVALID;
180 fsp->file_pid = 0;
181 fsp->can_lock = True;
182 fsp->can_read = True;
183 fsp->can_write = True;
184 fsp->print_file = NULL;
185 fsp->modified = False;
186 fsp->sent_oplock_break = NO_BREAK_SENT;
187 fsp->is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
189 status = SMB_VFS_FSET_NT_ACL( fsp, security_info_sent, sd);
190 if (!NT_STATUS_IS_OK(status)) {
191 DEBUG(0,("set_nt_acl_no_snum: fset_nt_acl returned %s.\n", nt_errstr(status)));
194 SMB_VFS_CLOSE(fsp);
196 conn_free(conn);
197 TALLOC_FREE(frame);
199 umask(saved_umask);
200 return status;
203 static NTSTATUS get_nt_acl_conn(TALLOC_CTX *mem_ctx,
204 const char *fname,
205 connection_struct *conn,
206 uint32_t security_info_wanted,
207 struct security_descriptor **sd)
209 TALLOC_CTX *frame = talloc_stackframe();
210 NTSTATUS status;
211 struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
212 fname,
213 NULL,
214 NULL,
215 lp_posix_pathnames() ?
216 SMB_FILENAME_POSIX_PATH : 0);
218 if (smb_fname == NULL) {
219 TALLOC_FREE(frame);
220 return NT_STATUS_NO_MEMORY;
223 status = SMB_VFS_GET_NT_ACL(conn,
224 smb_fname,
225 security_info_wanted,
226 mem_ctx,
227 sd);
228 if (!NT_STATUS_IS_OK(status)) {
229 DEBUG(0,("get_nt_acl_conn: get_nt_acl returned %s.\n", nt_errstr(status)));
232 TALLOC_FREE(frame);
234 return status;
237 static int set_acl_entry_perms(SMB_ACL_ENTRY_T entry, mode_t perm_mask)
239 SMB_ACL_PERMSET_T perms = NULL;
241 if (sys_acl_get_permset(entry, &perms) != 0) {
242 return -1;
245 if (sys_acl_clear_perms(perms) != 0) {
246 return -1;
249 if ((perm_mask & SMB_ACL_READ) != 0 &&
250 sys_acl_add_perm(perms, SMB_ACL_READ) != 0) {
251 return -1;
254 if ((perm_mask & SMB_ACL_WRITE) != 0 &&
255 sys_acl_add_perm(perms, SMB_ACL_WRITE) != 0) {
256 return -1;
259 if ((perm_mask & SMB_ACL_EXECUTE) != 0 &&
260 sys_acl_add_perm(perms, SMB_ACL_EXECUTE) != 0) {
261 return -1;
264 if (sys_acl_set_permset(entry, perms) != 0) {
265 return -1;
268 return 0;
271 static SMB_ACL_T make_simple_acl(gid_t gid, mode_t chmod_mode)
273 TALLOC_CTX *frame = talloc_stackframe();
275 mode_t mode = SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE;
277 mode_t mode_user = (chmod_mode & 0700) >> 6;
278 mode_t mode_group = (chmod_mode & 070) >> 3;
279 mode_t mode_other = chmod_mode & 07;
280 SMB_ACL_ENTRY_T entry;
281 SMB_ACL_T acl = sys_acl_init(frame);
283 if (!acl) {
284 return NULL;
287 if (sys_acl_create_entry(&acl, &entry) != 0) {
288 TALLOC_FREE(frame);
289 return NULL;
292 if (sys_acl_set_tag_type(entry, SMB_ACL_USER_OBJ) != 0) {
293 TALLOC_FREE(frame);
294 return NULL;
297 if (set_acl_entry_perms(entry, mode_user) != 0) {
298 TALLOC_FREE(frame);
299 return NULL;
302 if (sys_acl_create_entry(&acl, &entry) != 0) {
303 TALLOC_FREE(frame);
304 return NULL;
307 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP_OBJ) != 0) {
308 TALLOC_FREE(frame);
309 return NULL;
312 if (set_acl_entry_perms(entry, mode_group) != 0) {
313 TALLOC_FREE(frame);
314 return NULL;
317 if (sys_acl_create_entry(&acl, &entry) != 0) {
318 TALLOC_FREE(frame);
319 return NULL;
322 if (sys_acl_set_tag_type(entry, SMB_ACL_OTHER) != 0) {
323 TALLOC_FREE(frame);
324 return NULL;
327 if (set_acl_entry_perms(entry, mode_other) != 0) {
328 TALLOC_FREE(frame);
329 return NULL;
332 if (gid != -1) {
333 if (sys_acl_create_entry(&acl, &entry) != 0) {
334 TALLOC_FREE(frame);
335 return NULL;
338 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP) != 0) {
339 TALLOC_FREE(frame);
340 return NULL;
343 if (sys_acl_set_qualifier(entry, &gid) != 0) {
344 TALLOC_FREE(frame);
345 return NULL;
348 if (set_acl_entry_perms(entry, mode_group) != 0) {
349 TALLOC_FREE(frame);
350 return NULL;
354 if (sys_acl_create_entry(&acl, &entry) != 0) {
355 TALLOC_FREE(frame);
356 return NULL;
359 if (sys_acl_set_tag_type(entry, SMB_ACL_MASK) != 0) {
360 TALLOC_FREE(frame);
361 return NULL;
364 if (set_acl_entry_perms(entry, mode) != 0) {
365 TALLOC_FREE(frame);
366 return NULL;
368 return acl;
372 set a simple ACL on a file, as a test
374 static PyObject *py_smbd_set_simple_acl(PyObject *self, PyObject *args, PyObject *kwargs)
376 const char * const kwnames[] = { "fname", "mode", "gid", "service", NULL };
377 char *fname, *service = NULL;
378 int ret;
379 int mode, gid = -1;
380 SMB_ACL_T acl;
381 TALLOC_CTX *frame;
382 connection_struct *conn;
384 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|iz",
385 discard_const_p(char *, kwnames),
386 &fname, &mode, &gid, &service))
387 return NULL;
389 acl = make_simple_acl(gid, mode);
391 frame = talloc_stackframe();
393 conn = get_conn(frame, service);
394 if (!conn) {
395 return NULL;
398 ret = set_sys_acl_conn(fname, SMB_ACL_TYPE_ACCESS, acl, conn);
399 TALLOC_FREE(acl);
401 if (ret != 0) {
402 TALLOC_FREE(frame);
403 errno = ret;
404 return PyErr_SetFromErrno(PyExc_OSError);
407 TALLOC_FREE(frame);
409 Py_RETURN_NONE;
413 chown a file
415 static PyObject *py_smbd_chown(PyObject *self, PyObject *args, PyObject *kwargs)
417 const char * const kwnames[] = { "fname", "uid", "gid", "service", NULL };
418 connection_struct *conn;
419 int ret;
421 char *fname, *service = NULL;
422 int uid, gid;
423 TALLOC_CTX *frame;
424 mode_t saved_umask;
425 struct smb_filename *smb_fname = NULL;
427 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sii|z",
428 discard_const_p(char *, kwnames),
429 &fname, &uid, &gid, &service))
430 return NULL;
432 frame = talloc_stackframe();
434 conn = get_conn(frame, service);
435 if (!conn) {
436 return NULL;
439 /* we want total control over the permissions on created files,
440 so set our umask to 0 */
441 saved_umask = umask(0);
443 smb_fname = synthetic_smb_fname(talloc_tos(),
444 fname,
445 NULL,
446 NULL,
447 lp_posix_pathnames() ?
448 SMB_FILENAME_POSIX_PATH : 0);
449 if (smb_fname == NULL) {
450 umask(saved_umask);
451 TALLOC_FREE(frame);
452 errno = ENOMEM;
453 return PyErr_SetFromErrno(PyExc_OSError);
456 ret = SMB_VFS_CHOWN(conn, smb_fname, uid, gid);
457 if (ret != 0) {
458 umask(saved_umask);
459 TALLOC_FREE(frame);
460 errno = ret;
461 return PyErr_SetFromErrno(PyExc_OSError);
464 umask(saved_umask);
466 TALLOC_FREE(frame);
468 Py_RETURN_NONE;
472 unlink a file
474 static PyObject *py_smbd_unlink(PyObject *self, PyObject *args, PyObject *kwargs)
476 const char * const kwnames[] = { "fname", "service", NULL };
477 connection_struct *conn;
478 int ret;
479 struct smb_filename *smb_fname = NULL;
480 char *fname, *service = NULL;
481 TALLOC_CTX *frame;
483 frame = talloc_stackframe();
485 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|z",
486 discard_const_p(char *, kwnames),
487 &fname, &service)) {
488 TALLOC_FREE(frame);
489 return NULL;
492 conn = get_conn(frame, service);
493 if (!conn) {
494 TALLOC_FREE(frame);
495 return NULL;
498 smb_fname = synthetic_smb_fname_split(frame,
499 fname,
500 lp_posix_pathnames());
501 if (smb_fname == NULL) {
502 TALLOC_FREE(frame);
503 return PyErr_NoMemory();
506 ret = SMB_VFS_UNLINK(conn, smb_fname);
507 if (ret != 0) {
508 TALLOC_FREE(frame);
509 errno = ret;
510 return PyErr_SetFromErrno(PyExc_OSError);
513 TALLOC_FREE(frame);
515 Py_RETURN_NONE;
519 check if we have ACL support
521 static PyObject *py_smbd_have_posix_acls(PyObject *self)
523 #ifdef HAVE_POSIX_ACLS
524 return PyBool_FromLong(true);
525 #else
526 return PyBool_FromLong(false);
527 #endif
531 set the NT ACL on a file
533 static PyObject *py_smbd_set_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
535 const char * const kwnames[] = { "fname", "security_info_sent", "sd", "service", NULL };
536 NTSTATUS status;
537 char *fname, *service = NULL;
538 int security_info_sent;
539 PyObject *py_sd;
540 struct security_descriptor *sd;
541 connection_struct *conn;
542 TALLOC_CTX *frame;
544 frame = talloc_stackframe();
546 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
547 "siO|z", discard_const_p(char *, kwnames),
548 &fname, &security_info_sent, &py_sd, &service)) {
549 TALLOC_FREE(frame);
550 return NULL;
553 if (!py_check_dcerpc_type(py_sd, "samba.dcerpc.security", "descriptor")) {
554 TALLOC_FREE(frame);
555 return NULL;
558 conn = get_conn(frame, service);
559 if (!conn) {
560 TALLOC_FREE(frame);
561 return NULL;
564 sd = pytalloc_get_type(py_sd, struct security_descriptor);
566 status = set_nt_acl_conn(fname, security_info_sent, sd, conn);
567 TALLOC_FREE(frame);
568 PyErr_NTSTATUS_IS_ERR_RAISE(status);
570 Py_RETURN_NONE;
574 Return the NT ACL on a file
576 static PyObject *py_smbd_get_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
578 const char * const kwnames[] = { "fname", "security_info_wanted", "service", NULL };
579 char *fname, *service = NULL;
580 int security_info_wanted;
581 PyObject *py_sd;
582 struct security_descriptor *sd;
583 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
584 connection_struct *conn;
585 NTSTATUS status;
587 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|z", discard_const_p(char *, kwnames),
588 &fname, &security_info_wanted, &service)) {
589 TALLOC_FREE(tmp_ctx);
590 return NULL;
593 conn = get_conn(tmp_ctx, service);
594 if (!conn) {
595 TALLOC_FREE(tmp_ctx);
596 return NULL;
599 status = get_nt_acl_conn(tmp_ctx, fname, conn, security_info_wanted, &sd);
600 PyErr_NTSTATUS_IS_ERR_RAISE(status);
602 py_sd = py_return_ndr_struct("samba.dcerpc.security", "descriptor", sd, sd);
604 TALLOC_FREE(tmp_ctx);
606 return py_sd;
610 set the posix (or similar) ACL on a file
612 static PyObject *py_smbd_set_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
614 const char * const kwnames[] = { "fname", "acl_type", "acl", "service", NULL };
615 TALLOC_CTX *frame = talloc_stackframe();
616 int ret;
617 char *fname, *service = NULL;
618 PyObject *py_acl;
619 struct smb_acl_t *acl;
620 int acl_type;
621 connection_struct *conn;
623 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|z",
624 discard_const_p(char *, kwnames),
625 &fname, &acl_type, &py_acl, &service)) {
626 TALLOC_FREE(frame);
627 return NULL;
630 if (!py_check_dcerpc_type(py_acl, "samba.dcerpc.smb_acl", "t")) {
631 TALLOC_FREE(frame);
632 return NULL;
635 conn = get_conn(frame, service);
636 if (!conn) {
637 TALLOC_FREE(frame);
638 return NULL;
641 acl = pytalloc_get_type(py_acl, struct smb_acl_t);
643 ret = set_sys_acl_conn(fname, acl_type, acl, conn);
644 if (ret != 0) {
645 TALLOC_FREE(frame);
646 errno = ret;
647 return PyErr_SetFromErrno(PyExc_OSError);
650 TALLOC_FREE(frame);
651 Py_RETURN_NONE;
655 Return the posix (or similar) ACL on a file
657 static PyObject *py_smbd_get_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
659 const char * const kwnames[] = { "fname", "acl_type", "service", NULL };
660 char *fname;
661 PyObject *py_acl;
662 struct smb_acl_t *acl;
663 int acl_type;
664 TALLOC_CTX *frame = talloc_stackframe();
665 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
666 connection_struct *conn;
667 char *service = NULL;
668 struct smb_filename *smb_fname = NULL;
670 if (!tmp_ctx) {
671 PyErr_NoMemory();
672 return NULL;
675 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|z",
676 discard_const_p(char *, kwnames),
677 &fname, &acl_type, &service)) {
678 TALLOC_FREE(frame);
679 TALLOC_FREE(tmp_ctx);
680 return NULL;
683 conn = get_conn(frame, service);
684 if (!conn) {
685 TALLOC_FREE(frame);
686 TALLOC_FREE(tmp_ctx);
687 return NULL;
690 smb_fname = synthetic_smb_fname_split(frame,
691 fname,
692 lp_posix_pathnames());
693 if (smb_fname == NULL) {
694 TALLOC_FREE(frame);
695 TALLOC_FREE(tmp_ctx);
696 return NULL;
698 acl = SMB_VFS_SYS_ACL_GET_FILE( conn, smb_fname, acl_type, tmp_ctx);
699 if (!acl) {
700 TALLOC_FREE(frame);
701 TALLOC_FREE(tmp_ctx);
702 return PyErr_SetFromErrno(PyExc_OSError);
705 py_acl = py_return_ndr_struct("samba.dcerpc.smb_acl", "t", acl, acl);
707 TALLOC_FREE(frame);
708 TALLOC_FREE(tmp_ctx);
710 return py_acl;
713 static PyMethodDef py_smbd_methods[] = {
714 { "have_posix_acls",
715 (PyCFunction)py_smbd_have_posix_acls, METH_NOARGS,
716 NULL },
717 { "set_simple_acl",
718 (PyCFunction)py_smbd_set_simple_acl, METH_VARARGS|METH_KEYWORDS,
719 NULL },
720 { "set_nt_acl",
721 (PyCFunction)py_smbd_set_nt_acl, METH_VARARGS|METH_KEYWORDS,
722 NULL },
723 { "get_nt_acl",
724 (PyCFunction)py_smbd_get_nt_acl, METH_VARARGS|METH_KEYWORDS,
725 NULL },
726 { "get_sys_acl",
727 (PyCFunction)py_smbd_get_sys_acl, METH_VARARGS|METH_KEYWORDS,
728 NULL },
729 { "set_sys_acl",
730 (PyCFunction)py_smbd_set_sys_acl, METH_VARARGS|METH_KEYWORDS,
731 NULL },
732 { "chown",
733 (PyCFunction)py_smbd_chown, METH_VARARGS|METH_KEYWORDS,
734 NULL },
735 { "unlink",
736 (PyCFunction)py_smbd_unlink, METH_VARARGS|METH_KEYWORDS,
737 NULL },
738 { NULL }
741 void initsmbd(void);
742 void initsmbd(void)
744 PyObject *m;
746 m = Py_InitModule3("smbd", py_smbd_methods,
747 "Python bindings for the smbd file server.");
748 if (m == NULL)
749 return;