PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/samba3/source/modules/vfs_tru64acl.c

https://gitlab.com/envieidoc/advancedtomato2
C | 505 lines | 400 code | 61 blank | 44 comment | 66 complexity | 43ee73612d6aebdf4357271bdb4a599d MD5 | raw file
  1. /*
  2. Unix SMB/Netbios implementation.
  3. VFS module to get and set Tru64 acls
  4. Copyright (C) Michael Adam 2006
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include "includes.h"
  18. /* prototypes for private functions first - for clarity */
  19. static struct smb_acl_t *tru64_acl_to_smb_acl(const struct acl *tru64_acl);
  20. static BOOL tru64_ace_to_smb_ace(acl_entry_t tru64_ace,
  21. struct smb_acl_entry *smb_ace);
  22. static acl_t smb_acl_to_tru64_acl(const SMB_ACL_T smb_acl);
  23. static acl_tag_t smb_tag_to_tru64(SMB_ACL_TAG_T smb_tag);
  24. static SMB_ACL_TAG_T tru64_tag_to_smb(acl_tag_t tru64_tag);
  25. static acl_perm_t smb_permset_to_tru64(SMB_ACL_PERM_T smb_permset);
  26. static SMB_ACL_PERM_T tru64_permset_to_smb(const acl_perm_t tru64_permset);
  27. /* public functions - the api */
  28. SMB_ACL_T tru64acl_sys_acl_get_file(vfs_handle_struct *handle,
  29. const char *path_p,
  30. SMB_ACL_TYPE_T type)
  31. {
  32. struct smb_acl_t *result;
  33. acl_type_t the_acl_type;
  34. acl_t tru64_acl;
  35. DEBUG(10, ("Hi! This is tru64acl_sys_acl_get_file.\n"));
  36. switch(type) {
  37. case SMB_ACL_TYPE_ACCESS:
  38. the_acl_type = ACL_TYPE_ACCESS;
  39. break;
  40. case SMB_ACL_TYPE_DEFAULT:
  41. the_acl_type = ACL_TYPE_DEFAULT;
  42. break;
  43. default:
  44. errno = EINVAL;
  45. return NULL;
  46. }
  47. tru64_acl = acl_get_file((char *)path_p, the_acl_type);
  48. if (tru64_acl == NULL) {
  49. return NULL;
  50. }
  51. result = tru64_acl_to_smb_acl(tru64_acl);
  52. acl_free(tru64_acl);
  53. return result;
  54. }
  55. SMB_ACL_T tru64acl_sys_acl_get_fd(vfs_handle_struct *handle,
  56. files_struct *fsp,
  57. int fd)
  58. {
  59. struct smb_acl_t *result;
  60. acl_t tru64_acl = acl_get_fd(fd, ACL_TYPE_ACCESS);
  61. if (tru64_acl == NULL) {
  62. return NULL;
  63. }
  64. result = tru64_acl_to_smb_acl(tru64_acl);
  65. acl_free(tru64_acl);
  66. return result;
  67. }
  68. int tru64acl_sys_acl_set_file(vfs_handle_struct *handle,
  69. const char *name,
  70. SMB_ACL_TYPE_T type,
  71. SMB_ACL_T theacl)
  72. {
  73. int res;
  74. acl_type_t the_acl_type;
  75. acl_t tru64_acl;
  76. DEBUG(10, ("tru64acl_sys_acl_set_file called with name %s, type %d\n",
  77. name, type));
  78. switch(type) {
  79. case SMB_ACL_TYPE_ACCESS:
  80. DEBUGADD(10, ("got acl type ACL_TYPE_ACCESS\n"));
  81. the_acl_type = ACL_TYPE_ACCESS;
  82. break;
  83. case SMB_ACL_TYPE_DEFAULT:
  84. DEBUGADD(10, ("got acl type ACL_TYPE_DEFAULT\n"));
  85. the_acl_type = ACL_TYPE_DEFAULT;
  86. break;
  87. default:
  88. DEBUGADD(10, ("invalid acl type\n"));
  89. errno = EINVAL;
  90. goto fail;
  91. }
  92. tru64_acl = smb_acl_to_tru64_acl(theacl);
  93. if (tru64_acl == NULL) {
  94. DEBUG(10, ("smb_acl_to_tru64_acl failed!\n"));
  95. goto fail;
  96. }
  97. DEBUG(10, ("got tru64 acl...\n"));
  98. res = acl_set_file((char *)name, the_acl_type, tru64_acl);
  99. acl_free(tru64_acl);
  100. if (res != 0) {
  101. DEBUG(10, ("acl_set_file failed: %s\n", strerror(errno)));
  102. goto fail;
  103. }
  104. return res;
  105. fail:
  106. DEBUG(1, ("tru64acl_sys_acl_set_file failed!\n"));
  107. return -1;
  108. }
  109. int tru64acl_sys_acl_set_fd(vfs_handle_struct *handle,
  110. files_struct *fsp,
  111. int fd, SMB_ACL_T theacl)
  112. {
  113. int res;
  114. acl_t tru64_acl = smb_acl_to_tru64_acl(theacl);
  115. if (tru64_acl == NULL) {
  116. return -1;
  117. }
  118. res = acl_set_fd(fd, ACL_TYPE_ACCESS, tru64_acl);
  119. acl_free(tru64_acl);
  120. return res;
  121. }
  122. int tru64acl_sys_acl_delete_def_file(vfs_handle_struct *handle,
  123. const char *path)
  124. {
  125. return acl_delete_def_file((char *)path);
  126. }
  127. /* private functions */
  128. static struct smb_acl_t *tru64_acl_to_smb_acl(const struct acl *tru64_acl)
  129. {
  130. struct smb_acl_t *result;
  131. acl_entry_t entry;
  132. DEBUG(10, ("Hi! This is tru64_acl_to_smb_acl.\n"));
  133. if ((result = SMB_MALLOC_P(struct smb_acl_t)) == NULL) {
  134. DEBUG(0, ("SMB_MALLOC_P failed in tru64_acl_to_smb_acl\n"));
  135. errno = ENOMEM;
  136. goto fail;
  137. }
  138. ZERO_STRUCTP(result);
  139. if (acl_first_entry((struct acl *)tru64_acl) != 0) {
  140. DEBUG(10, ("acl_first_entry failed: %s\n", strerror(errno)));
  141. goto fail;
  142. }
  143. while ((entry = acl_get_entry((struct acl *)tru64_acl)) != NULL) {
  144. result = SMB_REALLOC(result, sizeof(struct smb_acl_t) +
  145. (sizeof(struct smb_acl_entry) *
  146. (result->count + 1)));
  147. if (result == NULL) {
  148. DEBUG(0, ("SMB_REALLOC failed in tru64_acl_to_smb_acl\n"));
  149. errno = ENOMEM;
  150. goto fail;
  151. }
  152. /* XYZ */
  153. if (!tru64_ace_to_smb_ace(entry, &result->acl[result->count])) {
  154. SAFE_FREE(result);
  155. goto fail;
  156. }
  157. result->count += 1;
  158. }
  159. return result;
  160. fail:
  161. if (result != NULL) {
  162. SAFE_FREE(result);
  163. }
  164. DEBUG(1, ("tru64_acl_to_smb_acl failed!\n"));
  165. return NULL;
  166. }
  167. static BOOL tru64_ace_to_smb_ace(acl_entry_t tru64_ace,
  168. struct smb_acl_entry *smb_ace)
  169. {
  170. acl_tag_t tru64_tag;
  171. acl_permset_t permset;
  172. SMB_ACL_TAG_T smb_tag_type;
  173. SMB_ACL_PERM_T smb_permset;
  174. void *qualifier;
  175. if (acl_get_tag_type(tru64_ace, &tru64_tag) != 0) {
  176. DEBUG(0, ("acl_get_tag_type failed: %s\n", strerror(errno)));
  177. return False;
  178. }
  179. /* On could set the tag type directly to save a function call,
  180. * but I like this better... */
  181. smb_tag_type = tru64_tag_to_smb(tru64_tag);
  182. if (smb_tag_type == 0) {
  183. DEBUG(3, ("invalid tag type given: %d\n", tru64_tag));
  184. return False;
  185. }
  186. if (sys_acl_set_tag_type(smb_ace, smb_tag_type) != 0) {
  187. DEBUG(3, ("sys_acl_set_tag_type failed: %s\n",
  188. strerror(errno)));
  189. return False;
  190. }
  191. qualifier = acl_get_qualifier(tru64_ace);
  192. if (qualifier != NULL) {
  193. if (sys_acl_set_qualifier(smb_ace, qualifier) != 0) {
  194. DEBUG(3, ("sys_acl_set_qualifier failed\n"));
  195. return False;
  196. }
  197. }
  198. if (acl_get_permset(tru64_ace, &permset) != 0) {
  199. DEBUG(3, ("acl_get_permset failed: %s\n", strerror(errno)));
  200. return False;
  201. }
  202. smb_permset = tru64_permset_to_smb(*permset);
  203. if (sys_acl_set_permset(smb_ace, &smb_permset) != 0) {
  204. DEBUG(3, ("sys_acl_set_permset failed: %s\n", strerror(errno)));
  205. return False;
  206. }
  207. return True;
  208. }
  209. static acl_t smb_acl_to_tru64_acl(const SMB_ACL_T smb_acl)
  210. {
  211. acl_t result;
  212. acl_entry_t tru64_entry;
  213. int i;
  214. char *acl_text;
  215. ssize_t acl_text_len;
  216. /* The tru64 acl_init function takes a size_t value
  217. * instead of a count of entries (as with posix).
  218. * the size parameter "Specifies the size of the working
  219. * storage in bytes" (according to the man page).
  220. * But it is unclear to me, how this size is to be
  221. * calculated.
  222. *
  223. * It should not matter, since acl_create_entry enlarges
  224. * the working storage at need. ... */
  225. DEBUG(10, ("Hi! This is smb_acl_to_tru64_acl.\n"));
  226. result = acl_init(1);
  227. if (result == NULL) {
  228. DEBUG(3, ("acl_init failed!\n"));
  229. goto fail;
  230. }
  231. DEBUGADD(10, ("parsing acl entries...\n"));
  232. for (i = 0; i < smb_acl->count; i++) {
  233. /* XYZ - maybe eliminate this direct access? */
  234. const struct smb_acl_entry *smb_entry = &smb_acl->acl[i];
  235. acl_tag_t tru64_tag;
  236. acl_perm_t tru64_permset;
  237. tru64_tag = smb_tag_to_tru64(smb_entry->a_type);
  238. if (tru64_tag == -1) {
  239. DEBUG(3, ("smb_tag_to_tru64 failed!\n"));
  240. goto fail;
  241. }
  242. if (tru64_tag == ACL_MASK) {
  243. DEBUGADD(10, (" - acl type ACL_MASK: not implemented on Tru64 ==> skipping\n"));
  244. continue;
  245. }
  246. tru64_entry = acl_create_entry(&result);
  247. if (tru64_entry == NULL) {
  248. DEBUG(3, ("acl_create_entry failed: %s\n",
  249. strerror(errno)));
  250. goto fail;
  251. }
  252. if (acl_set_tag_type(tru64_entry, tru64_tag) != 0) {
  253. DEBUG(3, ("acl_set_tag_type(%d) failed: %s\n",
  254. strerror(errno)));
  255. goto fail;
  256. }
  257. switch (smb_entry->a_type) {
  258. case SMB_ACL_USER:
  259. if (acl_set_qualifier(tru64_entry,
  260. (int *)&smb_entry->uid) != 0)
  261. {
  262. DEBUG(3, ("acl_set_qualifier failed: %s\n",
  263. strerror(errno)));
  264. goto fail;
  265. }
  266. DEBUGADD(10, (" - setting uid to %d\n", smb_entry->uid));
  267. break;
  268. case SMB_ACL_GROUP:
  269. if (acl_set_qualifier(tru64_entry,
  270. (int *)&smb_entry->gid) != 0)
  271. {
  272. DEBUG(3, ("acl_set_qualifier failed: %s\n",
  273. strerror(errno)));
  274. goto fail;
  275. }
  276. DEBUGADD(10, (" - setting gid to %d\n", smb_entry->gid));
  277. break;
  278. default:
  279. break;
  280. }
  281. tru64_permset = smb_permset_to_tru64(smb_entry->a_perm);
  282. if (tru64_permset == -1) {
  283. DEBUG(3, ("smb_permset_to_tru64 failed!\n"));
  284. goto fail;
  285. }
  286. DEBUGADD(10, (" - setting perms to %0d\n", tru64_permset));
  287. if (acl_set_permset(tru64_entry, &tru64_permset) != 0)
  288. {
  289. DEBUG(3, ("acl_set_permset failed: %s\n", strerror(errno)));
  290. goto fail;
  291. }
  292. } /* for */
  293. DEBUGADD(10, ("done parsing acl entries\n"));
  294. tru64_entry = NULL;
  295. if (acl_valid(result, &tru64_entry) != 0) {
  296. DEBUG(1, ("smb_acl_to_tru64_acl: ACL is invalid (%s)\n",
  297. strerror(errno)));
  298. if (tru64_entry != NULL) {
  299. DEBUGADD(1, ("the acl contains duplicate entries\n"));
  300. }
  301. goto fail;
  302. }
  303. DEBUGADD(10, ("acl is valid\n"));
  304. acl_text = acl_to_text(result, &acl_text_len);
  305. if (acl_text == NULL) {
  306. DEBUG(3, ("acl_to_text failed: %s\n", strerror(errno)));
  307. goto fail;
  308. }
  309. DEBUG(1, ("acl_text: %s\n", acl_text));
  310. free(acl_text);
  311. return result;
  312. fail:
  313. if (result != NULL) {
  314. acl_free(result);
  315. }
  316. DEBUG(1, ("smb_acl_to_tru64_acl failed!\n"));
  317. return NULL;
  318. }
  319. static acl_tag_t smb_tag_to_tru64(SMB_ACL_TAG_T smb_tag)
  320. {
  321. acl_tag_t result;
  322. switch (smb_tag) {
  323. case SMB_ACL_USER:
  324. result = ACL_USER;
  325. DEBUGADD(10, ("got acl type ACL_USER\n"));
  326. break;
  327. case SMB_ACL_USER_OBJ:
  328. result = ACL_USER_OBJ;
  329. DEBUGADD(10, ("got acl type ACL_USER_OBJ\n"));
  330. break;
  331. case SMB_ACL_GROUP:
  332. result = ACL_GROUP;
  333. DEBUGADD(10, ("got acl type ACL_GROUP\n"));
  334. break;
  335. case SMB_ACL_GROUP_OBJ:
  336. result = ACL_GROUP_OBJ;
  337. DEBUGADD(10, ("got acl type ACL_GROUP_OBJ\n"));
  338. break;
  339. case SMB_ACL_OTHER:
  340. result = ACL_OTHER;
  341. DEBUGADD(10, ("got acl type ACL_OTHER\n"));
  342. break;
  343. case SMB_ACL_MASK:
  344. result = ACL_MASK;
  345. DEBUGADD(10, ("got acl type ACL_MASK\n"));
  346. break;
  347. default:
  348. DEBUG(1, ("Unknown tag type %d\n", smb_tag));
  349. result = -1;
  350. }
  351. return result;
  352. }
  353. static SMB_ACL_TAG_T tru64_tag_to_smb(acl_tag_t tru64_tag)
  354. {
  355. SMB_ACL_TAG_T smb_tag_type;
  356. switch(tru64_tag) {
  357. case ACL_USER:
  358. smb_tag_type = SMB_ACL_USER;
  359. DEBUGADD(10, ("got smb acl tag type SMB_ACL_USER\n"));
  360. break;
  361. case ACL_USER_OBJ:
  362. smb_tag_type = SMB_ACL_USER_OBJ;
  363. DEBUGADD(10, ("got smb acl tag type SMB_ACL_USER_OBJ\n"));
  364. break;
  365. case ACL_GROUP:
  366. smb_tag_type = SMB_ACL_GROUP;
  367. DEBUGADD(10, ("got smb acl tag type SMB_ACL_GROUP\n"));
  368. break;
  369. case ACL_GROUP_OBJ:
  370. smb_tag_type = SMB_ACL_GROUP_OBJ;
  371. DEBUGADD(10, ("got smb acl tag type SMB_ACL_GROUP_OBJ\n"));
  372. break;
  373. case ACL_OTHER:
  374. smb_tag_type = SMB_ACL_OTHER;
  375. DEBUGADD(10, ("got smb acl tag type SMB_ACL_OTHER\n"));
  376. break;
  377. case ACL_MASK:
  378. smb_tag_type = SMB_ACL_MASK;
  379. DEBUGADD(10, ("got smb acl tag type SMB_ACL_MASK\n"));
  380. break;
  381. default:
  382. DEBUG(0, ("Unknown tag type %d\n", (unsigned int)tru64_tag));
  383. smb_tag_type = 0;
  384. }
  385. return smb_tag_type;
  386. }
  387. static acl_perm_t smb_permset_to_tru64(SMB_ACL_PERM_T smb_permset)
  388. {
  389. /* originally, I thought that acl_clear_perm was the
  390. * proper way to reset the permset to 0. but without
  391. * initializing it to 0, acl_clear_perm fails.
  392. * so probably, acl_clear_perm is not necessary here... ?! */
  393. acl_perm_t tru64_permset = 0;
  394. if (acl_clear_perm(&tru64_permset) != 0) {
  395. DEBUG(5, ("acl_clear_perm failed: %s\n", strerror(errno)));
  396. return -1;
  397. }
  398. /* according to original lib/sysacls.c, acl_add_perm is
  399. * broken on tru64 ... */
  400. tru64_permset |= ((smb_permset & SMB_ACL_READ) ? ACL_READ : 0);
  401. tru64_permset |= ((smb_permset & SMB_ACL_WRITE) ? ACL_WRITE : 0);
  402. tru64_permset |= ((smb_permset & SMB_ACL_EXECUTE) ? ACL_EXECUTE : 0);
  403. return tru64_permset;
  404. }
  405. static SMB_ACL_PERM_T tru64_permset_to_smb(const acl_perm_t tru64_permset)
  406. {
  407. SMB_ACL_PERM_T smb_permset = 0;
  408. smb_permset |= ((tru64_permset & ACL_READ) ? SMB_ACL_READ : 0);
  409. smb_permset |= ((tru64_permset & ACL_WRITE) ? SMB_ACL_WRITE : 0);
  410. smb_permset |= ((tru64_permset & ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
  411. return smb_permset;
  412. }
  413. /* VFS operations structure */
  414. static vfs_op_tuple tru64acl_op_tuples[] = {
  415. /* Disk operations */
  416. {SMB_VFS_OP(tru64acl_sys_acl_get_file),
  417. SMB_VFS_OP_SYS_ACL_GET_FILE,
  418. SMB_VFS_LAYER_TRANSPARENT},
  419. {SMB_VFS_OP(tru64acl_sys_acl_get_fd),
  420. SMB_VFS_OP_SYS_ACL_GET_FD,
  421. SMB_VFS_LAYER_TRANSPARENT},
  422. {SMB_VFS_OP(tru64acl_sys_acl_set_file),
  423. SMB_VFS_OP_SYS_ACL_SET_FILE,
  424. SMB_VFS_LAYER_TRANSPARENT},
  425. {SMB_VFS_OP(tru64acl_sys_acl_set_fd),
  426. SMB_VFS_OP_SYS_ACL_SET_FD,
  427. SMB_VFS_LAYER_TRANSPARENT},
  428. {SMB_VFS_OP(tru64acl_sys_acl_delete_def_file),
  429. SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE,
  430. SMB_VFS_LAYER_TRANSPARENT},
  431. {SMB_VFS_OP(NULL),
  432. SMB_VFS_OP_NOOP,
  433. SMB_VFS_LAYER_NOOP}
  434. };
  435. NTSTATUS vfs_tru64acl_init(void);
  436. NTSTATUS vfs_tru64acl_init(void)
  437. {
  438. return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "tru64acl",
  439. tru64acl_op_tuples);
  440. }
  441. /* ENTE */