cifsacl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. #include <linux/fs.h>
  24. #include "cifspdu.h"
  25. #include "cifsglob.h"
  26. #include "cifsacl.h"
  27. #include "cifsproto.h"
  28. #include "cifs_debug.h"
  29. #ifdef CONFIG_CIFS_EXPERIMENTAL
  30. static struct cifs_wksid wksidarr[NUM_WK_SIDS] = {
  31. {{1, 0, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0} }, "null user"},
  32. {{1, 1, {0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0} }, "nobody"},
  33. {{1, 1, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(11), 0, 0, 0, 0} }, "net-users"},
  34. {{1, 1, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(18), 0, 0, 0, 0} }, "sys"},
  35. {{1, 2, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(32), cpu_to_le32(544), 0, 0, 0} }, "root"},
  36. {{1, 2, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(32), cpu_to_le32(545), 0, 0, 0} }, "users"},
  37. {{1, 2, {0, 0, 0, 0, 0, 5}, {cpu_to_le32(32), cpu_to_le32(546), 0, 0, 0} }, "guest"} }
  38. ;
  39. /* security id for everyone */
  40. static const struct cifs_sid sid_everyone = {
  41. 1, 1, {0, 0, 0, 0, 0, 1}, {0} };
  42. /* group users */
  43. static const struct cifs_sid sid_user =
  44. {1, 2 , {0, 0, 0, 0, 0, 5}, {} };
  45. int match_sid(struct cifs_sid *ctsid)
  46. {
  47. int i, j;
  48. int num_subauth, num_sat, num_saw;
  49. struct cifs_sid *cwsid;
  50. if (!ctsid)
  51. return (-1);
  52. for (i = 0; i < NUM_WK_SIDS; ++i) {
  53. cwsid = &(wksidarr[i].cifssid);
  54. /* compare the revision */
  55. if (ctsid->revision != cwsid->revision)
  56. continue;
  57. /* compare all of the six auth values */
  58. for (j = 0; j < 6; ++j) {
  59. if (ctsid->authority[j] != cwsid->authority[j])
  60. break;
  61. }
  62. if (j < 6)
  63. continue; /* all of the auth values did not match */
  64. /* compare all of the subauth values if any */
  65. num_sat = ctsid->num_subauth;
  66. num_saw = cwsid->num_subauth;
  67. num_subauth = num_sat < num_saw ? num_sat : num_saw;
  68. if (num_subauth) {
  69. for (j = 0; j < num_subauth; ++j) {
  70. if (ctsid->sub_auth[j] != cwsid->sub_auth[j])
  71. break;
  72. }
  73. if (j < num_subauth)
  74. continue; /* all sub_auth values do not match */
  75. }
  76. cFYI(1, ("matching sid: %s\n", wksidarr[i].sidname));
  77. return (0); /* sids compare/match */
  78. }
  79. cFYI(1, ("No matching sid"));
  80. return (-1);
  81. }
  82. /* if the two SIDs (roughly equivalent to a UUID for a user or group) are
  83. the same returns 1, if they do not match returns 0 */
  84. int compare_sids(const struct cifs_sid *ctsid, const struct cifs_sid *cwsid)
  85. {
  86. int i;
  87. int num_subauth, num_sat, num_saw;
  88. if ((!ctsid) || (!cwsid))
  89. return (0);
  90. /* compare the revision */
  91. if (ctsid->revision != cwsid->revision)
  92. return (0);
  93. /* compare all of the six auth values */
  94. for (i = 0; i < 6; ++i) {
  95. if (ctsid->authority[i] != cwsid->authority[i])
  96. return (0);
  97. }
  98. /* compare all of the subauth values if any */
  99. num_sat = ctsid->num_subauth;
  100. num_saw = cwsid->num_subauth;
  101. num_subauth = num_sat < num_saw ? num_sat : num_saw;
  102. if (num_subauth) {
  103. for (i = 0; i < num_subauth; ++i) {
  104. if (ctsid->sub_auth[i] != cwsid->sub_auth[i])
  105. return (0);
  106. }
  107. }
  108. return (1); /* sids compare/match */
  109. }
  110. /*
  111. change posix mode to reflect permissions
  112. pmode is the existing mode (we only want to overwrite part of this
  113. bits to set can be: S_IRWXU, S_IRWXG or S_IRWXO ie 00700 or 00070 or 00007
  114. */
  115. static void access_flags_to_mode(__u32 ace_flags, umode_t *pmode,
  116. umode_t bits_to_set)
  117. {
  118. if (ace_flags & GENERIC_ALL) {
  119. *pmode |= (S_IRWXUGO & bits_to_set);
  120. #ifdef CONFIG_CIFS_DEBUG2
  121. cFYI(1, ("all perms"));
  122. #endif
  123. return;
  124. }
  125. if ((ace_flags & GENERIC_WRITE) ||
  126. ((ace_flags & FILE_WRITE_RIGHTS) == FILE_WRITE_RIGHTS))
  127. *pmode |= (S_IWUGO & bits_to_set);
  128. if ((ace_flags & GENERIC_READ) ||
  129. ((ace_flags & FILE_READ_RIGHTS) == FILE_READ_RIGHTS))
  130. *pmode |= (S_IRUGO & bits_to_set);
  131. if ((ace_flags & GENERIC_EXECUTE) ||
  132. ((ace_flags & FILE_EXEC_RIGHTS) == FILE_EXEC_RIGHTS))
  133. *pmode |= (S_IXUGO & bits_to_set);
  134. #ifdef CONFIG_CIFS_DEBUG2
  135. cFYI(1, ("access flags 0x%x mode now 0x%x", ace_flags, *pmode));
  136. #endif
  137. return;
  138. }
  139. #ifdef CONFIG_CIFS_DEBUG2
  140. static void dump_ace(struct cifs_ace *pace, char *end_of_acl)
  141. {
  142. int num_subauth;
  143. /* validate that we do not go past end of acl */
  144. if (le16_to_cpu(pace->size) < 16) {
  145. cERROR(1, ("ACE too small, %d", le16_to_cpu(pace->size)));
  146. return;
  147. }
  148. if (end_of_acl < (char *)pace + le16_to_cpu(pace->size)) {
  149. cERROR(1, ("ACL too small to parse ACE"));
  150. return;
  151. }
  152. num_subauth = pace->sid.num_subauth;
  153. if (num_subauth) {
  154. int i;
  155. cFYI(1, ("ACE revision %d num_auth %d type %d flags %d size %d",
  156. pace->sid.revision, pace->sid.num_subauth, pace->type,
  157. pace->flags, pace->size));
  158. for (i = 0; i < num_subauth; ++i) {
  159. cFYI(1, ("ACE sub_auth[%d]: 0x%x", i,
  160. le32_to_cpu(pace->sid.sub_auth[i])));
  161. }
  162. /* BB add length check to make sure that we do not have huge
  163. num auths and therefore go off the end */
  164. }
  165. return;
  166. }
  167. #endif
  168. static void parse_dacl(struct cifs_acl *pdacl, char *end_of_acl,
  169. struct cifs_sid *pownersid, struct cifs_sid *pgrpsid,
  170. struct inode *inode)
  171. {
  172. int i;
  173. int num_aces = 0;
  174. int acl_size;
  175. char *acl_base;
  176. struct cifs_ace **ppace;
  177. /* BB need to add parm so we can store the SID BB */
  178. /* validate that we do not go past end of acl */
  179. if (end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) {
  180. cERROR(1, ("ACL too small to parse DACL"));
  181. return;
  182. }
  183. #ifdef CONFIG_CIFS_DEBUG2
  184. cFYI(1, ("DACL revision %d size %d num aces %d",
  185. le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size),
  186. le32_to_cpu(pdacl->num_aces)));
  187. #endif
  188. /* reset rwx permissions for user/group/other.
  189. Also, if num_aces is 0 i.e. DACL has no ACEs,
  190. user/group/other have no permissions */
  191. inode->i_mode &= ~(S_IRWXUGO);
  192. if (!pdacl) {
  193. /* no DACL in the security descriptor, set
  194. all the permissions for user/group/other */
  195. inode->i_mode |= S_IRWXUGO;
  196. return;
  197. }
  198. acl_base = (char *)pdacl;
  199. acl_size = sizeof(struct cifs_acl);
  200. num_aces = le32_to_cpu(pdacl->num_aces);
  201. if (num_aces > 0) {
  202. ppace = kmalloc(num_aces * sizeof(struct cifs_ace *),
  203. GFP_KERNEL);
  204. /* cifscred->cecount = pdacl->num_aces;
  205. cifscred->aces = kmalloc(num_aces *
  206. sizeof(struct cifs_ace *), GFP_KERNEL);*/
  207. for (i = 0; i < num_aces; ++i) {
  208. ppace[i] = (struct cifs_ace *) (acl_base + acl_size);
  209. #ifdef CONFIG_CIFS_DEBUG2
  210. dump_ace(ppace[i], end_of_acl);
  211. #endif
  212. if (compare_sids(&(ppace[i]->sid), pownersid))
  213. access_flags_to_mode(ppace[i]->access_req,
  214. &(inode->i_mode), S_IRWXU);
  215. if (compare_sids(&(ppace[i]->sid), pgrpsid))
  216. access_flags_to_mode(ppace[i]->access_req,
  217. &(inode->i_mode), S_IRWXG);
  218. if (compare_sids(&(ppace[i]->sid), &sid_everyone))
  219. access_flags_to_mode(ppace[i]->access_req,
  220. &(inode->i_mode), S_IRWXO);
  221. /* memcpy((void *)(&(cifscred->aces[i])),
  222. (void *)ppace[i],
  223. sizeof(struct cifs_ace)); */
  224. acl_base = (char *)ppace[i];
  225. acl_size = le16_to_cpu(ppace[i]->size);
  226. }
  227. kfree(ppace);
  228. }
  229. return;
  230. }
  231. static int parse_sid(struct cifs_sid *psid, char *end_of_acl)
  232. {
  233. /* BB need to add parm so we can store the SID BB */
  234. /* validate that we do not go past end of ACL - sid must be at least 8
  235. bytes long (assuming no sub-auths - e.g. the null SID */
  236. if (end_of_acl < (char *)psid + 8) {
  237. cERROR(1, ("ACL too small to parse SID %p", psid));
  238. return -EINVAL;
  239. }
  240. if (psid->num_subauth) {
  241. #ifdef CONFIG_CIFS_DEBUG2
  242. int i;
  243. cFYI(1, ("SID revision %d num_auth %d",
  244. psid->revision, psid->num_subauth));
  245. for (i = 0; i < psid->num_subauth; i++) {
  246. cFYI(1, ("SID sub_auth[%d]: 0x%x ", i,
  247. le32_to_cpu(psid->sub_auth[i])));
  248. }
  249. /* BB add length check to make sure that we do not have huge
  250. num auths and therefore go off the end */
  251. cFYI(1, ("RID 0x%x",
  252. le32_to_cpu(psid->sub_auth[psid->num_subauth-1])));
  253. #endif
  254. }
  255. return 0;
  256. }
  257. /* Convert CIFS ACL to POSIX form */
  258. static int parse_sec_desc(struct cifs_ntsd *pntsd, int acl_len,
  259. struct inode *inode)
  260. {
  261. int rc;
  262. struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
  263. struct cifs_acl *dacl_ptr; /* no need for SACL ptr */
  264. char *end_of_acl = ((char *)pntsd) + acl_len;
  265. __u32 dacloffset;
  266. if ((inode == NULL) || (pntsd == NULL))
  267. return -EIO;
  268. owner_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  269. le32_to_cpu(pntsd->osidoffset));
  270. group_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  271. le32_to_cpu(pntsd->gsidoffset));
  272. dacloffset = le32_to_cpu(pntsd->dacloffset);
  273. dacl_ptr = (struct cifs_acl *)((char *)pntsd + dacloffset);
  274. #ifdef CONFIG_CIFS_DEBUG2
  275. cFYI(1, ("revision %d type 0x%x ooffset 0x%x goffset 0x%x "
  276. "sacloffset 0x%x dacloffset 0x%x",
  277. pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset),
  278. le32_to_cpu(pntsd->gsidoffset),
  279. le32_to_cpu(pntsd->sacloffset), dacloffset));
  280. #endif
  281. /* cifs_dump_mem("owner_sid: ", owner_sid_ptr, 64); */
  282. rc = parse_sid(owner_sid_ptr, end_of_acl);
  283. if (rc)
  284. return rc;
  285. rc = parse_sid(group_sid_ptr, end_of_acl);
  286. if (rc)
  287. return rc;
  288. if (dacloffset)
  289. parse_dacl(dacl_ptr, end_of_acl, owner_sid_ptr,
  290. group_sid_ptr, inode);
  291. else
  292. cFYI(1, ("no ACL")); /* BB grant all or default perms? */
  293. /* cifscred->uid = owner_sid_ptr->rid;
  294. cifscred->gid = group_sid_ptr->rid;
  295. memcpy((void *)(&(cifscred->osid)), (void *)owner_sid_ptr,
  296. sizeof(struct cifs_sid));
  297. memcpy((void *)(&(cifscred->gsid)), (void *)group_sid_ptr,
  298. sizeof(struct cifs_sid)); */
  299. return (0);
  300. }
  301. /* Retrieve an ACL from the server */
  302. static struct cifs_ntsd *get_cifs_acl(u32 *pacllen, struct inode *inode,
  303. const char *path)
  304. {
  305. struct cifsFileInfo *open_file;
  306. int unlock_file = FALSE;
  307. int xid;
  308. int rc = -EIO;
  309. __u16 fid;
  310. struct super_block *sb;
  311. struct cifs_sb_info *cifs_sb;
  312. struct cifs_ntsd *pntsd = NULL;
  313. cFYI(1, ("get mode from ACL for %s", path));
  314. if (inode == NULL)
  315. return NULL;
  316. xid = GetXid();
  317. open_file = find_readable_file(CIFS_I(inode));
  318. sb = inode->i_sb;
  319. if (sb == NULL) {
  320. FreeXid(xid);
  321. return NULL;
  322. }
  323. cifs_sb = CIFS_SB(sb);
  324. if (open_file) {
  325. unlock_file = TRUE;
  326. fid = open_file->netfid;
  327. } else {
  328. int oplock = FALSE;
  329. /* open file */
  330. rc = CIFSSMBOpen(xid, cifs_sb->tcon, path, FILE_OPEN,
  331. READ_CONTROL, 0, &fid, &oplock, NULL,
  332. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  333. CIFS_MOUNT_MAP_SPECIAL_CHR);
  334. if (rc != 0) {
  335. cERROR(1, ("Unable to open file to get ACL"));
  336. FreeXid(xid);
  337. return NULL;
  338. }
  339. }
  340. rc = CIFSSMBGetCIFSACL(xid, cifs_sb->tcon, fid, &pntsd, pacllen);
  341. cFYI(1, ("GetCIFSACL rc = %d ACL len %d", rc, *pacllen));
  342. if (unlock_file == TRUE)
  343. atomic_dec(&open_file->wrtPending);
  344. else
  345. CIFSSMBClose(xid, cifs_sb->tcon, fid);
  346. FreeXid(xid);
  347. return pntsd;
  348. }
  349. /* Translate the CIFS ACL (simlar to NTFS ACL) for a file into mode bits */
  350. void acl_to_uid_mode(struct inode *inode, const char *path)
  351. {
  352. struct cifs_ntsd *pntsd = NULL;
  353. u32 acllen = 0;
  354. int rc = 0;
  355. #ifdef CONFIG_CIFS_DEBUG2
  356. cFYI(1, ("converting ACL to mode for %s", path));
  357. #endif
  358. pntsd = get_cifs_acl(&acllen, inode, path);
  359. /* if we can retrieve the ACL, now parse Access Control Entries, ACEs */
  360. if (pntsd)
  361. rc = parse_sec_desc(pntsd, acllen, inode);
  362. if (rc)
  363. cFYI(1, ("parse sec desc failed rc = %d", rc));
  364. kfree(pntsd);
  365. return;
  366. }
  367. /* Convert mode bits to an ACL so we can update the ACL on the server */
  368. int mode_to_acl(struct inode *inode, const char *path)
  369. {
  370. int rc = 0;
  371. __u32 acllen = 0;
  372. struct cifs_ntsd *pntsd = NULL;
  373. cFYI(1, ("set ACL from mode for %s", path));
  374. /* Get the security descriptor */
  375. pntsd = get_cifs_acl(&acllen, inode, path);
  376. /* Add/Modify the three ACEs for owner, group, everyone
  377. while retaining the other ACEs */
  378. /* Set the security descriptor */
  379. kfree(pntsd);
  380. return rc;
  381. }
  382. #endif /* CONFIG_CIFS_EXPERIMENTAL */