cifsacl.c 8.9 KB

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