cifsacl.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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}, {11, 0, 0, 0, 0} }, "net-users"},
  34. {{1, 1, {0, 0, 0, 0, 0, 5}, {18, 0, 0, 0, 0} }, "sys"},
  35. {{1, 2, {0, 0, 0, 0, 0, 5}, {32, 544, 0, 0, 0} }, "root"},
  36. {{1, 2, {0, 0, 0, 0, 0, 5}, {32, 545, 0, 0, 0} }, "users"},
  37. {{1, 2, {0, 0, 0, 0, 0, 5}, {32, 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 = cpu_to_le32(ctsid->num_subauth);
  66. num_saw = cpu_to_le32(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. int compare_sids(struct cifs_sid *ctsid, struct cifs_sid *cwsid)
  83. {
  84. int i;
  85. int num_subauth, num_sat, num_saw;
  86. if ((!ctsid) || (!cwsid))
  87. return (-1);
  88. /* compare the revision */
  89. if (ctsid->revision != cwsid->revision)
  90. return (-1);
  91. /* compare all of the six auth values */
  92. for (i = 0; i < 6; ++i) {
  93. if (ctsid->authority[i] != cwsid->authority[i])
  94. return (-1);
  95. }
  96. /* compare all of the subauth values if any */
  97. num_sat = cpu_to_le32(ctsid->num_subauth);
  98. num_saw = cpu_to_le32(cwsid->num_subauth);
  99. num_subauth = num_sat < num_saw ? num_sat : num_saw;
  100. if (num_subauth) {
  101. for (i = 0; i < num_subauth; ++i) {
  102. if (ctsid->sub_auth[i] != cwsid->sub_auth[i])
  103. return (-1);
  104. }
  105. }
  106. return (0); /* sids compare/match */
  107. }
  108. static void parse_ace(struct cifs_ace *pace, char *end_of_acl)
  109. {
  110. int num_subauth;
  111. /* validate that we do not go past end of acl */
  112. /* XXX this if statement can be removed
  113. if (end_of_acl < (char *)pace + sizeof(struct cifs_ace)) {
  114. cERROR(1, ("ACL too small to parse ACE"));
  115. return;
  116. } */
  117. num_subauth = cpu_to_le32(pace->num_subauth);
  118. if (num_subauth) {
  119. #ifdef CONFIG_CIFS_DEBUG2
  120. int i;
  121. cFYI(1, ("ACE revision %d num_subauth %d",
  122. pace->revision, pace->num_subauth));
  123. for (i = 0; i < num_subauth; ++i) {
  124. cFYI(1, ("ACE sub_auth[%d]: 0x%x", i,
  125. le32_to_cpu(pace->sub_auth[i])));
  126. }
  127. /* BB add length check to make sure that we do not have huge
  128. num auths and therefore go off the end */
  129. cFYI(1, ("RID %d", le32_to_cpu(pace->sub_auth[num_subauth-1])));
  130. #endif
  131. }
  132. return;
  133. }
  134. static void parse_ntace(struct cifs_ntace *pntace, char *end_of_acl)
  135. {
  136. /* validate that we do not go past end of acl */
  137. if (end_of_acl < (char *)pntace + sizeof(struct cifs_ntace)) {
  138. cERROR(1, ("ACL too small to parse NT ACE"));
  139. return;
  140. }
  141. #ifdef CONFIG_CIFS_DEBUG2
  142. cFYI(1, ("NTACE type %d flags 0x%x size %d, access Req 0x%x",
  143. pntace->type, pntace->flags, pntace->size,
  144. pntace->access_req));
  145. #endif
  146. return;
  147. }
  148. static void parse_dacl(struct cifs_acl *pdacl, char *end_of_acl)
  149. {
  150. int i;
  151. int num_aces = 0;
  152. int acl_size;
  153. char *acl_base;
  154. struct cifs_ntace **ppntace;
  155. struct cifs_ace **ppace;
  156. /* BB need to add parm so we can store the SID BB */
  157. /* validate that we do not go past end of acl */
  158. if (end_of_acl < (char *)pdacl + le16_to_cpu(pdacl->size)) {
  159. cERROR(1, ("ACL too small to parse DACL"));
  160. return;
  161. }
  162. #ifdef CONFIG_CIFS_DEBUG2
  163. cFYI(1, ("DACL revision %d size %d num aces %d",
  164. le16_to_cpu(pdacl->revision), le16_to_cpu(pdacl->size),
  165. le32_to_cpu(pdacl->num_aces)));
  166. #endif
  167. acl_base = (char *)pdacl;
  168. acl_size = sizeof(struct cifs_acl);
  169. num_aces = cpu_to_le32(pdacl->num_aces);
  170. if (num_aces > 0) {
  171. ppntace = kmalloc(num_aces * sizeof(struct cifs_ntace *),
  172. GFP_KERNEL);
  173. ppace = kmalloc(num_aces * sizeof(struct cifs_ace *),
  174. GFP_KERNEL);
  175. /* cifscred->cecount = pdacl->num_aces;
  176. cifscred->ntaces = kmalloc(num_aces *
  177. sizeof(struct cifs_ntace *), GFP_KERNEL);
  178. cifscred->aces = kmalloc(num_aces *
  179. sizeof(struct cifs_ace *), GFP_KERNEL);*/
  180. for (i = 0; i < num_aces; ++i) {
  181. ppntace[i] = (struct cifs_ntace *)
  182. (acl_base + acl_size);
  183. ppace[i] = (struct cifs_ace *) ((char *)ppntace[i] +
  184. sizeof(struct cifs_ntace));
  185. parse_ntace(ppntace[i], end_of_acl);
  186. if (end_of_acl < ((char *)ppace[i] +
  187. (ppntace[i]->size -
  188. sizeof(struct cifs_ntace)))) {
  189. cERROR(1, ("ACL too small to parse ACE"));
  190. break;
  191. } else
  192. parse_ace(ppace[i], end_of_acl);
  193. /* memcpy((void *)(&(cifscred->ntaces[i])),
  194. (void *)ppntace[i],
  195. sizeof(struct cifs_ntace));
  196. memcpy((void *)(&(cifscred->aces[i])),
  197. (void *)ppace[i],
  198. sizeof(struct cifs_ace)); */
  199. acl_base = (char *)ppntace[i];
  200. acl_size = cpu_to_le32(ppntace[i]->size);
  201. }
  202. kfree(ppace);
  203. kfree(ppntace);
  204. }
  205. return;
  206. }
  207. static int parse_sid(struct cifs_sid *psid, char *end_of_acl)
  208. {
  209. /* BB need to add parm so we can store the SID BB */
  210. /* validate that we do not go past end of acl */
  211. if (end_of_acl < (char *)psid + sizeof(struct cifs_sid)) {
  212. cERROR(1, ("ACL too small to parse SID"));
  213. return -EINVAL;
  214. }
  215. if (psid->num_subauth) {
  216. #ifdef CONFIG_CIFS_DEBUG2
  217. int i;
  218. cFYI(1, ("SID revision %d num_auth %d First subauth 0x%x",
  219. psid->revision, psid->num_subauth, psid->sub_auth[0]));
  220. for (i = 0; i < psid->num_subauth; i++) {
  221. cFYI(1, ("SID sub_auth[%d]: 0x%x ", i,
  222. le32_to_cpu(psid->sub_auth[i])));
  223. }
  224. /* BB add length check to make sure that we do not have huge
  225. num auths and therefore go off the end */
  226. cFYI(1, ("RID 0x%x",
  227. le32_to_cpu(psid->sub_auth[psid->num_subauth-1])));
  228. #endif
  229. }
  230. return 0;
  231. }
  232. /* Convert CIFS ACL to POSIX form */
  233. int parse_sec_desc(struct cifs_ntsd *pntsd, int acl_len)
  234. {
  235. int rc;
  236. struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
  237. struct cifs_acl *dacl_ptr; /* no need for SACL ptr */
  238. char *end_of_acl = ((char *)pntsd) + acl_len;
  239. owner_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  240. le32_to_cpu(pntsd->osidoffset));
  241. group_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  242. le32_to_cpu(pntsd->gsidoffset));
  243. dacl_ptr = (struct cifs_acl *)((char *)pntsd +
  244. le32_to_cpu(pntsd->dacloffset));
  245. #ifdef CONFIG_CIFS_DEBUG2
  246. cFYI(1, ("revision %d type 0x%x ooffset 0x%x goffset 0x%x "
  247. "sacloffset 0x%x dacloffset 0x%x",
  248. pntsd->revision, pntsd->type, le32_to_cpu(pntsd->osidoffset),
  249. le32_to_cpu(pntsd->gsidoffset),
  250. le32_to_cpu(pntsd->sacloffset),
  251. le32_to_cpu(pntsd->dacloffset));
  252. #endif
  253. rc = parse_sid(owner_sid_ptr, end_of_acl);
  254. if (rc)
  255. return rc;
  256. rc = parse_sid(group_sid_ptr, end_of_acl);
  257. if (rc)
  258. return rc;
  259. parse_dacl(dacl_ptr, end_of_acl);
  260. /* cifscred->uid = owner_sid_ptr->rid;
  261. cifscred->gid = group_sid_ptr->rid;
  262. memcpy((void *)(&(cifscred->osid)), (void *)owner_sid_ptr,
  263. sizeof (struct cifs_sid));
  264. memcpy((void *)(&(cifscred->gsid)), (void *)group_sid_ptr,
  265. sizeof (struct cifs_sid)); */
  266. return (0);
  267. }
  268. #endif /* CONFIG_CIFS_EXPERIMENTAL */