Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/cifsacl.c |
| 3 | * |
| 4 | * Copyright (C) International Business Machines Corp., 2007 |
| 5 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 6 | * |
| 7 | * Contains the routines for mapping CIFS/NTFS ACLs |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Lesser General Public License as published |
| 11 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 17 | * the GNU Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public License |
| 20 | * along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 24 | #include <linux/fs.h> |
| 25 | #include "cifspdu.h" |
| 26 | #include "cifsglob.h" |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 27 | #include "cifsacl.h" |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 28 | #include "cifsproto.h" |
| 29 | #include "cifs_debug.h" |
Steve French | 6587400 | 2007-09-25 19:53:44 +0000 | [diff] [blame] | 30 | |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 31 | |
| 32 | #ifdef CONFIG_CIFS_EXPERIMENTAL |
| 33 | |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 34 | static struct cifs_wksid wksidarr[NUM_WK_SIDS] = { |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 35 | {{1, 0, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0} }, "null user"}, |
| 36 | {{1, 1, {0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0} }, "nobody"}, |
Dave Kleikamp | ce51ae1 | 2007-10-16 21:35:39 +0000 | [diff] [blame] | 37 | {{1, 1, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(11), 0, 0, 0, 0} }, "net-users"}, |
| 38 | {{1, 1, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(18), 0, 0, 0, 0} }, "sys"}, |
| 39 | {{1, 2, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(32), cpu_to_le32(544), 0, 0, 0} }, "root"}, |
| 40 | {{1, 2, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(32), cpu_to_le32(545), 0, 0, 0} }, "users"}, |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 41 | {{1, 2, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(32), cpu_to_le32(546), 0, 0, 0} }, "guest"} } |
| 42 | ; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 43 | |
| 44 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 45 | /* security id for everyone */ |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 46 | static const struct cifs_sid sid_everyone = { |
| 47 | 1, 1, {0, 0, 0, 0, 0, 1}, {0} }; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 48 | /* group users */ |
| 49 | static const struct cifs_sid sid_user = |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 50 | {1, 2 , {0, 0, 0, 0, 0, 5}, {} }; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 51 | |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 52 | |
| 53 | int match_sid(struct cifs_sid *ctsid) |
| 54 | { |
| 55 | int i, j; |
| 56 | int num_subauth, num_sat, num_saw; |
| 57 | struct cifs_sid *cwsid; |
| 58 | |
| 59 | if (!ctsid) |
| 60 | return (-1); |
| 61 | |
| 62 | for (i = 0; i < NUM_WK_SIDS; ++i) { |
| 63 | cwsid = &(wksidarr[i].cifssid); |
| 64 | |
| 65 | /* compare the revision */ |
| 66 | if (ctsid->revision != cwsid->revision) |
| 67 | continue; |
| 68 | |
| 69 | /* compare all of the six auth values */ |
| 70 | for (j = 0; j < 6; ++j) { |
| 71 | if (ctsid->authority[j] != cwsid->authority[j]) |
| 72 | break; |
| 73 | } |
| 74 | if (j < 6) |
| 75 | continue; /* all of the auth values did not match */ |
| 76 | |
| 77 | /* compare all of the subauth values if any */ |
Dave Kleikamp | ce51ae1 | 2007-10-16 21:35:39 +0000 | [diff] [blame] | 78 | num_sat = ctsid->num_subauth; |
| 79 | num_saw = cwsid->num_subauth; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 80 | num_subauth = num_sat < num_saw ? num_sat : num_saw; |
| 81 | if (num_subauth) { |
| 82 | for (j = 0; j < num_subauth; ++j) { |
| 83 | if (ctsid->sub_auth[j] != cwsid->sub_auth[j]) |
| 84 | break; |
| 85 | } |
| 86 | if (j < num_subauth) |
| 87 | continue; /* all sub_auth values do not match */ |
| 88 | } |
| 89 | |
| 90 | cFYI(1, ("matching sid: %s\n", wksidarr[i].sidname)); |
| 91 | return (0); /* sids compare/match */ |
| 92 | } |
| 93 | |
| 94 | cFYI(1, ("No matching sid")); |
| 95 | return (-1); |
| 96 | } |
| 97 | |
Steve French | a750e77 | 2007-10-17 22:50:39 +0000 | [diff] [blame] | 98 | /* if the two SIDs (roughly equivalent to a UUID for a user or group) are |
| 99 | the same returns 1, if they do not match returns 0 */ |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 100 | int compare_sids(const struct cifs_sid *ctsid, const struct cifs_sid *cwsid) |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 101 | { |
| 102 | int i; |
| 103 | int num_subauth, num_sat, num_saw; |
| 104 | |
| 105 | if ((!ctsid) || (!cwsid)) |
Steve French | a750e77 | 2007-10-17 22:50:39 +0000 | [diff] [blame] | 106 | return (0); |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 107 | |
| 108 | /* compare the revision */ |
| 109 | if (ctsid->revision != cwsid->revision) |
Steve French | a750e77 | 2007-10-17 22:50:39 +0000 | [diff] [blame] | 110 | return (0); |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 111 | |
| 112 | /* compare all of the six auth values */ |
| 113 | for (i = 0; i < 6; ++i) { |
| 114 | if (ctsid->authority[i] != cwsid->authority[i]) |
Steve French | a750e77 | 2007-10-17 22:50:39 +0000 | [diff] [blame] | 115 | return (0); |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* compare all of the subauth values if any */ |
Steve French | adbc035 | 2007-10-17 02:12:46 +0000 | [diff] [blame] | 119 | num_sat = ctsid->num_subauth; |
Steve French | adddd49 | 2007-10-17 02:48:17 +0000 | [diff] [blame] | 120 | num_saw = cwsid->num_subauth; |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 121 | num_subauth = num_sat < num_saw ? num_sat : num_saw; |
| 122 | if (num_subauth) { |
| 123 | for (i = 0; i < num_subauth; ++i) { |
| 124 | if (ctsid->sub_auth[i] != cwsid->sub_auth[i]) |
Steve French | a750e77 | 2007-10-17 22:50:39 +0000 | [diff] [blame] | 125 | return (0); |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Steve French | a750e77 | 2007-10-17 22:50:39 +0000 | [diff] [blame] | 129 | return (1); /* sids compare/match */ |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 132 | |
| 133 | /* copy ntsd, owner sid, and group sid from a security descriptor to another */ |
| 134 | static void copy_sec_desc(const struct cifs_ntsd *pntsd, |
| 135 | struct cifs_ntsd *pnntsd, __u32 sidsoffset) |
| 136 | { |
| 137 | int i; |
| 138 | |
| 139 | struct cifs_sid *owner_sid_ptr, *group_sid_ptr; |
| 140 | struct cifs_sid *nowner_sid_ptr, *ngroup_sid_ptr; |
| 141 | |
| 142 | /* copy security descriptor control portion */ |
| 143 | pnntsd->revision = pntsd->revision; |
| 144 | pnntsd->type = pntsd->type; |
| 145 | pnntsd->dacloffset = cpu_to_le32(sizeof(struct cifs_ntsd)); |
| 146 | pnntsd->sacloffset = 0; |
| 147 | pnntsd->osidoffset = cpu_to_le32(sidsoffset); |
| 148 | pnntsd->gsidoffset = cpu_to_le32(sidsoffset + sizeof(struct cifs_sid)); |
| 149 | |
| 150 | /* copy owner sid */ |
| 151 | owner_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 152 | le32_to_cpu(pntsd->osidoffset)); |
| 153 | nowner_sid_ptr = (struct cifs_sid *)((char *)pnntsd + sidsoffset); |
| 154 | |
| 155 | nowner_sid_ptr->revision = owner_sid_ptr->revision; |
| 156 | nowner_sid_ptr->num_subauth = owner_sid_ptr->num_subauth; |
| 157 | for (i = 0; i < 6; i++) |
| 158 | nowner_sid_ptr->authority[i] = owner_sid_ptr->authority[i]; |
| 159 | for (i = 0; i < 5; i++) |
| 160 | nowner_sid_ptr->sub_auth[i] = owner_sid_ptr->sub_auth[i]; |
| 161 | |
| 162 | /* copy group sid */ |
| 163 | group_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 164 | le32_to_cpu(pntsd->gsidoffset)); |
| 165 | ngroup_sid_ptr = (struct cifs_sid *)((char *)pnntsd + sidsoffset + |
| 166 | sizeof(struct cifs_sid)); |
| 167 | |
| 168 | ngroup_sid_ptr->revision = group_sid_ptr->revision; |
| 169 | ngroup_sid_ptr->num_subauth = group_sid_ptr->num_subauth; |
| 170 | for (i = 0; i < 6; i++) |
| 171 | ngroup_sid_ptr->authority[i] = group_sid_ptr->authority[i]; |
| 172 | for (i = 0; i < 5; i++) |
| 173 | ngroup_sid_ptr->sub_auth[i] = |
| 174 | cpu_to_le32(group_sid_ptr->sub_auth[i]); |
| 175 | |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 180 | /* |
| 181 | change posix mode to reflect permissions |
| 182 | pmode is the existing mode (we only want to overwrite part of this |
| 183 | bits to set can be: S_IRWXU, S_IRWXG or S_IRWXO ie 00700 or 00070 or 00007 |
| 184 | */ |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 185 | static void access_flags_to_mode(__le32 ace_flags, int type, umode_t *pmode, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 186 | umode_t *pbits_to_set) |
Steve French | 4879b44 | 2007-10-19 21:57:39 +0000 | [diff] [blame] | 187 | { |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 188 | __u32 flags = le32_to_cpu(ace_flags); |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 189 | /* the order of ACEs is important. The canonical order is to begin with |
Steve French | ce06c9f | 2007-11-08 21:12:01 +0000 | [diff] [blame] | 190 | DENY entries followed by ALLOW, otherwise an allow entry could be |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 191 | encountered first, making the subsequent deny entry like "dead code" |
Steve French | ce06c9f | 2007-11-08 21:12:01 +0000 | [diff] [blame] | 192 | which would be superflous since Windows stops when a match is made |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 193 | for the operation you are trying to perform for your user */ |
| 194 | |
| 195 | /* For deny ACEs we change the mask so that subsequent allow access |
| 196 | control entries do not turn on the bits we are denying */ |
| 197 | if (type == ACCESS_DENIED) { |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 198 | if (flags & GENERIC_ALL) { |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 199 | *pbits_to_set &= ~S_IRWXUGO; |
| 200 | } |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 201 | if ((flags & GENERIC_WRITE) || |
| 202 | ((flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 203 | *pbits_to_set &= ~S_IWUGO; |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 204 | if ((flags & GENERIC_READ) || |
| 205 | ((flags & FILE_READ_RIGHTS) == FILE_READ_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 206 | *pbits_to_set &= ~S_IRUGO; |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 207 | if ((flags & GENERIC_EXECUTE) || |
| 208 | ((flags & FILE_EXEC_RIGHTS) == FILE_EXEC_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 209 | *pbits_to_set &= ~S_IXUGO; |
| 210 | return; |
| 211 | } else if (type != ACCESS_ALLOWED) { |
| 212 | cERROR(1, ("unknown access control type %d", type)); |
| 213 | return; |
| 214 | } |
| 215 | /* else ACCESS_ALLOWED type */ |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 216 | |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 217 | if (flags & GENERIC_ALL) { |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 218 | *pmode |= (S_IRWXUGO & (*pbits_to_set)); |
Steve French | d61e580 | 2007-10-26 04:32:43 +0000 | [diff] [blame] | 219 | #ifdef CONFIG_CIFS_DEBUG2 |
| 220 | cFYI(1, ("all perms")); |
| 221 | #endif |
| 222 | return; |
| 223 | } |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 224 | if ((flags & GENERIC_WRITE) || |
| 225 | ((flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 226 | *pmode |= (S_IWUGO & (*pbits_to_set)); |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 227 | if ((flags & GENERIC_READ) || |
| 228 | ((flags & FILE_READ_RIGHTS) == FILE_READ_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 229 | *pmode |= (S_IRUGO & (*pbits_to_set)); |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 230 | if ((flags & GENERIC_EXECUTE) || |
| 231 | ((flags & FILE_EXEC_RIGHTS) == FILE_EXEC_RIGHTS)) |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 232 | *pmode |= (S_IXUGO & (*pbits_to_set)); |
Steve French | d61e580 | 2007-10-26 04:32:43 +0000 | [diff] [blame] | 233 | |
| 234 | #ifdef CONFIG_CIFS_DEBUG2 |
Al Viro | 9b5e685 | 2007-12-05 08:24:38 +0000 | [diff] [blame] | 235 | cFYI(1, ("access flags 0x%x mode now 0x%x", flags, *pmode)); |
Steve French | d61e580 | 2007-10-26 04:32:43 +0000 | [diff] [blame] | 236 | #endif |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 237 | return; |
| 238 | } |
| 239 | |
Steve French | ce06c9f | 2007-11-08 21:12:01 +0000 | [diff] [blame] | 240 | /* |
| 241 | Generate access flags to reflect permissions mode is the existing mode. |
| 242 | This function is called for every ACE in the DACL whose SID matches |
| 243 | with either owner or group or everyone. |
| 244 | */ |
| 245 | |
| 246 | static void mode_to_access_flags(umode_t mode, umode_t bits_to_use, |
| 247 | __u32 *pace_flags) |
| 248 | { |
| 249 | /* reset access mask */ |
| 250 | *pace_flags = 0x0; |
| 251 | |
| 252 | /* bits to use are either S_IRWXU or S_IRWXG or S_IRWXO */ |
| 253 | mode &= bits_to_use; |
| 254 | |
| 255 | /* check for R/W/X UGO since we do not know whose flags |
| 256 | is this but we have cleared all the bits sans RWX for |
| 257 | either user or group or other as per bits_to_use */ |
| 258 | if (mode & S_IRUGO) |
| 259 | *pace_flags |= SET_FILE_READ_RIGHTS; |
| 260 | if (mode & S_IWUGO) |
| 261 | *pace_flags |= SET_FILE_WRITE_RIGHTS; |
| 262 | if (mode & S_IXUGO) |
| 263 | *pace_flags |= SET_FILE_EXEC_RIGHTS; |
| 264 | |
| 265 | #ifdef CONFIG_CIFS_DEBUG2 |
| 266 | cFYI(1, ("mode: 0x%x, access flags now 0x%x", mode, *pace_flags)); |
| 267 | #endif |
| 268 | return; |
| 269 | } |
| 270 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 271 | static __le16 fill_ace_for_sid(struct cifs_ace *pntace, |
| 272 | const struct cifs_sid *psid, __u64 nmode, umode_t bits) |
| 273 | { |
| 274 | int i; |
| 275 | __u16 size = 0; |
| 276 | __u32 access_req = 0; |
| 277 | |
| 278 | pntace->type = ACCESS_ALLOWED; |
| 279 | pntace->flags = 0x0; |
| 280 | mode_to_access_flags(nmode, bits, &access_req); |
| 281 | if (!access_req) |
| 282 | access_req = SET_MINIMUM_RIGHTS; |
| 283 | pntace->access_req = cpu_to_le32(access_req); |
| 284 | |
| 285 | pntace->sid.revision = psid->revision; |
| 286 | pntace->sid.num_subauth = psid->num_subauth; |
| 287 | for (i = 0; i < 6; i++) |
| 288 | pntace->sid.authority[i] = psid->authority[i]; |
| 289 | for (i = 0; i < psid->num_subauth; i++) |
| 290 | pntace->sid.sub_auth[i] = psid->sub_auth[i]; |
| 291 | |
| 292 | size = 1 + 1 + 2 + 4 + 1 + 1 + 6 + (psid->num_subauth * 4); |
| 293 | pntace->size = cpu_to_le16(size); |
| 294 | |
| 295 | return (size); |
| 296 | } |
| 297 | |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 298 | |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 299 | #ifdef CONFIG_CIFS_DEBUG2 |
| 300 | static void dump_ace(struct cifs_ace *pace, char *end_of_acl) |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 301 | { |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 302 | int num_subauth; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 303 | |
| 304 | /* validate that we do not go past end of acl */ |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 305 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 306 | if (le16_to_cpu(pace->size) < 16) { |
| 307 | cERROR(1, ("ACE too small, %d", le16_to_cpu(pace->size))); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | if (end_of_acl < (char *)pace + le16_to_cpu(pace->size)) { |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 312 | cERROR(1, ("ACL too small to parse ACE")); |
| 313 | return; |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 314 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 315 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 316 | num_subauth = pace->sid.num_subauth; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 317 | if (num_subauth) { |
Steve French | 8f18c13 | 2007-10-12 18:54:12 +0000 | [diff] [blame] | 318 | int i; |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 319 | cFYI(1, ("ACE revision %d num_auth %d type %d flags %d size %d", |
| 320 | pace->sid.revision, pace->sid.num_subauth, pace->type, |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 321 | pace->flags, le16_to_cpu(pace->size))); |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 322 | for (i = 0; i < num_subauth; ++i) { |
| 323 | cFYI(1, ("ACE sub_auth[%d]: 0x%x", i, |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 324 | le32_to_cpu(pace->sid.sub_auth[i]))); |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 325 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 326 | |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 327 | /* BB add length check to make sure that we do not have huge |
| 328 | num auths and therefore go off the end */ |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 329 | } |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 330 | |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 331 | return; |
| 332 | } |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 333 | #endif |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 334 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 335 | |
Steve French | a750e77 | 2007-10-17 22:50:39 +0000 | [diff] [blame] | 336 | static void parse_dacl(struct cifs_acl *pdacl, char *end_of_acl, |
Steve French | d61e580 | 2007-10-26 04:32:43 +0000 | [diff] [blame] | 337 | struct cifs_sid *pownersid, struct cifs_sid *pgrpsid, |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 338 | struct inode *inode) |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 339 | { |
| 340 | int i; |
| 341 | int num_aces = 0; |
| 342 | int acl_size; |
| 343 | char *acl_base; |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 344 | struct cifs_ace **ppace; |
| 345 | |
| 346 | /* BB need to add parm so we can store the SID BB */ |
| 347 | |
Steve French | 2b83457 | 2007-11-25 10:01:00 +0000 | [diff] [blame] | 348 | if (!pdacl) { |
| 349 | /* no DACL in the security descriptor, set |
| 350 | all the permissions for user/group/other */ |
| 351 | inode->i_mode |= S_IRWXUGO; |
| 352 | return; |
| 353 | } |
| 354 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 355 | /* validate that we do not go past end of acl */ |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 356 | if (end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) { |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 357 | cERROR(1, ("ACL too small to parse DACL")); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | #ifdef CONFIG_CIFS_DEBUG2 |
| 362 | cFYI(1, ("DACL revision %d size %d num aces %d", |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 363 | le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size), |
| 364 | le32_to_cpu(pdacl->num_aces))); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 365 | #endif |
| 366 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 367 | /* reset rwx permissions for user/group/other. |
| 368 | Also, if num_aces is 0 i.e. DACL has no ACEs, |
| 369 | user/group/other have no permissions */ |
| 370 | inode->i_mode &= ~(S_IRWXUGO); |
| 371 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 372 | acl_base = (char *)pdacl; |
| 373 | acl_size = sizeof(struct cifs_acl); |
| 374 | |
Steve French | adbc035 | 2007-10-17 02:12:46 +0000 | [diff] [blame] | 375 | num_aces = le32_to_cpu(pdacl->num_aces); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 376 | if (num_aces > 0) { |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 377 | umode_t user_mask = S_IRWXU; |
| 378 | umode_t group_mask = S_IRWXG; |
| 379 | umode_t other_mask = S_IRWXO; |
| 380 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 381 | ppace = kmalloc(num_aces * sizeof(struct cifs_ace *), |
| 382 | GFP_KERNEL); |
| 383 | |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 384 | /* cifscred->cecount = pdacl->num_aces; |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 385 | cifscred->aces = kmalloc(num_aces * |
| 386 | sizeof(struct cifs_ace *), GFP_KERNEL);*/ |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 387 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 388 | for (i = 0; i < num_aces; ++i) { |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 389 | ppace[i] = (struct cifs_ace *) (acl_base + acl_size); |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 390 | #ifdef CONFIG_CIFS_DEBUG2 |
| 391 | dump_ace(ppace[i], end_of_acl); |
| 392 | #endif |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 393 | if (compare_sids(&(ppace[i]->sid), pownersid)) |
| 394 | access_flags_to_mode(ppace[i]->access_req, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 395 | ppace[i]->type, |
| 396 | &(inode->i_mode), |
| 397 | &user_mask); |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 398 | if (compare_sids(&(ppace[i]->sid), pgrpsid)) |
| 399 | access_flags_to_mode(ppace[i]->access_req, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 400 | ppace[i]->type, |
| 401 | &(inode->i_mode), |
| 402 | &group_mask); |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 403 | if (compare_sids(&(ppace[i]->sid), &sid_everyone)) |
| 404 | access_flags_to_mode(ppace[i]->access_req, |
Steve French | 15b0395 | 2007-11-08 17:57:40 +0000 | [diff] [blame] | 405 | ppace[i]->type, |
| 406 | &(inode->i_mode), |
| 407 | &other_mask); |
Shirish Pargaonkar | e01b640 | 2007-10-30 04:45:14 +0000 | [diff] [blame] | 408 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 409 | /* memcpy((void *)(&(cifscred->aces[i])), |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 410 | (void *)ppace[i], |
| 411 | sizeof(struct cifs_ace)); */ |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 412 | |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 413 | acl_base = (char *)ppace[i]; |
| 414 | acl_size = le16_to_cpu(ppace[i]->size); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | kfree(ppace); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return; |
| 421 | } |
| 422 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 423 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 424 | static int set_chmod_dacl(struct cifs_acl *pndacl, struct cifs_sid *pownersid, |
| 425 | struct cifs_sid *pgrpsid, __u64 nmode) |
| 426 | { |
| 427 | __le16 size = 0; |
| 428 | struct cifs_acl *pnndacl; |
| 429 | |
| 430 | pnndacl = (struct cifs_acl *)((char *)pndacl + sizeof(struct cifs_acl)); |
| 431 | |
| 432 | size += fill_ace_for_sid((struct cifs_ace *) ((char *)pnndacl + size), |
| 433 | pownersid, nmode, S_IRWXU); |
| 434 | size += fill_ace_for_sid((struct cifs_ace *)((char *)pnndacl + size), |
| 435 | pgrpsid, nmode, S_IRWXG); |
| 436 | size += fill_ace_for_sid((struct cifs_ace *)((char *)pnndacl + size), |
| 437 | &sid_everyone, nmode, S_IRWXO); |
| 438 | |
| 439 | pndacl->size = cpu_to_le16(size + sizeof(struct cifs_acl)); |
| 440 | pndacl->num_aces = 3; |
| 441 | |
| 442 | return (0); |
| 443 | } |
| 444 | |
| 445 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 446 | static int parse_sid(struct cifs_sid *psid, char *end_of_acl) |
| 447 | { |
| 448 | /* BB need to add parm so we can store the SID BB */ |
| 449 | |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 450 | /* validate that we do not go past end of ACL - sid must be at least 8 |
| 451 | bytes long (assuming no sub-auths - e.g. the null SID */ |
| 452 | if (end_of_acl < (char *)psid + 8) { |
| 453 | cERROR(1, ("ACL too small to parse SID %p", psid)); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 454 | return -EINVAL; |
| 455 | } |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 456 | |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 457 | if (psid->num_subauth) { |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 458 | #ifdef CONFIG_CIFS_DEBUG2 |
Steve French | 8f18c13 | 2007-10-12 18:54:12 +0000 | [diff] [blame] | 459 | int i; |
Steve French | 44093ca | 2007-10-23 21:22:55 +0000 | [diff] [blame] | 460 | cFYI(1, ("SID revision %d num_auth %d", |
| 461 | psid->revision, psid->num_subauth)); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 462 | |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 463 | for (i = 0; i < psid->num_subauth; i++) { |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 464 | cFYI(1, ("SID sub_auth[%d]: 0x%x ", i, |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 465 | le32_to_cpu(psid->sub_auth[i]))); |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 468 | /* BB add length check to make sure that we do not have huge |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 469 | num auths and therefore go off the end */ |
Steve French | d12fd12 | 2007-10-03 19:43:19 +0000 | [diff] [blame] | 470 | cFYI(1, ("RID 0x%x", |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 471 | le32_to_cpu(psid->sub_auth[psid->num_subauth-1]))); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 472 | #endif |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 475 | return 0; |
| 476 | } |
| 477 | |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 478 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 479 | /* Convert CIFS ACL to POSIX form */ |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 480 | static int parse_sec_desc(struct cifs_ntsd *pntsd, int acl_len, |
| 481 | struct inode *inode) |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 482 | { |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 483 | int rc; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 484 | struct cifs_sid *owner_sid_ptr, *group_sid_ptr; |
| 485 | struct cifs_acl *dacl_ptr; /* no need for SACL ptr */ |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 486 | char *end_of_acl = ((char *)pntsd) + acl_len; |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 487 | __u32 dacloffset; |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 488 | |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 489 | if ((inode == NULL) || (pntsd == NULL)) |
| 490 | return -EIO; |
| 491 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 492 | owner_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 493 | le32_to_cpu(pntsd->osidoffset)); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 494 | group_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 495 | le32_to_cpu(pntsd->gsidoffset)); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 496 | dacloffset = le32_to_cpu(pntsd->dacloffset); |
Steve French | 63d2583 | 2007-11-05 21:46:10 +0000 | [diff] [blame] | 497 | dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 498 | #ifdef CONFIG_CIFS_DEBUG2 |
| 499 | cFYI(1, ("revision %d type 0x%x ooffset 0x%x goffset 0x%x " |
| 500 | "sacloffset 0x%x dacloffset 0x%x", |
Steve French | af6f461 | 2007-10-16 18:40:37 +0000 | [diff] [blame] | 501 | pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset), |
| 502 | le32_to_cpu(pntsd->gsidoffset), |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 503 | le32_to_cpu(pntsd->sacloffset), dacloffset)); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 504 | #endif |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 505 | /* cifs_dump_mem("owner_sid: ", owner_sid_ptr, 64); */ |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 506 | rc = parse_sid(owner_sid_ptr, end_of_acl); |
| 507 | if (rc) |
| 508 | return rc; |
| 509 | |
| 510 | rc = parse_sid(group_sid_ptr, end_of_acl); |
| 511 | if (rc) |
| 512 | return rc; |
| 513 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 514 | if (dacloffset) |
| 515 | parse_dacl(dacl_ptr, end_of_acl, owner_sid_ptr, |
Steve French | 63d2583 | 2007-11-05 21:46:10 +0000 | [diff] [blame] | 516 | group_sid_ptr, inode); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 517 | else |
| 518 | cFYI(1, ("no ACL")); /* BB grant all or default perms? */ |
Shirish Pargaonkar | d0d66c4 | 2007-10-03 18:22:19 +0000 | [diff] [blame] | 519 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 520 | /* cifscred->uid = owner_sid_ptr->rid; |
| 521 | cifscred->gid = group_sid_ptr->rid; |
| 522 | memcpy((void *)(&(cifscred->osid)), (void *)owner_sid_ptr, |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 523 | sizeof(struct cifs_sid)); |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 524 | memcpy((void *)(&(cifscred->gsid)), (void *)group_sid_ptr, |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 525 | sizeof(struct cifs_sid)); */ |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 526 | |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 527 | |
Steve French | bcb0203 | 2007-09-25 16:17:24 +0000 | [diff] [blame] | 528 | return (0); |
| 529 | } |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 530 | |
| 531 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 532 | /* Convert permission bits from mode to equivalent CIFS ACL */ |
| 533 | static int build_sec_desc(struct cifs_ntsd *pntsd, struct cifs_ntsd *pnntsd, |
| 534 | int acl_len, struct inode *inode, __u64 nmode) |
| 535 | { |
| 536 | int rc = 0; |
| 537 | __u32 dacloffset; |
| 538 | __u32 ndacloffset; |
| 539 | __u32 sidsoffset; |
| 540 | struct cifs_sid *owner_sid_ptr, *group_sid_ptr; |
| 541 | struct cifs_acl *dacl_ptr = NULL; /* no need for SACL ptr */ |
| 542 | struct cifs_acl *ndacl_ptr = NULL; /* no need for SACL ptr */ |
| 543 | |
| 544 | if ((inode == NULL) || (pntsd == NULL) || (pnntsd == NULL)) |
| 545 | return (-EIO); |
| 546 | |
| 547 | owner_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 548 | le32_to_cpu(pntsd->osidoffset)); |
| 549 | group_sid_ptr = (struct cifs_sid *)((char *)pntsd + |
| 550 | le32_to_cpu(pntsd->gsidoffset)); |
| 551 | |
| 552 | dacloffset = le32_to_cpu(pntsd->dacloffset); |
| 553 | dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset); |
| 554 | |
| 555 | ndacloffset = sizeof(struct cifs_ntsd); |
| 556 | ndacl_ptr = (struct cifs_acl *)((char *)pnntsd + ndacloffset); |
| 557 | ndacl_ptr->revision = dacl_ptr->revision; |
| 558 | ndacl_ptr->size = 0; |
| 559 | ndacl_ptr->num_aces = 0; |
| 560 | |
| 561 | rc = set_chmod_dacl(ndacl_ptr, owner_sid_ptr, group_sid_ptr, nmode); |
| 562 | |
| 563 | sidsoffset = ndacloffset + le16_to_cpu(ndacl_ptr->size); |
| 564 | |
| 565 | /* copy security descriptor control portion and owner and group sid */ |
| 566 | copy_sec_desc(pntsd, pnntsd, sidsoffset); |
| 567 | |
| 568 | return (rc); |
| 569 | } |
| 570 | |
| 571 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 572 | /* Retrieve an ACL from the server */ |
| 573 | static struct cifs_ntsd *get_cifs_acl(u32 *pacllen, struct inode *inode, |
| 574 | const char *path) |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 575 | { |
| 576 | struct cifsFileInfo *open_file; |
| 577 | int unlock_file = FALSE; |
| 578 | int xid; |
| 579 | int rc = -EIO; |
| 580 | __u16 fid; |
| 581 | struct super_block *sb; |
| 582 | struct cifs_sb_info *cifs_sb; |
| 583 | struct cifs_ntsd *pntsd = NULL; |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 584 | |
| 585 | cFYI(1, ("get mode from ACL for %s", path)); |
| 586 | |
| 587 | if (inode == NULL) |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 588 | return NULL; |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 589 | |
| 590 | xid = GetXid(); |
| 591 | open_file = find_readable_file(CIFS_I(inode)); |
| 592 | sb = inode->i_sb; |
| 593 | if (sb == NULL) { |
| 594 | FreeXid(xid); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 595 | return NULL; |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 596 | } |
| 597 | cifs_sb = CIFS_SB(sb); |
| 598 | |
| 599 | if (open_file) { |
| 600 | unlock_file = TRUE; |
| 601 | fid = open_file->netfid; |
| 602 | } else { |
| 603 | int oplock = FALSE; |
| 604 | /* open file */ |
| 605 | rc = CIFSSMBOpen(xid, cifs_sb->tcon, path, FILE_OPEN, |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 606 | READ_CONTROL, 0, &fid, &oplock, NULL, |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 607 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
| 608 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 609 | if (rc != 0) { |
| 610 | cERROR(1, ("Unable to open file to get ACL")); |
| 611 | FreeXid(xid); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 612 | return NULL; |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 616 | rc = CIFSSMBGetCIFSACL(xid, cifs_sb->tcon, fid, &pntsd, pacllen); |
| 617 | cFYI(1, ("GetCIFSACL rc = %d ACL len %d", rc, *pacllen)); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 618 | if (unlock_file == TRUE) |
| 619 | atomic_dec(&open_file->wrtPending); |
| 620 | else |
| 621 | CIFSSMBClose(xid, cifs_sb->tcon, fid); |
| 622 | |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 623 | FreeXid(xid); |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 624 | return pntsd; |
| 625 | } |
| 626 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 627 | /* Set an ACL on the server */ |
| 628 | static int set_cifs_acl(struct cifs_ntsd *pnntsd, __u32 acllen, |
| 629 | struct inode *inode, const char *path) |
| 630 | { |
| 631 | struct cifsFileInfo *open_file; |
| 632 | int unlock_file = FALSE; |
| 633 | int xid; |
| 634 | int rc = -EIO; |
| 635 | __u16 fid; |
| 636 | struct super_block *sb; |
| 637 | struct cifs_sb_info *cifs_sb; |
| 638 | |
| 639 | #ifdef CONFIG_CIFS_DEBUG2 |
| 640 | cFYI(1, ("set ACL for %s from mode 0x%x", path, inode->i_mode)); |
| 641 | #endif |
| 642 | |
| 643 | if (!inode) |
| 644 | return (rc); |
| 645 | |
| 646 | sb = inode->i_sb; |
| 647 | if (sb == NULL) |
| 648 | return (rc); |
| 649 | |
| 650 | cifs_sb = CIFS_SB(sb); |
| 651 | xid = GetXid(); |
| 652 | |
| 653 | open_file = find_readable_file(CIFS_I(inode)); |
| 654 | if (open_file) { |
| 655 | unlock_file = TRUE; |
| 656 | fid = open_file->netfid; |
| 657 | } else { |
| 658 | int oplock = FALSE; |
| 659 | /* open file */ |
| 660 | rc = CIFSSMBOpen(xid, cifs_sb->tcon, path, FILE_OPEN, |
| 661 | WRITE_DAC, 0, &fid, &oplock, NULL, |
| 662 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
| 663 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 664 | if (rc != 0) { |
| 665 | cERROR(1, ("Unable to open file to set ACL")); |
| 666 | FreeXid(xid); |
| 667 | return (rc); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | rc = CIFSSMBSetCIFSACL(xid, cifs_sb->tcon, fid, pnntsd, acllen); |
| 672 | #ifdef CONFIG_CIFS_DEBUG2 |
| 673 | cFYI(1, ("SetCIFSACL rc = %d", rc)); |
| 674 | #endif |
| 675 | if (unlock_file == TRUE) |
| 676 | atomic_dec(&open_file->wrtPending); |
| 677 | else |
| 678 | CIFSSMBClose(xid, cifs_sb->tcon, fid); |
| 679 | |
| 680 | FreeXid(xid); |
| 681 | |
| 682 | return (rc); |
| 683 | } |
| 684 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 685 | /* Translate the CIFS ACL (simlar to NTFS ACL) for a file into mode bits */ |
| 686 | void acl_to_uid_mode(struct inode *inode, const char *path) |
| 687 | { |
| 688 | struct cifs_ntsd *pntsd = NULL; |
| 689 | u32 acllen = 0; |
| 690 | int rc = 0; |
| 691 | |
| 692 | #ifdef CONFIG_CIFS_DEBUG2 |
| 693 | cFYI(1, ("converting ACL to mode for %s", path)); |
| 694 | #endif |
| 695 | pntsd = get_cifs_acl(&acllen, inode, path); |
| 696 | |
| 697 | /* if we can retrieve the ACL, now parse Access Control Entries, ACEs */ |
| 698 | if (pntsd) |
| 699 | rc = parse_sec_desc(pntsd, acllen, inode); |
| 700 | if (rc) |
| 701 | cFYI(1, ("parse sec desc failed rc = %d", rc)); |
| 702 | |
| 703 | kfree(pntsd); |
Steve French | b9c7a2b | 2007-10-26 23:40:20 +0000 | [diff] [blame] | 704 | return; |
| 705 | } |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 706 | |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 707 | /* Convert mode bits to an ACL so we can update the ACL on the server */ |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 708 | int mode_to_acl(struct inode *inode, const char *path, __u64 nmode) |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 709 | { |
| 710 | int rc = 0; |
| 711 | __u32 acllen = 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 712 | struct cifs_ntsd *pntsd = NULL; /* acl obtained from server */ |
| 713 | struct cifs_ntsd *pnntsd = NULL; /* modified acl to be sent to server */ |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 714 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 715 | #ifdef CONFIG_CIFS_DEBUG2 |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 716 | cFYI(1, ("set ACL from mode for %s", path)); |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 717 | #endif |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 718 | |
| 719 | /* Get the security descriptor */ |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 720 | pntsd = get_cifs_acl(&acllen, inode, path); |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 721 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 722 | /* Add three ACEs for owner, group, everyone getting rid of |
| 723 | other ACEs as chmod disables ACEs and set the security descriptor */ |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 724 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 725 | if (pntsd) { |
| 726 | /* allocate memory for the smb header, |
| 727 | set security descriptor request security descriptor |
| 728 | parameters, and secuirty descriptor itself */ |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 729 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 730 | pnntsd = kmalloc(acllen, GFP_KERNEL); |
| 731 | if (!pnntsd) { |
| 732 | cERROR(1, ("Unable to allocate security descriptor")); |
| 733 | kfree(pntsd); |
| 734 | return (-ENOMEM); |
| 735 | } |
Steve French | 7505e05 | 2007-11-01 18:03:01 +0000 | [diff] [blame] | 736 | |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 737 | rc = build_sec_desc(pntsd, pnntsd, acllen, inode, nmode); |
| 738 | |
| 739 | #ifdef CONFIG_CIFS_DEBUG2 |
| 740 | cFYI(1, ("build_sec_desc rc: %d", rc)); |
| 741 | #endif |
| 742 | |
| 743 | if (!rc) { |
| 744 | /* Set the security descriptor */ |
| 745 | rc = set_cifs_acl(pnntsd, acllen, inode, path); |
| 746 | #ifdef CONFIG_CIFS_DEBUG2 |
| 747 | cFYI(1, ("set_cifs_acl rc: %d", rc)); |
| 748 | #endif |
| 749 | } |
| 750 | |
| 751 | kfree(pnntsd); |
| 752 | kfree(pntsd); |
| 753 | } |
| 754 | |
| 755 | return (rc); |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 756 | } |
Steve French | 297647c | 2007-10-12 04:11:59 +0000 | [diff] [blame] | 757 | #endif /* CONFIG_CIFS_EXPERIMENTAL */ |