cifsacl.c 9.6 KB

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