1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
/*
Unix SMB/CIFS implementation.
Check access to files based on security descriptors.
Copyright (C) Jeremy Allison 2005-2006.
Copyright (C) Michael Adam 2007.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "system/filesys.h"
#include "../libcli/security/security.h"
#include "../librpc/gen_ndr/ndr_security.h"
#include "smbd/smbd.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_ACLS
/****************************************************************************
Actually emulate the in-kernel access checking for delete access. We need
this to successfully return ACCESS_DENIED on a file open for delete access.
****************************************************************************/
bool can_delete_file_in_directory(connection_struct *conn,
const struct smb_filename *smb_fname)
{
TALLOC_CTX *ctx = talloc_tos();
char *dname = NULL;
struct smb_filename *smb_fname_parent = NULL;
NTSTATUS status;
bool ret;
if (!CAN_WRITE(conn)) {
return False;
}
if (!lp_acl_check_permissions(SNUM(conn))) {
/* This option means don't check. */
return true;
}
/* Get the parent directory permission mask and owners. */
if (!parent_dirname(ctx, smb_fname->base_name, &dname, NULL)) {
return False;
}
status = create_synthetic_smb_fname(ctx, dname, NULL, NULL,
&smb_fname_parent);
if (!NT_STATUS_IS_OK(status)) {
ret = false;
goto out;
}
if(SMB_VFS_STAT(conn, smb_fname_parent) != 0) {
ret = false;
goto out;
}
/* fast paths first */
if (!S_ISDIR(smb_fname_parent->st.st_ex_mode)) {
ret = false;
goto out;
}
if (get_current_uid(conn) == (uid_t)0) {
/* I'm sorry sir, I didn't know you were root... */
ret = true;
goto out;
}
#ifdef S_ISVTX
/* sticky bit means delete only by owner of file or by root or
* by owner of directory. */
if (smb_fname_parent->st.st_ex_mode & S_ISVTX) {
if (!VALID_STAT(smb_fname->st)) {
/* If the file doesn't already exist then
* yes we'll be able to delete it. */
ret = true;
goto out;
}
/*
* Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
* for bug #3348. Don't assume owning sticky bit
* directory means write access allowed.
* Fail to delete if we're not the owner of the file,
* or the owner of the directory as we have no possible
* chance of deleting. Otherwise, go on and check the ACL.
*/
if ((get_current_uid(conn) !=
smb_fname_parent->st.st_ex_uid) &&
(get_current_uid(conn) != smb_fname->st.st_ex_uid)) {
DEBUG(10,("can_delete_file_in_directory: not "
"owner of file %s or directory %s",
smb_fname_str_dbg(smb_fname),
smb_fname_str_dbg(smb_fname_parent)));
ret = false;
goto out;
}
}
#endif
/* now for ACL checks */
/*
* There's two ways to get the permission to delete a file: First by
* having the DELETE bit on the file itself and second if that does
* not help, by the DELETE_CHILD bit on the containing directory.
*
* Here we only check the directory permissions, we will
* check the file DELETE permission separately.
*/
ret = NT_STATUS_IS_OK(smbd_check_access_rights(conn,
smb_fname_parent,
FILE_DELETE_CHILD));
out:
TALLOC_FREE(dname);
TALLOC_FREE(smb_fname_parent);
return ret;
}
/****************************************************************************
Userspace check for write access.
****************************************************************************/
bool can_write_to_file(connection_struct *conn,
const struct smb_filename *smb_fname)
{
return NT_STATUS_IS_OK(smbd_check_access_rights(conn,
smb_fname,
FILE_WRITE_DATA));
}
/****************************************************************************
Check for an existing default Windows ACL on a directory.
****************************************************************************/
bool directory_has_default_acl(connection_struct *conn, const char *fname)
{
/* returns talloced off tos. */
struct security_descriptor *secdesc = NULL;
unsigned int i;
NTSTATUS status = SMB_VFS_GET_NT_ACL(conn, fname,
SECINFO_DACL, &secdesc);
if (!NT_STATUS_IS_OK(status) || secdesc == NULL) {
return false;
}
for (i = 0; i < secdesc->dacl->num_aces; i++) {
struct security_ace *psa = &secdesc->dacl->aces[i];
if (psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|
SEC_ACE_FLAG_CONTAINER_INHERIT)) {
TALLOC_FREE(secdesc);
return true;
}
}
TALLOC_FREE(secdesc);
return false;
}
|