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 "rpc_client/cli_pipe.h"
27 #include "../librpc/gen_ndr/ndr_lsa.h"
28 #include "rpc_client/cli_lsarpc.h"
29 #include "../libcli/security/security.h"
30 #include "libsmb/libsmb.h"
31 #include "libsmb/clirap.h"
32 #include "passdb/machine_sid.h"
36 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
38 /* numeric is set when the user wants numeric SIDs and ACEs rather
39 than going via LSA calls to resolve them */
44 enum acl_mode
{SMB_ACL_SET
, SMB_ACL_DELETE
, SMB_ACL_MODIFY
, SMB_ACL_ADD
};
45 enum chown_mode
{REQUEST_NONE
, REQUEST_CHOWN
, REQUEST_CHGRP
, REQUEST_INHERIT
};
46 enum exit_values
{EXIT_OK
, EXIT_FAILED
, EXIT_PARSE_ERROR
};
53 /* These values discovered by inspection */
55 static const struct perm_value special_values
[] = {
65 static const struct perm_value standard_values
[] = {
66 { "READ", 0x001200a9 },
67 { "CHANGE", 0x001301bf },
68 { "FULL", 0x001f01ff },
72 /* Open cli connection and policy handle */
74 static NTSTATUS
cli_lsa_lookup_sid(struct cli_state
*cli
,
75 const struct dom_sid
*sid
,
77 enum lsa_SidType
*type
,
78 char **domain
, char **name
)
80 uint16 orig_cnum
= cli_state_get_tid(cli
);
81 struct rpc_pipe_client
*p
= NULL
;
82 struct policy_handle handle
;
84 TALLOC_CTX
*frame
= talloc_stackframe();
85 enum lsa_SidType
*types
;
89 status
= cli_tcon_andx(cli
, "IPC$", "?????", "", 0);
90 if (!NT_STATUS_IS_OK(status
)) {
94 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
96 if (!NT_STATUS_IS_OK(status
)) {
100 status
= rpccli_lsa_open_policy(p
, talloc_tos(), True
,
101 GENERIC_EXECUTE_ACCESS
, &handle
);
102 if (!NT_STATUS_IS_OK(status
)) {
106 status
= rpccli_lsa_lookup_sids(p
, talloc_tos(), &handle
, 1, sid
,
107 &domains
, &names
, &types
);
108 if (!NT_STATUS_IS_OK(status
)) {
113 *domain
= talloc_move(mem_ctx
, &domains
[0]);
114 *name
= talloc_move(mem_ctx
, &names
[0]);
116 status
= NT_STATUS_OK
;
120 cli_state_set_tid(cli
, orig_cnum
);
125 static NTSTATUS
cli_lsa_lookup_name(struct cli_state
*cli
,
127 enum lsa_SidType
*type
,
130 uint16 orig_cnum
= cli_state_get_tid(cli
);
131 struct rpc_pipe_client
*p
;
132 struct policy_handle handle
;
134 TALLOC_CTX
*frame
= talloc_stackframe();
135 struct dom_sid
*sids
;
136 enum lsa_SidType
*types
;
138 status
= cli_tcon_andx(cli
, "IPC$", "?????", "", 0);
139 if (!NT_STATUS_IS_OK(status
)) {
143 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
145 if (!NT_STATUS_IS_OK(status
)) {
149 status
= rpccli_lsa_open_policy(p
, talloc_tos(), True
,
150 GENERIC_EXECUTE_ACCESS
, &handle
);
151 if (!NT_STATUS_IS_OK(status
)) {
155 status
= rpccli_lsa_lookup_names(p
, talloc_tos(), &handle
, 1, &name
,
156 NULL
, 1, &sids
, &types
);
157 if (!NT_STATUS_IS_OK(status
)) {
164 status
= NT_STATUS_OK
;
168 cli_state_set_tid(cli
, orig_cnum
);
173 /* convert a SID to a string, either numeric or username/group */
174 static void SidToString(struct cli_state
*cli
, fstring str
, const struct dom_sid
*sid
)
178 enum lsa_SidType type
;
181 sid_to_fstring(str
, sid
);
187 status
= cli_lsa_lookup_sid(cli
, sid
, talloc_tos(), &type
,
190 if (!NT_STATUS_IS_OK(status
)) {
195 slprintf(str
, sizeof(fstring
) - 1, "%s%s%s",
196 domain
, lp_winbind_separator(), name
);
202 /* convert a string to a SID, either numeric or username/group */
203 static bool StringToSid(struct cli_state
*cli
, struct dom_sid
*sid
, const char *str
)
205 enum lsa_SidType type
;
207 if (string_to_sid(sid
, str
)) {
211 return NT_STATUS_IS_OK(cli_lsa_lookup_name(cli
, str
, &type
, sid
));
214 static void print_ace_flags(FILE *f
, uint8_t flags
)
216 char *str
= talloc_strdup(NULL
, "");
222 if (flags
& SEC_ACE_FLAG_OBJECT_INHERIT
) {
223 str
= talloc_asprintf(str
, "%s%s",
229 if (flags
& SEC_ACE_FLAG_CONTAINER_INHERIT
) {
230 str
= talloc_asprintf(str
, "%s%s",
236 if (flags
& SEC_ACE_FLAG_NO_PROPAGATE_INHERIT
) {
237 str
= talloc_asprintf(str
, "%s%s",
243 if (flags
& SEC_ACE_FLAG_INHERIT_ONLY
) {
244 str
= talloc_asprintf(str
, "%s%s",
250 if (flags
& SEC_ACE_FLAG_INHERITED_ACE
) {
251 str
= talloc_asprintf(str
, "%s%s",
257 /* Ignore define SEC_ACE_FLAG_SUCCESSFUL_ACCESS ( 0x40 )
258 and SEC_ACE_FLAG_FAILED_ACCESS ( 0x80 ) as they're
261 if (str
[strlen(str
)-1] == '|') {
262 str
[strlen(str
)-1] = '\0';
263 fprintf(f
, "/%s/", str
);
265 fprintf(f
, "/0x%x/", flags
);
271 fprintf(f
, "/0x%x/", flags
);
274 /* print an ACE on a FILE, using either numeric or ascii representation */
275 static void print_ace(struct cli_state
*cli
, FILE *f
, struct security_ace
*ace
)
277 const struct perm_value
*v
;
282 SidToString(cli
, sidstr
, &ace
->trustee
);
284 fprintf(f
, "%s:", sidstr
);
287 fprintf(f
, "%d/0x%x/0x%08x",
288 ace
->type
, ace
->flags
, ace
->access_mask
);
294 if (ace
->type
== SEC_ACE_TYPE_ACCESS_ALLOWED
) {
295 fprintf(f
, "ALLOWED");
296 } else if (ace
->type
== SEC_ACE_TYPE_ACCESS_DENIED
) {
297 fprintf(f
, "DENIED");
299 fprintf(f
, "%d", ace
->type
);
302 print_ace_flags(f
, ace
->flags
);
304 /* Standard permissions */
306 for (v
= standard_values
; v
->perm
; v
++) {
307 if (ace
->access_mask
== v
->mask
) {
308 fprintf(f
, "%s", v
->perm
);
313 /* Special permissions. Print out a hex value if we have
314 leftover bits in the mask. */
316 got_mask
= ace
->access_mask
;
319 for (v
= special_values
; v
->perm
; v
++) {
320 if ((ace
->access_mask
& v
->mask
) == v
->mask
) {
322 fprintf(f
, "%s", v
->perm
);
324 got_mask
&= ~v
->mask
;
330 fprintf(f
, "0x%08x", ace
->access_mask
);
338 static bool parse_ace_flags(const char *str
, unsigned int *pflags
)
344 if (strnequal(p
, "OI", 2)) {
345 *pflags
|= SEC_ACE_FLAG_OBJECT_INHERIT
;
347 } else if (strnequal(p
, "CI", 2)) {
348 *pflags
|= SEC_ACE_FLAG_CONTAINER_INHERIT
;
350 } else if (strnequal(p
, "NP", 2)) {
351 *pflags
|= SEC_ACE_FLAG_NO_PROPAGATE_INHERIT
;
353 } else if (strnequal(p
, "IO", 2)) {
354 *pflags
|= SEC_ACE_FLAG_INHERIT_ONLY
;
356 } else if (*p
== 'I') {
357 *pflags
|= SEC_ACE_FLAG_INHERITED_ACE
;
375 /* parse an ACE in the same format as print_ace() */
376 static bool parse_ace(struct cli_state
*cli
, struct security_ace
*ace
,
377 const char *orig_str
)
382 unsigned int atype
= 0;
383 unsigned int aflags
= 0;
384 unsigned int amask
= 0;
387 const struct perm_value
*v
;
388 char *str
= SMB_STRDUP(orig_str
);
389 TALLOC_CTX
*frame
= talloc_stackframe();
397 p
= strchr_m(str
,':');
399 printf("ACE '%s': missing ':'.\n", orig_str
);
406 /* Try to parse numeric form */
408 if (sscanf(p
, "%i/%i/%i", &atype
, &aflags
, &amask
) == 3 &&
409 StringToSid(cli
, &sid
, str
)) {
413 /* Try to parse text form */
415 if (!StringToSid(cli
, &sid
, str
)) {
416 printf("ACE '%s': failed to convert '%s' to SID\n",
424 if (!next_token_talloc(frame
, &cp
, &tok
, "/")) {
425 printf("ACE '%s': failed to find '/' character.\n",
432 if (strncmp(tok
, "ALLOWED", strlen("ALLOWED")) == 0) {
433 atype
= SEC_ACE_TYPE_ACCESS_ALLOWED
;
434 } else if (strncmp(tok
, "DENIED", strlen("DENIED")) == 0) {
435 atype
= SEC_ACE_TYPE_ACCESS_DENIED
;
437 printf("ACE '%s': missing 'ALLOWED' or 'DENIED' entry at '%s'\n",
444 /* Only numeric form accepted for flags at present */
446 if (!next_token_talloc(frame
, &cp
, &tok
, "/")) {
447 printf("ACE '%s': bad flags entry at '%s'\n",
454 if (tok
[0] < '0' || tok
[0] > '9') {
455 if (!parse_ace_flags(tok
, &aflags
)) {
456 printf("ACE '%s': bad named flags entry at '%s'\n",
462 } else if (strnequal(tok
, "0x", 2)) {
463 if (!sscanf(tok
, "%x", &aflags
)) {
464 printf("ACE '%s': bad hex flags entry at '%s'\n",
471 if (!sscanf(tok
, "%i", &aflags
)) {
472 printf("ACE '%s': bad integer flags entry at '%s'\n",
480 if (!next_token_talloc(frame
, &cp
, &tok
, "/")) {
481 printf("ACE '%s': missing / at '%s'\n",
488 if (strncmp(tok
, "0x", 2) == 0) {
489 if (sscanf(tok
, "%i", &amask
) != 1) {
490 printf("ACE '%s': bad hex number at '%s'\n",
499 for (v
= standard_values
; v
->perm
; v
++) {
500 if (strcmp(tok
, v
->perm
) == 0) {
511 for (v
= special_values
; v
->perm
; v
++) {
512 if (v
->perm
[0] == *p
) {
519 printf("ACE '%s': bad permission value at '%s'\n",
536 init_sec_ace(ace
, &sid
, atype
, mask
, aflags
);
542 /* add an ACE to a list of ACEs in a struct security_acl */
543 static bool add_ace(struct security_acl
**the_acl
, struct security_ace
*ace
)
545 struct security_acl
*new_ace
;
546 struct security_ace
*aces
;
548 return (((*the_acl
) = make_sec_acl(talloc_tos(), 3, 1, ace
))
552 if (!(aces
= SMB_CALLOC_ARRAY(struct security_ace
, 1+(*the_acl
)->num_aces
))) {
555 memcpy(aces
, (*the_acl
)->aces
, (*the_acl
)->num_aces
* sizeof(struct
557 memcpy(aces
+(*the_acl
)->num_aces
, ace
, sizeof(struct security_ace
));
558 new_ace
= make_sec_acl(talloc_tos(),(*the_acl
)->revision
,1+(*the_acl
)->num_aces
, aces
);
560 (*the_acl
) = new_ace
;
564 /* parse a ascii version of a security descriptor */
565 static struct security_descriptor
*sec_desc_parse(TALLOC_CTX
*ctx
, struct cli_state
*cli
, char *str
)
569 struct security_descriptor
*ret
= NULL
;
571 struct dom_sid
*grp_sid
=NULL
, *owner_sid
=NULL
;
572 struct security_acl
*dacl
=NULL
;
575 while (next_token_talloc(ctx
, &p
, &tok
, "\t,\r\n")) {
576 if (strncmp(tok
,"REVISION:", 9) == 0) {
577 revision
= strtol(tok
+9, NULL
, 16);
581 if (strncmp(tok
,"OWNER:", 6) == 0) {
583 printf("Only specify owner once\n");
586 owner_sid
= SMB_CALLOC_ARRAY(struct dom_sid
, 1);
588 !StringToSid(cli
, owner_sid
, tok
+6)) {
589 printf("Failed to parse owner sid\n");
595 if (strncmp(tok
,"GROUP:", 6) == 0) {
597 printf("Only specify group once\n");
600 grp_sid
= SMB_CALLOC_ARRAY(struct dom_sid
, 1);
602 !StringToSid(cli
, grp_sid
, tok
+6)) {
603 printf("Failed to parse group sid\n");
609 if (strncmp(tok
,"ACL:", 4) == 0) {
610 struct security_ace ace
;
611 if (!parse_ace(cli
, &ace
, tok
+4)) {
614 if(!add_ace(&dacl
, &ace
)) {
615 printf("Failed to add ACL %s\n", tok
);
621 printf("Failed to parse token '%s' in security descriptor,\n", tok
);
625 ret
= make_sec_desc(ctx
,revision
, SEC_DESC_SELF_RELATIVE
, owner_sid
, grp_sid
,
626 NULL
, dacl
, &sd_size
);
630 SAFE_FREE(owner_sid
);
635 static const struct {
639 } sec_desc_ctrl_bits
[] = {
640 {SEC_DESC_OWNER_DEFAULTED
, "OD", "Owner Defaulted"},
641 {SEC_DESC_GROUP_DEFAULTED
, "GD", "Group Defaulted"},
642 {SEC_DESC_DACL_PRESENT
, "DP", "DACL Present"},
643 {SEC_DESC_DACL_DEFAULTED
, "DD", "DACL Defaulted"},
644 {SEC_DESC_SACL_PRESENT
, "SP", "SACL Present"},
645 {SEC_DESC_SACL_DEFAULTED
, "SD", "SACL Defaulted"},
646 {SEC_DESC_DACL_TRUSTED
, "DT", "DACL Trusted"},
647 {SEC_DESC_SERVER_SECURITY
, "SS", "Server Security"},
648 {SEC_DESC_DACL_AUTO_INHERIT_REQ
, "DR", "DACL Inheritance Required"},
649 {SEC_DESC_SACL_AUTO_INHERIT_REQ
, "SR", "SACL Inheritance Required"},
650 {SEC_DESC_DACL_AUTO_INHERITED
, "DI", "DACL Auto Inherited"},
651 {SEC_DESC_SACL_AUTO_INHERITED
, "SI", "SACL Auto Inherited"},
652 {SEC_DESC_DACL_PROTECTED
, "PD", "DACL Protected"},
653 {SEC_DESC_SACL_PROTECTED
, "PS", "SACL Protected"},
654 {SEC_DESC_RM_CONTROL_VALID
, "RM", "RM Control Valid"},
655 {SEC_DESC_SELF_RELATIVE
, "SR", "Self Relative"},
658 static void print_acl_ctrl(FILE *file
, uint16_t ctrl
)
661 const char* separator
= "";
663 fprintf(file
, "CONTROL:");
665 fprintf(file
, "0x%x\n", ctrl
);
669 for (i
= ARRAY_SIZE(sec_desc_ctrl_bits
) - 1; i
>= 0; i
--) {
670 if (ctrl
& sec_desc_ctrl_bits
[i
].mask
) {
671 fprintf(file
, "%s%s", separator
, sec_desc_ctrl_bits
[i
].str
);
678 /* print a ascii version of a security descriptor on a FILE handle */
679 static void sec_desc_print(struct cli_state
*cli
, FILE *f
, struct security_descriptor
*sd
)
684 fprintf(f
, "REVISION:%d\n", sd
->revision
);
685 print_acl_ctrl(f
, sd
->type
);
687 /* Print owner and group sid */
690 SidToString(cli
, sidstr
, sd
->owner_sid
);
695 fprintf(f
, "OWNER:%s\n", sidstr
);
698 SidToString(cli
, sidstr
, sd
->group_sid
);
703 fprintf(f
, "GROUP:%s\n", sidstr
);
706 for (i
= 0; sd
->dacl
&& i
< sd
->dacl
->num_aces
; i
++) {
707 struct security_ace
*ace
= &sd
->dacl
->aces
[i
];
709 print_ace(cli
, f
, ace
);
715 /*****************************************************
716 get fileinfo for filename
717 *******************************************************/
718 static uint16
get_fileinfo(struct cli_state
*cli
, const char *filename
)
720 uint16_t fnum
= (uint16_t)-1;
724 /* The desired access below is the only one I could find that works
725 with NT4, W2KP and Samba */
727 status
= cli_ntcreate(cli
, filename
, 0, CREATE_ACCESS_READ
,
728 0, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
729 FILE_OPEN
, 0x0, 0x0, &fnum
);
730 if (!NT_STATUS_IS_OK(status
)) {
731 printf("Failed to open %s: %s\n", filename
, nt_errstr(status
));
735 status
= cli_qfileinfo_basic(cli
, fnum
, &mode
, NULL
, NULL
, NULL
,
737 if (!NT_STATUS_IS_OK(status
)) {
738 printf("Failed to file info %s: %s\n", filename
,
742 cli_close(cli
, fnum
);
747 /*****************************************************
748 get sec desc for filename
749 *******************************************************/
750 static struct security_descriptor
*get_secdesc(struct cli_state
*cli
, const char *filename
)
752 uint16_t fnum
= (uint16_t)-1;
753 struct security_descriptor
*sd
;
756 /* The desired access below is the only one I could find that works
757 with NT4, W2KP and Samba */
759 status
= cli_ntcreate(cli
, filename
, 0, CREATE_ACCESS_READ
,
760 0, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
761 FILE_OPEN
, 0x0, 0x0, &fnum
);
762 if (!NT_STATUS_IS_OK(status
)) {
763 printf("Failed to open %s: %s\n", filename
, nt_errstr(status
));
767 status
= cli_query_secdesc(cli
, fnum
, talloc_tos(), &sd
);
769 cli_close(cli
, fnum
);
771 if (!NT_STATUS_IS_OK(status
)) {
772 printf("Failed to get security descriptor: %s\n",
779 /*****************************************************
780 set sec desc for filename
781 *******************************************************/
782 static bool set_secdesc(struct cli_state
*cli
, const char *filename
,
783 struct security_descriptor
*sd
)
785 uint16_t fnum
= (uint16_t)-1;
789 /* The desired access below is the only one I could find that works
790 with NT4, W2KP and Samba */
792 status
= cli_ntcreate(cli
, filename
, 0,
793 WRITE_DAC_ACCESS
|WRITE_OWNER_ACCESS
,
794 0, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
795 FILE_OPEN
, 0x0, 0x0, &fnum
);
796 if (!NT_STATUS_IS_OK(status
)) {
797 printf("Failed to open %s: %s\n", filename
, nt_errstr(status
));
801 status
= cli_set_secdesc(cli
, fnum
, sd
);
802 if (!NT_STATUS_IS_OK(status
)) {
803 printf("ERROR: security description set failed: %s\n",
808 cli_close(cli
, fnum
);
812 /*****************************************************
813 dump the acls for a file
814 *******************************************************/
815 static int cacl_dump(struct cli_state
*cli
, const char *filename
)
817 int result
= EXIT_FAILED
;
818 struct security_descriptor
*sd
;
823 sd
= get_secdesc(cli
, filename
);
827 printf("%s\n", sddl_encode(talloc_tos(), sd
,
828 get_global_sam_sid()));
830 sec_desc_print(cli
, stdout
, sd
);
838 /*****************************************************
839 Change the ownership or group ownership of a file. Just
840 because the NT docs say this can't be done :-). JRA.
841 *******************************************************/
843 static int owner_set(struct cli_state
*cli
, enum chown_mode change_mode
,
844 const char *filename
, const char *new_username
)
847 struct security_descriptor
*sd
, *old
;
850 if (!StringToSid(cli
, &sid
, new_username
))
851 return EXIT_PARSE_ERROR
;
853 old
= get_secdesc(cli
, filename
);
859 sd
= make_sec_desc(talloc_tos(),old
->revision
, old
->type
,
860 (change_mode
== REQUEST_CHOWN
) ? &sid
: NULL
,
861 (change_mode
== REQUEST_CHGRP
) ? &sid
: NULL
,
862 NULL
, NULL
, &sd_size
);
864 if (!set_secdesc(cli
, filename
, sd
)) {
872 /* The MSDN is contradictory over the ordering of ACE entries in an
873 ACL. However NT4 gives a "The information may have been modified
874 by a computer running Windows NT 5.0" if denied ACEs do not appear
875 before allowed ACEs. At
876 http://technet.microsoft.com/en-us/library/cc781716.aspx the
877 canonical order is specified as "Explicit Deny, Explicit Allow,
878 Inherited ACEs unchanged" */
880 static int ace_compare(struct security_ace
*ace1
, struct security_ace
*ace2
)
882 if (sec_ace_equal(ace1
, ace2
))
885 if ((ace1
->flags
& SEC_ACE_FLAG_INHERITED_ACE
) &&
886 !(ace2
->flags
& SEC_ACE_FLAG_INHERITED_ACE
))
888 if (!(ace1
->flags
& SEC_ACE_FLAG_INHERITED_ACE
) &&
889 (ace2
->flags
& SEC_ACE_FLAG_INHERITED_ACE
))
891 if ((ace1
->flags
& SEC_ACE_FLAG_INHERITED_ACE
) &&
892 (ace2
->flags
& SEC_ACE_FLAG_INHERITED_ACE
))
895 if (ace1
->type
!= ace2
->type
)
896 return ace2
->type
- ace1
->type
;
898 if (dom_sid_compare(&ace1
->trustee
, &ace2
->trustee
))
899 return dom_sid_compare(&ace1
->trustee
, &ace2
->trustee
);
901 if (ace1
->flags
!= ace2
->flags
)
902 return ace1
->flags
- ace2
->flags
;
904 if (ace1
->access_mask
!= ace2
->access_mask
)
905 return ace1
->access_mask
- ace2
->access_mask
;
907 if (ace1
->size
!= ace2
->size
)
908 return ace1
->size
- ace2
->size
;
910 return memcmp(ace1
, ace2
, sizeof(struct security_ace
));
913 static void sort_acl(struct security_acl
*the_acl
)
916 if (!the_acl
) return;
918 TYPESAFE_QSORT(the_acl
->aces
, the_acl
->num_aces
, ace_compare
);
920 for (i
=1;i
<the_acl
->num_aces
;) {
921 if (sec_ace_equal(&the_acl
->aces
[i
-1], &the_acl
->aces
[i
])) {
923 for (j
=i
; j
<the_acl
->num_aces
-1; j
++) {
924 the_acl
->aces
[j
] = the_acl
->aces
[j
+1];
933 /*****************************************************
934 set the ACLs on a file given an ascii description
935 *******************************************************/
937 static int cacl_set(struct cli_state
*cli
, const char *filename
,
938 char *the_acl
, enum acl_mode mode
)
940 struct security_descriptor
*sd
, *old
;
943 int result
= EXIT_OK
;
946 sd
= sddl_decode(talloc_tos(), the_acl
, get_global_sam_sid());
948 sd
= sec_desc_parse(talloc_tos(), cli
, the_acl
);
951 if (!sd
) return EXIT_PARSE_ERROR
;
952 if (test_args
) return EXIT_OK
;
954 old
= get_secdesc(cli
, filename
);
960 /* the logic here is rather more complex than I would like */
963 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
966 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
967 if (sec_ace_equal(&sd
->dacl
->aces
[i
],
968 &old
->dacl
->aces
[j
])) {
970 for (k
=j
; k
<old
->dacl
->num_aces
-1;k
++) {
971 old
->dacl
->aces
[k
] = old
->dacl
->aces
[k
+1];
973 old
->dacl
->num_aces
--;
980 printf("ACL for ACE:");
981 print_ace(cli
, stdout
, &sd
->dacl
->aces
[i
]);
982 printf(" not found\n");
988 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
991 for (j
=0;old
->dacl
&& j
<old
->dacl
->num_aces
;j
++) {
992 if (dom_sid_equal(&sd
->dacl
->aces
[i
].trustee
,
993 &old
->dacl
->aces
[j
].trustee
)) {
994 old
->dacl
->aces
[j
] = sd
->dacl
->aces
[i
];
1002 SidToString(cli
, str
,
1003 &sd
->dacl
->aces
[i
].trustee
);
1004 printf("ACL for SID %s not found\n", str
);
1008 if (sd
->owner_sid
) {
1009 old
->owner_sid
= sd
->owner_sid
;
1012 if (sd
->group_sid
) {
1013 old
->group_sid
= sd
->group_sid
;
1019 for (i
=0;sd
->dacl
&& i
<sd
->dacl
->num_aces
;i
++) {
1020 add_ace(&old
->dacl
, &sd
->dacl
->aces
[i
]);
1029 /* Denied ACE entries must come before allowed ones */
1030 sort_acl(old
->dacl
);
1032 /* Create new security descriptor and set it */
1034 /* We used to just have "WRITE_DAC_ACCESS" without WRITE_OWNER.
1035 But if we're sending an owner, even if it's the same as the one
1036 that already exists then W2K3 insists we open with WRITE_OWNER access.
1037 I need to check that setting a SD with no owner set works against WNT
1041 sd
= make_sec_desc(talloc_tos(),old
->revision
, old
->type
,
1042 old
->owner_sid
, old
->group_sid
,
1043 NULL
, old
->dacl
, &sd_size
);
1045 if (!set_secdesc(cli
, filename
, sd
)) {
1046 result
= EXIT_FAILED
;
1052 /*****************************************************
1053 set the inherit on a file
1054 *******************************************************/
1055 static int inherit(struct cli_state
*cli
, const char *filename
,
1058 struct security_descriptor
*old
,*sd
;
1061 int result
= EXIT_OK
;
1063 old
= get_secdesc(cli
, filename
);
1069 oldattr
= get_fileinfo(cli
,filename
);
1071 if (strcmp(type
,"allow")==0) {
1072 if ((old
->type
& SEC_DESC_DACL_PROTECTED
) ==
1073 SEC_DESC_DACL_PROTECTED
) {
1075 char *parentname
,*temp
;
1076 struct security_descriptor
*parent
;
1077 temp
= talloc_strdup(talloc_tos(), filename
);
1079 old
->type
=old
->type
& (~SEC_DESC_DACL_PROTECTED
);
1081 /* look at parent and copy in all its inheritable ACL's. */
1082 string_replace(temp
, '\\', '/');
1083 if (!parent_dirname(talloc_tos(),temp
,&parentname
,NULL
)) {
1086 string_replace(parentname
, '/', '\\');
1087 parent
= get_secdesc(cli
,parentname
);
1088 if (parent
== NULL
) {
1091 for (i
=0;i
<parent
->dacl
->num_aces
;i
++) {
1092 struct security_ace
*ace
=&parent
->dacl
->aces
[i
];
1093 /* Add inherited flag to all aces */
1094 ace
->flags
=ace
->flags
|
1095 SEC_ACE_FLAG_INHERITED_ACE
;
1096 if ((oldattr
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
) {
1097 if ((ace
->flags
& SEC_ACE_FLAG_CONTAINER_INHERIT
) ==
1098 SEC_ACE_FLAG_CONTAINER_INHERIT
) {
1099 add_ace(&old
->dacl
, ace
);
1102 if ((ace
->flags
& SEC_ACE_FLAG_OBJECT_INHERIT
) ==
1103 SEC_ACE_FLAG_OBJECT_INHERIT
) {
1104 /* clear flags for files */
1106 add_ace(&old
->dacl
, ace
);
1111 printf("Already set to inheritable permissions.\n");
1114 } else if (strcmp(type
,"remove")==0) {
1115 if ((old
->type
& SEC_DESC_DACL_PROTECTED
) !=
1116 SEC_DESC_DACL_PROTECTED
) {
1117 old
->type
=old
->type
| SEC_DESC_DACL_PROTECTED
;
1119 /* remove all inherited ACL's. */
1122 struct security_acl
*temp
=old
->dacl
;
1123 old
->dacl
=make_sec_acl(talloc_tos(), 3, 0, NULL
);
1124 for (i
=temp
->num_aces
-1;i
>=0;i
--) {
1125 struct security_ace
*ace
=&temp
->aces
[i
];
1126 /* Remove all ace with INHERITED flag set */
1127 if ((ace
->flags
& SEC_ACE_FLAG_INHERITED_ACE
) !=
1128 SEC_ACE_FLAG_INHERITED_ACE
) {
1129 add_ace(&old
->dacl
,ace
);
1134 printf("Already set to no inheritable permissions.\n");
1137 } else if (strcmp(type
,"copy")==0) {
1138 if ((old
->type
& SEC_DESC_DACL_PROTECTED
) !=
1139 SEC_DESC_DACL_PROTECTED
) {
1140 old
->type
=old
->type
| SEC_DESC_DACL_PROTECTED
;
1142 /* convert all inherited ACL's to non inherated ACL's. */
1145 for (i
=0;i
<old
->dacl
->num_aces
;i
++) {
1146 struct security_ace
*ace
=&old
->dacl
->aces
[i
];
1147 /* Remove INHERITED FLAG from all aces */
1148 ace
->flags
=ace
->flags
&(~SEC_ACE_FLAG_INHERITED_ACE
);
1152 printf("Already set to no inheritable permissions.\n");
1157 /* Denied ACE entries must come before allowed ones */
1158 sort_acl(old
->dacl
);
1160 sd
= make_sec_desc(talloc_tos(),old
->revision
, old
->type
,
1161 old
->owner_sid
, old
->group_sid
,
1162 NULL
, old
->dacl
, &sd_size
);
1164 if (!set_secdesc(cli
, filename
, sd
)) {
1165 result
= EXIT_FAILED
;
1171 /*****************************************************
1172 Return a connection to a server.
1173 *******************************************************/
1174 static struct cli_state
*connect_one(struct user_auth_info
*auth_info
,
1175 const char *server
, const char *share
)
1177 struct cli_state
*c
= NULL
;
1181 if (get_cmdline_auth_info_use_kerberos(auth_info
)) {
1182 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
|
1183 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
1186 if (get_cmdline_auth_info_use_machine_account(auth_info
) &&
1187 !set_cmdline_auth_info_machine_account_creds(auth_info
)) {
1191 set_cmdline_auth_info_getpass(auth_info
);
1193 nt_status
= cli_full_connection(&c
, lp_netbios_name(), server
,
1196 get_cmdline_auth_info_username(auth_info
),
1198 get_cmdline_auth_info_password(auth_info
),
1200 get_cmdline_auth_info_signing_state(auth_info
));
1201 if (!NT_STATUS_IS_OK(nt_status
)) {
1202 DEBUG(0,("cli_full_connection failed! (%s)\n", nt_errstr(nt_status
)));
1206 if (get_cmdline_auth_info_smb_encrypt(auth_info
)) {
1207 nt_status
= cli_cm_force_encryption(c
,
1208 get_cmdline_auth_info_username(auth_info
),
1209 get_cmdline_auth_info_password(auth_info
),
1212 if (!NT_STATUS_IS_OK(nt_status
)) {
1221 /****************************************************************************
1223 ****************************************************************************/
1224 int main(int argc
, const char *argv
[])
1228 enum acl_mode mode
= SMB_ACL_SET
;
1229 static char *the_acl
= NULL
;
1230 enum chown_mode change_mode
= REQUEST_NONE
;
1233 char *filename
= NULL
;
1235 struct poptOption long_options
[] = {
1237 { "delete", 'D', POPT_ARG_STRING
, NULL
, 'D', "Delete an acl", "ACL" },
1238 { "modify", 'M', POPT_ARG_STRING
, NULL
, 'M', "Modify an acl", "ACL" },
1239 { "add", 'a', POPT_ARG_STRING
, NULL
, 'a', "Add an acl", "ACL" },
1240 { "set", 'S', POPT_ARG_STRING
, NULL
, 'S', "Set acls", "ACLS" },
1241 { "chown", 'C', POPT_ARG_STRING
, NULL
, 'C', "Change ownership of a file", "USERNAME" },
1242 { "chgrp", 'G', POPT_ARG_STRING
, NULL
, 'G', "Change group ownership of a file", "GROUPNAME" },
1243 { "inherit", 'I', POPT_ARG_STRING
, NULL
, 'I', "Inherit allow|remove|copy" },
1244 { "numeric", 0, POPT_ARG_NONE
, &numeric
, 1, "Don't resolve sids or masks to names" },
1245 { "sddl", 0, POPT_ARG_NONE
, &sddl
, 1, "Output and input acls in sddl format" },
1246 { "test-args", 't', POPT_ARG_NONE
, &test_args
, 1, "Test arguments"},
1248 POPT_COMMON_CONNECTION
1249 POPT_COMMON_CREDENTIALS
1253 struct cli_state
*cli
;
1254 TALLOC_CTX
*frame
= talloc_stackframe();
1255 const char *owner_username
= "";
1257 struct user_auth_info
*auth_info
;
1261 /* set default debug level to 1 regardless of what smb.conf sets */
1262 setup_logging( "smbcacls", DEBUG_STDERR
);
1263 lp_set_cmdline("log level", "1");
1267 lp_load_global(get_dyn_CONFIGFILE());
1270 auth_info
= user_auth_info_init(frame
);
1271 if (auth_info
== NULL
) {
1274 popt_common_set_auth_info(auth_info
);
1276 pc
= poptGetContext("smbcacls", argc
, argv
, long_options
, 0);
1278 poptSetOtherOptionHelp(pc
, "//server1/share1 filename\nACLs look like: "
1279 "'ACL:user:[ALLOWED|DENIED]/flags/permissions'");
1281 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1284 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
1289 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
1290 mode
= SMB_ACL_DELETE
;
1294 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
1295 mode
= SMB_ACL_MODIFY
;
1299 the_acl
= smb_xstrdup(poptGetOptArg(pc
));
1304 owner_username
= poptGetOptArg(pc
);
1305 change_mode
= REQUEST_CHOWN
;
1309 owner_username
= poptGetOptArg(pc
);
1310 change_mode
= REQUEST_CHGRP
;
1314 owner_username
= poptGetOptArg(pc
);
1315 change_mode
= REQUEST_INHERIT
;
1320 /* Make connection to server */
1321 if(!poptPeekArg(pc
)) {
1322 poptPrintUsage(pc
, stderr
, 0);
1326 path
= talloc_strdup(frame
, poptGetArg(pc
));
1331 if(!poptPeekArg(pc
)) {
1332 poptPrintUsage(pc
, stderr
, 0);
1336 filename
= talloc_strdup(frame
, poptGetArg(pc
));
1341 string_replace(path
,'/','\\');
1343 server
= talloc_strdup(frame
, path
+2);
1347 share
= strchr_m(server
,'\\');
1349 printf("Invalid argument: %s\n", share
);
1357 cli
= connect_one(auth_info
, server
, share
);
1365 string_replace(filename
, '/', '\\');
1366 if (filename
[0] != '\\') {
1367 filename
= talloc_asprintf(frame
,
1375 /* Perform requested action */
1377 if (change_mode
== REQUEST_INHERIT
) {
1378 result
= inherit(cli
, filename
, owner_username
);
1379 } else if (change_mode
!= REQUEST_NONE
) {
1380 result
= owner_set(cli
, change_mode
, filename
, owner_username
);
1381 } else if (the_acl
) {
1382 result
= cacl_set(cli
, filename
, the_acl
, mode
);
1384 result
= cacl_dump(cli
, filename
);