cifsacl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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, 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. *pmode &= ~bits_to_set;
  119. if (ace_flags & GENERIC_ALL) {
  120. *pmode |= (S_IRWXUGO & bits_to_set);
  121. #ifdef CONFIG_CIFS_DEBUG2
  122. cFYI(1, ("all perms"));
  123. #endif
  124. return;
  125. }
  126. if ((ace_flags & GENERIC_WRITE) || (ace_flags & FILE_WRITE_RIGHTS))
  127. *pmode |= (S_IWUGO & bits_to_set);
  128. if ((ace_flags & GENERIC_READ) || (ace_flags & FILE_READ_RIGHTS))
  129. *pmode |= (S_IRUGO & bits_to_set);
  130. if ((ace_flags & GENERIC_EXECUTE) || (ace_flags & FILE_EXEC_RIGHTS))
  131. *pmode |= (S_IXUGO & bits_to_set);
  132. #ifdef CONFIG_CIFS_DEBUG2
  133. cFYI(1, ("access flags 0x%x mode now 0x%x", ace_flags, *pmode));
  134. #endif
  135. return;
  136. }
  137. static void parse_ace(struct cifs_ace *pace, char *end_of_acl)
  138. {
  139. int num_subauth;
  140. /* validate that we do not go past end of acl */
  141. if (le16_to_cpu(pace->size) < 16) {
  142. cERROR(1, ("ACE too small, %d", le16_to_cpu(pace->size)));
  143. return;
  144. }
  145. if (end_of_acl < (char *)pace + le16_to_cpu(pace->size)) {
  146. cERROR(1, ("ACL too small to parse ACE"));
  147. return;
  148. }
  149. num_subauth = pace->sid.num_subauth;
  150. if (num_subauth) {
  151. #ifdef CONFIG_CIFS_DEBUG2
  152. int i;
  153. cFYI(1, ("ACE revision %d num_auth %d type %d flags %d size %d",
  154. pace->sid.revision, pace->sid.num_subauth, pace->type,
  155. pace->flags, pace->size));
  156. for (i = 0; i < num_subauth; ++i) {
  157. cFYI(1, ("ACE sub_auth[%d]: 0x%x", i,
  158. le32_to_cpu(pace->sid.sub_auth[i])));
  159. }
  160. /* BB add length check to make sure that we do not have huge
  161. num auths and therefore go off the end */
  162. #endif
  163. }
  164. return;
  165. }
  166. static void parse_dacl(struct cifs_acl *pdacl, char *end_of_acl,
  167. struct cifs_sid *pownersid, struct cifs_sid *pgrpsid,
  168. struct inode *inode)
  169. {
  170. int i;
  171. int num_aces = 0;
  172. int acl_size;
  173. char *acl_base;
  174. struct cifs_ace **ppace;
  175. /* BB need to add parm so we can store the SID BB */
  176. /* validate that we do not go past end of acl */
  177. if (end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) {
  178. cERROR(1, ("ACL too small to parse DACL"));
  179. return;
  180. }
  181. #ifdef CONFIG_CIFS_DEBUG2
  182. cFYI(1, ("DACL revision %d size %d num aces %d",
  183. le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size),
  184. le32_to_cpu(pdacl->num_aces)));
  185. #endif
  186. acl_base = (char *)pdacl;
  187. acl_size = sizeof(struct cifs_acl);
  188. num_aces = le32_to_cpu(pdacl->num_aces);
  189. if (num_aces > 0) {
  190. ppace = kmalloc(num_aces * sizeof(struct cifs_ace *),
  191. GFP_KERNEL);
  192. /* cifscred->cecount = pdacl->num_aces;
  193. cifscred->aces = kmalloc(num_aces *
  194. sizeof(struct cifs_ace *), GFP_KERNEL);*/
  195. for (i = 0; i < num_aces; ++i) {
  196. ppace[i] = (struct cifs_ace *) (acl_base + acl_size);
  197. parse_ace(ppace[i], end_of_acl);
  198. /* memcpy((void *)(&(cifscred->aces[i])),
  199. (void *)ppace[i],
  200. sizeof(struct cifs_ace)); */
  201. acl_base = (char *)ppace[i];
  202. acl_size = le16_to_cpu(ppace[i]->size);
  203. }
  204. kfree(ppace);
  205. }
  206. return;
  207. }
  208. static int parse_sid(struct cifs_sid *psid, char *end_of_acl)
  209. {
  210. /* BB need to add parm so we can store the SID BB */
  211. /* validate that we do not go past end of ACL - sid must be at least 8
  212. bytes long (assuming no sub-auths - e.g. the null SID */
  213. if (end_of_acl < (char *)psid + 8) {
  214. cERROR(1, ("ACL too small to parse SID %p", psid));
  215. return -EINVAL;
  216. }
  217. if (psid->num_subauth) {
  218. #ifdef CONFIG_CIFS_DEBUG2
  219. int i;
  220. cFYI(1, ("SID revision %d num_auth %d",
  221. psid->revision, psid->num_subauth));
  222. for (i = 0; i < psid->num_subauth; i++) {
  223. cFYI(1, ("SID sub_auth[%d]: 0x%x ", i,
  224. le32_to_cpu(psid->sub_auth[i])));
  225. }
  226. /* BB add length check to make sure that we do not have huge
  227. num auths and therefore go off the end */
  228. cFYI(1, ("RID 0x%x",
  229. le32_to_cpu(psid->sub_auth[psid->num_subauth-1])));
  230. #endif
  231. }
  232. return 0;
  233. }
  234. /* Convert CIFS ACL to POSIX form */
  235. static int parse_sec_desc(struct cifs_ntsd *pntsd, int acl_len,
  236. struct inode *inode)
  237. {
  238. int rc;
  239. struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
  240. struct cifs_acl *dacl_ptr; /* no need for SACL ptr */
  241. char *end_of_acl = ((char *)pntsd) + acl_len;
  242. if ((inode == NULL) || (pntsd == NULL))
  243. return -EIO;
  244. owner_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  245. le32_to_cpu(pntsd->osidoffset));
  246. group_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  247. le32_to_cpu(pntsd->gsidoffset));
  248. dacl_ptr = (struct cifs_acl *)((char *)pntsd +
  249. le32_to_cpu(pntsd->dacloffset));
  250. #ifdef CONFIG_CIFS_DEBUG2
  251. cFYI(1, ("revision %d type 0x%x ooffset 0x%x goffset 0x%x "
  252. "sacloffset 0x%x dacloffset 0x%x",
  253. pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset),
  254. le32_to_cpu(pntsd->gsidoffset),
  255. le32_to_cpu(pntsd->sacloffset),
  256. le32_to_cpu(pntsd->dacloffset)));
  257. #endif
  258. /* cifs_dump_mem("owner_sid: ", owner_sid_ptr, 64); */
  259. rc = parse_sid(owner_sid_ptr, end_of_acl);
  260. if (rc)
  261. return rc;
  262. rc = parse_sid(group_sid_ptr, end_of_acl);
  263. if (rc)
  264. return rc;
  265. parse_dacl(dacl_ptr, end_of_acl, owner_sid_ptr, group_sid_ptr, inode);
  266. /* cifscred->uid = owner_sid_ptr->rid;
  267. cifscred->gid = group_sid_ptr->rid;
  268. memcpy((void *)(&(cifscred->osid)), (void *)owner_sid_ptr,
  269. sizeof(struct cifs_sid));
  270. memcpy((void *)(&(cifscred->gsid)), (void *)group_sid_ptr,
  271. sizeof(struct cifs_sid)); */
  272. return (0);
  273. }
  274. /* Translate the CIFS ACL (simlar to NTFS ACL) for a file into mode bits */
  275. void acl_to_uid_mode(struct inode *inode, const char *path)
  276. {
  277. struct cifsFileInfo *open_file;
  278. int unlock_file = FALSE;
  279. int xid;
  280. int rc = -EIO;
  281. __u16 fid;
  282. struct super_block *sb;
  283. struct cifs_sb_info *cifs_sb;
  284. struct cifs_ntsd *pntsd = NULL;
  285. __u32 acllen;
  286. cFYI(1, ("get mode from ACL for %s", path));
  287. if (inode == NULL)
  288. return;
  289. xid = GetXid();
  290. open_file = find_readable_file(CIFS_I(inode));
  291. sb = inode->i_sb;
  292. if (sb == NULL) {
  293. FreeXid(xid);
  294. return;
  295. }
  296. cifs_sb = CIFS_SB(sb);
  297. if (open_file) {
  298. unlock_file = TRUE;
  299. fid = open_file->netfid;
  300. } else {
  301. int oplock = FALSE;
  302. /* open file */
  303. rc = CIFSSMBOpen(xid, cifs_sb->tcon, path, FILE_OPEN,
  304. GENERIC_READ, 0, &fid, &oplock, NULL,
  305. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  306. CIFS_MOUNT_MAP_SPECIAL_CHR);
  307. if (rc != 0) {
  308. cERROR(1, ("Unable to open file to get ACL"));
  309. FreeXid(xid);
  310. return;
  311. }
  312. }
  313. rc = CIFSSMBGetCIFSACL(xid, cifs_sb->tcon, fid, &pntsd, &acllen);
  314. cFYI(1, ("GetCIFSACL rc = %d ACL len %d", rc, acllen));
  315. if (unlock_file == TRUE)
  316. atomic_dec(&open_file->wrtPending);
  317. else
  318. CIFSSMBClose(xid, cifs_sb->tcon, fid);
  319. /* parse ACEs */
  320. if (!rc)
  321. rc = parse_sec_desc(pntsd, acllen, inode);
  322. kfree(pntsd);
  323. FreeXid(xid);
  324. return;
  325. }
  326. #endif /* CONFIG_CIFS_EXPERIMENTAL */