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. 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 + 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. pdacl->revision, pdacl->size, pdacl->num_aces));
  165. #endif
  166. acl_base = (char *)pdacl;
  167. acl_size = sizeof(struct cifs_acl);
  168. num_aces = cpu_to_le32(pdacl->num_aces);
  169. if (num_aces > 0) {
  170. ppntace = kmalloc(num_aces * sizeof(struct cifs_ntace *),
  171. GFP_KERNEL);
  172. ppace = kmalloc(num_aces * sizeof(struct cifs_ace *),
  173. GFP_KERNEL);
  174. /* cifscred->cecount = pdacl->num_aces;
  175. cifscred->ntaces = kmalloc(num_aces *
  176. sizeof(struct cifs_ntace *), GFP_KERNEL);
  177. cifscred->aces = kmalloc(num_aces *
  178. sizeof(struct cifs_ace *), GFP_KERNEL);*/
  179. for (i = 0; i < num_aces; ++i) {
  180. ppntace[i] = (struct cifs_ntace *)
  181. (acl_base + acl_size);
  182. ppace[i] = (struct cifs_ace *) ((char *)ppntace[i] +
  183. sizeof(struct cifs_ntace));
  184. parse_ntace(ppntace[i], end_of_acl);
  185. if (end_of_acl < ((char *)ppace[i] +
  186. (ppntace[i]->size -
  187. sizeof(struct cifs_ntace)))) {
  188. cERROR(1, ("ACL too small to parse ACE"));
  189. break;
  190. } else
  191. parse_ace(ppace[i], end_of_acl);
  192. /* memcpy((void *)(&(cifscred->ntaces[i])),
  193. (void *)ppntace[i],
  194. sizeof(struct cifs_ntace));
  195. memcpy((void *)(&(cifscred->aces[i])),
  196. (void *)ppace[i],
  197. sizeof(struct cifs_ace)); */
  198. acl_base = (char *)ppntace[i];
  199. acl_size = cpu_to_le32(ppntace[i]->size);
  200. }
  201. kfree(ppace);
  202. kfree(ppntace);
  203. }
  204. return;
  205. }
  206. static int parse_sid(struct cifs_sid *psid, char *end_of_acl)
  207. {
  208. int num_subauth;
  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. num_subauth = cpu_to_le32(psid->num_subauth);
  216. if (num_subauth) {
  217. #ifdef CONFIG_CIFS_DEBUG2
  218. int i;
  219. cFYI(1, ("SID revision %d num_auth %d First subauth 0x%x",
  220. psid->revision, psid->num_subauth, psid->sub_auth[0]));
  221. for (i = 0; i < num_subauth; ++i) {
  222. cFYI(1, ("SID sub_auth[%d]: 0x%x ", i,
  223. le32_to_cpu(psid->sub_auth[i])));
  224. }
  225. /* BB add length check to make sure that we do not have huge
  226. num auths and therefore go off the end */
  227. cFYI(1, ("RID 0x%x",
  228. le32_to_cpu(psid->sub_auth[num_subauth-1])));
  229. #endif
  230. }
  231. return 0;
  232. }
  233. /* Convert CIFS ACL to POSIX form */
  234. int parse_sec_desc(struct cifs_ntsd *pntsd, int acl_len)
  235. {
  236. int rc;
  237. struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
  238. struct cifs_acl *dacl_ptr; /* no need for SACL ptr */
  239. char *end_of_acl = ((char *)pntsd) + acl_len;
  240. owner_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  241. cpu_to_le32(pntsd->osidoffset));
  242. group_sid_ptr = (struct cifs_sid *)((char *)pntsd +
  243. cpu_to_le32(pntsd->gsidoffset));
  244. dacl_ptr = (struct cifs_acl *)((char *)pntsd +
  245. cpu_to_le32(pntsd->dacloffset));
  246. #ifdef CONFIG_CIFS_DEBUG2
  247. cFYI(1, ("revision %d type 0x%x ooffset 0x%x goffset 0x%x "
  248. "sacloffset 0x%x dacloffset 0x%x",
  249. pntsd->revision, pntsd->type,
  250. pntsd->osidoffset, pntsd->gsidoffset, pntsd->sacloffset,
  251. 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 */