Wrap the unix token info in a unix_user_token in auth_serversupplied_info
[ira/wip.git] / source3 / smbd / file_access.c
1 /*
2    Unix SMB/CIFS implementation.
3    Check access to files based on security descriptors.
4    Copyright (C) Jeremy Allison 2005-2006.
5    Copyright (C) Michael Adam 2007.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22
23 #undef  DBGC_CLASS
24 #define DBGC_CLASS DBGC_ACLS
25
26 /**
27  * Security descriptor / NT Token level access check function.
28  */
29 bool can_access_file_acl(struct connection_struct *conn,
30                                 const char * fname,
31                                 uint32_t access_mask)
32 {
33         bool result;
34         NTSTATUS status;
35         uint32_t access_granted;
36         struct security_descriptor *secdesc = NULL;
37
38         status = SMB_VFS_GET_NT_ACL(conn, fname,
39                                     (OWNER_SECURITY_INFORMATION |
40                                      GROUP_SECURITY_INFORMATION |
41                                      DACL_SECURITY_INFORMATION),
42                                     &secdesc);
43         if (!NT_STATUS_IS_OK(status)) {
44                 DEBUG(5, ("Could not get acl: %s\n", nt_errstr(status)));
45                 return false;
46         }
47
48         result = se_access_check(secdesc, conn->server_info->ptok,
49                                  access_mask, &access_granted, &status);
50         TALLOC_FREE(secdesc);
51         return result;
52 }
53
54 /****************************************************************************
55  Actually emulate the in-kernel access checking for delete access. We need
56  this to successfully return ACCESS_DENIED on a file open for delete access.
57 ****************************************************************************/
58
59 bool can_delete_file_in_directory(connection_struct *conn, const char *fname)
60 {
61         SMB_STRUCT_STAT sbuf;
62         TALLOC_CTX *ctx = talloc_tos();
63         char *dname = NULL;
64
65         if (!CAN_WRITE(conn)) {
66                 return False;
67         }
68
69         /* Get the parent directory permission mask and owners. */
70         if (!parent_dirname_talloc(ctx,
71                                 fname,
72                                 &dname,
73                                 NULL)) {
74                 return False;
75         }
76         if(SMB_VFS_STAT(conn, dname, &sbuf) != 0) {
77                 return False;
78         }
79
80         /* fast paths first */
81
82         if (!S_ISDIR(sbuf.st_mode)) {
83                 return False;
84         }
85         if (conn->server_info->utok.uid == 0 || conn->admin_user) {
86                 /* I'm sorry sir, I didn't know you were root... */
87                 return True;
88         }
89
90 #ifdef S_ISVTX
91         /* sticky bit means delete only by owner or root. */
92         if (sbuf.st_mode & S_ISVTX) {
93                 SMB_STRUCT_STAT sbuf_file;
94                 if(SMB_VFS_STAT(conn, fname, &sbuf_file) != 0) {
95                         if (errno == ENOENT) {
96                                 /* If the file doesn't already exist then
97                                  * yes we'll be able to delete it. */
98                                 return True;
99                         }
100                         return False;
101                 }
102                 /*
103                  * Patch from SATOH Fumiyasu <fumiyas@miraclelinux.com>
104                  * for bug #3348. Don't assume owning sticky bit
105                  * directory means write access allowed.
106                  */
107                 if (conn->server_info->utok.uid != sbuf_file.st_uid) {
108                         return False;
109                 }
110         }
111 #endif
112
113         /* now for ACL checks */
114
115         /*
116          * There's two ways to get the permission to delete a file: First by
117          * having the DELETE bit on the file itself and second if that does
118          * not help, by the DELETE_CHILD bit on the containing directory.
119          *
120          * Here we check the other way round because with just posix
121          * permissions looking at the file itself will never grant DELETE, so
122          * by looking at the directory first we save one get_acl call.
123          */
124
125         if (can_access_file_acl(conn, dname, FILE_DELETE_CHILD)) {
126                 return true;
127         }
128
129         return can_access_file_acl(conn, fname, DELETE_ACCESS);
130 }
131
132 /****************************************************************************
133  Actually emulate the in-kernel access checking for read/write access. We need
134  this to successfully check for ability to write for dos filetimes.
135  Note this doesn't take into account share write permissions.
136 ****************************************************************************/
137
138 bool can_access_file_data(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf, uint32 access_mask)
139 {
140         if (!(access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))) {
141                 return False;
142         }
143         access_mask &= (FILE_READ_DATA|FILE_WRITE_DATA);
144
145         /* some fast paths first */
146
147         DEBUG(10,("can_access_file_data: requesting 0x%x on file %s\n",
148                 (unsigned int)access_mask, fname ));
149
150         if (conn->server_info->utok.uid == 0 || conn->admin_user) {
151                 /* I'm sorry sir, I didn't know you were root... */
152                 return True;
153         }
154
155         if (!VALID_STAT(*psbuf)) {
156                 /* Get the file permission mask and owners. */
157                 if(SMB_VFS_STAT(conn, fname, psbuf) != 0) {
158                         return False;
159                 }
160         }
161
162         /* Check primary owner access. */
163         if (conn->server_info->utok.uid == psbuf->st_uid) {
164                 switch (access_mask) {
165                         case FILE_READ_DATA:
166                                 return (psbuf->st_mode & S_IRUSR) ? True : False;
167
168                         case FILE_WRITE_DATA:
169                                 return (psbuf->st_mode & S_IWUSR) ? True : False;
170
171                         default: /* FILE_READ_DATA|FILE_WRITE_DATA */
172
173                                 if ((psbuf->st_mode & (S_IWUSR|S_IRUSR)) == (S_IWUSR|S_IRUSR)) {
174                                         return True;
175                                 } else {
176                                         return False;
177                                 }
178                 }
179         }
180
181         /* now for ACL checks */
182
183         return can_access_file_acl(conn, fname, access_mask);
184 }
185
186 /****************************************************************************
187  Userspace check for write access.
188  Note this doesn't take into account share write permissions.
189 ****************************************************************************/
190
191 bool can_write_to_file(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf)
192 {
193         return can_access_file_data(conn, fname, psbuf, FILE_WRITE_DATA);
194 }
195
196 /****************************************************************************
197  Check for an existing default Windows ACL on a directory.
198 ****************************************************************************/
199
200 bool directory_has_default_acl(connection_struct *conn, const char *fname)
201 {
202         /* returns talloced off tos. */
203         struct security_descriptor *secdesc = NULL;
204         unsigned int i;
205         NTSTATUS status = SMB_VFS_GET_NT_ACL(conn, fname,
206                                 DACL_SECURITY_INFORMATION, &secdesc);
207
208         if (!NT_STATUS_IS_OK(status) || secdesc == NULL) {
209                 return false;
210         }
211
212         for (i = 0; i < secdesc->dacl->num_aces; i++) {
213                 struct security_ace *psa = &secdesc->dacl->aces[i];
214                 if (psa->flags & (SEC_ACE_FLAG_OBJECT_INHERIT|
215                                 SEC_ACE_FLAG_CONTAINER_INHERIT)) {
216                         TALLOC_FREE(secdesc);
217                         return true;
218                 }
219         }
220         TALLOC_FREE(secdesc);
221         return false;
222 }