2 Unix SMB/CIFS implementation.
5 Copyright (C) Andrew Tridgell 2000
6 Copyright (C) Tim Potter 2000
7 Copyright (C) Jeremy Allison 2000
8 Copyright (C) Jelmer Vernooij 2003
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 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "popt_common.h"
26 #include "../librpc/gen_ndr/ndr_lsa.h"
27 #include "rpc_client/cli_lsarpc.h"
28 #include "../libcli/security/security.h"
30 extern bool AllowDebugChange;
34 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
36 /* numeric is set when the user wants numeric SIDs and ACEs rather
37 than going via LSA calls to resolve them */
42 enum acl_mode {SMB_ACL_SET, SMB_ACL_DELETE, SMB_ACL_MODIFY, SMB_ACL_ADD };
43 enum chown_mode {REQUEST_NONE, REQUEST_CHOWN, REQUEST_CHGRP, REQUEST_INHERIT};
44 enum exit_values {EXIT_OK, EXIT_FAILED, EXIT_PARSE_ERROR};
51 /* These values discovered by inspection */
53 static const struct perm_value special_values[] = {
63 static const struct perm_value standard_values[] = {
64 { "READ", 0x001200a9 },
65 { "CHANGE", 0x001301bf },
66 { "FULL", 0x001f01ff },
70 /* Open cli connection and policy handle */
72 static NTSTATUS cli_lsa_lookup_sid(struct cli_state *cli,
73 const struct dom_sid *sid,
75 enum lsa_SidType *type,
76 char **domain, char **name)
78 uint16 orig_cnum = cli->cnum;
79 struct rpc_pipe_client *p = NULL;
80 struct policy_handle handle;
82 TALLOC_CTX *frame = talloc_stackframe();
83 enum lsa_SidType *types;
87 status = cli_tcon_andx(cli, "IPC$", "?????", "", 0);
88 if (!NT_STATUS_IS_OK(status)) {
92 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
94 if (!NT_STATUS_IS_OK(status)) {
98 status = rpccli_lsa_open_policy(p, talloc_tos(), True,
99 GENERIC_EXECUTE_ACCESS, &handle);
100 if (!NT_STATUS_IS_OK(status)) {
104 status = rpccli_lsa_lookup_sids(p, talloc_tos(), &handle, 1, sid,
105 &domains, &names, &types);
106 if (!NT_STATUS_IS_OK(status)) {
111 *domain = talloc_move(mem_ctx, &domains[0]);
112 *name = talloc_move(mem_ctx, &names[0]);
114 status = NT_STATUS_OK;
118 cli->cnum = orig_cnum;
123 static NTSTATUS cli_lsa_lookup_name(struct cli_state *cli,
125 enum lsa_SidType *type,
128 uint16 orig_cnum = cli->cnum;
129 struct rpc_pipe_client *p;
130 struct policy_handle handle;
132 TALLOC_CTX *frame = talloc_stackframe();
133 struct dom_sid *sids;
134 enum lsa_SidType *types;
136 status = cli_tcon_andx(cli, "IPC$", "?????", "", 0);
137 if (!NT_STATUS_IS_OK(status)) {
141 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
143 if (!NT_STATUS_IS_OK(status)) {
147 status = rpccli_lsa_open_policy(p, talloc_tos(), True,
148 GENERIC_EXECUTE_ACCESS, &handle);
149 if (!NT_STATUS_IS_OK(status)) {
153 status = rpccli_lsa_lookup_names(p, talloc_tos(), &handle, 1, &name,
154 NULL, 1, &sids, &types);
155 if (!NT_STATUS_IS_OK(status)) {
162 status = NT_STATUS_OK;
166 cli->cnum = orig_cnum;
171 /* convert a SID to a string, either numeric or username/group */
172 static void SidToString(struct cli_state *cli, fstring str, const struct dom_sid *sid)
176 enum lsa_SidType type;
179 sid_to_fstring(str, sid);
185 status = cli_lsa_lookup_sid(cli, sid, talloc_tos(), &type,
188 if (!NT_STATUS_IS_OK(status)) {
193 slprintf(str, sizeof(fstring) - 1, "%s%s%s",
194 domain, lp_winbind_separator(), name);
200 /* convert a string to a SID, either numeric or username/group */
201 static bool StringToSid(struct cli_state *cli, struct dom_sid *sid, const char *str)
203 enum lsa_SidType type;
205 if (strncmp(str, "S-", 2) == 0) {
206 return string_to_sid(sid, str);
209 return NT_STATUS_IS_OK(cli_lsa_lookup_name(cli, str, &type, sid));
212 static void print_ace_flags(FILE *f, uint8_t flags)
214 char *str = talloc_strdup(NULL, "");
220 if (flags & SEC_ACE_FLAG_OBJECT_INHERIT) {
221 str = talloc_asprintf(str, "%s%s",
227 if (flags & SEC_ACE_FLAG_CONTAINER_INHERIT) {
228 str = talloc_asprintf(str, "%s%s",
234 if (flags & SEC_ACE_FLAG_NO_PROPAGATE_INHERIT) {
235 str = talloc_asprintf(str, "%s%s",
241 if (flags & SEC_ACE_FLAG_INHERIT_ONLY) {
242 str = talloc_asprintf(str, "%s%s",
248 if (flags & SEC_ACE_FLAG_INHERITED_ACE) {
249 str = talloc_asprintf(str, "%s%s",
255 /* Ignore define SEC_ACE_FLAG_SUCCESSFUL_ACCESS ( 0x40 )
256 and SEC_ACE_FLAG_FAILED_ACCESS ( 0x80 ) as they're
259 if (str[strlen(str)-1] == '|') {
260 str[strlen(str)-1] = '\0';
261 fprintf(f, "/%s/", str);
263 fprintf(f, "/0x%x/", flags);
269 fprintf(f, "/0x%x/", flags);
272 /* print an ACE on a FILE, using either numeric or ascii representation */
273 static void print_ace(struct cli_state *cli, FILE *f, struct security_ace *ace)
275 const struct perm_value *v;
280 SidToString(cli, sidstr, &ace->trustee);
282 fprintf(f, "%s:", sidstr);
285 fprintf(f, "%d/0x%x/0x%08x",
286 ace->type, ace->flags, ace->access_mask);
292 if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED) {
293 fprintf(f, "ALLOWED");
294 } else if (ace->type == SEC_ACE_TYPE_ACCESS_DENIED) {
295 fprintf(f, "DENIED");
297 fprintf(f, "%d", ace->type);
300 print_ace_flags(f, ace->flags);
302 /* Standard permissions */
304 for (v = standard_values; v->perm; v++) {
305 if (ace->access_mask == v->mask) {
306 fprintf(f, "%s", v->perm);
311 /* Special permissions. Print out a hex value if we have
312 leftover bits in the mask. */
314 got_mask = ace->access_mask;
317 for (v = special_values; v->perm; v++) {
318 if ((ace->access_mask & v->mask) == v->mask) {
320 fprintf(f, "%s", v->perm);
322 got_mask &= ~v->mask;
328 fprintf(f, "0x%08x", ace->access_mask);
336 static bool parse_ace_flags(const char *str, unsigned int *pflags)
342 if (strnequal(p, "OI", 2)) {
343 *pflags |= SEC_ACE_FLAG_OBJECT_INHERIT;
345 } else if (strnequal(p, "CI", 2)) {
346 *pflags |= SEC_ACE_FLAG_CONTAINER_INHERIT;
348 } else if (strnequal(p, "NP", 2)) {
349 *pflags |= SEC_ACE_FLAG_NO_PROPAGATE_INHERIT;
351 } else if (strnequal(p, "IO", 2)) {
352 *pflags |= SEC_ACE_FLAG_INHERIT_ONLY;
354 } else if (*p == 'I') {
355 *pflags |= SEC_ACE_FLAG_INHERITED_ACE;
361 if (*p != '|' && *p != '\0') {
368 /* parse an ACE in the same format as print_ace() */
369 static bool parse_ace(struct cli_state *cli, struct security_ace *ace,
370 const char *orig_str)
375 unsigned int atype = 0;
376 unsigned int aflags = 0;
377 unsigned int amask = 0;
380 const struct perm_value *v;
381 char *str = SMB_STRDUP(orig_str);
382 TALLOC_CTX *frame = talloc_stackframe();
390 p = strchr_m(str,':');
392 printf("ACE '%s': missing ':'.\n", orig_str);
399 /* Try to parse numeric form */
401 if (sscanf(p, "%i/%i/%i", &atype, &aflags, &amask) == 3 &&
402 StringToSid(cli, &sid, str)) {
406 /* Try to parse text form */
408 if (!StringToSid(cli, &sid, str)) {
409 printf("ACE '%s': failed to convert '%s' to SID\n",
417 if (!next_token_talloc(frame, &cp, &tok, "/")) {
418 printf("ACE '%s': failed to find '/' character.\n",
425 if (strncmp(tok, "ALLOWED", strlen("ALLOWED")) == 0) {
426 atype = SEC_ACE_TYPE_ACCESS_ALLOWED;
427 } else if (strncmp(tok, "DENIED", strlen("DENIED")) == 0) {
428 atype = SEC_ACE_TYPE_ACCESS_DENIED;
430 printf("ACE '%s': missing 'ALLOWED' or 'DENIED' entry at '%s'\n",
437 /* Only numeric form accepted for flags at present */
439 if (!next_token_talloc(frame, &cp, &tok, "/")) {
440 printf("ACE '%s': bad flags entry at '%s'\n",
447 if (tok[0] < '0' || tok[0] > '9') {
448 if (!parse_ace_flags(tok, &aflags)) {
449 printf("ACE '%s': bad named flags entry at '%s'\n",
455 } else if (strnequal(tok, "0x", 2)) {
456 if (!sscanf(tok, "%x", &aflags)) {
457 printf("ACE '%s': bad hex flags entry at '%s'\n",
464 if (!sscanf(tok, "%i", &aflags)) {
465 printf("ACE '%s': bad integer flags entry at '%s'\n",
473 if (!next_token_talloc(frame, &cp, &tok, "/")) {
474 printf("ACE '%s': missing / at '%s'\n",
481 if (strncmp(tok, "0x", 2) == 0) {
482 if (sscanf(tok, "%i", &amask) != 1) {
483 printf("ACE '%s': bad hex number at '%s'\n",
492 for (v = standard_values; v->perm; v++) {
493 if (strcmp(tok, v->perm) == 0) {
504 for (v = special_values; v->perm; v++) {
505 if (v->perm[0] == *p) {
512 printf("ACE '%s': bad permission value at '%s'\n",
529 init_sec_ace(ace, &sid, atype, mask, aflags);
535 /* add an ACE to a list of ACEs in a struct security_acl */
536 static bool add_ace(struct security_acl **the_acl, struct security_ace *ace)
538 struct security_acl *new_ace;
539 struct security_ace *aces;
541 return (((*the_acl) = make_sec_acl(talloc_tos(), 3, 1, ace))
545 if (!(aces = SMB_CALLOC_ARRAY(struct security_ace, 1+(*the_acl)->num_aces))) {
548 memcpy(aces, (*the_acl)->aces, (*the_acl)->num_aces * sizeof(struct
550 memcpy(aces+(*the_acl)->num_aces, ace, sizeof(struct security_ace));
551 new_ace = make_sec_acl(talloc_tos(),(*the_acl)->revision,1+(*the_acl)->num_aces, aces);
553 (*the_acl) = new_ace;
557 /* parse a ascii version of a security descriptor */
558 static struct security_descriptor *sec_desc_parse(TALLOC_CTX *ctx, struct cli_state *cli, char *str)
562 struct security_descriptor *ret = NULL;
564 struct dom_sid *grp_sid=NULL, *owner_sid=NULL;
565 struct security_acl *dacl=NULL;
568 while (next_token_talloc(ctx, &p, &tok, "\t,\r\n")) {
569 if (strncmp(tok,"REVISION:", 9) == 0) {
570 revision = strtol(tok+9, NULL, 16);
574 if (strncmp(tok,"OWNER:", 6) == 0) {
576 printf("Only specify owner once\n");
579 owner_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
581 !StringToSid(cli, owner_sid, tok+6)) {
582 printf("Failed to parse owner sid\n");
588 if (strncmp(tok,"GROUP:", 6) == 0) {
590 printf("Only specify group once\n");
593 grp_sid = SMB_CALLOC_ARRAY(struct dom_sid, 1);
595 !StringToSid(cli, grp_sid, tok+6)) {
596 printf("Failed to parse group sid\n");
602 if (strncmp(tok,"ACL:", 4) == 0) {
603 struct security_ace ace;
604 if (!parse_ace(cli, &ace, tok+4)) {
607 if(!add_ace(&dacl, &ace)) {
608 printf("Failed to add ACL %s\n", tok);
614 printf("Failed to parse token '%s' in security descriptor,\n", tok);
618 ret = make_sec_desc(ctx,revision, SEC_DESC_SELF_RELATIVE, owner_sid, grp_sid,
619 NULL, dacl, &sd_size);
623 SAFE_FREE(owner_sid);
629 /* print a ascii version of a security descriptor on a FILE handle */
630 static void sec_desc_print(struct cli_state *cli, FILE *f, struct security_descriptor *sd)
635 fprintf(f, "REVISION:%d\n", sd->revision);
636 fprintf(f, "CONTROL:0x%x\n", sd->type);
638 /* Print owner and group sid */
641 SidToString(cli, sidstr, sd->owner_sid);
646 fprintf(f, "OWNER:%s\n", sidstr);
649 SidToString(cli, sidstr, sd->group_sid);
654 fprintf(f, "GROUP:%s\n", sidstr);
657 for (i = 0; sd->dacl && i < sd->dacl->num_aces; i++) {
658 struct security_ace *ace = &sd->dacl->aces[i];
660 print_ace(cli, f, ace);
666 /*****************************************************
667 get fileinfo for filename
668 *******************************************************/
669 static uint16 get_fileinfo(struct cli_state *cli, const char *filename)
671 uint16_t fnum = (uint16_t)-1;
674 /* The desired access below is the only one I could find that works
675 with NT4, W2KP and Samba */
677 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, filename, 0, CREATE_ACCESS_READ,
678 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
679 FILE_OPEN, 0x0, 0x0, &fnum))) {
680 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
683 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
684 cli, fnum, &mode, NULL, NULL, NULL,
685 NULL, NULL, NULL))) {
686 printf("Failed to file info %s: %s\n", filename,
690 cli_close(cli, fnum);
695 /*****************************************************
696 get sec desc for filename
697 *******************************************************/
698 static struct security_descriptor *get_secdesc(struct cli_state *cli, const char *filename)
700 uint16_t fnum = (uint16_t)-1;
701 struct security_descriptor *sd;
703 /* The desired access below is the only one I could find that works
704 with NT4, W2KP and Samba */
706 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, filename, 0, CREATE_ACCESS_READ,
707 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
708 FILE_OPEN, 0x0, 0x0, &fnum))) {
709 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
713 sd = cli_query_secdesc(cli, fnum, talloc_tos());
715 cli_close(cli, fnum);
718 printf("Failed to get security descriptor\n");
724 /*****************************************************
725 set sec desc for filename
726 *******************************************************/
727 static bool set_secdesc(struct cli_state *cli, const char *filename,
728 struct security_descriptor *sd)
730 uint16_t fnum = (uint16_t)-1;
733 /* The desired access below is the only one I could find that works
734 with NT4, W2KP and Samba */
736 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, filename, 0,
737 WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS,
738 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
739 FILE_OPEN, 0x0, 0x0, &fnum))) {
740 printf("Failed to open %s: %s\n", filename, cli_errstr(cli));
744 if (!cli_set_secdesc(cli, fnum, sd)) {
745 printf("ERROR: security description set failed: %s\n",
750 cli_close(cli, fnum);
754 /*****************************************************
755 dump the acls for a file
756 *******************************************************/
757 static int cacl_dump(struct cli_state *cli, const char *filename)
759 int result = EXIT_FAILED;
760 struct security_descriptor *sd;
765 sd = get_secdesc(cli, filename);
769 printf("%s\n", sddl_encode(talloc_tos(), sd,
770 get_global_sam_sid()));
772 sec_desc_print(cli, stdout, sd);
780 /*****************************************************
781 Change the ownership or group ownership of a file. Just
782 because the NT docs say this can't be done :-). JRA.
783 *******************************************************/
785 static int owner_set(struct cli_state *cli, enum chown_mode change_mode,
786 const char *filename, const char *new_username)
789 struct security_descriptor *sd, *old;
792 if (!StringToSid(cli, &sid, new_username))
793 return EXIT_PARSE_ERROR;
795 old = get_secdesc(cli, filename);
801 sd = make_sec_desc(talloc_tos(),old->revision, old->type,
802 (change_mode == REQUEST_CHOWN) ? &sid : NULL,
803 (change_mode == REQUEST_CHGRP) ? &sid : NULL,
804 NULL, NULL, &sd_size);
806 if (!set_secdesc(cli, filename, sd)) {
814 /* The MSDN is contradictory over the ordering of ACE entries in an
815 ACL. However NT4 gives a "The information may have been modified
816 by a computer running Windows NT 5.0" if denied ACEs do not appear
817 before allowed ACEs. At
818 http://technet.microsoft.com/en-us/library/cc781716.aspx the
819 canonical order is specified as "Explicit Deny, Explicit Allow,
820 Inherited ACEs unchanged" */
822 static int ace_compare(struct security_ace *ace1, struct security_ace *ace2)
824 if (sec_ace_equal(ace1, ace2))
827 if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
828 !(ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
830 if (!(ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
831 (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
833 if ((ace1->flags & SEC_ACE_FLAG_INHERITED_ACE) &&
834 (ace2->flags & SEC_ACE_FLAG_INHERITED_ACE))
837 if (ace1->type != ace2->type)
838 return ace2->type - ace1->type;
840 if (dom_sid_compare(&ace1->trustee, &ace2->trustee))
841 return dom_sid_compare(&ace1->trustee, &ace2->trustee);
843 if (ace1->flags != ace2->flags)
844 return ace1->flags - ace2->flags;
846 if (ace1->access_mask != ace2->access_mask)
847 return ace1->access_mask - ace2->access_mask;
849 if (ace1->size != ace2->size)
850 return ace1->size - ace2->size;
852 return memcmp(ace1, ace2, sizeof(struct security_ace));
855 static void sort_acl(struct security_acl *the_acl)
858 if (!the_acl) return;
860 TYPESAFE_QSORT(the_acl->aces, the_acl->num_aces, ace_compare);
862 for (i=1;i<the_acl->num_aces;) {
863 if (sec_ace_equal(&the_acl->aces[i-1], &the_acl->aces[i])) {
865 for (j=i; j<the_acl->num_aces-1; j++) {
866 the_acl->aces[j] = the_acl->aces[j+1];
875 /*****************************************************
876 set the ACLs on a file given an ascii description
877 *******************************************************/
879 static int cacl_set(struct cli_state *cli, const char *filename,
880 char *the_acl, enum acl_mode mode)
882 struct security_descriptor *sd, *old;
885 int result = EXIT_OK;
888 sd = sddl_decode(talloc_tos(), the_acl, get_global_sam_sid());
890 sd = sec_desc_parse(talloc_tos(), cli, the_acl);
893 if (!sd) return EXIT_PARSE_ERROR;
894 if (test_args) return EXIT_OK;
896 old = get_secdesc(cli, filename);
902 /* the logic here is rather more complex than I would like */
905 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
908 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
909 if (sec_ace_equal(&sd->dacl->aces[i],
910 &old->dacl->aces[j])) {
912 for (k=j; k<old->dacl->num_aces-1;k++) {
913 old->dacl->aces[k] = old->dacl->aces[k+1];
915 old->dacl->num_aces--;
922 printf("ACL for ACE:");
923 print_ace(cli, stdout, &sd->dacl->aces[i]);
924 printf(" not found\n");
930 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
933 for (j=0;old->dacl && j<old->dacl->num_aces;j++) {
934 if (dom_sid_equal(&sd->dacl->aces[i].trustee,
935 &old->dacl->aces[j].trustee)) {
936 old->dacl->aces[j] = sd->dacl->aces[i];
944 SidToString(cli, str,
945 &sd->dacl->aces[i].trustee);
946 printf("ACL for SID %s not found\n", str);
951 old->owner_sid = sd->owner_sid;
955 old->group_sid = sd->group_sid;
961 for (i=0;sd->dacl && i<sd->dacl->num_aces;i++) {
962 add_ace(&old->dacl, &sd->dacl->aces[i]);
971 /* Denied ACE entries must come before allowed ones */
974 /* Create new security descriptor and set it */
976 /* We used to just have "WRITE_DAC_ACCESS" without WRITE_OWNER.
977 But if we're sending an owner, even if it's the same as the one
978 that already exists then W2K3 insists we open with WRITE_OWNER access.
979 I need to check that setting a SD with no owner set works against WNT
983 sd = make_sec_desc(talloc_tos(),old->revision, old->type,
984 old->owner_sid, old->group_sid,
985 NULL, old->dacl, &sd_size);
987 if (!set_secdesc(cli, filename, sd)) {
988 result = EXIT_FAILED;
994 /*****************************************************
995 set the inherit on a file
996 *******************************************************/
997 static int inherit(struct cli_state *cli, const char *filename,
1000 struct security_descriptor *old,*sd;
1003 int result = EXIT_OK;
1005 old = get_secdesc(cli, filename);
1011 oldattr = get_fileinfo(cli,filename);
1013 if (strcmp(type,"allow")==0) {
1014 if ((old->type & SEC_DESC_DACL_PROTECTED) ==
1015 SEC_DESC_DACL_PROTECTED) {
1017 char *parentname,*temp;
1018 struct security_descriptor *parent;
1019 temp = talloc_strdup(talloc_tos(), filename);
1021 old->type=old->type & (~SEC_DESC_DACL_PROTECTED);
1023 /* look at parent and copy in all its inheritable ACL's. */
1024 string_replace(temp, '\\', '/');
1025 if (!parent_dirname(talloc_tos(),temp,&parentname,NULL)) {
1028 string_replace(parentname, '/', '\\');
1029 parent = get_secdesc(cli,parentname);
1030 for (i=0;i<parent->dacl->num_aces;i++) {
1031 struct security_ace *ace=&parent->dacl->aces[i];
1032 /* Add inherited flag to all aces */
1033 ace->flags=ace->flags|
1034 SEC_ACE_FLAG_INHERITED_ACE;
1035 if ((oldattr & aDIR) == aDIR) {
1036 if ((ace->flags & SEC_ACE_FLAG_CONTAINER_INHERIT) ==
1037 SEC_ACE_FLAG_CONTAINER_INHERIT) {
1038 add_ace(&old->dacl, ace);
1041 if ((ace->flags & SEC_ACE_FLAG_OBJECT_INHERIT) ==
1042 SEC_ACE_FLAG_OBJECT_INHERIT) {
1043 /* clear flags for files */
1045 add_ace(&old->dacl, ace);
1050 printf("Already set to inheritable permissions.\n");
1053 } else if (strcmp(type,"remove")==0) {
1054 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
1055 SEC_DESC_DACL_PROTECTED) {
1056 old->type=old->type | SEC_DESC_DACL_PROTECTED;
1058 /* remove all inherited ACL's. */
1061 struct security_acl *temp=old->dacl;
1062 old->dacl=make_sec_acl(talloc_tos(), 3, 0, NULL);
1063 for (i=temp->num_aces-1;i>=0;i--) {
1064 struct security_ace *ace=&temp->aces[i];
1065 /* Remove all ace with INHERITED flag set */
1066 if ((ace->flags & SEC_ACE_FLAG_INHERITED_ACE) !=
1067 SEC_ACE_FLAG_INHERITED_ACE) {
1068 add_ace(&old->dacl,ace);
1073 printf("Already set to no inheritable permissions.\n");
1076 } else if (strcmp(type,"copy")==0) {
1077 if ((old->type & SEC_DESC_DACL_PROTECTED) !=
1078 SEC_DESC_DACL_PROTECTED) {
1079 old->type=old->type | SEC_DESC_DACL_PROTECTED;
1081 /* convert all inherited ACL's to non inherated ACL's. */
1084 for (i=0;i<old->dacl->num_aces;i++) {
1085 struct security_ace *ace=&old->dacl->aces[i];
1086 /* Remove INHERITED FLAG from all aces */
1087 ace->flags=ace->flags&(~SEC_ACE_FLAG_INHERITED_ACE);
1091 printf("Already set to no inheritable permissions.\n");
1096 /* Denied ACE entries must come before allowed ones */
1097 sort_acl(old->dacl);
1099 sd = make_sec_desc(talloc_tos(),old->revision, old->type,
1100 old->owner_sid, old->group_sid,
1101 NULL, old->dacl, &sd_size);
1103 if (!set_secdesc(cli, filename, sd)) {
1104 result = EXIT_FAILED;
1110 /*****************************************************
1111 Return a connection to a server.
1112 *******************************************************/
1113 static struct cli_state *connect_one(struct user_auth_info *auth_info,
1114 const char *server, const char *share)
1116 struct cli_state *c = NULL;
1117 struct sockaddr_storage ss;
1123 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
1124 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
1125 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1128 if (get_cmdline_auth_info_use_machine_account(auth_info) &&
1129 !set_cmdline_auth_info_machine_account_creds(auth_info)) {
1133 set_cmdline_auth_info_getpass(auth_info);
1135 nt_status = cli_full_connection(&c, global_myname(), server,
1138 get_cmdline_auth_info_username(auth_info),
1140 get_cmdline_auth_info_password(auth_info),
1142 get_cmdline_auth_info_signing_state(auth_info),
1144 if (!NT_STATUS_IS_OK(nt_status)) {
1145 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status)));
1149 if (get_cmdline_auth_info_smb_encrypt(auth_info)) {
1150 nt_status = cli_cm_force_encryption(c,
1151 get_cmdline_auth_info_username(auth_info),
1152 get_cmdline_auth_info_password(auth_info),
1155 if (!NT_STATUS_IS_OK(nt_status)) {
1164 /****************************************************************************
1166 ****************************************************************************/
1167 int main(int argc, const char *argv[])
1171 enum acl_mode mode = SMB_ACL_SET;
1172 static char *the_acl = NULL;
1173 enum chown_mode change_mode = REQUEST_NONE;
1176 char *filename = NULL;
1178 struct poptOption long_options[] = {
1180 { "delete", 'D', POPT_ARG_STRING, NULL, 'D', "Delete an acl", "ACL" },
1181 { "modify", 'M', POPT_ARG_STRING, NULL, 'M', "Modify an acl", "ACL" },
1182 { "add", 'a', POPT_ARG_STRING, NULL, 'a', "Add an acl", "ACL" },
1183 { "set", 'S', POPT_ARG_STRING, NULL, 'S', "Set acls", "ACLS" },
1184 { "chown", 'C', POPT_ARG_STRING, NULL, 'C', "Change ownership of a file", "USERNAME" },
1185 { "chgrp", 'G', POPT_ARG_STRING, NULL, 'G', "Change group ownership of a file", "GROUPNAME" },
1186 { "inherit", 'I', POPT_ARG_STRING, NULL, 'I', "Inherit allow|remove|copy" },
1187 { "numeric", 0, POPT_ARG_NONE, &numeric, 1, "Don't resolve sids or masks to names" },
1188 { "sddl", 0, POPT_ARG_NONE, &sddl, 1, "Output and input acls in sddl format" },
1189 { "test-args", 't', POPT_ARG_NONE, &test_args, 1, "Test arguments"},
1191 POPT_COMMON_CONNECTION
1192 POPT_COMMON_CREDENTIALS
1196 struct cli_state *cli;
1197 TALLOC_CTX *frame = talloc_stackframe();
1198 const char *owner_username = "";
1200 struct user_auth_info *auth_info;
1205 /* set default debug level to 1 regardless of what smb.conf sets */
1206 setup_logging( "smbcacls", True );
1207 DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
1209 x_setbuf( x_stderr, NULL );
1210 AllowDebugChange = false;
1214 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
1217 auth_info = user_auth_info_init(frame);
1218 if (auth_info == NULL) {
1221 popt_common_set_auth_info(auth_info);
1223 pc = poptGetContext("smbcacls", argc, argv, long_options, 0);
1225 poptSetOtherOptionHelp(pc, "//server1/share1 filename\nACLs look like: "
1226 "'ACL:user:[ALLOWED|DENIED]/flags/permissions'");
1228 while ((opt = poptGetNextOpt(pc)) != -1) {
1231 the_acl = smb_xstrdup(poptGetOptArg(pc));
1236 the_acl = smb_xstrdup(poptGetOptArg(pc));
1237 mode = SMB_ACL_DELETE;
1241 the_acl = smb_xstrdup(poptGetOptArg(pc));
1242 mode = SMB_ACL_MODIFY;
1246 the_acl = smb_xstrdup(poptGetOptArg(pc));
1251 owner_username = poptGetOptArg(pc);
1252 change_mode = REQUEST_CHOWN;
1256 owner_username = poptGetOptArg(pc);
1257 change_mode = REQUEST_CHGRP;
1261 owner_username = poptGetOptArg(pc);
1262 change_mode = REQUEST_INHERIT;
1267 /* Make connection to server */
1268 if(!poptPeekArg(pc)) {
1269 poptPrintUsage(pc, stderr, 0);
1273 path = talloc_strdup(frame, poptGetArg(pc));
1278 if(!poptPeekArg(pc)) {
1279 poptPrintUsage(pc, stderr, 0);
1283 filename = talloc_strdup(frame, poptGetArg(pc));
1288 string_replace(path,'/','\\');
1290 server = talloc_strdup(frame, path+2);
1294 share = strchr_m(server,'\\');
1296 printf("Invalid argument: %s\n", share);
1304 cli = connect_one(auth_info, server, share);
1312 string_replace(filename, '/', '\\');
1313 if (filename[0] != '\\') {
1314 filename = talloc_asprintf(frame,
1322 /* Perform requested action */
1324 if (change_mode == REQUEST_INHERIT) {
1325 result = inherit(cli, filename, owner_username);
1326 } else if (change_mode != REQUEST_NONE) {
1327 result = owner_set(cli, change_mode, filename, owner_username);
1328 } else if (the_acl) {
1329 result = cacl_set(cli, filename, the_acl, mode);
1331 result = cacl_dump(cli, filename);