4 * Copyright (C) Shirish Pargaonkar (shirishp@us.ibm.com) 2011
6 * Used to alter entries of an ACL or replace an entire ACL in a
7 * security descriptor of a file system object that belongs to a
8 * share mounted using option cifsacl.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 * This program 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 the
17 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #endif /* HAVE_CONFIG_H */
37 #include <sys/xattr.h>
40 #include "idmap_plugin.h"
42 enum setcifsacl_actions {
50 static void *plugin_handle;
53 copy_cifs_sid(struct cifs_sid *dst, const struct cifs_sid *src)
57 dst->revision = src->revision;
58 dst->num_subauth = src->num_subauth;
59 for (i = 0; i < NUM_AUTHS; i++)
60 dst->authority[i] = src->authority[i];
61 for (i = 0; i < src->num_subauth; i++)
62 dst->sub_auth[i] = src->sub_auth[i];
66 copy_sec_desc(const struct cifs_ntsd *pntsd, struct cifs_ntsd *pnntsd,
67 int numaces, int acessize)
69 int osidsoffset, gsidsoffset, dacloffset;
70 struct cifs_sid *owner_sid_ptr, *group_sid_ptr;
71 struct cifs_sid *nowner_sid_ptr, *ngroup_sid_ptr;
72 struct cifs_ctrl_acl *dacl_ptr, *ndacl_ptr;
74 /* copy security descriptor control portion */
75 osidsoffset = le32toh(pntsd->osidoffset);
76 gsidsoffset = le32toh(pntsd->gsidoffset);
77 dacloffset = le32toh(pntsd->dacloffset);
79 pnntsd->revision = pntsd->revision;
80 pnntsd->type = pntsd->type;
81 pnntsd->osidoffset = pntsd->osidoffset;
82 pnntsd->gsidoffset = pntsd->gsidoffset;
83 pnntsd->dacloffset = pntsd->dacloffset;
85 dacl_ptr = (struct cifs_ctrl_acl *)((char *)pntsd + dacloffset);
86 ndacl_ptr = (struct cifs_ctrl_acl *)((char *)pnntsd + dacloffset);
88 ndacl_ptr->revision = dacl_ptr->revision;
89 ndacl_ptr->size = htole16(acessize + sizeof(struct cifs_ctrl_acl));
90 ndacl_ptr->num_aces = htole32(numaces);
93 owner_sid_ptr = (struct cifs_sid *)((char *)pntsd + osidsoffset);
94 nowner_sid_ptr = (struct cifs_sid *)((char *)pnntsd + osidsoffset);
95 copy_cifs_sid(nowner_sid_ptr, owner_sid_ptr);
98 group_sid_ptr = (struct cifs_sid *)((char *)pntsd + gsidsoffset);
99 ngroup_sid_ptr = (struct cifs_sid *)((char *)pnntsd + gsidsoffset);
100 copy_cifs_sid(ngroup_sid_ptr, group_sid_ptr);
106 copy_ace(struct cifs_ace *dace, struct cifs_ace *sace)
108 dace->type = sace->type;
109 dace->flags = sace->flags;
110 dace->access_req = sace->access_req;
112 copy_cifs_sid(&dace->sid, &sace->sid);
114 dace->size = sace->size;
116 return le16toh(dace->size);
120 compare_aces(struct cifs_ace *sace, struct cifs_ace *dace, int compflags)
124 if (compflags & COMPSID) {
125 if (dace->sid.revision != sace->sid.revision)
127 if (dace->sid.num_subauth != sace->sid.num_subauth)
129 for (i = 0; i < NUM_AUTHS; i++) {
130 if (dace->sid.authority[i] != sace->sid.authority[i])
133 for (i = 0; i < sace->sid.num_subauth; i++) {
134 if (dace->sid.sub_auth[i] != sace->sid.sub_auth[i])
139 if (compflags & COMPTYPE) {
140 if (dace->type != sace->type)
144 if (compflags & COMPFLAG) {
145 if (dace->flags != sace->flags)
149 if (compflags & COMPMASK) {
150 if (dace->access_req != sace->access_req)
158 get_sec_desc_size(struct cifs_ntsd *pntsd, struct cifs_ntsd **npntsd,
159 int aces, ssize_t *bufsize, size_t *acesoffset)
161 unsigned int size, acessize, dacloffset;
163 size = sizeof(struct cifs_ntsd) +
164 2 * sizeof(struct cifs_sid) +
165 sizeof(struct cifs_ctrl_acl);
167 dacloffset = le32toh(pntsd->dacloffset);
169 *acesoffset = dacloffset + sizeof(struct cifs_ctrl_acl);
170 acessize = aces * sizeof(struct cifs_ace);
171 *bufsize = size + acessize;
173 *npntsd = malloc(*bufsize);
175 printf("%s: Memory allocation failure", __func__);
183 ace_set(struct cifs_ntsd *pntsd, struct cifs_ntsd **npntsd, ssize_t *bufsize,
184 struct cifs_ace **cacesptr, int numcaces)
186 int i, rc, size = 0, acessize = 0;
190 rc = get_sec_desc_size(pntsd, npntsd, numcaces, bufsize, &acesoffset);
194 acesptr = (char *)*npntsd + acesoffset;
195 for (i = 0; i < numcaces; ++i) {
196 size = copy_ace((struct cifs_ace *)acesptr, cacesptr[i]);
200 copy_sec_desc(pntsd, *npntsd, numcaces, acessize);
201 acesptr = (char *)*npntsd + acesoffset;
208 ace_add(struct cifs_ntsd *pntsd, struct cifs_ntsd **npntsd, ssize_t *bufsize,
209 struct cifs_ace **facesptr, int numfaces,
210 struct cifs_ace **cacesptr, int numcaces)
212 int i, rc, numaces, size, acessize = 0;
216 numaces = numfaces + numcaces;
217 rc = get_sec_desc_size(pntsd, npntsd, numaces, bufsize, &acesoffset);
221 acesptr = (char *)*npntsd + acesoffset;
222 for (i = 0; i < numfaces; ++i) {
223 size = copy_ace((struct cifs_ace *)acesptr, facesptr[i]);
227 for (i = 0; i < numcaces; ++i) {
228 size = copy_ace((struct cifs_ace *)acesptr, cacesptr[i]);
232 copy_sec_desc(pntsd, *npntsd, numaces, acessize);
238 ace_modify(struct cifs_ntsd *pntsd, struct cifs_ntsd **npntsd, ssize_t *bufsize,
239 struct cifs_ace **facesptr, int numfaces,
240 struct cifs_ace **cacesptr, int numcaces)
242 int i, j, rc, size, acessize = 0;
247 printf("%s: No entries to modify", __func__);
251 rc = get_sec_desc_size(pntsd, npntsd, numfaces, bufsize, &acesoffset);
255 for (j = 0; j < numcaces; ++j) {
256 for (i = 0; i < numfaces; ++i) {
257 if (compare_aces(facesptr[i], cacesptr[j],
258 COMPSID | COMPTYPE)) {
259 copy_ace(facesptr[i], cacesptr[j]);
265 acesptr = (char *)*npntsd + acesoffset;
266 for (i = 0; i < numfaces; ++i) {
267 size = copy_ace((struct cifs_ace *)acesptr, facesptr[i]);
272 copy_sec_desc(pntsd, *npntsd, numfaces, acessize);
278 ace_delete(struct cifs_ntsd *pntsd, struct cifs_ntsd **npntsd, ssize_t *bufsize,
279 struct cifs_ace **facesptr, int numfaces,
280 struct cifs_ace **cacesptr, int numcaces)
282 int i, j, numaces = 0, rc, size, acessize = 0;
287 printf("%s: No entries to delete\n", __func__);
291 if (numfaces < numcaces) {
292 printf("%s: Invalid entries to delete\n", __func__);
296 rc = get_sec_desc_size(pntsd, npntsd, numfaces, bufsize, &acesoffset);
300 acesptr = (char *)*npntsd + acesoffset;
301 for (i = 0; i < numfaces; ++i) {
302 for (j = 0; j < numcaces; ++j) {
303 if (compare_aces(facesptr[i], cacesptr[j], COMPALL))
307 size = copy_ace((struct cifs_ace *)acesptr,
315 if (numaces == numfaces) {
316 printf("%s: Nothing to delete\n", __func__);
319 copy_sec_desc(pntsd, *npntsd, numaces, acessize);
325 get_numfaces(struct cifs_ntsd *pntsd, ssize_t acl_len,
326 struct cifs_ctrl_acl **daclptr)
330 struct cifs_ctrl_acl *ldaclptr;
331 char *end_of_acl = ((char *)pntsd) + acl_len;
333 dacloffset = le32toh(pntsd->dacloffset);
337 ldaclptr = (struct cifs_ctrl_acl *)((char *)pntsd + dacloffset);
339 /* validate that we do not go past end of acl */
340 if (end_of_acl >= (char *)ldaclptr + le16toh(ldaclptr->size)) {
341 numfaces = le32toh(ldaclptr->num_aces);
348 static struct cifs_ace **
349 build_fetched_aces(char *daclptr, int numfaces)
353 struct cifs_ace *pace, **facesptr;
355 facesptr = calloc(numfaces, sizeof(struct cifs_aces *));
357 printf("%s: Error %d allocating ACE array",
363 acl_size = sizeof(struct cifs_ctrl_acl);
364 for (i = 0; i < numfaces; ++i) {
365 facesptr[i] = malloc(sizeof(struct cifs_ace));
367 goto build_fetched_aces_err;
368 pace = (struct cifs_ace *) (acl_base + acl_size);
369 memcpy(facesptr[i], pace, sizeof(struct cifs_ace));
370 acl_base = (char *)pace;
371 acl_size = le16toh(pace->size);
375 build_fetched_aces_err:
376 printf("%s: Invalid fetched ace\n", __func__);
377 for (i = 0; i < numfaces; ++i)
384 verify_ace_type(char *typestr, uint8_t *typeval)
389 if (strstr(typestr, "0x")) { /* hex type value */
390 *typeval = strtol(typestr, &invaltype, 16);
391 if (!strlen(invaltype)) {
392 if (*typeval != ACCESS_ALLOWED &&
393 *typeval != ACCESS_DENIED &&
394 *typeval != ACCESS_ALLOWED_OBJECT &&
395 *typeval != ACCESS_DENIED_OBJECT) {
396 printf("%s: Invalid type: %s\n",
404 len = strlen(typestr);
405 for (i = 0; i < len; ++i)
406 *(typestr + i) = toupper(*(typestr + i));
407 if (!strcmp(typestr, "ALLOWED"))
409 else if (!strcmp(typestr, "DENIED"))
411 else if (!strcmp(typestr, "ALLOWED_OBJECT"))
413 else if (!strcmp(typestr, "DENIED_OBJECT"))
416 printf("%s: Invalid type: %s\n", __func__, typestr);
424 ace_flag_value(char *flagstr)
426 uint8_t flagval = 0x0;
429 iflag = strtok(flagstr, "|"); /* everything before | */
431 if (!strcmp(iflag, "OI"))
433 else if (!strcmp(iflag, "CI"))
435 else if (!strcmp(iflag, "NP"))
437 else if (!strcmp(iflag, "IO"))
439 else if (!strcmp(iflag, "I"))
442 return 0x0; /* Invalid flag */
443 iflag = strtok(NULL, "|"); /* everything before | */
450 verify_ace_flags(char *flagstr, uint8_t *flagval)
454 if (!strcmp(flagstr, "0") || !strcmp(flagstr, "0x0"))
457 if (strstr(flagstr, "0x")) { /* hex flag value */
458 *flagval = strtol(flagstr, &invalflag, 16);
459 if (strlen(invalflag)) {
460 printf("%s: Invalid flags: %s\n", __func__, flagstr);
464 *flagval = ace_flag_value(flagstr);
466 if (!*flagval || (*flagval & ~VFLAGS)) {
467 printf("%s: Invalid flag %s and value: 0x%x\n",
468 __func__, flagstr, *flagval);
476 ace_mask_value(char *mask)
478 uint32_t maskval = 0;
481 if (!strcmp(mask, "FULL"))
483 if (!strcmp(mask, "CHANGE"))
485 if (!strcmp(mask, "READ"))
488 while((cur = *mask++)) {
503 maskval |= WRITE_DAC;
506 maskval |= WRITE_OWNER;
516 verify_ace_mask(char *maskstr, uint32_t *maskval)
522 val = strtoul(maskstr, &ep, 0);
523 if (errno == 0 && *ep == '\0')
524 *maskval = htole32((uint32_t)val);
526 *maskval = htole32(ace_mask_value(maskstr));
529 printf("%s: Invalid mask %s (value 0x%x)\n", __func__,
537 static struct cifs_ace **
538 build_cmdline_aces(char **arrptr, int numcaces)
541 char *acesid, *acetype, *aceflag, *acemask;
542 struct cifs_ace **cacesptr;
544 cacesptr = calloc(numcaces, sizeof(struct cifs_aces *));
546 printf("%s: Error %d allocating ACE array", __func__, errno);
550 for (i = 0; i < numcaces; ++i) {
551 acesid = strtok(arrptr[i], ":");
552 acetype = strtok(NULL, "/");
553 aceflag = strtok(NULL, "/");
554 acemask = strtok(NULL, "/");
556 if (!acesid || !acetype || !aceflag || !acemask) {
557 printf("%s: Incomplete ACE: %s\n", __func__, arrptr[i]);
558 goto build_cmdline_aces_ret;
561 cacesptr[i] = malloc(sizeof(struct cifs_ace));
563 printf("%s: ACE alloc error %d\n", __func__, errno);
564 goto build_cmdline_aces_ret;
567 if (str_to_sid(plugin_handle, acesid, &cacesptr[i]->sid)) {
568 printf("%s: Invalid SID (%s): %s\n", __func__, arrptr[i],
570 goto build_cmdline_aces_ret;
573 if (verify_ace_type(acetype, &cacesptr[i]->type)) {
574 printf("%s: Invalid ACE type: %s\n",
575 __func__, arrptr[i]);
576 goto build_cmdline_aces_ret;
579 if (verify_ace_flags(aceflag, &cacesptr[i]->flags)) {
580 printf("%s: Invalid ACE flag: %s\n",
581 __func__, arrptr[i]);
582 goto build_cmdline_aces_ret;
585 if (verify_ace_mask(acemask, &cacesptr[i]->access_req)) {
586 printf("%s: Invalid ACE mask: %s\n",
587 __func__, arrptr[i]);
588 goto build_cmdline_aces_ret;
591 cacesptr[i]->size = htole16(1 + 1 + 2 + 4 + 1 + 1 + 6 +
592 cacesptr[i]->sid.num_subauth * 4);
596 build_cmdline_aces_ret:
597 for (i = 0; i < numcaces; ++i)
604 parse_cmdline_aces(char *acelist, int numcaces)
607 char *acestr, *vacestr, **arrptr = NULL;
609 arrptr = (char **)malloc(numcaces * sizeof(char *));
611 printf("%s: Unable to allocate char array\n", __func__);
615 while (i < numcaces) {
616 acestr = strtok(acelist, ","); /* everything before , */
618 goto parse_cmdline_aces_err;
620 vacestr = strstr(acestr, "ACL:"); /* ace as ACL:*" */
622 goto parse_cmdline_aces_err;
623 vacestr += 4; /* skip past "ACL:" */
632 parse_cmdline_aces_err:
633 printf("%s: Error parsing ACEs\n", __func__);
638 /* How many aces were provided on the command-line? Count the commas. */
640 get_numcaces(const char *aces)
642 unsigned int num = 1;
646 while((current = strchr(current, ','))) {
655 setacl_action(struct cifs_ntsd *pntsd, struct cifs_ntsd **npntsd,
656 ssize_t *bufsize, struct cifs_ace **facesptr, int numfaces,
657 struct cifs_ace **cacesptr, int numcaces,
658 enum setcifsacl_actions maction)
664 rc = ace_delete(pntsd, npntsd, bufsize, facesptr,
665 numfaces, cacesptr, numcaces);
668 rc = ace_modify(pntsd, npntsd, bufsize, facesptr,
669 numfaces, cacesptr, numcaces);
672 rc = ace_add(pntsd, npntsd, bufsize, facesptr,
673 numfaces, cacesptr, numcaces);
676 rc = ace_set(pntsd, npntsd, bufsize, cacesptr, numcaces);
679 printf("%s: Invalid action: %d\n", __func__, maction);
687 setcifsacl_usage(const char *prog)
690 "%s: Alter CIFS/NTFS ACL in a security descriptor of a file object\n",
692 fprintf(stderr, "Usage: %s option <list_of_ACEs> <file_name>\n", prog);
693 fprintf(stderr, "Valid options:\n");
694 fprintf(stderr, "\t-v Version of the program\n");
695 fprintf(stderr, "\n\t-a Add ACE(s), separated by a comma, to an ACL\n");
697 "\tsetcifsacl -a \"ACL:Administrator:ALLOWED/0x0/FULL\" <file_name>\n");
698 fprintf(stderr, "\n");
700 "\t-D Delete ACE(s), separated by a comma, from an ACL\n");
702 "\tsetcifsacl -D \"ACL:Administrator:DENIED/0x0/D\" <file_name>\n");
703 fprintf(stderr, "\n");
705 "\t-M Modify ACE(s), separated by a comma, in an ACL\n");
707 "\tsetcifsacl -M \"ACL:user1:ALLOWED/0x0/0x1e01ff\" <file_name>\n");
709 "\n\t-S Replace existing ACL with ACE(s), separated by a comma\n");
711 "\tsetcifsacl -S \"ACL:Administrator:ALLOWED/0x0/D\" <file_name>\n");
712 fprintf(stderr, "\nRefer to setcifsacl(1) manpage for details\n");
716 main(const int argc, char *const argv[])
718 int i, rc, c, numcaces, numfaces;
719 enum setcifsacl_actions maction = ActUnknown;
720 ssize_t attrlen, bufsize = BUFSIZE;
721 char *ace_list, *filename, *attrval, **arrptr = NULL;
722 struct cifs_ctrl_acl *daclptr = NULL;
723 struct cifs_ace **cacesptr = NULL, **facesptr = NULL;
724 struct cifs_ntsd *ntsdptr = NULL;
726 c = getopt(argc, argv, "hvD:M:a:S:");
745 setcifsacl_usage(basename(argv[0]));
748 printf("Version: %s\n", VERSION);
751 setcifsacl_usage(basename(argv[0]));
755 /* We expect 1 argument in addition to the option */
757 setcifsacl_usage(basename(argv[0]));
763 printf("%s: No valid ACEs specified\n", __func__);
767 if (init_plugin(&plugin_handle)) {
768 printf("ERROR: unable to initialize idmapping plugin: %s\n",
773 numcaces = get_numcaces(ace_list);
775 arrptr = parse_cmdline_aces(ace_list, numcaces);
777 goto setcifsacl_numcaces_ret;
779 cacesptr = build_cmdline_aces(arrptr, numcaces);
781 goto setcifsacl_cmdlineparse_ret;
784 if (bufsize >= XATTR_SIZE_MAX) {
785 printf("%s: Buffer size %zd exceeds max size of %d\n",
786 __func__, bufsize, XATTR_SIZE_MAX);
787 goto setcifsacl_cmdlineverify_ret;
790 attrval = malloc(bufsize * sizeof(char));
792 printf("error allocating memory for attribute value buffer\n");
793 goto setcifsacl_cmdlineverify_ret;
796 attrlen = getxattr(filename, ATTRNAME, attrval, bufsize);
798 if (errno == ERANGE) {
803 printf("getxattr error: %d\n", errno);
804 goto setcifsacl_getx_ret;
808 numfaces = get_numfaces((struct cifs_ntsd *)attrval, attrlen, &daclptr);
809 if (!numfaces && maction != ActAdd) { /* if we are not adding aces */
810 printf("%s: Empty DACL\n", __func__);
811 goto setcifsacl_facenum_ret;
814 facesptr = build_fetched_aces((char *)daclptr, numfaces);
816 goto setcifsacl_facenum_ret;
819 rc = setacl_action((struct cifs_ntsd *)attrval, &ntsdptr, &bufsize,
820 facesptr, numfaces, cacesptr, numcaces, maction);
822 goto setcifsacl_action_ret;
824 attrlen = setxattr(filename, ATTRNAME, ntsdptr, bufsize, 0);
826 printf("%s: setxattr error: %s\n", __func__, strerror(errno));
827 goto setcifsacl_facenum_ret;
830 exit_plugin(plugin_handle);
833 setcifsacl_action_ret:
836 setcifsacl_facenum_ret:
837 for (i = 0; i < numfaces; ++i)
844 setcifsacl_cmdlineverify_ret:
845 for (i = 0; i < numcaces; ++i)
849 setcifsacl_cmdlineparse_ret:
852 setcifsacl_numcaces_ret:
853 exit_plugin(plugin_handle);